Statistics
| Revision:

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

History | View | Annotate | Download (5.35 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.andami.actioninfo.impl;
25

    
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.Comparator;
29
import java.util.HashMap;
30
import java.util.Iterator;
31
import java.util.List;
32
import java.util.Map;
33

    
34
import org.gvsig.andami.actioninfo.ActionInfo;
35
import org.gvsig.andami.actioninfo.ActionInfoManager;
36
import org.gvsig.andami.actioninfo.ActionInfoStatusCache;
37
import org.gvsig.andami.plugins.IExtension;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.identitymanagement.SimpleIdentityManager;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

    
43
public class DefaultActionInfoManager implements  ActionInfoManager {
44
        private static Logger logger = LoggerFactory.getLogger(DefaultActionInfoManager.class);
45
        
46
        private Map<String, ActionInfo>actions = new HashMap<String, ActionInfo>();
47
        private int anonymousCounter = 1;
48
        
49
    public ActionInfo createAction(Class<? extends IExtension> extension, String name, String text, String command, String icon, String accelerator, long position, String tip) {
50
            name = emptyToNull(name);
51
            String actionName = name;
52
            if( actionName==null ){
53
                    actionName = String.format("anonymous__%04d",this.anonymousCounter++);
54
            }
55
            ActionInfo action = new DefaultActionInfo(extension, actionName, text, command, icon, accelerator, position, tip);
56
            ActionInfo previous = this.getAction(action.getName());
57
            if( previous != null ) {
58
                    ((DefaultActionInfo)action).merge(previous);
59
            }
60
            if( name == null  ){
61
                    logger.info("createAction: name of action is null/empty, rename to '"+actionName+"' ("+action.toString()+").");
62
            }
63
            if( action.getLabel()==null && action.getIconName()==null ) {
64
                    logger.info("createAction(name='"+name+"'): text and icon of action is null");
65
            }
66
            return action;
67
    }
68
        
69
        private String emptyToNull(String s) {
70
                if( s==null ) {
71
                        return null;
72
                }
73
                return "".equals(s.trim())? null:s;
74
        }
75
        
76
    public ActionInfo registerAction(ActionInfo action) {
77
            if( action == null ) {
78
                    // Avisamos en el log de que se intenta registrar una accion null, intentado
79
                    // sacar el stack para que se pueda ver quien lo esta haciendo, pero no
80
                    // provocamos un error, solo retornamos null.
81
                        try {
82
                                throw new IllegalArgumentException();
83
                        } catch (IllegalArgumentException e) {
84
                                logger.info("registerAction(null).", e);
85
                        }
86
                        return null;
87
            }
88
            ActionInfo previous = this.getAction(action.getName());
89
            if( previous != null ) {
90
                    ((DefaultActionInfo)previous).merge(action);
91
                return previous;
92
            } else {
93
                    this.actions.put(action.getName(), action);
94
                SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager();
95
                identityManager.registerAction(action.getName());
96
                return action;
97
            }
98
    }
99
    
100
    public ActionInfo getAction(String name) {
101
            if( name == null || "".equals(name) ) {
102
                        try {
103
                                throw new IllegalArgumentException();
104
                        } catch (IllegalArgumentException e) {
105
                                logger.info("getAction(null/empty) return null.", e);
106
                                return null;
107
                        }
108
            }
109
            return this.actions.get(name);
110
    }
111
    
112
    public Iterator<ActionInfo> getActions() {
113
            List<ActionInfo> actions = new ArrayList<ActionInfo>();
114
            actions.addAll(this.actions.values());
115
            Collections.sort(actions, new Comparator<ActionInfo>() {
116
                    public int compare(ActionInfo arg0, ActionInfo arg1) {
117
                            String s0 = String.format("%s/%012d", arg0.getPluginName(), arg0.getPosition());
118
                            String s1 = String.format("%s/%012d", arg1.getPluginName(), arg1.getPosition());
119
                            return s0.compareTo(s1);
120
                    };
121
                });
122
            return actions.iterator();
123
    }
124
    
125
    public ActionInfoStatusCache createActionStatusCache() {
126
            return new DefaultActionInfoStatusCache();
127
    }
128

    
129
        public void redirect(String sourceName, String targetName) {
130
                ActionInfo source = this.getAction(sourceName);
131
                if( source == null ) {
132
                        throw new IllegalArgumentException("Can't locate source action '"+sourceName+"'.");
133
                }
134
                ActionInfo target = this.getAction(targetName);
135
                if( target == null ) {
136
                        throw new IllegalArgumentException("Can't locate target action '"+targetName+"'.");
137
                }
138
                source.getRedirections().add(target);
139
        }
140

    
141
    public void execute(String actionName, Object[] parameters) {
142
            ActionInfo action = this.actions.get(actionName);
143
            if( action == null ) {
144
                return;
145
            }
146
            action.execute(parameters);
147
    }
148

    
149
        
150
}