Eclipse-PyUML/pyUml/src/pyUML/actions/ManageViewsAction.java

93 lines
2.7 KiB
Java
Executable File

package pyUML.actions;
import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.uml2.uml.Model;
import org.python.pydev.navigator.elements.PythonFile;
import pyUML.backend.GlobalConstants;
import pyUML.views.ManageViewsPage;
public class ManageViewsAction implements IObjectActionDelegate{
IWorkbenchPart part;
ISelection selection;
IProject project;
/**
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart part) {
this.part = part;
}
/**
* Manual way to open a window to manage pyUML.views
* A project is needed to open a ManagViews Window
* @param project An eclipse project
*/
public static void run(IProject project) {
ManageViewsAction manageViews = new ManageViewsAction();
manageViews.project = project;
manageViews.run((IAction) null);
}
/**
* The run method used when the action is executed
* -> a new manage pyUML.views window is opened
*/
public void run(IAction action) {
try{
String umlFileName=project.getLocation().
append(GlobalConstants.getPyUmlDir()).
append(project.getName()+".uml").toOSString();
EObject diagramRoot = null;
try {
Resource resource = new ResourceSetImpl().getResource(URI.createFileURI(umlFileName), true);
diagramRoot = (EObject) resource.getContents().get(0);
} catch (WrappedException ex) {
MessageDialog.openError(null,"Error opening UML model",
"Unable to load model: " + umlFileName + "\n" +
"You must create a model before you can create pyUML.views!");
return;
}
Model model = (Model) diagramRoot;
ManageViewsPage page = new ManageViewsPage((Shell) null, model, project);
if (page != null)
page.open();
}catch (Throwable t) {
MessageDialog.openError(null,t.getClass().getName(),"Error Managing Views:\n"+t.getClass().getName()+"\n"+t.getMessage());
t.printStackTrace();
}
}
public void selectionChanged(IAction action, ISelection selection) {
if (selection instanceof TreeSelection) {
TreeSelection ts = (TreeSelection) selection;
Object selectedElement = ts.getFirstElement();
this.project = (IProject) selectedElement;
}
}
}