Statistics
| Revision:

gvsig-3d / 2.1 / trunk / org.gvsig.gvsig3d / org.gvsig.gvsig3d.app / org.gvsig.gvsig3d.app.commons / src / main / java / org / gvsig / gvsig3d / app / skin / JDialogSkin.java @ 385

History | View | Annotate | Download (36.3 KB)

1
/* gvSIG 3D extension for gvSIG
2
 *
3
 * Copyright (C) 2012 Prodevelop.
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
 *   Prodevelop, S.L.
22
 *   Pza. Don Juan de Villarrasa, 14 - 5
23
 *   46001 Valencia
24
 *   Spain
25
 *
26
 *   +34 963 510 612
27
 *   +34 963 510 968
28
 *   prode@prodevelop.es
29
 *   http://www.prodevelop.es
30
 */
31
/*
32
 * AUTHORS:
33
 * 2012 AI2 - Instituto Universitario de Automatica e Informatica Industrial.
34
 * Universitat Politecnica de Valencia (UPV)
35
 * http://www.ai2.upv.es
36
 */
37

    
38

    
39
package org.gvsig.gvsig3d.app.skin;
40

    
41
import java.awt.BorderLayout;
42
import java.awt.Component;
43
import java.awt.Container;
44
import java.awt.Cursor;
45
import java.awt.Dimension;
46
import java.awt.Graphics;
47
import java.awt.KeyEventDispatcher;
48
import java.awt.Point;
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
51
import java.awt.event.KeyEvent;
52
import java.awt.event.WindowAdapter;
53
import java.awt.event.WindowEvent;
54
import java.awt.event.WindowListener;
55
import java.beans.PropertyVetoException;
56
import java.util.ArrayList;
57
import java.util.HashMap;
58
import java.util.Iterator;
59
import java.util.TreeMap;
60

    
61
import javax.swing.DefaultDesktopManager;
62
import javax.swing.DesktopManager;
63
import javax.swing.ImageIcon;
64
import javax.swing.JDesktopPane;
65
import javax.swing.JDialog;
66
import javax.swing.JFrame;
67
import javax.swing.JInternalFrame;
68
import javax.swing.KeyStroke;
69

    
70
import org.gvsig.andami.PluginServices;
71
import org.gvsig.andami.ui.mdiFrame.GlassPane;
72
import org.gvsig.andami.ui.mdiFrame.MDIFrame;
73
import org.gvsig.andami.ui.mdiFrame.NewStatusBar;
74
import org.gvsig.andami.ui.mdiManager.IWindow;
75
import org.gvsig.andami.ui.mdiManager.IWindowListener;
76
import org.gvsig.andami.ui.mdiManager.MDIUtilities;
77
import org.gvsig.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
78
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
79
import org.gvsig.andami.ui.mdiManager.WindowInfo;
80
import org.gvsig.andami.ui.theme.Theme;
81
import org.gvsig.coreplugin.mdiManager.NewSkin;
82
import org.slf4j.Logger;
83
import org.slf4j.LoggerFactory;
84

    
85

    
86
/**
87
 * @author Jesus Zarzoso- jzarzoso@ai2.upv.es
88
 * @version $Id$
89
 * 
90
 */
91
public class JDialogSkin extends NewSkin {
92

    
93
    private static final int DefaultXMargin = 20; // Added for the method
94
                                                  // 'centreJInternalFrame'
95
    private static final int DefaultYMargin = 20; // Added for the method
96
                                                  // 'centreJInternalFrame'
97
    private static final int MinimumXMargin = 130; // Added for the method
98
                                                   // 'centreJInternalFrame'
99
    private static final int MinimumYMargin = 60; // Added for the method
100
                                                  // 'centreJInternalFrame'
101

    
102
    /**
103
     * Variable privada <code>desktopManager</code> para usarlo cuando sale
104
     * una ventana que no queremos que nos restaure las que tenemos maximizadas.
105
     * Justo antes de usar el setMaximize(false), le pegamos el cambiazo.
106
     */
107
    private static DesktopManager desktopManager = new DefaultDesktopManager();
108

    
109
    /** log */
110
    private static Logger logger =
111
        LoggerFactory.getLogger(JDialogSkin.class.getName());
112

    
113
    /** Panel de la MDIFrame */
114
    private MyDesktopPane panel = new MyDesktopPane();
115

    
116
    /** MDIFrame */
117
    private MDIFrame mainFrame;
118

    
119
    private GlassPane glassPane = new GlassPane();
120

    
121
    private JDialogStackSupport dss;
122

    
123
    /**
124
     * Associates JInternalFrames with the IWindow they contain
125
     */
126
    private JDialogFrameWindowSupport fws;
127

    
128
    private JDialogWindowInfoSupport wis;
129

    
130
    private JDialogWindowStackSupport wss;
131

    
132
    private JDialogSingletonWindowSupport sws;
133

    
134
    private Cursor lastCursor = null;
135
    private ImageIcon image;
136
    private String typeDesktop;
137

    
138
    private IWindow lastActiveWindow = null;
139

    
140
    /**
141
     * @see com.iver.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
142
     */
143
    public void init(MDIFrame f) {
144
        // Inicializa el Frame y la consola
145
        mainFrame = f;
146
        mainFrame.setGlassPane(glassPane);
147
        panel.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
148

    
149
        mainFrame.getContentPane().add(panel, BorderLayout.CENTER);
150
        panel.setDesktopManager(desktopManager);
151

    
152
        fws = new JDialogFrameWindowSupport(mainFrame);
153
        dss = new JDialogStackSupport(mainFrame);
154
        sws = new JDialogSingletonWindowSupport(wis, fws);
155
        wis = new JDialogWindowInfoSupport(mainFrame, fws, sws);
156
        fws.setVis(wis);
157
        wss = new JDialogWindowStackSupport(wis);
158

    
159
        KeyStroke controlTab =
160
            KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.CTRL_MASK);
161

    
162
        PluginServices.registerKeyStroke(controlTab, new KeyEventDispatcher() {
163

    
164
            public boolean dispatchKeyEvent(KeyEvent e) {
165
                IWindow[] views = getAllWindows();
166
                if (views.length <= 0 || e.getID() == KeyEvent.KEY_PRESSED)
167
                    return false;
168

    
169
                int current = 0;
170
                for (int i = 0; i < views.length; i++) {
171
                    if (views[i].equals(getActiveWindow())) {
172
                        current = i;
173
                        break;
174
                    }
175
                }
176
                addWindow(views[(current + 1) % views.length]);
177
                return true;
178
            }
179

    
180
        });
181
    }
