Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenrik Rentz-Reichert2011-03-15 21:41:36 +0000
committerHenrik Rentz-Reichert2011-03-15 21:41:36 +0000
commit2cd39c7889f47c68514aac54de82836587fde389 (patch)
tree310483556f539d3a4f397e7394741c6936a67253
parent226bef496139eda78cc59238dcc5455a1fea2c0e (diff)
downloadorg.eclipse.etrice-2cd39c7889f47c68514aac54de82836587fde389.tar.gz
org.eclipse.etrice-2cd39c7889f47c68514aac54de82836587fde389.tar.xz
org.eclipse.etrice-2cd39c7889f47c68514aac54de82836587fde389.zip
ui.commands: re-factored commands to avoid redundancy
-rw-r--r--plugins/org.eclipse.etrice.ui.commands/plugin.xml70
-rw-r--r--plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/AbstractEditHandler.java111
-rw-r--r--plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditModelHandler.java (renamed from plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenModelHandler.java)4
-rw-r--r--plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenBehaviorHandler.java70
-rw-r--r--plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenStructureHandler.java68
5 files changed, 103 insertions, 220 deletions
diff --git a/plugins/org.eclipse.etrice.ui.commands/plugin.xml b/plugins/org.eclipse.etrice.ui.commands/plugin.xml
index 018f81dee..cf0a1c902 100644
--- a/plugins/org.eclipse.etrice.ui.commands/plugin.xml
+++ b/plugins/org.eclipse.etrice.ui.commands/plugin.xml
@@ -9,19 +9,9 @@
id="org.eclipse.etrice.ui.commands.category">
</category>
<command
- name="Open Behavior"
+ name="open associated textual model"
categoryId="org.eclipse.etrice.ui.commands.category"
- id="org.eclipse.etrice.ui.commands.commands.openBehavior">
- </command>
- <command
- name="Open Structure"
- categoryId="org.eclipse.etrice.ui.commands.category"
- id="org.eclipse.etrice.ui.commands.commands.openStructure">
- </command>
- <command
- name="Open Model"
- categoryId="org.eclipse.etrice.ui.commands.category"
- id="org.eclipse.etrice.ui.commands.commands.openModel">
+ id="org.eclipse.etrice.ui.commands.commands.editModel">
</command>
<command
description="open this ActorClass&apos;s structure as diagram"
@@ -37,8 +27,8 @@
<extension
point="org.eclipse.ui.handlers">
<handler
- commandId="org.eclipse.etrice.ui.commands.commands.openBehavior"
- class="org.eclipse.etrice.ui.commands.handlers.OpenBehaviorHandler">
+ commandId="org.eclipse.etrice.core.room.ui.editBehavior"
+ class="org.eclipse.etrice.ui.commands.handlers.EditBehaviorHandler">
<activeWhen>
<with variable="activeEditorId">
<or>
@@ -53,8 +43,8 @@
</activeWhen>
</handler>
<handler
- commandId="org.eclipse.etrice.ui.commands.commands.openStructure"
- class="org.eclipse.etrice.ui.commands.handlers.OpenStructureHandler">
+ commandId="org.eclipse.etrice.core.room.ui.editStructure"
+ class="org.eclipse.etrice.ui.commands.handlers.EditStructureHandler">
<activeWhen>
<with
variable="activeEditorId">
@@ -70,8 +60,8 @@
</activeWhen>
</handler>
<handler
- commandId="org.eclipse.etrice.ui.commands.commands.openModel"
- class="org.eclipse.etrice.ui.commands.handlers.OpenModelHandler">
+ commandId="org.eclipse.etrice.ui.commands.commands.editModel"
+ class="org.eclipse.etrice.ui.commands.handlers.EditModelHandler">
<activeWhen>
<or>
<with
@@ -89,31 +79,23 @@
</or>
</activeWhen>
</handler>
- <handler
- class="org.eclipse.etrice.ui.commands.handlers.EditBehaviorHandler"
- commandId="org.eclipse.etrice.core.room.ui.editBehavior">
- </handler>
- <handler
- class="org.eclipse.etrice.ui.commands.handlers.EditStructureHandler"
- commandId="org.eclipse.etrice.core.room.ui.editStructure">
- </handler>
</extension>
<extension
point="org.eclipse.ui.bindings">
<key
- commandId="org.eclipse.etrice.ui.commands.commands.openBehavior"
+ commandId="org.eclipse.etrice.core.room.ui.editBehavior"
contextId="org.eclipse.ui.contexts.window"
sequence="M3+B"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
</key>
<key
- commandId="org.eclipse.etrice.ui.commands.commands.openStructure"
+ commandId="org.eclipse.etrice.core.room.ui.editStructure"
contextId="org.eclipse.ui.contexts.window"
sequence="M3+S"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
</key>
<key
- commandId="org.eclipse.etrice.ui.commands.commands.openModel"
+ commandId="org.eclipse.etrice.ui.commands.commands.editModel"
contextId="org.eclipse.ui.contexts.window"
sequence="M3+M"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
@@ -128,21 +110,21 @@
mnemonic="T"
id="org.eclipse.etrice.ui.commands.menus.eTrice">
<command
- commandId="org.eclipse.etrice.ui.commands.commands.openBehavior"
+ commandId="org.eclipse.etrice.core.room.ui.editBehavior"
icon="icons/Behavior.gif"
- id="org.eclipse.etrice.ui.commands.menus.openBehavior"
+ id="org.eclipse.etrice.ui.commands.menus.editBehavior"
label="Open Behavior"
mnemonic="B">
</command>
<command
- commandId="org.eclipse.etrice.ui.commands.commands.openStructure"
+ commandId="org.eclipse.etrice.core.room.ui.editStructure"
icon="icons/Structure.gif"
- id="org.eclipse.etrice.ui.commands.menus.openStructure"
+ id="org.eclipse.etrice.ui.commands.menus.editStructure"
label="Open Structure"
mnemonic="S">
</command>
<command
- commandId="org.eclipse.etrice.ui.commands.commands.openModel"
+ commandId="org.eclipse.etrice.ui.commands.commands.editModel"
icon="icons/RoomModel.gif"
id="org.eclipse.etrice.ui.commands.menus.openModel"
label="Open Model"
@@ -191,26 +173,6 @@
</visibleWhen>
</command>
</menuContribution>
- <!--
- <menuContribution
- locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
- <toolbar
- id="org.eclipse.etrice.ui.commands.toolbars.eTriceToolbar">
- <command
- commandId="org.eclipse.etrice.ui.commands.commands.openBehavior"
- icon="icons/Behavior.gif"
- tooltip="Open Behavior Editor"
- id="org.eclipse.etrice.ui.commands.toolbars.openBehavior">
- </command>
- <command
- commandId="org.eclipse.etrice.ui.commands.commands.openStructure"
- icon="icons/Structure.gif"
- tooltip="Open Structure Editor"
- id="org.eclipse.etrice.ui.commands.toolbars.openStructure">
- </command>
- </toolbar>
- </menuContribution>
- -->
</extension>
</plugin>
diff --git a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/AbstractEditHandler.java b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/AbstractEditHandler.java
index 57b82b1d9..c34418cdc 100644
--- a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/AbstractEditHandler.java
+++ b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/AbstractEditHandler.java
@@ -14,11 +14,20 @@ import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.ecore.EObject;
+import org.eclipse.etrice.core.room.ActorClass;
+import org.eclipse.etrice.core.room.StructureClass;
import org.eclipse.etrice.core.ui.RoomUiModule;
+import org.eclipse.etrice.ui.behavior.editor.BehaviorEditor;
+import org.eclipse.etrice.ui.structure.editor.StructureEditor;
import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
+import org.eclipse.xtext.resource.IFragmentProvider;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
@@ -43,6 +52,8 @@ public abstract class AbstractEditHandler extends AbstractHandler {
@Inject
protected IResourceValidator resourceValidator;
+ @Inject IFragmentProvider fragmentProvider;
+
public AbstractEditHandler() {
super();
@@ -55,42 +66,90 @@ public abstract class AbstractEditHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
- ISelection selection = HandlerUtil.getCurrentSelection(event);
- if (selection instanceof IStructuredSelection) {
- IStructuredSelection ss = (IStructuredSelection) selection;
- Object sel = ss.getFirstElement();
- if (sel instanceof ContentOutlineNode) {
- XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
- IXtextDocument document = xtextEditor.getDocument();
- if (hasIssues(document, new NullProgressMonitor())) {
- MessageDialog.openError(xtextEditor.getSite().getShell(), "Validation Errors", "The editor has validation errors.\nCannot open diagram!");
- return null;
- }
- if (xtextEditor.isDirty()) {
- if (!MessageDialog.openQuestion(xtextEditor.getSite().getShell(), "Save model file", "The editor will be saved before opening the diagram editor.\nProceed?"))
- return null;
- // postpone save to avoid doing it twice
- }
- final String fragment = ((ContentOutlineNode) sel).getURI().fragment();
- if (!prepare(xtextEditor, fragment))
- return null;
- if (xtextEditor.isDirty()) {
- xtextEditor.doSave(new NullProgressMonitor());
+ IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
+ IEditorPart editor = window.getActivePage().getActiveEditor();
+ if (editor instanceof XtextEditor) {
+ ISelection selection = HandlerUtil.getCurrentSelection(event);
+ if (selection instanceof IStructuredSelection) {
+ // event from the xtext editor's outline view
+ IStructuredSelection ss = (IStructuredSelection) selection;
+ Object sel = ss.getFirstElement();
+ if (sel instanceof ContentOutlineNode) {
+ XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
+ IXtextDocument document = xtextEditor.getDocument();
+ final String fragment = ((ContentOutlineNode) sel).getURI().fragment();
+ if (checkPrerequisites(xtextEditor, document, fragment)) {
+ openEditor(document, fragment);
+ }
}
- document.readOnly(new IUnitOfWork.Void<XtextResource>() {
+ }
+ else if (selection instanceof ITextSelection) {
+ // event from the xtext editor itself
+ final ITextSelection ss = (ITextSelection) selection;
+ XtextEditor xed = (XtextEditor) editor;
+ IXtextDocument document = xed.getDocument();
+ String fragment = document.readOnly(new IUnitOfWork<String, XtextResource>() {
@Override
- public void process(XtextResource resource) throws Exception {
- if (resource != null) {
- EObject object = resource.getEObject(fragment);
- openEditor(object);
+ public String exec(XtextResource resource) throws Exception {
+ EObject obj = EObjectAtOffsetHelper.resolveElementAt(resource, ss.getOffset(), null);
+ while (obj!=null) {
+ if (obj instanceof ActorClass) {
+ return fragmentProvider.getFragment(obj, null);
+ }
+ obj = obj.eContainer();
}
+ return "";
}
});
+ if (checkPrerequisites(xed, document, fragment)) {
+ openEditor(document, fragment);
+ }
}
}
+ else if (editor instanceof StructureEditor) {
+ StructureClass sc = ((StructureEditor)editor).getStructureClass();
+ if (sc instanceof ActorClass) {
+ openEditor(sc);
+ }
+ }
+ else if (editor instanceof BehaviorEditor) {
+ ActorClass ac = ((BehaviorEditor)editor).getActorClass();
+ openEditor(ac);
+ }
return null;
}
+ protected void openEditor(IXtextDocument document, final String fragment) {
+ document.readOnly(new IUnitOfWork.Void<XtextResource>() {
+ @Override
+ public void process(XtextResource resource) throws Exception {
+ if (resource != null) {
+ EObject object = resource.getEObject(fragment);
+ openEditor(object);
+ }
+ }
+ });
+ }
+
+ protected boolean checkPrerequisites(XtextEditor xtextEditor,
+ IXtextDocument document, final String fragment) {
+ if (hasIssues(document, new NullProgressMonitor())) {
+ MessageDialog.openError(xtextEditor.getSite().getShell(), "Validation Errors", "The editor has validation errors.\nCannot open diagram!");
+ return false;
+ }
+ if (xtextEditor.isDirty()) {
+ if (!MessageDialog.openQuestion(xtextEditor.getSite().getShell(), "Save model file", "The editor will be saved before opening the diagram editor.\nProceed?"))
+ return false;
+ // postpone save to avoid doing it twice
+ }
+ if (!prepare(xtextEditor, fragment))
+ return false;
+ if (xtextEditor.isDirty()) {
+ xtextEditor.doSave(new NullProgressMonitor());
+ }
+ return true;
+ }
+
public boolean hasIssues(IXtextDocument xtextDocument, final IProgressMonitor monitor) {
final boolean issues = xtextDocument
.readOnly(new IUnitOfWork<Boolean, XtextResource>() {
diff --git a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenModelHandler.java b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditModelHandler.java
index 769e8b231..15c73b2a4 100644
--- a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenModelHandler.java
+++ b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditModelHandler.java
@@ -24,7 +24,7 @@ import com.google.inject.Injector;
* @see org.eclipse.core.commands.IHandler
* @see org.eclipse.core.commands.AbstractHandler
*/
-public class OpenModelHandler extends AbstractHandler {
+public class EditModelHandler extends AbstractHandler {
@Inject
ILocationInFileProvider locationProvider;
@@ -32,7 +32,7 @@ public class OpenModelHandler extends AbstractHandler {
/**
* The constructor.
*/
- public OpenModelHandler() {
+ public EditModelHandler() {
Injector injector = RoomUiModule.getInjector();
injector.injectMembers(this);
}
diff --git a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenBehaviorHandler.java b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenBehaviorHandler.java
deleted file mode 100644
index 432e4c358..000000000
--- a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenBehaviorHandler.java
+++ /dev/null
@@ -1,70 +0,0 @@
-package org.eclipse.etrice.ui.commands.handlers;
-
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.etrice.core.room.ActorClass;
-import org.eclipse.etrice.core.room.StructureClass;
-import org.eclipse.etrice.ui.behavior.DiagramAccess;
-import org.eclipse.etrice.ui.structure.editor.StructureEditor;
-import org.eclipse.jface.text.ITextSelection;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.handlers.HandlerUtil;
-import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
-import org.eclipse.xtext.resource.XtextResource;
-import org.eclipse.xtext.ui.editor.XtextEditor;
-import org.eclipse.xtext.util.concurrent.IUnitOfWork;
-
-/**
- * Our sample handler extends AbstractHandler, an IHandler base class.
- * @see org.eclipse.core.commands.IHandler
- * @see org.eclipse.core.commands.AbstractHandler
- */
-public class OpenBehaviorHandler extends AbstractHandler {
- /**
- * The constructor.
- */
- public OpenBehaviorHandler() {
- }
-
- /**
- * the command has been executed, so extract extract the needed information
- * from the application context.
- */
- public Object execute(ExecutionEvent event) throws ExecutionException {
- IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
- IEditorPart editor = window.getActivePage().getActiveEditor();
- if (editor instanceof StructureEditor) {
- StructureClass sc = ((StructureEditor)editor).getStructureClass();
- if (sc instanceof ActorClass) {
- DiagramAccess da = new DiagramAccess();
- da.openDiagramEditor(sc);
- }
- }
- else if (editor instanceof XtextEditor) {
- XtextEditor xed = (XtextEditor) editor;
- ISelection selection = xed.getSelectionProvider().getSelection();
- if (selection instanceof ITextSelection) {
- final ITextSelection ss = (ITextSelection) selection;
- xed.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
- @Override
- public void process(XtextResource resource) throws Exception {
- EObject obj = EObjectAtOffsetHelper.resolveElementAt(resource, ss.getOffset(), null);
- while (obj!=null) {
- if (obj instanceof ActorClass) {
- DiagramAccess da = new DiagramAccess();
- da.openDiagramEditor((ActorClass)obj);
- break;
- }
- obj = obj.eContainer();
- }
- }
- });
- }
- }
- return null;
- }
-}
diff --git a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenStructureHandler.java b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenStructureHandler.java
deleted file mode 100644
index 0365adb15..000000000
--- a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/OpenStructureHandler.java
+++ /dev/null
@@ -1,68 +0,0 @@
-package org.eclipse.etrice.ui.commands.handlers;
-
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.etrice.core.room.ActorClass;
-import org.eclipse.etrice.core.room.StructureClass;
-import org.eclipse.etrice.ui.structure.DiagramAccess;
-import org.eclipse.etrice.ui.behavior.editor.BehaviorEditor;
-import org.eclipse.jface.text.ITextSelection;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.handlers.HandlerUtil;
-import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
-import org.eclipse.xtext.resource.XtextResource;
-import org.eclipse.xtext.ui.editor.XtextEditor;
-import org.eclipse.xtext.util.concurrent.IUnitOfWork;
-
-/**
- * Our sample handler extends AbstractHandler, an IHandler base class.
- * @see org.eclipse.core.commands.IHandler
- * @see org.eclipse.core.commands.AbstractHandler
- */
-public class OpenStructureHandler extends AbstractHandler {
- /**
- * The constructor.
- */
- public OpenStructureHandler() {
- }
-
- /**
- * the command has been executed, so extract extract the needed information
- * from the application context.
- */
- public Object execute(ExecutionEvent event) throws ExecutionException {
- IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
- IEditorPart editor = window.getActivePage().getActiveEditor();
- if (editor instanceof BehaviorEditor) {
- ActorClass ac = ((BehaviorEditor)editor).getActorClass();
- DiagramAccess da = new DiagramAccess();
- da.openDiagramEditor(ac);
- }
- else if (editor instanceof XtextEditor) {
- XtextEditor xed = (XtextEditor) editor;
- ISelection selection = xed.getSelectionProvider().getSelection();
- if (selection instanceof ITextSelection) {
- final ITextSelection ss = (ITextSelection) selection;
- xed.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
- @Override
- public void process(XtextResource resource) throws Exception {
- EObject obj = EObjectAtOffsetHelper.resolveElementAt(resource, ss.getOffset(), null);
- while (obj!=null) {
- if (obj instanceof StructureClass) {
- DiagramAccess da = new DiagramAccess();
- da.openDiagramEditor((StructureClass)obj);
- break;
- }
- obj = obj.eContainer();
- }
- }
- });
- }
- }
- return null;
- }
-}

Back to the top