3 |
3 |
*
|
4 |
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
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.
|
|
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 |
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.
|
|
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 |
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.
|
|
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.
|
20 |
19 |
*
|
21 |
|
* For any additional information, do not hesitate to contact us
|
22 |
|
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
23 |
22 |
*/
|
24 |
23 |
package org.gvsig.andami.actioninfo.impl;
|
25 |
24 |
|
... | ... | |
40 |
39 |
import org.slf4j.Logger;
|
41 |
40 |
import org.slf4j.LoggerFactory;
|
42 |
41 |
|
43 |
|
public class DefaultActionInfoManager implements ActionInfoManager {
|
44 |
|
private static Logger logger = LoggerFactory.getLogger(DefaultActionInfoManager.class);
|
|
42 |
public class DefaultActionInfoManager implements ActionInfoManager {
|
45 |
43 |
|
46 |
|
private Map<String, ActionInfo>actions = new HashMap<String, ActionInfo>();
|
47 |
|
private int anonymousCounter = 1;
|
|
44 |
private static Logger logger = LoggerFactory.getLogger(DefaultActionInfoManager.class);
|
48 |
45 |
|
|
46 |
private Map<String, ActionInfo> actions = new HashMap<String, ActionInfo>();
|
|
47 |
private int anonymousCounter = 1;
|
|
48 |
|
49 |
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;
|
|
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 |
67 |
}
|
68 |
68 |
|
69 |
|
private String emptyToNull(String s) {
|
70 |
|
if( s==null ) {
|
71 |
|
return null;
|
72 |
|
}
|
73 |
|
return "".equals(s.trim())? null:s;
|
74 |
|
}
|
|
69 |
public ActionInfo createAction(IExtension extension, String name, String text, String command, String icon, String accelerator, long position, String tip) {
|
|
70 |
name = emptyToNull(name);
|
|
71 |
String actionName = name;
|
|
72 |
if (actionName == null) {
|
|
73 |
actionName = String.format("anonymous__%04d", this.anonymousCounter++);
|
|
74 |
}
|
|
75 |
ActionInfo action = new DefaultActionInfo(extension, actionName, text, command, icon, accelerator, position, tip);
|
|
76 |
ActionInfo previous = this.getAction(action.getName());
|
|
77 |
if (previous != null) {
|
|
78 |
((DefaultActionInfo) action).merge(previous);
|
|
79 |
}
|
|
80 |
if (name == null) {
|
|
81 |
logger.info("createAction: name of action is null/empty, rename to '" + actionName + "' (" + action.toString() + ").");
|
|
82 |
}
|
|
83 |
if (action.getLabel() == null && action.getIconName() == null) {
|
|
84 |
logger.info("createAction(name='" + name + "'): text and icon of action is null");
|
|
85 |
}
|
|
86 |
return action;
|
|
87 |
}
|
75 |
88 |
|
|
89 |
private String emptyToNull(String s) {
|
|
90 |
if (s == null) {
|
|
91 |
return null;
|
|
92 |
}
|
|
93 |
return "".equals(s.trim()) ? null : s;
|
|
94 |
}
|
|
95 |
|
76 |
96 |
public ActionInfo registerAction(ActionInfo action) {
|
77 |
|
if( action == null ) {
|
|
97 |
if (action == null) {
|
78 |
98 |
// 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 |
|
}
|
|
99 |
// sacar el stack para que se pueda ver quien lo esta haciendo, pero no
|
|
100 |
// provocamos un error, solo retornamos null.
|
|
101 |
try {
|
|
102 |
throw new IllegalArgumentException();
|
|
103 |
} catch (IllegalArgumentException e) {
|
|
104 |
logger.info("registerAction(null).", e);
|
|
105 |
}
|
|
106 |
return null;
|
|
107 |
}
|
|
108 |
ActionInfo previous = this.getAction(action.getName());
|
|
109 |
if (previous != null) {
|
|
110 |
((DefaultActionInfo) previous).merge(action);
|
|
111 |
return previous;
|
|
112 |
} else {
|
|
113 |
this.actions.put(action.getName(), action);
|
|
114 |
SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager();
|
|
115 |
identityManager.registerAction(action.getName());
|
|
116 |
return action;
|
|
117 |
}
|
98 |
118 |
}
|
99 |
119 |
|
100 |
120 |
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);
|
|
121 |
if (name == null || "".equals(name)) {
|
|
122 |
try {
|
|
123 |
throw new IllegalArgumentException();
|
|
124 |
} catch (IllegalArgumentException e) {
|
|
125 |
logger.info("getAction(null/empty) return null.", e);
|
|
126 |
return null;
|
|
127 |
}
|
|
128 |
}
|
|
129 |
return this.actions.get(name);
|
110 |
130 |
}
|
111 |
131 |
|
112 |
132 |
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 |
|
});
|
|
133 |
List<ActionInfo> actions = new ArrayList<ActionInfo>();
|
|
134 |
actions.addAll(this.actions.values());
|
|
135 |
Collections.sort(actions, new Comparator<ActionInfo>() {
|
|
136 |
public int compare(ActionInfo arg0, ActionInfo arg1) {
|
|
137 |
String s0 = String.format("%s/%012d", arg0.getPluginName(), arg0.getPosition());
|
|
138 |
String s1 = String.format("%s/%012d", arg1.getPluginName(), arg1.getPosition());
|
|
139 |
return s0.compareTo(s1);
|
|
140 |
}
|
|
141 |
;
|
|
142 |
});
|
122 |
143 |
return actions.iterator();
|
123 |
144 |
}
|
124 |
145 |
|
125 |
146 |
public ActionInfoStatusCache createActionStatusCache() {
|
126 |
|
return new DefaultActionInfoStatusCache();
|
|
147 |
return new DefaultActionInfoStatusCache();
|
127 |
148 |
}
|
128 |
149 |
|
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 |
|
}
|
|
150 |
public void redirect(String sourceName, String targetName) {
|
|
151 |
ActionInfo source = this.getAction(sourceName);
|
|
152 |
if (source == null) {
|
|
153 |
throw new IllegalArgumentException("Can't locate source action '" + sourceName + "'.");
|
|
154 |
}
|
|
155 |
ActionInfo target = this.getAction(targetName);
|
|
156 |
if (target == null) {
|
|
157 |
throw new IllegalArgumentException("Can't locate target action '" + targetName + "'.");
|
|
158 |
}
|
|
159 |
source.getRedirections().add(target);
|
|
160 |
}
|
140 |
161 |
|
141 |
162 |
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);
|
|
163 |
ActionInfo action = this.actions.get(actionName);
|
|
164 |
if (action == null) {
|
|
165 |
return;
|
|
166 |
}
|
|
167 |
action.execute(parameters);
|
147 |
168 |
}
|
148 |
169 |
|
149 |
170 |
public ActionInfo getTranslated(ActionInfo actionInfo) {
|
150 |
171 |
return new TranslatedActionInfo(actionInfo);
|
151 |
172 |
}
|
152 |
173 |
|
153 |
|
|
154 |
174 |
}
|