182

    
183
    /**
184
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
185
     */
186
    public IWindow addWindow(IWindow p)
187
        throws SingletonDialogAlreadyShownException {
188
        // se obtiene la informacion de la vista
189
        WindowInfo wi = wis.getWindowInfo(p);
190

    
191
        // Se comprueban las incompatibilidades que pudieran haber en la vista
192
        MDIUtilities.checkWindowInfo(wi);
193
        if ((p instanceof SingletonWindow) && (wi.isModal())) {
194
            throw new RuntimeException("A modal view cannot be a SingletonView");
195
        }
196

    
197
        /*
198
         * Se obtiene la referencia a la vista anterior por si es una singleton
199
         * y esta siendo mostrada. Se obtiene su informacion si ya fue mostrada
200
         */
201
        boolean singletonPreviouslyAdded = false;
202

    
203
        if (p instanceof SingletonWindow) {
204
            SingletonWindow sw = (SingletonWindow) p;
205
            if (sws.registerWindow(sw.getClass(), sw.getWindowModel(), wi)) {
206
                singletonPreviouslyAdded = true;
207
            }
208
        }
209

    
210
        if (singletonPreviouslyAdded) {
211
            // Si la vista no esta actualmente abierta
212
            if (!sws.contains((SingletonWindow) p)) {
213
                JDialog frame = (JDialog) fws.getFrame(p);
214
                sws.openSingletonWindow((SingletonWindow) p, frame);
215

    
216
                addFrame(frame, wi);
217
                wss.add(p, new ActionListener() {
218

    
219
                    public void actionPerformed(ActionEvent e) {
220
                        IWindow v =
221
                            wis.getWindowById(Integer.parseInt(e
222
                                .getActionCommand()));
223
                        JDialog frame = (JDialog) fws.getFrame(v);
224
                        activateFrame(frame);
225
                    }
226
                });
227
                return p;
228
            } else {
229
                // La vista esta actualmente abierta
230
                JDialog frame = (JDialog) sws.getFrame((SingletonWindow) p);
231
                activateFrame(frame);
232
                wss.setActive(p);
233
                return fws.getWindow(frame);
234
            }
235
        } else {
236
            if (wi.isModal()) {
237
                addJDialog(p);
238
            } else {
239
                // Se situa la vista en la pila de vistas
240
                wss.add(p, new ActionListener() {
241

    
242
                    public void actionPerformed(ActionEvent e) {
243
                        IWindow v =
244
                            wis.getWindowById(Integer.parseInt(e
245
                                .getActionCommand()));
246
                        JDialog f = (JDialog) fws.getFrame(v);
247
                        activateFrame(f);
248
                    }
249
                });
250
                addFrame(p);
251
            }
252

    
253
            return p;
254
        }
255
    }
256

    
257
    /**
258
     * Situa una ventana en el centro de la pantalla
259
     * 
260
     * @param d
261
     *            Dialogo que se quiere situar
262
     */
263
    private void centerWindow(IWindow win) {
264
        JDialog dialog = (JDialog) fws.getFrame(win);
265

    
266
        int offSetX = dialog.getWidth() / 2;
267
        int offSetY = dialog.getHeight() / 2;
268

    
269
        dialog.setLocation((mainFrame.getWidth() / 2) - offSetX, (mainFrame
270
            .getHeight() / 2)
271
            - offSetY);
272
    }
