Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbblajer2006-11-29 13:51:53 +0000
committerbblajer2006-11-29 13:51:53 +0000
commite8ee6d4e584afacae9842f46af2c2c540beddee9 (patch)
tree295cc98c16a48a73a6f7b38f5b3454211424940c /plugins/org.eclipse.gmf.runtime.lite/src/org
parent0653622f7082f08e739d23adafb2dc0180e3eff9 (diff)
downloadorg.eclipse.gmf-tooling-e8ee6d4e584afacae9842f46af2c2c540beddee9.tar.gz
org.eclipse.gmf-tooling-e8ee6d4e584afacae9842f46af2c2c540beddee9.tar.xz
org.eclipse.gmf-tooling-e8ee6d4e584afacae9842f46af2c2c540beddee9.zip
[165791]: Diagrams that share EMF EditingDomain should share GEF CommandStacks
Diffstat (limited to 'plugins/org.eclipse.gmf.runtime.lite/src/org')
-rw-r--r--plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramDisplayer.java56
-rw-r--r--plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramEditor.java102
-rw-r--r--plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramViewPart.java63
3 files changed, 154 insertions, 67 deletions
diff --git a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramDisplayer.java b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramDisplayer.java
index c61fb3e28..2aea83b7e 100644
--- a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramDisplayer.java
+++ b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramDisplayer.java
@@ -21,7 +21,9 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
+import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.GraphicalViewer;
@@ -108,11 +110,22 @@ public class DiagramDisplayer implements IDiagramOutlineHost {
setEditDomain(gefEditDomain);
editingDomain = emfEditingDomain;
+ gefEditDomain.setCommandStack(createCommandStack());
getCommandStack().addCommandStackListener(commandStackListener);
myDiagramManager.getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(selectionListener);
myDiagramManager.getSite().getPart().addPropertyListener(propertyListener);
}
+ private CommandStack createCommandStack() {
+ CommandStackAdapterManager manager = (CommandStackAdapterManager) EcoreUtil.getExistingAdapter(editingDomain.getResourceSet(), CommandStackAdapterManager.class);
+ if (manager == null) {
+ manager = new CommandStackAdapterManager();
+ editingDomain.getResourceSet().eAdapters().add(manager);
+ }
+ manager.acquire();
+ return manager.getCommandStack();
+ }
+
public void dispose() {
// remove CommandStackListener
getCommandStack().removeCommandStackListener(commandStackListener);
@@ -124,6 +137,13 @@ public class DiagramDisplayer implements IDiagramOutlineHost {
getEditDomain().setActiveTool(null);
+ CommandStackAdapterManager manager = (CommandStackAdapterManager) EcoreUtil.getExistingAdapter(getEditingDomain().getResourceSet(), CommandStackAdapterManager.class);
+ assert manager != null;
+ manager.release();
+ if (manager.isReleased()) {
+ getEditingDomain().getResourceSet().eAdapters().remove(manager);
+ }
+
// dispose the ActionRegistry (will dispose all actions)
getActionRegistry().dispose();
}
@@ -397,4 +417,40 @@ public class DiagramDisplayer implements IDiagramOutlineHost {
progressMonitor.done();
}
}
+
+ private static class CommandStackAdapterManager extends AdapterImpl {
+ @Override
+ public boolean isAdapterForType(Object type) {
+ return CommandStackAdapterManager.class.equals(type);
+ }
+
+ public CommandStack getCommandStack() {
+ if (myCommandStack == null) {
+ myCommandStack = new CommandStack();
+ }
+ return myCommandStack;
+ }
+
+ public void acquire() {
+ myRefCount++;
+ }
+
+ public void release() {
+ if (myRefCount == 0) {
+ throw new IllegalStateException();
+ }
+ myRefCount--;
+ if (myRefCount == 0) {
+ myCommandStack.dispose();
+ myCommandStack = null;
+ }
+ }
+
+ public boolean isReleased() {
+ return myRefCount == 0;
+ }
+
+ private CommandStack myCommandStack;
+ private int myRefCount;
+ }
}
diff --git a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramEditor.java b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramEditor.java
index 2904657c4..2fe16390a 100644
--- a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramEditor.java
+++ b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramEditor.java
@@ -25,16 +25,19 @@ import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.emf.workspace.WorkspaceEditingDomainFactory;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.GraphicalViewer;
-import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
+import org.eclipse.gef.commands.CommandStackEvent;
+import org.eclipse.gef.commands.CommandStackEventListener;
import org.eclipse.gef.commands.CommandStackListener;
import org.eclipse.gef.editparts.ZoomManager;
import org.eclipse.gef.palette.PaletteRoot;
@@ -68,6 +71,26 @@ public abstract class DiagramEditor extends EditorPart implements IDiagramManage
}
};
+ private CommandStackEventListener mySaveListener = new CommandStackEventListener() {
+ public void stackChanged(CommandStackEvent event) {
+ if (event.isPostChangeEvent() && isSaved()) {
+ getCommandStack().markSaveLocation();
+ }
+ }
+ private boolean isSaved() {
+ for(Iterator it = getEditingDomain().getResourceSet().getResources().iterator(); it.hasNext(); ) {
+ Resource next = (Resource) it.next();
+ if (!next.isLoaded()) {
+ continue;
+ }
+ if (!next.isTrackingModification() || next.isModified()) {
+ return false;
+ }
+ }
+ return true;
+ }
+ };
+
protected void save(IProgressMonitor monitor) throws CoreException {
myDiagramDisplayer.save(monitor);
}
@@ -137,16 +160,24 @@ public abstract class DiagramEditor extends EditorPart implements IDiagramManage
}
myDiagramDisplayer = new DiagramDisplayer(this, createEditDomain(), editingDomain);
getCommandStack().addCommandStackListener(commandStackListener);
+ getCommandStack().addCommandStackEventListener(mySaveListener);
setInput(input);
}
@Override
public void dispose() {
if (myDiagramDisplayer != null) {
+ getCommandStack().removeCommandStackEventListener(mySaveListener);
getCommandStack().removeCommandStackListener(commandStackListener);
myDiagramDisplayer.dispose();
myDiagramDisplayer = null;
}
+ ForceTrackingModificationAdapter adapter = (ForceTrackingModificationAdapter) EcoreUtil.getExistingAdapter(getEditingDomain().getResourceSet(), ForceTrackingModificationAdapter.class);
+ assert adapter != null;
+ adapter.release();
+ if (adapter.isReleased()) {
+ getEditingDomain().getResourceSet().eAdapters().remove(adapter);
+ }
super.dispose();
}
@@ -256,7 +287,19 @@ public abstract class DiagramEditor extends EditorPart implements IDiagramManage
* a {@link #createEditDomain() default instance} will be created and used.
* By default, return <code>null</code>. Subclasses may reimplement.
*/
- protected TransactionalEditingDomain getEditingDomain(IEditorInput editorInput) {
+ protected TransactionalEditingDomain getEditingDomain(IEditorInput input) {
+ if (input instanceof DiagramEditorInput) {
+ TransactionalEditingDomain result = TransactionUtil.getEditingDomain(((DiagramEditorInput) input).getDiagram());
+ if (result != null) {
+ ForceTrackingModificationAdapter adapter = (ForceTrackingModificationAdapter) EcoreUtil.getExistingAdapter(result.getResourceSet(), ForceTrackingModificationAdapter.class);
+ if (adapter == null) {
+ adapter = new ForceTrackingModificationAdapter();
+ result.getResourceSet().eAdapters().add(adapter);
+ }
+ adapter.acquire();
+ }
+ return result;
+ }
return null;
}
@@ -283,41 +326,6 @@ public abstract class DiagramEditor extends EditorPart implements IDiagramManage
*/
protected EditDomain createEditDomain() {
DefaultEditDomain domain = new DefaultEditDomain(this);
- domain.setCommandStack(new CommandStack(){
- @Override
- public void execute(Command command) {
- super.execute(command);
- if (isSaved()) {
- markSaveLocation();
- }
- }
- @Override
- public void undo() {
- super.undo();
- if (isSaved()) {
- markSaveLocation();
- }
- }
- @Override
- public void redo() {
- super.redo();
- if (isSaved()) {
- markSaveLocation();
- }
- }
- private boolean isSaved() {
- for(Iterator it = getEditingDomain().getResourceSet().getResources().iterator(); it.hasNext(); ) {
- Resource next = (Resource) it.next();
- if (!next.isLoaded()) {
- continue;
- }
- if (!next.isTrackingModification() || next.isModified()) {
- return false;
- }
- }
- return true;
- }
- });
return domain;
}
@@ -360,5 +368,25 @@ public abstract class DiagramEditor extends EditorPart implements IDiagramManage
}
}
}
+ @Override
+ public boolean isAdapterForType(Object type) {
+ return ForceTrackingModificationAdapter.class.equals(type);
+ }
+ public void acquire() {
+ myRefCount++;
+ }
+
+ public void release() {
+ if (myRefCount == 0) {
+ throw new IllegalStateException();
+ }
+ myRefCount--;
+ }
+
+ public boolean isReleased() {
+ return myRefCount == 0;
+ }
+
+ private int myRefCount;
}
}
diff --git a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramViewPart.java b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramViewPart.java
index 0ddfb2093..aaadb28b6 100644
--- a/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramViewPart.java
+++ b/plugins/org.eclipse.gmf.runtime.lite/src/org/eclipse/gmf/runtime/lite/parts/DiagramViewPart.java
@@ -12,6 +12,7 @@
package org.eclipse.gmf.runtime.lite.parts;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.emf.common.notify.AdapterFactory;
@@ -23,8 +24,9 @@ import org.eclipse.gef.EditDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.GraphicalViewer;
-import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
+import org.eclipse.gef.commands.CommandStackEvent;
+import org.eclipse.gef.commands.CommandStackEventListener;
import org.eclipse.gef.editparts.ZoomManager;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.actions.ActionRegistry;
@@ -57,6 +59,20 @@ public abstract class DiagramViewPart extends ViewPart implements IDiagramManage
private PageBook myBook;
private Control myUninitializedControl;
+ private CommandStackEventListener mySaveListener = new CommandStackEventListener() {
+ public void stackChanged(CommandStackEvent event) {
+ assert event.getSource() == myDiagramDisplayer.getCommandStack();
+ if (event.isPostChangeEvent()) {
+ try {
+ myDiagramDisplayer.save(new NullProgressMonitor());
+ myDiagramDisplayer.getCommandStack().markSaveLocation();
+ } catch (CoreException e) {
+ Activator.getDefault().getLog().log(e.getStatus());
+ }
+ }
+ }
+ };
+
@Override
public void init(IViewSite site) throws PartInitException {
super.init(site);
@@ -65,7 +81,7 @@ public abstract class DiagramViewPart extends ViewPart implements IDiagramManage
@Override
public void dispose() {
if (myDiagramDisplayer != null) {
- myDiagramDisplayer.dispose();
+ disposeDisplayer(myDiagramDisplayer);
}
myDiagramDisplayer = null;
super.dispose();
@@ -117,6 +133,7 @@ public abstract class DiagramViewPart extends ViewPart implements IDiagramManage
editingDomain = createEditingDomain();
}
myDiagramDisplayer = new DiagramDisplayer(this, createEditDomain(), editingDomain);
+ initDisplayer(myDiagramDisplayer);
try {
setInput(diagramURI);
myDiagramDisplayer.createViewer(myBook);
@@ -127,8 +144,7 @@ public abstract class DiagramViewPart extends ViewPart implements IDiagramManage
Activator.getDefault().getLog().log(e.getStatus());
ErrorDialog.openError(getSite().getShell(), "Error", "Failed to open diagram", e.getStatus());
if (myDiagramDisplayer.getTopLevelControl() != null && !myDiagramDisplayer.getTopLevelControl().isDisposed()) {
- myDiagramDisplayer.getTopLevelControl().dispose();
- myDiagramDisplayer.dispose();
+ disposeDisplayer(myDiagramDisplayer);
myDiagramDisplayer = oldDiagramDisplayer;
//prevent navigation from the old diagram.
oldDiagramDisplayer = null;
@@ -136,13 +152,24 @@ public abstract class DiagramViewPart extends ViewPart implements IDiagramManage
}
}
if (oldDiagramDisplayer != null) {
- oldDiagramDisplayer.getTopLevelControl().dispose();
- oldDiagramDisplayer.dispose();
+ disposeDisplayer(oldDiagramDisplayer);
}
updateActionBars();
return result;
}
+ protected void initDisplayer(DiagramDisplayer diagramDisplayer) {
+ diagramDisplayer.getCommandStack().addCommandStackEventListener(mySaveListener);
+ }
+
+ protected void disposeDisplayer(DiagramDisplayer diagramDisplayer) {
+ if (diagramDisplayer.getTopLevelControl() != null) {
+ diagramDisplayer.getTopLevelControl().dispose();
+ }
+ diagramDisplayer.getCommandStack().removeCommandStackEventListener(mySaveListener);
+ diagramDisplayer.dispose();
+ }
+
protected abstract void updateActionBars();
public void configureGraphicalViewer() {
@@ -163,30 +190,6 @@ public abstract class DiagramViewPart extends ViewPart implements IDiagramManage
*/
protected EditDomain createEditDomain() {
EditDomain domain = new EditDomain();
- domain.setCommandStack(new CommandStack() {
- @Override
- public void execute(Command command) {
- super.execute(command);
- save();
- }
- @Override
- public void undo() {
- super.undo();
- save();
- }
- @Override
- public void redo() {
- super.redo();
- save();
- }
- private void save() {
- try {
- myDiagramDisplayer.save(null);
- } catch (CoreException e) {
- Activator.getDefault().getLog().log(e.getStatus());
- }
- }
- });
return domain;
}

Back to the top