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
/**
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.util.ArrayList;
43
import java.util.Enumeration;
44
import java.util.HashMap;
45
import java.util.Iterator;
46
import java.util.List;
47
import java.util.Locale;
48
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
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

    
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
import org.gvsig.andami.PluginsManager;
78
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
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 static final String noIcon = "no-icon";
208

    
209
    private Map<JComponent,IExtension> control2extensions = new HashMap<JComponent, IExtension>();
210

    
211
    private MDIFrame() {
212

    
213
    }
214

    
215
    public static boolean isInitialized() {
216
        return instance!=null ;
217
    }
218

    
219
    public static MDIFrame getInstance() {
220
        if ( instance == null ) {
221
            instance = new MDIFrame();
222
        }
223
        return instance;
224
    }
225

    
226
    /**
227
     * Makes some initialization tasks.
228
     *
229
     * @throws RuntimeException
230
     */
231
    public void init() {
232
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
233
        if ( !SwingUtilities.isEventDispatchThread() ) {
234
            throw new RuntimeException("Not Event Dispatch Thread");
235
        }
236

    
237
        // Se añaden los listeners del JFrame
238
        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
         * Se configura y se añade el JPanel de las barras de
256
         * 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
        // Add the status bar of the application
265
        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
                MainFrame.MAIN_FRAME_SIZE_DEFAULT[0],
278
                MainFrame.MAIN_FRAME_SIZE_DEFAULT[1]);
279
        setLocation(
280
                MainFrame.MAIN_FRAME_POS_DEFAULT[0],
281
                MainFrame.MAIN_FRAME_POS_DEFAULT[1]);
282
        setExtendedState(
283
                MainFrame.MAIN_FRAME_EXT_STATE_DEFAULT);
284

    
285
        mdiManager.init(this);
286
    }
287

    
288
    @Override
289
    public void setTitle(final String title) {
290
        if ( !SwingUtilities.isEventDispatchThread() ) {
291
            SwingUtilities.invokeLater(new Runnable() {
292
                public void run() {
293
                    setTitle(title);
294
                }
295
            });
296
            return;
297
        }
298
        super.setTitle(titlePrefix + " : " + title);
299
    }
300

    
301
    private SelectableToolBar getToolBar(final String toolBarName, String pluginName) {
302
        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
    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
    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
                            logger.warn("¿¿¿ Eehh????", ex);
343
                        }
344
                    }
345
                });
346
            } catch (Exception ex) {
347
                // Do nothing
348
            }
349
            return;
350
        }
351
        String name = toolBar.getName();
352
        SelectableToolBar jtb = getToolBar(name, loader.getPluginName());
353
        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
                dropDownButton.add(actionManager.getTranslated(action));
365
            }
366
            return;
367
        }
368

    
369
        I18nManager i18nManager = ToolsLocator.getI18nManager();
370

    
371
        ImageIcon image = PluginServices.getIconTheme().get(selectableTool.getIcon());
372
        if ( image != null ) {
373
            logger.warn("Unable to find icon '" + selectableTool.getIcon() + "'.");
374
            image = PluginServices.getIconTheme().get(noIcon);
375
        }
376

    
377
        ToggleButtonModel buttonModel = new ToggleButtonModel();
378
        JToolBarToggleButton btn = new JToolBarToggleButton(selectableTool.getText(), image);
379
        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
        ButtonGroup group = (ButtonGroup) buttonGroupMap.get(selectableTool.getGroup());
390
        if ( group == null ) {
391
            group = new ButtonGroup();
392
            buttonGroupMap.put(selectableTool.getGroup(), group);
393
        }
394

    
395
        jtb.addButton(group, btn);
396

    
397
        buttonModel.setGroupName(selectableTool.getGroup());
398
        if ( selectableTool.getIsDefault() ) {
399
            btn.setSelected(true);
400
            initialSelectedTools.put(selectableTool.getGroup(),
401
                    btn.getActionCommand());
402
        }
403

    
404
        addControl(btn);
405

    
406
        if ( selectableTool.getName() != null ) {
407
            btn.setName(selectableTool.getName());
408
        }
409

    
410
        if ( selectableTool.getTooltip() != null ) {
411
            btn.setToolTip(i18nManager.getTranslation(selectableTool.getTooltip()));
412
            btn.setToolTipKey(selectableTool.getTooltip());
413
        }
414

    
415
        if ( selectableTool.getEnableText() != null ) {
416
            btn.setEnableText(i18nManager.getTranslation(selectableTool.getEnableText()));
417
        }
418

    
419
        if ( selectableTool.getLast() == true ) {
420
            jtb.addSeparator();
421
        }
422
    }
423

    
424
    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
                            logger.warn("¿¿¿ Eehh????", ex);
434
                        }
435
                    }
436
                });
437
            } catch (Exception ex) {
438
                // Do nothing
439
            }
440
            return;
441
        }
442

    
443
        String name = toolBar.getName();
444
        SelectableToolBar jtb = getToolBar(name, loader.getPluginName());
445
        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
                dropDownButton.add(actionManager.getTranslated(action));
457
            }
458
            return;
459
        }
460

    
461
        I18nManager i18nManager = ToolsLocator.getI18nManager();
462

    
463
        ImageIcon image = IconThemeHelper.getImageIcon(actionTool.getIcon());