273

    
274
    /**
275
     * Similar method as 'addView' but in this case centres the new
276
     * JInternalFrame of the View in the contentPane of the MainFrame
277
     * 
278
     * @see com.iver.core.mdiManager.NewSkin#addWindow(com.iver.andami.ui.mdiManager.IWindow)
279
     * 
280
     * @author Pablo Piqueras Bartolom
281
     */
282
    public IWindow addCentredWindow(IWindow p)
283
        throws SingletonDialogAlreadyShownException {
284
        IWindow window = addWindow(p);
285
        centerWindow(window);
286
        return window;
287
    }
288

    
289
    /**
290
     * Centres the JInternalFrame in the center of the contentPane of the
291
     * MainFrame
292
     * If it can't be showed completely the JInternalFrame, tries that the
293
     * top-left corner of the JInternalFrame would be showed
294
     * 
295
     * @author Pablo Piqueras Bartolom
296
     * 
297
     * @param jInternalFrame
298
     *            A reference to the frame to centring
299
     */
300
    private static synchronized void centreJFrame(JFrame frame) {
301

    
302
        // The top-left square of frame reference
303
        Point newReferencePoint = new Point();
304

    
305
        // A reference to the panel where the JInternalFrame will be displayed
306
        Container contentPane =
307
            ((JFrame) PluginServices.getMainFrame()).getContentPane();
308

    
309
        // Get the NewStatusBar component
310
        NewStatusBar newStatusBar =
311
            ((NewStatusBar) contentPane.getComponent(1));
312
        JDesktopPane jDesktopPane =
313
            ((JDesktopPane) contentPane.getComponent(2));
314

    
315
        int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The
316
                                                                        // last
317
                                                                        // substraction
318
                                                                        // is
319
                                                                        // for
320
                                                                        // if
321
                                                                        // there
322
                                                                        // is
323
                                                                        // any
324
                                                                        // menu,...
325
                                                                        // at
326
                                                                        // left
327
        int visibleHeight =
328
            contentPane.getHeight() - newStatusBar.getHeight()
329
                - contentPane.getY()
330
                - Math.abs(jDesktopPane.getY() - contentPane.getY()); // The
331
                                                                      // last
332
                                                                      // substraction
333
                                                                      // is for
334
                                                                      // if
335
                                                                      // there
336
                                                                      // is any
337
                                                                      // menu,...
338
                                                                      // at top
339
        int freeWidth = visibleWidth - frame.getWidth();
340
        int freeHeight = visibleHeight - frame.getHeight();
341

    
342
        // Calculate the new point reference (Assure that the top-left corner is
343
        // showed)
344
        if (freeWidth < 0) {
345
            if (visibleWidth > MinimumXMargin)
346
                newReferencePoint.x = DefaultXMargin;
347
            else
348
                newReferencePoint.x = 0;
349
        } else
350
            newReferencePoint.x = freeWidth / 2;
351

    
352
        if (freeHeight < 0) {
353
            if (visibleHeight > MinimumYMargin)
354
                newReferencePoint.y = DefaultYMargin;
355
            else
356
                newReferencePoint.y = 0;
357
        } else
358
            newReferencePoint.y = freeHeight / 2;
359

    
360
        // Set the new location for this JInternalFrame
361
        frame.setLocation(newReferencePoint);
362
    }
363

    
364
    /**
365
     * DOCUMENT ME!
366
     * 
367
     * @param wnd
368
     *            DOCUMENT ME!
369
     * @param wi
370
     *            DOCUMENT ME!
371
     */
372
    private void addFrame(JDialog wnd, WindowInfo wi) {
373
        wnd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
374
        FrameListener listener = new FrameListener();
375
        wnd.addWindowListener(listener);
376
        wnd.addWindowStateListener(listener);
377
        wnd.addWindowFocusListener(listener);
378

    
379
        if (wi.isModeless() || wi.isPalette()) {
380
            // panel.add(wnd, JDesktopPane.PALETTE_LAYER);
381
            if (wi.isPalette())
382
                wnd.setFocusable(false);
383
        } else {
384
            // panel.add(wnd);
385
        }
386
        updateFrameProperties(wnd, wi);
387

    
388
        activateFrame(wnd);
389
        /*
390
         * if (wi.isMaximized()) {
391
         * wnd.setExtendedState(JFrame.MAXIMIZED_BOTH);
392
         * }
393
         * else {
394
         * int state = wnd.getExtendedState();
395
         * state = (state & (~JFrame.MAXIMIZED_BOTH));
396
         * wnd.setExtendedState(state);
397
         * }
398
         */
399
    }
400

    
401
    /**
402
     * DOCUMENT ME!
403
     * 
404
     * @param wnd
405
     *            DOCUMENT ME!
406
     * @param wi
407
     *            DOCUMENT ME!
408
     */
