KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > commands > operations > AbstractOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.commands.operations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21
22 /**
23  * <p>
24  * Abstract implementation for an undoable operation. At a minimum, subclasses
25  * should implement behavior for
26  * {@link IUndoableOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)},
27  * {@link IUndoableOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)},
28  * and
29  * {@link IUndoableOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)}.
30  * </p>
31  *
32  * @see org.eclipse.core.commands.operations.IUndoableOperation
33  *
34  * @since 3.1
35  */

36 public abstract class AbstractOperation implements IUndoableOperation {
37     List JavaDoc contexts = new ArrayList JavaDoc();
38
39     private String JavaDoc label = ""; //$NON-NLS-1$
40

41     /**
42      * Construct an operation that has the specified label.
43      *
44      * @param label
45      * the label to be used for the operation.
46      */

47     public AbstractOperation(String JavaDoc label) {
48         this.label = label;
49     }
50
51     /*
52      * (non-Javadoc)
53      *
54      * @see org.eclipse.core.commands.operations.IUndoableOperation#addContext(org.eclipse.core.commands.operations.IUndoContext)
55      *
56      * <p> Subclasses may override this method. </p>
57      */

58     public void addContext(IUndoContext context) {
59         if (!contexts.contains(context)) {
60             contexts.add(context);
61         }
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see org.eclipse.core.commands.operations.IUndoableOperation#canExecute()
68      * <p> Default implementation. Subclasses may override this method.
69      * </p>
70      *
71      */

72     public boolean canExecute() {
73         return true;
74     }
75
76     /*
77      * (non-Javadoc)
78      *
79      * @see org.eclipse.core.commands.operations.IUndoableOperation#canRedo()
80      * <p> Default implementation. Subclasses may override this method.
81      * </p>
82      */

83     public boolean canRedo() {
84         return true;
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see org.eclipse.core.commands.operations.IUndoableOperation#canUndo()
91      * <p> Default implementation. Subclasses may override this method.
92      * </p>
93      */

94     public boolean canUndo() {
95         return true;
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see org.eclipse.core.commands.operations.IUndoableOperation#dispose()
102      * <p> Default implementation. Subclasses may override this method.
103      * </p>
104      */

105     public void dispose() {
106         // nothing to dispose.
107
}
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.eclipse.core.commands.operations.IUndoableOperation#execute(org.eclipse.core.runtime.IProgressMonitor,
113      * org.eclipse.core.runtime.IAdaptable)
114      */

115     public abstract IStatus execute(IProgressMonitor monitor, IAdaptable info)
116             throws ExecutionException;
117
118     public final IUndoContext[] getContexts() {
119         return (IUndoContext[]) contexts.toArray(new IUndoContext[contexts
120                 .size()]);
121     }
122
123     /*
124      * (non-Javadoc)
125      *
126      * @see org.eclipse.core.commands.operations.IUndoableOperation#getLabel()
127      * <p> Default implementation. Subclasses may override this method.
128      * </p>
129      */

130     public String JavaDoc getLabel() {
131         return label;
132     }
133
134     /**
135      * Set the label of the operation to the specified name.
136      *
137      * @param name
138      * the string to be used for the label.
139      */

140     public void setLabel(String JavaDoc name) {
141         label = name;
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see org.eclipse.core.commands.operations.IUndoableOperation#hasContext(org.eclipse.core.commands.operations.IUndoContext)
148      */

149     public final boolean hasContext(IUndoContext context) {
150         Assert.isNotNull(context);
151         for (int i = 0; i < contexts.size(); i++) {
152             IUndoContext otherContext = (IUndoContext) contexts.get(i);
153             // have to check both ways because one context may be more general
154
// in
155
// its matching rules than another.
156
if (context.matches(otherContext) || otherContext.matches(context)) {
157                 return true;
158             }
159         }
160         return false;
161     }
162
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.eclipse.core.commands.operations.IUndoableOperation#redo(org.eclipse.core.runtime.IProgressMonitor,
167      * org.eclipse.core.runtime.IAdaptable)
168      */

169     public abstract IStatus redo(IProgressMonitor monitor, IAdaptable info)
170             throws ExecutionException;
171
172     /*
173      * (non-Javadoc)
174      *
175      * @see org.eclipse.core.commands.operations.IUndoableOperation#removeContext(org.eclipse.core.commands.operations.IUndoContext)
176      * <p> Default implementation. Subclasses may override this method.
177      * </p>
178      */

179
180     public void removeContext(IUndoContext context) {
181         contexts.remove(context);
182     }
183
184     /*
185      * (non-Javadoc)
186      *
187      * @see org.eclipse.core.commands.operations.IUndoableOperation#undo(org.eclipse.core.runtime.IProgressMonitor,
188      * org.eclipse.core.runtime.IAdaptable)
189      */

190     public abstract IStatus undo(IProgressMonitor monitor, IAdaptable info)
191             throws ExecutionException;
192
193     /**
194      * The string representation of this operation. Used for debugging purposes
195      * only. This string should not be shown to an end user.
196      *
197      * @return The string representation.
198      */

199     public String JavaDoc toString() {
200         final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
201         stringBuffer.append(getLabel());
202         stringBuffer.append("("); //$NON-NLS-1$
203
IUndoContext[] contexts = getContexts();
204         for (int i = 0; i < contexts.length; i++) {
205             stringBuffer.append(contexts[i].toString());
206             if (i != contexts.length - 1) {
207                 stringBuffer.append(',');
208             }
209         }
210         stringBuffer.append(')');
211         return stringBuffer.toString();
212     }
213 }
214
Popular Tags