464
        if ( image != null ) {
465
            logger.warn("Unable to find icon '" + actionTool.getIcon() + "'.");
466
            image =  PluginServices.getIconTheme().get(noIcon);
467
        }
468

    
469
        JToolBarButton btn = new JToolBarButton(actionTool.getText(), image);
470
        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
        addControl(btn);
481

    
482
        if ( actionTool.getName() != null ) {
483
            btn.setName(actionTool.getName());
484
        }
485

    
486
        if ( actionTool.getTooltip() != null ) {
487
            btn.setToolTip(i18nManager.getTranslation(actionTool.getTooltip()));
488
            btn.setToolTipKey(actionTool.getTooltip());
489
        }
490

    
491
        if ( actionTool.getEnableText() != null ) {
492
            btn.setEnableText(i18nManager.getTranslation(actionTool.getEnableText()));
493
        }
494

    
495
        if ( actionTool.getLast() == true ) {
496
            jtb.addSeparator();
497
        }
498
    }
499

    
500
    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
        SelectableToolBar jtb = getToolBar(toolBarName, action.getPluginName());
510
        DropDownButton dropDownButton = (DropDownButton) jtb.findComponent(dropDownName);
511
        if( dropDownButton == null ) {
512
            dropDownButton = new DropDownButton();
513
            jtb.add(dropDownButton);
514
        }
515
        ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
516
        dropDownButton.add(actionManager.getTranslated(action));
517
    }
518
    public void addTool(final ActionInfo action, final String toolBarName) {
519
        addTool(action, toolBarName, false);
520
    }
521

    
522
    public void addTool(final ActionInfo action, final String toolBarName, final boolean useText) {
523
        I18nManager i18nManager = ToolsLocator.getI18nManager();
524

    
525
        if ( !SwingUtilities.isEventDispatchThread() ) {
526
            SwingUtilities.invokeLater(new Runnable() {
527
                public void run() {
528
                    addTool(action, toolBarName, useText);
529
                }
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
        if( useText ) {
543
            btn.setText(action.getLabel());
544
        }
545
        if ( action.getTooltip() != null ) {
546
            btn.setToolTip(i18nManager.getTranslation(action.getTooltip()));
547
            btn.setToolTipKey(action.getTooltip());
548
        }
549

    
550
        SelectableToolBar jtb = getToolBar(toolBarName, action.getPluginName());
551
        jtb.add(btn);
552

    
553
        addControl(btn);
554

    
555
    }
556

    
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

    
595
        SelectableToolBar jtb = getToolBar(toolBarName, action.getPluginName());
596
        jtb.addButton(group, btn);
597

    
598
        addControl(btn);
599

    
600
    }
601

    
602
    /**
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
     *
607
     * Crea la estructura de menus necesaria para añadir el menu a la barra.
608
     * Devuelve el padre del cual debe colgar el menu que se pasa como
609
     * parametro.
610
     *
611
     * @param menu
612
     * The Menu whose support is going to be added
613
     * @param loader
614
     * The plugin's class loader
615
     *
616
     * @return The proper father for the menu which was provided as parameter
617
     */
618
    private JMenu createMenuAncestors(Menu menu, PluginClassLoader loader) {
619
        return createMenuAncestors(menu.getText());
620
    }
621

    
622
    private JMenu createMenuAncestors(String text) {
623
        I18nManager i18nManager = ToolsLocator.getI18nManager();
624

    
625
        MenuElement menuPadre = null;
626

    
627
        String[] menues = text.split("/");
628
        List menuList = new ArrayList();
629
        menuList.add(menues[0]);
630
        menuPadre = getMenu(menuList, menuBar);
631

    
632
        JMenu padre = null;
633

    
634
        if ( menuPadre == null ) {
635
            padre = new JMenuTraslatable(i18nManager.getTranslation(menues[0]));
636
            ((JMenuTraslatable) padre).setTextKey(menues[0]);
637
            padre.setName(menues[0]);
638
            addControl(padre);
639
            menuBar.add(padre);
640
        } 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

    
647
        // Se crea el resto de menus
648
        ArrayList temp = new ArrayList();
649

    
650
        for ( int i = 1; i < (menues.length - 1); i++ ) {
651
            temp.add(menues[i]);
652
        }
653

    
654
        menuPadre = createMenus(temp, padre);
655

    
656
        return (JMenu) menuPadre;
657
    }
658

    
659
    /**
660
     * Añade la informacion del menu al framework.
661
     * Debido a que los men�es se
662
     * pueden introducir en un orden determinado por el usuario, pero los
663
     * plugins se instalan en un orden arbitrario, primero se almacena la
664
     * informacion de todos los menus para luego ordenarlos y posteriormente
665
     * añadirlos al interfaz
666
     *
667
     * @param loader
668
     * Posicion del menu. Se ordena por este campo
669
     * @param ext
670
     * Array con los nombres de los padres del menu
671
     * @param menu
672
     * Texto del menu
673
     *
674
     * @throws ClassNotFoundException
675
     * @throws RuntimeException
676
     */
677
    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
                            logger.warn("¿¿¿ Eehh????", ex);
687
                        }
688
                    }
689
                });
690
            } catch (Exception ex) {
691
                // Do nothing
692
            }
