diff options
Diffstat (limited to 'plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers')
5 files changed, 745 insertions, 745 deletions
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 2885d44dd..e550b3899 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 @@ -1,242 +1,242 @@ -/*******************************************************************************
- * 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.ui.commands.handlers;
-
-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.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.IWorkbench;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.handlers.HandlerUtil;
-import org.eclipse.ui.views.contentoutline.ContentOutline;
-import org.eclipse.xtext.diagnostics.Severity;
-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;
-import org.eclipse.xtext.ui.editor.outline.impl.EObjectNode;
-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 org.eclipse.xtext.validation.Issue;
-
-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;
-
- @Inject
- IFragmentProvider fragmentProvider;
-
- private EObjectAtOffsetHelper helper = new EObjectAtOffsetHelper();
-
- public AbstractEditHandler() {
- super();
-
- Injector injector = RoomUiModule.getInjector();
- injector.injectMembers(this);
- }
-
- abstract protected boolean prepare(XtextEditor xtextEditor, final String fragment);
- abstract protected void openEditor(EObject object);
- abstract protected boolean isEnabled(String fragment);
-
- @Override
- public Object execute(ExecutionEvent event) throws ExecutionException {
- 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 EObjectNode) {
- XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
- IXtextDocument document = xtextEditor.getDocument();
- final String fragment = ((EObjectNode) sel).getEObjectURI().fragment();
- if (checkPrerequisites(xtextEditor, document, fragment)) {
- openEditor(document, fragment);
- }
- }
- }
- 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 String exec(XtextResource resource) throws Exception {
- EObject obj = helper.resolveElementAt(resource, ss.getOffset());
- while (obj!=null) {
- if (obj instanceof StructureClass) {
- return fragmentProvider.getFragment(obj, null);
- }
- obj = obj.eContainer();
- }
- return null;
- }
- });
- 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;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
- */
- @Override
- public boolean isEnabled() {
- IWorkbench wb = PlatformUI.getWorkbench();
- IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
- IWorkbenchPage page = win.getActivePage();
- IWorkbenchPart part = page.getActivePart();
- if (part instanceof ContentOutline) {
- ISelection selection = ((ContentOutline)part).getSelection();
- if (selection instanceof IStructuredSelection) {
- IStructuredSelection ss = (IStructuredSelection) selection;
- Object sel = ss.getFirstElement();
- if (sel instanceof EObjectNode) {
- EObjectNode node = (EObjectNode) sel;
- String fragment = node.getEObjectURI().fragment();
- return isEnabled(fragment);
- }
- }
- }
- IEditorPart editor = page.getActiveEditor();
- if (editor instanceof XtextEditor) {
- ISelection selection = ((XtextEditor) editor).getSelectionProvider().getSelection();
- 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 String exec(XtextResource resource) throws Exception {
- EObject obj = helper.resolveElementAt(resource, ss.getOffset());
- while (obj!=null) {
- if (obj instanceof StructureClass) {
- return fragmentProvider.getFragment(obj, null);
- }
- obj = obj.eContainer();
- }
- return "";
- }
- });
- return isEnabled(fragment);
- }
- }
- else if (editor instanceof StructureEditor) {
- StructureClass sc = ((StructureEditor)editor).getStructureClass();
- return isEnabled(fragmentProvider.getFragment(sc, null));
- }
- else if (editor instanceof BehaviorEditor) {
- ActorClass ac = ((BehaviorEditor)editor).getActorClass();
- return isEnabled(fragmentProvider.getFragment(ac, null));
- }
- return false;
- }
-
- 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>() {
- public Boolean exec(XtextResource resource) throws Exception {
- if (resource == null)
- return false;
- List<Issue> issueList = resourceValidator.validate(resource, CheckMode.NORMAL_AND_FAST, new CancelIndicator() {
- public boolean isCanceled() {
- return monitor.isCanceled();
- }
- });
- for (Issue issue : issueList) {
- if (issue.getSeverity()==Severity.ERROR)
- return true;
- }
- return false;
- }
- });
- return issues;
- }
-}
+/******************************************************************************* + * 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.ui.commands.handlers; + +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.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.IWorkbench; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.ui.views.contentoutline.ContentOutline; +import org.eclipse.xtext.diagnostics.Severity; +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; +import org.eclipse.xtext.ui.editor.outline.impl.EObjectNode; +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 org.eclipse.xtext.validation.Issue; + +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; + + @Inject + IFragmentProvider fragmentProvider; + + private EObjectAtOffsetHelper helper = new EObjectAtOffsetHelper(); + + public AbstractEditHandler() { + super(); + + Injector injector = RoomUiModule.getInjector(); + injector.injectMembers(this); + } + + abstract protected boolean prepare(XtextEditor xtextEditor, final String fragment); + abstract protected void openEditor(EObject object); + abstract protected boolean isEnabled(String fragment); + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + 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 EObjectNode) { + XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event); + IXtextDocument document = xtextEditor.getDocument(); + final String fragment = ((EObjectNode) sel).getEObjectURI().fragment(); + if (checkPrerequisites(xtextEditor, document, fragment)) { + openEditor(document, fragment); + } + } + } + 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 String exec(XtextResource resource) throws Exception { + EObject obj = helper.resolveElementAt(resource, ss.getOffset()); + while (obj!=null) { + if (obj instanceof StructureClass) { + return fragmentProvider.getFragment(obj, null); + } + obj = obj.eContainer(); + } + return null; + } + }); + 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; + } + + /* (non-Javadoc) + * @see org.eclipse.core.commands.AbstractHandler#isEnabled() + */ + @Override + public boolean isEnabled() { + IWorkbench wb = PlatformUI.getWorkbench(); + IWorkbenchWindow win = wb.getActiveWorkbenchWindow(); + IWorkbenchPage page = win.getActivePage(); + IWorkbenchPart part = page.getActivePart(); + if (part instanceof ContentOutline) { + ISelection selection = ((ContentOutline)part).getSelection(); + if (selection instanceof IStructuredSelection) { + IStructuredSelection ss = (IStructuredSelection) selection; + Object sel = ss.getFirstElement(); + if (sel instanceof EObjectNode) { + EObjectNode node = (EObjectNode) sel; + String fragment = node.getEObjectURI().fragment(); + return isEnabled(fragment); + } + } + } + IEditorPart editor = page.getActiveEditor(); + if (editor instanceof XtextEditor) { + ISelection selection = ((XtextEditor) editor).getSelectionProvider().getSelection(); + 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 String exec(XtextResource resource) throws Exception { + EObject obj = helper.resolveElementAt(resource, ss.getOffset()); + while (obj!=null) { + if (obj instanceof StructureClass) { + return fragmentProvider.getFragment(obj, null); + } + obj = obj.eContainer(); + } + return ""; + } + }); + return isEnabled(fragment); + } + } + else if (editor instanceof StructureEditor) { + StructureClass sc = ((StructureEditor)editor).getStructureClass(); + return isEnabled(fragmentProvider.getFragment(sc, null)); + } + else if (editor instanceof BehaviorEditor) { + ActorClass ac = ((BehaviorEditor)editor).getActorClass(); + return isEnabled(fragmentProvider.getFragment(ac, null)); + } + return false; + } + + 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>() { + public Boolean exec(XtextResource resource) throws Exception { + if (resource == null) + return false; + List<Issue> issueList = resourceValidator.validate(resource, CheckMode.NORMAL_AND_FAST, new CancelIndicator() { + public boolean isCanceled() { + return monitor.isCanceled(); + } + }); + for (Issue issue : issueList) { + if (issue.getSeverity()==Severity.ERROR) + return true; + } + return false; + } + }); + return issues; + } +} diff --git a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditBehaviorHandler.java b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditBehaviorHandler.java index 018303aa0..fa6eb1a2e 100644 --- a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditBehaviorHandler.java +++ b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditBehaviorHandler.java @@ -1,134 +1,134 @@ -/*******************************************************************************
- * Copyright (c) 2010 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
- *
- * CONTRIBUTORS:
- * Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
- *
- *******************************************************************************/
-
-package org.eclipse.etrice.ui.commands.handlers;
-
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.etrice.core.naming.RoomFragmentProvider;
-import org.eclipse.etrice.core.naming.RoomNameProvider;
-import org.eclipse.etrice.core.room.ActorClass;
-import org.eclipse.etrice.core.room.State;
-import org.eclipse.etrice.core.room.StateGraph;
-import org.eclipse.etrice.core.room.Transition;
-import org.eclipse.etrice.ui.behavior.DiagramAccess;
-import org.eclipse.jface.dialogs.MessageDialog;
-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.util.concurrent.IUnitOfWork;
-
-/**
- * Handler for outline menu item to open behavior editor.
- * The handler will ask to save unsaved editor before opening the diagram.
- *
- * If there are unnamed transitions they will be named with unique names.
- *
- * @author Henrik Rentz-Reichert initial contribution and API
- *
- */
-public class EditBehaviorHandler extends AbstractEditHandler {
-
- 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 true;
- }
-
- /**
- * @param document
- * @param fragment
- */
- private void createTransitionNames(IXtextDocument document, final String fragment) {
- document.modify(new IUnitOfWork.Void<XtextResource>() {
- @Override
- public void process(XtextResource resource) throws Exception {
- if (resource != null) {
- EObject object = resource.getEObject(fragment);
- if (object instanceof ActorClass) {
- createTransitionNames(((ActorClass) object).getStateMachine());
- }
- }
- }
-
- private void createTransitionNames(StateGraph sg) {
- if (sg==null)
- return;
-
- for (Transition tr : sg.getTransitions()) {
- if (tr.getName()==null || tr.getName().isEmpty()) {
- tr.setName(RoomNameProvider.getUniqueTransitionName(sg));
- }
- }
-
- for (State s : sg.getStates()) {
- createTransitionNames(s.getSubgraph());
- }
- }
-
- });
- }
-
- /**
- * @param document
- * @param fragment
- * @return
- */
- private boolean hasUnnamedTransitions(IXtextDocument document, final String fragment) {
- return document.readOnly(new IUnitOfWork<Boolean, XtextResource>() {
- @Override
- public Boolean exec(XtextResource resource) throws Exception {
- if (resource != null) {
- EObject object = resource.getEObject(fragment);
- if (object instanceof ActorClass) {
- return hasUnnamedTransitions(((ActorClass) object).getStateMachine());
- }
- }
- return false;
- }
-
- private boolean hasUnnamedTransitions(StateGraph sg) {
- if (sg==null)
- return false;
-
- for (Transition tr : sg.getTransitions()) {
- if (tr.getName()==null || tr.getName().isEmpty())
- return true;
- }
-
- for (State s : sg.getStates()) {
- if (hasUnnamedTransitions(s.getSubgraph()))
- return true;
- }
-
- return false;
- }
- });
- }
-
- protected void openEditor(EObject object) {
- if (object instanceof ActorClass) {
- DiagramAccess diagramAccess = new DiagramAccess();
- diagramAccess.openDiagramEditor((ActorClass) object);
- }
- }
-
- @Override
- protected boolean isEnabled(String fragment) {
- return RoomFragmentProvider.isActorClass(fragment);
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2010 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 + * + * CONTRIBUTORS: + * Thomas Schuetz and Henrik Rentz-Reichert (initial contribution) + * + *******************************************************************************/ + +package org.eclipse.etrice.ui.commands.handlers; + +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.etrice.core.naming.RoomFragmentProvider; +import org.eclipse.etrice.core.naming.RoomNameProvider; +import org.eclipse.etrice.core.room.ActorClass; +import org.eclipse.etrice.core.room.State; +import org.eclipse.etrice.core.room.StateGraph; +import org.eclipse.etrice.core.room.Transition; +import org.eclipse.etrice.ui.behavior.DiagramAccess; +import org.eclipse.jface.dialogs.MessageDialog; +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.util.concurrent.IUnitOfWork; + +/** + * Handler for outline menu item to open behavior editor. + * The handler will ask to save unsaved editor before opening the diagram. + * + * If there are unnamed transitions they will be named with unique names. + * + * @author Henrik Rentz-Reichert initial contribution and API + * + */ +public class EditBehaviorHandler extends AbstractEditHandler { + + 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 true; + } + + /** + * @param document + * @param fragment + */ + private void createTransitionNames(IXtextDocument document, final String fragment) { + document.modify(new IUnitOfWork.Void<XtextResource>() { + @Override + public void process(XtextResource resource) throws Exception { + if (resource != null) { + EObject object = resource.getEObject(fragment); + if (object instanceof ActorClass) { + createTransitionNames(((ActorClass) object).getStateMachine()); + } + } + } + + private void createTransitionNames(StateGraph sg) { + if (sg==null) + return; + + for (Transition tr : sg.getTransitions()) { + if (tr.getName()==null || tr.getName().isEmpty()) { + tr.setName(RoomNameProvider.getUniqueTransitionName(sg)); + } + } + + for (State s : sg.getStates()) { + createTransitionNames(s.getSubgraph()); + } + } + + }); + } + + /** + * @param document + * @param fragment + * @return + */ + private boolean hasUnnamedTransitions(IXtextDocument document, final String fragment) { + return document.readOnly(new IUnitOfWork<Boolean, XtextResource>() { + @Override + public Boolean exec(XtextResource resource) throws Exception { + if (resource != null) { + EObject object = resource.getEObject(fragment); + if (object instanceof ActorClass) { + return hasUnnamedTransitions(((ActorClass) object).getStateMachine()); + } + } + return false; + } + + private boolean hasUnnamedTransitions(StateGraph sg) { + if (sg==null) + return false; + + for (Transition tr : sg.getTransitions()) { + if (tr.getName()==null || tr.getName().isEmpty()) + return true; + } + + for (State s : sg.getStates()) { + if (hasUnnamedTransitions(s.getSubgraph())) + return true; + } + + return false; + } + }); + } + + protected void openEditor(EObject object) { + if (object instanceof ActorClass) { + DiagramAccess diagramAccess = new DiagramAccess(); + diagramAccess.openDiagramEditor((ActorClass) object); + } + } + + @Override + protected boolean isEnabled(String fragment) { + return RoomFragmentProvider.isActorClass(fragment); + } + +} diff --git a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditModelHandler.java b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditModelHandler.java index ae4ed8fcb..9480ffc30 100644 --- a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditModelHandler.java +++ b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditModelHandler.java @@ -1,70 +1,70 @@ -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.edit.ui.util.EditUIUtil;
-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.ui.IEditorPart;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.handlers.HandlerUtil;
-import org.eclipse.xtext.resource.ILocationInFileProvider;
-import org.eclipse.xtext.ui.editor.XtextEditor;
-import org.eclipse.xtext.util.ITextRegion;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-
-/**
- * Our sample handler extends AbstractHandler, an IHandler base class.
- * @see org.eclipse.core.commands.IHandler
- * @see org.eclipse.core.commands.AbstractHandler
- */
-public class EditModelHandler extends AbstractHandler {
-
- @Inject
- ILocationInFileProvider locationProvider;
-
- /**
- * The constructor.
- */
- public EditModelHandler() {
- Injector injector = RoomUiModule.getInjector();
- injector.injectMembers(this);
- }
-
- /**
- * 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();
- StructureClass sc = null;
- if (editor instanceof StructureEditor) {
- sc = ((StructureEditor)editor).getStructureClass();
- }
- else if (editor instanceof BehaviorEditor) {
- sc = ((BehaviorEditor)editor).getActorClass();
- }
- if (sc!=null) {
- try {
- if (EditUIUtil.openEditor(sc)) {
- editor = window.getActivePage().getActiveEditor();
- if (editor instanceof XtextEditor) {
- XtextEditor xed = (XtextEditor) editor;
- ITextRegion location = locationProvider.getFullTextRegion(sc);
- xed.selectAndReveal(location.getOffset(), location.getLength());
- }
- }
- } catch (PartInitException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
-}
+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.edit.ui.util.EditUIUtil; +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.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.xtext.resource.ILocationInFileProvider; +import org.eclipse.xtext.ui.editor.XtextEditor; +import org.eclipse.xtext.util.ITextRegion; + +import com.google.inject.Inject; +import com.google.inject.Injector; + +/** + * Our sample handler extends AbstractHandler, an IHandler base class. + * @see org.eclipse.core.commands.IHandler + * @see org.eclipse.core.commands.AbstractHandler + */ +public class EditModelHandler extends AbstractHandler { + + @Inject + ILocationInFileProvider locationProvider; + + /** + * The constructor. + */ + public EditModelHandler() { + Injector injector = RoomUiModule.getInjector(); + injector.injectMembers(this); + } + + /** + * 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(); + StructureClass sc = null; + if (editor instanceof StructureEditor) { + sc = ((StructureEditor)editor).getStructureClass(); + } + else if (editor instanceof BehaviorEditor) { + sc = ((BehaviorEditor)editor).getActorClass(); + } + if (sc!=null) { + try { + if (EditUIUtil.openEditor(sc)) { + editor = window.getActivePage().getActiveEditor(); + if (editor instanceof XtextEditor) { + XtextEditor xed = (XtextEditor) editor; + ITextRegion location = locationProvider.getFullTextRegion(sc); + xed.selectAndReveal(location.getOffset(), location.getLength()); + } + } + } catch (PartInitException e) { + e.printStackTrace(); + } + } + return null; + } +} diff --git a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditStructureHandler.java b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditStructureHandler.java index 31ce7123b..126ccd8a0 100644 --- a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditStructureHandler.java +++ b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/EditStructureHandler.java @@ -1,54 +1,54 @@ -/*******************************************************************************
- * Copyright (c) 2010 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
- *
- * CONTRIBUTORS:
- * Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
- *
- *******************************************************************************/
-
-package org.eclipse.etrice.ui.commands.handlers;
-
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.etrice.core.naming.RoomFragmentProvider;
-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.
- * The handler will ask to save unsaved editor before opening the diagram.
- *
- * @author Henrik Rentz-Reichert initial contribution and API
- *
- */
-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
- protected void openEditor(EObject object) {
- if (object instanceof StructureClass) {
- DiagramAccess diagramAccess = new DiagramAccess();
- diagramAccess.openDiagramEditor((StructureClass) object);
- }
- }
-
- @Override
- protected boolean isEnabled(String fragment) {
- return RoomFragmentProvider.isStructureClass(fragment);
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2010 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 + * + * CONTRIBUTORS: + * Thomas Schuetz and Henrik Rentz-Reichert (initial contribution) + * + *******************************************************************************/ + +package org.eclipse.etrice.ui.commands.handlers; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.etrice.core.naming.RoomFragmentProvider; +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. + * The handler will ask to save unsaved editor before opening the diagram. + * + * @author Henrik Rentz-Reichert initial contribution and API + * + */ +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 + protected void openEditor(EObject object) { + if (object instanceof StructureClass) { + DiagramAccess diagramAccess = new DiagramAccess(); + diagramAccess.openDiagramEditor((StructureClass) object); + } + } + + @Override + protected boolean isEnabled(String fragment) { + return RoomFragmentProvider.isStructureClass(fragment); + } + +} diff --git a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/ExportDiagramsHandler.java b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/ExportDiagramsHandler.java index 1448666de..bce9478dc 100644 --- a/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/ExportDiagramsHandler.java +++ b/plugins/org.eclipse.etrice.ui.commands/src/org/eclipse/etrice/ui/commands/handlers/ExportDiagramsHandler.java @@ -1,245 +1,245 @@ -/*******************************************************************************
- * Copyright (c) 2012 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
- *
- * CONTRIBUTORS:
- * Henrik Rentz-Reichert (initial contribution)
- *
- *******************************************************************************/
-
-package org.eclipse.etrice.ui.commands.handlers;
-
-import java.io.ByteArrayInputStream;
-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.resources.IContainer;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.Path;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.etrice.core.room.ActorClass;
-import org.eclipse.etrice.core.room.RoomModel;
-import org.eclipse.etrice.core.room.SubSystemClass;
-import org.eclipse.etrice.core.ui.RoomUiModule;
-import org.eclipse.etrice.ui.behavior.editor.BehaviorExporter;
-import org.eclipse.etrice.ui.common.Activator;
-import org.eclipse.etrice.ui.common.preferences.PreferenceConstants;
-import org.eclipse.etrice.ui.structure.editor.StructureExporter;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.preference.IPreferenceStore;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IWorkbench;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.handlers.HandlerUtil;
-import org.eclipse.ui.part.FileEditorInput;
-import org.eclipse.ui.views.contentoutline.ContentOutline;
-import org.eclipse.xtext.diagnostics.Severity;
-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.impl.EObjectNode;
-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 org.eclipse.xtext.validation.Issue;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-
-public class ExportDiagramsHandler extends AbstractHandler {
-
- @Inject
- protected IResourceValidator resourceValidator;
- private IPreferenceStore store;
-
- public ExportDiagramsHandler() {
- super();
-
- Injector injector = RoomUiModule.getInjector();
- injector.injectMembers(this);
-
- this.store = Activator.getDefault().getPreferenceStore();
- }
-
- // TODO: code copied from org.eclipse.etrice.ui.commands.handlers.AbstractEditHandler - refactor
- @Override
- public Object execute(ExecutionEvent event) throws ExecutionException {
- IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
- final IEditorPart editor = window.getActivePage().getActiveEditor();
- if (editor instanceof XtextEditor) {
- final IPath path = ((FileEditorInput)editor.getEditorInput()).getPath();
-
- 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 EObjectNode) {
- XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
- IXtextDocument document = xtextEditor.getDocument();
- final String fragment = ((EObjectNode) sel).getEObjectURI().fragment();
- if (checkPrerequisites(xtextEditor, document, fragment)) {
- document.readOnly(new IUnitOfWork.Void<XtextResource>() {
- @Override
- public void process(XtextResource resource) throws Exception {
- if (resource != null) {
- EObject object = resource.getEObject(fragment);
-
- if (object instanceof RoomModel)
- exportDiagrams((RoomModel)object, path, editor.getSite().getShell());
- }
- }
- });
- }
- }
- }
- }
- return null;
- }
-
- protected void exportDiagrams(RoomModel model, IPath modelPath, Shell shell) {
- IContainer container = ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(modelPath.removeLastSegments(1));
- String relPath = store.getString(PreferenceConstants.EXPORT_DIAGRAM_PATH);
- boolean projectRelative = PreferenceConstants.PATH_REL_TO_PROJECT.equals(
- store.getString(PreferenceConstants.EXPORT_DIAGRAM_PATH_RELATIVE_TO));
-
- IFolder folder;
- if (projectRelative) {
- IProject project = container.getProject();
- folder = project.getFolder(relPath);
- }
- else {
- folder = container.getFolder(new Path(relPath));
- }
- if (folder!=null) {
- if (!folder.exists())
- try {
- create(folder);
- } catch (CoreException e) {
- e.printStackTrace();
- }
-
- if (folder.exists()) {
- String folderPath = folder.getLocation().toOSString();
-
- for (ActorClass ac : model.getActorClasses()) {
- if (ac.getStateMachine()!=null)
- BehaviorExporter.export(ac, folderPath);
-
- StructureExporter.export(ac, folderPath);
- }
-
- for (SubSystemClass ssc : model.getSubSystemClasses()) {
- StructureExporter.export(ssc, folderPath);
- }
-
- try {
- folder.refreshLocal(IResource.DEPTH_ONE, null);
- } catch (CoreException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- protected void create(final IResource resource) throws CoreException {
- if (resource == null || resource.exists())
- return;
- if (!resource.getParent().exists())
- create(resource.getParent());
- switch (resource.getType()) {
- case IResource.FILE:
- ((IFile) resource).create(new ByteArrayInputStream(new byte[0]), true, null);
- break;
- case IResource.FOLDER:
- ((IFolder) resource).create(IResource.NONE, true, null);
- break;
- case IResource.PROJECT:
- ((IProject) resource).create(null);
- ((IProject) resource).open(null);
- break;
- }
- }
- // TODO: code copied from org.eclipse.etrice.ui.commands.handlers.AbstractEditHandler - refactor
- 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 (xtextEditor.isDirty()) {
- xtextEditor.doSave(new NullProgressMonitor());
- }
- return true;
- }
-
- // TODO: code copied from org.eclipse.etrice.ui.commands.handlers.AbstractEditHandler - refactor
- 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;
- List<Issue> issueList = resourceValidator.validate(resource, CheckMode.NORMAL_AND_FAST, new CancelIndicator() {
- public boolean isCanceled() {
- return monitor.isCanceled();
- }
- });
- for (Issue issue : issueList) {
- if (issue.getSeverity()==Severity.ERROR)
- return true;
- }
- return false;
- }
- });
- return issues;
- }
-
- @Override
- public boolean isEnabled() {
- IWorkbench wb = PlatformUI.getWorkbench();
- IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
- IWorkbenchPage page = win.getActivePage();
- IWorkbenchPart part = page.getActivePart();
- if (part instanceof ContentOutline) {
- ISelection selection = ((ContentOutline)part).getSelection();
- if (selection instanceof IStructuredSelection) {
- IStructuredSelection ss = (IStructuredSelection) selection;
- Object sel = ss.getFirstElement();
- if (sel instanceof EObjectNode) {
- EObjectNode node = (EObjectNode) sel;
- String fragment = node.getEObjectURI().fragment();
- return fragment.equals("/");
- }
- }
- }
- return false;
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2012 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 + * + * CONTRIBUTORS: + * Henrik Rentz-Reichert (initial contribution) + * + *******************************************************************************/ + +package org.eclipse.etrice.ui.commands.handlers; + +import java.io.ByteArrayInputStream; +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.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.etrice.core.room.ActorClass; +import org.eclipse.etrice.core.room.RoomModel; +import org.eclipse.etrice.core.room.SubSystemClass; +import org.eclipse.etrice.core.ui.RoomUiModule; +import org.eclipse.etrice.ui.behavior.editor.BehaviorExporter; +import org.eclipse.etrice.ui.common.Activator; +import org.eclipse.etrice.ui.common.preferences.PreferenceConstants; +import org.eclipse.etrice.ui.structure.editor.StructureExporter; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.ui.part.FileEditorInput; +import org.eclipse.ui.views.contentoutline.ContentOutline; +import org.eclipse.xtext.diagnostics.Severity; +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.impl.EObjectNode; +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 org.eclipse.xtext.validation.Issue; + +import com.google.inject.Inject; +import com.google.inject.Injector; + +public class ExportDiagramsHandler extends AbstractHandler { + + @Inject + protected IResourceValidator resourceValidator; + private IPreferenceStore store; + + public ExportDiagramsHandler() { + super(); + + Injector injector = RoomUiModule.getInjector(); + injector.injectMembers(this); + + this.store = Activator.getDefault().getPreferenceStore(); + } + + // TODO: code copied from org.eclipse.etrice.ui.commands.handlers.AbstractEditHandler - refactor + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); + final IEditorPart editor = window.getActivePage().getActiveEditor(); + if (editor instanceof XtextEditor) { + final IPath path = ((FileEditorInput)editor.getEditorInput()).getPath(); + + 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 EObjectNode) { + XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event); + IXtextDocument document = xtextEditor.getDocument(); + final String fragment = ((EObjectNode) sel).getEObjectURI().fragment(); + if (checkPrerequisites(xtextEditor, document, fragment)) { + document.readOnly(new IUnitOfWork.Void<XtextResource>() { + @Override + public void process(XtextResource resource) throws Exception { + if (resource != null) { + EObject object = resource.getEObject(fragment); + + if (object instanceof RoomModel) + exportDiagrams((RoomModel)object, path, editor.getSite().getShell()); + } + } + }); + } + } + } + } + return null; + } + + protected void exportDiagrams(RoomModel model, IPath modelPath, Shell shell) { + IContainer container = ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(modelPath.removeLastSegments(1)); + String relPath = store.getString(PreferenceConstants.EXPORT_DIAGRAM_PATH); + boolean projectRelative = PreferenceConstants.PATH_REL_TO_PROJECT.equals( + store.getString(PreferenceConstants.EXPORT_DIAGRAM_PATH_RELATIVE_TO)); + + IFolder folder; + if (projectRelative) { + IProject project = container.getProject(); + folder = project.getFolder(relPath); + } + else { + folder = container.getFolder(new Path(relPath)); + } + if (folder!=null) { + if (!folder.exists()) + try { + create(folder); + } catch (CoreException e) { + e.printStackTrace(); + } + + if (folder.exists()) { + String folderPath = folder.getLocation().toOSString(); + + for (ActorClass ac : model.getActorClasses()) { + if (ac.getStateMachine()!=null) + BehaviorExporter.export(ac, folderPath); + + StructureExporter.export(ac, folderPath); + } + + for (SubSystemClass ssc : model.getSubSystemClasses()) { + StructureExporter.export(ssc, folderPath); + } + + try { + folder.refreshLocal(IResource.DEPTH_ONE, null); + } catch (CoreException e) { + e.printStackTrace(); + } + } + } + } + + protected void create(final IResource resource) throws CoreException { + if (resource == null || resource.exists()) + return; + if (!resource.getParent().exists()) + create(resource.getParent()); + switch (resource.getType()) { + case IResource.FILE: + ((IFile) resource).create(new ByteArrayInputStream(new byte[0]), true, null); + break; + case IResource.FOLDER: + ((IFolder) resource).create(IResource.NONE, true, null); + break; + case IResource.PROJECT: + ((IProject) resource).create(null); + ((IProject) resource).open(null); + break; + } + } + // TODO: code copied from org.eclipse.etrice.ui.commands.handlers.AbstractEditHandler - refactor + 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 (xtextEditor.isDirty()) { + xtextEditor.doSave(new NullProgressMonitor()); + } + return true; + } + + // TODO: code copied from org.eclipse.etrice.ui.commands.handlers.AbstractEditHandler - refactor + 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; + List<Issue> issueList = resourceValidator.validate(resource, CheckMode.NORMAL_AND_FAST, new CancelIndicator() { + public boolean isCanceled() { + return monitor.isCanceled(); + } + }); + for (Issue issue : issueList) { + if (issue.getSeverity()==Severity.ERROR) + return true; + } + return false; + } + }); + return issues; + } + + @Override + public boolean isEnabled() { + IWorkbench wb = PlatformUI.getWorkbench(); + IWorkbenchWindow win = wb.getActiveWorkbenchWindow(); + IWorkbenchPage page = win.getActivePage(); + IWorkbenchPart part = page.getActivePart(); + if (part instanceof ContentOutline) { + ISelection selection = ((ContentOutline)part).getSelection(); + if (selection instanceof IStructuredSelection) { + IStructuredSelection ss = (IStructuredSelection) selection; + Object sel = ss.getFirstElement(); + if (sel instanceof EObjectNode) { + EObjectNode node = (EObjectNode) sel; + String fragment = node.getEObjectURI().fragment(); + return fragment.equals("/"); + } + } + } + return false; + } + +} |