root / trunk / frameworks / _fwAndami / src / com / iver / andami / ui / mdiFrame / MDIFrame.java @ 1340
History | View | Annotate | Download (28.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 com.iver.andami.ui.mdiFrame; |
42 |
|
43 |
import com.iver.andami.Launcher; |
44 |
import com.iver.andami.PluginServices; |
45 |
import com.iver.andami.messages.Messages; |
46 |
import com.iver.andami.messages.NotificationManager; |
47 |
import com.iver.andami.plugins.PluginClassLoader; |
48 |
import com.iver.andami.plugins.config.generate.ActionTool; |
49 |
import com.iver.andami.plugins.config.generate.Label; |
50 |
import com.iver.andami.plugins.config.generate.Menu; |
51 |
import com.iver.andami.plugins.config.generate.PopupMenu; |
52 |
import com.iver.andami.plugins.config.generate.SelectableTool; |
53 |
import com.iver.andami.plugins.config.generate.SkinExtensionType; |
54 |
import com.iver.andami.plugins.config.generate.ToolBar; |
55 |
import com.iver.andami.ui.mdiManager.MDIManager; |
56 |
import com.iver.andami.ui.mdiManager.MDIManagerFactory; |
57 |
|
58 |
import org.apache.log4j.Logger; |
59 |
|
60 |
import java.awt.BorderLayout; |
61 |
import java.awt.Component; |
62 |
import java.awt.Cursor; |
63 |
import java.awt.Dimension; |
64 |
import java.awt.FlowLayout; |
65 |
import java.awt.Insets; |
66 |
import java.awt.event.ActionEvent; |
67 |
import java.awt.event.ActionListener; |
68 |
import java.awt.event.ComponentEvent; |
69 |
import java.awt.event.ComponentListener; |
70 |
import java.awt.event.ContainerEvent; |
71 |
import java.awt.event.ContainerListener; |
72 |
import java.awt.event.MouseAdapter; |
73 |
import java.awt.event.MouseEvent; |
74 |
import java.awt.event.WindowAdapter; |
75 |
import java.awt.event.WindowEvent; |
76 |
|
77 |
import java.net.URL; |
78 |
|
79 |
import java.util.ArrayList; |
80 |
import java.util.HashMap; |
81 |
import java.util.Iterator; |
82 |
import java.util.NoSuchElementException; |
83 |
import java.util.Stack; |
84 |
import java.util.Vector; |
85 |
|
86 |
import javax.swing.AbstractButton; |
87 |
import javax.swing.ImageIcon; |
88 |
import javax.swing.JComponent; |
89 |
import javax.swing.JFrame; |
90 |
import javax.swing.JMenu; |
91 |
import javax.swing.JMenuBar; |
92 |
import javax.swing.JOptionPane; |
93 |
import javax.swing.JPanel; |
94 |
import javax.swing.JPopupMenu; |
95 |
import javax.swing.KeyStroke; |
96 |
import javax.swing.MenuElement; |
97 |
import javax.swing.SwingUtilities; |
98 |
import javax.swing.Timer; |
99 |
|
100 |
|
101 |
/**
|
102 |
* Frame principal de la aplicaci?n.
|
103 |
*
|
104 |
* @version $Revision: 1340 $
|
105 |
*/
|
106 |
public class MDIFrame extends JFrame implements ComponentListener, |
107 |
ContainerListener, ActionListener, MainFrame { |
108 |
/** DOCUMENT ME! */
|
109 |
private static Logger logger = Logger.getLogger(MDIFrame.class.getName()); |
110 |
private MDIManager mdiManager = MDIManagerFactory.createManager();
|
111 |
|
112 |
/** Elementos de la aplicaci?n */
|
113 |
private JMenuBar menuBar = new JMenuBar(); |
114 |
|
115 |
/** DOCUMENT ME! */
|
116 |
private JPanel toolBars = new JPanel(); |
117 |
|
118 |
/** DOCUMENT ME! */
|
119 |
private NewStatusBar bEstado = null; |
120 |
|
121 |
/** Asocia los nombres con las barras de herramientas */
|
122 |
private HashMap toolBarMap = new HashMap(); |
123 |
|
124 |
/** Asocia los nombres con los popupMenus */
|
125 |
private HashMap popupMap = new HashMap(); |
126 |
|
127 |
/** Asocia controles con la clase de la extension asociada */
|
128 |
private HashMap controlClass = new HashMap(); |
129 |
|
130 |
/**
|
131 |
* Asocia la informaci?n sobre las etiquetas que van en la status bar con
|
132 |
* cada extension
|
133 |
*/
|
134 |
private HashMap classLabels = new HashMap(); |
135 |
|
136 |
/** ProgressListeners (ver interfaz com.iver.mdiApp.ui.ProgressListener) */
|
137 |
private ArrayList progressListeners = new ArrayList(); |
138 |
|
139 |
/** Timer para invocar los enventos de la interfaz anterior */
|
140 |
private Timer progressTimer = null; |
141 |
|
142 |
/** Tabla hash que asocia las clases con las extensiones */
|
143 |
private HashMap classesExtensions = new HashMap(); |
144 |
|
145 |
/** ?ltima clase que activ? etiquetas */
|
146 |
private Class lastLabelClass; |
147 |
|
148 |
/** Instancia que pone los tooltip en la barra de estado */
|
149 |
private TooltipListener tooltipListener = new TooltipListener(); |
150 |
private HashMap infoCodedMenus = new HashMap(); |
151 |
|
152 |
/**
|
153 |
* Realiza tareas para poner en marcha la aplicaci?n
|
154 |
*
|
155 |
* @throws RuntimeException DOCUMENT ME!
|
156 |
*/
|
157 |
public void init() { |
158 |
if (!SwingUtilities.isEventDispatchThread()) { |
159 |
throw new RuntimeException("Not Event Dispatch Thread"); |
160 |
} |
161 |
|
162 |
//Se a?aden los listeners del JFrame
|
163 |
this.addWindowListener(new WindowAdapter() { |
164 |
public void windowClosing(WindowEvent e) { |
165 |
/*JOptionPane dlgSalir = new JOptionPane(Utilities.getMessage(this,"quiere_salir"),
|
166 |
JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
|
167 |
*/
|
168 |
int option = JOptionPane.showConfirmDialog(MDIFrame.this, |
169 |
Messages.getString("MDIFrame.quiere_salir"),
|
170 |
Messages.getString("MDIFrame.salir"),
|
171 |
JOptionPane.YES_NO_OPTION);
|
172 |
|
173 |
if (option == JOptionPane.YES_OPTION) { |
174 |
Launcher.closeApplication(); |
175 |
} |
176 |
} |
177 |
}); |
178 |
this.addComponentListener(this); |
179 |
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
180 |
|
181 |
//Se configura la barra de menu
|
182 |
setJMenuBar(menuBar); |
183 |
|
184 |
//Se configura el layout del JFrame principal
|
185 |
this.getContentPane().setLayout(new BorderLayout()); |
186 |
|
187 |
/*
|
188 |
* Se configura y se a?ade el JPanel de las barras de
|
189 |
* herramientas
|
190 |
*/
|
191 |
FlowLayout layout = new FlowLayout(FlowLayout.LEFT); |
192 |
layout.setHgap(0);
|
193 |
layout.setVgap(0);
|
194 |
toolBars.setLayout(layout); |
195 |
getContentPane().add(toolBars, BorderLayout.PAGE_START);
|
196 |
|
197 |
// Se a?ade la barra de estado a la aplicaci?n
|
198 |
bEstado = new NewStatusBar();
|
199 |
getContentPane().add(bEstado, BorderLayout.SOUTH);
|
200 |
|
201 |
this.toolBars.addContainerListener(this); |
202 |
|
203 |
pack(); |
204 |
|
205 |
this.setSize(500, 500); |
206 |
this.setExtendedState(MAXIMIZED_BOTH);
|
207 |
|
208 |
mdiManager.init(this);
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* A?ade un modo de operaci?n a la caja de herramientas
|
213 |
*
|
214 |
* @param ext Texto del boton, si es null no aparece texto
|
215 |
* @param ext Icono del boton, si es null no aparece icono
|
216 |
* @param ext Extensi?n asociada al control
|
217 |
* @param selectableTool Enable text del control
|
218 |
*
|
219 |
* @throws ClassNotFoundException
|
220 |
* @throws RuntimeException DOCUMENT ME!
|
221 |
*/
|
222 |
public void addTool(PluginClassLoader loader, SkinExtensionType ext, |
223 |
ToolBar toolBar, SelectableTool selectableTool) |
224 |
throws ClassNotFoundException { |
225 |
if (!SwingUtilities.isEventDispatchThread()) { |
226 |
throw new RuntimeException("No Event Dispatch Thread"); |
227 |
} |
228 |
|
229 |
// Para traducir
|
230 |
PluginServices ps = PluginServices.getPluginServices(loader.getPluginName()); |
231 |
|
232 |
JToggleButton btn;
|
233 |
URL image = loader.getResource(selectableTool.getIcon());
|
234 |
|
235 |
if (image != null) { |
236 |
btn = new JToggleButton(selectableTool.getText(), |
237 |
new ImageIcon(image)); |
238 |
} else {
|
239 |
btn = new JToggleButton(selectableTool.getText()); |
240 |
} |
241 |
|
242 |
btn.setMargin(new Insets(0, 0, 0, 0)); |
243 |
btn.addMouseListener(tooltipListener); |
244 |
btn.addActionListener(this);
|
245 |
btn.setFocusable(false);
|
246 |
btn.setActionCommand(selectableTool.getActionCommand()); |
247 |
btn.setToolTipText(selectableTool.getTooltip()); |
248 |
btn.setEnabled(false);
|
249 |
btn.setVisible(false);
|
250 |
|
251 |
if (selectableTool.getIsDefault()) {
|
252 |
btn.setSelected(true);
|
253 |
} |
254 |
|
255 |
String name = getName(toolBar.getName(), loader);
|
256 |
|
257 |
SelectableToolBar jtb = (SelectableToolBar) toolBarMap.get(name); |
258 |
|
259 |
if (jtb == null) { |
260 |
jtb = new SelectableToolBar(toolBar.getName());
|
261 |
jtb.setRollover(true);
|
262 |
toolBarMap.put(name, jtb); |
263 |
toolBars.add(jtb); |
264 |
} |
265 |
|
266 |
jtb.addButton(selectableTool.getGroup(), btn); |
267 |
|
268 |
controlClass.put(btn, loader.loadClass(ext.getClassName())); |
269 |
|
270 |
if (selectableTool.getTooltip() != null) { |
271 |
btn.setToolTip(ps.getText(selectableTool.getTooltip())); |
272 |
} |
273 |
|
274 |
if (selectableTool.getEnableText() != null) { |
275 |
btn.setEnableText(ps.getText(selectableTool.getEnableText())); |
276 |
} |
277 |
|
278 |
if (selectableTool.getLast() == true) { |
279 |
jtb.addSeparator(); |
280 |
} |
281 |
} |
282 |
|
283 |
/**
|
284 |
* A?ade un bot?n a la barra de herramientas
|
285 |
*
|
286 |
* @param ext Texto del boton, si es null no aparece texto
|
287 |
* @param ext Extensi?n asociada al control
|
288 |
* @param toolBar Icono del boton, si es null no aparece texto
|
289 |
* @param actionTool Tooltip de la barra de herramientas
|
290 |
*
|
291 |
* @throws ClassNotFoundException
|
292 |
* @throws RuntimeException DOCUMENT ME!
|
293 |
*/
|
294 |
public void addTool(PluginClassLoader loader, SkinExtensionType ext, |
295 |
ToolBar toolBar, ActionTool actionTool) throws ClassNotFoundException { |
296 |
if (!SwingUtilities.isEventDispatchThread()) { |
297 |
throw new RuntimeException("No Event Dispatch Thread"); |
298 |
} |
299 |
|
300 |
// Para traducir los textos que vengan
|
301 |
PluginServices ps = PluginServices.getPluginServices(loader.getPluginName()); |
302 |
|
303 |
JButton btn;
|
304 |
URL image = loader.getResource(actionTool.getIcon());
|
305 |
|
306 |
if (image != null) { |
307 |
btn = new JButton(actionTool.getText(), new ImageIcon(image)); |
308 |
} else {
|
309 |
btn = new JButton(actionTool.getText()); |
310 |
} |
311 |
|
312 |
btn.setMargin(new Insets(0, 0, 0, 0)); |
313 |
btn.addMouseListener(tooltipListener); |
314 |
btn.addActionListener(this);
|
315 |
btn.setFocusable(false);
|
316 |
btn.setActionCommand(actionTool.getActionCommand()); |
317 |
btn.setEnabled(false);
|
318 |
btn.setVisible(false);
|
319 |
|
320 |
String name = getName(toolBar.getName(), loader);
|
321 |
|
322 |
SelectableToolBar jtb = (SelectableToolBar) toolBarMap.get(name); |
323 |
|
324 |
if (jtb == null) { |
325 |
jtb = new SelectableToolBar(toolBar.getName());
|
326 |
jtb.setRollover(true);
|
327 |
toolBarMap.put(name, jtb); |
328 |
toolBars.add(jtb); |
329 |
} |
330 |
|
331 |
jtb.add(btn); |
332 |
|
333 |
controlClass.put(btn, loader.loadClass(ext.getClassName())); |
334 |
|
335 |
if (actionTool.getTooltip() != null) { |
336 |
btn.setToolTip(ps.getText(actionTool.getTooltip())); |
337 |
} |
338 |
|
339 |
if (actionTool.getEnableText() != null) { |
340 |
btn.setEnableText(ps.getText(actionTool.getEnableText())); |
341 |
} |
342 |
|
343 |
if (actionTool.getLast() == true) { |
344 |
jtb.addSeparator(); |
345 |
} |
346 |
} |
347 |
|
348 |
/**
|
349 |
* Crea la estructura de men?s necesaria para a?adir el menu a la barra.
|
350 |
* Devuelve el padre del cual deve colgar el menu que se pasa como
|
351 |
* par?metro
|
352 |
*
|
353 |
* @param menu Menu cuya soporte se quiere a?adir
|
354 |
* @param loader DOCUMENT ME!
|
355 |
*
|
356 |
* @return Padre del men? que se pasa como par?metro
|
357 |
*/
|
358 |
private JMenu createMenuAncestors(Menu menu, PluginClassLoader loader) { |
359 |
JMenu menuPadre = null; |
360 |
|
361 |
String[] menues = menu.getText().split("/"); |
362 |
PluginServices ps = PluginServices.getPluginServices(loader.getPluginName()); |
363 |
|
364 |
for (int i = 0; i < menues.length; i++) { |
365 |
menues[i] = ps.getText(menues[i]); |
366 |
} |
367 |
|
368 |
//Se busca el padre en la menuBar
|
369 |
for (int i = 0; i < menuBar.getMenuCount(); i++) { |
370 |
if (menuBar.getMenu(i).getText().compareToIgnoreCase(menues[0]) == 0) { |
371 |
menuPadre = (JMenu) menuBar.getMenu(i);
|
372 |
} |
373 |
} |
374 |
|
375 |
//Si no se encuentra el padre se crea
|
376 |
if (menuPadre == null) { |
377 |
menuPadre = new JMenu(menues[0]); |
378 |
menuBar.add(menuPadre); |
379 |
} |
380 |
|
381 |
//Se crea el resto de menus
|
382 |
Vector temp = new Vector(); |
383 |
|
384 |
for (int i = 1; i < (menues.length - 1); i++) { |
385 |
temp.add(menues[i]); |
386 |
} |
387 |
|
388 |
menuPadre = createMenus(temp, menuPadre); |
389 |
|
390 |
return menuPadre;
|
391 |
} |
392 |
|
393 |
/**
|
394 |
* A?ade la informaci?n del menu al framework. Debido a que los men?es se
|
395 |
* pueden introducir en un orden determinado por el usuario, pero los
|
396 |
* plugins se instalan en un orden arbitrario, primero se almacena la
|
397 |
* informaci?n de todos los menus para luego ordenarlos y posteriormente
|
398 |
* a?adirlos al interfaz
|
399 |
*
|
400 |
* @param loader Posicion del menu. Se ordena por este campo
|
401 |
* @param ext Array con los nombres de los padres del menu
|
402 |
* @param menu Texto del menu
|
403 |
*
|
404 |
* @throws ClassNotFoundException
|
405 |
* @throws RuntimeException DOCUMENT ME!
|
406 |
*/
|
407 |
public void addMenu(PluginClassLoader loader, SkinExtensionType ext, |
408 |
Menu menu) throws ClassNotFoundException { |
409 |
if (!SwingUtilities.isEventDispatchThread()) { |
410 |
throw new RuntimeException("No Event Dispatch Thread"); |
411 |
} |
412 |
|
413 |
JMenu menuPadre = createMenuAncestors(menu, loader);
|
414 |
|
415 |
//Se registra y a?ade el menu
|
416 |
JMenuItem nuevoMenu = createJMenuItem(loader, menu);
|
417 |
nuevoMenu.addMouseListener(tooltipListener); |
418 |
nuevoMenu.addActionListener(this);
|
419 |
menuPadre.add(nuevoMenu); |
420 |
|
421 |
controlClass.put(nuevoMenu, loader.loadClass(ext.getClassName())); |
422 |
} |
423 |
|
424 |
/**
|
425 |
* Dado un array de nombres de menu traducidos, encuentra el men?
|
426 |
*
|
427 |
* @param nombres DOCUMENT ME!
|
428 |
* @param padre DOCUMENT ME!
|
429 |
*
|
430 |
* @return DOCUMENT ME!
|
431 |
*/
|
432 |
private javax.swing.JMenuItem getMenu(Vector nombres, JMenu padre) { |
433 |
javax.swing.JMenuItem buscado = null;
|
434 |
|
435 |
for (int i = 0; i < padre.getMenuComponentCount(); i++) { |
436 |
javax.swing.JMenuItem hijo = (javax.swing.JMenuItem) padre.getMenuComponent(i); |
437 |
|
438 |
if (hijo.getText().compareToIgnoreCase((String) nombres.get(0)) == 0) { |
439 |
buscado = hijo; |
440 |
} |
441 |
} |
442 |
|
443 |
//Si no se encuentra el menu
|
444 |
if (buscado == null) { |
445 |
return null; |
446 |
} |
447 |
|
448 |
nombres.remove(0);
|
449 |
|
450 |
//Si se ha llegado al fin de la ruta completa
|
451 |
if (nombres.isEmpty()) {
|
452 |
return buscado;
|
453 |
} else {
|
454 |
if (buscado instanceof JMenu) { |
455 |
return getMenu(nombres, (JMenu) buscado); |
456 |
} else {
|
457 |
return null; |
458 |
} |
459 |
} |
460 |
} |
461 |
|
462 |
/**
|
463 |
* Crea la estructura de menus recursivamente. Por ejemplo, si se le pasa
|
464 |
* en el par?metro nombres el array {"Search", "References", "Workspace"}
|
465 |
* crear? un men? Search, un submen? del anterior que se llamar?
|
466 |
* References y debajo de ?ste ?ltimo otro menu llamado Workspace
|
467 |
*
|
468 |
* @param nombres Array con los nombres de los men?s que se quieren crear
|
469 |
* @param padre Menu padre de los men?s creados. Es ?til porque es un
|
470 |
* algoritmo recursivo
|
471 |
*
|
472 |
* @return Devuelve el men? creado. Al final de toda la recursividad,
|
473 |
* devolver? el men? de m?s abajo en la jerarqu?a
|
474 |
*
|
475 |
* @throws RuntimeException DOCUMENT ME!
|
476 |
*/
|
477 |
private JMenu createMenus(Vector nombres, JMenu padre) { |
478 |
if (!SwingUtilities.isEventDispatchThread()) { |
479 |
throw new RuntimeException("No Event Dispatch Thread"); |
480 |
} |
481 |
|
482 |
//si no quedan nombres de menu por crear se vuelve: caso base
|
483 |
if (nombres.size() == 0) { |
484 |
return padre;
|
485 |
} |
486 |
|
487 |
//Se busca el menu por si ya existiera para no crearlo otra vez
|
488 |
JMenu buscado = null; |
489 |
|
490 |
for (int i = 0; i < padre.getMenuComponentCount(); i++) { |
491 |
try {
|
492 |
JMenu hijo = (JMenu) padre.getMenuComponent(i); |
493 |
|
494 |
if (hijo.getText().compareToIgnoreCase((String) nombres.get(0)) == 0) { |
495 |
buscado = hijo; |
496 |
} |
497 |
} catch (ClassCastException e) { |
498 |
/*
|
499 |
* Se ha encontrado un elemento hoja del arbol de men?es
|
500 |
*/
|
501 |
} |
502 |
} |
503 |
|
504 |
if (buscado != null) { |
505 |
//Si lo hemos encontrado creamos el resto
|
506 |
nombres.remove(0);
|
507 |
|
508 |
return createMenus(nombres, buscado);
|
509 |
} else {
|
510 |
//Si no lo hemos encontrado se crea el menu, se a?ade al padre
|
511 |
//y se crea el resto
|
512 |
JMenu menuPadre = new JMenu((String) nombres.get(0)); |
513 |
padre.add(menuPadre); |
514 |
|
515 |
nombres.remove(0);
|
516 |
|
517 |
return createMenus(nombres, menuPadre);
|
518 |
} |
519 |
} |
520 |
|
521 |
/**
|
522 |
* M?todo invocado en respuesta a ciertos eventos de la interfaz que pueden
|
523 |
* ocultar botones de las barras de herramientas y que redimensiona ?sta
|
524 |
* de manera conveniente para que no se oculte ninguno
|
525 |
*/
|
526 |
private void ajustarToolBar() { |
527 |
int margen = 8; |
528 |
int numFilas = 1; |
529 |
double acum = margen;
|
530 |
|
531 |
int toolHeight = 0; |
532 |
|
533 |
for (int i = 0; i < toolBars.getComponentCount(); i++) { |
534 |
Component c = toolBars.getComponent(i);
|
535 |
|
536 |
if (!c.isVisible()) {
|
537 |
continue;
|
538 |
} |
539 |
|
540 |
double width = c.getPreferredSize().getWidth();
|
541 |
acum = acum + width; |
542 |
|
543 |
if (acum > this.getWidth()) { |
544 |
numFilas++; |
545 |
acum = width + margen; |
546 |
} |
547 |
|
548 |
if (c.getPreferredSize().getHeight() > toolHeight) {
|
549 |
toolHeight = c.getPreferredSize().height; |
550 |
} |
551 |
} |
552 |
|
553 |
toolBars.setPreferredSize(new Dimension(this.getWidth(), |
554 |
(int) (numFilas * toolHeight)));
|
555 |
|
556 |
toolBars.updateUI(); |
557 |
} |
558 |
|
559 |
/**
|
560 |
* DOCUMENT ME!
|
561 |
*
|
562 |
* @param classesExtensions
|
563 |
*/
|
564 |
public void setClassesExtensions(HashMap classesExtensions) { |
565 |
this.classesExtensions = classesExtensions;
|
566 |
} |
567 |
|
568 |
/**
|
569 |
* M?todo de callback invocado cuando se selecciona un men? o un bot?n de
|
570 |
* la barra de herramientas. Se busca la extensi?n asociada y se ejecuta
|
571 |
*
|
572 |
* @param e Evento producido
|
573 |
*/
|
574 |
public void actionPerformed(ActionEvent e) { |
575 |
AbstractButton control = (AbstractButton) e.getSource(); |
576 |
com.iver.andami.plugins.Extension ext = (com.iver.andami.plugins.Extension) classesExtensions.get((Class) controlClass.get(
|
577 |
control)); |
578 |
String actionCommand = control.getActionCommand();
|
579 |
|
580 |
try {
|
581 |
ext.execute(actionCommand); |
582 |
} catch (RuntimeException t) { |
583 |
NotificationManager.addError(Messages.getString( |
584 |
"MDIFrame.Error_no_capturado_por_el_usuario"), t);
|
585 |
} |
586 |
|
587 |
enableControls(); |
588 |
showMemory(); |
589 |
} |
590 |
|
591 |
/**
|
592 |
* DOCUMENT ME!
|
593 |
*
|
594 |
* @param name DOCUMENT ME!
|
595 |
* @param loader DOCUMENT ME!
|
596 |
*
|
597 |
* @return DOCUMENT ME!
|
598 |
*/
|
599 |
private String getName(String name, PluginClassLoader loader) { |
600 |
if (name.indexOf('.') == -1) { |
601 |
return loader.getPluginName() + "." + name; |
602 |
} else {
|
603 |
return name;
|
604 |
} |
605 |
} |
606 |
|
607 |
/**
|
608 |
* DOCUMENT ME!
|
609 |
*
|
610 |
* @param loader DOCUMENT ME!
|
611 |
* @param menu DOCUMENT ME!
|
612 |
*
|
613 |
* @throws RuntimeException DOCUMENT ME!
|
614 |
*/
|
615 |
public void addPopupMenu(PluginClassLoader loader, PopupMenu menu) { |
616 |
if (!SwingUtilities.isEventDispatchThread()) { |
617 |
throw new RuntimeException("No Event Dispatch Thread"); |
618 |
} |
619 |
|
620 |
String name = getName(menu.getName(), loader);
|
621 |
|
622 |
//Se crea el control popupmenu
|
623 |
JPopUpMenu popupMenu = (JPopUpMenu) popupMap.get(name); |
624 |
|
625 |
if (popupMenu == null) { |
626 |
popupMenu = new JPopUpMenu(menu.getName());
|
627 |
popupMap.put(name, popupMenu); |
628 |
} |
629 |
|
630 |
// Se a?aden las entradas
|
631 |
Menu[] menues = menu.getMenu(); |
632 |
|
633 |
for (int i = 0; i < menues.length; i++) { |
634 |
//Se registra y a?ade el menu
|
635 |
JMenuItem nuevoMenu = createJMenuItem(loader, menues[i]);
|
636 |
|
637 |
popupMenu.add(nuevoMenu); |
638 |
} |
639 |
} |
640 |
|
641 |
/**
|
642 |
* DOCUMENT ME!
|
643 |
*
|
644 |
* @param loader
|
645 |
* @param menu
|
646 |
*
|
647 |
* @return
|
648 |
*
|
649 |
* @throws RuntimeException DOCUMENT ME!
|
650 |
*/
|
651 |
private JMenuItem createJMenuItem(PluginClassLoader loader, Menu menu) { |
652 |
JMenuItem nuevoMenu = null; |
653 |
|
654 |
PluginServices ps = PluginServices.getPluginServices(loader.getPluginName()); |
655 |
String texto = menu.getText();
|
656 |
texto = texto.substring(texto.lastIndexOf('/') + 1); |
657 |
texto = ps.getText(texto); |
658 |
|
659 |
if (menu.getIcon() != null) { |
660 |
nuevoMenu = new JMenuItem(texto, |
661 |
new ImageIcon(loader.getResource(menu.getIcon()))); |
662 |
} else {
|
663 |
nuevoMenu = new JMenuItem(texto); |
664 |
} |
665 |
|
666 |
if (menu.getMnemonic() != null) { |
667 |
if (menu.getMnemonic().length() != 1) { |
668 |
throw new RuntimeException( |
669 |
"Mnemonic must be 1 character length");
|
670 |
} |
671 |
|
672 |
nuevoMenu.setMnemonic(KeyMapping.getKey(menu.getMnemonic().charAt(0)));
|
673 |
} |
674 |
|
675 |
if (menu.getKey() != null) { |
676 |
nuevoMenu.setAccelerator(KeyStroke.getKeyStroke(KeyMapping.getKey(
|
677 |
menu.getKey().charAt(0)), ActionEvent.CTRL_MASK)); |
678 |
} |
679 |
|
680 |
nuevoMenu.setActionCommand(menu.getActionCommand()); |
681 |
|
682 |
if (menu.getTooltip() != null) { |
683 |
nuevoMenu.setToolTip(ps.getText(menu.getTooltip())); |
684 |
} |
685 |
|
686 |
if (menu.getEnableText() != null) { |
687 |
nuevoMenu.setEnableText(ps.getText(menu.getEnableText())); |
688 |
} |
689 |
|
690 |
nuevoMenu.setEnabled(true);
|
691 |
nuevoMenu.setVisible(true);
|
692 |
|
693 |
return nuevoMenu;
|
694 |
} |
695 |
|
696 |
/**
|
697 |
* Muestra u oculta el menu de nombre 'name'
|
698 |
*
|
699 |
* @param name Nombre del menu que se quiere mostrar
|
700 |
* @param x Evento de raton
|
701 |
* @param y DOCUMENT ME!
|
702 |
* @param c DOCUMENT ME!
|
703 |
*/
|
704 |
private void showPopupMenu(String name, int x, int y, Component c) { |
705 |
JPopupMenu menu = (JPopupMenu) popupMap.get(name); |
706 |
|
707 |
if (menu != null) { |
708 |
menu.show(c, x, y); |
709 |
} |
710 |
} |
711 |
|
712 |
/**
|
713 |
* DOCUMENT ME!
|
714 |
*
|
715 |
* @param name DOCUMENT ME!
|
716 |
* @param listener DOCUMENT ME!
|
717 |
*/
|
718 |
public void removePopupMenuListener(String name, ActionListener listener) { |
719 |
JPopupMenu menu = (JPopupMenu) popupMap.get(name); |
720 |
|
721 |
if (menu != null) { |
722 |
Component[] jmenuitems = menu.getComponents(); |
723 |
|
724 |
for (int i = 0; i < jmenuitems.length; i++) { |
725 |
if (jmenuitems[i] instanceof JMenuItem) { |
726 |
((JMenuItem) jmenuitems[i]).removeActionListener(listener);
|
727 |
} |
728 |
} |
729 |
} |
730 |
} |
731 |
|
732 |
/**
|
733 |
* DOCUMENT ME!
|
734 |
*
|
735 |
* @param popupName
|
736 |
* @param c DOCUMENT ME!
|
737 |
* @param listener
|
738 |
* @param loader
|
739 |
*/
|
740 |
public void addPopupMenuListener(String popupName, Component c, |
741 |
ActionListener listener, PluginClassLoader loader) {
|
742 |
final String name = getName(popupName, loader); |
743 |
|
744 |
JPopupMenu menu = (JPopupMenu) popupMap.get(name); |
745 |
|
746 |
if (menu != null) { |
747 |
Component[] jmenuitems = menu.getComponents(); |
748 |
|
749 |
for (int i = 0; i < jmenuitems.length; i++) { |
750 |
if (jmenuitems[i] instanceof JMenuItem) { |
751 |
((JMenuItem) jmenuitems[i]).addActionListener(listener);
|
752 |
} |
753 |
} |
754 |
} |
755 |
|
756 |
c.addMouseListener(new MouseAdapter() { |
757 |
public void mousePressed(MouseEvent e) { |
758 |
if (e.isPopupTrigger()) {
|
759 |
showPopupMenu(name, e.getX(), e.getY(), e.getComponent()); |
760 |
} |
761 |
} |
762 |
|
763 |
public void mouseReleased(MouseEvent e) { |
764 |
if (e.isPopupTrigger()) {
|
765 |
showPopupMenu(name, e.getX(), e.getY(), e.getComponent()); |
766 |
} |
767 |
} |
768 |
}); |
769 |
} |
770 |
|
771 |
/**
|
772 |
* Itera por los controles preguntando a las extensiones si estos est?n
|
773 |
* habilitados y visibles
|
774 |
*
|
775 |
* @throws RuntimeException DOCUMENT ME!
|
776 |
*/
|
777 |
public void enableControls() { |
778 |
if (!SwingUtilities.isEventDispatchThread()) { |
779 |
throw new RuntimeException("No Event Dispatch Thread"); |
780 |
} |
781 |
|
782 |
Iterator e = classesExtensions.values().iterator();
|
783 |
HashMap estadoExtensiones = new HashMap(); |
784 |
HashMap visibilidadExtensiones = new HashMap(); |
785 |
|
786 |
while (e.hasNext()) {
|
787 |
com.iver.andami.plugins.Extension ext = (com.iver.andami.plugins.Extension) e.next(); |
788 |
|
789 |
try {
|
790 |
if (estadoExtensiones.get(ext) == null) { |
791 |
Boolean visible = new Boolean(ext.isVisible()); |
792 |
Boolean enabled = new Boolean(false); |
793 |
|
794 |
if (visible.booleanValue()) {
|
795 |
enabled = new Boolean(ext.isEnabled()); |
796 |
} |
797 |
|
798 |
estadoExtensiones.put(ext, enabled); |
799 |
visibilidadExtensiones.put(ext, visible); |
800 |
} |
801 |
} catch (Throwable e1) { |
802 |
NotificationManager.addError(Messages.getString( |
803 |
"MDIFrame.Error_no_capturado_por_el_usuario"), e1);
|
804 |
estadoExtensiones.put(ext, Boolean.FALSE);
|
805 |
} |
806 |
} |
807 |
|
808 |
//Se habilitan y deshabilitan los controles
|
809 |
e = controlClass.keySet().iterator(); |
810 |
|
811 |
while (e.hasNext()) {
|
812 |
JComponent control = (JComponent) e.next(); |
813 |
|
814 |
try {
|
815 |
com.iver.andami.plugins.Extension ext = (com.iver.andami.plugins.Extension) classesExtensions.get((Class) controlClass.get(
|
816 |
control)); |
817 |
boolean enabled = ((Boolean) estadoExtensiones.get(ext)).booleanValue(); |
818 |
boolean visible = ((Boolean) visibilidadExtensiones.get(ext)).booleanValue(); |
819 |
control.setEnabled(enabled); |
820 |
control.setVisible(visible); |
821 |
} catch (Exception ex) { |
822 |
control.setEnabled(false);
|
823 |
control.setVisible(false);
|
824 |
} |
825 |
} |
826 |
|
827 |
//Se itera por los men?es para ocultar los que no tengan hijos visibles
|
828 |
for (int i = 0; i < menuBar.getMenuCount(); i++) { |
829 |
MenuElement menu = menuBar.getMenu(i);
|
830 |
ocultarMenus(menu); |
831 |
} |
832 |
|
833 |
//Se ocultan las barras que no tienen herramientas
|
834 |
Iterator it = toolBarMap.values().iterator();
|
835 |
|
836 |
while (it.hasNext()) {
|
837 |
SelectableToolBar t = (SelectableToolBar) it.next(); |
838 |
boolean todosOcultos = true; |
839 |
|
840 |
for (int i = 0; i < t.getComponentCount(); i++) { |
841 |
if (t.getComponent(i).isVisible()) {
|
842 |
todosOcultos = false;
|
843 |
} |
844 |
} |
845 |
|
846 |
if (todosOcultos) {
|
847 |
t.setVisible(false);
|
848 |
} else {
|
849 |
t.setVisible(true);
|
850 |
} |
851 |
} |
852 |
|
853 |
if (mdiManager != null) { |
854 |
JPanel f = (JPanel) mdiManager.getActiveView(); |
855 |
|
856 |
if (f != null) { |
857 |
if (lastLabelClass != f.getClass()) {
|
858 |
lastLabelClass = f.getClass(); |
859 |
|
860 |
Label[] lbls = (Label[]) classLabels.get(lastLabelClass); |
861 |
|
862 |
if (lbls != null) { |
863 |
bEstado.setLabelSet(lbls); |
864 |
} |
865 |
} |
866 |
} |
867 |
} |
868 |
|
869 |
ajustarToolBar(); |
870 |
|
871 |
showMemory(); |
872 |
} |
873 |
|
874 |
/**
|
875 |
* Establece la visibilidad de un menu y todos sus descendientes en la
|
876 |
* jerarquia teniendo en cuenta la visibilidad de todos los submenus.
|
877 |
*
|
878 |
* @param menu Menu que se quiere visualizar
|
879 |
*
|
880 |
* @return Devuelve true si el menu es visible y false en caso contrario
|
881 |
*/
|
882 |
private boolean ocultarMenus(MenuElement menu) { |
883 |
MenuElement[] submenus = menu.getSubElements(); |
884 |
|
885 |
//Si no tiene hijos se devuelve su visibilidad
|
886 |
if (submenus.length == 0) { |
887 |
return menu.getComponent().isVisible();
|
888 |
} |
889 |
|
890 |
/*
|
891 |
* Si tiene hijos se devuelve true si alg?no de ellos es visible,
|
892 |
* pero se itera por todos ellos
|
893 |
*/
|
894 |
boolean visible = false; |
895 |
|
896 |
for (int i = 0; i < submenus.length; i++) { |
897 |
if (ocultarMenus(submenus[i])) {
|
898 |
if (!(menu instanceof JPopupMenu)) { |
899 |
menu.getComponent().setVisible(true);
|
900 |
} |
901 |
|
902 |
visible = true;
|
903 |
} |
904 |
} |
905 |
|
906 |
if (visible) {
|
907 |
return true; |
908 |
} |
909 |
|
910 |
menu.getComponent().setVisible(false);
|
911 |
|
912 |
return false; |
913 |
} |
914 |
|
915 |
/**
|
916 |
* Muestra la memoria consumida por el programa
|
917 |
*/
|
918 |
private void showMemory() { |
919 |
Runtime r = Runtime.getRuntime(); |
920 |
long mem = r.totalMemory() - r.freeMemory();
|
921 |
logger.debug("Memoria total: " + mem);
|
922 |
} |
923 |
|
924 |
/**
|
925 |
* DOCUMENT ME!
|
926 |
*
|
927 |
* @return
|
928 |
*/
|
929 |
public MDIManager getMDIManager() {
|
930 |
return mdiManager;
|
931 |
} |
932 |
|
933 |
/**
|
934 |
* Establece el mensaje en la barra de estado asociado a una etiqueta
|
935 |
*
|
936 |
* @return DOCUMENT ME!
|
937 |
*/
|
938 |
public NewStatusBar getStatusBar() {
|
939 |
return bEstado;
|
940 |
} |
941 |
|
942 |
/**
|
943 |
* DOCUMENT ME!
|
944 |
*
|
945 |
* @param clase
|
946 |
* @param label
|
947 |
*/
|
948 |
public void setLabels(Class clase, Label[] label) { |
949 |
classLabels.put(clase, label); |
950 |
} |
951 |
|
952 |
/**
|
953 |
* @see com.iver.andami.ui.mdiFrame.MainFrame#removeMenu(com.iver.andami.plugins.config.generate.Menu)
|
954 |
*/
|
955 |
public void removeMenu(Menu menu) { |
956 |
JMenuItem delete = (JMenuItem) infoCodedMenus.get(menu); |
957 |
|
958 |
if (delete == null) { |
959 |
throw new NoSuchElementException(menu.getText()); |
960 |
} |
961 |
|
962 |
delete.getParent().remove(delete); |
963 |
infoCodedMenus.remove(menu); |
964 |
} |
965 |
|
966 |
/**
|
967 |
* @see com.iver.andami.ui.mdiFrame.MainFrame#addMenu(com.iver.andami.plugins.config.generate.Menu,
|
968 |
* java.awt.event.ActionListener, PluginClassLoader)
|
969 |
*/
|
970 |
public void addMenu(Menu menu, ActionListener listener, |
971 |
PluginClassLoader loader) { |
972 |
JMenu menuPadre = createMenuAncestors(menu, loader);
|
973 |
|
974 |
//Se registra y a?ade el menu
|
975 |
JMenuItem nuevoMenu = createJMenuItem(loader, menu);
|
976 |
nuevoMenu.addMouseListener(tooltipListener); |
977 |
nuevoMenu.addActionListener(listener); |
978 |
menuPadre.add(nuevoMenu); |
979 |
|
980 |
infoCodedMenus.put(menu, nuevoMenu); |
981 |
} |
982 |
|
983 |
/**
|
984 |
* @see com.iver.andami.ui.mdiFrame.MainFrame#changeMenuName(java.lang.String[],
|
985 |
* String, com.iver.andami.plugins.PluginClassLoader)
|
986 |
*/
|
987 |
public void changeMenuName(String[] menu, String newName, |
988 |
PluginClassLoader loader) { |
989 |
PluginServices ps = PluginServices.getPluginServices(loader.getPluginName()); |
990 |
|
991 |
for (int i = 0; i < menu.length; i++) { |
992 |
menu[i] = ps.getText(menu[i]); |
993 |
} |
994 |
|
995 |
JMenu menuPadre = null; |
996 |
|
997 |
//Se busca el padre en la menuBar
|
998 |
for (int i = 0; i < menuBar.getMenuCount(); i++) { |
999 |
if (menuBar.getMenu(i).getText().compareToIgnoreCase(menu[0]) == 0) { |
1000 |
menuPadre = (JMenu) menuBar.getMenu(i);
|
1001 |
} |
1002 |
} |
1003 |
|
1004 |
if (menuPadre == null) { |
1005 |
throw new NoSuchMenuException(menu[0]); |
1006 |
} |
1007 |
|
1008 |
Vector nombres = new Vector(); |
1009 |
|
1010 |
for (int i = 1; i < menu.length; i++) { |
1011 |
nombres.add(menu[i]); |
1012 |
} |
1013 |
|
1014 |
javax.swing.JMenuItem m = getMenu(nombres, menuPadre); |
1015 |
|
1016 |
if (m != null) { |
1017 |
m.setText(newName); |
1018 |
}else{
|
1019 |
throw new NoSuchMenuException(); |
1020 |
} |
1021 |
} |
1022 |
|
1023 |
/**
|
1024 |
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
1025 |
*/
|
1026 |
public void componentHidden(ComponentEvent arg0) { |
1027 |
} |
1028 |
|
1029 |
/**
|
1030 |
* @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
1031 |
*/
|
1032 |
public void componentMoved(ComponentEvent arg0) { |
1033 |
} |
1034 |
|
1035 |
/**
|
1036 |
* @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
1037 |
*/
|
1038 |
public void componentResized(ComponentEvent arg0) { |
1039 |
ajustarToolBar(); |
1040 |
bEstado.setFixedLabelWidth(this.getWidth() * 0.3); |
1041 |
} |
1042 |
|
1043 |
/**
|
1044 |
* @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
1045 |
*/
|
1046 |
public void componentShown(ComponentEvent arg0) { |
1047 |
} |
1048 |
|
1049 |
/**
|
1050 |
* @see java.awt.event.ContainerListener#componentAdded(java.awt.event.ContainerEvent)
|
1051 |
*/
|
1052 |
public void componentAdded(ContainerEvent arg0) { |
1053 |
ajustarToolBar(); |
1054 |
} |
1055 |
|
1056 |
/**
|
1057 |
* @see java.awt.event.ContainerListener#componentRemoved(java.awt.event.ContainerEvent)
|
1058 |
*/
|
1059 |
public void componentRemoved(ContainerEvent arg0) { |
1060 |
ajustarToolBar(); |
1061 |
} |
1062 |
|
1063 |
/**
|
1064 |
* DOCUMENT ME!
|
1065 |
*
|
1066 |
* @author $author$
|
1067 |
* @version $Revision: 1340 $
|
1068 |
*/
|
1069 |
public class TooltipListener extends MouseAdapter { |
1070 |
/**
|
1071 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
1072 |
*/
|
1073 |
public void mouseEntered(MouseEvent e) { |
1074 |
JComponent control = (JComponent) e.getSource(); |
1075 |
EnableTextSupport ets = (EnableTextSupport) e.getSource(); |
1076 |
|
1077 |
String texto = null; |
1078 |
texto = control.getToolTipText(); |
1079 |
|
1080 |
if (texto != null) { |
1081 |
bEstado.setInfoTextTemporal(texto); |
1082 |
} |
1083 |
} |
1084 |
|
1085 |
/**
|
1086 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
1087 |
*/
|
1088 |
public void mouseExited(MouseEvent arg0) { |
1089 |
bEstado.restaurarTexto(); |
1090 |
} |
1091 |
|
1092 |
/**
|
1093 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
1094 |
*/
|
1095 |
public void mousePressed(MouseEvent e) { |
1096 |
bEstado.restaurarTexto(); |
1097 |
} |
1098 |
} |
1099 |
} |