Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenrik Rentz-Reichert2011-03-10 11:41:21 +0000
committerHenrik Rentz-Reichert2011-03-10 11:41:21 +0000
commit7f4850c74d207c03c5357134168ddc6ee3d647fa (patch)
tree12142f2e1a56e05121261e5203da72e5ef8da874 /plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline
parentd678ef3018aa292cc5d55d48f5560068765e544e (diff)
downloadorg.eclipse.etrice-7f4850c74d207c03c5357134168ddc6ee3d647fa.tar.gz
org.eclipse.etrice-7f4850c74d207c03c5357134168ddc6ee3d647fa.tar.xz
org.eclipse.etrice-7f4850c74d207c03c5357134168ddc6ee3d647fa.zip
core.room.ui: improved opening diagrams - validation and factored out
common code
Diffstat (limited to 'plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline')
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/AbstractEditHandler.java109
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/EditBehaviorHandler.java61
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/EditStructureHandler.java59
3 files changed, 141 insertions, 88 deletions
diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/AbstractEditHandler.java b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/AbstractEditHandler.java
new file mode 100644
index 000000000..f79c9beee
--- /dev/null
+++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/AbstractEditHandler.java
@@ -0,0 +1,109 @@
+/*******************************************************************************
+ * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.etrice.core.ui.outline;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+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.ui.internal.RoomActivator;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.xtext.resource.XtextResource;
+import org.eclipse.xtext.ui.editor.XtextEditor;
+import org.eclipse.xtext.ui.editor.model.IXtextDocument;
+import org.eclipse.xtext.ui.editor.outline.ContentOutlineNode;
+import org.eclipse.xtext.ui.editor.utils.EditorUtils;
+import org.eclipse.xtext.util.CancelIndicator;
+import org.eclipse.xtext.util.concurrent.IUnitOfWork;
+import org.eclipse.xtext.validation.CheckMode;
+import org.eclipse.xtext.validation.IResourceValidator;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+
+/**
+ * description
+ *
+ * @author Henrik Rentz-Reichert initial contribution and API
+ *
+ */
+public abstract class AbstractEditHandler extends AbstractHandler {
+
+ @Inject
+ protected IResourceValidator resourceValidator;
+
+ public AbstractEditHandler() {
+ super();
+
+ Injector injector = RoomActivator.getInstance().getInjector("org.eclipse.etrice.core.Room");
+ injector.injectMembers(this);
+ }
+
+ abstract protected boolean prepare(XtextEditor xtextEditor, final String fragment);
+ abstract protected void openEditor(EObject object);
+
+ @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());
+ }
+ document.readOnly(new IUnitOfWork.Void<XtextResource>() {
+ @Override
+ public void process(XtextResource resource) throws Exception {
+ if (resource != null) {
+ EObject object = resource.getEObject(fragment);
+ openEditor(object);
+ }
+ }
+ });
+ }
+ }
+ return null;
+ }
+
+ public boolean hasIssues(IXtextDocument xtextDocument, final IProgressMonitor monitor) {
+ final boolean issues = xtextDocument
+ .readOnly(new IUnitOfWork<Boolean, XtextResource>() {
+ public Boolean exec(XtextResource resource) throws Exception {
+ if (resource == null)
+ return false;
+ return !resourceValidator.validate(resource, CheckMode.NORMAL_AND_FAST, new CancelIndicator() {
+ public boolean isCanceled() {
+ return monitor.isCanceled();
+ }
+ }).isEmpty();
+ }
+ });
+ return issues;
+ }
+}
diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/EditBehaviorHandler.java b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/EditBehaviorHandler.java
index 24096bded..b25e1a7a3 100644
--- a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/EditBehaviorHandler.java
+++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/EditBehaviorHandler.java
@@ -12,9 +12,6 @@
package org.eclipse.etrice.core.ui.outline;
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.naming.RoomNameProvider;
@@ -25,15 +22,10 @@ import org.eclipse.etrice.core.room.Transition;
import org.eclipse.etrice.core.ui.internal.RoomActivator;
import org.eclipse.etrice.ui.behavior.DiagramAccess;
import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.ui.editor.model.edit.DefaultDocumentEditor;
-import org.eclipse.xtext.ui.editor.outline.ContentOutlineNode;
-import org.eclipse.xtext.ui.editor.utils.EditorUtils;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
import com.google.inject.Injector;
@@ -47,46 +39,16 @@ import com.google.inject.Injector;
* @author Henrik Rentz-Reichert initial contribution and API
*
*/
-public class EditBehaviorHandler extends AbstractHandler {
+public class EditBehaviorHandler extends AbstractEditHandler {
- @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) {
- final ContentOutlineNode node = (ContentOutlineNode) sel;
- XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
- 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
- }
- if (hasUnnamedTransitions(xtextEditor.getDocument(), node.getURI().fragment())) {
- if (!MessageDialog.openQuestion(xtextEditor.getSite().getShell(), "Create transition names", "Transition names will be created where missing.\nProceed?"))
- return null;
- createTransitionNames(xtextEditor.getDocument(), node.getURI().fragment());
- xtextEditor.doSave(new NullProgressMonitor());
- }
- if (xtextEditor.isDirty()) {
- xtextEditor.doSave(new NullProgressMonitor());
- }
- xtextEditor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
- @Override
- public void process(XtextResource resource) throws Exception {
- if (resource != null) {
- EObject object = resource.getEObject(node.getURI().fragment());
- if (object instanceof ActorClass) {
- DiagramAccess diagramAccess = new DiagramAccess();
- diagramAccess.openDiagramEditor((ActorClass) object);
- }
- }
- }
- });
- }
+ protected boolean prepare(XtextEditor xtextEditor, final String fragment) {
+ if (hasUnnamedTransitions(xtextEditor.getDocument(), fragment)) {
+ if (!MessageDialog.openQuestion(xtextEditor.getSite().getShell(), "Create transition names", "Transition names will be created where missing.\nProceed?"))
+ return false;
+ createTransitionNames(xtextEditor.getDocument(), fragment);
+ xtextEditor.doSave(new NullProgressMonitor());
}
- return null;
+ return true;
}
/**
@@ -163,4 +125,11 @@ public class EditBehaviorHandler extends AbstractHandler {
});
}
+ protected void openEditor(EObject object) {
+ if (object instanceof ActorClass) {
+ DiagramAccess diagramAccess = new DiagramAccess();
+ diagramAccess.openDiagramEditor((ActorClass) object);
+ }
+ }
+
}
diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/EditStructureHandler.java b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/EditStructureHandler.java
index 763dd86d5..dd1d915e0 100644
--- a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/EditStructureHandler.java
+++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/outline/EditStructureHandler.java
@@ -12,23 +12,10 @@
package org.eclipse.etrice.core.ui.outline;
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.ecore.EObject;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.ui.handlers.HandlerUtil;
-import org.eclipse.xtext.resource.XtextResource;
-import org.eclipse.xtext.ui.editor.XtextEditor;
-import org.eclipse.xtext.ui.editor.outline.ContentOutlineNode;
-import org.eclipse.xtext.ui.editor.utils.EditorUtils;
-import org.eclipse.xtext.util.concurrent.IUnitOfWork;
-
import org.eclipse.etrice.core.room.StructureClass;
import org.eclipse.etrice.ui.structure.DiagramAccess;
+import org.eclipse.xtext.ui.editor.XtextEditor;
/**
* Handler for outline menu item to open structure editor.
@@ -37,37 +24,25 @@ import org.eclipse.etrice.ui.structure.DiagramAccess;
* @author Henrik Rentz-Reichert initial contribution and API
*
*/
-public class EditStructureHandler extends AbstractHandler {
+public class EditStructureHandler extends AbstractEditHandler {
+
+ /* (non-Javadoc)
+ * @see org.eclipse.etrice.core.ui.outline.AbstractEditHandler#prepare(org.eclipse.xtext.ui.editor.XtextEditor, java.lang.String)
+ */
+ @Override
+ protected boolean prepare(XtextEditor xtextEditor, String fragment) {
+ return true;
+ }
+ /* (non-Javadoc)
+ * @see org.eclipse.etrice.core.ui.outline.AbstractEditHandler#openEditor(org.eclipse.emf.ecore.EObject)
+ */
@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) {
- final ContentOutlineNode node = (ContentOutlineNode) sel;
- XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
- 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;
- xtextEditor.doSave(new NullProgressMonitor());
- }
- xtextEditor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
- @Override
- public void process(XtextResource resource) throws Exception {
- if (resource != null) {
- EObject object = resource.getEObject(node.getURI().fragment());
- if (object instanceof StructureClass) {
- DiagramAccess diagramAccess = new DiagramAccess();
- diagramAccess.openDiagramEditor((StructureClass) object);
- }
- }
- }
- });
- }
+ protected void openEditor(EObject object) {
+ if (object instanceof StructureClass) {
+ DiagramAccess diagramAccess = new DiagramAccess();
+ diagramAccess.openDiagramEditor((StructureClass) object);
}
- return null;
}
}

Back to the top