svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / actioninfo / impl / DefaultActionInfoManager.java @ 41619
History | View | Annotate | Download (5.1 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.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
public class DefaultActionInfoManager implements ActionInfoManager { |
42 |
private static Logger logger = LoggerFactory.getLogger(DefaultActionInfoManager.class); |
43 |
|
44 |
private Map<String, ActionInfo>actions = new HashMap<String, ActionInfo>(); |
45 |
private int anonymousCounter = 1; |
46 |
|
47 |
public ActionInfo createAction(Class<? extends IExtension> extension, String name, String text, String command, String icon, String accelerator, long position, String tip) { |
48 |
name = emptyToNull(name); |
49 |
String actionName = name;
|
50 |
if( actionName==null ){ |
51 |
actionName = String.format("anonymous__%04d",this.anonymousCounter++); |
52 |
} |
53 |
ActionInfo action = new DefaultActionInfo(extension, actionName, text, command, icon, accelerator, position, tip);
|
54 |
ActionInfo previous = this.getAction(action.getName());
|
55 |
if( previous != null ) { |
56 |
((DefaultActionInfo)action).merge(previous); |
57 |
} |
58 |
if( name == null ){ |
59 |
logger.info("createAction: name of action is null/empty, rename to '"+actionName+"' ("+action.toString()+")."); |
60 |
} |
61 |
if( action.getLabel()==null && action.getIconName()==null ) { |
62 |
logger.info("createAction(name='"+name+"'): text and icon of action is null"); |
63 |
} |
64 |
return action;
|
65 |
} |
66 |
|
67 |
private String emptyToNull(String s) { |
68 |
if( s==null ) { |
69 |
return null; |
70 |
} |
71 |
return "".equals(s.trim())? null:s; |
72 |
} |
73 |
|
74 |
public ActionInfo registerAction(ActionInfo action) {
|
75 |
if( action == null ) { |
76 |
// Avisamos en el log de que se intenta registrar una accion null, intentado
|
77 |
// sacar el stack para que se pueda ver quien lo esta haciendo, pero no
|
78 |
// provocamos un error, solo retornamos null.
|
79 |
try {
|
80 |
throw new IllegalArgumentException(); |
81 |
} catch (IllegalArgumentException e) { |
82 |
logger.info("registerAction(null).", e);
|
83 |
} |
84 |
return null; |
85 |
} |
86 |
ActionInfo previous = this.getAction(action.getName());
|
87 |
if( previous != null ) { |
88 |
((DefaultActionInfo)previous).merge(action); |
89 |
return previous;
|
90 |
} else {
|
91 |
this.actions.put(action.getName(), action);
|
92 |
return action;
|
93 |
} |
94 |
} |
95 |
|
96 |
public ActionInfo getAction(String name) { |
97 |
if( name == null || "".equals(name) ) { |
98 |
try {
|
99 |
throw new IllegalArgumentException(); |
100 |
} catch (IllegalArgumentException e) { |
101 |
logger.info("getAction(null/empty) return null.", e);
|
102 |
return null; |
103 |
} |
104 |
} |
105 |
return this.actions.get(name); |
106 |
} |
107 |
|
108 |
public Iterator<ActionInfo> getActions() { |
109 |
List<ActionInfo> actions = new ArrayList<ActionInfo>(); |
110 |
actions.addAll(this.actions.values());
|
111 |
Collections.sort(actions, new Comparator<ActionInfo>() { |
112 |
public int compare(ActionInfo arg0, ActionInfo arg1) { |
113 |
String s0 = String.format("%s/%012d", arg0.getPluginName(), arg0.getPosition()); |
114 |
String s1 = String.format("%s/%012d", arg1.getPluginName(), arg1.getPosition()); |
115 |
return s0.compareTo(s1);
|
116 |
}; |
117 |
}); |
118 |
return actions.iterator();
|
119 |
} |
120 |
|
121 |
public ActionInfoStatusCache createActionStatusCache() {
|
122 |
return new DefaultActionInfoStatusCache(); |
123 |
} |
124 |
|
125 |
public void redirect(String sourceName, String targetName) { |
126 |
ActionInfo source = this.getAction(sourceName);
|
127 |
if( source == null ) { |
128 |
throw new IllegalArgumentException("Can't locate source action '"+sourceName+"'."); |
129 |
} |
130 |
ActionInfo target = this.getAction(targetName);
|
131 |
if( target == null ) { |
132 |
throw new IllegalArgumentException("Can't locate target action '"+targetName+"'."); |
133 |
} |
134 |
source.getRedirections().add(target); |
135 |
} |
136 |
|
137 |
public void execute(String actionName, Object[] parameters) { |
138 |
ActionInfo action = this.actions.get(actionName);
|
139 |
if( action == null ) { |
140 |
return;
|
141 |
} |
142 |
action.execute(parameters); |
143 |
} |
144 |
|
145 |
|
146 |
} |