Pārlūkot izejas kodu

SubmachineSelectionDialog and Question for adding Xtext-Nature if Diagram editor opened

benjamin.schwertfeger@gmail.com 13 gadi atpakaļ
vecāks
revīzija
156e8e8808

+ 3 - 1
plugins/org.yakindu.sct.ui.editor/META-INF/MANIFEST.MF

@@ -42,7 +42,9 @@ Require-Bundle: org.eclipse.core.runtime,
  org.yakindu.sct.ui,
  org.yakindu.sct.model.sgraph,
  org.yakindu.sct.model.sgraph.edit,
- org.yakindu.sct.model.sgraph.resource
+ org.yakindu.sct.model.sgraph.resource,
+ org.yakindu.sct.model.stext.ui;bundle-version="1.0.0",
+ org.eclipse.xtext.builder;bundle-version="2.0.1"
 Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
 Export-Package: org.yakindu.sct.ui.editor,

+ 71 - 65
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/dialogs/SelectSubmachineDialog.java

@@ -10,23 +10,16 @@
  */
 package org.yakindu.sct.ui.editor.dialogs;
 
-import java.io.IOException;
 import java.util.Collections;
+import java.util.List;
 
-import org.eclipse.core.resources.IFile;
-import org.eclipse.emf.common.ui.dialogs.WorkspaceResourceDialog;
-import org.eclipse.emf.common.util.URI;
 import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.emf.ecore.resource.Resource.Factory;
-import org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryImpl;
 import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.dialogs.IDialogSettings;
+import org.eclipse.jface.viewers.ILabelProvider;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.StructuredSelection;
-import org.eclipse.jface.viewers.Viewer;
-import org.eclipse.jface.viewers.ViewerFilter;
 import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
@@ -37,14 +30,20 @@ import org.eclipse.swt.widgets.Link;
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.dialogs.ElementListSelectionDialog;
 import org.eclipse.ui.internal.dialogs.NewWizard;
 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
-import org.eclipse.ui.model.WorkbenchContentProvider;
-import org.eclipse.ui.model.WorkbenchLabelProvider;
+import org.eclipse.xtext.resource.IEObjectDescription;
+import org.eclipse.xtext.scoping.IGlobalScopeProvider;
+import org.eclipse.xtext.scoping.IScope;
 import org.yakindu.sct.model.sgraph.SGraphPackage;
 import org.yakindu.sct.model.sgraph.Statechart;
+import org.yakindu.sct.model.stext.ui.internal.STextActivator;
 import org.yakindu.sct.ui.editor.StatechartImages;
 
+import com.google.common.base.Predicates;
+import com.google.common.collect.Iterables;
+
 /**
  * Basic resource selection dialog for Statecharts with a link that opens the
  * new project wizard.
@@ -53,31 +52,56 @@ import org.yakindu.sct.ui.editor.StatechartImages;
  * 
  */
 @SuppressWarnings("restriction")
