package org.eclipse.stem.ui.grapheditor.handlers; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.eclipse.core.commands.AbstractHandler; | |
import org.eclipse.core.commands.ExecutionEvent; | |
import org.eclipse.core.commands.ExecutionException; | |
import org.eclipse.core.commands.IHandler; | |
import org.eclipse.core.resources.IContainer; | |
import org.eclipse.core.resources.IProject; | |
import org.eclipse.core.resources.IResource; | |
import org.eclipse.core.resources.ResourcesPlugin; | |
import org.eclipse.core.runtime.Platform; | |
import org.eclipse.emf.common.util.EList; | |
import org.eclipse.emf.common.util.URI; | |
import org.eclipse.emf.edit.provider.DelegatingWrapperItemProvider; | |
import org.eclipse.jface.viewers.ISelection; | |
import org.eclipse.jface.viewers.StructuredSelection; | |
import org.eclipse.jface.viewers.TreePath; | |
import org.eclipse.jface.viewers.TreeSelection; | |
import org.eclipse.stem.adapters.file.File; | |
import org.eclipse.stem.core.Utility; | |
import org.eclipse.stem.core.common.Identifiable; | |
import org.eclipse.stem.core.graph.Graph; | |
import org.eclipse.stem.core.model.Model; | |
import org.eclipse.stem.core.model.impl.ModelImpl; | |
import org.eclipse.stem.ui.Activator; | |
import org.eclipse.stem.ui.grapheditor.GraphCanvas; | |
import org.eclipse.stem.ui.views.explorer.IdentifiableTreeNode; | |
import org.eclipse.ui.handlers.HandlerUtil; | |
public class CanonicalGraphDisplay extends AbstractHandler | |
implements IHandler { | |
IProject project; | |
public Object execute(final ExecutionEvent executionEvent) | |
throws ExecutionException { | |
final ISelection selection = HandlerUtil | |
.getCurrentSelectionChecked(executionEvent); | |
if(selection instanceof TreeSelection) { | |
TreeSelection ts = (TreeSelection)selection; | |
TreePath[] paths = ts.getPaths(); | |
if(paths.length > 0) { | |
TreePath parent = paths[0].getParentPath(); | |
Object projectSegment = parent.getFirstSegment(); | |
//Object parentSeg = parent.getLastSegment(); | |
if(projectSegment != null) { | |
project = null; | |
if(projectSegment instanceof IdentifiableTreeNode) | |
project =((IdentifiableTreeNode)projectSegment).getProject(); | |
if(projectSegment instanceof IProject) | |
project = (IProject)projectSegment; | |
} | |
} | |
} | |
// Structured Selection? | |
if (selection instanceof StructuredSelection) { | |
// Yes | |
// Iterate through everything that's in the selection and put each | |
// object into the appropriate collection. | |
for (final Object obj : ((StructuredSelection) selection).toList()) { | |
// IExecutable executable = (IExecutable) ExecutableAdapterFactory.INSTANCE | |
// .adapt(obj, IExecutable.class); | |
File file = (File) Platform | |
.getAdapterManager().getAdapter(obj, File.class); | |
if (obj instanceof ModelImpl) | |
{ | |
ModelImpl model = | |
(ModelImpl) Platform.getAdapterManager().getAdapter(obj, ModelImpl.class); | |
if (model != null) | |
{ | |
doit(model); | |
} | |
else | |
{ | |
// Internal error | |
Activator.logError( | |
"STEM Internal error display command applied to \"" | |
+ obj.getClass().getName() + "\"", null); | |
} | |
} | |
else if (obj instanceof DelegatingWrapperItemProvider) | |
{ | |
Identifiable retValue = null; | |
DelegatingWrapperItemProvider dWIP = (DelegatingWrapperItemProvider) obj; | |
if (dWIP.getValue() instanceof Identifiable) { | |
// Yes | |
retValue = (Identifiable) dWIP.getValue(); | |
} // if Identifiable | |
else { | |
// No | |
// Keep adapting... | |
retValue = (Identifiable) Platform.getAdapterManager() | |
.getAdapter(dWIP.getValue(), Identifiable.class); | |
} | |
ModelImpl model = | |
(ModelImpl) Platform.getAdapterManager().getAdapter(retValue, ModelImpl.class); | |
if (model != null) | |
{ | |
doit(model); | |
} | |
else | |
{ | |
// Internal error | |
Activator.logError( | |
"STEM Internal error display command applied to \"" | |
+ obj.getClass().getName() + "\"", null); | |
} | |
} | |
else if (obj instanceof org.eclipse.stem.ui.views.explorer.IdentifiableTreeNode) | |
{ | |
org.eclipse.stem.ui.views.explorer.IdentifiableTreeNode idTreeNode = | |
(org.eclipse.stem.ui.views.explorer.IdentifiableTreeNode) Platform. | |
getAdapterManager().getAdapter( | |
obj, org.eclipse.stem.ui.views.explorer.IdentifiableTreeNode.class); | |
// Were we successful in adapting? | |
if (idTreeNode != null) { | |
// Yes | |
doit(idTreeNode); | |
} // if | |
else { | |
// Internal error | |
Activator.logError( | |
"STEM Internal error display command applied to \"" | |
+ obj.getClass().getName() + "\"", null); | |
} | |
} | |
} // for each selection | |
} // if StructuredSelection | |
return null; | |
} | |
protected void doit(org.eclipse.stem.ui.views.explorer.IdentifiableTreeNode idTreeNode) | |
{ | |
List<Graph> graphList = new ArrayList<Graph>(); | |
if (project == null) project = idTreeNode.getProject(); | |
IContainer graphsFolder = project.getFolder("models"); //before: graphs | |
IResource[] graphs = null; | |
try { | |
graphs = graphsFolder.members(); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
if (graphs != null) { | |
for(IResource r:graphs) { | |
// ignore system files | |
if(r.getName().startsWith(".")) continue; | |
try { | |
URI uri = URI.createURI(r.getLocationURI().toString()); | |
Identifiable id = Utility.getIdentifiable(uri); | |
if (id instanceof Model) | |
{ | |
graphList = getGraphs(graphList, (Model)id); | |
} | |
else if (id instanceof Graph) graphList.add((Graph)id); | |
} catch(Exception e) { | |
// Skip bad file | |
} | |
} | |
} | |
//final Graph copyGraph = (Graph) EcoreUtil.copy(canonicalGraph); | |
if (graphList.size() != 0) { | |
GraphCanvas graphCanvas = new GraphCanvas(); | |
graphCanvas.display(graphList, null, project); | |
} | |
//update all models in the project that used this graph | |
//TODO | |
} //end doit with ExplorerTreeLeaf | |
protected void doit(Model model) | |
{ | |
List<Graph> graphList = new ArrayList<Graph>(); | |
graphList = getGraphs(graphList, model); | |
if (project == null) //should not happen... | |
{ | |
List<IProject> projects = org.eclipse.stem.ui.Utility.getSTEMProjectsFromWorkspace(ResourcesPlugin.getWorkspace()); | |
for (IProject stemProject : projects) | |
{ | |
//System.err.println("project: " + project.getName()+ " graph:" + stemGraph.getURI()); | |
//TODO contain is not enough, must be identical segment! | |
//note: when project cannot be found, it is a reference to STEM repository | |
if (model.getURI().toString().contains(stemProject.getName()) ) | |
{ | |
project = stemProject; | |
} | |
} | |
} | |
if (graphList.size() != 0) { | |
GraphCanvas graphCanvas = new GraphCanvas(); | |
graphCanvas.display(graphList, null, project, model); | |
} | |
} | |
protected List<Graph> getGraphs(List<Graph> parentgraphs, Model parentmodel) | |
{ | |
EList<Model> models = parentmodel.getModels(); | |
List<Graph> graphs = new ArrayList<Graph>(); | |
for (Model model : models) | |
{ | |
graphs.addAll(getGraphs(graphs, model)); | |
} | |
for (Graph graph : graphs) | |
{ | |
if (!parentgraphs.contains(graph)) | |
{ | |
parentgraphs.add(graph); | |
} | |
} | |
for (Graph graph : parentmodel.getGraphs()) | |
{ | |
if (!parentgraphs.contains(graph)) | |
{ | |
parentgraphs.add(graph); | |
} | |
} | |
//parentgraphs.addAll(graphs); | |
//parentgraphs.addAll(parentmodel.getGraphs()); | |
return parentgraphs; | |
} | |
} | |