svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / mdiFrame / MDIFrame.java @ 46673
History | View | Annotate | Download (72.3 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.ui.mdiFrame; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Component; |
28 |
import java.awt.Dimension; |
29 |
import java.awt.FlowLayout; |
30 |
import java.awt.Insets; |
31 |
import java.awt.event.ActionEvent; |
32 |
import java.awt.event.ActionListener; |
33 |
import java.awt.event.ComponentEvent; |
34 |
import java.awt.event.ComponentListener; |
35 |
import java.awt.event.ContainerEvent; |
36 |
import java.awt.event.ContainerListener; |
37 |
import java.awt.event.MouseAdapter; |
38 |
import java.awt.event.MouseEvent; |
39 |
import java.awt.event.WindowAdapter; |
40 |
import java.awt.event.WindowEvent; |
41 |
import java.io.File; |
42 |
import java.nio.charset.Charset; |
43 |
import java.util.ArrayList; |
44 |
import java.util.Enumeration; |
45 |
import java.util.HashMap; |
46 |
import java.util.Iterator; |
47 |
import java.util.List; |
48 |
import java.util.Locale; |
49 |
import java.util.Map; |
50 |
import java.util.NoSuchElementException; |
51 |
import java.util.StringTokenizer; |
52 |
import java.util.Vector; |
53 |
import javax.swing.AbstractButton; |
54 |
import javax.swing.ButtonGroup; |
55 |
import javax.swing.ImageIcon; |
56 |
import javax.swing.JComponent; |
57 |
import javax.swing.JFileChooser; |
58 |
import javax.swing.JFrame; |
59 |
import javax.swing.JMenu; |
60 |
import javax.swing.JMenuBar; |
61 |
import javax.swing.JOptionPane; |
62 |
import javax.swing.JPanel; |
63 |
import javax.swing.JPopupMenu; |
64 |
import javax.swing.JSeparator; |
65 |
import javax.swing.JToolBar; |
66 |
import javax.swing.KeyStroke; |
67 |
import javax.swing.MenuElement; |
68 |
import javax.swing.SwingUtilities; |
69 |
import javax.swing.Timer; |
70 |
import javax.swing.WindowConstants; |
71 |
import javax.swing.filechooser.FileFilter; |
72 |
import org.apache.commons.lang3.StringUtils; |
73 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
74 |
import org.gvsig.andami.IconThemeHelper; |
75 |
import org.gvsig.andami.Launcher; |
76 |
import org.gvsig.andami.PluginServices; |
77 |
import org.gvsig.andami.PluginsLocator; |
78 |
import org.gvsig.andami.PluginsManager; |
79 |
import org.gvsig.andami.actioninfo.ActionInfo; |
80 |
import org.gvsig.andami.actioninfo.ActionInfoManager; |
81 |
import org.gvsig.andami.actioninfo.ActionInfoStatusCache; |
82 |
import org.gvsig.andami.plugins.ExtensionDecorator; |
83 |
import org.gvsig.andami.plugins.IExtension; |
84 |
import org.gvsig.andami.plugins.PluginClassLoader; |
85 |
import org.gvsig.andami.plugins.config.generate.ActionTool; |
86 |
import org.gvsig.andami.plugins.config.generate.Label; |
87 |
import org.gvsig.andami.plugins.config.generate.Menu; |
88 |
import org.gvsig.andami.plugins.config.generate.PopupMenu; |
89 |
import org.gvsig.andami.plugins.config.generate.SelectableTool; |
90 |
import org.gvsig.andami.plugins.config.generate.SkinExtensionType; |
91 |
import org.gvsig.andami.plugins.config.generate.ToolBar; |
92 |
import org.gvsig.andami.ui.mdiFrame.TranslatableButtonHelper.TranslatableButton; |
93 |
import org.gvsig.andami.ui.mdiManager.MDIManager; |
94 |
import org.gvsig.andami.ui.mdiManager.MDIManagerFactory; |
95 |
import org.gvsig.gui.beans.controls.IControl; |
96 |
import org.gvsig.tools.ToolsLocator; |
97 |
import org.gvsig.tools.i18n.I18nManager; |
98 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
99 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
100 |
import org.slf4j.Logger; |
101 |
import org.slf4j.LoggerFactory; |
102 |
|
103 |
/**
|
104 |
* Main application window.
|
105 |
*
|
106 |
* Use Launcher.getMDIFrame to get the instance of this class.
|
107 |
*
|
108 |
* @version $Revision: 39484 $
|
109 |
*/
|
110 |
@SuppressWarnings("unchecked") |
111 |
public class MDIFrame extends JFrame implements ComponentListener, |
112 |
ContainerListener, ActionListener, MainFrame { |
113 |
|
114 |
private static final long serialVersionUID = -2472484309160847654L; |
115 |
|
116 |
private static Logger logger = LoggerFactory.getLogger(MDIFrame.class); |
117 |
|
118 |
private static MDIFrame instance = null; |
119 |
|
120 |
private MDIManager mdiManager = MDIManagerFactory.createManager();
|
121 |
|
122 |
private boolean refreshingControls = false; |
123 |
|
124 |
/**
|
125 |
* Elementos de la aplicaci�n
|
126 |
*/
|
127 |
private JMenuBar menuBar = new JMenuBar(); |
128 |
|
129 |
/**
|
130 |
* Panel which contains the toolbars
|
131 |
*/
|
132 |
private JPanel toolBars = new JPanel(); |
133 |
|
134 |
/**
|
135 |
* Status bar
|
136 |
*/
|
137 |
private NewStatusBar bEstado = null; |
138 |
|
139 |
/**
|
140 |
* Asocia los nombres con las barras de herramientas
|
141 |
*/
|
142 |
private HashMap toolBarMap = new HashMap(); |
143 |
|
144 |
/**
|
145 |
* Almacena los grupos de selectableTools
|
146 |
*/
|
147 |
private HashMap buttonGroupMap = new HashMap(); |
148 |
/**
|
149 |
* Stores the initially selected tools.
|
150 |
* It contains pairs (String groupName, JToolBarToggleButton button)
|
151 |
*/
|
152 |
private HashMap initialSelectedTools = new HashMap(); |
153 |
|
154 |
/**
|
155 |
* Stores the actionCommand of the selected tool, for each group.
|
156 |
* It contains pairs (String groupName, JToolBarToggleButton button)
|
157 |
*/
|
158 |
private Map selectedTool = null; |
159 |
// this should be the same value defined at plugin-config.xsd
|
160 |
private String defaultGroup = "unico"; |
161 |
|
162 |
/**
|
163 |
* Asocia los nombres con los popupMenus
|
164 |
*/
|
165 |
private HashMap popupMap = new HashMap(); |
166 |
|
167 |
/**
|
168 |
* Asocia controles con la clase de la extension asociada
|
169 |
*/
|
170 |
private List<JComponent> controls = new ArrayList<JComponent>(); |
171 |
|
172 |
/**
|
173 |
* Asocia la informaci�n sobre las etiquetas que van en la status bar con
|
174 |
* cada extension
|
175 |
*/
|
176 |
private HashMap classLabels = new HashMap(); |
177 |
|
178 |
// private HashMap classControls = new HashMap();
|
179 |
/**
|
180 |
* ProgressListeners (ver interfaz com.iver.mdiApp.ui.ProgressListener)
|
181 |
*/
|
182 |
private ArrayList progressListeners = new ArrayList(); |
183 |
|
184 |
/**
|
185 |
* Timer para invocar los enventos de la interfaz anterior
|
186 |
*/
|
187 |
private Timer progressTimer = null; |
188 |
|
189 |
/**
|
190 |
* Tabla hash que asocia las clases con las extensiones
|
191 |
*/
|
192 |
// private Map classesExtensions = new HashMap<Class<? extends IExtension>, ExtensionDecorator>();
|
193 |
/**
|
194 |
* �ltima clase que activ� etiquetas
|
195 |
*/
|
196 |
private Class lastLabelClass; |
197 |
|
198 |
/**
|
199 |
* Instancia que pone los tooltip en la barra de estado
|
200 |
*/
|
201 |
private TooltipListener tooltipListener = new TooltipListener(); |
202 |
|
203 |
private HashMap infoCodedMenus = new HashMap(); |
204 |
|
205 |
private String titlePrefix; |
206 |
|
207 |
private Map<JComponent,IExtension> control2extensions = new HashMap<JComponent, IExtension>(); |
208 |
|
209 |
private MDIFrame() {
|
210 |
|
211 |
} |
212 |
|
213 |
public static boolean isInitialized() { |
214 |
return instance!=null ; |
215 |
} |
216 |
|
217 |
public static MDIFrame getInstance() { |
218 |
if ( instance == null ) { |
219 |
instance = new MDIFrame();
|
220 |
} |
221 |
return instance;
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* Makes some initialization tasks.
|
226 |
*
|
227 |
* @throws RuntimeException
|
228 |
*/
|
229 |
public void init() { |
230 |
JPopupMenu.setDefaultLightWeightPopupEnabled(false); |
231 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
232 |
throw new RuntimeException("Not Event Dispatch Thread"); |
233 |
} |
234 |
|
235 |
// Se añaden los listeners del JFrame
|
236 |
this.addWindowListener(new WindowAdapter() { |
237 |
|
238 |
@Override
|
239 |
public void windowClosing(WindowEvent e) { |
240 |
Launcher.closeApplication(); |
241 |
} |
242 |
}); |
243 |
this.addComponentListener(this); |
244 |
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
245 |
|
246 |
// Se configura la barra de menu
|
247 |
setJMenuBar(menuBar); |
248 |
|
249 |
// Se configura el layout del JFrame principal
|
250 |
this.getContentPane().setLayout(new BorderLayout()); |
251 |
|
252 |
/*
|
253 |
* Se configura y se añade el JPanel de las barras de
|
254 |
* herramientas
|
255 |
*/
|
256 |
FlowLayout layout = new FlowLayout(FlowLayout.LEFT); |
257 |
layout.setHgap(0);
|
258 |
layout.setVgap(0);
|
259 |
toolBars.setLayout(layout); |
260 |
getContentPane().add(toolBars, BorderLayout.PAGE_START);
|
261 |
|
262 |
// Add the status bar of the application
|
263 |
bEstado = new NewStatusBar();
|
264 |
getContentPane().add(bEstado, BorderLayout.SOUTH);
|
265 |
|
266 |
this.toolBars.addContainerListener(this); |
267 |
|
268 |
|
269 |
/*
|
270 |
* Setting default values. Persistence is read
|
271 |
* afterwards
|
272 |
*/
|
273 |
setSize( |
274 |
MainFrame.MAIN_FRAME_SIZE_DEFAULT[0],
|
275 |
MainFrame.MAIN_FRAME_SIZE_DEFAULT[1]);
|
276 |
setLocation( |
277 |
MainFrame.MAIN_FRAME_POS_DEFAULT[0],
|
278 |
MainFrame.MAIN_FRAME_POS_DEFAULT[1]);
|
279 |
setExtendedState( |
280 |
MainFrame.MAIN_FRAME_EXT_STATE_DEFAULT); |
281 |
|
282 |
mdiManager.init(this);
|
283 |
} |
284 |
|
285 |
@Override
|
286 |
public void setTitle(final String title) { |
287 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
288 |
SwingUtilities.invokeLater(new Runnable() { |
289 |
public void run() { |
290 |
setTitle(title); |
291 |
} |
292 |
}); |
293 |
return;
|
294 |
} |
295 |
super.setTitle(titlePrefix + " : " + title); |
296 |
} |
297 |
|
298 |
private SelectableToolBar getToolBar(final String toolBarName, String pluginName) { |
299 |
SelectableToolBar jtb = (SelectableToolBar) toolBarMap.get(toolBarName); |
300 |
if ( jtb == null ) { |
301 |
jtb = new SelectableToolBar(toolBarName);
|
302 |
jtb.setRollover(true);
|
303 |
jtb.setAndamiVisibility(true);
|
304 |
toolBarMap.put(toolBarName, jtb); |
305 |
toolBars.add(jtb); |
306 |
} |
307 |
return jtb;
|
308 |
} |
309 |
|
310 |
public SelectableToolBar addToolBar(final String toolBarName, String description, int position) { |
311 |
SelectableToolBar toolBar = (SelectableToolBar) toolBarMap.get(toolBarName); |
312 |
if( toolBar == null ) { |
313 |
toolBar = new SelectableToolBar(toolBarName, description, position);
|
314 |
toolBar.setRollover(true);
|
315 |
toolBar.setAndamiVisibility(true);
|
316 |
toolBarMap.put(toolBarName, toolBar); |
317 |
toolBars.add(toolBar); |
318 |
|
319 |
} else if( toolBar.getPosition() < 1 ) { |
320 |
toolBar.setPosition(position); |
321 |
|
322 |
} else if( toolBar.getPosition() != position ) { |
323 |
logger.warn("Add duplicate tool-bar ("+toolBarName+") with diferent position."); |
324 |
} |
325 |
|
326 |
return toolBar;
|
327 |
} |
328 |
|
329 |
public void addTool(final PluginClassLoader loader, final SkinExtensionType ext, |
330 |
final ToolBar toolBar, final SelectableTool selectableTool) |
331 |
throws ClassNotFoundException { |
332 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
333 |
try {
|
334 |
SwingUtilities.invokeAndWait(new Runnable() { |
335 |
public void run() { |
336 |
try {
|
337 |
addTool(loader, ext, toolBar, selectableTool); |
338 |
} catch (ClassNotFoundException ex) { |
339 |
logger.warn("Eehh????", ex);
|
340 |
} |
341 |
} |
342 |
}); |
343 |
} catch (Exception ex) { |
344 |
// Do nothing
|
345 |
} |
346 |
return;
|
347 |
} |
348 |
|
349 |
ButtonGroup group = (ButtonGroup) buttonGroupMap.get(selectableTool.getGroup()); |
350 |
if ( group == null ) { |
351 |
group = new ButtonGroup(); |
352 |
buttonGroupMap.put(selectableTool.getGroup(), group); |
353 |
} |
354 |
|
355 |
String name = toolBar.getName();
|
356 |
SelectableToolBar jtb = getToolBar(name, loader.getPluginName()); |
357 |
if( selectableTool.getDropDownGroup()!=null ) { |
358 |
ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); |
359 |
ActionInfo action = actionManager.getAction(selectableTool.getName()); |
360 |
if( action!=null ) { |
361 |
DropDownButton dropDownButton = (DropDownButton) jtb.findComponent(selectableTool.getDropDownGroup()); |
362 |
if (dropDownButton == null) { |
363 |
ToggleButtonModel buttonModel = new ToggleButtonModel();
|
364 |
dropDownButton = new DropDownButton();
|
365 |
dropDownButton.setModel(buttonModel); |
366 |
dropDownButton.setName(selectableTool.getDropDownGroup()); |
367 |
dropDownButton.setMargin(new Insets(2, 2, 2, 2)); |
368 |
jtb.addButton(group, dropDownButton); |
369 |
addControl(dropDownButton); |
370 |
buttonModel.setGroupName(selectableTool.getGroup()); |
371 |
|
372 |
} |
373 |
dropDownButton.add(actionManager.getTranslated(action)); |
374 |
} |
375 |
return;
|
376 |
} |
377 |
|
378 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
379 |
|
380 |
ImageIcon image = PluginServices.getIconTheme().get(selectableTool.getIcon());
|
381 |
if ( image == null ) { |
382 |
logger.warn("Unable to find icon '" + selectableTool.getIcon() + "'."); |
383 |
image = PluginServices.getIconTheme().getDefaultIcon(); |
384 |
} |
385 |
|
386 |
ToggleButtonModel buttonModel = new ToggleButtonModel();
|
387 |
JToolBarToggleButton btn = new JToolBarToggleButton(selectableTool.getText(), image);
|
388 |
btn.setModel(buttonModel); |
389 |
btn.setMargin(new Insets(2, 2, 2, 2)); |
390 |
btn.addMouseListener(tooltipListener); |
391 |
btn.addActionListener(this);
|
392 |
btn.setFocusable(false);
|
393 |
btn.setActionCommand(selectableTool.getActionCommand()); |
394 |
btn.setToolTipText(selectableTool.getTooltip()); |
395 |
btn.setEnabled(false);
|
396 |
btn.setVisible(false);
|
397 |
|
398 |
// ButtonGroup group = (ButtonGroup) buttonGroupMap.get(selectableTool.getGroup());
|
399 |
// if ( group == null ) {
|
400 |
// group = new ButtonGroup();
|
401 |
// buttonGroupMap.put(selectableTool.getGroup(), group);
|
402 |
// }
|
403 |
//
|
404 |
jtb.addButton(group, btn); |
405 |
|
406 |
buttonModel.setGroupName(selectableTool.getGroup()); |
407 |
if ( selectableTool.getIsDefault() ) {
|
408 |
btn.setSelected(true);
|
409 |
initialSelectedTools.put(selectableTool.getGroup(), |
410 |
btn.getActionCommand()); |
411 |
} |
412 |
|
413 |
addControl(btn); |
414 |
|
415 |
if ( selectableTool.getName() != null ) { |
416 |
btn.setName(selectableTool.getName()); |
417 |
} |
418 |
|
419 |
if ( selectableTool.getTooltip() != null ) { |
420 |
btn.setToolTip(i18nManager.getTranslation(selectableTool.getTooltip())); |
421 |
btn.setToolTipKey(selectableTool.getTooltip()); |
422 |
} |
423 |
|
424 |
if ( selectableTool.getEnableText() != null ) { |
425 |
btn.setEnableText(i18nManager.getTranslation(selectableTool.getEnableText())); |
426 |
} |
427 |
|
428 |
if ( selectableTool.getLast() == true ) { |
429 |
jtb.addSeparator(); |
430 |
} |
431 |
} |
432 |
|
433 |
public void addTool(final PluginClassLoader loader, final SkinExtensionType ext, |
434 |
final ToolBar toolBar, final ActionTool actionTool) throws ClassNotFoundException { |
435 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
436 |
try {
|
437 |
SwingUtilities.invokeAndWait(new Runnable() { |
438 |
public void run() { |
439 |
try {
|
440 |
addTool(loader, ext, toolBar, actionTool); |
441 |
} catch (ClassNotFoundException ex) { |
442 |
logger.warn("Eehh????", ex);
|
443 |
} |
444 |
} |
445 |
}); |
446 |
} catch (Exception ex) { |
447 |
// Do nothing
|
448 |
} |
449 |
return;
|
450 |
} |
451 |
|
452 |
String name = toolBar.getName();
|
453 |
SelectableToolBar jtb = getToolBar(name, loader.getPluginName()); |
454 |
if( actionTool.getDropDownGroup()!=null ) { |
455 |
ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); |
456 |
ActionInfo action = actionManager.getAction(actionTool.getName()); |
457 |
if( action!=null ) { |
458 |
DropDownButton dropDownButton = (DropDownButton) jtb.findComponent(actionTool.getDropDownGroup()); |
459 |
if( dropDownButton==null ) { |
460 |
dropDownButton = new DropDownButton();
|
461 |
dropDownButton.setName(actionTool.getDropDownGroup()); |
462 |
jtb.add(dropDownButton); |
463 |
addControl(dropDownButton); |
464 |
} |
465 |
dropDownButton.add(actionManager.getTranslated(action)); |
466 |
} |
467 |
return;
|
468 |
} |
469 |
|
470 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
471 |
|
472 |
ImageIcon image = IconThemeHelper.getImageIcon(actionTool.getIcon());
|
473 |
if ( image == null ) { |
474 |
logger.warn("Unable to find icon '" + actionTool.getIcon() + "'."); |
475 |
image = PluginServices.getIconTheme().getDefaultIcon(); |
476 |
} |
477 |
|
478 |
JToolBarButton btn = new JToolBarButton(actionTool.getText(), image);
|
479 |
btn.setMargin(new Insets(2, 2, 2, 2)); |
480 |
btn.addMouseListener(tooltipListener); |
481 |
btn.addActionListener(this);
|
482 |
btn.setFocusable(false);
|
483 |
btn.setActionCommand(actionTool.getActionCommand()); |
484 |
btn.setEnabled(false);
|
485 |
btn.setVisible(false);
|
486 |
|
487 |
jtb.add(btn); |
488 |
|
489 |
addControl(btn); |
490 |
|
491 |
if ( actionTool.getName() != null ) { |
492 |
btn.setName(actionTool.getName()); |
493 |
} |
494 |
|
495 |
if ( actionTool.getTooltip() != null ) { |
496 |
btn.setToolTip(i18nManager.getTranslation(actionTool.getTooltip())); |
497 |
btn.setToolTipKey(actionTool.getTooltip()); |
498 |
} |
499 |
|
500 |
if ( actionTool.getEnableText() != null ) { |
501 |
btn.setEnableText(i18nManager.getTranslation(actionTool.getEnableText())); |
502 |
} |
503 |
|
504 |
if ( actionTool.getLast() == true ) { |
505 |
jtb.addSeparator(); |
506 |
} |
507 |
} |
508 |
|
509 |
public void addTool(final ActionInfo action, final String toolBarName, final String dropDownName) { |
510 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
511 |
SwingUtilities.invokeLater(new Runnable() { |
512 |
public void run() { |
513 |
addTool(action, toolBarName,dropDownName); |
514 |
} |
515 |
}); |
516 |
return;
|
517 |
} |
518 |
SelectableToolBar jtb = getToolBar(toolBarName, action.getPluginName()); |
519 |
DropDownButton dropDownButton = (DropDownButton) jtb.findComponent(dropDownName); |
520 |
if( dropDownButton == null ) { |
521 |
dropDownButton = new DropDownButton();
|
522 |
jtb.add(dropDownButton); |
523 |
} |
524 |
ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); |
525 |
dropDownButton.add(actionManager.getTranslated(action)); |
526 |
} |
527 |
public void addTool(final ActionInfo action, final String toolBarName) { |
528 |
addTool(action, toolBarName, false);
|
529 |
} |
530 |
|
531 |
public void addTool(final ActionInfo action, final String toolBarName, final boolean useText) { |
532 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
533 |
|
534 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
535 |
SwingUtilities.invokeLater(new Runnable() { |
536 |
public void run() { |
537 |
addTool(action, toolBarName, useText); |
538 |
} |
539 |
}); |
540 |
return;
|
541 |
} |
542 |
JToolBarButton btn = new JToolBarButton(action.getIcon());
|
543 |
btn.setMargin(new Insets(2, 2, 2, 2)); |
544 |
btn.addMouseListener(tooltipListener); |
545 |
btn.addActionListener(this);
|
546 |
btn.setFocusable(false);
|
547 |
btn.setActionCommand(action.getCommand()); |
548 |
btn.setEnabled(false);
|
549 |
btn.setVisible(false);
|
550 |
btn.setName(action.getName()); |
551 |
if( useText ) {
|
552 |
btn.setText(action.getLabel()); |
553 |
} |
554 |
if ( action.getTooltip() != null ) { |
555 |
btn.setToolTip(i18nManager.getTranslation(action.getTooltip())); |
556 |
btn.setToolTipKey(action.getTooltip()); |
557 |
} |
558 |
|
559 |
SelectableToolBar jtb = getToolBar(toolBarName, action.getPluginName()); |
560 |
jtb.add(btn); |
561 |
|
562 |
addControl(btn); |
563 |
|
564 |
} |
565 |
|
566 |
public void addSelectableTool(final ActionInfo action, final String toolBarName, final String groupName, final boolean useText) { |
567 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
568 |
|
569 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
570 |
SwingUtilities.invokeLater(new Runnable() { |
571 |
public void run() { |
572 |
addSelectableTool(action, toolBarName,groupName,useText); |
573 |
} |
574 |
}); |
575 |
return;
|
576 |
} |
577 |
|
578 |
ButtonGroup group = (ButtonGroup) buttonGroupMap.get(groupName); |
579 |
if ( group == null ) { |
580 |
group = new ButtonGroup(); |
581 |
buttonGroupMap.put(groupName, group); |
582 |
} |
583 |
ToggleButtonModel buttonModel = new ToggleButtonModel();
|
584 |
buttonModel.setGroupName(groupName); |
585 |
|
586 |
JToolBarToggleButton btn = new JToolBarToggleButton(action.getIcon());
|
587 |
btn.setModel(buttonModel); |
588 |
btn.setMargin(new Insets(2, 2, 2, 2)); |
589 |
btn.addMouseListener(tooltipListener); |
590 |
btn.addActionListener(this);
|
591 |
btn.setFocusable(false);
|
592 |
btn.setActionCommand(action.getCommand()); |
593 |
btn.setEnabled(false);
|
594 |
btn.setVisible(false);
|
595 |
btn.setName(action.getName()); |
596 |
if( useText ) {
|
597 |
btn.setText(action.getLabel()); |
598 |
} |
599 |
if ( action.getTooltip() != null ) { |
600 |
btn.setToolTip(i18nManager.getTranslation(action.getTooltip())); |
601 |
btn.setToolTipKey(action.getTooltip()); |
602 |
} |
603 |
|
604 |
SelectableToolBar jtb = getToolBar(toolBarName, action.getPluginName()); |
605 |
jtb.addButton(group, btn); |
606 |
|
607 |
addControl(btn); |
608 |
|
609 |
} |
610 |
|
611 |
/**
|
612 |
* Creates the needed menu structure to add the menu to the bar.
|
613 |
* Returns the father which must hold the menu which was
|
614 |
* provided as parameter.
|
615 |
*
|
616 |
* Crea la estructura de menus necesaria para añadir el menu a la barra.
|
617 |
* Devuelve el padre del cual debe colgar el menu que se pasa como
|
618 |
* parametro.
|
619 |
*
|
620 |
* @param menu
|
621 |
* The Menu whose support is going to be added
|
622 |
* @param loader
|
623 |
* The plugin's class loader
|
624 |
*
|
625 |
* @return The proper father for the menu which was provided as parameter
|
626 |
*/
|
627 |
private JMenu createMenuAncestors(Menu menu, PluginClassLoader loader) { |
628 |
return createMenuAncestors(menu.getText());
|
629 |
} |
630 |
|
631 |
private JMenu createMenuAncestors(String text) { |
632 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
633 |
|
634 |
MenuElement menuPadre = null; |
635 |
|
636 |
String[] menues = text.split("/"); |
637 |
List menuList = new ArrayList(); |
638 |
menuList.add(menues[0]);
|
639 |
menuPadre = getMenu(menuList, menuBar); |
640 |
|
641 |
JMenu padre = null; |
642 |
|
643 |
if ( menuPadre == null ) { |
644 |
padre = new JMenuTraslatable(i18nManager.getTranslation(menues[0])); |
645 |
((JMenuTraslatable) padre).setTextKey(menues[0]);
|
646 |
padre.setName(menues[0]);
|
647 |
addControl(padre); |
648 |
menuBar.add(padre); |
649 |
} else if ( menuPadre instanceof JMenu ) { |
650 |
padre = (JMenu) menuPadre;
|
651 |
} else {
|
652 |
logger.warn("Error creating menu. Ancestor does not exist (" + text + ")."); |
653 |
return null; |
654 |
} |
655 |
|
656 |
// Se crea el resto de menus
|
657 |
ArrayList temp = new ArrayList(); |
658 |
|
659 |
for ( int i = 1; i < (menues.length - 1); i++ ) { |
660 |
temp.add(menues[i]); |
661 |
} |
662 |
|
663 |
menuPadre = createMenus(temp, padre); |
664 |
|
665 |
return (JMenu) menuPadre; |
666 |
} |
667 |
|
668 |
/**
|
669 |
* Añade la informacion del menu al framework.
|
670 |
* Debido a que los men�es se
|
671 |
* pueden introducir en un orden determinado por el usuario, pero los
|
672 |
* plugins se instalan en un orden arbitrario, primero se almacena la
|
673 |
* informacion de todos los menus para luego ordenarlos y posteriormente
|
674 |
* añadirlos al interfaz
|
675 |
*
|
676 |
* @param loader
|
677 |
* Posicion del menu. Se ordena por este campo
|
678 |
* @param ext
|
679 |
* Array con los nombres de los padres del menu
|
680 |
* @param menu
|
681 |
* Texto del menu
|
682 |
*
|
683 |
* @throws ClassNotFoundException
|
684 |
* @throws RuntimeException
|
685 |
*/
|
686 |
public void addMenu(final PluginClassLoader loader, final SkinExtensionType ext, |
687 |
final Menu menu) throws ClassNotFoundException { |
688 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
689 |
try {
|
690 |
SwingUtilities.invokeAndWait(new Runnable() { |
691 |
public void run() { |
692 |
try {
|
693 |
addMenu(loader, ext, menu); |
694 |
} catch (ClassNotFoundException ex) { |
695 |
logger.warn("¿¿¿ Eehh????", ex);
|
696 |
} |
697 |
} |
698 |
}); |
699 |
} catch (Exception ex) { |
700 |
// Do nothing
|
701 |
} |
702 |
return;
|
703 |
} |
704 |
JMenu menuPadre = createMenuAncestors(menu, loader);
|
705 |
|
706 |
if ( menu.getIs_separator() ) {
|
707 |
menuPadre.addSeparator(); |
708 |
return;
|
709 |
} |
710 |
|
711 |
JMenuItem nuevoMenu = createJMenuItem(loader, menu);
|
712 |
nuevoMenu.addMouseListener(tooltipListener); |
713 |
menuPadre.add(nuevoMenu); |
714 |
addControl(nuevoMenu); |
715 |
|
716 |
} |
717 |
|
718 |
public void addMenu(final ActionInfo action, final String text) { |
719 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
720 |
SwingUtilities.invokeLater(new Runnable() { |
721 |
public void run() { |
722 |
addMenu(action, text); |
723 |
} |
724 |
}); |
725 |
return;
|
726 |
} |
727 |
JMenu menuPadre = createMenuAncestors(text);
|
728 |
JMenuItem nuevoMenu = createJMenuItem(action, text);
|
729 |
nuevoMenu.addMouseListener(tooltipListener); |
730 |
menuPadre.add(nuevoMenu); |
731 |
menuPadre.invalidate(); |
732 |
Class<? extends IExtension> classExtension = action.getExtension().getClass(); |
733 |
addControl(nuevoMenu); |
734 |
} |
735 |
|
736 |
/**
|
737 |
* Dado lista de nombres de menu, encuentra el menu
|
738 |
*
|
739 |
* @param nombres
|
740 |
* @param padre
|
741 |
* @return
|
742 |
*/
|
743 |
private javax.swing.JMenuItem getMenu(List nombres, MenuElement parent) { |
744 |
if ( parent instanceof javax.swing.JMenu ) { |
745 |
javax.swing.JMenu parentItem = (javax.swing.JMenu) parent; |
746 |
|
747 |
for ( int i = 0; i < parentItem.getMenuComponentCount(); i++ ) { |
748 |
|
749 |
String item_name = parentItem.getMenuComponent(i).getName();
|
750 |
if ( ((item_name != null) && (item_name.compareTo((String) nombres.get(0)) == 0)) |
751 |
|| /*
|
752 |
* We also accept "leaf menus" with no name
|
753 |
* (Project manager, View-1, View-2, etc)
|
754 |
*/ lastMenuItemWithoutName(parentItem.getMenuComponent(i), nombres) ) {
|
755 |
|
756 |
nombres.remove(0);
|
757 |
if ( nombres.isEmpty() ) {
|
758 |
if ( parentItem.getMenuComponent(i) instanceof javax.swing.JMenuItem ) { |
759 |
return (javax.swing.JMenuItem) parentItem
|
760 |
.getMenuComponent(i); |
761 |
} else {
|
762 |
logger.error(PluginServices.getText(this,
|
763 |
"Menu_type_not_supported_")
|
764 |
+ " "
|
765 |
+ parentItem.getMenuComponent(i).getClass() |
766 |
.getName()); |
767 |
return null; |
768 |
} |
769 |
} else {
|
770 |
return getMenu(nombres,
|
771 |
(MenuElement) parentItem.getMenuComponent(i));
|
772 |
} |
773 |
} |
774 |
} |
775 |
} else if ( parent instanceof JMenuBar ) { |
776 |
javax.swing.JMenuBar parentItem = (javax.swing.JMenuBar) parent; |
777 |
|
778 |
for ( int i = 0; i < parentItem.getMenuCount(); i++ ) { |
779 |
if ( (parentItem.getMenu(i).getName() != null // not a |
780 |
// JToolBar.Separator
|
781 |
) |
782 |
&& (parentItem.getMenu(i).getName() |
783 |
.compareTo((String) nombres.get(0)) == 0) ) { |
784 |
nombres.remove(0);
|
785 |
if ( nombres.isEmpty() ) {
|
786 |
if ( parentItem.getMenu(i) instanceof javax.swing.JMenuItem ) { |
787 |
return parentItem.getMenu(i);
|
788 |
} else {
|
789 |
logger.error(PluginServices.getText(this,
|
790 |
"Menu_type_not_supported_")
|
791 |
+ " "
|
792 |
+ parentItem.getMenu(i).getClass() |
793 |
.getName()); |
794 |
return null; |
795 |
} |
796 |
} else {
|
797 |
return getMenu(nombres, parentItem.getMenu(i));
|
798 |
} |
799 |
} |
800 |
} |
801 |
} else {
|
802 |
logger.error(PluginServices.getText(this,
|
803 |
"Menu_type_not_supported_")
|
804 |
+ " "
|
805 |
+ parent.getClass().getName() + " " + parent.toString());
|
806 |
} |
807 |
return null; |
808 |
} |
809 |
|
810 |
/**
|
811 |
* @param menuComponent
|
812 |
* @param nombres
|
813 |
* @return
|
814 |
*/
|
815 |
private boolean lastMenuItemWithoutName(Component mc, List names) { |
816 |
|
817 |
/*
|
818 |
* names must have 1 string
|
819 |
*/
|
820 |
if ( names == null || names.size() != 1 ) { |
821 |
return false; |
822 |
} |
823 |
if ( !(mc instanceof JMenuItem) ) { |
824 |
return false; |
825 |
} |
826 |
JMenuItem jmi = (JMenuItem) mc; |
827 |
if ( jmi.getName() != null ) { |
828 |
/*
|
829 |
* Name must be null, so this is a menu leaf
|
830 |
*/
|
831 |
return false; |
832 |
} |
833 |
if ( jmi.getText() == null ) { |
834 |
return false; |
835 |
} |
836 |
return jmi.getText().compareTo((String) names.get(0)) == 0; |
837 |
} |
838 |
|
839 |
private class JMenuTraslatable extends JMenu implements TranslatableButton { |
840 |
|
841 |
private TranslatableButtonHelper i18nHelper = new TranslatableButtonHelper(); |
842 |
|
843 |
public JMenuTraslatable(String text) { |
844 |
super(text);
|
845 |
} |
846 |
|
847 |
public void setTextKey(String key) { |
848 |
this.i18nHelper.setText(key);
|
849 |
} |
850 |
|
851 |
public void setToolTipKey(String key) { |
852 |
this.i18nHelper.setTooltip(key);
|
853 |
} |
854 |
|
855 |
public void translate() { |
856 |
if ( this.i18nHelper.needTranslation() ) { |
857 |
this.i18nHelper.translate();
|
858 |
this.setText(this.i18nHelper.getText()); |
859 |
} |
860 |
} |
861 |
|
862 |
} |
863 |
|
864 |
/**
|
865 |
* Crea la estructura de menus recursivamente. Por ejemplo, si se le pasa
|
866 |
* en el par�metro nombres el array {"Search", "References", "Workspace"}
|
867 |
* crear� un men� Search, un submen� del anterior que se llamar�
|
868 |
* References y debajo de �ste �ltimo otro menu llamado Workspace
|
869 |
*
|
870 |
* @param nombres
|
871 |
* Array con los nombres de los men�s que se quieren crear
|
872 |
* @param padre
|
873 |
* Menu padre de los men�s creados. Es �til porque es un
|
874 |
* algoritmo recursivo
|
875 |
*
|
876 |
* @return Devuelve el men� creado. Al final de toda la recursividad,
|
877 |
* devolver� el men� de m�s abajo en la jerarqu�a
|
878 |
*
|
879 |
* @throws RuntimeException
|
880 |
*/
|
881 |
private JMenu createMenus(ArrayList nombres, JMenu padre) { |
882 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
883 |
logger.warn("Este metodo requiere que se este ejecutando en el hilo de eventos de AWT.");
|
884 |
throw new RuntimeException("No Event Dispatch Thread"); |
885 |
} |
886 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
887 |
|
888 |
// si no quedan nombres de menu por crear se vuelve: caso base
|
889 |
if ( nombres.isEmpty() ) {
|
890 |
return padre;
|
891 |
} |
892 |
|
893 |
// Se busca el menu por si ya existiera para no crearlo otra vez
|
894 |
JMenu buscado = null; |
895 |
|
896 |
for ( int i = 0; i < padre.getMenuComponentCount(); i++ ) { |
897 |
try {
|
898 |
JMenu hijo = (JMenu) padre.getMenuComponent(i); |
899 |
|
900 |
if ( hijo.getName().compareTo((String) nombres.get(0)) == 0 ) { |
901 |
buscado = hijo; |
902 |
} |
903 |
} catch (ClassCastException e) { |
904 |
/*
|
905 |
* Se ha encontrado un elemento hoja del arbol de men�es
|
906 |
*/
|
907 |
} |
908 |
} |
909 |
|
910 |
if ( buscado != null ) { |
911 |
// Si lo hemos encontrado creamos el resto
|
912 |
nombres.remove(0);
|
913 |
|
914 |
return createMenus(nombres, buscado);
|
915 |
} else {
|
916 |
// Si no lo hemos encontrado se crea el menu, se a�ade al padre
|
917 |
// y se crea el resto
|
918 |
String nombre = (String) nombres.get(0); |
919 |
JMenuTraslatable menuPadre = new JMenuTraslatable(i18nManager.getTranslation(nombre));
|
920 |
menuPadre.setTextKey(nombre); |
921 |
menuPadre.setName(nombre); |
922 |
addControl(menuPadre); |
923 |
padre.add(menuPadre); |
924 |
|
925 |
nombres.remove(0);
|
926 |
|
927 |
return createMenus(nombres, menuPadre);
|
928 |
} |
929 |
} |
930 |
|
931 |
/**
|
932 |
* M�todo invocado en respuesta a ciertos eventos de la interfaz que
|
933 |
* pueden
|
934 |
* ocultar botones de las barras de herramientas y que redimensiona �sta
|
935 |
* de manera conveniente para que no se oculte ninguno
|
936 |
*/
|
937 |
private void ajustarToolBar() { |
938 |
int margen = 8; |
939 |
int numFilas = 1; |
940 |
double acum = margen;
|
941 |
|
942 |
int toolHeight = 0; |
943 |
|
944 |
for ( int i = 0; i < toolBars.getComponentCount(); i++ ) { |
945 |
Component c = toolBars.getComponent(i);
|
946 |
|
947 |
if ( !c.isVisible() ) {
|
948 |
continue;
|
949 |
} |
950 |
|
951 |
double width = c.getPreferredSize().getWidth();
|
952 |
acum = acum + width; |
953 |
|
954 |
if ( acum > this.getWidth() ) { |
955 |
numFilas++; |
956 |
acum = width + margen; |
957 |
} |
958 |
|
959 |
if ( c.getPreferredSize().getHeight() > toolHeight ) {
|
960 |
toolHeight = c.getPreferredSize().height; |
961 |
} |
962 |
} |
963 |
|
964 |
toolBars.setPreferredSize(new Dimension(this.getWidth(), |
965 |
(numFilas * toolHeight))); |
966 |
|
967 |
toolBars.updateUI(); |
968 |
} |
969 |
|
970 |
public void setClassesExtensions(Map<Class<? extends IExtension>, ExtensionDecorator> classesExtensions) { |
971 |
// Ya no es necesario que se mantenga el Map con las extensiones.
|
972 |
|
973 |
// Map<Class<? extends IExtension>, ExtensionDecorator> extensions = new HashMap<Class<? extends IExtension>, ExtensionDecorator>();
|
974 |
// for ( Entry<Class<? extends IExtension>, ExtensionDecorator> entry: classesExtensions.entrySet()) {
|
975 |
// if( ! (entry.getValue().getExtension() instanceof LibraryExtension) ) {
|
976 |
// extensions.put(entry.getKey(), entry.getValue());
|
977 |
// }
|
978 |
// }
|
979 |
// this.classesExtensions = extensions;
|
980 |
} |
981 |
|
982 |
/**
|
983 |
* Metodo de callback invocado cuando se selecciona un menu o un boton
|
984 |
* de
|
985 |
* la barra de herramientas. Se busca la extensi�n asociada y se ejecuta
|
986 |
*
|
987 |
* @param e
|
988 |
* Evento producido
|
989 |
*/
|
990 |
public void actionPerformed(ActionEvent e) { |
991 |
|
992 |
String actionName = "(unknow)"; |
993 |
try {
|
994 |
JComponent control = (JComponent) e.getSource(); |
995 |
actionName = control.getName(); |
996 |
|
997 |
ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); |
998 |
ActionInfo action = actionManager.getAction(control.getName()); |
999 |
Object args = null; |
1000 |
if ( control instanceof IControl ) { |
1001 |
args = ((IControl) control).getValue(); |
1002 |
} |
1003 |
if ( args == null ) { |
1004 |
action.execute(); |
1005 |
} else {
|
1006 |
action.execute(args); |
1007 |
} |
1008 |
String actionCommand = e.getActionCommand();
|
1009 |
try {
|
1010 |
JToolBarToggleButton toggle = (JToolBarToggleButton) control; |
1011 |
ToggleButtonModel model = (ToggleButtonModel) toggle.getModel(); |
1012 |
selectedTool.put(model.getGroupName(), actionCommand); |
1013 |
} catch (ClassCastException ex) { |
1014 |
} catch (NullPointerException ex) { |
1015 |
} |
1016 |
|
1017 |
} catch (Throwable ex) { |
1018 |
logger.error("Can't perform action '" + actionName + "'.", ex); |
1019 |
} |
1020 |
|
1021 |
enableControls(); |
1022 |
showMemory(); |
1023 |
} |
1024 |
|
1025 |
private String getName(String name, PluginClassLoader loader) { |
1026 |
if ( name.indexOf('.') == -1 ) { |
1027 |
return loader.getPluginName() + "." + name; |
1028 |
} else {
|
1029 |
return name;
|
1030 |
} |
1031 |
} |
1032 |
|
1033 |
public void addPopupMenu(PluginClassLoader loader, PopupMenu menu) { |
1034 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
1035 |
throw new RuntimeException("No Event Dispatch Thread"); |
1036 |
} |
1037 |
|
1038 |
String name = getName(menu.getName(), loader);
|
1039 |
|
1040 |
// Se crea el control popupmenu
|
1041 |
JPopUpMenu popupMenu = (JPopUpMenu) popupMap.get(name); |
1042 |
|
1043 |
if ( popupMenu == null ) { |
1044 |
popupMenu = new JPopUpMenu(menu.getName());
|
1045 |
popupMap.put(name, popupMenu); |
1046 |
} |
1047 |
|
1048 |
Menu[] menues = menu.getMenu(); |
1049 |
for ( int i = 0; i < menues.length; i++ ) { |
1050 |
JMenuItem nuevoMenu = createJMenuItem(loader, menues[i]);
|
1051 |
popupMenu.add(nuevoMenu); |
1052 |
} |
1053 |
} |
1054 |
|
1055 |
private JMenuItem createJMenuItem(PluginClassLoader loader, Menu menu) { |
1056 |
JMenuItem nuevoMenu = null; |
1057 |
|
1058 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
1059 |
|
1060 |
String text = menu.getText();
|
1061 |
int n = text.lastIndexOf('/'); |
1062 |
if ( n >= 0 ) { |
1063 |
text = text.substring(n + 1);
|
1064 |
} |
1065 |
String translatedText = i18nManager.getTranslation(text);
|
1066 |
|
1067 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
1068 |
if ( menu.getIcon() != null ) { |
1069 |
if ( iconTheme.exists(menu.getIcon()) ) {
|
1070 |
ImageIcon image = iconTheme.get(menu.getIcon());
|
1071 |
nuevoMenu = new JMenuItem(translatedText, image); |
1072 |
} else {
|
1073 |
nuevoMenu = new JMenuItem(translatedText); |
1074 |
logger.info("menu icon '" + menu.getIcon() + "' not exists."); |
1075 |
} |
1076 |
} else {
|
1077 |
nuevoMenu = new JMenuItem(translatedText); |
1078 |
} |
1079 |
nuevoMenu.setTextKey(text); |
1080 |
nuevoMenu.setName(menu.getName()); |
1081 |
|
1082 |
if ( menu.getKey() != null ) { |
1083 |
nuevoMenu.setAccelerator(KeyMapping.getKeyStroke(menu.getKey())); |
1084 |
} |
1085 |
|
1086 |
nuevoMenu.setActionCommand(menu.getActionCommand()); |
1087 |
|
1088 |
if ( menu.getTooltip() != null ) { |
1089 |
nuevoMenu.setToolTip(i18nManager.getTranslation(menu.getTooltip())); |
1090 |
nuevoMenu.setToolTipKey(menu.getTooltip()); |
1091 |
} |
1092 |
|
1093 |
if ( menu.getEnableText() != null ) { |
1094 |
nuevoMenu.setEnableText(i18nManager.getTranslation(menu.getEnableText())); |
1095 |
} |
1096 |
|
1097 |
nuevoMenu.setEnabled(true);
|
1098 |
nuevoMenu.setVisible(true);
|
1099 |
|
1100 |
if ( menu.getName() != null ) { |
1101 |
ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); |
1102 |
final ActionInfo actionInfo = actionManager.getAction(menu.getName());
|
1103 |
if ( actionInfo != null ) { |
1104 |
nuevoMenu.addActionListener(actionInfo); |
1105 |
} |
1106 |
} |
1107 |
return nuevoMenu;
|
1108 |
} |
1109 |
|
1110 |
private JMenuItem createJMenuItem(ActionInfo action, String text) { |
1111 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
1112 |
String label = null; |
1113 |
|
1114 |
if ( text == null ) { |
1115 |
label = action.getLabel(); |
1116 |
} else if ( text.contains("/") ) { |
1117 |
String[] ss = text.split("/"); |
1118 |
label = ss[ss.length - 1];
|
1119 |
} else {
|
1120 |
label = text; |
1121 |
} |
1122 |
JMenuItem nuevoMenu = new JMenuItem(); |
1123 |
nuevoMenu.setTextKey(label); |
1124 |
nuevoMenu.setText(i18nManager.getTranslation(label)); |
1125 |
nuevoMenu.setName(action.getName()); |
1126 |
if ( action.getIconName() != null ) { |
1127 |
nuevoMenu.setIcon(action.getIcon()); |
1128 |
} |
1129 |
KeyStroke key = action.getKeyStroke();
|
1130 |
if ( key != null ) { |
1131 |
nuevoMenu.setAccelerator(key); |
1132 |
} |
1133 |
nuevoMenu.setActionCommand(action.getCommand()); |
1134 |
if ( action.getTooltip() != null ) { |
1135 |
nuevoMenu.setToolTip(i18nManager.getTranslation(action.getTooltip())); |
1136 |
nuevoMenu.setToolTipKey(action.getTooltip()); |
1137 |
} |
1138 |
nuevoMenu.setEnabled(true);
|
1139 |
nuevoMenu.setVisible(true);
|
1140 |
final ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
|
1141 |
nuevoMenu.addActionListener((ActionEvent e) -> {
|
1142 |
ActionInfo a = actionManager.getAction(action.getName()); |
1143 |
if( a==null ) { |
1144 |
action.actionPerformed(e); |
1145 |
} else {
|
1146 |
a.actionPerformed(e); |
1147 |
} |
1148 |
}); |
1149 |
return nuevoMenu;
|
1150 |
} |
1151 |
|
1152 |
/**
|
1153 |
* Muestra u oculta el menu de nombre 'name'
|
1154 |
*
|
1155 |
* @param name
|
1156 |
* Nombre del menu que se quiere mostrar
|
1157 |
* @param x
|
1158 |
* Evento de raton
|
1159 |
* @param y
|
1160 |
* @param c
|
1161 |
*/
|
1162 |
private void showPopupMenu(String name, int x, int y, Component c) { |
1163 |
JPopupMenu menu = (JPopupMenu) popupMap.get(name); |
1164 |
|
1165 |
if ( menu != null ) { |
1166 |
menu.show(c, x, y); |
1167 |
} |
1168 |
} |
1169 |
|
1170 |
public void removePopupMenuListener(String name, ActionListener listener) { |
1171 |
JPopupMenu menu = (JPopupMenu) popupMap.get(name); |
1172 |
|
1173 |
if ( menu != null ) { |
1174 |
Component[] jmenuitems = menu.getComponents(); |
1175 |
|
1176 |
for ( int i = 0; i < jmenuitems.length; i++ ) { |
1177 |
if ( jmenuitems[i] instanceof JMenuItem ) { |
1178 |
((JMenuItem) jmenuitems[i]).removeActionListener(listener);
|
1179 |
} |
1180 |
} |
1181 |
} |
1182 |
} |
1183 |
|
1184 |
public void addPopupMenuListener(String popupName, Component c, |
1185 |
ActionListener listener, PluginClassLoader loader) {
|
1186 |
final String name = getName(popupName, loader); |
1187 |
|
1188 |
JPopupMenu menu = (JPopupMenu) popupMap.get(name); |
1189 |
|
1190 |
if ( menu != null ) { |
1191 |
Component[] jmenuitems = menu.getComponents(); |
1192 |
|
1193 |
for ( int i = 0; i < jmenuitems.length; i++ ) { |
1194 |
if ( jmenuitems[i] instanceof JMenuItem ) { |
1195 |
((JMenuItem) jmenuitems[i]).addActionListener(listener);
|
1196 |
} |
1197 |
} |
1198 |
} |
1199 |
|
1200 |
c.addMouseListener(new MouseAdapter() { |
1201 |
|
1202 |
@Override
|
1203 |
public void mousePressed(MouseEvent e) { |
1204 |
if ( e.isPopupTrigger() ) {
|
1205 |
showPopupMenu(name, e.getX(), e.getY(), e.getComponent()); |
1206 |
} |
1207 |
} |
1208 |
|
1209 |
@Override
|
1210 |
public void mouseReleased(MouseEvent e) { |
1211 |
if ( e.isPopupTrigger() ) {
|
1212 |
showPopupMenu(name, e.getX(), e.getY(), e.getComponent()); |
1213 |
} |
1214 |
} |
1215 |
}); |
1216 |
} |
1217 |
|
1218 |
/**
|
1219 |
* Loop on the controls to enable/disable and show/hide them, according to
|
1220 |
* its associated action (ActionInfo)
|
1221 |
*
|
1222 |
* @throws RuntimeException
|
1223 |
*/
|
1224 |
public void enableControls() { |
1225 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
1226 |
SwingUtilities.invokeLater(new Runnable() { |
1227 |
public void run() { |
1228 |
enableControls(); |
1229 |
} |
1230 |
}); |
1231 |
return;
|
1232 |
} |
1233 |
|
1234 |
ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); |
1235 |
ActionInfoStatusCache cache = actionManager.createActionStatusCache(); |
1236 |
|
1237 |
// Enable or disable controls, according to its associated extensions
|
1238 |
Iterator<JComponent> e = controlsIterator(); |
1239 |
|
1240 |
while ( e.hasNext() ) {
|
1241 |
JComponent control = (JComponent) e.next(); |
1242 |
String actionName = control.getName();
|
1243 |
if( StringUtils.isEmpty(actionName) ) {
|
1244 |
continue;
|
1245 |
} |
1246 |
ActionInfo action = actionManager.getAction(actionName); |
1247 |
try {
|
1248 |
boolean enabled = false; |
1249 |
boolean visible = false; |
1250 |
|
1251 |
if ( action == null ) { |
1252 |
if( control instanceof DropDownButton ) { |
1253 |
DropDownButton dropDownButton = (DropDownButton)control; |
1254 |
visible = !dropDownButton.isAllHiden(); |
1255 |
enabled = !dropDownButton.isAllDisabled(); |
1256 |
if (visible && enabled){
|
1257 |
dropDownButton.updateMainButton(); |
1258 |
} |
1259 |
} else if( control instanceof JMenuTraslatable ) { |
1260 |
enabled = true;
|
1261 |
visible = true;
|
1262 |
} else {
|
1263 |
IExtension extension = this.control2extensions.get(control);
|
1264 |
if( extension!=null ) { |
1265 |
enabled = extension.isEnabled(); |
1266 |
visible = extension.isVisible(); |
1267 |
} else {
|
1268 |
logger.warn("Can't enable/show control '"+control.getClass().getName()+"/"+control.getName()+"."); |
1269 |
enabled = true;
|
1270 |
visible = true;
|
1271 |
} |
1272 |
} |
1273 |
} else {
|
1274 |
enabled = false;
|
1275 |
visible = cache.isVisible(action); |
1276 |
if ( visible ) {
|
1277 |
enabled = cache.isEnabled(action); |
1278 |
} |
1279 |
} |
1280 |
//logger.info(control.getClass().getName() +" - " +control.getName() + " visible " + visible );
|
1281 |
control.setEnabled(enabled); |
1282 |
control.setVisible(visible); |
1283 |
} catch (Throwable ex) { |
1284 |
// Catch exceptions and errors (class not found)
|
1285 |
logger.info("Can't enable/show control '" + actionName + "'.", ex); |
1286 |
this.message("Can't enable/show control '" + actionName + "'.", JOptionPane.ERROR_MESSAGE); |
1287 |
try {
|
1288 |
control.setEnabled(false);
|
1289 |
control.setVisible(false);
|
1290 |
} catch (Exception ex2) { |
1291 |
// Ignore errors
|
1292 |
} |
1293 |
} |
1294 |
} |
1295 |
|
1296 |
// Loop in the menus to hide the menus that don't have visible children
|
1297 |
for ( int i = 0; i < menuBar.getMenuCount(); i++ ) { |
1298 |
MenuElement menu = menuBar.getMenu(i);
|
1299 |
hideMenus(menu); |
1300 |
if ( menu instanceof JMenu ) { |
1301 |
// hide (ugly) redundant separators and assign keyboard
|
1302 |
// mnemonics
|
1303 |
Component[] comps = ((JMenu) menu).getMenuComponents(); |
1304 |
// mnemonics have to be unique for each top-level menu
|
1305 |
char mnemonics[] = new char[comps.length]; |
1306 |
if ( comps.length > 0 ) { |
1307 |
// Set keyboard mnemonic for this top-level entry
|
1308 |
String text = ((JMenu) menu).getText(); |
1309 |
char mnemonic = getMnemonic(text, mnemonics);
|
1310 |
if ( ' ' != mnemonic ) { |
1311 |
((JMenu) menu).setMnemonic(mnemonic);
|
1312 |
mnemonics[0] = mnemonic;
|
1313 |
} |
1314 |
} |
1315 |
// now go through all entries in this menu, hid
|
1316 |
// separators if necessary and assing remaining mnemonics
|
1317 |
hideSeparatorsAndMakeMnemonics(menu, mnemonics); |
1318 |
} |
1319 |
} |
1320 |
|
1321 |
// hide the toolbars that don't contain any visible tool
|
1322 |
Iterator it = toolBarMap.values().iterator();
|
1323 |
|
1324 |
while ( it.hasNext() ) {
|
1325 |
JComponent t = (JComponent) it.next(); |
1326 |
boolean todosOcultos = true; |
1327 |
|
1328 |
for ( int i = 0; i < t.getComponentCount(); i++ ) { |
1329 |
if ( !(t.getComponent(i) instanceof JSeparator) // separators |
1330 |
// don't matter
|
1331 |
&& t.getComponent(i).isVisible() ) { |
1332 |
todosOcultos = false;
|
1333 |
} |
1334 |
} |
1335 |
|
1336 |
if ( todosOcultos ) {
|
1337 |
t.setVisible(false);
|
1338 |
} else {
|
1339 |
if ( t instanceof SelectableToolBar ) { |
1340 |
t.setVisible(((SelectableToolBar) t).getAndamiVisibility()); |
1341 |
} else {
|
1342 |
t.setVisible(true);
|
1343 |
} |
1344 |
} |
1345 |
} |
1346 |
|
1347 |
if ( mdiManager != null ) { |
1348 |
JPanel f = (JPanel) mdiManager.getActiveWindow(); |
1349 |
|
1350 |
if ( f != null ) { |
1351 |
if ( lastLabelClass != f.getClass() ) {
|
1352 |
lastLabelClass = f.getClass(); |
1353 |
|
1354 |
Label[] lbls = (Label[]) classLabels.get(lastLabelClass); |
1355 |
|
1356 |
if ( lbls != null ) { |
1357 |
bEstado.setLabelSet(lbls); |
1358 |
} |
1359 |
} |
1360 |
} |
1361 |
} |
1362 |
|
1363 |
ajustarToolBar(); |
1364 |
|
1365 |
showMemory(); |
1366 |
} |
1367 |
|
1368 |
public synchronized void refreshControls() { |
1369 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
1370 |
SwingUtilities.invokeLater(new Runnable() { |
1371 |
public void run() { |
1372 |
refreshControls(); |
1373 |
} |
1374 |
}); |
1375 |
return;
|
1376 |
} |
1377 |
|
1378 |
if(refreshingControls){
|
1379 |
return;
|
1380 |
} |
1381 |
try {
|
1382 |
refreshingControls = true;
|
1383 |
ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); |
1384 |
ActionInfoStatusCache cache = actionManager.createActionStatusCache(); |
1385 |
IconTheme icontheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
1386 |
|
1387 |
Iterator e = controlsIterator();
|
1388 |
|
1389 |
while ( e.hasNext() ) {
|
1390 |
JComponent control = (JComponent) e.next(); |
1391 |
if ( control instanceof TranslatableButton ) { |
1392 |
((TranslatableButton) control).translate(); |
1393 |
} |
1394 |
String actionlName = control.getName();
|
1395 |
ActionInfo action = actionManager.getAction(actionlName); |
1396 |
if ( action != null ) { |
1397 |
if ( control instanceof AbstractButton ) { |
1398 |
AbstractButton button = (AbstractButton) control; |
1399 |
if ( control instanceof javax.swing.JMenuItem ) { |
1400 |
if ( action.getIconName() != null && action.getIconName().length() > 0 ) { |
1401 |
if ( icontheme.exists(action.getIconName()) ) {
|
1402 |
button.setIcon(action.getIcon()); |
1403 |
} |
1404 |
} |
1405 |
} else {
|
1406 |
button.setIcon(action.getIcon()); |
1407 |
} |
1408 |
} |
1409 |
} |
1410 |
} |
1411 |
|
1412 |
enableControls(); |
1413 |
} finally {
|
1414 |
refreshingControls = false;
|
1415 |
} |
1416 |
} |
1417 |
|
1418 |
@Override
|
1419 |
public void message(String msg, int messageTyoe) { |
1420 |
this.getStatusBar().message(msg, messageTyoe);
|
1421 |
} |
1422 |
|
1423 |
/**
|
1424 |
* Establece la visibilidad de un menu y todos sus descendientes en la
|
1425 |
* jerarquia teniendo en cuenta la visibilidad de todos los submenus.
|
1426 |
*
|
1427 |
* @param menu
|
1428 |
* Menu que se quiere visualizar
|
1429 |
*
|
1430 |
* @return Devuelve true si el menu es visible y false en caso contrario
|
1431 |
*/
|
1432 |
private boolean hideMenus(MenuElement menu) { |
1433 |
MenuElement[] submenus = menu.getSubElements(); |
1434 |
|
1435 |
// Si no tiene hijos se devuelve su visibilidad
|
1436 |
if ( submenus.length == 0 ) { |
1437 |
return menu.getComponent().isVisible();
|
1438 |
} |
1439 |
|
1440 |
/*
|
1441 |
* Si tiene hijos se devuelve true si alg�no de ellos es visible,
|
1442 |
* pero se itera por todos ellos
|
1443 |
*/
|
1444 |
boolean visible = false; |
1445 |
|
1446 |
for ( int i = 0; i < submenus.length; i++ ) { |
1447 |
if ( hideMenus(submenus[i]) ) {
|
1448 |
if ( !(menu instanceof JPopupMenu) ) { |
1449 |
menu.getComponent().setVisible(true);
|
1450 |
} |
1451 |
|
1452 |
visible = true;
|
1453 |
} |
1454 |
} |
1455 |
|
1456 |
if ( visible ) {
|
1457 |
return true; |
1458 |
} |
1459 |
|
1460 |
menu.getComponent().setVisible(false);
|
1461 |
|
1462 |
return false; |
1463 |
} |
1464 |
|
1465 |
/**
|
1466 |
*
|
1467 |
* Recurse through all menu elements and make sure there are no
|
1468 |
* redundant separators.
|
1469 |
* This method will make sure that a separator only becomes visible
|
1470 |
* if at least one visible non-separator menu entry preceeded it.
|
1471 |
*
|
1472 |
*
|
1473 |
*/
|
1474 |
private void hideSeparatorsAndMakeMnemonics(MenuElement menu, |
1475 |
char[] mnemonics) { |
1476 |
// flag that indicates whether a separator is to be displayed or not
|
1477 |
boolean allowSeparator;
|
1478 |
|
1479 |
allowSeparator = false; // separator not allowed as very first menu item |
1480 |
Component[] comps = ((JMenu) menu).getMenuComponents(); |
1481 |
if ( comps.length < 1 ) { |
1482 |
// zero-length menu: skip
|
1483 |
return;
|
1484 |
} |
1485 |
|
1486 |
for ( int i = 0; i < comps.length; i++ ) { |
1487 |
if ( comps[i] instanceof JSeparator ) { |
1488 |
// got a separator: display only if allowed at this position
|
1489 |
if ( allowSeparator == true ) { |
1490 |
// look at all successive menu entries to make sure that at
|
1491 |
// least one
|
1492 |
// is visible and not a separator (otherwise, this separator
|
1493 |
// would
|
1494 |
// be the last visible item in this menu) -- we don't want
|
1495 |
// that
|
1496 |
comps[i].setVisible(false);
|
1497 |
for ( int j = i; j < comps.length; j++ ) { |
1498 |
if ( !(comps[j] instanceof JSeparator) ) { |
1499 |
if ( comps[j].isVisible() ) {
|
1500 |
comps[i].setVisible(true); // display separator! |
1501 |
break;
|
1502 |
} |
1503 |
} |
1504 |
} |
1505 |
} else {
|
1506 |
comps[i].setVisible(false);
|
1507 |
} |
1508 |
allowSeparator = false; // separator is not allowed right after |
1509 |
// another separator
|
1510 |
} else {
|
1511 |
if ( comps[i] instanceof JMenu ) { // got a submenu: recurse |
1512 |
// through it
|
1513 |
// get number of submenu components
|
1514 |
Component[] scomps = ((JMenu) comps[i]).getMenuComponents(); |
1515 |
// make a new, fresh array to hold unique mnemonics for this
|
1516 |
// submenu
|
1517 |
char[] smnemonics = new char[scomps.length]; |
1518 |
hideSeparatorsAndMakeMnemonics(((MenuElement) comps[i]),
|
1519 |
smnemonics); |
1520 |
if ( comps[i].isVisible() ) {
|
1521 |
allowSeparator = true; // separators are OK after |
1522 |
// visible submenus
|
1523 |
// Set keyboard mnemonic for this submenu
|
1524 |
String text = ((JMenu) comps[i]).getText(); |
1525 |
char mnemonic = getMnemonic(text, mnemonics);
|
1526 |
if ( ' ' != mnemonic ) { |
1527 |
((JMenu) comps[i]).setMnemonic(mnemonic);
|
1528 |
mnemonics[i] = mnemonic; |
1529 |
} |
1530 |
} |
1531 |
} else {
|
1532 |
if ( comps[i].isVisible() ) {
|
1533 |
if ( comps[i] instanceof JMenuItem ) { |
1534 |
// Set keyboard mnemonic for this menu item
|
1535 |
String text = ((JMenuItem) comps[i]).getText(); |
1536 |
char mnemonic = getMnemonic(text, mnemonics);
|
1537 |
if ( ' ' != mnemonic ) { |
1538 |
((JMenuItem) comps[i]).setMnemonic(mnemonic);
|
1539 |
mnemonics[i] = mnemonic; |
1540 |
} |
1541 |
} |
1542 |
allowSeparator = true; // separators are OK after |
1543 |
// regular, visible entries
|
1544 |
} |
1545 |
} |
1546 |
} |
1547 |
} |
1548 |
} |
1549 |
|
1550 |
/**
|
1551 |
* Helper functios for assigning a unique mnemomic char from
|
1552 |
* a pool of unassigned onces, stored in the array "mnemomnics"
|
1553 |
*/
|
1554 |
private char getMnemonic(String text, char[] mnemonics) { |
1555 |
if( text==null ) { |
1556 |
return ' '; |
1557 |
} |
1558 |
Vector words = new Vector(); |
1559 |
StringTokenizer t = new StringTokenizer(text); |
1560 |
int maxsize = 0; |
1561 |
|
1562 |
while ( t.hasMoreTokens() ) {
|
1563 |
String word = t.nextToken();
|
1564 |
if ( word.length() > maxsize ) {
|
1565 |
maxsize = word.length(); |
1566 |
} |
1567 |
words.addElement(word); |
1568 |
} |
1569 |
words.trimToSize(); |
1570 |
|
1571 |
for ( int i = 0; i < maxsize; ++i ) { |
1572 |
char mnemonic = getMnemonic(words, mnemonics, i);
|
1573 |
if ( ' ' != mnemonic ) { |
1574 |
return mnemonic;
|
1575 |
} |
1576 |
} |
1577 |
|
1578 |
return ' '; |
1579 |
} |
1580 |
|
1581 |
private char getMnemonic(Vector words, char[] mnemonics, int index) { |
1582 |
int numwords = words.size();
|
1583 |
|
1584 |
for ( int i = 0; i < numwords; ++i ) { |
1585 |
String word = (String) words.elementAt(i); |
1586 |
if ( index >= word.length() ) {
|
1587 |
continue;
|
1588 |
} |
1589 |
|
1590 |
char c = word.charAt(index);
|
1591 |
if ( !isMnemonicExists(c, mnemonics) ) {
|
1592 |
/* pick only valid chars */
|
1593 |
if ( (c != ':') && (c != '.') && (c != ',') && (c != ';') |
1594 |
&& (c != '-') && (c != '+') && (c != '/') && (c != '\\') |
1595 |
&& (c != '\'') && (c != '\"') && (c != ' ') && (c != '=') |
1596 |
&& (c != '(') && (c != ')') && (c != '[') && (c != ']') |
1597 |
&& (c != '{') && (c != '}') && (c != '$') && (c != '*') |
1598 |
&& (c != '&') && (c != '%') && (c != '!') && (c != '?') |
1599 |
&& (c != '#') && (c != '~') && (c != '_') ) { |
1600 |
return c;
|
1601 |
} |
1602 |
} |
1603 |
} |
1604 |
return ' '; |
1605 |
} |
1606 |
|
1607 |
private boolean isMnemonicExists(char c, char[] mnemonics) { |
1608 |
int num = mnemonics.length;
|
1609 |
for ( int i = 0; i < num; ++i ) { |
1610 |
if ( mnemonics[i] == c ) {
|
1611 |
return true; |
1612 |
} |
1613 |
} |
1614 |
return false; |
1615 |
} |
1616 |
|
1617 |
/**
|
1618 |
* Muestra la memoria consumida por el programa
|
1619 |
*/
|
1620 |
private void showMemory() { |
1621 |
Runtime r = Runtime.getRuntime(); |
1622 |
long mem = r.totalMemory() - r.freeMemory();
|
1623 |
logger.debug(PluginServices.getText(this, "memory_usage") + " " + mem |
1624 |
/ 1024 + " KB"); |
1625 |
} |
1626 |
|
1627 |
public MDIManager getMDIManager() {
|
1628 |
return mdiManager;
|
1629 |
} |
1630 |
|
1631 |
public NewStatusBar getStatusBar() {
|
1632 |
return bEstado;
|
1633 |
} |
1634 |
|
1635 |
/**
|
1636 |
* You can use this function to select the appropiate
|
1637 |
* tool inside the toolbars
|
1638 |
*/
|
1639 |
public void setSelectedTool(String actionCommand) { |
1640 |
setSelectedTool(defaultGroup, actionCommand); |
1641 |
} |
1642 |
|
1643 |
/**
|
1644 |
* You can use this function to select the appropiate
|
1645 |
* tool inside the toolbars
|
1646 |
*/
|
1647 |
public void setSelectedTool(String groupName, String actionCommand) { |
1648 |
ButtonGroup group = (ButtonGroup) buttonGroupMap.get(groupName); |
1649 |
if ( group == null ) { |
1650 |
return;
|
1651 |
} |
1652 |
|
1653 |
Enumeration enumeration = group.getElements();
|
1654 |
while ( enumeration.hasMoreElements() ) {
|
1655 |
AbstractButton button = (AbstractButton) enumeration.nextElement(); |
1656 |
if(button instanceof DropDownButton){ |
1657 |
((DropDownButton)button).setSelected(actionCommand, true);
|
1658 |
} else {
|
1659 |
if ( button.getActionCommand().equals(actionCommand) ) {
|
1660 |
button.setSelected(true);
|
1661 |
} |
1662 |
} |
1663 |
} |
1664 |
|
1665 |
selectedTool.put(groupName, actionCommand); |
1666 |
} |
1667 |
|
1668 |
/**
|
1669 |
* You can use this function to select the appropiate
|
1670 |
* tool inside the toolbars
|
1671 |
*/
|
1672 |
public void setSelectedTools(Map selectedTools) { |
1673 |
selectedTool = selectedTools; |
1674 |
if ( selectedTools == null ) { |
1675 |
return;
|
1676 |
} |
1677 |
Iterator groupNames = selectedTools.keySet().iterator();
|
1678 |
while ( groupNames.hasNext() ) {
|
1679 |
try {
|
1680 |
String groupName = (String) groupNames.next(); |
1681 |
ButtonGroup group = (ButtonGroup) buttonGroupMap.get(groupName); |
1682 |
Enumeration enumeration = group.getElements();
|
1683 |
String actionCommand = (String) selectedTools.get(groupName); |
1684 |
if ( actionCommand == null ) { |
1685 |
continue;
|
1686 |
} |
1687 |
while ( enumeration.hasMoreElements() ) {
|
1688 |
AbstractButton button
|
1689 |
= (AbstractButton) enumeration.nextElement();
|
1690 |
if ( button.getActionCommand().equals(actionCommand) ) {
|
1691 |
button.setSelected(true);
|
1692 |
} |
1693 |
} |
1694 |
} catch (ClassCastException ex) { |
1695 |
logger |
1696 |
.error("selectedTool should only contain pairs (String groupName, JToolBarToggleButton button)");
|
1697 |
} |
1698 |
} |
1699 |
} |
1700 |
|
1701 |
/**
|
1702 |
* DOCUMENT ME!
|
1703 |
*
|
1704 |
* @param clase
|
1705 |
* @param label
|
1706 |
*/
|
1707 |
public void setStatusBarLabels(Class<?> clase, Label[] label) { |
1708 |
classLabels.put(clase, label); |
1709 |
} |
1710 |
|
1711 |
public void removeStatusBarLabels(Class<?> clase) { |
1712 |
classLabels.remove(clase); |
1713 |
} |
1714 |
|
1715 |
public void addStatusBarControl(Class<?> extensionClass, IControl control) { |
1716 |
control.addActionListener(this);
|
1717 |
bEstado.addControl(control.getName(), (Component) control);
|
1718 |
if ( control instanceof JComponent ) { |
1719 |
addControl((JComponent) control);
|
1720 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
1721 |
IExtension extension = pluginsManager.getExtension((Class<? extends IExtension>) extensionClass); |
1722 |
this.control2extensions.put((JComponent)control, extension); |
1723 |
} |
1724 |
} |
1725 |
|
1726 |
public void addToolBarControl(Class<?> extensionClass, JToolBar control, String name) { |
1727 |
toolBars.add(control); |
1728 |
addControl(control); |
1729 |
|
1730 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
1731 |
IExtension extension = pluginsManager.getExtension((Class<? extends IExtension>) extensionClass); |
1732 |
this.control2extensions.put(control, extension);
|
1733 |
} |
1734 |
|
1735 |
public void removeStatusBarControl(String name) { |
1736 |
Component c = bEstado.removeControl(name);
|
1737 |
if ( c instanceof JComponent ) { |
1738 |
removeControl((JComponent) c);
|
1739 |
} |
1740 |
} |
1741 |
|
1742 |
public void removeMenu(Menu menu) { |
1743 |
JMenuItem delete = (JMenuItem) infoCodedMenus.get(menu); |
1744 |
|
1745 |
if ( delete == null ) { |
1746 |
throw new NoSuchElementException(menu.getText()); |
1747 |
} |
1748 |
|
1749 |
delete.getParent().remove(delete); |
1750 |
infoCodedMenus.remove(menu); |
1751 |
} |
1752 |
|
1753 |
public void addMenu(Menu menu, ActionListener listener, |
1754 |
PluginClassLoader loader) { |
1755 |
JMenu menuPadre = createMenuAncestors(menu, loader);
|
1756 |
|
1757 |
// Se registra y añaade el menu
|
1758 |
JMenuItem nuevoMenu = createJMenuItem(loader, menu);
|
1759 |
nuevoMenu.addMouseListener(tooltipListener); |
1760 |
if ( listener != null && menu.getName() != null && menu.getName().trim().length() > 0 ) { |
1761 |
ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); |
1762 |
final ActionInfo actionInfo = actionManager.getAction(menu.getName());
|
1763 |
if ( actionInfo != null ) { |
1764 |
nuevoMenu.removeActionListener(actionInfo); |
1765 |
} |
1766 |
nuevoMenu.addActionListener(listener); |
1767 |
} else {
|
1768 |
/*
|
1769 |
* We also add action listener for menu with
|
1770 |
* no name but with text:
|
1771 |
*
|
1772 |
* Window/Project manager
|
1773 |
* Window/View - 1
|
1774 |
* Window/View - 2
|
1775 |
* etc
|
1776 |
*/
|
1777 |
if ( listener != null && menu.getText() != null && menu.getText().trim().length() > 0 ) { |
1778 |
nuevoMenu.addActionListener(listener); |
1779 |
} |
1780 |
|
1781 |
} |
1782 |
menuPadre.add(nuevoMenu); |
1783 |
|
1784 |
infoCodedMenus.put(menu, nuevoMenu); |
1785 |
} |
1786 |
|
1787 |
public void changeMenuName(String[] menu, String newName, |
1788 |
PluginClassLoader loader) { |
1789 |
|
1790 |
ArrayList menuList = new ArrayList(); |
1791 |
for ( int i = 0; i < menu.length; i++ ) { |
1792 |
menuList.add(menu[i]); |
1793 |
} |
1794 |
|
1795 |
javax.swing.JMenuItem newMenu = getMenu(menuList, menuBar); |
1796 |
if ( newMenu == null ) { |
1797 |
throw new NoSuchMenuException(menu[0]); |
1798 |
} else {
|
1799 |
newMenu.setText(PluginServices.getText(this, newName));
|
1800 |
} |
1801 |
} |
1802 |
|
1803 |
public void componentHidden(ComponentEvent arg0) { |
1804 |
} |
1805 |
|
1806 |
public void componentMoved(ComponentEvent arg0) { |
1807 |
} |
1808 |
|
1809 |
public void componentResized(ComponentEvent arg0) { |
1810 |
ajustarToolBar(); |
1811 |
} |
1812 |
|
1813 |
public void componentShown(ComponentEvent arg0) { |
1814 |
} |
1815 |
|
1816 |
public void componentAdded(ContainerEvent arg0) { |
1817 |
ajustarToolBar(); |
1818 |
} |
1819 |
|
1820 |
public void componentRemoved(ContainerEvent arg0) { |
1821 |
ajustarToolBar(); |
1822 |
} |
1823 |
|
1824 |
public class TooltipListener extends MouseAdapter { |
1825 |
|
1826 |
@Override
|
1827 |
public void mouseEntered(MouseEvent e) { |
1828 |
JComponent control = (JComponent) e.getSource(); |
1829 |
EnableTextSupport ets = (EnableTextSupport) e.getSource(); |
1830 |
|
1831 |
String texto = null; |
1832 |
texto = control.getToolTipText(); |
1833 |
|
1834 |
if ( texto != null ) { |
1835 |
bEstado.setInfoTextTemporal(texto); |
1836 |
} |
1837 |
} |
1838 |
|
1839 |
@Override
|
1840 |
public void mouseExited(MouseEvent arg0) { |
1841 |
bEstado.restaurarTexto(); |
1842 |
} |
1843 |
|
1844 |
@Override
|
1845 |
public void mousePressed(MouseEvent e) { |
1846 |
bEstado.restaurarTexto(); |
1847 |
} |
1848 |
} |
1849 |
|
1850 |
public String getTitlePrefix() { |
1851 |
return titlePrefix;
|
1852 |
} |
1853 |
|
1854 |
public void setTitlePrefix(String titlePrefix) { |
1855 |
this.titlePrefix = titlePrefix;
|
1856 |
} |
1857 |
|
1858 |
public Map getSelectedTools() { |
1859 |
return selectedTool;
|
1860 |
} |
1861 |
|
1862 |
public HashMap getInitialSelectedTools() { |
1863 |
return initialSelectedTools;
|
1864 |
} |
1865 |
|
1866 |
/**
|
1867 |
* Get a previously added JComponent by name.
|
1868 |
* For example you can use it if you need to obtain a JToolBar to
|
1869 |
* add some customized component.
|
1870 |
*
|
1871 |
* @param name
|
1872 |
* @return the JComponent or null if none has been found
|
1873 |
*/
|
1874 |
public JComponent getComponentByName(String name) { |
1875 |
Iterator e = controlsIterator();
|
1876 |
|
1877 |
while ( e.hasNext() ) {
|
1878 |
JComponent control = (JComponent) e.next(); |
1879 |
String nameCtrl = control.getName();
|
1880 |
if ( nameCtrl != null ) { |
1881 |
if ( nameCtrl.compareTo(name) == 0 ) { |
1882 |
return control;
|
1883 |
} |
1884 |
} |
1885 |
} |
1886 |
Iterator it = toolBarMap.values().iterator();
|
1887 |
while ( it.hasNext() ) {
|
1888 |
JComponent t = (JComponent) it.next(); |
1889 |
String nameCtrl = t.getName();
|
1890 |
if ( nameCtrl != null ) { |
1891 |
if ( nameCtrl.compareTo(name) == 0 ) { |
1892 |
return t;
|
1893 |
} |
1894 |
} |
1895 |
|
1896 |
} |
1897 |
|
1898 |
return null; |
1899 |
} |
1900 |
|
1901 |
public SelectableToolBar[] getToolbars() { |
1902 |
return (SelectableToolBar[]) toolBarMap.values().toArray( |
1903 |
new SelectableToolBar[0]); |
1904 |
} |
1905 |
|
1906 |
public boolean getToolbarVisibility(String name) { |
1907 |
JComponent component
|
1908 |
= PluginServices.getMainFrame().getComponentByName(name); |
1909 |
if ( (component != null) && (component instanceof SelectableToolBar) ) { |
1910 |
SelectableToolBar toolBar = (SelectableToolBar) component; |
1911 |
return toolBar.getAndamiVisibility();
|
1912 |
} |
1913 |
return false; |
1914 |
} |
1915 |
|
1916 |
public boolean setToolbarVisibility(String name, boolean visibility) { |
1917 |
JComponent component
|
1918 |
= PluginServices.getMainFrame().getComponentByName(name); |
1919 |
if ( (component != null) && (component instanceof SelectableToolBar) ) { |
1920 |
SelectableToolBar toolBar = (SelectableToolBar) component; |
1921 |
boolean oldVisibility = toolBar.getAndamiVisibility();
|
1922 |
toolBar.setAndamiVisibility(visibility); |
1923 |
enableControls(); |
1924 |
return oldVisibility;
|
1925 |
} |
1926 |
return false; |
1927 |
} |
1928 |
|
1929 |
public javax.swing.JMenuItem getMenuEntry(String[] menuPath) { |
1930 |
ArrayList menu = new ArrayList(); |
1931 |
for ( int i = 0; i < menuPath.length; i++ ) { |
1932 |
menu.add(menuPath[i]); |
1933 |
} |
1934 |
return getMenu(menu, menuBar);
|
1935 |
} |
1936 |
|
1937 |
public void messageDialog(String message, String title, int messageType) { |
1938 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
1939 |
helper.messageDialog(message, title, messageType); |
1940 |
} |
1941 |
|
1942 |
public void messageDialog(String message, String[] messageArgs, |
1943 |
String title, int messageType) { |
1944 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
1945 |
helper.messageDialog(message, messageArgs, title, messageType); |
1946 |
} |
1947 |
|
1948 |
@Override
|
1949 |
public void messageDialog(String message, String[] messageArgs, |
1950 |
String title, int messageType, String msgid) { |
1951 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
1952 |
helper.messageDialog(message, messageArgs, title, messageType, msgid); |
1953 |
} |
1954 |
|
1955 |
@Override
|
1956 |
public int confirmDialog(String message, String title, int optionType, |
1957 |
int messageType) {
|
1958 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
1959 |
return helper.confirmDialog(message, title, optionType, messageType);
|
1960 |
} |
1961 |
|
1962 |
|
1963 |
@Override
|
1964 |
public int confirmDialog(String message, String title, int optionType, |
1965 |
int messageType, String msgid) { |
1966 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
1967 |
return helper.confirmDialog(message, title, optionType, messageType, msgid);
|
1968 |
} |
1969 |
|
1970 |
@Override
|
1971 |
public String inputDialog(String message, String title, int messageType, |
1972 |
String initialValue) {
|
1973 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
1974 |
return helper.inputDialog(message, title, messageType, initialValue);
|
1975 |
} |
1976 |
|
1977 |
@Override
|
1978 |
public String inputDialog(String message, String title) { |
1979 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
1980 |
return helper.inputDialog(message, title);
|
1981 |
} |
1982 |
|
1983 |
@Override
|
1984 |
public void showDialog(Component contents, String title) { |
1985 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
1986 |
helper.showDialog(contents, title); |
1987 |
} |
1988 |
|
1989 |
@Override
|
1990 |
public Component createComponent(Class<? extends Component> theClass, |
1991 |
Object... parameters) {
|
1992 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
1993 |
return helper.createComponentWithParams(theClass, parameters);
|
1994 |
} |
1995 |
|
1996 |
@Override
|
1997 |
public Component createComponentWithParams( |
1998 |
Class<? extends Component> theClass, Object[] parameters) { |
1999 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
2000 |
return helper.createComponentWithParams(theClass, parameters);
|
2001 |
} |
2002 |
|
2003 |
@Override
|
2004 |
public File[] showChooserDialog( |
2005 |
final String title, |
2006 |
final int type, // SAVE_DIALOG / OPEN_DIALOG |
2007 |
final int selectionMode, // JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES |
2008 |
final boolean multiselection, |
2009 |
final File initialPath, |
2010 |
final FileFilter filter, |
2011 |
final boolean fileHidingEnabled |
2012 |
) { |
2013 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
2014 |
return helper.showChooserDialog(title, type, selectionMode, multiselection, initialPath, filter, fileHidingEnabled);
|
2015 |
} |
2016 |
|
2017 |
@Override
|
2018 |
public ImmutablePair<File[], Charset> showChooserDialog( |
2019 |
final String title, |
2020 |
final int type, // SAVE_DIALOG / OPEN_DIALOG |
2021 |
final int selectionMode, // JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES |
2022 |
final boolean multiselection, |
2023 |
final File initialPath, |
2024 |
final FileFilter filter, |
2025 |
final boolean fileHidingEnabled, |
2026 |
final boolean askCharset |
2027 |
) { |
2028 |
DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado); |
2029 |
return helper.showChooserDialog(title, type, selectionMode, multiselection, initialPath, filter, fileHidingEnabled, askCharset);
|
2030 |
} |
2031 |
|
2032 |
@Override
|
2033 |
public File[] showOpenDirectoryDialog(String title, File initialPath) { |
2034 |
return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.DIRECTORIES_ONLY, false, initialPath, null, false); |
2035 |
} |
2036 |
|
2037 |
@Override
|
2038 |
public File[] showOpenFileDialog(String title, File initialPath) { |
2039 |
return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, null, false); |
2040 |
} |
2041 |
|
2042 |
@Override
|
2043 |
public File[] showSaveFileDialog(String title, File initialPath) { |
2044 |
return showChooserDialog(title, JFileChooser.SAVE_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, null, false); |
2045 |
} |
2046 |
|
2047 |
private void addControl(JComponent control) { |
2048 |
controls.add(control); |
2049 |
} |
2050 |
|
2051 |
private void removeControl(JComponent control) { |
2052 |
controls.remove(control); |
2053 |
} |
2054 |
|
2055 |
private Iterator<JComponent> controlsIterator() { |
2056 |
return this.controls.iterator(); |
2057 |
} |
2058 |
|
2059 |
@Override
|
2060 |
public void setLocale(Locale locale) { |
2061 |
super.setLocale(locale); //To change body of generated methods, choose Tools | Templates. |
2062 |
MDIManager mdiManager = MDIManagerFactory.createManager(); |
2063 |
mdiManager.setLocale(locale); |
2064 |
if ( this.controls != null && !this.controls.isEmpty() ) { |
2065 |
this.refreshControls();
|
2066 |
} |
2067 |
} |
2068 |
|
2069 |
} |