409
    /*
410
     * private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
411
     * wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
412
     * wnd.add
413
     * wnd.addInternalFrameListener(new FrameListener());
414
     * 
415
     * if (wi.isModeless() || wi.isPalette()) {
416
     * panel.add(wnd, JDesktopPane.PALETTE_LAYER);
417
     * if (wi.isPalette())
418
     * wnd.setFocusable(false);
419
     * } else {
420
     * panel.add(wnd);
421
     * }
422
     * // updateFrameProperties(wnd, wi);
423
     * 
424
     * activateJInternalFrame(wnd);
425
     * try{
426
     * wnd.setMaximum(wi.isMaximized());
427
     * }catch(Exception ex){
428
     * logger.warn("Error: ", ex);
429
     * }
430
     * }
431
     */
432

    
433
    private void updateFrameProperties(JDialog frame, WindowInfo wi) {
434
        int height, width;
435
        if (wi.isMaximized()) {
436
            if (wi.getNormalWidth() != -1)
437
                width = wi.getNormalWidth();
438
            else
439
                width = frame.getWidth();
440
            if (wi.getNormalHeight() != -1)
441
                height = wi.getNormalHeight();
442
            else
443
                height = frame.getHeight();
444

    
445
            frame.setSize(width, height);
446
            frame.setLocation(wi.getNormalX(), wi.getNormalY());
447
        } else {
448
            if (wi.getWidth() != -1)
449
                width = wi.getWidth();
450
            else
451
                width = frame.getWidth();
452
            if (wi.getHeight() != -1)
453
                height = wi.getHeight();
454
            else
455
                height = frame.getHeight();
456
            frame.setSize(width, height);
457
            frame.setLocation(wi.getX(), wi.getY());
458
        }
459
        frame.setTitle(wi.getTitle());
460
        frame.setVisible(wi.isVisible());
461
        frame.setResizable(wi.isResizable());
462
        /*
463
         * if (wi.isMaximized()) {
464
         * frame.set
465
         * frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
466
         * }
467
         * else {
468
         * int state = frame.getExtendedState();
469
         * state = (state & (~JFrame.MAXIMIZED_BOTH));
470
         * frame.setExtendedState(state);
471
         * }
472
         */
473
    }
474

    
475
    /**
476
     * DOCUMENT ME!
477
     * 
478
     * @param p
479
     */
480
    /*
481
     * private void addJInternalFrame(IWindow p) {
482
     * WindowInfo wi = wis.getWindowInfo(p);
483
     * 
484
     * JInternalFrame wnd = fws.getJInternalFrame(p);
485
     * 
486
     * if (p instanceof SingletonWindow) {
487
     * SingletonWindow sv = (SingletonWindow) p;
488
     * sws.openSingletonWindow(sv, wnd);
489
     * }
490
     * 
491
     * addJInternalFrame(wnd, wi);
492
     * }
493
     */
494

    
495
    /**
496
     * DOCUMENT ME!
497
     * 
498
     * @param p
499
     */
500
    /*
501
     * private void addJFrame(IWindow p) {
502
     * WindowInfo wi = wis.getWindowInfo(p);
503
     * 
504
     * JFrame wnd = fws.getFrame(p);
505
     * 
506
     * if (p instanceof SingletonWindow) {
507
     * SingletonWindow sv = (SingletonWindow) p;
508
     * sws.openSingletonWindow(sv, wnd);
509
     * }
510
     * 
511
     * addJFrame(wnd, wi);
512
     * }
513
     */
514

    
515
    /**
516
     * DOCUMENT ME!
517
     * 
518
     * @param wnd
519
     */
520
    private void activateJInternalFrame(JInternalFrame wnd) {
521
        try {
522
            wnd.moveToFront();
523
            // logger.debug("Activando " + wnd.getTitle());
524
            wnd.setSelected(true);
525
            wnd.setIcon(false);
526
        } catch (PropertyVetoException e) {
527
            // logger.error(e);
528
        }
529
    }
530

    
531
    /**
532
     * DOCUMENT ME!
533
     * 
534
     * @param wnd
535
     */
536
    private void activateFrame(JDialog wnd) {
537
        wnd.setVisible(true);
538
        wnd.toFront();
539

    
540
        /*
541
         * int state = wnd.getExtendedState();
542
         * state = (state & (~JFrame.ICONIFIED));
543
         * wnd.setExtendedState(state);
544
         */
545
    }
546

    
547
    /**
548
     * Situa un dialogo modal en el centro de la pantalla
549
     * 
550
     * @param d
551
     *            Dialogo que se quiere situar
552
     */
553
    private void centerDialog(JDialog d) {
554
        int offSetX = d.getWidth() / 2;
555
        int offSetY = d.getHeight() / 2;
556

    
557
        d.setLocation((mainFrame.getWidth() / 2) - offSetX, (mainFrame
558
            .getHeight() / 2)
559
            - offSetY);
560
    }
561

    
562
    /**
563
     * DOCUMENT ME!
564
     * 
565
     * @param p
566
     */
567
    private void addJDialog(IWindow p) {
568
        JDialog dlg = fws.getJDialog(p);
569

    
570
        centerDialog(dlg);
571

    
572
        dlg.addWindowListener(new DialogWindowListener());
573
        dss.pushDialog(dlg);
574

    
575
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
576
    }
