root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / actioninfo / impl / DefaultActionInfo.java @ 38564
History | View | Annotate | Download (5.96 KB)
1 |
package org.gvsig.andami.actioninfo.impl; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.beans.PropertyChangeListener; |
5 |
|
6 |
import javax.swing.Action; |
7 |
import javax.swing.ImageIcon; |
8 |
import javax.swing.KeyStroke; |
9 |
|
10 |
import org.gvsig.andami.PluginServices; |
11 |
import org.gvsig.andami.PluginsLocator; |
12 |
import org.gvsig.andami.actioninfo.ActionInfo; |
13 |
import org.gvsig.andami.plugins.ExclusiveUIExtension; |
14 |
import org.gvsig.andami.plugins.ExtensionHelper; |
15 |
import org.gvsig.andami.plugins.IExtension; |
16 |
import org.gvsig.andami.ui.mdiFrame.KeyMapping; |
17 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
18 |
import org.slf4j.Logger; |
19 |
import org.slf4j.LoggerFactory; |
20 |
|
21 |
public class DefaultActionInfo implements ActionInfo { |
22 |
private static Logger logger = LoggerFactory.getLogger(DefaultActionInfo.class); |
23 |
|
24 |
private Class<? extends IExtension> extensionClass; |
25 |
private IExtension extension;
|
26 |
private String name; |
27 |
private String text; |
28 |
private String command; |
29 |
private String iconName; |
30 |
private String accelerator; |
31 |
private long position; |
32 |
private String tip; |
33 |
|
34 |
DefaultActionInfo(Class<? extends IExtension> extension, String name, String text, String command, String icon, String accelerator, long position, String tip) { |
35 |
this.extensionClass = extension;
|
36 |
this.name = name;
|
37 |
this.text = emptyToNull(text) ;
|
38 |
this.command = emptyToNull(command);
|
39 |
this.iconName = emptyToNull(icon);
|
40 |
this.accelerator = emptyToNull(accelerator);
|
41 |
this.position = position;
|
42 |
this.tip = emptyToNull(tip);
|
43 |
} |
44 |
|
45 |
|
46 |
private String emptyToNull(String s) { |
47 |
if( s==null ) { |
48 |
return null; |
49 |
} |
50 |
return s.trim().length()<0 ? null:s; |
51 |
} |
52 |
|
53 |
public void merge(ActionInfo other) { |
54 |
if( this.extensionClass == null ) { |
55 |
this.extensionClass = other.getExtension().getClass() ;
|
56 |
this.extension = other.getExtension();
|
57 |
} |
58 |
if( this.text == null ) { |
59 |
this.text = other.getLabel();
|
60 |
} |
61 |
if( this.command == null ) { |
62 |
this.command = other.getCommand();
|
63 |
} |
64 |
if( this.iconName == null ) { |
65 |
this.iconName = other.getIconName();
|
66 |
} |
67 |
if( this.accelerator == null ) { |
68 |
this.accelerator = other.getAccelerator();
|
69 |
} |
70 |
if( this.position < 1 ) { |
71 |
this.position = other.getPosition();
|
72 |
} |
73 |
if( this.tip == null ) { |
74 |
this.tip = other.getTooltip();
|
75 |
} |
76 |
|
77 |
} |
78 |
|
79 |
public PluginServices getPlugin() {
|
80 |
PluginServices plugin = PluginsLocator.getManager().getPlugin(this.extensionClass);
|
81 |
return plugin;
|
82 |
} |
83 |
|
84 |
public String getPluginName() { |
85 |
return this.getPlugin().getPluginDirectory().getName(); |
86 |
} |
87 |
|
88 |
public IExtension getExtension() {
|
89 |
if( this.extension == null ) { |
90 |
this.extension = PluginsLocator.getManager().getExtension(this.extensionClass); |
91 |
} |
92 |
return this.extension; |
93 |
} |
94 |
|
95 |
public String getExtensionName() { |
96 |
return this.extensionClass.getName(); |
97 |
} |
98 |
|
99 |
public boolean isVisible() { |
100 |
ExclusiveUIExtension eui = PluginsLocator.getManager().getExclusiveUIExtension(); |
101 |
if (eui == null) { |
102 |
return ExtensionHelper.isVisible(this.getExtension(), this.command); |
103 |
} |
104 |
return eui.isVisible(this.getExtension()); |
105 |
} |
106 |
|
107 |
public boolean isEnabled() { |
108 |
ExclusiveUIExtension eui = PluginsLocator.getManager().getExclusiveUIExtension(); |
109 |
if (eui == null) { |
110 |
return ExtensionHelper.isEnabled(this.getExtension(), this.command); |
111 |
} |
112 |
return eui.isEnabled(this.getExtension()); |
113 |
} |
114 |
|
115 |
public void execute() { |
116 |
logger.info("{}.execute('{}')", this.getPluginName()+":"+this.getExtensionName(), this.getCommand()); |
117 |
this.getExtension().execute(this.command); |
118 |
} |
119 |
|
120 |
public void execute(Object[] args) { |
121 |
logger.info("{}.execute('{}')", this.getPluginName()+":"+this.getExtensionName(), this.getCommand()); |
122 |
ExtensionHelper.execute(this.getExtension(), this.command, args); |
123 |
} |
124 |
|
125 |
public void execute(Object arg) { |
126 |
if( arg instanceof Object[] ) { |
127 |
execute((Object[])arg); |
128 |
return;
|
129 |
} |
130 |
execute( new Object[] { arg } ); |
131 |
} |
132 |
|
133 |
public void actionPerformed(ActionEvent arg0) { |
134 |
this.execute();
|
135 |
} |
136 |
|
137 |
public String getName() { |
138 |
return this.name; |
139 |
} |
140 |
|
141 |
public String getLabel() { |
142 |
return this.text; |
143 |
} |
144 |
|
145 |
public String getCommand() { |
146 |
return this.command; |
147 |
} |
148 |
|
149 |
public String getIconName() { |
150 |
return this.iconName; |
151 |
} |
152 |
|
153 |
public ImageIcon getIcon() { |
154 |
IconTheme iconTheme = PluginServices.getIconTheme(); |
155 |
return iconTheme.get(this.iconName); |
156 |
} |
157 |
|
158 |
public String getAccelerator() { |
159 |
return this.accelerator; |
160 |
} |
161 |
|
162 |
public KeyStroke getKeyStroke() { |
163 |
if( emptyToNull(this.accelerator) == null ) { |
164 |
return null; |
165 |
} |
166 |
return KeyMapping.getKeyStroke(this.accelerator); |
167 |
} |
168 |
|
169 |
public long getPosition() { |
170 |
return this.position; |
171 |
} |
172 |
public String getTooltip() { |
173 |
return this.tip; |
174 |
} |
175 |
|
176 |
public void addPropertyChangeListener(PropertyChangeListener arg0) { |
177 |
// not implemented
|
178 |
} |
179 |
|
180 |
public Object getValue(String name) { |
181 |
if( Action.ACTION_COMMAND_KEY.equalsIgnoreCase(name)) { |
182 |
return this.command; |
183 |
} |
184 |
if( Action.LONG_DESCRIPTION.equalsIgnoreCase(name)) { |
185 |
return this.tip; |
186 |
} |
187 |
if( Action.NAME.equalsIgnoreCase(name)) { |
188 |
return this.name; |
189 |
} |
190 |
if( Action.SMALL_ICON.equalsIgnoreCase(name)) { |
191 |
return this.iconName; |
192 |
//return IconThemeManager.getIconThemeManager().getCurrent().get(this.iconName);
|
193 |
} |
194 |
return null; |
195 |
} |
196 |
|
197 |
public void putValue(String arg0, Object arg1) { |
198 |
|
199 |
} |
200 |
|
201 |
public void removePropertyChangeListener(PropertyChangeListener arg0) { |
202 |
// not implemented
|
203 |
} |
204 |
|
205 |
public void setEnabled(boolean arg0) { |
206 |
// Do nothing
|
207 |
} |
208 |
|
209 |
public String toString() { |
210 |
StringBuffer buffer = new StringBuffer(); |
211 |
buffer.append("ActionInfo {");
|
212 |
buffer.append("name='").append(this.name).append("', "); |
213 |
buffer.append("label='").append(this.text).append("', "); |
214 |
buffer.append("tooltip='").append(this.tip).append("', "); |
215 |
buffer.append("actionCommand='").append(this.command).append("', "); |
216 |
buffer.append("position='").append(this.position).append("', "); |
217 |
buffer.append("icon='").append(this.iconName).append("', "); |
218 |
buffer.append("extension='").append(this.getExtensionName()).append("', "); |
219 |
buffer.append("accelerator='").append(this.accelerator).append("' }"); |
220 |
return buffer.toString();
|
221 |
} |
222 |
} |