svn-gvsig-desktop / tags / v2_0_0_Build_2057 / frameworks / _fwAndami / src / org / gvsig / andami / actioninfo / impl / DefaultActionInfo.java @ 39145
History | View | Annotate | Download (9.19 KB)
1 |
package org.gvsig.andami.actioninfo.impl; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.beans.PropertyChangeListener; |
5 |
import java.net.URL; |
6 |
import java.util.ArrayList; |
7 |
import java.util.Collection; |
8 |
import java.util.List; |
9 |
|
10 |
import javax.swing.Action; |
11 |
import javax.swing.ImageIcon; |
12 |
import javax.swing.KeyStroke; |
13 |
|
14 |
import org.gvsig.andami.PluginServices; |
15 |
import org.gvsig.andami.PluginsLocator; |
16 |
import org.gvsig.andami.actioninfo.ActionInfo; |
17 |
import org.gvsig.andami.plugins.ExclusiveUIExtension; |
18 |
import org.gvsig.andami.plugins.ExtensionHelper; |
19 |
import org.gvsig.andami.plugins.IExtension; |
20 |
import org.gvsig.andami.ui.mdiFrame.KeyMapping; |
21 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
22 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
23 |
|
24 |
import org.apache.commons.io.FilenameUtils; |
25 |
import org.slf4j.Logger; |
26 |
import org.slf4j.LoggerFactory; |
27 |
|
28 |
public class DefaultActionInfo implements ActionInfo { |
29 |
private static Logger logger = LoggerFactory |
30 |
.getLogger(DefaultActionInfo.class); |
31 |
|
32 |
private Class<? extends IExtension> extensionClass; |
33 |
private IExtension extension;
|
34 |
private String name; |
35 |
private String text; |
36 |
private String command; |
37 |
private String iconName; |
38 |
private String accelerator; |
39 |
private long position; |
40 |
private String tip; |
41 |
private List<ActionInfo> redirections; |
42 |
private boolean active; |
43 |
|
44 |
DefaultActionInfo(Class<? extends IExtension> extension, String name, |
45 |
String text, String command, String icon, String accelerator, |
46 |
long position, String tip) { |
47 |
this.extensionClass = extension;
|
48 |
this.name = name;
|
49 |
this.text = emptyToNull(text);
|
50 |
this.command = emptyToNull(command);
|
51 |
this.iconName = emptyToNull(icon);
|
52 |
this.accelerator = emptyToNull(accelerator);
|
53 |
this.position = position;
|
54 |
this.tip = emptyToNull(tip);
|
55 |
this.redirections = null; |
56 |
this.active = true; |
57 |
|
58 |
fixIcon(); |
59 |
} |
60 |
|
61 |
private void fixIcon() { |
62 |
if (iconName != null && (iconName.contains("/") || iconName.contains("."))) { |
63 |
// it's a file path
|
64 |
String name = FilenameUtils.getBaseName(iconName);
|
65 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
66 |
URL resource = null; |
67 |
try {
|
68 |
resource = this.extensionClass.getClassLoader().getResource(iconName);
|
69 |
} catch (Exception e) { |
70 |
return;
|
71 |
} |
72 |
if (resource == null) { |
73 |
return;
|
74 |
} |
75 |
iconTheme.registerDefault(this.getPluginName(), "broken", name, null, resource); |
76 |
logger.info("Plugin " + this.getPluginName() + " contains icons out of icon theme (" + iconName + ")"); |
77 |
iconName = name; |
78 |
} |
79 |
} |
80 |
|
81 |
|
82 |
|
83 |
private String emptyToNull(String s) { |
84 |
if (s == null) { |
85 |
return null; |
86 |
} |
87 |
return s.trim().length() < 0 ? null : s; |
88 |
} |
89 |
|
90 |
public Collection<ActionInfo> getRedirections() { |
91 |
if (this.redirections == null) { |
92 |
this.redirections = new ArrayList<ActionInfo>(); |
93 |
} |
94 |
return this.redirections; |
95 |
} |
96 |
|
97 |
public void merge(ActionInfo other) { |
98 |
if (this.extensionClass == null) { |
99 |
this.extensionClass = other.getExtension().getClass();
|
100 |
this.extension = other.getExtension();
|
101 |
} |
102 |
if (this.text == null) { |
103 |
this.text = other.getLabel();
|
104 |
} |
105 |
if (this.command == null) { |
106 |
this.command = other.getCommand();
|
107 |
} |
108 |
if (this.iconName == null) { |
109 |
this.iconName = other.getIconName();
|
110 |
} |
111 |
if (this.accelerator == null) { |
112 |
this.accelerator = other.getAccelerator();
|
113 |
} |
114 |
if (this.position < 1) { |
115 |
this.position = other.getPosition();
|
116 |
} |
117 |
if (this.tip == null) { |
118 |
this.tip = other.getTooltip();
|
119 |
} |
120 |
|
121 |
} |
122 |
|
123 |
public PluginServices getPlugin() {
|
124 |
PluginServices plugin = PluginsLocator.getManager().getPlugin( |
125 |
this.extensionClass);
|
126 |
return plugin;
|
127 |
} |
128 |
|
129 |
public String getPluginName() { |
130 |
if( this.getPlugin()==null ) { |
131 |
return null; |
132 |
} |
133 |
return this.getPlugin().getPluginName(); |
134 |
} |
135 |
|
136 |
public IExtension getExtension() {
|
137 |
if (this.extension == null) { |
138 |
this.extension = PluginsLocator.getManager().getExtension(
|
139 |
this.extensionClass);
|
140 |
} |
141 |
return this.extension; |
142 |
} |
143 |
|
144 |
public String getExtensionName() { |
145 |
if( this.extensionClass==null ) { |
146 |
return null; |
147 |
} |
148 |
return this.extensionClass.getName(); |
149 |
} |
150 |
|
151 |
public boolean isVisible() { |
152 |
if (!this.isActive()) { |
153 |
logger.info("isVisible(), action {} not active", this.getName()); |
154 |
return false; |
155 |
} |
156 |
ActionInfo redirection = this.getRedirection();
|
157 |
if (redirection!=null) { |
158 |
return redirection.isVisible();
|
159 |
} |
160 |
|
161 |
ExclusiveUIExtension eui = PluginsLocator.getManager() |
162 |
.getExclusiveUIExtension(); |
163 |
if (eui == null) { |
164 |
return ExtensionHelper.isVisible(this.getExtension(), this.command); |
165 |
} |
166 |
return eui.isVisible(this.getExtension()); |
167 |
} |
168 |
|
169 |
public boolean isEnabled() { |
170 |
if (!this.isActive()) { |
171 |
logger.info("isEnabled(), action {} not active", this.getName()); |
172 |
return false; |
173 |
} |
174 |
ActionInfo redirection = this.getRedirection();
|
175 |
if (redirection!=null) { |
176 |
return true; |
177 |
} |
178 |
|
179 |
ExclusiveUIExtension eui = PluginsLocator.getManager() |
180 |
.getExclusiveUIExtension(); |
181 |
if (eui == null) { |
182 |
return ExtensionHelper.isEnabled(this.getExtension(), this.command); |
183 |
} |
184 |
return eui.isEnabled(this.getExtension()); |
185 |
} |
186 |
|
187 |
private ActionInfo getRedirection() {
|
188 |
if( this.redirections == null ) { |
189 |
return null; |
190 |
} |
191 |
for( int n=this.redirections.size()-1; n>=0 ; n-- ) { |
192 |
ActionInfo redirection = this.redirections.get(n);
|
193 |
if (redirection.isEnabled()) {
|
194 |
return redirection;
|
195 |
} |
196 |
} |
197 |
return null; |
198 |
} |
199 |
|
200 |
public void execute() { |
201 |
// if (!this.isActive()) {
|
202 |
// logger.info("execute(), action {} not active", this.getName());
|
203 |
// return;
|
204 |
// }
|
205 |
ActionInfo redirection = this.getRedirection();
|
206 |
if (redirection!=null) { |
207 |
logger.info("{}.execute('{}') redirected", this.getPluginName() |
208 |
+ ":" + this.getExtensionName(), this.getCommand()); |
209 |
redirection.execute(); |
210 |
return;
|
211 |
} |
212 |
logger.info("{}.execute('{}')",
|
213 |
this.getPluginName() + ":" + this.getExtensionName(), |
214 |
this.getCommand());
|
215 |
this.getExtension().execute(this.command); |
216 |
} |
217 |
|
218 |
public void execute(Object[] args) { |
219 |
// if (!this.isActive()) {
|
220 |
// logger.info("execute(args), action {} not active", this.getName());
|
221 |
// return;
|
222 |
// }
|
223 |
ActionInfo redirection = this.getRedirection();
|
224 |
if (redirection!=null) { |
225 |
logger.info("{}.execute('{}', args) redirected", this.getPluginName() |
226 |
+ ":" + this.getExtensionName(), this.getCommand()); |
227 |
redirection.execute(args); |
228 |
return;
|
229 |
} |
230 |
logger.info("{}.execute('{}', args)",
|
231 |
this.getPluginName() + ":" + this.getExtensionName(), |
232 |
this.getCommand());
|
233 |
ExtensionHelper.execute(this.getExtension(), this.command, args); |
234 |
} |
235 |
|
236 |
public void execute(Object arg) { |
237 |
if (arg instanceof Object[]) { |
238 |
execute((Object[]) arg); |
239 |
return;
|
240 |
} |
241 |
execute(new Object[] { arg }); |
242 |
} |
243 |
|
244 |
public void actionPerformed(ActionEvent arg0) { |
245 |
this.execute();
|
246 |
} |
247 |
|
248 |
public String getName() { |
249 |
return this.name; |
250 |
} |
251 |
|
252 |
public String getLabel() { |
253 |
return this.text; |
254 |
} |
255 |
|
256 |
public String getCommand() { |
257 |
return this.command; |
258 |
} |
259 |
|
260 |
public String getIconName() { |
261 |
return this.iconName; |
262 |
} |
263 |
|
264 |
public ImageIcon getIcon() { |
265 |
IconTheme iconTheme = PluginServices.getIconTheme(); |
266 |
return iconTheme.get(this.iconName); |
267 |
} |
268 |
|
269 |
public String getAccelerator() { |
270 |
return this.accelerator; |
271 |
} |
272 |
|
273 |
public KeyStroke getKeyStroke() { |
274 |
if (emptyToNull(this.accelerator) == null) { |
275 |
return null; |
276 |
} |
277 |
return KeyMapping.getKeyStroke(this.accelerator); |
278 |
} |
279 |
|
280 |
public long getPosition() { |
281 |
return this.position; |
282 |
} |
283 |
|
284 |
public String getTooltip() { |
285 |
return this.tip; |
286 |
} |
287 |
|
288 |
public void addPropertyChangeListener(PropertyChangeListener arg0) { |
289 |
// not implemented
|
290 |
} |
291 |
|
292 |
public Object getValue(String name) { |
293 |
if (Action.ACTION_COMMAND_KEY.equalsIgnoreCase(name)) { |
294 |
return this.command; |
295 |
} |
296 |
if (Action.LONG_DESCRIPTION.equalsIgnoreCase(name)) { |
297 |
return this.tip; |
298 |
} |
299 |
if (Action.NAME.equalsIgnoreCase(name)) { |
300 |
return this.name; |
301 |
} |
302 |
if (Action.SMALL_ICON.equalsIgnoreCase(name)) { |
303 |
return this.iconName; |
304 |
// return
|
305 |
// IconThemeManager.getIconThemeManager().getCurrent().get(this.iconName);
|
306 |
} |
307 |
return null; |
308 |
} |
309 |
|
310 |
public void putValue(String arg0, Object arg1) { |
311 |
|
312 |
} |
313 |
|
314 |
public void removePropertyChangeListener(PropertyChangeListener arg0) { |
315 |
// not implemented
|
316 |
} |
317 |
|
318 |
public void setEnabled(boolean arg0) { |
319 |
// Do nothing
|
320 |
} |
321 |
|
322 |
public String toString() { |
323 |
StringBuffer buffer = new StringBuffer(); |
324 |
buffer.append("ActionInfo {");
|
325 |
buffer.append("name='").append(this.name).append("', "); |
326 |
buffer.append("active='").append(this.active).append("', "); |
327 |
buffer.append("label='").append(this.text).append("', "); |
328 |
buffer.append("tooltip='").append(this.tip).append("', "); |
329 |
buffer.append("actionCommand='").append(this.command).append("', "); |
330 |
buffer.append("position='").append(this.position).append("', "); |
331 |
buffer.append("icon='").append(this.iconName).append("', "); |
332 |
buffer.append("extension='").append(this.getExtensionName()) |
333 |
.append("', ");
|
334 |
if (this.redirections != null) { |
335 |
buffer.append("redirection=(");
|
336 |
for (ActionInfo redirection : this.redirections) { |
337 |
buffer.append(redirection.getName()); |
338 |
buffer.append(" ");
|
339 |
} |
340 |
buffer.append("), ");
|
341 |
} else {
|
342 |
buffer.append("redirections=( ), ");
|
343 |
} |
344 |
buffer.append("accelerator='").append(this.accelerator); |
345 |
buffer.append("' }");
|
346 |
return buffer.toString();
|
347 |
} |
348 |
|
349 |
public boolean isActive() { |
350 |
return this.active; |
351 |
} |
352 |
|
353 |
public void setActive(boolean active) { |
354 |
logger.info("setActive({})", active);
|
355 |
this.active = active;
|
356 |
} |
357 |
|
358 |
} |