01: /*******************************************************************************
02: * Copyright (c) 2005, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.examples.undo;
11:
12: import org.eclipse.core.commands.ExecutionException;
13: import org.eclipse.core.commands.operations.IUndoContext;
14: import org.eclipse.core.runtime.IAdaptable;
15: import org.eclipse.core.runtime.IProgressMonitor;
16: import org.eclipse.core.runtime.IStatus;
17: import org.eclipse.core.runtime.Status;
18: import org.eclipse.swt.graphics.Point;
19: import org.eclipse.swt.widgets.Canvas;
20:
21: /**
22: * An operation that adds a box.
23: */
24: public class MoveBoxOperation extends BoxOperation {
25:
26: /*
27: * The point the box should move to/from.
28: */
29: private Point origin;
30: private Point target;
31:
32: public MoveBoxOperation(String label, IUndoContext context,
33: Box box, Canvas canvas, Point newOrigin) {
34: super (label, context, null, box, canvas);
35: origin = new Point(box.x1, box.y1);
36: target = new Point(newOrigin.x, newOrigin.y);
37: }
38:
39: /*
40: * (non-Javadoc)
41: * @see org.eclipse.core.commands.operations.IUndoableOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
42: */
43: public IStatus execute(IProgressMonitor monitor, IAdaptable info)
44: throws ExecutionException {
45: if (box == null) {
46: throw new ExecutionException("box ix null");
47: }
48: box.move(target);
49: canvas.redraw();
50: return Status.OK_STATUS;
51: }
52:
53: /*
54: * (non-Javadoc)
55: * @see org.eclipse.core.commands.operations.IUndoableOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
56: */
57: public IStatus redo(IProgressMonitor monitor, IAdaptable info)
58: throws ExecutionException {
59: return execute(monitor, info);
60: }
61:
62: /*
63: * (non-Javadoc)
64: * @see org.eclipse.core.commands.operations.IUndoableOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
65: */
66: public IStatus undo(IProgressMonitor monitor, IAdaptable info)
67: throws ExecutionException {
68: if (box == null) {
69: throw new ExecutionException("box ix null");
70: }
71: box.move(origin);
72: canvas.redraw();
73: return Status.OK_STATUS;
74: }
75:
76: /*
77: * (non-Javadoc)
78: * @see org.eclipse.core.commands.operations.IUndoableOperation#getLabel()
79: */
80: public String getLabel() {
81: final StringBuffer stringBuffer = new StringBuffer();
82: stringBuffer.append(super .getLabel());
83: stringBuffer.append("["); //$NON-NLS-1$
84: stringBuffer.append("("); //$NON-NLS-1$
85: stringBuffer.append(new Integer(origin.x).toString());
86: stringBuffer.append(", "); //$NON-NLS-1$
87: stringBuffer.append(new Integer(origin.y).toString());
88: stringBuffer.append(')');
89: stringBuffer.append(", "); //$NON-NLS-1$
90: stringBuffer.append("("); //$NON-NLS-1$
91: stringBuffer.append(new Integer(target.x).toString());
92: stringBuffer.append(", "); //$NON-NLS-1$
93: stringBuffer.append(new Integer(target.y).toString());
94: stringBuffer.append(')');
95: stringBuffer.append(']');
96: return stringBuffer.toString();
97: }
98:
99: }
|