gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.swing / org.gvsig.scripting.swing.impl / src / main / java / org / gvsig / scripting / swing / impl / DefaultScriptingUIManager.java @ 1063
History | View | Annotate | Download (14.5 KB)
1 | 165 | jobacas | package org.gvsig.scripting.swing.impl; |
---|---|---|---|
2 | |||
3 | import java.net.URL; |
||
4 | 702 | jjdelcerro | import java.util.ArrayList; |
5 | 630 | jjdelcerro | import java.util.Collection; |
6 | 702 | jjdelcerro | import java.util.Collections; |
7 | import java.util.Comparator; |
||
8 | 180 | jobacas | import java.util.HashMap; |
9 | 687 | jjdelcerro | import java.util.HashSet; |
10 | 980 | jjdelcerro | import java.util.LinkedHashMap; |
11 | 702 | jjdelcerro | import java.util.List; |
12 | 180 | jobacas | import java.util.Locale; |
13 | import java.util.Map; |
||
14 | 687 | jjdelcerro | import java.util.Set; |
15 | 595 | jjdelcerro | import javax.swing.Action; |
16 | 165 | jobacas | import javax.swing.ImageIcon; |
17 | 1063 | jjdelcerro | import javax.swing.JButton; |
18 | import javax.swing.JComboBox; |
||
19 | 639 | jjdelcerro | import javax.swing.JComponent; |
20 | 165 | jobacas | import javax.swing.JPanel; |
21 | 1063 | jjdelcerro | import javax.swing.JTextField; |
22 | 165 | jobacas | import org.gvsig.about.AboutLocator; |
23 | import org.gvsig.about.AboutManager; |
||
24 | import org.gvsig.scripting.ScriptingDialog; |
||
25 | import org.gvsig.scripting.ScriptingFolder; |
||
26 | import org.gvsig.scripting.ScriptingLocator; |
||
27 | import org.gvsig.scripting.ScriptingManager; |
||
28 | import org.gvsig.scripting.ScriptingScript; |
||
29 | 639 | jjdelcerro | import org.gvsig.scripting.ScriptingUnit; |
30 | 691 | jjdelcerro | import org.gvsig.scripting.swing.api.CreateComponentListener; |
31 | 702 | jjdelcerro | import org.gvsig.scripting.swing.api.EditorFactory; |
32 | 165 | jobacas | import org.gvsig.scripting.swing.api.JCodeEditor; |
33 | import org.gvsig.scripting.swing.api.JDialogEditor; |
||
34 | 639 | jjdelcerro | import org.gvsig.scripting.swing.api.JEditor; |
35 | 165 | jobacas | import org.gvsig.scripting.swing.api.JScriptingBrowser; |
36 | import org.gvsig.scripting.swing.api.JScriptingComposer; |
||
37 | 478 | jjdelcerro | import org.gvsig.scripting.swing.api.JScriptingConsole; |
38 | 165 | jobacas | import org.gvsig.scripting.swing.api.JScriptingLauncher; |
39 | import org.gvsig.scripting.swing.api.JThinG; |
||
40 | import org.gvsig.scripting.swing.api.JThinlet; |
||
41 | import org.gvsig.scripting.swing.api.ScriptingUIManager; |
||
42 | 702 | jjdelcerro | import org.gvsig.scripting.swing.api.ScriptingUIManager.MenuEntry; |
43 | 368 | jjdelcerro | import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent; |
44 | 165 | jobacas | import org.gvsig.scripting.swing.impl.composer.DefaultJScriptingComposer; |
45 | 702 | jjdelcerro | import org.gvsig.scripting.swing.impl.composer.editors.DialogEditor; |
46 | import org.gvsig.scripting.swing.impl.composer.editors.DialogEditorFactory; |
||
47 | import org.gvsig.scripting.swing.impl.composer.editors.ExternalFileEditorFactory; |
||
48 | 822 | jjdelcerro | import org.gvsig.scripting.swing.impl.composer.editors.PropertiesEditorFactory; |
49 | 702 | jjdelcerro | import org.gvsig.scripting.swing.impl.composer.editors.ScriptEditor; |
50 | import org.gvsig.scripting.swing.impl.composer.editors.ScriptEditorFactory; |
||
51 | import org.gvsig.scripting.swing.impl.composer.editors.TextEditorFactory; |
||
52 | 165 | jobacas | import org.gvsig.scripting.swing.impl.syntaxhighlight.JEditorPaneSyntaxHighlighting; |
53 | 368 | jjdelcerro | import org.gvsig.scripting.swing.impl.syntaxhighlight.JRSyntaxTextArea; |
54 | 1063 | jjdelcerro | import org.gvsig.scripting.swing.impl.tools.DefaultScriptChooserPanel; |
55 | import org.gvsig.scripting.swing.impl.tools.ScriptListPickerController; |
||
56 | import org.gvsig.scripting.swing.impl.tools.ScriptPickerController; |
||
57 | import org.gvsig.scripting.swing.impl.tools.ScriptSwingManager; |
||
58 | 457 | jbadia | import org.gvsig.tools.ToolsLocator; |
59 | import org.gvsig.tools.i18n.I18nManager; |
||
60 | 1063 | jjdelcerro | import org.gvsig.tools.script.Script; |
61 | 687 | jjdelcerro | import org.gvsig.tools.swing.api.Component; |
62 | 252 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
63 | 1063 | jjdelcerro | import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
64 | 252 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
65 | 468 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
66 | 368 | jjdelcerro | import org.slf4j.Logger; |
67 | import org.slf4j.LoggerFactory; |
||
68 | 165 | jobacas | |
69 | 1063 | jjdelcerro | public class DefaultScriptingUIManager |
70 | implements ScriptingUIManager , ScriptSwingManager
|
||
71 | { |
||
72 | 165 | jobacas | |
73 | 468 | jjdelcerro | private final static Logger logger = LoggerFactory.getLogger(DefaultScriptingUIManager.class); |
74 | 595 | jjdelcerro | |
75 | 639 | jjdelcerro | private final ScriptingManager manager; |
76 | private final Map<String, URL> userHelps = new HashMap<>(); |
||
77 | 468 | jjdelcerro | private I18nManager i18nManager;
|
78 | private WindowManager windowsManager = null; |
||
79 | 980 | jjdelcerro | private final Map<String,Action> composerTools = new LinkedHashMap<>(); |
80 | private final Map<String,MenuEntry> composerMenus = new LinkedHashMap<>(); |
||
81 | 639 | jjdelcerro | private JScriptingComposer activeComposer;
|
82 | 702 | jjdelcerro | private Set<CreateComponentListener> createComponentListeners; |
83 | 739 | jjdelcerro | private final Map<String,EditorFactory> editorFactories = new HashMap<>(); |
84 | private List<EditorFactory> editorFactoriesOrderer = null; |
||
85 | 595 | jjdelcerro | |
86 | private static class DefaultMenuEntry implements MenuEntry { |
||
87 | private final Action action; |
||
88 | private final String text; |
||
89 | 185 | jobacas | |
90 | 595 | jjdelcerro | public DefaultMenuEntry(String text, Action action) { |
91 | this.text = text;
|
||
92 | this.action = action;
|
||
93 | } |
||
94 | |||
95 | @Override
|
||
96 | public String getText() { |
||
97 | return this.text; |
||
98 | } |
||
99 | |||
100 | @Override
|
||
101 | public Action getAction() { |
||
102 | return this.action; |
||
103 | } |
||
104 | |||
105 | } |
||
106 | 468 | jjdelcerro | public DefaultScriptingUIManager() {
|
107 | 165 | jobacas | |
108 | 468 | jjdelcerro | this.manager = ScriptingLocator.getManager();
|
109 | 1063 | jjdelcerro | this.manager.put("ScriptingUIManager", this); // ?????? |
110 | 687 | jjdelcerro | |
111 | 468 | jjdelcerro | this.i18nManager = ToolsLocator.getI18nManager();
|
112 | 1063 | jjdelcerro | this.init();
|
113 | } |
||
114 | |||
115 | private void init() { |
||
116 | 702 | jjdelcerro | this.registerEditor(new DialogEditorFactory()); |
117 | this.registerEditor(new ScriptEditorFactory()); |
||
118 | this.registerEditor(new TextEditorFactory()); |
||
119 | this.registerEditor(new ExternalFileEditorFactory()); |
||
120 | 822 | jjdelcerro | this.registerEditor(new PropertiesEditorFactory()); |
121 | 468 | jjdelcerro | } |
122 | 687 | jjdelcerro | |
123 | @Override
|
||
124 | public void addCreatedComponentListener(CreateComponentListener listener) { |
||
125 | if( this.createComponentListeners==null ) { |
||
126 | this.createComponentListeners = new HashSet<>(); |
||
127 | } |
||
128 | this.createComponentListeners.add(listener);
|
||
129 | } |
||
130 | |||
131 | @Override
|
||
132 | public void removeCreatedComponentListener(CreateComponentListener listener) { |
||
133 | if( this.createComponentListeners==null ) { |
||
134 | return;
|
||
135 | } |
||
136 | this.createComponentListeners.remove(listener);
|
||
137 | } |
||
138 | |||
139 | public void fireCreateComponent(Component component) { |
||
140 | if (this.createComponentListeners == null) { |
||
141 | return;
|
||
142 | } |
||
143 | 691 | jjdelcerro | for (CreateComponentListener listener : this.createComponentListeners) { |
144 | 687 | jjdelcerro | try {
|
145 | listener.componentCreated(component); |
||
146 | } catch (Throwable th) { |
||
147 | 952 | jjdelcerro | String s = "unknow"; |
148 | try {
|
||
149 | s = component.getClass().getName(); |
||
150 | } catch(Throwable th1) { |
||
151 | } |
||
152 | logger.warn("Problems call listener",th);
|
||
153 | 687 | jjdelcerro | } |
154 | } |
||
155 | } |
||
156 | |||
157 | 639 | jjdelcerro | @Override
|
158 | 468 | jjdelcerro | public JScriptingComposer createComposer() {
|
159 | 687 | jjdelcerro | JScriptingComposer composer = new DefaultJScriptingComposer(this); |
160 | fireCreateComponent(composer); |
||
161 | return composer;
|
||
162 | 468 | jjdelcerro | } |
163 | 165 | jobacas | |
164 | 639 | jjdelcerro | @Override
|
165 | 468 | jjdelcerro | public JPanel createInstaller() { |
166 | // TODO Auto-generated method stub
|
||
167 | return null; |
||
168 | } |
||
169 | 165 | jobacas | |
170 | 639 | jjdelcerro | @Override
|
171 | 468 | jjdelcerro | public JScriptingLauncher createLauncher() {
|
172 | 687 | jjdelcerro | JScriptingLauncher launcher = new DefaultJScriptingLauncher(this); |
173 | fireCreateComponent(launcher); |
||
174 | return launcher;
|
||
175 | 468 | jjdelcerro | } |
176 | 165 | jobacas | |
177 | 639 | jjdelcerro | @Override
|
178 | 468 | jjdelcerro | public ImageIcon getIcon(String name) { |
179 | return this.getIcon(name, null); |
||
180 | } |
||
181 | 185 | jobacas | |
182 | 639 | jjdelcerro | @Override
|
183 | 468 | jjdelcerro | public ImageIcon getIcon(String name, String size) { |
184 | if (name == null) { |
||
185 | return new ImageIcon(); |
||
186 | } |
||
187 | String tamanyo = "16x16/"; |
||
188 | if (size != null && size.equals("big")) { |
||
189 | tamanyo = "32x32/";
|
||
190 | } |
||
191 | ClassLoader loader = this.getClass().getClassLoader(); |
||
192 | URL url = loader.getResource("org/gvsig/scripting/images/" + tamanyo + name + ".png"); |
||
193 | if (url == null) { |
||
194 | url = loader.getResource("org/gvsig/scripting/images/" + tamanyo + "image-missing.png"); |
||
195 | } |
||
196 | if (url == null) { |
||
197 | return new ImageIcon(); |
||
198 | } |
||
199 | return new ImageIcon(url); |
||
200 | } |
||
201 | 180 | jobacas | |
202 | 478 | jjdelcerro | @Override
|
203 | 468 | jjdelcerro | public ScriptingManager getManager() {
|
204 | return this.manager; |
||
205 | } |
||
206 | 180 | jobacas | |
207 | 468 | jjdelcerro | public String[] getPrerreferedLanguages() { |
208 | String curr = Locale.getDefault().getLanguage().toLowerCase(); |
||
209 | if (curr.equals("gl")) { |
||
210 | return new String[]{"gl", "es", "en"}; |
||
211 | } |
||
212 | if (curr.equals("ca")) { |
||
213 | return new String[]{"ca", "es", "en"}; |
||
214 | } |
||
215 | if (curr.equals("eu")) { |
||
216 | return new String[]{"eu", "es", "en"}; |
||
217 | } |
||
218 | if (curr.equals("va")) { |
||
219 | return new String[]{"va", "ca", "es", "en"}; |
||
220 | } |
||
221 | return new String[]{curr, "en", "es"}; |
||
222 | } |
||
223 | |||
224 | 639 | jjdelcerro | @Override
|
225 | 468 | jjdelcerro | public AboutManager getAboutManager() {
|
226 | return AboutLocator.getManager();
|
||
227 | } |
||
228 | |||
229 | 639 | jjdelcerro | private void showWindow(JComponent panel, String title, WindowManager.MODE mode) { |
230 | 468 | jjdelcerro | WindowManager winmanager = this.getWindowManager();
|
231 | if( winmanager instanceof WindowManager_v2 ) { |
||
232 | ((WindowManager_v2)winmanager).showWindow(panel, title, mode, getIcon("scripting_python").getImage());
|
||
233 | } else {
|
||
234 | winmanager.showWindow(panel, title, mode); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | 639 | jjdelcerro | @Override
|
239 | public void showDialog(JComponent panel, String title) { |
||
240 | 468 | jjdelcerro | this.showWindow(panel, title, WindowManager.MODE.DIALOG);
|
241 | 252 | jjdelcerro | } |
242 | 165 | jobacas | |
243 | 639 | jjdelcerro | @Override
|
244 | public void showWindow(JComponent panel, String title) { |
||
245 | 468 | jjdelcerro | this.showWindow(panel, title, WindowManager.MODE.WINDOW);
|
246 | 252 | jjdelcerro | } |
247 | 165 | jobacas | |
248 | 639 | jjdelcerro | @Override
|
249 | public void showTool(JComponent panel, String title) { |
||
250 | 468 | jjdelcerro | this.showWindow(panel, title, WindowManager.MODE.TOOL);
|
251 | 252 | jjdelcerro | } |
252 | |||
253 | 639 | jjdelcerro | @Override
|
254 | 649 | jjdelcerro | public JScriptingBrowser createBrowser(ScriptingFolder root, boolean foldersOnly) { |
255 | 687 | jjdelcerro | JScriptingBrowser browser = new DefaultJScriptingBrowser(this, root, foldersOnly); |
256 | fireCreateComponent(browser); |
||
257 | return browser;
|
||
258 | 468 | jjdelcerro | } |
259 | 165 | jobacas | |
260 | 639 | jjdelcerro | @Override
|
261 | 649 | jjdelcerro | public JScriptingBrowser createBrowser(ScriptingFolder root) {
|
262 | 687 | jjdelcerro | return this.createBrowser(root,false); |
263 | 468 | jjdelcerro | } |
264 | 165 | jobacas | |
265 | 639 | jjdelcerro | @Override
|
266 | 649 | jjdelcerro | public JCodeEditor createJCodeEditor(ScriptingScript script) {
|
267 | 702 | jjdelcerro | JCodeEditor editor = new ScriptEditor(this, script); |
268 | 687 | jjdelcerro | fireCreateComponent(editor); |
269 | return editor;
|
||
270 | 468 | jjdelcerro | } |
271 | 165 | jobacas | |
272 | 639 | jjdelcerro | @Override
|
273 | 649 | jjdelcerro | public JDialogEditor createJDialogEditor(ScriptingDialog script) {
|
274 | 702 | jjdelcerro | return new DialogEditor(this, script); |
275 | 468 | jjdelcerro | } |
276 | 165 | jobacas | |
277 | 639 | jjdelcerro | @Override
|
278 | 468 | jjdelcerro | public JThinG createJThinG() {
|
279 | JThinG thing = new DefaultJThinG();
|
||
280 | return thing;
|
||
281 | } |
||
282 | 165 | jobacas | |
283 | 639 | jjdelcerro | @Override
|
284 | 468 | jjdelcerro | public JThinlet createJThinlet() {
|
285 | JThinlet thinlet = new DefaultJThinlet();
|
||
286 | return thinlet;
|
||
287 | } |
||
288 | 165 | jobacas | |
289 | 639 | jjdelcerro | @Override
|
290 | 468 | jjdelcerro | public SyntaxtHighlightTextComponent createSyntaxHighlightingPanel() {
|
291 | try {
|
||
292 | return new JRSyntaxTextArea(); |
||
293 | } catch (Throwable t) { |
||
294 | logger.info("Can't create advanced SyntaxHighlightingPanel.", t);
|
||
295 | return new JEditorPaneSyntaxHighlighting(); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | 639 | jjdelcerro | @Override
|
300 | 468 | jjdelcerro | public String getTranslation(String key) { |
301 | if (this.i18nManager == null) { |
||
302 | this.i18nManager = ToolsLocator.getI18nManager();
|
||
303 | } |
||
304 | return i18nManager.getTranslation(key);
|
||
305 | } |
||
306 | |||
307 | 639 | jjdelcerro | @Override
|
308 | 468 | jjdelcerro | public WindowManager getWindowManager() {
|
309 | if( this.windowsManager==null ) { |
||
310 | this.windowsManager = ToolsSwingLocator.getWindowManager();
|
||
311 | } |
||
312 | return this.windowsManager; |
||
313 | } |
||
314 | |||
315 | 639 | jjdelcerro | @Override
|
316 | 468 | jjdelcerro | public void setWindowManager(WindowManager windowsManager) { |
317 | this.windowsManager = windowsManager;
|
||
318 | } |
||
319 | 478 | jjdelcerro | |
320 | @Override
|
||
321 | public JScriptingConsole createJScriptingConsole() {
|
||
322 | 687 | jjdelcerro | return this.createJScriptingConsole(true); |
323 | 478 | jjdelcerro | } |
324 | 595 | jjdelcerro | |
325 | @Override
|
||
326 | 650 | jjdelcerro | public JScriptingConsole createJScriptingConsole(boolean captureOutput) { |
327 | 687 | jjdelcerro | JScriptingConsole console = new DefaultJScriptingConsole(captureOutput);
|
328 | fireCreateComponent(console); |
||
329 | return console;
|
||
330 | 650 | jjdelcerro | } |
331 | |||
332 | @Override
|
||
333 | 595 | jjdelcerro | public void addComposerMenu(String text, Action action) { |
334 | 630 | jjdelcerro | String name = (String) action.getValue(Action.ACTION_COMMAND_KEY); |
335 | this.composerMenus.put(name,new DefaultMenuEntry(text,action)); |
||
336 | 595 | jjdelcerro | } |
337 | |||
338 | @Override
|
||
339 | public void addComposerTool(Action action) { |
||
340 | 630 | jjdelcerro | String name = (String) action.getValue(Action.ACTION_COMMAND_KEY); |
341 | this.composerTools.put(name,action);
|
||
342 | 595 | jjdelcerro | } |
343 | |||
344 | @Override
|
||
345 | 630 | jjdelcerro | public Collection<MenuEntry> getComposerMenus() { |
346 | return this.composerMenus.values(); |
||
347 | 595 | jjdelcerro | } |
348 | |||
349 | @Override
|
||
350 | 630 | jjdelcerro | public Collection<Action> getComposerTools() { |
351 | return this.composerTools.values(); |
||
352 | 595 | jjdelcerro | } |
353 | |||
354 | 639 | jjdelcerro | @Override
|
355 | public JEditor createEditor(ScriptingUnit unit) {
|
||
356 | 702 | jjdelcerro | JEditor editor = null;
|
357 | 739 | jjdelcerro | for (EditorFactory factory : this.getEditorFactories() ) { |
358 | 763 | jjdelcerro | try {
|
359 | if( factory.canCreate(unit) ) {
|
||
360 | editor = factory.create(unit); |
||
361 | fireCreateComponent(editor); |
||
362 | break;
|
||
363 | } |
||
364 | } catch(Exception ex) { |
||
365 | logger.warn("Can't create an editor with factory '"+factory.getClass().getName()+"' for '"+unit.getClass().getName()+"'.",ex); |
||
366 | 702 | jjdelcerro | } |
367 | 639 | jjdelcerro | } |
368 | 687 | jjdelcerro | return editor;
|
369 | 639 | jjdelcerro | } |
370 | |||
371 | 739 | jjdelcerro | @Override
|
372 | 702 | jjdelcerro | public boolean hasEditor(ScriptingUnit unit) { |
373 | 763 | jjdelcerro | if( unit == null ) { |
374 | return false; |
||
375 | } |
||
376 | 739 | jjdelcerro | for (EditorFactory factory : this.getEditorFactories() ) { |
377 | 763 | jjdelcerro | try {
|
378 | if( factory.canCreate(unit) ) {
|
||
379 | return true; |
||
380 | } |
||
381 | } catch(Exception ex) { |
||
382 | logger.warn("Can't check if factory '"+factory.getClass().getName()+"' can create an editor for '"+unit.getClass().getName()+"'.",ex); |
||
383 | 702 | jjdelcerro | } |
384 | } |
||
385 | return false; |
||
386 | } |
||
387 | |||
388 | 639 | jjdelcerro | public void setActiveComposer(JScriptingComposer composer) { |
389 | this.activeComposer = composer;
|
||
390 | } |
||
391 | 595 | jjdelcerro | |
392 | 687 | jjdelcerro | @Override
|
393 | 639 | jjdelcerro | public JScriptingComposer getActiveComposer() {
|
394 | return this.activeComposer; |
||
395 | } |
||
396 | 702 | jjdelcerro | |
397 | 739 | jjdelcerro | @Override
|
398 | 702 | jjdelcerro | public void registerEditor(EditorFactory factory) { |
399 | 739 | jjdelcerro | this.editorFactories.put(factory.getName(), factory);
|
400 | this.editorFactoriesOrderer = null; |
||
401 | } |
||
402 | |||
403 | @Override
|
||
404 | public List<EditorFactory> getEditorFactories() { |
||
405 | if( this.editorFactoriesOrderer == null ) { |
||
406 | this.editorFactoriesOrderer = new ArrayList<>(this.editorFactories.values()); |
||
407 | Collections.sort(editorFactoriesOrderer, new Comparator<EditorFactory>() { |
||
408 | |||
409 | @Override
|
||
410 | public int compare(EditorFactory o1, EditorFactory o2) { |
||
411 | return o2.getPriority() - o1.getPriority();
|
||
412 | } |
||
413 | }); |
||
414 | 702 | jjdelcerro | } |
415 | 739 | jjdelcerro | return editorFactoriesOrderer;
|
416 | 702 | jjdelcerro | } |
417 | 1063 | jjdelcerro | |
418 | @Override
|
||
419 | public ScriptChooserPanel createScriptChooserPanel() {
|
||
420 | return new DefaultScriptChooserPanel(this); |
||
421 | } |
||
422 | |||
423 | @Override
|
||
424 | public PickerController<Script> createScriptPickerController(JTextField text, JButton chooser) { |
||
425 | ScriptPickerController x = new ScriptPickerController(text, chooser);
|
||
426 | return x;
|
||
427 | } |
||
428 | |||
429 | @Override
|
||
430 | public PickerController<List<Script>> createListScriptPickerController(JComboBox combo, JButton remove, JButton chooser) { |
||
431 | ScriptListPickerController x = new ScriptListPickerController(combo, remove, chooser);
|
||
432 | return x;
|
||
433 | } |
||
434 | 165 | jobacas | } |