577

    
578
    /**
579
     * DOCUMENT ME!
580
     * 
581
     * @param p
582
     */
583
    private void addFrame(IWindow p) {
584
        WindowInfo wi = wis.getWindowInfo(p);
585

    
586
        JDialog wnd = (JDialog) fws.getFrame(p);
587
        if (wnd == null)
588
            wnd = new JDialog();
589
        if (p instanceof SingletonWindow) {
590
            SingletonWindow sv = (SingletonWindow) p;
591
            sws.openSingletonWindow(sv, wnd);
592
        }
593

    
594
        addFrame(wnd, wi);
595
    }
596

    
597
    /**
598
     * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveWindow()
599
     */
600
    public IWindow getActiveWindow() {
601
        return wss.getActiveWindow();
602
        /*
603
         * JFrame frame = panel.getSelectedFrame();
604
         * 
605
         * if (frame != null) {
606
         * IWindow theWindow = fws.getWindow(frame);
607
         * if (theWindow == null)
608
         * return null;
609
         * if (theWindow.getWindowInfo().isPalette())
610
         * return wss.getActiveWindow();
611
         * else
612
         * return fws.getWindow(frame);
613
         * }
614
         * // return vss.getActiveView();
615
         * 
616
         * return null;
617
         */
618
    }
619

    
620
    public IWindow getFocusWindow() {
621
        return wss.getActiveWindow();
622
        /*
623
         * fws.getFocusWindow()
624
         * JFrame.get
625
         * JFrame frame = panel.getSelectedFrame();
626
         * 
627
         * if (frame != null) {
628
         * IWindow theView = fws.getWindow(frame);
629
         * if (theView == null)
630
         * return null;
631
         * return fws.getWindow(frame);
632
         * }
633
         * return null;
634
         */
635
    }
636

    
637
    /**
638
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeWindow(com.iver.andami.ui.mdiManager.IWindow)
639
     */
640
    public void closeWindow(IWindow p) {
641
        // Si es un dialogo modal
642
        if (p.getWindowInfo().isModal()) {
643
            closeJDialog();
644
        } else { // Si no es modal se cierra el JInternalFrame
645
            closeJFrame((JDialog) fws.getFrame(p));
646
        }
647
    }
648

    
649
    /**
650
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllWindows()
651
     */
652
    public void closeAllWindows() {
653
        ArrayList eliminar = new ArrayList();
654
        Iterator i = fws.getWindowIterator();
655

    
656
        while (i.hasNext()) {
657
            eliminar.add((IWindow) i.next());
658
        }
659

    
660
        for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
661
            IWindow vista = (IWindow) iter.next();
662
            closeWindow(vista);
663
        }
664
    }
665

    
666
    /**
667
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
668
     */
669
    public WindowInfo getWindowInfo(IWindow w) {
670
        WindowInfo wi = wis.getWindowInfo(w);
671

    
672
        /*
673
         * This is done now in vis.getWindowInfo(w)
674
         * 
675
         * JInternalFrame f = fws.getJInternalFrame(w);
676
         * wi.setX(f.getX());
677
         * wi.setY(f.getY());
678
         * wi.setHeight(f.getHeight());
679
         * wi.setWidth(f.getWidth());
680
         * // isClosed() doesn't work as (I) expected, why? Using isShowing
681
         * instead
682
         * wi.setClosed(!f.isShowing());
683
         * wi.setNormalBounds(f.getNormalBounds());
684
         * wi.setMaximized(f.isMaximum());
685
         */
686
        return wi;
687
    }
688

    
689
    /**
690
     * DOCUMENT ME!
691
     * 
692
     * @param dialog
693
     * @throws RuntimeException
694
     *             DOCUMENT ME!
695
     */
696
    private void closeJDialog() {
697
        JDialog dlg = dss.popDialog();
698

    
699
        dlg.setVisible(false);
700

    
701
        IWindow s = (IWindow) fws.getWindow(dlg);
702

    
703
        callWindowClosed(s);
704

    
705
        fws.closeWindow(s);
706

    
707
        // Si es singleton se desasocia el modelo con la vista
708
        if (s instanceof SingletonWindow) {
709
            sws.closeWindow((SingletonWindow) s);
710
        }
711
    }
712

    
713
    /**
714
     * DOCUMENT ME!
715
     * 
716
     * @param window
717
     *            DOCUMENT ME!
718
     */
719
    private void callWindowClosed(IWindow window) {
720
        if (window instanceof IWindowListener) {
721
            ((IWindowListener) window).windowClosed();
722
        }
723
    }
724

    
725
    /**
726
     * DOCUMENT ME!
727
     * 
728
     * @param window
729
     *            DOCUMENT ME!
730
     */
731
    private void callWindowActivated(IWindow window) {
732
        // logger.debug("View " + window.getWindowInfo().getTitle()
733
        // + " activated (callViewActivated)");
734
        if (window instanceof IWindowListener) {
735
            ((IWindowListener) window).windowActivated();
736
        }
737
    }
