Mercurial > projects > dwt2
comparison org.eclipse.core.commands/src/org/eclipse/core/commands/operations/AbstractOperation.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2005, 2008 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.core.commands.operations.AbstractOperation; | |
14 | |
15 import org.eclipse.core.commands.ExecutionException; | |
16 import org.eclipse.core.runtime.Assert; | |
17 import org.eclipse.core.runtime.IAdaptable; | |
18 import org.eclipse.core.runtime.IProgressMonitor; | |
19 import org.eclipse.core.runtime.IStatus; | |
20 | |
21 import org.eclipse.core.commands.operations.IUndoableOperation; | |
22 import org.eclipse.core.commands.operations.IUndoContext; | |
23 | |
24 import java.lang.all; | |
25 import java.util.List; | |
26 import java.util.ArrayList; | |
27 | |
28 /** | |
29 * <p> | |
30 * Abstract implementation for an undoable operation. At a minimum, subclasses | |
31 * should implement behavior for | |
32 * {@link IUndoableOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)}, | |
33 * {@link IUndoableOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)}, | |
34 * and | |
35 * {@link IUndoableOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)}. | |
36 * </p> | |
37 * | |
38 * @see org.eclipse.core.commands.operations.IUndoableOperation | |
39 * | |
40 * @since 3.1 | |
41 */ | |
42 public abstract class AbstractOperation : IUndoableOperation { | |
43 List contexts; | |
44 | |
45 private String label = ""; //$NON-NLS-1$ | |
46 | |
47 /** | |
48 * Construct an operation that has the specified label. | |
49 * | |
50 * @param label | |
51 * the label to be used for the operation. Should never be | |
52 * <code>null</code>. | |
53 */ | |
54 public this(String label) { | |
55 Assert.isNotNull(label); | |
56 this.label = label; | |
57 contexts = new ArrayList(); | |
58 } | |
59 | |
60 /* | |
61 * (non-Javadoc) | |
62 * | |
63 * @see org.eclipse.core.commands.operations.IUndoableOperation#addContext(org.eclipse.core.commands.operations.IUndoContext) | |
64 * | |
65 * <p> Subclasses may override this method. </p> | |
66 */ | |
67 public void addContext(IUndoContext context) { | |
68 if (!contexts.contains(cast(Object)context)) { | |
69 contexts.add(cast(Object)context); | |
70 } | |
71 } | |
72 | |
73 /* | |
74 * (non-Javadoc) | |
75 * | |
76 * @see org.eclipse.core.commands.operations.IUndoableOperation#canExecute() | |
77 * <p> Default implementation. Subclasses may override this method. | |
78 * </p> | |
79 * | |
80 */ | |
81 public bool canExecute() { | |
82 return true; | |
83 } | |
84 | |
85 /* | |
86 * (non-Javadoc) | |
87 * | |
88 * @see org.eclipse.core.commands.operations.IUndoableOperation#canRedo() | |
89 * <p> Default implementation. Subclasses may override this method. | |
90 * </p> | |
91 */ | |
92 public bool canRedo() { | |
93 return true; | |
94 } | |
95 | |
96 /* | |
97 * (non-Javadoc) | |
98 * | |
99 * @see org.eclipse.core.commands.operations.IUndoableOperation#canUndo() | |
100 * <p> Default implementation. Subclasses may override this method. | |
101 * </p> | |
102 */ | |
103 public bool canUndo() { | |
104 return true; | |
105 } | |
106 | |
107 /* | |
108 * (non-Javadoc) | |
109 * | |
110 * @see org.eclipse.core.commands.operations.IUndoableOperation#dispose() | |
111 * <p> Default implementation. Subclasses may override this method. | |
112 * </p> | |
113 */ | |
114 public void dispose() { | |
115 // nothing to dispose. | |
116 } | |
117 | |
118 /* | |
119 * (non-Javadoc) | |
120 * | |
121 * @see org.eclipse.core.commands.operations.IUndoableOperation#execute(org.eclipse.core.runtime.IProgressMonitor, | |
122 * org.eclipse.core.runtime.IAdaptable) | |
123 */ | |
124 public abstract IStatus execute(IProgressMonitor monitor, IAdaptable info); | |
125 | |
126 public final IUndoContext[] getContexts() { | |
127 return arraycast!(IUndoContext)(contexts.toArray()); | |
128 } | |
129 | |
130 /* | |
131 * (non-Javadoc) | |
132 * | |
133 * @see org.eclipse.core.commands.operations.IUndoableOperation#getLabel() | |
134 * <p> Default implementation. Subclasses may override this method. | |
135 * </p> | |
136 */ | |
137 public String getLabel() { | |
138 return label; | |
139 } | |
140 | |
141 /** | |
142 * Set the label of the operation to the specified name. | |
143 * | |
144 * @param name | |
145 * the string to be used for the label. Should never be | |
146 * <code>null</code>. | |
147 */ | |
148 public void setLabel(String name) { | |
149 label = name; | |
150 } | |
151 | |
152 /* | |
153 * (non-Javadoc) | |
154 * | |
155 * @see org.eclipse.core.commands.operations.IUndoableOperation#hasContext(org.eclipse.core.commands.operations.IUndoContext) | |
156 */ | |
157 public final bool hasContext(IUndoContext context) { | |
158 Assert.isNotNull(cast(Object)context); | |
159 for (int i = 0; i < contexts.size(); i++) { | |
160 IUndoContext otherContext = cast(IUndoContext)contexts.get(i); | |
161 // have to check both ways because one context may be more general | |
162 // in | |
163 // its matching rules than another. | |
164 if (context.matches(otherContext) || otherContext.matches(context)) { | |
165 return true; | |
166 } | |
167 } | |
168 return false; | |
169 } | |
170 | |
171 /* | |
172 * (non-Javadoc) | |
173 * | |
174 * @see org.eclipse.core.commands.operations.IUndoableOperation#redo(org.eclipse.core.runtime.IProgressMonitor, | |
175 * org.eclipse.core.runtime.IAdaptable) | |
176 */ | |
177 public abstract IStatus redo(IProgressMonitor monitor, IAdaptable info); | |
178 | |
179 /* | |
180 * (non-Javadoc) | |
181 * | |
182 * @see org.eclipse.core.commands.operations.IUndoableOperation#removeContext(org.eclipse.core.commands.operations.IUndoContext) | |
183 * <p> Default implementation. Subclasses may override this method. | |
184 * </p> | |
185 */ | |
186 | |
187 public void removeContext(IUndoContext context) { | |
188 contexts.remove(cast(Object)context); | |
189 } | |
190 | |
191 /* | |
192 * (non-Javadoc) | |
193 * | |
194 * @see org.eclipse.core.commands.operations.IUndoableOperation#undo(org.eclipse.core.runtime.IProgressMonitor, | |
195 * org.eclipse.core.runtime.IAdaptable) | |
196 */ | |
197 public abstract IStatus undo(IProgressMonitor monitor, IAdaptable info); | |
198 | |
199 /** | |
200 * The string representation of this operation. Used for debugging purposes | |
201 * only. This string should not be shown to an end user. | |
202 * | |
203 * @return The string representation. | |
204 */ | |
205 public override String toString() { | |
206 StringBuffer stringBuffer = new StringBuffer(); | |
207 stringBuffer.append(getLabel()); | |
208 stringBuffer.append("("); //$NON-NLS-1$ | |
209 IUndoContext[] contexts = getContexts(); | |
210 for (int i = 0; i < contexts.length; i++) { | |
211 stringBuffer.append((cast(Object)contexts[i]).toString()); | |
212 if (i !is contexts.length - 1) { | |
213 stringBuffer.append(','); | |
214 } | |
215 } | |
216 stringBuffer.append(')'); | |
217 return stringBuffer.toString(); | |
218 } | |
219 } |