693
            return;
694
        }
695
        JMenu menuPadre = createMenuAncestors(menu, loader);
696

    
697
        if ( menu.getIs_separator() ) {
698
            menuPadre.addSeparator();
699
            return;
700
        }
701

    
702
        JMenuItem nuevoMenu = createJMenuItem(loader, menu);
703
        nuevoMenu.addMouseListener(tooltipListener);
704
        menuPadre.add(nuevoMenu);
705
        addControl(nuevoMenu);
706

    
707
    }
708

    
709
    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
        }
718
        JMenu menuPadre = createMenuAncestors(text);
719
        JMenuItem nuevoMenu = createJMenuItem(action, text);
720
        nuevoMenu.addMouseListener(tooltipListener);
721
        menuPadre.add(nuevoMenu);
722
        menuPadre.invalidate();
723
        Class<? extends IExtension> classExtension = action.getExtension().getClass();
724
        addControl(nuevoMenu);
725
    }
726

    
727
    /**
728
     * Dado lista de nombres de menu, encuentra el menu
729
     *
730
     * @param nombres
731
     * @param padre
732
     * @return
733
     */
734
    private javax.swing.JMenuItem getMenu(List nombres, MenuElement parent) {
735
        if ( parent instanceof javax.swing.JMenu ) {
736
            javax.swing.JMenu parentItem = (javax.swing.JMenu) parent;
737

    
738
            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
                    nombres.remove(0);
748
                    if ( nombres.isEmpty() ) {
749
                        if ( parentItem.getMenuComponent(i) instanceof javax.swing.JMenuItem ) {
750
                            return (javax.swing.JMenuItem) parentItem
751
                                    .getMenuComponent(i);
752
                        } else {
753
                            logger.error(PluginServices.getText(this,
754
                                    "Menu_type_not_supported_")
755
                                    + " "
756
                                    + parentItem.getMenuComponent(i).getClass()
757
                                    .getName());
758
                            return null;
759
                        }
760
                    } else {
761
                        return getMenu(nombres,
762
                                (MenuElement) parentItem.getMenuComponent(i));
763
                    }
764
                }
765
            }
766
        } else if ( parent instanceof JMenuBar ) {
767
            javax.swing.JMenuBar parentItem = (javax.swing.JMenuBar) parent;
768

    
769
            for ( int i = 0; i < parentItem.getMenuCount(); i++ ) {
770
                if ( (parentItem.getMenu(i).getName() != null // not a
771
                        // JToolBar.Separator
772
                        )
773
                        && (parentItem.getMenu(i).getName()
774
                        .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
                                    "Menu_type_not_supported_")
782
                                    + " "
783
                                    + parentItem.getMenu(i).getClass()
784
                                    .getName());
785
                            return null;
786
                        }
787
                    } else {
788
                        return getMenu(nombres, parentItem.getMenu(i));
789
                    }
790
                }
791
            }
792
        } else {
793
            logger.error(PluginServices.getText(this,
794
                    "Menu_type_not_supported_")
795
                    + " "
796
                    + parent.getClass().getName() + " " + parent.toString());
797
        }
798
        return null;
799
    }
800

    
801
    /**
802
     * @param menuComponent
803
     * @param nombres
804
     * @return
805
     */
806
    private boolean lastMenuItemWithoutName(Component mc, List names) {
807

    
808
        /*
809
         * names must have 1 string
810
         */
811
        if ( names == null || names.size() != 1 ) {
812
            return false;
813
        }
814
        if ( !(mc instanceof JMenuItem) ) {
815
            return false;
816
        }
817
        JMenuItem jmi = (JMenuItem) mc;
818
        if ( jmi.getName() != null ) {
819
            /*
820
             * Name must be null, so this is a menu leaf
821
             */
822
            return false;
823
        }
824
        if ( jmi.getText() == null ) {
825
            return false;
826
        }
827
        return jmi.getText().compareTo((String) names.get(0)) == 0;
828
    }
829

    
830
    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
    /**
856
     * Crea la estructura de menus recursivamente. Por ejemplo, si se le pasa
857
     * 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
     *
861
     * @param nombres
862
     * Array con los nombres de los men�s que se quieren crear
863
     * @param padre
864
     * Menu padre de los men�s creados. Es �til porque es un
865
     * algoritmo recursivo
866
     *
867
     * @return Devuelve el men� creado. Al final de toda la recursividad,
868
     * devolver� el men� de m�s abajo en la jerarqu�a
869
     *
870
     * @throws RuntimeException
871
     */