738

    
739
    /**
740
     * DOCUMENT ME!
741
     * 
742
     * @param frame
743
     */
744
    private void closeJInternalFrame(JInternalFrame frame) {
745
        try {
746
            IWindow s = (IWindow) fws.getWindow(frame);
747

    
748
            frame.setClosed(true);
749
            callWindowClosed(s);
750
        } catch (PropertyVetoException e) {
751
            // logger
752
            // .error(
753
            // "Not compatible with property veto's. Use ViewInfo instead.",
754
            // e);
755
        }
756
    }
757

    
758
    /**
759
     * DOCUMENT ME!
760
     * 
761
     * @param frame
762
     */
763
    private void closeJFrame(JDialog frame) {
764
        WindowListener[] listeners = frame.getWindowListeners();
765
        for (int i = 0; i < listeners.length; i++) {
766
            listeners[i].windowClosing(new WindowEvent(frame,
767
                WindowEvent.WINDOW_CLOSING));
768
        }
769
        IWindow s = (IWindow) fws.getWindow(frame);
770
        frame.dispose();
771
        callWindowClosed(s);
772
    }
773

    
774
    /**
775
     * @see com.iver.andami.plugins.IExtension#initialize()
776
     */
777
    public void initialize() {
778
    }
779

    
780
    /**
781
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
782
     */
783
    public void execute(String actionCommand) {
784
        if (actionCommand.equals("cascada")) {
785
        } else
786
            if (actionCommand.equals("mosaico")) {
787
            }
788
    }
789

    
790
    /**
791
     * @see com.iver.andami.plugins.IExtension#isEnabled()
792
     */
793
    public boolean isEnabled() {
794
        // TODO Auto-generated method stub
795
        return false;
796
    }
797

    
798
    /**
799
     * @see com.iver.andami.plugins.IExtension#isVisible()
800
     */
801
    public boolean isVisible() {
802
        // TODO Auto-generated method stub
803
        return true;
804
    }
805

    
806
    /**
807
     * @see com.iver.andami.ui.mdiManager.MDIManager#setWaitCursor()
808
     */
809
    public void setWaitCursor() {
810
        if (mainFrame != null) {
811
            glassPane.setVisible(true);
812
            lastCursor = mainFrame.getCursor();
813
            dss.setWaitCursor();
814
            glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
815
        }
816
    }
817

    
818
    /**
819
     * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
820
     */
821
    public void restoreCursor() {
822
        if (mainFrame != null) {
823
            glassPane.setVisible(false);
824
            dss.restoreCursor();
825
            glassPane.setCursor(lastCursor);
826
        }
827
    }
828

    
829
    /**
830
     * Listener para los eventos de cerrado de los dialogos. Tiene su razon de
831
     * ser en que los dialogos han de devolverse al pool cuando se cierran
832
     * 
833
     */
834
    public class DialogWindowListener extends WindowAdapter {
835

    
836
        /**
837
         * Captura el evento de cerrado de los dialogos con el fin de realizar
838
         * tareas de mantenimiento
839
         * 
840
         * @param e
841
         *            evento
842
         */
843
        public void windowClosing(WindowEvent e) {
844
            closeJDialog();
845
        }
846
    }
847

    
848
    /**
849
     * DOCUMENT ME!
850
     */
