svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / actioninfo / impl / DefaultActionInfo.java @ 41689
History | View | Annotate | Download (14.3 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.andami.actioninfo.impl; |
24 |
|
25 |
import java.awt.event.ActionEvent; |
26 |
import java.net.URL; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Collection; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
|
32 |
import javax.swing.AbstractAction; |
33 |
import javax.swing.Action; |
34 |
import javax.swing.ImageIcon; |
35 |
import javax.swing.KeyStroke; |
36 |
|
37 |
import org.apache.commons.io.FilenameUtils; |
38 |
import org.gvsig.andami.PluginServices; |
39 |
import org.gvsig.andami.PluginsLocator; |
40 |
import org.gvsig.andami.actioninfo.ActionInfo; |
41 |
import org.gvsig.andami.plugins.ExclusiveUIExtension; |
42 |
import org.gvsig.andami.plugins.ExtensionHelper; |
43 |
import org.gvsig.andami.plugins.IExtension; |
44 |
import org.gvsig.andami.ui.mdiFrame.KeyMapping; |
45 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
46 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
47 |
import org.slf4j.Logger; |
48 |
import org.slf4j.LoggerFactory; |
49 |
|
50 |
public class DefaultActionInfo extends AbstractAction implements ActionInfo { |
51 |
|
52 |
/**
|
53 |
*
|
54 |
*/
|
55 |
private static final long serialVersionUID = 1620939552263334110L; |
56 |
|
57 |
private static Logger logger = LoggerFactory |
58 |
.getLogger(DefaultActionInfo.class); |
59 |
|
60 |
private Class<? extends IExtension> extensionClass; |
61 |
private IExtension extension;
|
62 |
private String name; |
63 |
private String text; |
64 |
private String command; |
65 |
private String iconName; |
66 |
private String accelerator; |
67 |
private long position; |
68 |
private String tip; |
69 |
private List<ActionInfo> redirections; |
70 |
private boolean active; |
71 |
|
72 |
private Boolean previousEnabled = null; |
73 |
|
74 |
DefaultActionInfo(Class<? extends IExtension> extension, String name, |
75 |
String text, String command, String icon, String accelerator, |
76 |
long position, String tip) { |
77 |
this.extensionClass = extension;
|
78 |
this.name = name;
|
79 |
this.text = emptyToNull(text);
|
80 |
this.command = emptyToNull(command);
|
81 |
this.iconName = emptyToNull(icon);
|
82 |
this.accelerator = emptyToNull(accelerator);
|
83 |
this.position = position;
|
84 |
this.tip = emptyToNull(tip);
|
85 |
this.redirections = null; |
86 |
this.active = true; |
87 |
|
88 |
fixIcon(); |
89 |
} |
90 |
|
91 |
public Object clone() throws CloneNotSupportedException { |
92 |
DefaultActionInfo other = (DefaultActionInfo) super.clone();
|
93 |
if (other.redirections != null) { |
94 |
other.redirections = new ArrayList<ActionInfo>(); |
95 |
other.redirections.addAll(this.redirections);
|
96 |
} |
97 |
return other;
|
98 |
} |
99 |
|
100 |
private void fixIcon() { |
101 |
if (iconName != null && (iconName.contains("/") || iconName.contains("."))) { |
102 |
// it's a file path
|
103 |
String name = FilenameUtils.getBaseName(iconName);
|
104 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
105 |
URL resource = null; |
106 |
try {
|
107 |
resource = this.extensionClass.getClassLoader().getResource(iconName);
|
108 |
} catch (Exception e) { |
109 |
return;
|
110 |
} |
111 |
if (resource == null) { |
112 |
return;
|
113 |
} |
114 |
iconTheme.registerDefault(this.getPluginName(), "broken", name, null, resource); |
115 |
logger.info("Plugin " + this.getPluginName() + " contains icons out of icon theme (" + iconName + ")"); |
116 |
iconName = name; |
117 |
} |
118 |
} |
119 |
|
120 |
private String emptyToNull(String s) { |
121 |
if (s == null) { |
122 |
return null; |
123 |
} |
124 |
return s.trim().length() < 0 ? null : s; |
125 |
} |
126 |
|
127 |
public Collection<ActionInfo> getRedirections() { |
128 |
if (this.redirections == null) { |
129 |
this.redirections = new ArrayList<ActionInfo>(); |
130 |
} |
131 |
return this.redirections; |
132 |
} |
133 |
|
134 |
public void merge(ActionInfo other) { |
135 |
if (this.extensionClass == null) { |
136 |
this.extensionClass = other.getExtension().getClass();
|
137 |
this.extension = other.getExtension();
|
138 |
} |
139 |
if (this.text == null) { |
140 |
this.text = other.getLabel();
|
141 |
} |
142 |
if (this.command == null) { |
143 |
this.command = other.getCommand();
|
144 |
} |
145 |
if (this.iconName == null) { |
146 |
this.iconName = other.getIconName();
|
147 |
} |
148 |
if (this.accelerator == null) { |
149 |
this.accelerator = other.getAccelerator();
|
150 |
} |
151 |
if (this.position < 1) { |
152 |
this.position = other.getPosition();
|
153 |
} |
154 |
if (this.tip == null) { |
155 |
this.tip = other.getTooltip();
|
156 |
} |
157 |
|
158 |
} |
159 |
|
160 |
public PluginServices getPlugin() {
|
161 |
PluginServices plugin = PluginsLocator.getManager().getPlugin( |
162 |
this.extensionClass);
|
163 |
return plugin;
|
164 |
} |
165 |
|
166 |
public String getPluginName() { |
167 |
if (this.getPlugin() == null) { |
168 |
return null; |
169 |
} |
170 |
return this.getPlugin().getPluginName(); |
171 |
} |
172 |
|
173 |
public IExtension getExtension() {
|
174 |
if (this.extension == null) { |
175 |
this.extension = PluginsLocator.getManager().getExtension(
|
176 |
this.extensionClass);
|
177 |
} |
178 |
return this.extension; |
179 |
} |
180 |
|
181 |
public String getExtensionName() { |
182 |
if (this.extensionClass == null) { |
183 |
return null; |
184 |
} |
185 |
return this.extensionClass.getName(); |
186 |
} |
187 |
|
188 |
public boolean isVisible() { |
189 |
if (!this.isActive()) { |
190 |
logger.info("isVisible(), action {} not active", this.getName()); |
191 |
return false; |
192 |
} |
193 |
ActionInfo redirection = this.getRedirection();
|
194 |
if (redirection != null) { |
195 |
return redirection.isVisible();
|
196 |
} |
197 |
|
198 |
ExclusiveUIExtension eui = PluginsLocator.getManager() |
199 |
.getExclusiveUIExtension(); |
200 |
if (eui == null) { |
201 |
return ExtensionHelper.isVisible(this.getExtension(), this.command); |
202 |
} |
203 |
return eui.isVisible(this.getExtension()); |
204 |
} |
205 |
|
206 |
public boolean isEnabled() { |
207 |
boolean value;
|
208 |
if (!this.isActive()) { |
209 |
logger.info("isEnabled(), action {} not active", this.getName()); |
210 |
value = false;
|
211 |
} else {
|
212 |
ActionInfo redirection = this.getRedirection();
|
213 |
if (redirection != null) { |
214 |
value = true;
|
215 |
} else {
|
216 |
ExclusiveUIExtension eui = PluginsLocator.getManager() |
217 |
.getExclusiveUIExtension(); |
218 |
if (eui == null) { |
219 |
value = ExtensionHelper.isEnabled(this.getExtension(), this.command); |
220 |
} else {
|
221 |
value = eui.isEnabled(this.getExtension());
|
222 |
} |
223 |
} |
224 |
} |
225 |
if (this.previousEnabled == null || this.previousEnabled.booleanValue() != value) { |
226 |
this.setEnabled(value); // force call listeners |
227 |
} |
228 |
return value;
|
229 |
} |
230 |
|
231 |
private ActionInfo getRedirection() {
|
232 |
if (this.redirections == null) { |
233 |
return null; |
234 |
} |
235 |
for (int n = this.redirections.size() - 1; n >= 0; n--) { |
236 |
ActionInfo redirection = this.redirections.get(n);
|
237 |
if (redirection.isEnabled()) {
|
238 |
return redirection;
|
239 |
} |
240 |
} |
241 |
return null; |
242 |
} |
243 |
|
244 |
public void execute() { |
245 |
// if (!this.isActive()) {
|
246 |
// logger.info("execute(), action {} not active", this.getName());
|
247 |
// return;
|
248 |
// }
|
249 |
ActionInfo redirection = this.getRedirection();
|
250 |
if (redirection != null) { |
251 |
logger.info("{}.execute('{}') redirected", this.getPluginName() |
252 |
+ ":" + this.getExtensionName(), this.getCommand()); |
253 |
redirection.execute(); |
254 |
return;
|
255 |
} |
256 |
logger.info("{}.execute('{}')",
|
257 |
this.getPluginName() + ":" + this.getExtensionName(), |
258 |
this.getCommand());
|
259 |
this.getExtension().execute(this.command); |
260 |
} |
261 |
|
262 |
public void execute(Object[] args) { |
263 |
// if (!this.isActive()) {
|
264 |
// logger.info("execute(args), action {} not active", this.getName());
|
265 |
// return;
|
266 |
// }
|
267 |
ActionInfo redirection = this.getRedirection();
|
268 |
if (redirection != null) { |
269 |
logger.info("{}.execute('{}', args) redirected", this.getPluginName() |
270 |
+ ":" + this.getExtensionName(), this.getCommand()); |
271 |
redirection.execute(args); |
272 |
return;
|
273 |
} |
274 |
logger.info("{}.execute('{}', Object[] args)",
|
275 |
this.getPluginName() + ":" + this.getExtensionName(), |
276 |
this.getCommand());
|
277 |
ExtensionHelper.execute(this.getExtension(), this.command, args); |
278 |
} |
279 |
|
280 |
public void execute(Object arg) { |
281 |
if (arg instanceof Object[]) { |
282 |
execute((Object[]) arg); |
283 |
return;
|
284 |
} |
285 |
execute(new Object[]{arg}); |
286 |
} |
287 |
|
288 |
public void execute(Map args) { |
289 |
logger.info("{}.execute('{}', Map args)",
|
290 |
this.getPluginName() + ":" + this.getExtensionName(), |
291 |
this.getCommand());
|
292 |
ExtensionHelper.execute(this.getExtension(), this.command, new Object[]{args}); |
293 |
} |
294 |
|
295 |
public void actionPerformed(ActionEvent arg0) { |
296 |
this.execute();
|
297 |
} |
298 |
|
299 |
public String getName() { |
300 |
return this.name; |
301 |
} |
302 |
|
303 |
public String getLabel() { |
304 |
return this.text; |
305 |
} |
306 |
|
307 |
public String getCommand() { |
308 |
return this.command; |
309 |
} |
310 |
|
311 |
public String getIconName() { |
312 |
return this.iconName; |
313 |
} |
314 |
|
315 |
public ImageIcon getIcon() { |
316 |
IconTheme iconTheme = PluginServices.getIconTheme(); |
317 |
return iconTheme.get(this.iconName); |
318 |
} |
319 |
|
320 |
public String getAccelerator() { |
321 |
return this.accelerator; |
322 |
} |
323 |
|
324 |
public KeyStroke getKeyStroke() { |
325 |
if (emptyToNull(this.accelerator) == null) { |
326 |
return null; |
327 |
} |
328 |
return KeyMapping.getKeyStroke(this.accelerator); |
329 |
} |
330 |
|
331 |
public long getPosition() { |
332 |
return this.position; |
333 |
} |
334 |
|
335 |
public String getTooltip() { |
336 |
return this.tip; |
337 |
} |
338 |
|
339 |
public Object getValue(String key) { |
340 |
if (Action.ACTION_COMMAND_KEY.equalsIgnoreCase(key)) { |
341 |
return this.command; |
342 |
} |
343 |
if (Action.LONG_DESCRIPTION.equalsIgnoreCase(key)) { |
344 |
return this.tip; |
345 |
} |
346 |
if (Action.NAME.equalsIgnoreCase(key)) { |
347 |
return this.name; |
348 |
} |
349 |
if (Action.SMALL_ICON.equalsIgnoreCase(key)) { |
350 |
return this.getIcon(); |
351 |
} |
352 |
if (Action.ACTION_COMMAND_KEY.equalsIgnoreCase(key)) { |
353 |
return this.command; |
354 |
} |
355 |
if (Action.ACCELERATOR_KEY.equalsIgnoreCase(key)) { |
356 |
return this.getKeyStroke(); |
357 |
} |
358 |
if (Action.LARGE_ICON_KEY.equalsIgnoreCase(key)) { |
359 |
return this.getIcon(); |
360 |
} |
361 |
if (ActionInfo.ICON_NAME.equalsIgnoreCase(key)) {
|
362 |
return this.iconName; |
363 |
} |
364 |
if (Action.SHORT_DESCRIPTION.equalsIgnoreCase(key)) { |
365 |
return this.text; |
366 |
} |
367 |
if (ActionInfo.TOOLTIP.equalsIgnoreCase(key)) {
|
368 |
return this.text; |
369 |
} |
370 |
if (ActionInfo.POSITION.equalsIgnoreCase(key)) {
|
371 |
return this.position; |
372 |
} |
373 |
if (ActionInfo.REDIRECTIONS.equalsIgnoreCase(key)) {
|
374 |
return this.redirections; |
375 |
} |
376 |
if (ActionInfo.REDIRECTION.equalsIgnoreCase(key)) {
|
377 |
return this.getRedirection(); |
378 |
} |
379 |
if (ActionInfo.EXTENSION.equalsIgnoreCase(key)) {
|
380 |
return this.getExtension(); |
381 |
} |
382 |
if (ActionInfo.EXTENSION_NAME.equalsIgnoreCase(key)) {
|
383 |
return this.getExtensionName(); |
384 |
} |
385 |
if (ActionInfo.PLUGIN.equalsIgnoreCase(key)) {
|
386 |
return this.getPlugin(); |
387 |
} |
388 |
if (ActionInfo.PLUGIN_NAME.equalsIgnoreCase(key)) {
|
389 |
return this.getPluginName(); |
390 |
} |
391 |
if (ActionInfo.VISIBLE.equalsIgnoreCase(key)) {
|
392 |
return this.isVisible(); |
393 |
} |
394 |
if (ActionInfo.ACTIVE.equalsIgnoreCase(key)) {
|
395 |
return this.active; |
396 |
} |
397 |
if (ActionInfo.ACCELERATOR.equalsIgnoreCase(key)) {
|
398 |
return this.accelerator; |
399 |
} |
400 |
return super.getValue(key); |
401 |
} |
402 |
|
403 |
public void putValue(String key, Object newValue) { |
404 |
super.putValue(key, newValue);
|
405 |
// This class is immutable, only "active" can be changed
|
406 |
if (ActionInfo.ACTIVE.equalsIgnoreCase(key)) {
|
407 |
this.setActive(this.active); |
408 |
} |
409 |
} |
410 |
|
411 |
public String toString() { |
412 |
StringBuffer buffer = new StringBuffer(); |
413 |
buffer.append("ActionInfo {");
|
414 |
buffer.append("name='").append(this.name).append("', "); |
415 |
buffer.append("active='").append(this.active).append("', "); |
416 |
buffer.append("label='").append(this.text).append("', "); |
417 |
buffer.append("tooltip='").append(this.tip).append("', "); |
418 |
buffer.append("actionCommand='").append(this.command).append("', "); |
419 |
buffer.append("position='").append(this.position).append("', "); |
420 |
buffer.append("icon='").append(this.iconName).append("', "); |
421 |
buffer.append("extension='").append(this.getExtensionName()) |
422 |
.append("', ");
|
423 |
if (this.redirections != null) { |
424 |
buffer.append("redirection=(");
|
425 |
for (ActionInfo redirection : this.redirections) { |
426 |
buffer.append(redirection.getName()); |
427 |
buffer.append(" ");
|
428 |
} |
429 |
buffer.append("), ");
|
430 |
} else {
|
431 |
buffer.append("redirections=( ), ");
|
432 |
} |
433 |
buffer.append("accelerator='").append(this.accelerator); |
434 |
buffer.append("' }");
|
435 |
return buffer.toString();
|
436 |
} |
437 |
|
438 |
public boolean isActive() { |
439 |
return this.active; |
440 |
} |
441 |
|
442 |
public void setActive(boolean active) { |
443 |
logger.info("setActive({})", active);
|
444 |
this.active = active;
|
445 |
} |
446 |
|
447 |
} |