From 676b6cb57b41f8822b49c11ebdb9f87bc376832a Mon Sep 17 00:00:00 2001 From: Lukas Ladenberger <lukas.ladenberger@googlemail.com> Date: Fri, 24 Aug 2012 11:02:52 +0200 Subject: [PATCH] improved and fixed some minor bugs in adding/removing/editing observer --- .../gef/editor/action/OpenObserverAction.java | 109 ++++++++++-------- ...erCommand.java => SetObserverCommand.java} | 43 +++---- .../gef/editor/model/BControl.java | 4 + 3 files changed, 82 insertions(+), 74 deletions(-) rename de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/command/{ObserverCommand.java => SetObserverCommand.java} (61%) diff --git a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/action/OpenObserverAction.java b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/action/OpenObserverAction.java index f63e35f8..fba03d3c 100644 --- a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/action/OpenObserverAction.java +++ b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/action/OpenObserverAction.java @@ -14,10 +14,10 @@ import org.eclipse.gef.ui.actions.SelectionAction; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.ui.IWorkbenchPart; -import de.bmotionstudio.gef.editor.AttributeConstants; import de.bmotionstudio.gef.editor.BMotionEditorPlugin; import de.bmotionstudio.gef.editor.BMotionStudioImage; -import de.bmotionstudio.gef.editor.command.ObserverCommand; +import de.bmotionstudio.gef.editor.command.RemoveObserverCommand; +import de.bmotionstudio.gef.editor.command.SetObserverCommand; import de.bmotionstudio.gef.editor.model.BControl; import de.bmotionstudio.gef.editor.observer.Observer; import de.bmotionstudio.gef.editor.observer.ObserverWizard; @@ -26,9 +26,10 @@ import de.prob.logging.Logger; public class OpenObserverAction extends SelectionAction { private String className; - private Observer clonedObserver; - private Observer newObserver; - private BControl actionControl; + + // private Observer oldObserver; + // private Observer currentObserver; + // private BControl actionControl; public OpenObserverAction(IWorkbenchPart part) { super(part); @@ -48,35 +49,35 @@ public class OpenObserverAction extends SelectionAction { @Override public void run() { - clonedObserver = null; - - actionControl = getControl(); + BControl actionControl = getControl(); if (actionControl != null) { - newObserver = getControl().getObserver(getClassName()); + Observer oldObserver = null; + Observer observer = getControl().getObserver(getClassName()); - // Add Observer - if (newObserver == null) { + // If an observer does not exist, add one + if (observer == null) { try { - newObserver = (Observer) BMotionEditorPlugin + observer = (Observer) BMotionEditorPlugin .getObserverExtension(getClassName()) .createExecutableExtension("class"); } catch (CoreException e) { } - } else { // Edit Observer + } else { // else edit the current observer - // Clone Observer + // therefore, clone the current observer, if the user aborts + // editing the current observer try { - clonedObserver = newObserver.clone(); + oldObserver = observer.clone(); } catch (CloneNotSupportedException e) { } } - ObserverWizard wizard = newObserver.getWizard(getControl()); + ObserverWizard wizard = observer.getWizard(actionControl); if (wizard != null) { @@ -84,65 +85,75 @@ public class OpenObserverAction extends SelectionAction { getWorkbenchPart(), wizard); dialog.create(); dialog.getShell().setSize(wizard.getSize()); - String title = "Observer: " - + newObserver.getName() - + " Control: " - + getControl().getAttributeValue( - AttributeConstants.ATTRIBUTE_ID); + String title = "Observer: " + observer.getName() + + " Control: " + getControl().getID(); wizard.setWindowTitle("BMotion Studio Observer Wizard"); dialog.setTitle(title); - dialog.setMessage(newObserver.getDescription()); + dialog.setMessage(observer.getDescription()); dialog.setTitleImage(BMotionStudioImage .getImage(BMotionStudioImage.IMG_LOGO_BMOTION64)); int status = dialog.open(); + // The user clicked on the "OK" button in order to confirm his + // changes on the observer if (status == WizardDialog.OK) { - ObserverCommand observerCommand = createObserverCommandCommand(); - observerCommand.setNewObserver(newObserver); - + // If the observer delete flag is set to true, delete the + // observer anyway if (wizard.isObserverDelete()) { - - RemoveObserverAction action = new RemoveObserverAction( - getWorkbenchPart()); - action.setControl(getControl()); - action.setObserver(newObserver); - action.run(); - + RemoveObserverCommand cmd = createRemoveObserverCommand( + observer, actionControl); + execute(cmd); } else { - if (clonedObserver != null) { - observerCommand.setClonedObserver(clonedObserver); - } - execute(observerCommand); + SetObserverCommand cmd = createObserverSetCommand( + actionControl, observer, oldObserver); + execute(cmd); } + // else the user canceled his changes on the observer } else if (status == WizardDialog.CANCEL) { - if (clonedObserver != null) - actionControl.addObserver(clonedObserver); + // Reset observer without using a command! + if (oldObserver != null) + actionControl.addObserver(oldObserver); + // else the user clicked on the delete button in order to + // delete the observer } else if (status == BMotionObserverWizardDialog.DELETE) { - RemoveObserverAction action = new RemoveObserverAction( - getWorkbenchPart()); - action.setControl(getControl()); - action.setObserver(newObserver); - action.run(); + RemoveObserverCommand cmd = createRemoveObserverCommand( + observer, actionControl); + execute(cmd); } } else { Logger.notifyUserWithoutBugreport("The Observer \"" - + newObserver.getName() + + observer.getName() + "\" does not support a wizard."); } } } - public ObserverCommand createObserverCommandCommand() { - ObserverCommand command = new ObserverCommand(); - command.setClassName(getClassName()); - command.setControl(actionControl); - return command; + private RemoveObserverCommand createRemoveObserverCommand( + Observer observer, BControl control) { + RemoveObserverCommand cmd = new RemoveObserverCommand(); + cmd.setControl(control); + cmd.setObserver(observer); + return cmd; + } + + public SetObserverCommand createObserverSetCommand(BControl control, + Observer newObserver, Observer oldObserver) { + SetObserverCommand cmd = new SetObserverCommand(); + cmd.setNewObserver(newObserver); + cmd.setOldObserver(oldObserver); + cmd.setControl(control); + return cmd; + } + + public SetObserverCommand createObserverSetCommand(BControl control, + Observer newObserver) { + return createObserverSetCommand(control, newObserver, null); } public void setClassName(String className) { diff --git a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/command/ObserverCommand.java b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/command/SetObserverCommand.java similarity index 61% rename from de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/command/ObserverCommand.java rename to de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/command/SetObserverCommand.java index b27dd0d8..76d48c54 100644 --- a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/command/ObserverCommand.java +++ b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/command/SetObserverCommand.java @@ -11,53 +11,46 @@ import org.eclipse.gef.commands.Command; import de.bmotionstudio.gef.editor.model.BControl; import de.bmotionstudio.gef.editor.observer.Observer; -public class ObserverCommand extends Command { +public class SetObserverCommand extends Command { - private String className; - private Observer clonedObserver; + private Observer oldObserver; private Observer newObserver; private Observer clonedNewObserver; private BControl control; public void execute() { - + // Clone the new observer try { clonedNewObserver = newObserver.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } - + // Set the new observer control.addObserver(newObserver); - } public boolean canExecute() { + if (newObserver == null || control == null) + return false; return true; } public void undo() { - - // Remove completely new Observer - if (clonedObserver == null) { - control.removeObserver(getClassName()); - } else { // Reset Observer - control.addObserver(clonedObserver); + // If we had an old observer, set the old one + if (oldObserver != null) { + control.addObserver(oldObserver); + // else remove the observer + } else { + control.removeObserver(newObserver); } - } public void redo() { + // Redo method adds the cloned observer, since the observer could be + // changed during set and redo action control.addObserver(clonedNewObserver); } - public void setClassName(String className) { - this.className = className; - } - - public String getClassName() { - return className; - } - public void setControl(BControl control) { this.control = control; } @@ -66,12 +59,12 @@ public class ObserverCommand extends Command { return this.control; } - public Observer getClonedObserver() { - return clonedObserver; + public Observer getOldObserver() { + return oldObserver; } - public void setClonedObserver(Observer clonedObserver) { - this.clonedObserver = clonedObserver; + public void setOldObserver(Observer oldObserver) { + this.oldObserver = oldObserver; } public Observer getNewObserver() { diff --git a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/model/BControl.java b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/model/BControl.java index 9ca33e48..d0ed727f 100644 --- a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/model/BControl.java +++ b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/model/BControl.java @@ -462,6 +462,10 @@ public abstract class BControl implements IAdaptable, Cloneable { } } + public void removeObserver(Observer observer) { + removeObserver(observer.getID()); + } + public void removeObserver(String observerID) { if (hasObserver(observerID)) observers.get(observerID).beforeDelete(this); -- GitLab