svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / actioninfo / impl / DefaultActionInfo.java @ 42348
History | View | Annotate | Download (16.4 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.ToolsLocator; |
46 |
import org.gvsig.tools.identitymanagement.SimpleIdentity; |
47 |
import org.gvsig.tools.identitymanagement.SimpleIdentityManager; |
48 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
49 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
50 |
import org.slf4j.Logger; |
51 |
import org.slf4j.LoggerFactory; |
52 |
|
53 |
public class DefaultActionInfo extends AbstractAction implements ActionInfo { |
54 |
|
55 |
/**
|
56 |
*
|
57 |
*/
|
58 |
private static final long serialVersionUID = 1620939552263334110L; |
59 |
|
60 |
private static Logger logger = LoggerFactory |
61 |
.getLogger(DefaultActionInfo.class); |
62 |
|
63 |
private Class<? extends IExtension> extensionClass; |
64 |
private IExtension extension;
|
65 |
private String name; |
66 |
private String text; |
67 |
private String command; |
68 |
private String iconName; |
69 |
private String accelerator; |
70 |
private long position; |
71 |
private String tip; |
72 |
private List<ActionInfo> redirections; |
73 |
private boolean active; |
74 |
|
75 |
private Boolean previousEnabled = null; |
76 |
private SimpleIdentityManager identityManager;
|
77 |
|
78 |
|
79 |
DefaultActionInfo(IExtension extension, String name,
|
80 |
String text, String command, String icon, String accelerator, |
81 |
long position, String tip) { |
82 |
this.extensionClass = extension.getClass();
|
83 |
this.extension = extension;
|
84 |
this.name = name;
|
85 |
this.text = emptyToNull(text);
|
86 |
this.command = emptyToNull(command);
|
87 |
this.iconName = emptyToNull(icon);
|
88 |
this.accelerator = emptyToNull(accelerator);
|
89 |
this.position = position;
|
90 |
this.tip = emptyToNull(tip);
|
91 |
this.redirections = null; |
92 |
this.active = true; |
93 |
|
94 |
fixIcon(); |
95 |
} |
96 |
|
97 |
DefaultActionInfo(Class<? extends IExtension> extensionClass, String name, |
98 |
String text, String command, String icon, String accelerator, |
99 |
long position, String tip) { |
100 |
this.extensionClass = extensionClass;
|
101 |
this.name = name;
|
102 |
this.text = emptyToNull(text);
|
103 |
this.command = emptyToNull(command);
|
104 |
this.iconName = emptyToNull(icon);
|
105 |
this.accelerator = emptyToNull(accelerator);
|
106 |
this.position = position;
|
107 |
this.tip = emptyToNull(tip);
|
108 |
this.redirections = null; |
109 |
this.active = true; |
110 |
|
111 |
fixIcon(); |
112 |
} |
113 |
|
114 |
public Object clone() throws CloneNotSupportedException { |
115 |
DefaultActionInfo other = (DefaultActionInfo) super.clone();
|
116 |
if (other.redirections != null) { |
117 |
other.redirections = new ArrayList<ActionInfo>(); |
118 |
other.redirections.addAll(this.redirections);
|
119 |
} |
120 |
return other;
|
121 |
} |
122 |
|
123 |
private void fixIcon() { |
124 |
if (iconName != null && (iconName.contains("/") || iconName.contains("."))) { |
125 |
// it's a file path
|
126 |
String name = FilenameUtils.getBaseName(iconName);
|
127 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
128 |
URL resource = null; |
129 |
try {
|
130 |
resource = this.extensionClass.getClassLoader().getResource(iconName);
|
131 |
} catch (Exception e) { |
132 |
return;
|
133 |
} |
134 |
if (resource == null) { |
135 |
return;
|
136 |
} |
137 |
iconTheme.registerDefault(this.getPluginName(), "broken", name, null, resource); |
138 |
logger.info("Plugin " + this.getPluginName() + " contains icons out of icon theme (" + iconName + ")"); |
139 |
iconName = name; |
140 |
} |
141 |
} |
142 |
|
143 |
private String emptyToNull(String s) { |
144 |
if (s == null) { |
145 |
return null; |
146 |
} |
147 |
return s.trim().length() < 0 ? null : s; |
148 |
} |
149 |
|
150 |
public Collection<ActionInfo> getRedirections() { |
151 |
if (this.redirections == null) { |
152 |
this.redirections = new ArrayList<ActionInfo>(); |
153 |
} |
154 |
return this.redirections; |
155 |
} |
156 |
|
157 |
public void merge(ActionInfo other) { |
158 |
if (this.extensionClass == null) { |
159 |
this.extensionClass = other.getExtension().getClass();
|
160 |
this.extension = other.getExtension();
|
161 |
} |
162 |
if (this.text == null) { |
163 |
this.text = other.getLabel();
|
164 |
} |
165 |
if (this.command == null) { |
166 |
this.command = other.getCommand();
|
167 |
} |
168 |
if (this.iconName == null) { |
169 |
this.iconName = other.getIconName();
|
170 |
} |
171 |
if (this.accelerator == null) { |
172 |
this.accelerator = other.getAccelerator();
|
173 |
} |
174 |
if (this.position < 1) { |
175 |
this.position = other.getPosition();
|
176 |
} |
177 |
if (this.tip == null) { |
178 |
this.tip = other.getTooltip();
|
179 |
} |
180 |
|
181 |
} |
182 |
|
183 |
public PluginServices getPlugin() {
|
184 |
PluginServices plugin = PluginsLocator.getManager().getPlugin( |
185 |
this.extensionClass);
|
186 |
return plugin;
|
187 |
} |
188 |
|
189 |
public String getPluginName() { |
190 |
if (this.getPlugin() == null) { |
191 |
return null; |
192 |
} |
193 |
return this.getPlugin().getPluginName(); |
194 |
} |
195 |
|
196 |
public IExtension getExtension() {
|
197 |
if (this.extension == null) { |
198 |
this.extension = PluginsLocator.getManager().getExtension(
|
199 |
this.extensionClass);
|
200 |
} |
201 |
return this.extension; |
202 |
} |
203 |
|
204 |
public String getExtensionName() { |
205 |
if (this.extensionClass == null) { |
206 |
return null; |
207 |
} |
208 |
return this.extensionClass.getName(); |
209 |
} |
210 |
|
211 |
private SimpleIdentityManager getIdentityManager() {
|
212 |
if( this.identityManager == null ) { |
213 |
this.identityManager = ToolsLocator.getIdentityManager();
|
214 |
} |
215 |
return this.identityManager; |
216 |
} |
217 |
|
218 |
private SimpleIdentity getCurrentUser() {
|
219 |
return this.getIdentityManager().getCurrentIdentity(); |
220 |
} |
221 |
|
222 |
public boolean isVisible() { |
223 |
if( !this.getCurrentUser().isAuthorized(this.getName()) ) { |
224 |
return false; |
225 |
} |
226 |
if (!this.isActive()) { |
227 |
logger.info("isVisible(), action {} not active", this.getName()); |
228 |
return false; |
229 |
} |
230 |
ActionInfo redirection = this.getRedirection();
|
231 |
if (redirection != null) { |
232 |
return redirection.isVisible();
|
233 |
} |
234 |
|
235 |
ExclusiveUIExtension eui = PluginsLocator.getManager() |
236 |
.getExclusiveUIExtension(); |
237 |
if (eui == null) { |
238 |
return ExtensionHelper.isVisible(this.getExtension(), this.command); |
239 |
} |
240 |
return eui.isVisible(this.getExtension()); |
241 |
} |
242 |
|
243 |
public boolean isEnabled() { |
244 |
boolean value;
|
245 |
if( !this.getCurrentUser().isAuthorized(this.getName()) ) { |
246 |
return false; |
247 |
} |
248 |
if (!this.isActive()) { |
249 |
logger.info("isEnabled(), action {} not active", this.getName()); |
250 |
value = false;
|
251 |
} else {
|
252 |
ActionInfo redirection = this.getRedirection();
|
253 |
if (redirection != null) { |
254 |
value = true;
|
255 |
} else {
|
256 |
ExclusiveUIExtension eui = PluginsLocator.getManager() |
257 |
.getExclusiveUIExtension(); |
258 |
if (eui == null) { |
259 |
value = ExtensionHelper.isEnabled(this.getExtension(), this.command); |
260 |
} else {
|
261 |
value = eui.isEnabled(this.getExtension());
|
262 |
} |
263 |
} |
264 |
} |
265 |
if (this.previousEnabled == null || this.previousEnabled.booleanValue() != value) { |
266 |
this.setEnabled(value); // force call listeners |
267 |
} |
268 |
return value;
|
269 |
} |
270 |
|
271 |
private ActionInfo getRedirection() {
|
272 |
if (this.redirections == null) { |
273 |
return null; |
274 |
} |
275 |
for (int n = this.redirections.size() - 1; n >= 0; n--) { |
276 |
ActionInfo redirection = this.redirections.get(n);
|
277 |
if (redirection.isEnabled()) {
|
278 |
return redirection;
|
279 |
} |
280 |
} |
281 |
return null; |
282 |
} |
283 |
|
284 |
public void execute() { |
285 |
// if (!this.isActive()) {
|
286 |
// logger.info("execute(), action {} not active", this.getName());
|
287 |
// return;
|
288 |
// }
|
289 |
if( !this.getCurrentUser().isAuthorized(this.getName()) ) { |
290 |
logger.warn("Current user '"+this.getCurrentUser().getID()+"' not authorized to execute this action '"+this.getName()+"'."); |
291 |
return ;
|
292 |
} |
293 |
ActionInfo redirection = this.getRedirection();
|
294 |
if (redirection != null) { |
295 |
logger.info("{}.execute('{}') redirected", this.getPluginName() |
296 |
+ ":" + this.getExtensionName(), this.getCommand()); |
297 |
redirection.execute(); |
298 |
return;
|
299 |
} |
300 |
logger.info("{}.execute('{}')",
|
301 |
this.getPluginName() + ":" + this.getExtensionName(), |
302 |
this.getCommand());
|
303 |
this.getExtension().execute(this.command); |
304 |
} |
305 |
|
306 |
public void execute(Object[] args) { |
307 |
// if (!this.isActive()) {
|
308 |
// logger.info("execute(args), action {} not active", this.getName());
|
309 |
// return;
|
310 |
// }
|
311 |
if( !this.getCurrentUser().isAuthorized(this.getName()) ) { |
312 |
logger.warn("Current user '"+this.getCurrentUser().getID()+"' not authorized to execute this action '"+this.getName()+"'."); |
313 |
return ;
|
314 |
} |
315 |
ActionInfo redirection = this.getRedirection();
|
316 |
if (redirection != null) { |
317 |
logger.info("{}.execute('{}', args) redirected", this.getPluginName() |
318 |
+ ":" + this.getExtensionName(), this.getCommand()); |
319 |
redirection.execute(args); |
320 |
return;
|
321 |
} |
322 |
logger.info("{}.execute('{}', Object[] args)",
|
323 |
this.getPluginName() + ":" + this.getExtensionName(), |
324 |
this.getCommand());
|
325 |
ExtensionHelper.execute(this.getExtension(), this.command, args); |
326 |
} |
327 |
|
328 |
public void execute(Object arg) { |
329 |
if (arg instanceof Object[]) { |
330 |
execute((Object[]) arg); |
331 |
return;
|
332 |
} |
333 |
execute(new Object[]{arg}); |
334 |
} |
335 |
|
336 |
public void execute(Map args) { |
337 |
if( !this.getCurrentUser().isAuthorized(this.getName()) ) { |
338 |
logger.warn("Current user '"+this.getCurrentUser().getID()+"' not authorized to execute this action '"+this.getName()+"'."); |
339 |
return ;
|
340 |
} |
341 |
logger.info("{}.execute('{}', Map args)",
|
342 |
this.getPluginName() + ":" + this.getExtensionName(), |
343 |
this.getCommand());
|
344 |
ExtensionHelper.execute(this.getExtension(), this.command, new Object[]{args}); |
345 |
} |
346 |
|
347 |
public void actionPerformed(ActionEvent arg0) { |
348 |
this.execute();
|
349 |
} |
350 |
|
351 |
public String getName() { |
352 |
return this.name; |
353 |
} |
354 |
|
355 |
public String getLabel() { |
356 |
return this.text; |
357 |
} |
358 |
|
359 |
public String getCommand() { |
360 |
return this.command; |
361 |
} |
362 |
|
363 |
public String getIconName() { |
364 |
return this.iconName; |
365 |
} |
366 |
|
367 |
public ImageIcon getIcon() { |
368 |
IconTheme iconTheme = PluginServices.getIconTheme(); |
369 |
return iconTheme.get(this.iconName); |
370 |
} |
371 |
|
372 |
public String getAccelerator() { |
373 |
return this.accelerator; |
374 |
} |
375 |
|
376 |
public KeyStroke getKeyStroke() { |
377 |
if (emptyToNull(this.accelerator) == null) { |
378 |
return null; |
379 |
} |
380 |
return KeyMapping.getKeyStroke(this.accelerator); |
381 |
} |
382 |
|
383 |
public long getPosition() { |
384 |
return this.position; |
385 |
} |
386 |
|
387 |
public String getTooltip() { |
388 |
return this.tip; |
389 |
} |
390 |
|
391 |
public Object getValue(String key) { |
392 |
if (Action.ACTION_COMMAND_KEY.equalsIgnoreCase(key)) { |
393 |
return this.command; |
394 |
} |
395 |
if (Action.LONG_DESCRIPTION.equalsIgnoreCase(key)) { |
396 |
return this.tip; |
397 |
} |
398 |
if (Action.NAME.equalsIgnoreCase(key)) { |
399 |
return this.name; |
400 |
} |
401 |
if (Action.SMALL_ICON.equalsIgnoreCase(key)) { |
402 |
return this.getIcon(); |
403 |
} |
404 |
if (Action.ACTION_COMMAND_KEY.equalsIgnoreCase(key)) { |
405 |
return this.command; |
406 |
} |
407 |
if (Action.ACCELERATOR_KEY.equalsIgnoreCase(key)) { |
408 |
return this.getKeyStroke(); |
409 |
} |
410 |
if (Action.LARGE_ICON_KEY.equalsIgnoreCase(key)) { |
411 |
return this.getIcon(); |
412 |
} |
413 |
if (ActionInfo.ICON_NAME.equalsIgnoreCase(key)) {
|
414 |
return this.iconName; |
415 |
} |
416 |
if (Action.SHORT_DESCRIPTION.equalsIgnoreCase(key)) { |
417 |
return this.getLabel(); |
418 |
} |
419 |
if (ActionInfo.TOOLTIP.equalsIgnoreCase(key)) {
|
420 |
return this.getTooltip(); |
421 |
} |
422 |
if (ActionInfo.POSITION.equalsIgnoreCase(key)) {
|
423 |
return this.position; |
424 |
} |
425 |
if (ActionInfo.REDIRECTIONS.equalsIgnoreCase(key)) {
|
426 |
return this.redirections; |
427 |
} |
428 |
if (ActionInfo.REDIRECTION.equalsIgnoreCase(key)) {
|
429 |
return this.getRedirection(); |
430 |
} |
431 |
if (ActionInfo.EXTENSION.equalsIgnoreCase(key)) {
|
432 |
return this.getExtension(); |
433 |
} |
434 |
if (ActionInfo.EXTENSION_NAME.equalsIgnoreCase(key)) {
|
435 |
return this.getExtensionName(); |
436 |
} |
437 |
if (ActionInfo.PLUGIN.equalsIgnoreCase(key)) {
|
438 |
return this.getPlugin(); |
439 |
} |
440 |
if (ActionInfo.PLUGIN_NAME.equalsIgnoreCase(key)) {
|
441 |
return this.getPluginName(); |
442 |
} |
443 |
if (ActionInfo.VISIBLE.equalsIgnoreCase(key)) {
|
444 |
return this.isVisible(); |
445 |
} |
446 |
if (ActionInfo.ACTIVE.equalsIgnoreCase(key)) {
|
447 |
return this.active; |
448 |
} |
449 |
if (ActionInfo.ACCELERATOR.equalsIgnoreCase(key)) {
|
450 |
return this.accelerator; |
451 |
} |
452 |
return super.getValue(key); |
453 |
} |
454 |
|
455 |
public void putValue(String key, Object newValue) { |
456 |
super.putValue(key, newValue);
|
457 |
// This class is immutable, only "active" can be changed
|
458 |
if (ActionInfo.ACTIVE.equalsIgnoreCase(key)) {
|
459 |
this.setActive(this.active); |
460 |
} |
461 |
} |
462 |
|
463 |
public String toString() { |
464 |
StringBuffer buffer = new StringBuffer(); |
465 |
buffer.append("ActionInfo {");
|
466 |
buffer.append("name='").append(this.name).append("', "); |
467 |
buffer.append("active='").append(this.active).append("', "); |
468 |
buffer.append("label='").append(this.text).append("', "); |
469 |
buffer.append("tooltip='").append(this.tip).append("', "); |
470 |
buffer.append("actionCommand='").append(this.command).append("', "); |
471 |
buffer.append("position='").append(this.position).append("', "); |
472 |
buffer.append("icon='").append(this.iconName).append("', "); |
473 |
buffer.append("extension='").append(this.getExtensionName()) |
474 |
.append("', ");
|
475 |
if (this.redirections != null) { |
476 |
buffer.append("redirection=(");
|
477 |
for (ActionInfo redirection : this.redirections) { |
478 |
buffer.append(redirection.getName()); |
479 |
buffer.append(" ");
|
480 |
} |
481 |
buffer.append("), ");
|
482 |
} else {
|
483 |
buffer.append("redirections=( ), ");
|
484 |
} |
485 |
buffer.append("accelerator='").append(this.accelerator); |
486 |
buffer.append("' }");
|
487 |
return buffer.toString();
|
488 |
} |
489 |
|
490 |
public boolean isActive() { |
491 |
return this.active; |
492 |
} |
493 |
|
494 |
public void setActive(boolean active) { |
495 |
logger.info("setActive({})", active);
|
496 |
this.active = active;
|
497 |
} |
498 |
|
499 |
} |