Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / mdiFrame / MDIFrame.java @ 43099

History | View | Annotate | Download (69.8 KB)

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