Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / ui / mdiFrame / MDIFrame.java @ 39103

History | View | Annotate | Download (60.8 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.andami.ui.mdiFrame;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.Insets;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.awt.event.ComponentEvent;
51
import java.awt.event.ComponentListener;
52
import java.awt.event.ContainerEvent;
53
import java.awt.event.ContainerListener;
54
import java.awt.event.MouseAdapter;
55
import java.awt.event.MouseEvent;
56
import java.awt.event.WindowAdapter;
57
import java.awt.event.WindowEvent;
58
import java.lang.reflect.InvocationTargetException;
59
import java.util.ArrayList;
60
import java.util.Enumeration;
61
import java.util.HashMap;
62
import java.util.Iterator;
63
import java.util.Map;
64
import java.util.Map.Entry;
65
import java.util.NoSuchElementException;
66
import java.util.StringTokenizer;
67
import java.util.Vector;
68

    
69
import javax.swing.AbstractButton;
70
import javax.swing.ButtonGroup;
71
import javax.swing.ImageIcon;
72
import javax.swing.JComponent;
73
import javax.swing.JFrame;
74
import javax.swing.JMenu;
75
import javax.swing.JMenuBar;
76
import javax.swing.JOptionPane;
77
import javax.swing.JPanel;
78
import javax.swing.JPopupMenu;
79
import javax.swing.JSeparator;
80
import javax.swing.JToolBar;
81
import javax.swing.MenuElement;
82
import javax.swing.SwingUtilities;
83
import javax.swing.Timer;
84
import javax.swing.WindowConstants;
85

    
86
import org.gvsig.andami.IconThemeHelper;
87
import org.gvsig.andami.Launcher;
88
import org.gvsig.andami.LibraryExtension;
89
import org.gvsig.andami.PluginServices;
90
import org.gvsig.andami.PluginsLocator;
91
import org.gvsig.andami.actioninfo.ActionInfo;
92
import org.gvsig.andami.actioninfo.ActionInfoManager;
93
import org.gvsig.andami.actioninfo.ActionInfoStatusCache;
94
import org.gvsig.andami.messages.Messages;
95
import org.gvsig.andami.plugins.ExtensionDecorator;
96
import org.gvsig.andami.plugins.IExtension;
97
import org.gvsig.andami.plugins.PluginClassLoader;
98
import org.gvsig.andami.plugins.config.generate.ActionTool;
99
import org.gvsig.andami.plugins.config.generate.Label;
100
import org.gvsig.andami.plugins.config.generate.Menu;
101
import org.gvsig.andami.plugins.config.generate.PopupMenu;
102
import org.gvsig.andami.plugins.config.generate.SelectableTool;
103
import org.gvsig.andami.plugins.config.generate.SkinExtensionType;
104
import org.gvsig.andami.plugins.config.generate.ToolBar;
105
import org.gvsig.andami.ui.mdiManager.MDIManager;
106
import org.gvsig.andami.ui.mdiManager.MDIManagerFactory;
107
import org.gvsig.gui.beans.controls.IControl;
108
import org.gvsig.tools.swing.api.ToolsSwingLocator;
109
import org.gvsig.tools.swing.icontheme.IconTheme;
110
import org.slf4j.Logger;
111
import org.slf4j.LoggerFactory;
112

    
113
/**
114
 * Main application window.
115
 * 
116
 * @version $Revision: 39103 $
117
 */
118
@SuppressWarnings("unchecked")
119
public class MDIFrame extends JFrame implements ComponentListener,
120
    ContainerListener, ActionListener, MainFrame {
121

    
122
    private static final long serialVersionUID = -2472484309160847654L;
123

    
124
    private static Logger logger = LoggerFactory.getLogger(MDIFrame.class);
125
    private MDIManager mdiManager = MDIManagerFactory.createManager();
126

    
127
    /** Elementos de la aplicaci�n */
128
    private JMenuBar menuBar = new JMenuBar();
129

    
130
    /** Panel which contains the toolbars */
131
    private JPanel toolBars = new JPanel();
132

    
133
    /** Status bar */
134
    private NewStatusBar bEstado = null;
135

    
136
    /** Asocia los nombres con las barras de herramientas */
137
    private HashMap toolBarMap = new HashMap();
138

    
139
    /** Almacena los grupos de selectableTools */
140
    private HashMap buttonGroupMap = new HashMap();
141
    /**
142
     * Stores the initially selected tools.
143
     * It contains pairs (String groupName, JToolBarToggleButton button)
144
     */
145
    private HashMap initialSelectedTools = new HashMap();
146

    
147
    /**
148
     * Stores the actionCommand of the selected tool, for each group.
149
     * It contains pairs (String groupName, JToolBarToggleButton button)
150
     */
151
    private Map selectedTool = null;
152
    // this should be the same value defined at plugin-config.xsd
153
    private String defaultGroup = "unico";
154

    
155
    /** Asocia los nombres con los popupMenus */
156
    private HashMap popupMap = new HashMap();
157

    
158
    /** Asocia controles con la clase de la extension asociada */
159
    private HashMap controlClass = new HashMap();
160

    
161
    /**
162
     * Asocia la informaci�n sobre las etiquetas que van en la status bar con
163
     * cada extension
164
     */
165
    private HashMap classLabels = new HashMap();
166

    
167
    // private HashMap classControls = new HashMap();
168

    
169
    /** ProgressListeners (ver interfaz com.iver.mdiApp.ui.ProgressListener) */
170
    private ArrayList progressListeners = new ArrayList();
171

    
172
    /** Timer para invocar los enventos de la interfaz anterior */
173
        private Timer progressTimer = null;
174

    
175
    /** Tabla hash que asocia las clases con las extensiones */
176
    private Map classesExtensions = new HashMap<Class<? extends IExtension>, ExtensionDecorator>();
177

    
178
    /** �ltima clase que activ� etiquetas */
179
    private Class lastLabelClass;
180

    
181
    /** Instancia que pone los tooltip en la barra de estado */
182
    private TooltipListener tooltipListener = new TooltipListener();
183
    
184
        private HashMap infoCodedMenus = new HashMap();
185

    
186
    private String titlePrefix;
187

    
188
    private static final String noIcon = "no-icon";
189

    
190
    /**
191
     * Makes some initialization tasks.
192
     * 
193
     * @throws RuntimeException
194
     *             DOCUMENT ME!
195
     */
196
    public void init() {
197
            JPopupMenu.setDefaultLightWeightPopupEnabled(false);
198
        if (!SwingUtilities.isEventDispatchThread()) {
199
            throw new RuntimeException("Not Event Dispatch Thread");
200
        }
201

    
202
        // Se a�aden los listeners del JFrame
203
        this.addWindowListener(new WindowAdapter() {
204

    
205
            @Override
206
            public void windowClosing(WindowEvent e) {
207
                Launcher.closeApplication();
208
            }
209
        });
210
        this.addComponentListener(this);
211
        this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
212

    
213
        // Se configura la barra de menu
214
        setJMenuBar(menuBar);
215

    
216
        // Se configura el layout del JFrame principal
217
        this.getContentPane().setLayout(new BorderLayout());
218

    
219
        /*
220
         * Se configura y se a�ade el JPanel de las barras de
221
         * herramientas
222
         */
223
        FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
224
        layout.setHgap(0);
225
        layout.setVgap(0);
226
        toolBars.setLayout(layout);
227
        getContentPane().add(toolBars, BorderLayout.PAGE_START);
228

    
229
        // Se a�ade la barra de estado a la aplicaci�n
230
        bEstado = new NewStatusBar();
231
        bEstado.message(Messages.getString("StatusBar.Aplicacion_iniciada"), JOptionPane.INFORMATION_MESSAGE);
232
        getContentPane().add(bEstado, BorderLayout.SOUTH);
233

    
234
        this.toolBars.addContainerListener(this);
235

    
236
        // TODO LWS Aqui deber�a cargar los valores salvados de la �ltima
237
        // ejecuci�n.
238
        setSize(700, 580);
239
        setLocation(10, 10);
240
        setExtendedState(MAXIMIZED_BOTH);
241

    
242
        mdiManager.init(this);
243

    
244
        pack();
245

    
246
    }
247

    
248
    /*
249
     * (non-javadoc)
250
     * 
251
     * @see java.awt.Frame.setTitle(String title)
252
     */
253
    @Override
254
    public void setTitle(final String title) {
255
        if (!SwingUtilities.isEventDispatchThread()) {
256
            SwingUtilities.invokeLater(new Runnable() {
257
                public void run() {
258
                        setTitle(title);
259
                }
260
            });
261
            return;
262
        }
263
        super.setTitle(titlePrefix + " : " + title);
264
    }
265

    
266
    /**
267
     * A�ade un modo de operaci�n a la caja de herramientas
268
     * 
269
     * @param ext
270
     *            Texto del boton, si es null no aparece texto
271
     * @param ext
272
     *            Icono del boton, si es null no aparece icono
273
     * @param ext
274
     *            Extensi�n asociada al control
275
     * @param selectableTool
276
     *            Enable text del control
277
     * 
278
     * @throws ClassNotFoundException
279
     * @throws RuntimeException
280
     *             DOCUMENT ME!
281
     */
282
    public void addTool(PluginClassLoader loader, SkinExtensionType ext,
283
        ToolBar toolBar, SelectableTool selectableTool)
284
        throws ClassNotFoundException {
285
        if (!SwingUtilities.isEventDispatchThread()) {
286
            throw new RuntimeException("No Event Dispatch Thread");
287
        }
288
        
289
        Class<? extends IExtension> classExtension = (Class<? extends IExtension>) loader.loadClass(ext.getClassName());
290
        
291
        // Para traducir
292
        PluginServices ps =
293
            PluginServices.getPluginServices(loader.getPluginName());
294

    
295
        JToolBarToggleButton btn;
296
        ImageIcon image =
297
            PluginServices.getIconTheme().get(selectableTool.getIcon());
298

    
299
        if (image != null) {
300
            btn = new JToolBarToggleButton(selectableTool.getText(), image);
301
        } else {
302
            logger.error(PluginServices.getText(this, "Unable_to_find_icon")
303
                + ": " + selectableTool.getIcon());
304
            btn =
305
                new JToolBarToggleButton(selectableTool.getText(),
306
                    PluginServices.getIconTheme().get(noIcon));
307
        }
308

    
309
        org.gvsig.andami.ui.mdiFrame.ToggleButtonModel buttonModel =
310
            new org.gvsig.andami.ui.mdiFrame.ToggleButtonModel();
311
        btn.setModel(buttonModel);
312
        btn.setMargin(new Insets(0, 0, 0, 0));
313
        btn.addMouseListener(tooltipListener);
314
        btn.addActionListener(this);
315
        btn.setFocusable(false);
316
        btn.setActionCommand(selectableTool.getActionCommand());
317
        btn.setToolTipText(selectableTool.getTooltip());
318
        btn.setEnabled(false);
319
        btn.setVisible(false);
320
        String name = toolBar.getName();
321

    
322
        SelectableToolBar jtb = (SelectableToolBar) toolBarMap.get(name);
323

    
324
        if (jtb == null) {
325
            jtb = new SelectableToolBar(name);
326
            jtb.setRollover(true);
327
            jtb.setAndamiVisibility(toolBar.getIsVisible());
328
            toolBarMap.put(name, jtb);
329
            toolBars.add(jtb);
330
        }
331

    
332
        ButtonGroup group;
333
        if (buttonGroupMap.containsKey(selectableTool.getGroup())) {
334
            group = (ButtonGroup) buttonGroupMap.get(selectableTool.getGroup());
335
        } else {
336
            group = new ButtonGroup();
337
            buttonGroupMap.put(selectableTool.getGroup(), group);
338

    
339
        }
340
        jtb.addButton(group, btn);
341
        buttonModel.setGroupName(selectableTool.getGroup());
342

    
343
        if (selectableTool.getIsDefault()) {
344
            btn.setSelected(true);
345
            initialSelectedTools.put(selectableTool.getGroup(),
346
                btn.getActionCommand());
347
        }
348

    
349
        controlClass.put(btn, classExtension);
350

    
351
        if (selectableTool.getName() != null) {
352
            btn.setName(selectableTool.getName());
353
        }
354

    
355
        if (selectableTool.getTooltip() != null) {
356
            btn.setToolTip(ps.getText(selectableTool.getTooltip()));
357
        }
358

    
359
        if (selectableTool.getEnableText() != null) {
360
            btn.setEnableText(ps.getText(selectableTool.getEnableText()));
361
        }
362

    
363
        if (selectableTool.getLast() == true) {
364
            jtb.addSeparator();
365
        }
366
    }
367

    
368
    /**
369
     * A�ade un bot�n a la barra de herramientas
370
     * 
371
     * @param ext
372
     *            Texto del boton, si es null no aparece texto
373
     * @param ext
374
     *            Extensi�n asociada al control
375
     * @param toolBar
376
     *            Icono del boton, si es null no aparece texto
377
     * @param actionTool
378
     *            Tooltip de la barra de herramientas
379
     * 
380
     * @throws ClassNotFoundException
381
     * @throws RuntimeException
382
     *             DOCUMENT ME!
383
     */
384
    public void addTool(PluginClassLoader loader, SkinExtensionType ext,
385
        ToolBar toolBar, ActionTool actionTool) throws ClassNotFoundException {
386
        if (!SwingUtilities.isEventDispatchThread()) {
387
            throw new RuntimeException("No Event Dispatch Thread");
388
        }
389
        Class<? extends IExtension> classExtension = (Class<? extends IExtension>) loader.loadClass(ext.getClassName());
390
        
391
        // Para traducir los textos que vengan
392
        PluginServices ps =
393
            PluginServices.getPluginServices(loader.getPluginName());
394

    
395
        JToolBarButton btn;
396
        ImageIcon image = IconThemeHelper.getImageIcon(actionTool.getIcon());
397

    
398
        if (image != null) {
399
            btn = new JToolBarButton(actionTool.getText(), image);
400
        } else {
401
            logger.error(PluginServices.getText(this, "Unable_to_find_icon")
402
                + ": " + actionTool.getIcon());
403
            btn =
404
                new JToolBarButton(actionTool.getText(), PluginServices
405
                    .getIconTheme().get(noIcon));
406
        }
407

    
408
        btn.setMargin(new Insets(0, 0, 0, 0));
409
        btn.addMouseListener(tooltipListener);
410
        btn.addActionListener(this);
411
        btn.setFocusable(false);
412
        btn.setActionCommand(actionTool.getActionCommand());
413
        btn.setEnabled(false);
414
        btn.setVisible(false);
415

    
416
        String name = toolBar.getName();
417

    
418
        SelectableToolBar jtb = (SelectableToolBar) toolBarMap.get(name);
419

    
420
        if (jtb == null) {
421
            jtb = new SelectableToolBar(name);
422
            jtb.setRollover(true);
423
            jtb.setAndamiVisibility(toolBar.getIsVisible());
424
            toolBarMap.put(name, jtb);
425
            toolBars.add(jtb);
426
        }
427

    
428
        jtb.add(btn);
429

    
430
        controlClass.put(btn, classExtension);
431

    
432
        if (actionTool.getName() != null) {
433
            btn.setName(actionTool.getName());
434
        }
435

    
436
        if (actionTool.getTooltip() != null) {
437
            btn.setToolTip(ps.getText(actionTool.getTooltip()));
438
        }
439

    
440
        if (actionTool.getEnableText() != null) {
441
            btn.setEnableText(ps.getText(actionTool.getEnableText()));
442
        }
443

    
444
        if (actionTool.getLast() == true) {
445
            jtb.addSeparator();
446
        }
447
    }
448

    
449
    /**
450
     * Creates the needed menu structure to add the menu to the bar.
451
     * Returns the father which must hold the menu which was
452
     * provided as parameter.
453
     * 
454
     * Crea la estructura de men�s necesaria para a�adir el menu a la barra.
455
     * Devuelve el padre del cual debe colgar el menu que se pasa como
456
     * par�metro.
457
     * 
458
     * @param menu
459
     *            The Menu whose support is going to be added
460
     * @param loader
461
     *            The plugin's class loader
462
     * 
463
     * @return The proper father for the menu which was provided as parameter
464
     */
465
    private JMenu createMenuAncestors(Menu menu, PluginClassLoader loader) {
466
        MenuElement menuPadre = null;
467

    
468
        PluginServices ps =
469
            PluginServices.getPluginServices(loader.getPluginName());
470

    
471
        String[] menues = menu.getText().split("/");
472
        ArrayList menuList = new ArrayList();
473
        menuList.add(menues[0]);
474
        menuPadre = getMenu(menuList, menuBar);
475

    
476
        JMenu padre = null;
477

    
478
        if (menuPadre == null) {
479
            padre = new JMenu(ps.getText(menues[0]));
480
            padre.setName(menues[0]);
481
            menuBar.add(padre);
482
        } else
483
            if (menuPadre instanceof JMenu) {
484
                padre = (JMenu) menuPadre;
485
            } else {
486
                logger.error(ps
487
                    .getText("error_creating_menu_Ancestor_does_not_exist"));
488
                return null;
489
            }
490

    
491
        // Se crea el resto de menus
492
        ArrayList temp = new ArrayList();
493

    
494
        for (int i = 1; i < (menues.length - 1); i++) {
495
            temp.add(menues[i]);
496
        }
497

    
498
        menuPadre = createMenus(temp, padre);
499

    
500
        return (JMenu) menuPadre;
501
    }
502

    
503
    /**
504
     * A�ade la informaci�n del menu al framework. Debido a que los men�es
505
     * se
506
     * pueden introducir en un orden determinado por el usuario, pero los
507
     * plugins se instalan en un orden arbitrario, primero se almacena la
508
     * informaci�n de todos los menus para luego ordenarlos y posteriormente
509
     * a�adirlos al interfaz
510
     * 
511
     * @param loader
512
     *            Posicion del menu. Se ordena por este campo
513
     * @param ext
514
     *            Array con los nombres de los padres del menu
515
     * @param menu
516
     *            Texto del menu
517
     * 
518
     * @throws ClassNotFoundException
519
     * @throws RuntimeException
520
     *             DOCUMENT ME!
521
     */
522
    public void addMenu(PluginClassLoader loader, SkinExtensionType ext,
523
        Menu menu) throws ClassNotFoundException {
524
        if (!SwingUtilities.isEventDispatchThread()) {
525
            throw new RuntimeException("No Event Dispatch Thread");
526
        }
527
                JMenu menuPadre = createMenuAncestors(menu, loader);
528

    
529
        if (menu.getIs_separator()) {
530
            menuPadre.addSeparator();
531
            return;
532
        }
533

    
534
        JMenuItem nuevoMenu = createJMenuItem(loader, menu);
535
        nuevoMenu.addMouseListener(tooltipListener);
536
        //nuevoMenu.addActionListener(this);
537
        menuPadre.add(nuevoMenu);
538
        Class<? extends IExtension> classExtension = (Class<? extends IExtension>) loader.loadClass(ext.getClassName());
539
        controlClass.put(nuevoMenu, classExtension);
540
    }
541

    
542
    /**
543
     * Dado un array de nombres de menu, encuentra el men�
544
     * 
545
     * @param nombres
546
     *            DOCUMENT ME!
547
     * @param padre
548
     *            DOCUMENT ME!
549
     * 
550
     * @return DOCUMENT ME!
551
     */
552
    private javax.swing.JMenuItem getMenu(ArrayList nombres, MenuElement parent) {
553
        if (parent instanceof javax.swing.JMenu) {
554
            javax.swing.JMenu parentItem = (javax.swing.JMenu) parent;
555

    
556
            for (int i = 0; i < parentItem.getMenuComponentCount(); i++) {
557
                if ((parentItem.getMenuComponent(i).getName() != null // not a
558
                                                                      // JToolBar.Separator
559
                    )
560
                    && (parentItem.getMenuComponent(i).getName()
561
                        .compareTo((String) nombres.get(0)) == 0)) {
562
                    nombres.remove(0);
563
                    if (nombres.isEmpty()) {
564
                        if (parentItem.getMenuComponent(i) instanceof javax.swing.JMenuItem) {
565
                            return (javax.swing.JMenuItem) parentItem
566
                                .getMenuComponent(i);
567
                        } else {
568
                            logger.error(PluginServices.getText(this,
569
                                "Menu_type_not_supported_")
570
                                + " "
571
                                + parentItem.getMenuComponent(i).getClass()
572
                                    .getName());
573
                            return null;
574
                        }
575
                    } else {
576
                        return getMenu(nombres,
577
                            (MenuElement) parentItem.getMenuComponent(i));
578
                    }
579
                }
580
            }
581
        } else
582
            if (parent instanceof JMenuBar) {
583
                javax.swing.JMenuBar parentItem = (javax.swing.JMenuBar) parent;
584

    
585
                for (int i = 0; i < parentItem.getMenuCount(); i++) {
586
                    if ((parentItem.getMenu(i).getName() != null // not a
587
                                                                 // JToolBar.Separator
588
                        )
589
                        && (parentItem.getMenu(i).getName()
590
                            .compareTo((String) nombres.get(0)) == 0)) {
591
                        nombres.remove(0);
592
                        if (nombres.isEmpty()) {
593
                            if (parentItem.getMenu(i) instanceof javax.swing.JMenuItem) {
594
                                return parentItem.getMenu(i);
595
                            } else {
596
                                logger.error(PluginServices.getText(this,
597
                                    "Menu_type_not_supported_")
598
                                    + " "
599
                                    + parentItem.getMenu(i).getClass()
600
                                        .getName());
601
                                return null;
602
                            }
603
                        } else {
604
                            return getMenu(nombres, parentItem.getMenu(i));
605
                        }
606
                    }
607
                }
608
            } else {
609
                logger.error(PluginServices.getText(this,
610
                    "Menu_type_not_supported_")
611
                    + " "
612
                    + parent.getClass().getName() + " " + parent.toString());
613
            }
614
        return null;
615
    }
616

    
617
    /**
618
     * Crea la estructura de menus recursivamente. Por ejemplo, si se le pasa
619
     * en el par�metro nombres el array {"Search", "References", "Workspace"}
620
     * crear� un men� Search, un submen� del anterior que se llamar�
621
     * References y debajo de �ste �ltimo otro menu llamado Workspace
622
     * 
623
     * @param nombres
624
     *            Array con los nombres de los men�s que se quieren crear
625
     * @param padre
626
     *            Menu padre de los men�s creados. Es �til porque es un
627
     *            algoritmo recursivo
628
     * 
629
     * @return Devuelve el men� creado. Al final de toda la recursividad,
630
     *         devolver� el men� de m�s abajo en la jerarqu�a
631
     * 
632
     * @throws RuntimeException
633
     *             DOCUMENT ME!
634
     */
635
    private JMenu createMenus(ArrayList nombres, JMenu padre) {
636
        if (!SwingUtilities.isEventDispatchThread()) {
637
            throw new RuntimeException("No Event Dispatch Thread");
638
        }
639

    
640
        // si no quedan nombres de menu por crear se vuelve: caso base
641
        if (nombres.size() == 0) {
642
            return padre;
643
        }
644

    
645
        // Se busca el menu por si ya existiera para no crearlo otra vez
646
        JMenu buscado = null;
647

    
648
        for (int i = 0; i < padre.getMenuComponentCount(); i++) {
649
            try {
650
                JMenu hijo = (JMenu) padre.getMenuComponent(i);
651

    
652
                if (hijo.getName().compareTo((String) nombres.get(0)) == 0) {
653
                    buscado = hijo;
654
                }
655
            } catch (ClassCastException e) {
656
                /*
657
                 * Se ha encontrado un elemento hoja del arbol de men�es
658
                 */
659
            }
660
        }
661

    
662
        if (buscado != null) {
663
            // Si lo hemos encontrado creamos el resto
664
            nombres.remove(0);
665

    
666
            return createMenus(nombres, buscado);
667
        } else {
668
            // Si no lo hemos encontrado se crea el menu, se a�ade al padre
669
            // y se crea el resto
670
            String nombre = (String) nombres.get(0);
671
            JMenu menuPadre = new JMenu(PluginServices.getText(this, nombre));
672
            menuPadre.setName(nombre);
673
            padre.add(menuPadre);
674

    
675
            nombres.remove(0);
676

    
677
            return createMenus(nombres, menuPadre);
678
        }
679
    }
680

    
681
    /**
682
     * M�todo invocado en respuesta a ciertos eventos de la interfaz que
683
     * pueden
684
     * ocultar botones de las barras de herramientas y que redimensiona �sta
685
     * de manera conveniente para que no se oculte ninguno
686
     */
687
    private void ajustarToolBar() {
688
        int margen = 8;
689
        int numFilas = 1;
690
        double acum = margen;
691

    
692
        int toolHeight = 0;
693

    
694
        for (int i = 0; i < toolBars.getComponentCount(); i++) {
695
            Component c = toolBars.getComponent(i);
696

    
697
            if (!c.isVisible()) {
698
                continue;
699
            }
700

    
701
            double width = c.getPreferredSize().getWidth();
702
            acum = acum + width;
703

    
704
            if (acum > this.getWidth()) {
705
                numFilas++;
706
                acum = width + margen;
707
            }
708

    
709
            if (c.getPreferredSize().getHeight() > toolHeight) {
710
                toolHeight = c.getPreferredSize().height;
711
            }
712
        }
713

    
714
        toolBars.setPreferredSize(new Dimension(this.getWidth(),
715
            (numFilas * toolHeight)));
716

    
717
        toolBars.updateUI();
718
    }
719

    
720
    /**
721
     * DOCUMENT ME!
722
     * 
723
     * @param classesExtensions
724
     */
725
    public void setClassesExtensions(Map<Class<? extends IExtension>, ExtensionDecorator> classesExtensions) {
726
            Map<Class<? extends IExtension>, ExtensionDecorator> extensions = new HashMap<Class<? extends IExtension>, ExtensionDecorator>();
727
            for ( Entry<Class<? extends IExtension>, ExtensionDecorator>  entry: classesExtensions.entrySet()) {
728
                        if( ! (entry.getValue().getExtension() instanceof LibraryExtension) ) {
729
                                extensions.put(entry.getKey(), entry.getValue());
730
                        }
731
                }
732
        this.classesExtensions = extensions;
733
    }
734

    
735
    /**
736
     * Metodo de callback invocado cuando se selecciona un menu o un boton
737
     * de
738
     * la barra de herramientas. Se busca la extensi�n asociada y se ejecuta
739
     * 
740
     * @param e
741
     *            Evento producido
742
     */
743
    public void actionPerformed(ActionEvent e) {
744
        
745
//        org.gvsig.andami.plugins.IExtension ext =
746
//            (org.gvsig.andami.plugins.IExtension) classesExtensions
747
//                .get(controlClass.get(control));
748
            String actionName = "(unknow)";
749
        try {
750
            JComponent control = (JComponent) e.getSource();
751
            actionName = control.getName();
752
            
753
                ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
754
                ActionInfo action = actionManager.getAction(control.getName());
755
                Object args = null;
756
            if( control instanceof IControl ) {
757
                    args = ((IControl) control).getValue();
758
            }
759
            if( args == null ) {
760
                    action.execute();
761
            } else {
762
                    action.execute(args);                    
763
            }
764
                
765
//            logger.info("{}.execute('{}')", ext.getClass().getSimpleName(), actionCommand);
766
//            ext.execute(actionCommand);
767

    
768
                String actionCommand = e.getActionCommand();
769
            try {
770
                JToolBarToggleButton toggle = (JToolBarToggleButton) control;
771
                ToggleButtonModel model = (ToggleButtonModel) toggle.getModel();
772
                selectedTool.put(model.getGroupName(), actionCommand);
773
            } catch (ClassCastException ex) {
774
            } catch (NullPointerException ex) {
775
            }
776

    
777
        } catch (Throwable ex) {
778
                logger.error("Can't perform action '"+actionName+"'.",ex);
779
//            if (ext == null) {
780
//                logger.error(Messages
781
//                    .getString("No_extension_associated_with_this_event_")
782
//                    + e.getActionCommand());
783
//            }
784
//            NotificationManager.addError(Messages
785
//                .getString("MDIFrame.Error_no_capturado_por_el_usuario"), t);
786
        }
787

    
788
        enableControls();
789
        showMemory();
790
    }
791

    
792
    /**
793
     * DOCUMENT ME!
794
     * 
795
     * @param name
796
     *            DOCUMENT ME!
797
     * @param loader
798
     *            DOCUMENT ME!
799
     * 
800
     * @return DOCUMENT ME!
801
     */
802
    private String getName(String name, PluginClassLoader loader) {
803
        if (name.indexOf('.') == -1) {
804
            return loader.getPluginName() + "." + name;
805
        } else {
806
            return name;
807
        }
808
    }
809

    
810
    /**
811
     * DOCUMENT ME!
812
     * 
813
     * @param loader
814
     *            DOCUMENT ME!
815
     * @param menu
816
     *            DOCUMENT ME!
817
     * 
818
     * @throws RuntimeException
819
     *             DOCUMENT ME!
820
     */
821
    public void addPopupMenu(PluginClassLoader loader, PopupMenu menu) {
822
        if (!SwingUtilities.isEventDispatchThread()) {
823
            throw new RuntimeException("No Event Dispatch Thread");
824
        }
825

    
826
        String name = getName(menu.getName(), loader);
827

    
828
        // Se crea el control popupmenu
829
        JPopUpMenu popupMenu = (JPopUpMenu) popupMap.get(name);
830

    
831
        if (popupMenu == null) {
832
            popupMenu = new JPopUpMenu(menu.getName());
833
            popupMap.put(name, popupMenu);
834
        }
835

    
836
        Menu[] menues = menu.getMenu();
837
        for (int i = 0; i < menues.length; i++) {
838
            JMenuItem nuevoMenu = createJMenuItem(loader, menues[i]);
839
            popupMenu.add(nuevoMenu);
840
        }
841
    }
842

    
843
    /**
844
     * DOCUMENT ME!
845
     * 
846
     * @param loader
847
     * @param menu
848
     * 
849
     * @return
850
     * 
851
     * @throws RuntimeException
852
     *             DOCUMENT ME!
853
     */
854
    private JMenuItem createJMenuItem(PluginClassLoader loader, Menu menu) {
855
        JMenuItem nuevoMenu = null;
856

    
857
        PluginServices ps =
858
            PluginServices.getPluginServices(loader.getPluginName());
859
        
860
        String text = menu.getText();
861
        int n = text.lastIndexOf('/');
862
        if( n>=0  ) {
863
                text = text.substring(n + 1);
864
        }
865
        String translatedText = ps.getText(text);
866

    
867
        IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent();
868
        if (menu.getIcon() != null) {
869
                if( iconTheme.exists(menu.getIcon()) ) {
870
                        ImageIcon image = iconTheme.get(menu.getIcon());
871
                nuevoMenu = new JMenuItem(translatedText, image);
872
            } else {
873
                nuevoMenu = new JMenuItem(translatedText);
874
                logger.info("menu icon '"+ menu.getIcon()+ "' not exists.");
875
            }
876
        } else {
877
            nuevoMenu = new JMenuItem(translatedText);
878
        }
879
        nuevoMenu.setName(menu.getName());
880
//                if (menu.getMnemonic() != null) {
881
//                        if (menu.getMnemonic().length() != 1) {
882
//                                throw new RuntimeException(
883
//                                                "Mnemonic must be 1 character length");
884
//                        }
885
//                        nuevoMenu.setMnemonic(KeyMapping.getKey(menu.getMnemonic()
886
//                                        .charAt(0)));
887
//                }
888

    
889
        if (menu.getKey() != null) {
890
            nuevoMenu.setAccelerator(KeyMapping.getKeyStroke(menu.getKey()));
891
        }
892

    
893
        nuevoMenu.setActionCommand(menu.getActionCommand());
894

    
895
        if (menu.getTooltip() != null) {
896
            nuevoMenu.setToolTip(ps.getText(menu.getTooltip()));
897
        }
898

    
899
        if (menu.getEnableText() != null) {
900
            nuevoMenu.setEnableText(ps.getText(menu.getEnableText()));
901
        }
902

    
903
        nuevoMenu.setEnabled(true);
904
        nuevoMenu.setVisible(true);
905

    
906
        if( menu.getName() != null ) {
907
                    ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
908
                    final ActionInfo actionInfo = actionManager.getAction(menu.getName());
909
                    if( actionInfo != null ) {
910
                            nuevoMenu.addActionListener(actionInfo);
911
                    }
912
        }
913
        return nuevoMenu;
914
    }
915

    
916
    /**
917
     * Muestra u oculta el menu de nombre 'name'
918
     * 
919
     * @param name
920
     *            Nombre del menu que se quiere mostrar
921
     * @param x
922
     *            Evento de raton
923
     * @param y
924
     *            DOCUMENT ME!
925
     * @param c
926
     *            DOCUMENT ME!
927
     */
928
    private void showPopupMenu(String name, int x, int y, Component c) {
929
        JPopupMenu menu = (JPopupMenu) popupMap.get(name);
930

    
931
        if (menu != null) {
932
            menu.show(c, x, y);
933
        }
934
    }
935

    
936
    /**
937
     * DOCUMENT ME!
938
     * 
939
     * @param name
940
     *            DOCUMENT ME!
941
     * @param listener
942
     *            DOCUMENT ME!
943
     */
944
    public void removePopupMenuListener(String name, ActionListener listener) {
945
        JPopupMenu menu = (JPopupMenu) popupMap.get(name);
946

    
947
        if (menu != null) {
948
            Component[] jmenuitems = menu.getComponents();
949

    
950
            for (int i = 0; i < jmenuitems.length; i++) {
951
                if (jmenuitems[i] instanceof JMenuItem) {
952
                    ((JMenuItem) jmenuitems[i]).removeActionListener(listener);
953
                }
954
            }
955
        }
956
    }
957

    
958
    /**
959
     * DOCUMENT ME!
960
     * 
961
     * @param popupName
962
     * @param c
963
     *            DOCUMENT ME!
964
     * @param listener
965
     * @param loader
966
     */
967
    public void addPopupMenuListener(String popupName, Component c,
968
        ActionListener listener, PluginClassLoader loader) {
969
        final String name = getName(popupName, loader);
970

    
971
        JPopupMenu menu = (JPopupMenu) popupMap.get(name);
972

    
973
        if (menu != null) {
974
            Component[] jmenuitems = menu.getComponents();
975

    
976
            for (int i = 0; i < jmenuitems.length; i++) {
977
                if (jmenuitems[i] instanceof JMenuItem) {
978
                    ((JMenuItem) jmenuitems[i]).addActionListener(listener);
979
                }
980
            }
981
        }
982

    
983
        c.addMouseListener(new MouseAdapter() {
984

    
985
            @Override
986
            public void mousePressed(MouseEvent e) {
987
                if (e.isPopupTrigger()) {
988
                    showPopupMenu(name, e.getX(), e.getY(), e.getComponent());
989
                }
990
            }
991

    
992
            @Override
993
            public void mouseReleased(MouseEvent e) {
994
                if (e.isPopupTrigger()) {
995
                    showPopupMenu(name, e.getX(), e.getY(), e.getComponent());
996
                }
997
            }
998
        });
999
    }
1000

    
1001
    /**
1002
     * Loop on the controls to enable/disable and show/hide them, according to
1003
     * its associated extension
1004
     * 
1005
     * @throws RuntimeException
1006
     *             DOCUMENT ME!
1007
     */
1008
    public void enableControls() {
1009
        if (!SwingUtilities.isEventDispatchThread()) {
1010
                SwingUtilities.invokeLater(new Runnable() {
1011
                                public void run() {
1012
                                        enableControls();
1013
                                }
1014
                        });
1015
                        return;
1016
        }
1017

    
1018
        ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
1019
        ActionInfoStatusCache cache = actionManager.createActionStatusCache();
1020
        
1021
        Iterator e = classesExtensions.values().iterator();
1022

    
1023
//        HashMap estadoExtensiones = new HashMap();
1024
//        HashMap visibilidadExtensiones = new HashMap();
1025
//
1026
//        while (e.hasNext()) {
1027
//            ExtensionDecorator ext = (ExtensionDecorator) e.next();
1028
//
1029
//            try {
1030
//                if (estadoExtensiones.get(ext) == null) {
1031
//                    boolean b;
1032
//                    if (ext.getVisibility() == ExtensionDecorator.ALWAYS_VISIBLE) {
1033
//                        b = true;
1034
//                    } else
1035
//                        if (ext.getVisibility() == ExtensionDecorator.ALWAYS_INVISIBLE) {
1036
//                            b = false;
1037
//                        } else {
1038
//                            if (PluginServices.getExclusiveUIExtension() == null) {
1039
//                                b = ext.isVisible();
1040
//                            } else {
1041
//                                b =
1042
//                                    PluginServices.getExclusiveUIExtension()
1043
//                                        .isVisible(ext.getExtension());
1044
//                            }
1045
//                        }
1046
//                    Boolean visible = new Boolean(b);
1047
//                    Boolean enabled = new Boolean(false);
1048
//
1049
//                    if (visible.booleanValue()) {
1050
//                        if (PluginServices.getExclusiveUIExtension() == null) {
1051
//                            enabled = new Boolean(ext.isEnabled());
1052
//                        } else {
1053
//                            enabled =
1054
//                                new Boolean(PluginServices
1055
//                                    .getExclusiveUIExtension().isEnabled(
1056
//                                        ext.getExtension()));
1057
//                        }
1058
//
1059
//                    }
1060
//
1061
//                    estadoExtensiones.put(ext, enabled);
1062
//                    visibilidadExtensiones.put(ext, visible);
1063
//                }
1064
//            } catch (Throwable e1) {
1065
//                NotificationManager.addError(Messages
1066
//                    .getString("MDIFrame.Error_no_capturado_por_el_usuario"),
1067
//                    e1);
1068
//                estadoExtensiones.put(ext, Boolean.FALSE);
1069
//            }
1070
//        }
1071

    
1072
        // Enable or disable controls, according to its associated extensions
1073
        e = controlClass.keySet().iterator();
1074

    
1075
        while (e.hasNext()) {
1076
            JComponent control = (JComponent) e.next();
1077
            String actionlName = control.getName();
1078
            ActionInfo action = actionManager.getAction(actionlName);
1079
            try {
1080
                boolean enabled = false;
1081
                boolean visible = false;
1082
                
1083
                    if(action == null) {
1084
                  IExtension ext =
1085
                          (IExtension) classesExtensions.get(controlClass.get(control));
1086
                  enabled = ext.isEnabled();
1087
                  visible = ext.isVisible(); 
1088
                    } else {
1089
                            enabled = false;
1090
                    visible = cache.isVisible(action);
1091
                    if( visible ) {
1092
                            enabled = cache.isEnabled(action);
1093
                    }
1094
                    }
1095
                control.setEnabled(enabled);
1096
                control.setVisible(visible);
1097
            } catch (Exception ex) {
1098
                    logger.info("Can't enable/show control '"+actionlName +"'.",ex);
1099
                    this.message("Can't enable/show control '"+actionlName +"'.", JOptionPane.ERROR_MESSAGE);
1100
                control.setEnabled(false);
1101
                control.setVisible(false);
1102
            }
1103
        }
1104

    
1105
        // Loop in the menus to hide the menus that don't have visible children
1106
        for (int i = 0; i < menuBar.getMenuCount(); i++) {
1107
            MenuElement menu = menuBar.getMenu(i);
1108
            hideMenus(menu);
1109
            if (menu instanceof JMenu) {
1110
                // hide (ugly) redundant separators and assign keyboard
1111
                // mnemonics
1112
                Component[] comps = ((JMenu) menu).getMenuComponents();
1113
                // mnemonics have to be unique for each top-level menu
1114
                char mnemonics[] = new char[comps.length];
1115
                if (comps.length > 0) {
1116
                    // Set keyboard mnemonic for this top-level entry
1117
                    String text = ((JMenu) menu).getText();
1118
                    char mnemonic = getMnemonic(text, mnemonics);
1119
                    if (' ' != mnemonic) {
1120
                        ((JMenu) menu).setMnemonic(mnemonic);
1121
                        mnemonics[0] = mnemonic;
1122
                    }
1123
                }
1124
                // now go through all entries in this menu, hid
1125
                // separators if necessary and assing remaining mnemonics
1126
                hideSeparatorsAndMakeMnemonics(menu, mnemonics);
1127
            }
1128
        }
1129

    
1130
        // hide the toolbars that don't contain any visible tool
1131
        Iterator it = toolBarMap.values().iterator();
1132

    
1133
        while (it.hasNext()) {
1134
                JComponent t = (JComponent) it.next();
1135
            boolean todosOcultos = true;
1136

    
1137
            for (int i = 0; i < t.getComponentCount(); i++) {
1138
                if (!(t.getComponent(i) instanceof JSeparator) // separators
1139
                                                               // don't matter
1140
                    && t.getComponent(i).isVisible()) {
1141
                    todosOcultos = false;
1142
                }
1143
            }
1144

    
1145
            if (todosOcultos) {
1146
                t.setVisible(false);
1147
            } else {
1148
                    if(t instanceof SelectableToolBar) {
1149
                            t.setVisible(((SelectableToolBar)t).getAndamiVisibility());
1150
                    } else
1151
                            t.setVisible(true);
1152
            }
1153
        }
1154

    
1155
        if (mdiManager != null) {
1156
            JPanel f = (JPanel) mdiManager.getActiveWindow();
1157

    
1158
            if (f != null) {
1159
                if (lastLabelClass != f.getClass()) {
1160
                    lastLabelClass = f.getClass();
1161

    
1162
                    Label[] lbls = (Label[]) classLabels.get(lastLabelClass);
1163

    
1164
                    if (lbls != null) {
1165
                        bEstado.setLabelSet(lbls);
1166
                    }
1167
                }
1168
            }
1169
        }
1170

    
1171
        ajustarToolBar();
1172

    
1173
        showMemory();
1174
    }
1175
    
1176
    public void refreshControls() {
1177
        if (!SwingUtilities.isEventDispatchThread()) {
1178
                SwingUtilities.invokeLater(new Runnable() {
1179
                                public void run() {
1180
                                        refreshControls();
1181
                                }
1182
                        });
1183
                        return;
1184
        }
1185

    
1186
        ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
1187
        ActionInfoStatusCache cache = actionManager.createActionStatusCache();
1188
        IconTheme icontheme = ToolsSwingLocator.getIconThemeManager().getCurrent();
1189
        
1190
        Iterator e = controlClass.keySet().iterator();
1191

    
1192
        while (e.hasNext()) {
1193
            JComponent control = (JComponent) e.next();
1194
            String actionlName = control.getName();
1195
            ActionInfo action = actionManager.getAction(actionlName);
1196
            if( action!=null ) {
1197
                    if( control instanceof AbstractButton ) {
1198

    
1199
                    AbstractButton button = (AbstractButton) control;
1200

    
1201
                        if (control instanceof javax.swing.JMenuItem) {
1202
                            
1203
                            if (action.getIconName() != null && action.getIconName().length() > 0) {
1204
                                
1205
                                if( icontheme.exists(action.getIconName()) ) {
1206
                                    button.setIcon(action.getIcon());
1207
                                }
1208
                            }
1209
                            
1210
                        } else {
1211
                            button.setIcon(action.getIcon());
1212
                        }
1213
                    }
1214
            }
1215
        }
1216
        enableControls();
1217
    }
1218

    
1219
    public void message(String msg, int messageTyoe) {
1220
            this.getStatusBar().message(msg, messageTyoe);
1221
        }
1222

    
1223
        /**
1224
     * Establece la visibilidad de un menu y todos sus descendientes en la
1225
     * jerarquia teniendo en cuenta la visibilidad de todos los submenus.
1226
     * 
1227
     * @param menu
1228
     *            Menu que se quiere visualizar
1229
     * 
1230
     * @return Devuelve true si el menu es visible y false en caso contrario
1231
     */
1232
    private boolean hideMenus(MenuElement menu) {
1233
        MenuElement[] submenus = menu.getSubElements();
1234

    
1235
        // Si no tiene hijos se devuelve su visibilidad
1236
        if (submenus.length == 0) {
1237
            return menu.getComponent().isVisible();
1238
        }
1239

    
1240
        /*
1241
         * Si tiene hijos se devuelve true si alg�no de ellos es visible,
1242
         * pero se itera por todos ellos
1243
         */
1244
        boolean visible = false;
1245

    
1246
        for (int i = 0; i < submenus.length; i++) {
1247
            if (hideMenus(submenus[i])) {
1248
                if (!(menu instanceof JPopupMenu)) {
1249
                    menu.getComponent().setVisible(true);
1250
                }
1251

    
1252
                visible = true;
1253
            }
1254
        }
1255

    
1256
        if (visible) {
1257
            return true;
1258
        }
1259

    
1260
        menu.getComponent().setVisible(false);
1261

    
1262
        return false;
1263
    }
1264

    
1265
    /**
1266
     * 
1267
     * Recurse through all menu elements and make sure there are no
1268
     * redundant separators.
1269
     * This method will make sure that a separator only becomes visible
1270
     * if at least one visible non-separator menu entry preceeded it.
1271
     * 
1272
     **/
1273
    private void hideSeparatorsAndMakeMnemonics(MenuElement menu,
1274
        char[] mnemonics) {
1275
        // flag that indicates whether a separator is to be displayed or not
1276
        boolean allowSeparator;
1277

    
1278
        allowSeparator = false; // separator not allowed as very first menu item
1279
        Component[] comps = ((JMenu) menu).getMenuComponents();
1280
        if (comps.length < 1) {
1281
            // zero-length menu: skip
1282
            return;
1283
        }
1284

    
1285
        for (int i = 0; i < comps.length; i++) {
1286
            if (comps[i] instanceof JSeparator) {
1287
                // got a separator: display only if allowed at this position
1288
                if (allowSeparator == true) {
1289
                    // look at all successive menu entries to make sure that at
1290
                    // least one
1291
                    // is visible and not a separator (otherwise, this separator
1292
                    // would
1293
                    // be the last visible item in this menu) -- we don't want
1294
                    // that
1295
                    comps[i].setVisible(false);
1296
                    for (int j = i; j < comps.length; j++) {
1297
                        if (!(comps[j] instanceof JSeparator)) {
1298
                            if (comps[j].isVisible()) {
1299
                                comps[i].setVisible(true); // display separator!
1300
                                break;
1301
                            }
1302
                        }
1303
                    }
1304
                } else {
1305
                    comps[i].setVisible(false);
1306
                }
1307
                allowSeparator = false; // separator is not allowed right after
1308
                                        // another separator
1309
            } else {
1310
                if (comps[i] instanceof JMenu) { // got a submenu: recurse
1311
                                                 // through it
1312
                    // get number of submenu components
1313
                    Component[] scomps = ((JMenu) comps[i]).getMenuComponents();
1314
                    // make a new, fresh array to hold unique mnemonics for this
1315
                    // submenu
1316
                    char[] smnemonics = new char[scomps.length];
1317
                    hideSeparatorsAndMakeMnemonics(((MenuElement) comps[i]),
1318
                        smnemonics);
1319
                    if (comps[i].isVisible()) {
1320
                        allowSeparator = true; // separators are OK after
1321
                                               // visible submenus
1322
                        // Set keyboard mnemonic for this submenu
1323
                        String text = ((JMenu) comps[i]).getText();
1324
                        char mnemonic = getMnemonic(text, mnemonics);
1325
                        if (' ' != mnemonic) {
1326
                            ((JMenu) comps[i]).setMnemonic(mnemonic);
1327
                            mnemonics[i] = mnemonic;
1328
                        }
1329
                    }
1330
                } else {
1331
                    if (comps[i].isVisible()) {
1332
                        if (comps[i] instanceof JMenuItem) {
1333
                            // Set keyboard mnemonic for this menu item
1334
                            String text = ((JMenuItem) comps[i]).getText();
1335
                            char mnemonic = getMnemonic(text, mnemonics);
1336
                            if (' ' != mnemonic) {
1337
                                ((JMenuItem) comps[i]).setMnemonic(mnemonic);
1338
                                mnemonics[i] = mnemonic;
1339
                            }
1340
                        }
1341
                        allowSeparator = true; // separators are OK after
1342
                                               // regular, visible entries
1343
                    }
1344
                }
1345
            }
1346
        }
1347
    }
1348

    
1349
    /**
1350
     * Helper functios for assigning a unique mnemomic char from
1351
     * a pool of unassigned onces, stored in the array "mnemomnics"
1352
     */
1353
    private char getMnemonic(String text, char[] mnemonics) {
1354
        Vector words = new Vector();
1355
        StringTokenizer t = new StringTokenizer(text);
1356
        int maxsize = 0;
1357

    
1358
        while (t.hasMoreTokens()) {
1359
            String word = t.nextToken();
1360
            if (word.length() > maxsize) {
1361
                maxsize = word.length();
1362
            }
1363
            words.addElement(word);
1364
        }
1365
        words.trimToSize();
1366

    
1367
        for (int i = 0; i < maxsize; ++i) {
1368
            char mnemonic = getMnemonic(words, mnemonics, i);
1369
            if (' ' != mnemonic) {
1370
                return mnemonic;
1371
            }
1372
        }
1373

    
1374
        return ' ';
1375
    }
1376

    
1377
    private char getMnemonic(Vector words, char[] mnemonics, int index) {
1378
        int numwords = words.size();
1379

    
1380
        for (int i = 0; i < numwords; ++i) {
1381
            String word = (String) words.elementAt(i);
1382
            if (index >= word.length()) {
1383
                continue;
1384
            }
1385

    
1386
            char c = word.charAt(index);
1387
            if (!isMnemonicExists(c, mnemonics)) {
1388
                /* pick only valid chars */
1389
                if ((c != ':') && (c != '.') && (c != ',') && (c != ';')
1390
                    && (c != '-') && (c != '+') && (c != '/') && (c != '\\')
1391
                    && (c != '\'') && (c != '\"') && (c != ' ') && (c != '=')
1392
                    && (c != '(') && (c != ')') && (c != '[') && (c != ']')
1393
                    && (c != '{') && (c != '}') && (c != '$') && (c != '*')
1394
                    && (c != '&') && (c != '%') && (c != '!') && (c != '?')
1395
                    && (c != '#') && (c != '~') && (c != '_')) {
1396
                    return c;
1397
                }
1398
            }
1399
        }
1400
        return ' ';
1401
    }
1402

    
1403
    private boolean isMnemonicExists(char c, char[] mnemonics) {
1404
        int num = mnemonics.length;
1405
        for (int i = 0; i < num; ++i) {
1406
            if (mnemonics[i] == c) {
1407
                return true;
1408
            }
1409
        }
1410
        return false;
1411
    }
1412

    
1413
    /**
1414
     * Muestra la memoria consumida por el programa
1415
     */
1416
    private void showMemory() {
1417
        Runtime r = Runtime.getRuntime();
1418
        long mem = r.totalMemory() - r.freeMemory();
1419
        logger.debug(PluginServices.getText(this, "memory_usage") + " " + mem
1420
            / 1024 + " KB");
1421
    }
1422

    
1423
    /**
1424
     * DOCUMENT ME!
1425
     * 
1426
     * @return
1427
     */
1428
    public MDIManager getMDIManager() {
1429
        return mdiManager;
1430
    }
1431

    
1432
    /**
1433
     * Establece el mensaje en la barra de estado asociado a una etiqueta
1434
     * 
1435
     * @return DOCUMENT ME!
1436
     */
1437
    public NewStatusBar getStatusBar() {
1438
        return bEstado;
1439
    }
1440

    
1441
    /**
1442
     * You can use this function to select the appropiate
1443
     * tool inside the toolbars
1444
     */
1445
    public void setSelectedTool(String actionCommand) {
1446
        setSelectedTool(defaultGroup, actionCommand);
1447
    }
1448

    
1449
    /**
1450
     * You can use this function to select the appropiate
1451
     * tool inside the toolbars
1452
     */
1453
    public void setSelectedTool(String groupName, String actionCommand) {
1454
        ButtonGroup group = (ButtonGroup) buttonGroupMap.get(groupName);
1455
        if (group == null) {
1456
            return;
1457
        }
1458

    
1459
        Enumeration enumeration = group.getElements();
1460
        while (enumeration.hasMoreElements()) {
1461
            AbstractButton button = (AbstractButton) enumeration.nextElement();
1462
            if (button.getActionCommand().equals(actionCommand)) {
1463
                button.setSelected(true);
1464
            }
1465
        }
1466

    
1467
        selectedTool.put(groupName, actionCommand);
1468
    }
1469

    
1470
    /**
1471
     * You can use this function to select the appropiate
1472
     * tool inside the toolbars
1473
     */
1474
    public void setSelectedTools(Map selectedTools) {
1475
        selectedTool = selectedTools;
1476
        if (selectedTools == null) {
1477
            return;
1478
        }
1479
        Iterator groupNames = selectedTools.keySet().iterator();
1480
        while (groupNames.hasNext()) {
1481
            try {
1482
                String groupName = (String) groupNames.next();
1483
                ButtonGroup group = (ButtonGroup) buttonGroupMap.get(groupName);
1484
                Enumeration enumeration = group.getElements();
1485
                String actionCommand = (String) selectedTools.get(groupName);
1486
                if (actionCommand == null) {
1487
                    continue;
1488
                }
1489
                while (enumeration.hasMoreElements()) {
1490
                    AbstractButton button =
1491
                        (AbstractButton) enumeration.nextElement();
1492
                    if (button.getActionCommand().equals(actionCommand)) {
1493
                        button.setSelected(true);
1494
                    }
1495
                }
1496
            } catch (ClassCastException ex) {
1497
                logger
1498
                    .error("selectedTool should only contain pairs (String groupName, JToolBarToggleButton button)");
1499
            }
1500
        }
1501
    }
1502

    
1503
    /**
1504
     * DOCUMENT ME!
1505
     * 
1506
     * @param clase
1507
     * @param label
1508
     */
1509
    public void setStatusBarLabels(Class<?> clase, Label[] label) {
1510
        classLabels.put(clase, label);
1511
    }
1512

    
1513
    public void removeStatusBarLabels(Class<?> clase) {
1514
        classLabels.remove(clase);
1515
    }
1516

    
1517
    public void addStatusBarControl(Class<?> extensionClass, IControl control) {
1518
        control.addActionListener(this);
1519
        bEstado.addControl(control.getName(), (Component) control);
1520
        controlClass.put(control, extensionClass);
1521
    }
1522
    
1523
    public void addToolBarControl(Class<?> extensionClass, JToolBar control, String name) {
1524
        //toolBarMap.put(name, control); 
1525
        toolBars.add(control);
1526
        controlClass.put(control, extensionClass);
1527
    }
1528

    
1529
    public void removeStatusBarControl(String name) {
1530
        Component c = bEstado.removeControl(name);
1531
        if (c != null) {
1532
            controlClass.remove(c);
1533
        }
1534
    }
1535

    
1536
    /**
1537
     * @see org.gvsig.andami.ui.mdiFrame.MainFrame#removeMenu(org.gvsig.andami.plugins.config.generate.Menu)
1538
     */
1539
    public void removeMenu(Menu menu) {
1540
        JMenuItem delete = (JMenuItem) infoCodedMenus.get(menu);
1541

    
1542
        if (delete == null) {
1543
            throw new NoSuchElementException(menu.getText());
1544
        }
1545

    
1546
        delete.getParent().remove(delete);
1547
        infoCodedMenus.remove(menu);
1548
    }
1549

    
1550
    /**
1551
     * @see org.gvsig.andami.ui.mdiFrame.MainFrame#addMenu(org.gvsig.andami.plugins.config.generate.Menu,
1552
     *      java.awt.event.ActionListener, PluginClassLoader)
1553
     */
1554
    public void addMenu(Menu menu, ActionListener listener,
1555
        PluginClassLoader loader) {
1556
        JMenu menuPadre = createMenuAncestors(menu, loader);
1557

    
1558
        // Se registra y a�ade el menu
1559
        JMenuItem nuevoMenu = createJMenuItem(loader, menu);
1560
        nuevoMenu.addMouseListener(tooltipListener);
1561
        if( listener != null && menu.getName() != null && menu.getName().trim().length()>0 ) {
1562
                    ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
1563
                    final ActionInfo actionInfo = actionManager.getAction(menu.getName());
1564
                    if( actionInfo != null ) {
1565
                            nuevoMenu.removeActionListener(actionInfo);
1566
                    }
1567
                nuevoMenu.addActionListener(listener);
1568
        }
1569
        menuPadre.add(nuevoMenu);
1570

    
1571
        infoCodedMenus.put(menu, nuevoMenu);
1572
    }
1573

    
1574
    /**
1575
     * @see org.gvsig.andami.ui.mdiFrame.MainFrame#changeMenuName(java.lang.String[],
1576
     *      String, org.gvsig.andami.plugins.PluginClassLoader)
1577
     */
1578
    public void changeMenuName(String[] menu, String newName,
1579
        PluginClassLoader loader) {
1580

    
1581
        ArrayList menuList = new ArrayList();
1582
        for (int i = 0; i < menu.length; i++) {
1583
            menuList.add(menu[i]);
1584
        }
1585

    
1586
        javax.swing.JMenuItem newMenu = getMenu(menuList, menuBar);
1587
        if (newMenu == null) {
1588
            throw new NoSuchMenuException(menu[0]);
1589
        } else {
1590
            newMenu.setText(PluginServices.getText(this, newName));
1591
        }
1592
    }
1593

    
1594
    /**
1595
     * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
1596
     */
1597
    public void componentHidden(ComponentEvent arg0) {
1598
    }
1599

    
1600
    /**
1601
     * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
1602
     */
1603
    public void componentMoved(ComponentEvent arg0) {
1604
    }
1605

    
1606
    /**
1607
     * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
1608
     */
1609
    public void componentResized(ComponentEvent arg0) {
1610
        ajustarToolBar();
1611
    }
1612

    
1613
    /**
1614
     * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
1615
     */
1616
    public void componentShown(ComponentEvent arg0) {
1617
    }
1618

    
1619
    /**
1620
     * @see java.awt.event.ContainerListener#componentAdded(java.awt.event.ContainerEvent)
1621
     */
1622
    public void componentAdded(ContainerEvent arg0) {
1623
        ajustarToolBar();
1624
    }
1625

    
1626
    /**
1627
     * @see java.awt.event.ContainerListener#componentRemoved(java.awt.event.ContainerEvent)
1628
     */
1629
    public void componentRemoved(ContainerEvent arg0) {
1630
        ajustarToolBar();
1631
    }
1632

    
1633
    /**
1634
     * DOCUMENT ME!
1635
     * 
1636
     * @author $author$
1637
     * @version $Revision: 39103 $
1638
     */
1639
    public class TooltipListener extends MouseAdapter {
1640

    
1641
        /**
1642
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
1643
         */
1644
        @Override
1645
        public void mouseEntered(MouseEvent e) {
1646
            JComponent control = (JComponent) e.getSource();
1647
            EnableTextSupport ets = (EnableTextSupport) e.getSource();
1648

    
1649
            String texto = null;
1650
            texto = control.getToolTipText();
1651

    
1652
            if (texto != null) {
1653
                bEstado.setInfoTextTemporal(texto);
1654
            }
1655
        }
1656

    
1657
        /**
1658
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
1659
         */
1660
        @Override
1661
        public void mouseExited(MouseEvent arg0) {
1662
            bEstado.restaurarTexto();
1663
        }
1664

    
1665
        /**
1666
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
1667
         */
1668
        @Override
1669
        public void mousePressed(MouseEvent e) {
1670
            bEstado.restaurarTexto();
1671
        }
1672
    }
1673

    
1674
    public String getTitlePrefix() {
1675
        return titlePrefix;
1676
    }
1677

    
1678
    public void setTitlePrefix(String titlePrefix) {
1679
        this.titlePrefix = titlePrefix;
1680
    }
1681

    
1682
    public Map getSelectedTools() {
1683
        return selectedTool;
1684
    }
1685

    
1686
    public HashMap getInitialSelectedTools() {
1687
        return initialSelectedTools;
1688
    }
1689

    
1690
    /**
1691
     * Get a previously added JComponent by name. For example
1692
     * you can use it if you need to obtain a JToolBar to
1693
     * add some customized component.
1694
     * 
1695
     * @param name
1696
     * @return the JComponent or null if none has been found
1697
     */
1698
    public JComponent getComponentByName(String name) {
1699
        Iterator e = controlClass.keySet().iterator();
1700

    
1701
        while (e.hasNext()) {
1702
            JComponent control = (JComponent) e.next();
1703
            String nameCtrl = control.getName();
1704
            if (nameCtrl != null) {
1705
                if (nameCtrl.compareTo(name) == 0) {
1706
                    return control;
1707
                }
1708
            }
1709
        }
1710
        Iterator it = toolBarMap.values().iterator();
1711
        while (it.hasNext()) {
1712
                JComponent t = (JComponent) it.next();
1713
            String nameCtrl = t.getName();
1714
            if (nameCtrl != null) {
1715
                if (nameCtrl.compareTo(name) == 0) {
1716
                    return t;
1717
                }
1718
            }
1719

    
1720
        }
1721

    
1722
        return null;
1723
    }
1724

    
1725
    public SelectableToolBar[] getToolbars() {
1726
        return (SelectableToolBar[]) toolBarMap.values().toArray(
1727
            new SelectableToolBar[0]);
1728
    }
1729

    
1730
    public boolean getToolbarVisibility(String name) {
1731
        JComponent component =
1732
            PluginServices.getMainFrame().getComponentByName(name);
1733
        if ((component != null) && (component instanceof SelectableToolBar)) {
1734
            SelectableToolBar toolBar = (SelectableToolBar) component;
1735
            return toolBar.getAndamiVisibility();
1736
        }
1737
        return false;
1738
    }
1739

    
1740
    public boolean setToolbarVisibility(String name, boolean visibility) {
1741
        JComponent component =
1742
            PluginServices.getMainFrame().getComponentByName(name);
1743
        if ((component != null) && (component instanceof SelectableToolBar)) {
1744
            SelectableToolBar toolBar = (SelectableToolBar) component;
1745
            boolean oldVisibility = toolBar.getAndamiVisibility();
1746
            toolBar.setAndamiVisibility(visibility);
1747
            enableControls();
1748
            return oldVisibility;
1749
        }
1750
        return false;
1751
    }
1752

    
1753
    public javax.swing.JMenuItem getMenuEntry(String[] menuPath) {
1754
        ArrayList menu = new ArrayList();
1755
        for (int i = 0; i < menuPath.length; i++) {
1756
            menu.add(menuPath[i]);
1757
        }
1758
        return getMenu(menu, menuBar);
1759
    }
1760

    
1761
        public void messageDialog(String message, String title, int messageType) {
1762
                DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado);
1763
                helper.messageDialog(message, title, messageType);
1764
        }
1765

    
1766
        public void messageDialog(String message, String[] messageArgs,
1767
                        String title, int messageType) {
1768
                DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado);
1769
                helper.messageDialog(message, messageArgs, title, messageType);
1770
        }