851
    public class FrameListener extends WindowAdapter {
852

    
853
        /**
854
         * @see java.awt.event.WindowAdapter#windowActivated(java.awt.event.WindowEvent)
855
         */
856
        public void windowActivated(WindowEvent e) {
857
            JDialog frame = (JDialog) e.getSource();
858
            IWindow panel = fws.getWindow(frame);
859

    
860
            if (panel != null) {
861

    
862
                WindowInfo wi = wis.getWindowInfo(panel);
863
                if (wi.isPalette())
864
                    return;
865

    
866
                wss.setActive(panel);
867

    
868
                // if (lastActiveWindow!=null &&
869
                // !lastActiveWindow.equals(panel)) {
870
                // wis.getWindowInfo(lastActiveWindow).setSelectedTools(mainFrame.getSelectedTools());
871
                // mainFrame.setSelectedTools(wi.getSelectedTools());
872
                // }
873
                if (wi.getSelectedTools() == null) {
874
                    // this is the first time this window is activated
875
                    wi.setSelectedTools(new HashMap(mainFrame
876
                        .getInitialSelectedTools()));
877
                }
878
                if (lastActiveWindow != null && !lastActiveWindow.equals(panel)) {
879
                    wis.getWindowInfo(lastActiveWindow).setSelectedTools(
880
                        mainFrame.getSelectedTools());
881
                    mainFrame.setSelectedTools(wi.getSelectedTools());
882
                }
883
                mainFrame.enableControls();
884
                callWindowActivated(panel);
885
            }
886

    
887
        }
888

    
889
        /**
890
         * @see java.awt.event.WindowAdapter#windowClosed(java.awt.event.WindowEvent)
891
         */
892
        public void windowClosed(WindowEvent e) {
893
        }
894

    
895
        /**
896
         * @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
897
         */
898
        public void windowClosing(WindowEvent e) {
899
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
900
            // logger.debug("internalFrameClosing " +
901
            // e.getInternalFrame().getTitle());
902

    
903
            JDialog c = (JDialog) e.getSource();
904
            IWindow win = (IWindow) fws.getWindow(c);
905
            if (win != null) {
906

    
907
                WindowInfo wi = wis.getWindowInfo(win);
908

    
909
                callWindowClosed(win);
910
                boolean alwaysLive;
911
                if (win instanceof SingletonWindow) {
912
                    sws.closeWindow((SingletonWindow) win);
913
                }
914

    
915
                fws.closeWindow(win);
916

    
917
                panel.remove(c);
918

    
919
                wss.remove(win);
920

    
921
                if (!wi.isPalette())
922
                    mainFrame.enableControls();
923
                panel.repaint();
924
            }
925

    
926
            // Para activar el JInternalFrame desde la que hemos
927
            // abierto la ventana que estamos cerrando
928
            IWindow lastWindow = wss.getActiveWindow();
929
            // La activamos
930
            if (lastWindow != null) {
931
                // logger.debug(PluginServices.getText(this,
932
                // "Devuelvo_el_foco_a_")+lastWindow.getWindowInfo().getTitle());
933
                JDialog frame = (JDialog) fws.getFrame(lastWindow);
934
                frame.toFront();
935
            }
936
        }
937

    
938
        /**
939
         * @see java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent)
940
         */
941
        public void windowDeactivated(WindowEvent e) {
942
            JDialog frame = (JDialog) e.getSource();
943
            IWindow win = fws.getWindow(frame);
944
            if (win != null) {
945
                WindowInfo wi = wis.getWindowInfo(win);
946
                if (wi.isPalette())
947
                    return;
948
                wi.setSelectedTools(mainFrame.getSelectedTools());
949
                lastActiveWindow = win;
950
            }
951
        }
952

    
953
        /**
954
         * @see java.awt.event.WindowAdapter#windowDeiconified(java.awt.event.WindowEvent)
955
         */
956
        public void windowDeiconified(WindowEvent e) {
957
            mainFrame.enableControls();
958
        }
959

    
960
        /**
961
         * @see java.awt.event.WindowAdapter#windowGainedFocus(java.awt.event.WindowEvent)
962
         */
963
        public void windowGainedFocus(WindowEvent e) {
964

    
965
        }
966

    
967
        /**
968
         * @see java.awt.event.WindowAdapter#windowIconified(java.awt.event.WindowEvent)
969
         */
970
        public void windowIconified(WindowEvent e) {
971
            mainFrame.enableControls();
972
        }
973

    
974
        /**
975
         * @see java.awt.event.WindowAdapter#windowLostFocus(java.awt.event.WindowEvent)
976
         *      n
977
         */
978
        public void windowLostFocus(WindowEvent e) {
979

    
980
        }
981

    
982
        /**
983
         * @see java.awt.event.WindowAdapter#windowOpened(java.awt.event.WindowEvent)
984
         */
985
        public void windowOpened(WindowEvent e) {
986
        }
987

    
988
        /**
989
         * @see java.awt.event.WindowAdapter#windowStateChanged(java.awt.event.WindowEvent)
990
         */
991
        public void windowStateChanged(WindowEvent e) {
992

    
993
        }
994
    }
995

    
996
    /**
997
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
998
     *      java.lang.Object)
999
     */
1000
    public boolean closeSingletonWindow(Class viewClass, Object model) {
1001
        JDialog frame = (JDialog) sws.getFrame(viewClass, model);
1002
        if (frame == null)
1003
            return false;
1004
        closeJFrame(frame);
1005
        return true;
1006
    }
1007

    
1008
    /**
1009
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
1010
     */
1011
    public boolean closeSingletonWindow(Object model) {
1012
        JDialog[] frames = (JDialog[]) sws.getFrames(model);
1013
        if (frames.length == 0)
1014
            return false;
1015
        for (int i = 0; i < frames.length; i++) {
1016
            closeJFrame(frames[i]);
1017
        }
1018
        return true;
1019
    }
1020

    
1021
    /**
1022
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
1023
     */
1024
    public IWindow[] getAllWindows() {
1025
        ArrayList windows = new ArrayList();
1026
        Iterator i = fws.getWindowIterator();
1027

    
1028
        while (i.hasNext()) {
1029
            windows.add((IWindow) i.next());
1030
        }
1031
        return (IWindow[]) windows.toArray(new IWindow[0]);
1032
    }
1033

    
1034
    /**
1035
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
1036
     */
