Skip to content
Snippets Groups Projects
Commit 676b6cb5 authored by Lukas Ladenberger's avatar Lukas Ladenberger
Browse files

improved and fixed some minor bugs in adding/removing/editing observer

parent 77a53d2a
No related branches found
No related tags found
No related merge requests found
......@@ -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) {
......
......@@ -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() {
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment