Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / actioninfo / impl / DefaultActionInfo.java @ 42161

History | View | Annotate | Download (15.7 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
    DefaultActionInfo(Class<? extends IExtension> extension, String name,
79
            String text, String command, String icon, String accelerator,
80
            long position, String tip) {
81
        this.extensionClass = extension;
82
        this.name = name;
83
        this.text = emptyToNull(text);
84
        this.command = emptyToNull(command);
85
        this.iconName = emptyToNull(icon);
86
        this.accelerator = emptyToNull(accelerator);
87
        this.position = position;
88
        this.tip = emptyToNull(tip);
89
        this.redirections = null;
90
        this.active = true;
91

    
92
        fixIcon();
93
    }
94

    
95
    public Object clone() throws CloneNotSupportedException {
96
        DefaultActionInfo other = (DefaultActionInfo) super.clone();
97
        if (other.redirections != null) {
98
            other.redirections = new ArrayList<ActionInfo>();
99
            other.redirections.addAll(this.redirections);
100
        }
101
        return other;
102
    }
103

    
104
    private void fixIcon() {
105
        if (iconName != null && (iconName.contains("/") || iconName.contains("."))) {
106
            // it's a file path
107
            String name = FilenameUtils.getBaseName(iconName);
108
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
109
            URL resource = null;
110
            try {
111
                resource = this.extensionClass.getClassLoader().getResource(iconName);
112
            } catch (Exception e) {
113
                return;
114
            }
115
            if (resource == null) {
116
                return;
117
            }
118
            iconTheme.registerDefault(this.getPluginName(), "broken", name, null, resource);
119
            logger.info("Plugin " + this.getPluginName() + " contains icons out of icon theme (" + iconName + ")");
120
            iconName = name;
121
        }
122
    }
123

    
124
    private String emptyToNull(String s) {
125
        if (s == null) {
126
            return null;
127
        }
128
        return s.trim().length() < 0 ? null : s;
129
    }
130

    
131
    public Collection<ActionInfo> getRedirections() {
132
        if (this.redirections == null) {
133
            this.redirections = new ArrayList<ActionInfo>();
134
        }
135
        return this.redirections;
136
    }
137

    
138
    public void merge(ActionInfo other) {
139
        if (this.extensionClass == null) {
140
            this.extensionClass = other.getExtension().getClass();
141
            this.extension = other.getExtension();
142
        }
143
        if (this.text == null) {
144
            this.text = other.getLabel();
145
        }
146
        if (this.command == null) {
147
            this.command = other.getCommand();
148
        }
149
        if (this.iconName == null) {
150
            this.iconName = other.getIconName();
151
        }
152
        if (this.accelerator == null) {
153
            this.accelerator = other.getAccelerator();
154
        }
155
        if (this.position < 1) {
156
            this.position = other.getPosition();
157
        }
158
        if (this.tip == null) {
159
            this.tip = other.getTooltip();
160
        }
161

    
162
    }
163

    
164
    public PluginServices getPlugin() {
165
        PluginServices plugin = PluginsLocator.getManager().getPlugin(
166
                this.extensionClass);
167
        return plugin;
168
    }
169

    
170
    public String getPluginName() {
171
        if (this.getPlugin() == null) {
172
            return null;
173
        }
174
        return this.getPlugin().getPluginName();
175
    }
176

    
177
    public IExtension getExtension() {
178
        if (this.extension == null) {
179
            this.extension = PluginsLocator.getManager().getExtension(
180
                    this.extensionClass);
181
        }
182
        return this.extension;
183
    }
184

    
185
    public String getExtensionName() {
186
        if (this.extensionClass == null) {
187
            return null;
188
        }
189
        return this.extensionClass.getName();
190
    }
191

    
192
    private SimpleIdentityManager getIdentityManager() {
193
        if( this.identityManager == null ) {
194
            this.identityManager = ToolsLocator.getIdentityManager();
195
        }
196
        return this.identityManager;
197
    }
198

    
199
    private SimpleIdentity getCurrentUser() {
200
        return this.getIdentityManager().getCurrentIdentity();
201
    }
202

    
203
    public boolean isVisible() {
204
        if( !this.getCurrentUser().isAuthorized(this.getName()) ) {
205
            return false;
206
        }
207
        if (!this.isActive()) {
208
            logger.info("isVisible(), action {} not active", this.getName());
209
            return false;
210
        }
211
        ActionInfo redirection = this.getRedirection();
212
        if (redirection != null) {
213
            return redirection.isVisible();
214
        }
215

    
216
        ExclusiveUIExtension eui = PluginsLocator.getManager()
217
                .getExclusiveUIExtension();
218
        if (eui == null) {
219
            return ExtensionHelper.isVisible(this.getExtension(), this.command);
220
        }
221
        return eui.isVisible(this.getExtension());
222
    }
223

    
224
    public boolean isEnabled() {
225
        boolean value;
226
        if( !this.getCurrentUser().isAuthorized(this.getName()) ) {
227
            return false;
228
        }
229
        if (!this.isActive()) {
230
            logger.info("isEnabled(), action {} not active", this.getName());
231
            value = false;
232
        } else {
233
            ActionInfo redirection = this.getRedirection();
234
            if (redirection != null) {
235
                value = true;
236
            } else {
237
                ExclusiveUIExtension eui = PluginsLocator.getManager()
238
                        .getExclusiveUIExtension();
239
                if (eui == null) {
240
                    value = ExtensionHelper.isEnabled(this.getExtension(), this.command);
241
                } else {
242
                    value = eui.isEnabled(this.getExtension());
243
                }
244
            }
245
        }
246
        if (this.previousEnabled == null || this.previousEnabled.booleanValue() != value) {
247
            this.setEnabled(value); // force call listeners
248
        }
249
        return value;
250
    }
251

    
252
    private ActionInfo getRedirection() {
253
        if (this.redirections == null) {
254
            return null;
255
        }
256
        for (int n = this.redirections.size() - 1; n >= 0; n--) {
257
            ActionInfo redirection = this.redirections.get(n);
258
            if (redirection.isEnabled()) {
259
                return redirection;
260
            }
261
        }
262
        return null;
263
    }
264

    
265
    public void execute() {
266
//                if (!this.isActive()) {
267
//                        logger.info("execute(), action {} not active",  this.getName());
268
//                        return;
269
//                }
270
        if( !this.getCurrentUser().isAuthorized(this.getName()) ) {
271
            logger.warn("Current user '"+this.getCurrentUser().getID()+"' not authorized to execute this action '"+this.getName()+"'.");
272
            return ;
273
        }
274
        ActionInfo redirection = this.getRedirection();
275
        if (redirection != null) {
276
            logger.info("{}.execute('{}') redirected", this.getPluginName()
277
                    + ":" + this.getExtensionName(), this.getCommand());
278
            redirection.execute();
279
            return;
280
        }
281
        logger.info("{}.execute('{}')",
282
                this.getPluginName() + ":" + this.getExtensionName(),
283
                this.getCommand());
284
        this.getExtension().execute(this.command);
285
    }
286

    
287
    public void execute(Object[] args) {
288
//                if (!this.isActive()) {
289
//                        logger.info("execute(args), action {} not active", this.getName());
290
//                        return;
291
//                }
292
        if( !this.getCurrentUser().isAuthorized(this.getName()) ) {
293
            logger.warn("Current user '"+this.getCurrentUser().getID()+"' not authorized to execute this action '"+this.getName()+"'.");
294
            return ;
295
        }
296
        ActionInfo redirection = this.getRedirection();
297
        if (redirection != null) {
298
            logger.info("{}.execute('{}', args) redirected", this.getPluginName()
299
                    + ":" + this.getExtensionName(), this.getCommand());
300
            redirection.execute(args);
301
            return;
302
        }
303
        logger.info("{}.execute('{}', Object[] args)",
304
                this.getPluginName() + ":" + this.getExtensionName(),
305
                this.getCommand());
306
        ExtensionHelper.execute(this.getExtension(), this.command, args);
307
    }
308

    
309
    public void execute(Object arg) {
310
        if (arg instanceof Object[]) {
311
            execute((Object[]) arg);
312
            return;
313
        }
314
        execute(new Object[]{arg});
315
    }
316

    
317
    public void execute(Map args) {
318
        if( !this.getCurrentUser().isAuthorized(this.getName()) ) {
319
            logger.warn("Current user '"+this.getCurrentUser().getID()+"' not authorized to execute this action '"+this.getName()+"'.");
320
            return ;
321
        }
322
        logger.info("{}.execute('{}', Map args)",
323
                this.getPluginName() + ":" + this.getExtensionName(),
324
                this.getCommand());
325
        ExtensionHelper.execute(this.getExtension(), this.command, new Object[]{args});
326
    }
327

    
328
    public void actionPerformed(ActionEvent arg0) {
329
        this.execute();
330
    }
331

    
332
    public String getName() {
333
        return this.name;
334
    }
335

    
336
    public String getLabel() {
337
        return this.text;
338
    }
339

    
340
    public String getCommand() {
341
        return this.command;
342
    }
343

    
344
    public String getIconName() {
345
        return this.iconName;
346
    }
347

    
348
    public ImageIcon getIcon() {
349
        IconTheme iconTheme = PluginServices.getIconTheme();
350
        return iconTheme.get(this.iconName);
351
    }
352

    
353
    public String getAccelerator() {
354
        return this.accelerator;
355
    }
356

    
357
    public KeyStroke getKeyStroke() {
358
        if (emptyToNull(this.accelerator) == null) {
359
            return null;
360
        }
361
        return KeyMapping.getKeyStroke(this.accelerator);
362
    }
363

    
364
    public long getPosition() {
365
        return this.position;
366
    }
367

    
368
    public String getTooltip() {
369
        return this.tip;
370
    }
371

    
372
    public Object getValue(String key) {
373
        if (Action.ACTION_COMMAND_KEY.equalsIgnoreCase(key)) {
374
            return this.command;
375
        }
376
        if (Action.LONG_DESCRIPTION.equalsIgnoreCase(key)) {
377
            return this.tip;
378
        }
379
        if (Action.NAME.equalsIgnoreCase(key)) {
380
            return this.name;
381
        }
382
        if (Action.SMALL_ICON.equalsIgnoreCase(key)) {
383
            return this.getIcon();
384
        }
385
        if (Action.ACTION_COMMAND_KEY.equalsIgnoreCase(key)) {
386
            return this.command;
387
        }
388
        if (Action.ACCELERATOR_KEY.equalsIgnoreCase(key)) {
389
            return this.getKeyStroke();
390
        }
391
        if (Action.LARGE_ICON_KEY.equalsIgnoreCase(key)) {
392
            return this.getIcon();
393
        }
394
        if (ActionInfo.ICON_NAME.equalsIgnoreCase(key)) {
395
            return this.iconName;
396
        }
397
        if (Action.SHORT_DESCRIPTION.equalsIgnoreCase(key)) {
398
            return this.getLabel();
399
        }
400
        if (ActionInfo.TOOLTIP.equalsIgnoreCase(key)) {
401
            return this.getTooltip();
402
        }
403
        if (ActionInfo.POSITION.equalsIgnoreCase(key)) {
404
            return this.position;
405
        }
406
        if (ActionInfo.REDIRECTIONS.equalsIgnoreCase(key)) {
407
            return this.redirections;
408
        }
409
        if (ActionInfo.REDIRECTION.equalsIgnoreCase(key)) {
410
            return this.getRedirection();
411
        }
412
        if (ActionInfo.EXTENSION.equalsIgnoreCase(key)) {
413
            return this.getExtension();
414
        }
415
        if (ActionInfo.EXTENSION_NAME.equalsIgnoreCase(key)) {
416
            return this.getExtensionName();
417
        }
418
        if (ActionInfo.PLUGIN.equalsIgnoreCase(key)) {
419
            return this.getPlugin();
420
        }
421
        if (ActionInfo.PLUGIN_NAME.equalsIgnoreCase(key)) {
422
            return this.getPluginName();
423
        }
424
        if (ActionInfo.VISIBLE.equalsIgnoreCase(key)) {
425
            return this.isVisible();
426
        }
427
        if (ActionInfo.ACTIVE.equalsIgnoreCase(key)) {
428
            return this.active;
429
        }
430
        if (ActionInfo.ACCELERATOR.equalsIgnoreCase(key)) {
431
            return this.accelerator;
432
        }
433
        return super.getValue(key);
434
    }
435

    
436
    public void putValue(String key, Object newValue) {
437
        super.putValue(key, newValue);
438
        // This class is immutable, only "active" can be changed
439
        if (ActionInfo.ACTIVE.equalsIgnoreCase(key)) {
440
            this.setActive(this.active);
441
        }
442
    }
443

    
444
    public String toString() {
445
        StringBuffer buffer = new StringBuffer();
446
        buffer.append("ActionInfo {");
447
        buffer.append("name='").append(this.name).append("', ");
448
        buffer.append("active='").append(this.active).append("', ");
449
        buffer.append("label='").append(this.text).append("', ");
450
        buffer.append("tooltip='").append(this.tip).append("', ");
451
        buffer.append("actionCommand='").append(this.command).append("', ");
452
        buffer.append("position='").append(this.position).append("', ");
453
        buffer.append("icon='").append(this.iconName).append("', ");
454
        buffer.append("extension='").append(this.getExtensionName())
455
                .append("', ");
456
        if (this.redirections != null) {
457
            buffer.append("redirection=(");
458
            for (ActionInfo redirection : this.redirections) {
459
                buffer.append(redirection.getName());
460
                buffer.append(" ");
461
            }
462
            buffer.append("), ");
463
        } else {
464
            buffer.append("redirections=( ), ");
465
        }
466
        buffer.append("accelerator='").append(this.accelerator);
467
        buffer.append("' }");
468
        return buffer.toString();
469
    }
470

    
471
    public boolean isActive() {
472
        return this.active;
473
    }
474

    
475
    public void setActive(boolean active) {
476
        logger.info("setActive({})", active);
477
        this.active = active;
478
    }
479

    
480
}