-public class SelectSubmachineDialog extends WorkspaceResourceDialog {
+public class SelectSubmachineDialog extends ElementListSelectionDialog {
 
 	public final static int CLEAR_BUTTON = IDialogConstants.CLIENT_ID + 1;
 
-	private final ViewerFilter filter;
 
 	private boolean clearSelected = false;
 
-	public SelectSubmachineDialog(Shell parent, ViewerFilter filter) {
-		super(parent, new WorkbenchLabelProvider(),
-				new WorkbenchContentProvider());
-		this.filter = filter;
+	private EObject context;
+
+	public SelectSubmachineDialog(Shell parent) {
+		super(parent, getLabelProvider());
 		initDialog();
 	}
 
+	private static ILabelProvider getLabelProvider() {
+		return STextActivator.getInstance().getInjector()
+				.getInstance(ILabelProvider.class);
+	}
+
+	private static Object[] getStatemachines(EObject context) {
+		IGlobalScopeProvider scopeProvider = STextActivator.getInstance()
+				.getInjector().getInstance(IGlobalScopeProvider.class);
+		IScope scope = scopeProvider.getScope(context.eResource(),
+				SGraphPackage.Literals.STATE__SUBSTATECHART,
+				Predicates.<IEObjectDescription> alwaysTrue());
+		Iterable<IEObjectDescription> statecharts = scope.getAllElements();
+		return Iterables.toArray(statecharts, IEObjectDescription.class);
+	}
+
 	protected void initDialog() {
-		setAllowMultiple(false);
 		setTitle("Select Submachine");
 		setMessage("Select the Submachine to include include into the Submachine State.");
 		setImage(StatechartImages.LOGO.image());
-		addFilter(filter);
-		loadContents();
 		clearSelected = false;
 	}
 
+	@Override
+	public void setElements(Object[] elements) {
+		if (elements == null || elements.length != 1) {
+			throw new IllegalStateException("No element was given");
+		}
+		Object object = elements[0];
+		if (!(object instanceof EObject)) {
+			throw new IllegalStateException("The element must be an EObject");
+		}
+		EObject element = (EObject) object;
+		this.context = element;
+		super.setElements(getStatemachines(element));
+	}
+
 	@Override
 	protected void createButtonsForButtonBar(Composite parent) {
 		createButton(parent, CLEAR_BUTTON, "None", false);
@@ -94,37 +118,42 @@ public class SelectSubmachineDialog extends WorkspaceResourceDialog {
 		return composite;
 	}
 
+	@Override
+	protected List getInitialElementSelections() {
+		if (context instanceof Statechart) {
+			return Collections.singletonList(context);
+		}
+		return super.getInitialElementSelections();
+	}
+
 	private IStructuredSelection getSelection() {
-		return new StructuredSelection(getSelectedContainers());
+		Statechart selectedSubmachine = getSelectedSubmachine();
+		if (selectedSubmachine != null) {
+			return new StructuredSelection(selectedSubmachine);
+		} else {
+			return StructuredSelection.EMPTY;
+		}
+
 	}
 
 	public Statechart getSelectedSubmachine() {
 		Object[] result = getResult();
-		if (result.length == 1 && result[0] instanceof IFile) {
-			IFile selectedFile = (IFile) result[0];
-			return loadFromFile(selectedFile);
+		if (result != null && result.length == 1) {
+			Statechart statechart = null;
+			if (result[0] instanceof IEObjectDescription) {
+				statechart = (Statechart) ((IEObjectDescription) result[0])
+						.getEObjectOrProxy();
+			}
+			if (statechart != null && statechart.eIsProxy()) {
+				statechart = (Statechart) EcoreUtil
+						.resolve(statechart, context);
+				;
+			}
+			return statechart;
 		}
 		return null;
 	}
 
-	public static Statechart loadFromFile(IFile fileResource) {
-		URI platformURI = URI.createPlatformResourceURI(fileResource
-				.getFullPath().toString(), true);
-		Factory factory = ResourceFactoryRegistryImpl.INSTANCE
-				.getFactory(platformURI);
-		Resource resource = factory.createResource(platformURI);
-		if (resource == null)
-			return null;
-		try {
-			resource.load(Collections.emptyMap());
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-		Statechart statechart = (Statechart) EcoreUtil.getObjectByType(
-				resource.getContents(), SGraphPackage.Literals.STATECHART);
-		return statechart;
-	}
-
 	protected static class OpenCreationWizardListener implements Listener {
 
 		private final IStructuredSelection selection;
@@ -153,29 +182,6 @@ public class SelectSubmachineDialog extends WorkspaceResourceDialog {
 		}
 	}
 
-	public static class StatechartViewerFilter extends ViewerFilter {
-
-		private final String fileExtension;
-
-		public StatechartViewerFilter(String fileExtension) {
-			this.fileExtension = fileExtension;
-		}
-
-		public StatechartViewerFilter(EObject eobject) {
-			this.fileExtension = eobject.eResource().getURI().fileExtension();
-		}
-
-		@Override
-		public boolean select(Viewer viewer, Object parentElement,
-				Object element) {
-			if (element instanceof IFile) {
-				return ((IFile) element).getFileExtension().endsWith(
-						fileExtension);
-			}
-			return true;
-		}
-	}
-
 	@Override
 	protected void buttonPressed(int buttonId) {
 		if (buttonId == CLEAR_BUTTON) {

+ 2 - 3
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/edithelper/StateEditHelper.java

@@ -22,7 +22,6 @@ import org.yakindu.sct.model.sgraph.SGraphPackage;
 import org.yakindu.sct.model.sgraph.State;
 import org.yakindu.sct.model.sgraph.Statechart;
 import org.yakindu.sct.ui.editor.dialogs.SelectSubmachineDialog;
-import org.yakindu.sct.ui.editor.dialogs.SelectSubmachineDialog.StatechartViewerFilter;
 import org.yakindu.sct.ui.editor.editor.StatechartElementTypes;
 
 /**
@@ -52,8 +51,8 @@ public class StateEditHelper extends VertexEditHelper {
 	}
 
 	private ICommand createSubmachineStateCommand(ConfigureRequest req) {
-		SelectSubmachineDialog dialog = new SelectSubmachineDialog(new Shell(),
-				new StatechartViewerFilter(req.getElementToConfigure()));
+		SelectSubmachineDialog dialog = new SelectSubmachineDialog(new Shell());
+		dialog.setElements(new Object[] { req.getElementToConfigure() });
 		if (Dialog.OK == dialog.open()) {
 			Statechart selectedSubmachine = dialog.getSelectedSubmachine();
 			if (selectedSubmachine != null) {

+ 63 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/editor/StatechartDiagramEditor.java

@@ -11,6 +11,11 @@
 package org.yakindu.sct.ui.editor.editor;
 
 import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.draw2d.ConnectionLayer;
 import org.eclipse.draw2d.ViewportAwareConnectionLayerClippingStrategy;
 import org.eclipse.emf.transaction.ResourceSetChangeEvent;
@@ -21,6 +26,8 @@ import org.eclipse.gef.RootEditPart;
 import org.eclipse.gef.editparts.LayerManager;
 import org.eclipse.gmf.runtime.common.ui.services.marker.MarkerNavigationService;
 import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorSite;
@@ -28,6 +35,7 @@ import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.help.IWorkbenchHelpSystem;
 import org.eclipse.ui.ide.IGotoMarker;
+import org.eclipse.xtext.ui.XtextProjectHelper;
 import org.yakindu.sct.ui.editor.DiagramActivator;
 import org.yakindu.sct.ui.editor.breadcrumb.BreadcrumbDiagramEditor;
 import org.yakindu.sct.ui.editor.utils.IYakinduSctHelpContextIds;
@@ -60,6 +68,61 @@ public class StatechartDiagramEditor extends BreadcrumbDiagramEditor implements
 			throws PartInitException {
 		super.init(site, input);
 		getEditingDomain().addResourceSetListener(validationListener);
+		checkXtextNature();
+	}
+
+	private void checkXtextNature() {
+		IProject project = getEditorInput().getFile().getProject();
+		if (project != null && !XtextProjectHelper.hasNature(project)
+				&& project.isAccessible() && !project.isHidden()) {
+			String title = "Add Xtext Nature";
+			String message = "Do you want to add the Xtext nature to the project '"
+					+ project.getName() + "'?";
+
+			MessageDialog dialog = new MessageDialog(
+					getEditorSite().getShell(), title, null, message,
+					MessageDialog.QUESTION, new String[] {
+							IDialogConstants.YES_LABEL,
+							IDialogConstants.NO_LABEL,
+							IDialogConstants.CANCEL_LABEL }, 0);
+			int open = dialog.open();
+			if (open == 0) {
+				toggleNature(project);
+			}
+		}
+	}
+
+	public void toggleNature(IProject project) {
+		try {
+			IProjectDescription description = project.getDescription();
+			String[] natures = description.getNatureIds();
+
+			for (int i = 0; i < natures.length; ++i) {
+				if (XtextProjectHelper.NATURE_ID.equals(natures[i])) {
+					// Remove the nature
+					String[] newNatures = new String[natures.length - 1];
+					System.arraycopy(natures, 0, newNatures, 0, i);
+					System.arraycopy(natures, i + 1, newNatures, i,
+							natures.length - i - 1);
+					description.setNatureIds(newNatures);
+					project.setDescription(description, null);
+					return;
+				}
+			}
+
+			// Add the nature
+			String[] newNatures = new String[natures.length + 1];
+			System.arraycopy(natures, 0, newNatures, 0, natures.length);
+			newNatures[natures.length] = XtextProjectHelper.NATURE_ID;
+			description.setNatureIds(newNatures);
+			project.setDescription(description, null);
+		} catch (CoreException e) {
+			DiagramActivator
+					.getDefault()
+					.getLog()
+					.log(new Status(IStatus.ERROR, DiagramActivator.PLUGIN_ID,
+							"Xtext nature can't be added", e));
+		}
 	}
 
 	@Override

+ 29 - 6
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/propertysheets/SubmachineSelectionDialogPropertyDescriptor.java

@@ -30,11 +30,16 @@ import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.ui.forms.widgets.FormToolkit;
+import org.eclipse.xtext.naming.IQualifiedNameConverter;
+import org.eclipse.xtext.naming.IQualifiedNameProvider;
+import org.eclipse.xtext.naming.QualifiedName;
 import org.yakindu.sct.model.sgraph.SGraphPackage;
 import org.yakindu.sct.model.sgraph.State;
 import org.yakindu.sct.model.sgraph.Statechart;
+import org.yakindu.sct.model.stext.ui.internal.STextActivator;
 import org.yakindu.sct.ui.editor.dialogs.SelectSubmachineDialog;
-import org.yakindu.sct.ui.editor.dialogs.SelectSubmachineDialog.StatechartViewerFilter;
+
+import com.google.inject.Inject;
 
 import de.itemis.gmf.runtime.commons.properties.descriptors.IFormPropertyDescriptor;
 
@@ -55,27 +60,43 @@ public class SubmachineSelectionDialogPropertyDescriptor implements
 		}
 	}
 
+	@Inject
+	IQualifiedNameProvider nameProvider;
+	@Inject
+	IQualifiedNameConverter nameConverter;
 	private State state;
 	private Label label;
 	private UpdateLabelAdapter updateLabelAdapter;
+	private EObject context;
+
+	public SubmachineSelectionDialogPropertyDescriptor() {
+		STextActivator.getInstance().getInjector().injectMembers(this);
+	}
 
 	public void updateModelBinding(EObject eObject) {
 		this.state = (State) eObject;
 		updateLabel(state);
+		context = eObject;
 		if (updateLabelAdapter == null) {
 			updateLabelAdapter = new UpdateLabelAdapter();
 			state.eAdapters().add(updateLabelAdapter);
 		}
-
 	}
 
 	private void updateLabel(State state) {
 		Statechart substatechart = state.getSubstatechart();
+		String labelText = "";
 		if (substatechart != null) {
-			label.setText(substatechart.eResource().getURI().toString());
-		} else {
-			label.setText("");
+			QualifiedName qualifiedName = nameProvider
+					.getFullyQualifiedName(substatechart);
+			if (qualifiedName != null) {
+				String text = nameConverter.toString(qualifiedName);
+				labelText = text;
+			} else {
+				labelText = "<UNRESOLVED>";
+			}
 		}
+		label.setText(labelText);
 	}
 
 	public void createLabelColumn(Composite parent) {
@@ -101,9 +122,11 @@ public class SubmachineSelectionDialogPropertyDescriptor implements
 				.applyTo(openDialog);
 		openDialog.setText("...");
 		openDialog.addListener(SWT.Selection, new Listener() {
+
 			public void handleEvent(Event event) {
 				SelectSubmachineDialog dialog = new SelectSubmachineDialog(
-						parent.getShell(), new StatechartViewerFilter(state));
+						parent.getShell());
+				dialog.setElements(new Object[] { context });
 				if (Dialog.OK == dialog.open()) {
 					Statechart selectedSubmachine = dialog
 							.getSelectedSubmachine();

+ 18 - 3
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/submachine/SubmachineDecorationProvider.java

@@ -14,8 +14,11 @@ import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.emf.common.util.EList;
 import org.eclipse.emf.common.util.URI;
 import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.gmf.runtime.diagram.ui.services.decorator.Decoration;
 import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorProvider;
 import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget;
@@ -25,6 +28,7 @@ import org.eclipse.gmf.runtime.notation.NotationPackage;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.xtext.EcoreUtil2;
 import org.yakindu.sct.model.sgraph.State;
+import org.yakindu.sct.model.sgraph.Statechart;
 import org.yakindu.sct.ui.editor.StatechartImages;
 import org.yakindu.sct.ui.editor.breadcrumb.BreadcrumbEditorUtil;
 
@@ -66,14 +70,25 @@ public class SubmachineDecorationProvider extends AbstractDecoratorProvider
 
 		@Override
 		protected Diagram getTooltipDiagramToRender(State state) {
-			return (Diagram) EcoreUtil2.getObjectByType(state
-					.getSubstatechart().eResource().getContents(),
+			Statechart substatechart = state.getSubstatechart();
+			if (substatechart == null) {
+				return null;
+			}
+			Resource eResource = substatechart.eResource();
+			if (eResource == null) {
+				return null;
+			}
+			EList<EObject> contents = eResource.getContents();
+			if (contents == null) {
+				return null;
+			}
+			return (Diagram) EcoreUtil2.getObjectByType(contents,
 					NotationPackage.Literals.DIAGRAM);
 		}
 
 		@Override
 		protected void mouseClicked(Decoration decoration, State semanticElement) {
-			URI uri = semanticElement.getSubstatechart().eResource().getURI();
+			URI uri = EcoreUtil.getURI(semanticElement.getSubstatechart());
 			IFile file = ResourcesPlugin.getWorkspace().getRoot()
 					.getFile(new Path(uri.toPlatformString(true)));
 			try {