872
    private JMenu createMenus(ArrayList nombres, JMenu padre) {
873
        if ( !SwingUtilities.isEventDispatchThread() ) {
874
            logger.warn("Este metodo requiere que se este ejecutando en el hilo de eventos de AWT.");
875
            throw new RuntimeException("No Event Dispatch Thread");
876
        }
877
        I18nManager i18nManager = ToolsLocator.getI18nManager();
878

    
879
        // si no quedan nombres de menu por crear se vuelve: caso base
880
        if ( nombres.isEmpty() ) {
881
            return padre;
882
        }
883

    
884
        // Se busca el menu por si ya existiera para no crearlo otra vez
885
        JMenu buscado = null;
886

    
887
        for ( int i = 0; i < padre.getMenuComponentCount(); i++ ) {
888
            try {
889
                JMenu hijo = (JMenu) padre.getMenuComponent(i);
890

    
891
                if ( hijo.getName().compareTo((String) nombres.get(0)) == 0 ) {
892
                    buscado = hijo;
893
                }
894
            } catch (ClassCastException e) {
895
                /*
896
                 * Se ha encontrado un elemento hoja del arbol de men�es
897
                 */
898
            }
899
        }
900

    
901
        if ( buscado != null ) {
902
            // Si lo hemos encontrado creamos el resto
903
            nombres.remove(0);
904

    
905
            return createMenus(nombres, buscado);
906
        } else {
907
            // Si no lo hemos encontrado se crea el menu, se a�ade al padre
908
            // y se crea el resto
909
            String nombre = (String) nombres.get(0);
910
            JMenuTraslatable menuPadre = new JMenuTraslatable(i18nManager.getTranslation(nombre));
911
            menuPadre.setTextKey(nombre);
912
            menuPadre.setName(nombre);
913
            addControl(menuPadre);
914
            padre.add(menuPadre);
915

    
916
            nombres.remove(0);
917

    
918
            return createMenus(nombres, menuPadre);
919
        }
920
    }
921

    
922
    /**
923
     * M�todo invocado en respuesta a ciertos eventos de la interfaz que
924
     * pueden
925
     * ocultar botones de las barras de herramientas y que redimensiona �sta
926
     * 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
        for ( int i = 0; i < toolBars.getComponentCount(); i++ ) {
936
            Component c = toolBars.getComponent(i);
937

    
938
            if ( !c.isVisible() ) {
939
                continue;
940
            }
941

    
942
            double width = c.getPreferredSize().getWidth();
943
            acum = acum + width;
944

    
945
            if ( acum > this.getWidth() ) {
946
                numFilas++;
947
                acum = width + margen;
948
            }
949

    
950
            if ( c.getPreferredSize().getHeight() > toolHeight ) {
951
                toolHeight = c.getPreferredSize().height;
952
            }
953
        }
954

    
955
        toolBars.setPreferredSize(new Dimension(this.getWidth(),
956
                (numFilas * toolHeight)));
957

    
958
        toolBars.updateUI();
959
    }
960

    
961
    public void setClassesExtensions(Map<Class<? extends IExtension>, ExtensionDecorator> classesExtensions) {
962
        // 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
    }
972

    
973
    /**
974
     * Metodo de callback invocado cuando se selecciona un menu o un boton
975
     * de
976
     * la barra de herramientas. Se busca la extensi�n asociada y se ejecuta
977
     *
978
     * @param e
979
     * Evento producido
980
     */
981
    public void actionPerformed(ActionEvent e) {
982

    
983
        String actionName = "(unknow)";
984
        try {
985
            JComponent control = (JComponent) e.getSource();
986
            actionName = control.getName();
987

    
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
            }
994
            if ( args == null ) {
995
                action.execute();
996
            } else {
997
                action.execute(args);
998
            }
999
            String actionCommand = e.getActionCommand();
1000
            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
            logger.error("Can't perform action '" + actionName + "'.", ex);
1010
        }
1011

    
1012
        enableControls();
1013
        showMemory();
1014
    }
1015

    
1016
    private String getName(String name, PluginClassLoader loader) {
1017
        if ( name.indexOf('.') == -1 ) {
1018
            return loader.getPluginName() + "." + name;
1019
        } else {
1020
            return name;
1021
        }
1022
    }
