Package org.gvt.action

Source Code of org.gvt.action.DeletePathwayAction

package org.gvt.action;

import org.biopax.paxtools.model.Model;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.widgets.MessageBox;
import org.gvt.ChisioMain;
import org.gvt.model.BioPAXGraph;
import org.gvt.model.CompoundModel;
import org.gvt.util.PathwayHolder;

import java.util.ArrayList;

/**
* Action for closing the currently open tab.
*
* @author Ozgun Babur
*
* Copyright: Bilkent Center for Bioinformatics, 2007 - present
*/
public class DeletePathwayAction extends Action
{
  private ChisioMain main;
  private boolean allOpenPathways;

  /**
   * Constructor
   */
  public DeletePathwayAction(ChisioMain main, boolean allOpenPathways)
  {
    super(allOpenPathways ? "Delete All Open Pathways" : "Delete Pathway");

    if (allOpenPathways)
    {
      setImageDescriptor(ImageDescriptor.createFromFile(ChisioMain.class, "icon/pathway-delete-all.png"));     
    }
    else
    {
      setImageDescriptor(ImageDescriptor.createFromFile(ChisioMain.class, "icon/pathway-delete.png"));
    }

    this.setToolTipText(getText());
    this.main = main;
    this.allOpenPathways = allOpenPathways;
  }

  public DeletePathwayAction(ChisioMain main)
  {
    this(main, false);
  }

  public void run()
  {
    CTabItem tab = main.getSelectedTab();

    if (tab != null)
    {
      String s = allOpenPathways ? "all open pathways" : "the pathway";
      MessageBox box = new MessageBox(main.getShell(), SWT.YES | SWT.NO | SWT.CANCEL);
      box.setText("Confirm");
      box.setMessage("Delete "+ s +"?\n" +
        "Contents will not be deleted from the model!");

      int answer = box.open();
     
      if (answer == SWT.YES)
      {
        if (allOpenPathways)
        {
          for (CTabItem tabItem : new ArrayList<CTabItem>(
            main.getTabToViewerMap().keySet()))
          {
            deletePathway(tabItem);
          }
        }
        else
        {
          deletePathway(tab);
        }
      }
    }
  }

  private void deletePathway(CTabItem tab)
  {

    ScrollingGraphicalViewer viewer = main.getTabToViewerMap().get(tab);

    main.closeTab(tab, false);

    CompoundModel root = (CompoundModel) viewer.getContents().getModel();

    if (root instanceof BioPAXGraph)
    {
      BioPAXGraph graph = (BioPAXGraph) root;

      if (graph.isMechanistic())
      {
        graph.forgetLayout();
        Model model = graph.getBiopaxModel();
        PathwayHolder p = graph.getPathway();
        p.removeFromModel(model);

        main.getAllPathwayNames().remove(p.getName());
      }
    }
  }
}
TOP

Related Classes of org.gvt.action.DeletePathwayAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.