package org.gvt.action;
import org.biopax.paxtools.model.Model;
import org.biopax.paxtools.model.level2.pathway;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.gvt.ChisioMain;
import org.gvt.inspector.ItemSelectionDialog;
import org.gvt.model.biopaxl2.BioPAXGraph;
import org.gvt.model.CompoundModel;
import org.patika.mada.util.XRef;
import java.util.*;
/**
* @author Ozgun Babur
*
* Copyright: Bilkent Center for Bioinformatics, 2007 - present
*/
public class OpenPathwaysAction extends Action
{
private ChisioMain main;
/**
* Pathways to open. If not supplied in constructor, then user is prompted with a dialog for
* selecting pathways to open.
*/
private List<String> pathways;
/**
* Objects in the opened view will be highlighted if this list is not null any of their
* references is in this list.
*/
private Collection<XRef> refsToHighlight;
/**
* Constructor
*/
public OpenPathwaysAction(ChisioMain main)
{
super("Open Pathway ...");
setToolTipText(getText());
setImageDescriptor(ImageDescriptor.createFromFile(ChisioMain.class, "icon/pathway-open.png"));
this.main = main;
}
public OpenPathwaysAction(ChisioMain main, List<String> pathways)
{
this(main);
this.pathways = pathways;
}
public void run()
{
Model model = main.getOwlModel();
if (model == null)
{
MessageDialog.openError(main.getShell(), "Error!",
"Load or query a BioPAX model first.");
return;
}
List<String> allNames = new ArrayList<String>(main.getAllPathwayNames());
Map<String, pathway> nameToPathwayMap = new HashMap<String, pathway>();
for (pathway p : model.getObjects(pathway.class))
{
if (!p.getPATHWAY_COMPONENTS().isEmpty())
{
nameToPathwayMap.put(p.getNAME(), p);
}
}
assert allNames.size() == nameToPathwayMap.size() : "Sizes do not match!";
if (allNames.isEmpty())
{
String message = model.getObjects().isEmpty() ? "BioPAX model is empty!" :
"There is no pathway defined in this BioPAX model.\n" +
"You can create one using \"Pathway > Create\" menu.";
MessageDialog.openInformation(main.getShell(), "No Pathway!", message);
return;
}
if (pathways != null)
{
pathways.retainAll(allNames);
}
else
{
pathways = new ArrayList<String>();
pathways.addAll(main.getOpenTabNames());
ItemSelectionDialog dialog = new ItemSelectionDialog(main.getShell(),
500,
"Pathway Selection Dialog",
"Select pathways to view",
allNames, pathways, true, true, null);
dialog.open();
if (dialog.isCancelled())
{
pathways = null;
return;
}
for (String name : new ArrayList<String>(main.getOpenTabNames()))
{
// Close pathways that user deselect
if (!pathways.contains(name))
{
main.closeTab(name, true);
}
// Remove names of already open pathways from selection
else
{
pathways.remove(name);
}
}
}
if (!pathways.isEmpty())
{
for (String name : pathways)
{
openPathwayInTab(nameToPathwayMap.get(name));
}
}
// Do not remmeber pathways the next time
pathways = null;
}
private void openPathwayInTab(pathway p)
{
assert p != null;
BioPAXGraph graph = main.getRootGraph().excise(p);
boolean layedout = graph.fetchLayout();
CTabItem tab = main.createNewTab(graph);
if (refsToHighlight != null && !refsToHighlight.isEmpty())
{
ScrollingGraphicalViewer viewer = main.getTabToViewerMap().get(tab);
CompoundModel root = (CompoundModel) viewer.getContents().getModel();
if (root instanceof BioPAXGraph)
{
new HighlightWithRefAction(main, (BioPAXGraph) root, refsToHighlight).run();
}
}
if (!layedout)
{
new CoSELayoutAction(main).run();
}
}
public Collection<XRef> getRefsToHighlight()
{
return refsToHighlight;
}
public void setRefsToHighlight(Collection<XRef> refsToHighlight)
{
this.refsToHighlight = refsToHighlight;
}
}