1771

    
1772
        public int confirmDialog(String message, String title, int optionType,
1773
                        int messageType) {
1774
                DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado);
1775
                return helper.confirmDialog(message, title, optionType, messageType);
1776
        }
1777

    
1778
        public String inputDialog(String message, String title, int messageType,
1779
                        String initialValue) {
1780
                DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado);
1781
                return helper.inputDialog(message, title, messageType, initialValue);
1782
        }
1783

    
1784
        public String inputDialog(String message, String title) {
1785
                DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado);
1786
                return helper.inputDialog(message, title);
1787
        }
1788

    
1789
        public void showDialog(Component contents, String title) {
1790
                DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado);
1791
                helper.showDialog(contents, title);
1792
        }
1793

    
1794
        public Component createComponent(Class<? extends Component> theClass,
1795
                        Object... parameters) {
1796
                DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado);
1797
                return helper.createComponentWithParams(theClass, parameters);
1798
        }
1799

    
1800
        public Component createComponentWithParams(
1801
                        Class<? extends Component> theClass, Object[] parameters) {
1802
                DefaultThreadSafeDialogs helper = new DefaultThreadSafeDialogs(this, this.bEstado);
1803
                return helper.createComponentWithParams(theClass, parameters);
1804
        }
1805

    
1806
}