1037
    public IWindow[] getOrderedWindows() {
1038
        // ArrayList windows = new ArrayList();
1039
        TreeMap windows = new TreeMap();
1040
        Iterator winIterator = fws.getWindowIterator();
1041

    
1042
        Component frame;
1043
        IWindow win;
1044
        /**
1045
         * The order of the window in the JDesktopPane. Smaller numbers
1046
         * are closer to the foreground.
1047
         */
1048
        int zPosition = 0;
1049
        while (winIterator.hasNext()) {
1050
            win = (IWindow) winIterator.next();
1051
            // unfortunately we can't use this now, and I don't know how to
1052
            // retrieve the zPosition for JDialogs
1053
            // frame = fws.getComponent(win);
1054
            // zPosition = panel.getPosition(frame);
1055
            windows.put(new Integer(zPosition++), win);
1056
        }
1057
        winIterator = windows.values().iterator();
1058
        ArrayList winList = new ArrayList();
1059
        while (winIterator.hasNext()) {
1060
            winList.add(winIterator.next());
1061
        }
1062

    
1063
        return (IWindow[]) winList.toArray(new IWindow[0]);
1064
    }
1065

    
1066
    public void setMaximum(IWindow v, boolean bMaximum)
1067
        throws PropertyVetoException {
1068
        /*
1069
         * JDialog frame = fws.getFrame(v);
1070
         * 
1071
         * if (bMaximum) {
1072
         * 
1073
         * frame.setExtendedState(frame.getExtendedState() |
1074
         * JFrame.MAXIMIZED_BOTH);
1075
         * }
1076
         * else {
1077
         * int state = frame.getExtendedState();
1078
         * state = (state & (~JFrame.MAXIMIZED_BOTH));
1079
         * frame.setExtendedState(state);
1080
         * }
1081
         */
1082
    }
1083

    
1084
    public void changeWindowInfo(IWindow w, WindowInfo wi) {
1085
        JDialog frame = fws.getJDialog(w);
1086
        frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1087
        int width;
1088
        int height;
1089
        if (wi.getWidth() != -1)
1090
            width = wi.getWidth();
1091
        else
1092
            width = frame.getWidth();
1093
        if (wi.getHeight() != -1)
1094
            height = wi.getHeight();
1095
        else
1096
            height = frame.getHeight();
1097
        frame.setSize(new Dimension(width, height));
1098
        frame.setLocation(wi.getX(), wi.getY());
1099
        if (wi.isPalette()) {
1100
            // panel.add(frame, JDesktopPane.PALETTE_LAYER);
1101
            frame.setFocusable(false);
1102
        } else {
1103
            // panel.add(frame, JDesktopPane.DEFAULT_LAYER);
1104
            frame.setFocusable(true);
1105
            if (wi.isClosed()) {
1106
                closeWindow(w);
1107
            }
1108
        }
1109

    
1110
        if (wi.isMaximized()) {
1111
            frame.setBounds(wi.getNormalBounds());
1112
            // frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
1113
        }
1114
        activateFrame(frame);
1115
    }
1116

    
1117
    public void refresh(IWindow win) {
1118
        Component frame = fws.getFrame(win);
1119
        if (frame != null)
1120
            frame.setVisible(true);
1121
    }
1122

    
1123
    public void setBackgroundImage(ImageIcon image, String typeDesktop) {
1124
        this.image = image;
1125
        this.typeDesktop = typeDesktop;
1126

    
1127
    }
1128

    
1129
    class MyDesktopPane extends JDesktopPane {
1130

    
1131
        public MyDesktopPane() {
1132
        }
1133

    
1134
        public void paintComponent(Graphics g) {
1135
            super.paintComponent(g);
1136
            int x = 0;
1137
            int y = 0;
1138
            int w = 0;
1139
            int h = 0;
1140
            if (image != null) {
1141
                if (typeDesktop.equals(Theme.CENTERED)) {
1142
                    w = image.getIconWidth();
1143
                    h = image.getIconHeight();
1144
                    x = (getWidth() - w) / 2;
1145
                    y = (getHeight() - h) / 2;
1146
                    g.drawImage(image.getImage(), x, y, w, h, this);
1147
                } else
1148
                    if (typeDesktop.equals(Theme.EXPAND)) {
1149
                        w = getWidth();
1150
                        h = getHeight();
1151
                        g.drawImage(image.getImage(), x, y, w, h, this);
1152
                    } else
1153
                        if (typeDesktop.equals(Theme.MOSAIC)) {
1154
                            int wAux = image.getIconWidth();
1155
                            int hAux = image.getIconHeight();
1156
                            int i = 0;
1157
                            int j = 0;
1158
                            w = wAux;
1159
                            h = hAux;
1160
                            while (x < getWidth()) {
1161
                                x = wAux * i;
1162
                                while (y < getHeight()) {
1163
                                    y = hAux * j;
1164
                                    j++;
1165
                                    g.drawImage(image.getImage(), x, y, w, h,
1166
                                        this);
1167
                                }
1168
                                y = 0;
1169
                                j = 0;
1170
                                i++;
1171
                            }
1172
                        }
1173
            }
1174
        }
1175
    }
1176

    
1177
}
1178