1023

    
1024
    public void addPopupMenu(PluginClassLoader loader, PopupMenu menu) {
1025
        if ( !SwingUtilities.isEventDispatchThread() ) {
1026
            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
        if ( popupMenu == null ) {
1035
            popupMenu = new JPopUpMenu(menu.getName());
1036
            popupMap.put(name, popupMenu);
1037
        }
1038

    
1039
        Menu[] menues = menu.getMenu();
1040
        for ( int i = 0; i < menues.length; i++ ) {
1041
            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
        I18nManager i18nManager = ToolsLocator.getI18nManager();
1050

    
1051
        String text = menu.getText();
1052
        int n = text.lastIndexOf('/');
1053
        if ( n >= 0 ) {
1054
            text = text.substring(n + 1);
1055
        }
1056
        String translatedText = i18nManager.getTranslation(text);
1057

    
1058
        IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent();
1059
        if ( menu.getIcon() != null ) {
1060
            if ( iconTheme.exists(menu.getIcon()) ) {
1061
                ImageIcon image = iconTheme.get(menu.getIcon());
1062
                nuevoMenu = new JMenuItem(translatedText, image);
1063
            } else {
1064
                nuevoMenu = new JMenuItem(translatedText);
1065
                logger.info("menu icon '" + menu.getIcon() + "' not exists.");
1066
            }
1067
        } else {
1068
            nuevoMenu = new JMenuItem(translatedText);
1069
        }
1070
        nuevoMenu.setTextKey(text);
1071
        nuevoMenu.setName(menu.getName());
1072

    
1073
        if ( menu.getKey() != null ) {
1074
            nuevoMenu.setAccelerator(KeyMapping.getKeyStroke(menu.getKey()));
1075
        }
1076

    
1077
        nuevoMenu.setActionCommand(menu.getActionCommand());
1078

    
1079
        if ( menu.getTooltip() != null ) {
1080
            nuevoMenu.setToolTip(i18nManager.getTranslation(menu.getTooltip()));
1081
            nuevoMenu.setToolTipKey(menu.getTooltip());
1082
        }
1083

    
1084
        if ( menu.getEnableText() != null ) {
1085
            nuevoMenu.setEnableText(i18nManager.getTranslation(menu.getEnableText()));
1086
        }
1087

    
1088
        nuevoMenu.setEnabled(true);
1089
        nuevoMenu.setVisible(true);
1090

    
1091
        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
        }
1098
        return nuevoMenu;
1099
    }
1100

    
1101
    private JMenuItem createJMenuItem(ActionInfo action, String text) {
1102
        I18nManager i18nManager = ToolsLocator.getI18nManager();
1103
        String label = null;
1104

    
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
        } else {
1111
            label = text;
1112
        }
1113
        JMenuItem nuevoMenu = new JMenuItem();
1114
        nuevoMenu.setTextKey(label);
1115
        nuevoMenu.setText(i18nManager.getTranslation(label));
1116
        nuevoMenu.setName(action.getName());
1117
        if ( action.getIconName() != null ) {
1118
            nuevoMenu.setIcon(action.getIcon());
1119
        }
1120
        KeyStroke key = action.getKeyStroke();
1121
        if ( key != null ) {
1122
            nuevoMenu.setAccelerator(key);
1123
        }
1124
        nuevoMenu.setActionCommand(action.getCommand());
1125
        if ( action.getTooltip() != null ) {
1126
            nuevoMenu.setToolTip(i18nManager.getTranslation(action.getTooltip()));
1127
            nuevoMenu.setToolTipKey(action.getTooltip());
1128
        }
1129
        nuevoMenu.setEnabled(true);
1130
        nuevoMenu.setVisible(true);
1131
        nuevoMenu.addActionListener(action);
1132
        return nuevoMenu;
1133
    }
1134

    
1135
    /**
1136
     * Muestra u oculta el menu de nombre 'name'
1137
     *
1138
     * @param name
1139
     * Nombre del menu que se quiere mostrar
1140
     * @param x
1141
     * Evento de raton
1142
     * @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
        if ( menu != null ) {
1149
            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
        if ( menu != null ) {
1157
            Component[] jmenuitems = menu.getComponents();
1158

    
1159
            for ( int i = 0; i < jmenuitems.length; i++ ) {
1160
                if ( jmenuitems[i] instanceof JMenuItem ) {
1161
                    ((JMenuItem) jmenuitems[i]).removeActionListener(listener);
1162
                }
1163
            }
1164
        }
1165
    }
1166

    
1167
    public void addPopupMenuListener(String popupName, Component c,
1168
            ActionListener listener, PluginClassLoader loader) {
1169
        final String name = getName(popupName, loader);
1170

    
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]).addActionListener(listener);
1179
                }
1180
            }
1181
        }
1182

    
1183
        c.addMouseListener(new MouseAdapter() {
1184

    
1185
            @Override
1186
            public void mousePressed(MouseEvent e) {
1187
                if ( e.isPopupTrigger() ) {
1188
                    showPopupMenu(name, e.getX(), e.getY(), e.getComponent());
1189
                }
1190
            }
1191

    
1192
            @Override
1193
            public void mouseReleased(MouseEvent e) {
1194
                if ( e.isPopupTrigger() ) {
1195
                    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
     * its associated action (ActionInfo)
1204
     *
1205
     * @throws RuntimeException
1206
     */
