1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 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.gef.commands;
|
12
|
|
13
|
/**
|
14
|
* An Abstract implementation of {@link Command}.
|
15
|
*
|
16
|
* @author hudsonr
|
17
|
* @since 2.0
|
18
|
*/
|
19
|
public abstract class Command {
|
20
|
|
21
|
private String label;
|
22
|
|
23
|
private String debugLabel;
|
24
|
|
25
|
/**
|
26
|
* Constructs a Command with no label.
|
27
|
*/
|
28
|
public Command() {
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* Constructs a Command with the specified label.
|
33
|
*
|
34
|
* @param label
|
35
|
* the Command's label
|
36
|
*/
|
37
|
public Command(String label) {
|
38
|
setLabel(label);
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* @return <code>true</code> if the command can be executed
|
43
|
*/
|
44
|
public boolean canExecute() {
|
45
|
return true;
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* @return <code>true</code> if the command can be undone. This method
|
50
|
* should only be called after <code>execute()</code> or
|
51
|
* <code>redo()</code> has been called.
|
52
|
*/
|
53
|
public boolean canUndo() {
|
54
|
return true;
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Returns a Command that represents the chaining of a specified Command to
|
59
|
* this Command. The Command being chained will <code>execute()</code> after
|
60
|
* this command has executed, and it will <code>undo()</code> before this
|
61
|
* Command is undone.
|
62
|
*
|
63
|
* @param command
|
64
|
* <code>null</code> or the Command being chained
|
65
|
* @return a Command representing the union
|
66
|
*/
|
67
|
public Command chain(Command command) {
|
68
|
if (command == null)
|
69
|
return this;
|
70
|
class ChainedCompoundCommand extends CompoundCommand {
|
71
|
public Command chain(Command c) {
|
72
|
add(c);
|
73
|
return this;
|
74
|
}
|
75
|
}
|
76
|
CompoundCommand result = new ChainedCompoundCommand();
|
77
|
result.setDebugLabel("Chained Commands"); //$NON-NLS-1$
|
78
|
result.add(this);
|
79
|
result.add(command);
|
80
|
return result;
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* This is called to indicate that the <code>Command</code> will not be used
|
85
|
* again. The Command may be in any state (executed, undone or redone) when
|
86
|
* dispose is called. The Command should not be referenced in any way after
|
87
|
* it has been disposed.
|
88
|
*/
|
89
|
public void dispose() {
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* executes the Command. This method should not be called if the Command is
|
94
|
* not executable.
|
95
|
*/
|
96
|
public void execute() {
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* @return an untranslated String used for debug purposes only
|
101
|
*/
|
102
|
public String getDebugLabel() {
|
103
|
return debugLabel + ' ' + getLabel();
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* @return a String used to describe this command to the User
|
108
|
*/
|
109
|
public String getLabel() {
|
110
|
return label;
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* Re-executes the Command. This method should only be called after
|
115
|
* <code>undo()</code> has been called.
|
116
|
*/
|
117
|
public void redo() {
|
118
|
execute();
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Sets the debug label for this command
|
123
|
*
|
124
|
* @param label
|
125
|
* a description used for debugging only
|
126
|
*/
|
127
|
public void setDebugLabel(String label) {
|
128
|
debugLabel = label;
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Sets the label used to describe this command to the User.
|
133
|
*
|
134
|
* @param label
|
135
|
* the label
|
136
|
*/
|
137
|
public void setLabel(String label) {
|
138
|
this.label = label;
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Undoes the changes performed during <code>execute()</code>. This method
|
143
|
* should only be called after <code>execute</code> has been called, and
|
144
|
* only when <code>canUndo()</code> returns <code>true</code>.
|
145
|
*
|
146
|
* @see #canUndo()
|
147
|
*/
|
148
|
public void undo() {
|
149
|
}
|
150
|
|
151
|
}
|