1207
    public void enableControls() {
1208
        if ( !SwingUtilities.isEventDispatchThread() ) {
1209
            SwingUtilities.invokeLater(new Runnable() {
1210
                public void run() {
1211
                    enableControls();
1212
                }
1213
            });
1214
            return;
1215
        }
1216

    
1217
        ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
1218
        ActionInfoStatusCache cache = actionManager.createActionStatusCache();
1219

    
1220
        // Enable or disable controls, according to its associated extensions
1221
        Iterator<JComponent> e = controlsIterator();
1222

    
1223
        while ( e.hasNext() ) {
1224
            JComponent control = (JComponent) e.next();
1225
            String actionName = control.getName();
1226
            ActionInfo action = actionManager.getAction(actionName);
1227
            try {
1228
                boolean enabled = false;
1229
                boolean visible = false;
1230

    
1231
                if ( action == null ) {
1232
                    if( control instanceof DropDownButton ) {
1233
                        DropDownButton dropDownButton = (DropDownButton)control;
1234
                        visible = !dropDownButton.isAllHiden();
1235
                        enabled = !dropDownButton.isAllDisabled();
1236
                        if (visible && enabled){
1237
                            dropDownButton.updateMainButton();
1238
                        }
1239
                    } else if( control instanceof JMenuTraslatable ) {
1240
                        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
                } else {
1254
                    enabled = false;
1255
                    visible = cache.isVisible(action);
1256
                    if ( visible ) {
1257
                        enabled = cache.isEnabled(action);
1258
                    }
1259
                }
1260
                //logger.info(control.getClass().getName() +" - " +control.getName() + " visible " + visible );
1261
                control.setEnabled(enabled);
1262
                control.setVisible(visible);
1263
            } catch (Throwable ex) {
1264
                // Catch exceptions and errors (class not found)
1265
                logger.info("Can't enable/show control '" + actionName + "'.", ex);
1266
                this.message("Can't enable/show control '" + actionName + "'.", JOptionPane.ERROR_MESSAGE);
1267
                try {
1268
                    control.setEnabled(false);
1269
                    control.setVisible(false);
1270
                } catch (Exception ex2) {
1271
                    // Ignore errors
1272
                }
1273
            }
1274
        }
1275

    
1276
        // Loop in the menus to hide the menus that don't have visible children
1277
        for ( int i = 0; i < menuBar.getMenuCount(); i++ ) {
1278
            MenuElement menu = menuBar.getMenu(i);
1279
            hideMenus(menu);
1280
            if ( menu instanceof JMenu ) {
1281
                // 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
                if ( comps.length > 0 ) {
1287
                    // Set keyboard mnemonic for this top-level entry
1288
                    String text = ((JMenu) menu).getText();
1289
                    char mnemonic = getMnemonic(text, mnemonics);
1290
                    if ( ' ' != mnemonic ) {
1291
                        ((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
        while ( it.hasNext() ) {
1305
            JComponent t = (JComponent) it.next();
1306
            boolean todosOcultos = true;
1307

    
1308
            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
                    todosOcultos = false;
1313
                }
1314
            }
1315

    
1316
            if ( todosOcultos ) {
1317
                t.setVisible(false);
1318
            } else {
1319
                if ( t instanceof SelectableToolBar ) {
1320
                    t.setVisible(((SelectableToolBar) t).getAndamiVisibility());
1321
                } else {
1322
                    t.setVisible(true);
1323
                }
1324
            }
1325
        }
1326

    
1327
        if ( mdiManager != null ) {
1328
            JPanel f = (JPanel) mdiManager.getActiveWindow();
1329

    
1330
            if ( f != null ) {
1331
                if ( lastLabelClass != f.getClass() ) {
1332
                    lastLabelClass = f.getClass();
1333

    
1334
                    Label[] lbls = (Label[]) classLabels.get(lastLabelClass);
1335

    
1336
                    if ( lbls != null ) {
1337
                        bEstado.setLabelSet(lbls);
1338
                    }
1339
                }
1340
            }
1341
        }
1342

    
1343
        ajustarToolBar();
1344

    
1345
        showMemory();
1346
    }
1347

    
1348
    public synchronized void refreshControls() {
1349
        if ( !SwingUtilities.isEventDispatchThread() ) {
1350
            SwingUtilities.invokeLater(new Runnable() {
1351
                public void run() {
1352
                    refreshControls();
1353
                }
1354
            });
1355
            return;
1356
        }
1357

    
1358
        if(refreshingControls){
1359
            return;
1360
        }
1361
        try {
1362
        refreshingControls = true;
1363
        ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
1364
        ActionInfoStatusCache cache = actionManager.createActionStatusCache();
1365
        IconTheme icontheme = ToolsSwingLocator.getIconThemeManager().getCurrent();
1366

    
1367
        Iterator e = controlsIterator();
1368

    
1369
        while ( e.hasNext() ) {
1370
            JComponent control = (JComponent) e.next();
1371
            if ( control instanceof TranslatableButton ) {
1372
                ((TranslatableButton) control).translate();
1373
            }
1374
            String actionlName = control.getName();
1375
            ActionInfo action = actionManager.getAction(actionlName);
1376
            if ( action != null ) {
1377
                if ( control instanceof AbstractButton ) {
1378
                    AbstractButton button = (AbstractButton) control;
1379
                    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
            }
1390
        }
1391

    
1392
        enableControls();
1393
        } finally {
1394
            refreshingControls = false;
1395
        }
1396
    }
1397

    
1398
    public void message(String msg, int messageTyoe) {
1399
        this.getStatusBar().message(msg, messageTyoe);
1400
    }
1401

    
1402
    /**
1403
     * Establece la visibilidad de un menu y todos sus descendientes en la
1404
     * jerarquia teniendo en cuenta la visibilidad de todos los submenus.
1405
     *
1406
     * @param menu
1407
     * Menu que se quiere visualizar
1408
     *
1409
     * @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
        if ( submenus.length == 0 ) {
1416
            return menu.getComponent().isVisible();
1417
        }
1418

    
1419
        /*
1420
         * Si tiene hijos se devuelve true si alg�no de ellos es visible,
1421
         * pero se itera por todos ellos
1422
         */
1423
        boolean visible = false;
1424

    
1425
        for ( int i = 0; i < submenus.length; i++ ) {
1426
            if ( hideMenus(submenus[i]) ) {
1427
                if ( !(menu instanceof JPopupMenu) ) {
1428
                    menu.getComponent().setVisible(true);
1429
                }
1430

    
1431
                visible = true;
1432
            }
1433
        }
1434

    
1435
        if ( visible ) {
1436
            return true;
1437
        }
1438

    
1439
        menu.getComponent().setVisible(false);
1440

    
1441
        return false;
1442
    }
1443

    
1444
    /**
1445
     *
1446
     * 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
     *
1451
     *
1452
     */
1453
    private void hideSeparatorsAndMakeMnemonics(MenuElement menu,
1454
            char[] mnemonics) {
1455
        // 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
        if ( comps.length < 1 ) {
1461
            // zero-length menu: skip
1462
            return;
1463
        }
1464

    
1465
        for ( int i = 0; i < comps.length; i++ ) {
1466
            if ( comps[i] instanceof JSeparator ) {
1467
                // got a separator: display only if allowed at this position
1468
                if ( allowSeparator == true ) {
1469
                    // 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
                    for ( int j = i; j < comps.length; j++ ) {
1477
                        if ( !(comps[j] instanceof JSeparator) ) {
1478
                            if ( comps[j].isVisible() ) {
1479
                                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
                // another separator
1489
            } else {
1490
                if ( comps[i] instanceof JMenu ) { // got a submenu: recurse
1491
                    // through it
1492
                    // 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
                            smnemonics);
1499
                    if ( comps[i].isVisible() ) {
1500
                        allowSeparator = true; // separators are OK after
1501
                        // visible submenus
1502
                        // Set keyboard mnemonic for this submenu
1503
                        String text = ((JMenu) comps[i]).getText();
1504
                        char mnemonic = getMnemonic(text, mnemonics);
1505
                        if ( ' ' != mnemonic ) {
1506
                            ((JMenu) comps[i]).setMnemonic(mnemonic);
1507
                            mnemonics[i] = mnemonic;
1508
                        }
1509
                    }
1510
                } else {
1511
                    if ( comps[i].isVisible() ) {
1512
                        if ( comps[i] instanceof JMenuItem ) {
1513
                            // Set keyboard mnemonic for this menu item
1514
                            String text = ((JMenuItem) comps[i]).getText();
1515
                            char mnemonic = getMnemonic(text, mnemonics);
1516
                            if ( ' ' != mnemonic ) {
1517
                                ((JMenuItem) comps[i]).setMnemonic(mnemonic);
1518
                                mnemonics[i] = mnemonic;
1519
                            }
1520
                        }
1521
                        allowSeparator = true; // separators are OK after
1522
                        // regular, visible entries
1523
                    }
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
        if( text==null ) {
1535
            return ' ';
1536
        }
1537
        Vector words = new Vector();
1538
        StringTokenizer t = new StringTokenizer(text);
1539
        int maxsize = 0;
1540

    
1541
        while ( t.hasMoreTokens() ) {
1542
            String word = t.nextToken();
1543
            if ( word.length() > maxsize ) {
1544
                maxsize = word.length();
1545
            }
1546
            words.addElement(word);
1547
        }
1548
        words.trimToSize();
1549

    
1550
        for ( int i = 0; i < maxsize; ++i ) {
1551
            char mnemonic = getMnemonic(words, mnemonics, i);
1552
            if ( ' ' != mnemonic ) {
1553
                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
        for ( int i = 0; i < numwords; ++i ) {
1564
            String word = (String) words.elementAt(i);
1565
            if ( index >= word.length() ) {
1566
                continue;
1567
            }
1568

    
1569
            char c = word.charAt(index);
1570
            if ( !isMnemonicExists(c, mnemonics) ) {
1571
                /* pick only valid chars */
1572
                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
                    return c;
1580
                }
1581
            }
1582
        }
1583
        return ' ';
1584
    }
1585

    
1586
    private boolean isMnemonicExists(char c, char[] mnemonics) {
1587
        int num = mnemonics.length;
1588
        for ( int i = 0; i < num; ++i ) {
1589
            if ( mnemonics[i] == c ) {
1590
                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
                / 1024 + " KB");
1604
    }
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
        if ( group == null ) {
1629
            return;
1630
        }
1631

    
1632
        Enumeration enumeration = group.getElements();
1633
        while ( enumeration.hasMoreElements() ) {
1634
            AbstractButton button = (AbstractButton) enumeration.nextElement();
1635
            if ( button.getActionCommand().equals(actionCommand) ) {
1636
                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
        if ( selectedTools == null ) {
1650
            return;
1651
        }
1652
        Iterator groupNames = selectedTools.keySet().iterator();
1653
        while ( groupNames.hasNext() ) {
1654
            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
                if ( actionCommand == null ) {
1660
                    continue;
1661
                }
1662
                while ( enumeration.hasMoreElements() ) {
1663
                    AbstractButton button
1664
                            = (AbstractButton) enumeration.nextElement();
1665
                    if ( button.getActionCommand().equals(actionCommand) ) {
1666
                        button.setSelected(true);
1667
                    }
1668
                }
1669
            } catch (ClassCastException ex) {
1670
                logger
1671
                        .error("selectedTool should only contain pairs (String groupName, JToolBarToggleButton button)");
1672
            }
1673
        }
1674
    }
1675

    
1676
    /**
1677
     * DOCUMENT ME!
1678
     *
1679
     * @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
        if ( control instanceof JComponent ) {
1694
            addControl((JComponent) control);
1695
            PluginsManager pluginsManager = PluginsLocator.getManager();
1696
            IExtension extension = pluginsManager.getExtension((Class<? extends IExtension>) extensionClass);
1697
            this.control2extensions.put((JComponent)control, extension);
1698
        }
1699
    }
1700

    
1701
    public void addToolBarControl(Class<?> extensionClass, JToolBar control, String name) {
1702
        toolBars.add(control);
1703
        addControl(control);
1704

    
1705
        PluginsManager pluginsManager = PluginsLocator.getManager();
1706
        IExtension extension = pluginsManager.getExtension((Class<? extends IExtension>) extensionClass);
1707
        this.control2extensions.put(control, extension);
1708
    }
1709

    
1710
    public void removeStatusBarControl(String name) {
1711
        Component c = bEstado.removeControl(name);
1712
        if ( c instanceof JComponent ) {
1713
            removeControl((JComponent) c);
1714
        }
1715
    }
1716

    
1717
    public void removeMenu(Menu menu) {
1718
        JMenuItem delete = (JMenuItem) infoCodedMenus.get(menu);
1719

    
1720
        if ( delete == null ) {
1721
            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
            PluginClassLoader loader) {
1730
        JMenu menuPadre = createMenuAncestors(menu, loader);
1731

    
1732
        // Se registra y añaade el menu
1733
        JMenuItem nuevoMenu = createJMenuItem(loader, menu);
1734
        nuevoMenu.addMouseListener(tooltipListener);
1735
        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
        } else {
1743
            /*
1744
             * We also add action listener for menu with
1745
             * no name but with text:
1746
             *
1747
             * Window/Project manager
1748
             * Window/View - 1
1749
             * Window/View - 2
1750
             * etc
1751
             */
1752
            if ( listener != null && menu.getText() != null && menu.getText().trim().length() > 0 ) {
1753
                nuevoMenu.addActionListener(listener);
1754
            }
1755

    
1756
        }
1757
        menuPadre.add(nuevoMenu);
1758

    
1759
        infoCodedMenus.put(menu, nuevoMenu);
1760
    }
1761

    
1762
    public void changeMenuName(String[] menu, String newName,
1763
            PluginClassLoader loader) {
1764

    
1765
        ArrayList menuList = new ArrayList();
1766
        for ( int i = 0; i < menu.length; i++ ) {
1767
            menuList.add(menu[i]);
1768
        }
1769

    
1770
        javax.swing.JMenuItem newMenu = getMenu(menuList, menuBar);
1771
        if ( newMenu == null ) {
1772
            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
            if ( texto != null ) {
1810
                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
     * Get a previously added JComponent by name.
1843
     * For example you can use it if you need to obtain a JToolBar to
1844
     * add some customized component.
1845
     *
1846
     * @param name
1847
     * @return the JComponent or null if none has been found
1848
     */
1849
    public JComponent getComponentByName(String name) {
1850
        Iterator e = controlsIterator();
1851

    
1852
        while ( e.hasNext() ) {
1853
            JComponent control = (JComponent) e.next();
1854
            String nameCtrl = control.getName();
1855
            if ( nameCtrl != null ) {
1856
                if ( nameCtrl.compareTo(name) == 0 ) {
1857
                    return control;
1858
                }
1859
            }
1860
        }
1861
        Iterator it = toolBarMap.values().iterator();
1862
        while ( it.hasNext() ) {
1863
            JComponent t = (JComponent) it.next();
1864
            String nameCtrl = t.getName();
1865
            if ( nameCtrl != null ) {
1866
                if ( nameCtrl.compareTo(name) == 0 ) {
1867
                    return t;
1868
                }
1869
            }
1870

    
1871
        }
1872

    
1873
        return null;
1874
    }
1875

    
1876
    public SelectableToolBar[] getToolbars() {
1877
        return (SelectableToolBar[]) toolBarMap.values().toArray(
1878
                new SelectableToolBar[0]);
1879
    }
1880

    
1881
    public boolean getToolbarVisibility(String name) {
1882
        JComponent component
1883
                = PluginServices.getMainFrame().getComponentByName(name);
1884
        if ( (component != null) && (component instanceof SelectableToolBar) ) {
1885
            SelectableToolBar toolBar = (SelectableToolBar) component;
1886
            return toolBar.getAndamiVisibility();
1887
        }
1888
        return false;
1889
    }
1890

    
1891
    public boolean setToolbarVisibility(String name, boolean visibility) {
1892
        JComponent component
1893
                = PluginServices.getMainFrame().getComponentByName(name);
1894
        if ( (component != null) && (component instanceof SelectableToolBar) ) {
1895
            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
        for ( int i = 0; i < menuPath.length; i++ ) {
1907
            menu.add(menuPath[i]);
1908
        }
1909
        return getMenu(menu, menuBar);
1910
    }
1911

    
1912
    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

    
1917
    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

    
1923
    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

    
1929
    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

    
1935
    public String inputDialog(String message, String title) {
1936
        DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado);
1937
        return helper.inputDialog(message, title);
1938
    }
1939

    
1940
    public void showDialog(Component contents, String title) {
1941
        DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado);
1942
        helper.showDialog(contents, title);
1943
    }
1944

    
1945
    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

    
1951
    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

    
1957
    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

    
1970
    public File[] showOpenDirectoryDialog(String title, File initialPath) {
1971
        return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.DIRECTORIES_ONLY, false, initialPath, null, false);
1972
    }
1973

    
1974
    public File[] showOpenFileDialog(String title, File initialPath) {
1975
        return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, null, false);
1976
    }
1977

    
1978
    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
}