Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCorePlugin / src / com / iver / core / mdiManager / NewSkin.java @ 7224

History | View | Annotate | Download (28 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.core.mdiManager;
42

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

    
60
import javax.swing.DefaultDesktopManager;
61
import javax.swing.DesktopManager;
62
import javax.swing.JDesktopPane;
63
import javax.swing.JDialog;
64
import javax.swing.JFrame;
65
import javax.swing.JInternalFrame;
66
import javax.swing.JRootPane;
67
import javax.swing.KeyStroke;
68
import javax.swing.event.InternalFrameEvent;
69
import javax.swing.event.InternalFrameListener;
70

    
71
import org.apache.log4j.Logger;
72

    
73
import com.iver.andami.GlobalKeyEventDispatcher;
74
import com.iver.andami.Launcher;
75
import com.iver.andami.PluginServices;
76
import com.iver.andami.plugins.Extension;
77
import com.iver.andami.ui.mdiFrame.GlassPane;
78
import com.iver.andami.ui.mdiFrame.MDIFrame;
79
import com.iver.andami.ui.mdiFrame.NewStatusBar;
80
import com.iver.andami.ui.mdiManager.MDIManager;
81
import com.iver.andami.ui.mdiManager.MDIUtilities;
82
import com.iver.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
83
import com.iver.andami.ui.mdiManager.SingletonWindow;
84
import com.iver.andami.ui.mdiManager.IWindow;
85
import com.iver.andami.ui.mdiManager.WindowInfo;
86
import com.iver.andami.ui.mdiManager.IWindowListener;
87
import com.iver.core.StatusBar;
88

    
89
/**
90
 *
91
 */
92
public class NewSkin extends Extension implements MDIManager{
93
        private static final int DefaultXMargin = 20; // Added for the method 'centreJInternalFrame'
94
        private static final int DefaultYMargin = 20; // Added for the method 'centreJInternalFrame'
95
        private static final int MinimumXMargin = 130; // Added for the method 'centreJInternalFrame'
96
        private static final int MinimumYMargin = 60; // Added for the method 'centreJInternalFrame'
97
        
98
        
99
    /**
100
     * Variable privada <code>desktopManager</code> para usarlo cuando sale
101
     * una ventana que no queremos que nos restaure las que tenemos maximizadas.
102
     * Justo antes de usar el setMaximize(false), le pegamos el cambiazo.
103
     */
104
    private static DesktopManager desktopManager = new DefaultDesktopManager();
105

    
106
    /** log */
107
    private static Logger logger = Logger.getLogger(NewSkin.class.getName());
108

    
109
    /** Panel de la MDIFrame */
110
    private JDesktopPane panel = new JDesktopPane();
111

    
112
    /** MDIFrame */
113
    private MDIFrame mainFrame;
114

    
115
    private GlassPane glassPane = new GlassPane();
116

    
117
    private DialogStackSupport dss;
118

    
119
    private FrameWindowSupport fvs;
120

    
121
    private WindowInfoSupport vis;
122

    
123
    private WindowStackSupport vss;
124

    
125
    private SingletonWindowSupport svs;
126

    
127
    private Cursor lastCursor = null;
128

    
129
    /**
130
     * @see com.iver.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
131
     */
132
    public void init(MDIFrame f) {
133
        // Inicializa el Frame y la consola
134
        mainFrame = f;
135
        mainFrame.setGlassPane(glassPane);
136
        panel.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
137

    
138
        mainFrame.getContentPane().add(panel, BorderLayout.CENTER);
139
        panel.setDesktopManager(desktopManager);
140

    
141
        fvs = new FrameWindowSupport(mainFrame);
142
        dss = new DialogStackSupport(mainFrame);
143
        svs = new SingletonWindowSupport(vis, fvs);
144
        vis = new WindowInfoSupport(mainFrame, fvs, svs);
145
        fvs.setVis(vis);
146
        vss = new WindowStackSupport(vis);
147

    
148

    
149
        // TODO (jaume) esto no deber?a de estar aqu?...
150
        // molar?a m?s en un di?logo de preferencias
151
        // es s?lo una prueba
152
        KeyStroke controlTab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.CTRL_MASK);
153

    
154
        PluginServices.registerKeyStroke(controlTab, new KeyEventDispatcher() {
155

    
156
                        public boolean dispatchKeyEvent(KeyEvent e) {
157
                                IWindow[] views = getAllWindows();
158
                                if (views.length<=0 || e.getID() == KeyEvent.KEY_PRESSED)
159
                                        return false;
160

    
161

    
162
                                int current = 0;
163
                                for (int i = 0; i < views.length; i++) {
164
                                        if (views[i].equals(getActiveWindow())) {
165
                                                current = i;
166
                                                break;
167
                                        }
168
                                }
169
                                addWindow(views[(current +1) % views.length]);
170
                                return true;
171
                        }
172

    
173
        });
174
    }
175

    
176
    /**
177
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
178
     */
179
    public IWindow addWindow(IWindow p) throws SingletonDialogAlreadyShownException {
180
        // se obtiene la informaci?n de la vista
181
        WindowInfo vi = vis.getWindowInfo(p);
182

    
183
        // Se comprueban las incompatibilidades que pudieran haber en la vista
184
        MDIUtilities.checkWindowInfo(vi);
185
        if ((p instanceof SingletonWindow) && (vi.isModal())) {
186
            throw new RuntimeException("A modal view cannot be a SingletonView");
187
        }
188

    
189
        /*
190
         * Se obtiene la referencia a la vista anterior por si es una singleton
191
         * y est? siendo mostrada. Se obtiene su informaci?n si ya fue mostrada
192
         */
193
        boolean singletonPreviouslyAdded = false;
194

    
195
        if (p instanceof SingletonWindow) {
196
            SingletonWindow sv = (SingletonWindow) p;
197
            if (svs.registerWindow(sv.getClass(), sv.getWindowModel(), vis
198
                    .getWindowInfo(sv))) {
199
                singletonPreviouslyAdded = true;
200
            }
201
        }
202

    
203
        if (singletonPreviouslyAdded) {
204
            // Si la vista no est? actualmente abierta
205
            if (!svs.contains((SingletonWindow) p)) {
206
                JInternalFrame frame = fvs.getJInternalFrame(p);
207
                svs.openSingletonView((SingletonWindow) p, frame);
208
                addJInternalFrame(frame, vi);
209
                vss.add(p, new ActionListener() {
210
                    public void actionPerformed(ActionEvent e) {
211
                        IWindow v = vis.getWindowById(Integer.parseInt(e
212
                                .getActionCommand()));
213
                        JInternalFrame f = fvs.getJInternalFrame(v);
214
                        activateJInternalFrame(f);
215
                    }
216
                });
217
                return p;
218
            } else {
219
                // La vista est? actualmente abierta
220
                JInternalFrame frame = (JInternalFrame) svs
221
                        .getFrame((SingletonWindow) p);
222
                activateJInternalFrame(frame);
223
                vss.setActive(p);
224
                return fvs.getWindow((JInternalFrame) frame);
225
            }
226
        } else {
227
            if (vi.isModal()) {
228
                addJDialog(p);
229
            } else {
230
                // Se sit?a la vista en la pila de vistas
231
                vss.add(p, new ActionListener() {
232
                    public void actionPerformed(ActionEvent e) {
233
                        IWindow v = vis.getWindowById(Integer.parseInt(e
234
                                .getActionCommand()));
235
                        JInternalFrame f = fvs.getJInternalFrame(v);
236
                        activateJInternalFrame(f);
237
                    }
238
                });
239
                addJInternalFrame(p);
240
            }
241

    
242
            return p;
243
        }
244
    }
245
    
246
    /**
247
     * Similar method as 'addView' but in this case centres the new JInternalFrame of the View in the contentPane of the MainFrame
248
     * 
249
     * @see com.iver.core.mdiManager.NewSkin#addWindow(com.iver.andami.ui.mdiManager.IWindow)
250
     * 
251
     * @author Pablo Piqueras Bartolom?
252
     */
253
        public IWindow addCentredWindow(IWindow p) throws SingletonDialogAlreadyShownException {
254
            // se obtiene la informaci?n de la vista
255
        WindowInfo vi = vis.getWindowInfo(p);
256

    
257
        // Se comprueban las incompatibilidades que pudieran haber en la vista
258
        MDIUtilities.checkWindowInfo(vi);
259
        if ((p instanceof SingletonWindow) && (vi.isModal())) {
260
            throw new RuntimeException("A modal view cannot be a SingletonView");
261
        }
262

    
263
        /*
264
         * Se obtiene la referencia a la vista anterior por si es una singleton
265
         * y est? siendo mostrada. Se obtiene su informaci?n si ya fue mostrada
266
         */
267
        boolean singletonPreviouslyAdded = false;
268

    
269
        if (p instanceof SingletonWindow) {
270
            SingletonWindow sv = (SingletonWindow) p;
271
            if (svs.registerWindow(sv.getClass(), sv.getWindowModel(), vis
272
                    .getWindowInfo(sv))) {
273
                singletonPreviouslyAdded = true;
274
            }
275
        }        
276
      
277
        if (singletonPreviouslyAdded) {
278
            // Si la vista no est? actualmente abierta
279
            if (!svs.contains((SingletonWindow) p)) {
280
                JInternalFrame frame = fvs.getJInternalFrame(p);
281
                svs.openSingletonView((SingletonWindow) p, frame);
282
                addJInternalFrame(frame, vi);
283
                vss.add(p, new ActionListener() {
284
                    public void actionPerformed(ActionEvent e) {
285
                        IWindow v = vis.getWindowById(Integer.parseInt(e
286
                                .getActionCommand()));
287
                        JInternalFrame f = fvs.getJInternalFrame(v);
288
                        centreJInternalFrame(f); // Centre this JInternalFrame
289
                        activateJInternalFrame(f);
290
                    }
291
                });
292
                return p;
293
            } else {
294
                // La vista est? actualmente abierta
295
                JInternalFrame frame = (JInternalFrame) svs
296
                        .getFrame((SingletonWindow) p);
297
                activateJInternalFrame(frame);
298
                centreJInternalFrame(frame); // Centre this JInternalFrame
299
                vss.setActive(p);
300
                return fvs.getWindow((JInternalFrame) frame);
301
            }
302
        } else {
303
                centreJInternalFrame(fvs.getJInternalFrame(p)); // Centre this JInternalFrame 
304
            if (vi.isModal()) {
305
                addJDialog(p);
306
            } else {
307
                // Se sit?a la vista en la pila de vistas
308
                vss.add(p, new ActionListener() {
309
                    public void actionPerformed(ActionEvent e) {
310
                        IWindow v = vis.getWindowById(Integer.parseInt(e
311
                                .getActionCommand()));
312
                        JInternalFrame f = fvs.getJInternalFrame(v);                        
313
                        activateJInternalFrame(f);
314
                    }
315
                });
316
                addJInternalFrame(p);
317
            }
318

    
319
            return p;
320
        }
321
        }
322
        
323
        /**
324
         * Centres the JInternalFrame in the center of the contentPane of the MainFrame
325
         * If it can't be showed completely the JInternalFrame, tries that the top-left corner of the JInternalFrame would be showed
326
         * 
327
         * @author Pablo Piqueras Bartolom?
328
         * 
329
         * @param jInternalFrame A reference to the frame to centring
330
         */
331
        private static synchronized void centreJInternalFrame(JInternalFrame jInternalFrame) {
332
                
333
                // The top-left square of frame reference
334
                Point newReferencePoint = new Point();
335
                
336
                // A reference to the panel where the JInternalFrame will be displayed
337
                Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
338
                
339
                // Get the NewStatusBar component
340
                NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
341
                JDesktopPane jDesktopPane = ((JDesktopPane)contentPane.getComponent(2));
342
                
343
                int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The last substraction is for if there is any menu,... at left
344
                int visibleHeight = contentPane.getHeight() - newStatusBar.getHeight() - contentPane.getY() - Math.abs(jDesktopPane.getY() - contentPane.getY()); // The last substraction is for if there is any menu,... at top
345
                int freeWidth = visibleWidth - jInternalFrame.getWidth();
346
                int freeHeight = visibleHeight - jInternalFrame.getHeight();
347

    
348
                // Calculate the new point reference (Assure that the top-left corner is showed)
349
                if (freeWidth < 0)
350
                {
351
                        if (visibleWidth > MinimumXMargin)
352
                                newReferencePoint.x = DefaultXMargin;
353
                        else
354
                                newReferencePoint.x = 0;
355
                }
356
                else
357
                        newReferencePoint.x = freeWidth / 2;
358
                        
359
                if (freeHeight < 0)
360
                {
361
                        if (visibleHeight > MinimumYMargin)
362
                                newReferencePoint.y = DefaultYMargin;
363
                        else
364
                                newReferencePoint.y = 0;
365
                }
366
                else
367
                        newReferencePoint.y = freeHeight / 2;
368

    
369
                // Set the new location for this JInternalFrame
370
                jInternalFrame.setLocation(newReferencePoint);
371
        }
372
        
373
        
374
    /**
375
     * DOCUMENT ME!
376
     *
377
     * @param wnd
378
     *            DOCUMENT ME!
379
     * @param vi
380
     *            DOCUMENT ME!
381
     */
382
    private void addJInternalFrame(JInternalFrame wnd, WindowInfo vi) {
383
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
384
        wnd.addInternalFrameListener(new FrameListener());
385

    
386
        if (vi.isModeless() || vi.isPalette()) {
387
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
388
            if (vi.isPalette())
389
                wnd.setFocusable(false);
390
        } else {
391
            panel.add(wnd);
392
        }
393

    
394
        activateJInternalFrame(wnd);
395
    }
396

    
397
    /**
398
     * DOCUMENT ME!
399
     *
400
     * @param p
401
     */
402
    private void addJInternalFrame(IWindow p) {
403
        WindowInfo vi = vis.getWindowInfo(p);
404

    
405
        JInternalFrame wnd = fvs.getJInternalFrame(p);
406

    
407
        if (p instanceof SingletonWindow) {
408
            SingletonWindow sv = (SingletonWindow) p;
409
            svs.openSingletonView(sv, wnd);
410
        }
411

    
412
        addJInternalFrame(wnd, vi);
413
    }
414

    
415
    /**
416
     * DOCUMENT ME!
417
     *
418
     * @param wnd
419
     */
420
    private void activateJInternalFrame(JInternalFrame wnd) {
421
        try {
422
            wnd.moveToFront();
423
            logger.debug("Activando " + wnd.getTitle());
424
            wnd.setSelected(true);
425
            wnd.setIcon(false);
426
        } catch (PropertyVetoException e) {
427
            logger.error(e);
428
        }
429
    }
430

    
431
    /**
432
     * Situa un di?logo modal en el centro de la pantalla
433
     *
434
     * @param d
435
     *            Di?logo que se quiere situar
436
     */
437
    private void centerDialog(JDialog d) {
438
        int offSetX = d.getWidth() / 2;
439
        int offSetY = d.getHeight() / 2;
440

    
441
        d.setLocation((mainFrame.getWidth() / 2) - offSetX, (mainFrame
442
                .getHeight() / 2)
443
                - offSetY);
444
    }
445

    
446
    /**
447
     * DOCUMENT ME!
448
     *
449
     * @param p
450
     */
451
    private void addJDialog(IWindow p) {
452
        JDialog dlg = fvs.getJDialog(p);
453

    
454
        centerDialog(dlg);
455

    
456
        dlg.addWindowListener(new DialogWindowListener());
457
        dss.pushDialog(dlg);
458

    
459
        dlg.setVisible(vis.getWindowInfo(p).isVisible());
460
    }
461

    
462
    /**
463
     * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveWindow()
464
     */
465
    public IWindow getActiveWindow() {
466
        JInternalFrame jif = panel.getSelectedFrame();
467

    
468
        if (jif != null) {
469
            IWindow theView = fvs.getWindow(jif);
470
            if (theView == null)
471
                return null;
472
            if (theView.getWindowInfo().isPalette())
473
                return vss.getActiveWindow();
474
            else
475
                return fvs.getWindow(jif);
476
        }
477
        // return vss.getActiveView();
478

    
479
        return null;
480
    }
481
    public IWindow getFocusWindow(){
482
             JInternalFrame jif = panel.getSelectedFrame();
483

    
484
         if (jif != null) {
485
             IWindow theView = fvs.getWindow(jif);
486
             if (theView == null)
487
                 return null;
488
             return fvs.getWindow(jif);
489
         }
490
         return null;
491
    }
492
    /**
493
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeWindow(com.iver.andami.ui.mdiManager.IWindow)
494
     */
495
    public void closeWindow(IWindow p) {
496
        // Si es un di?logo modal
497
        if (p.getWindowInfo().isModal()) {
498
            closeJDialog();
499
        } else { // Si no es modal se cierra el JInternalFrame
500
            closeJInternalFrame(fvs.getJInternalFrame(p));
501
        }
502
    }
503

    
504
    /**
505
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllWindows()
506
     */
507
    public void closeAllWindows() {
508
        ArrayList eliminar = new ArrayList();
509
        Iterator i = fvs.getWindowIterator();
510

    
511
        while (i.hasNext()) {
512
            eliminar.add((IWindow) i.next());
513
        }
514

    
515
        for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
516
            IWindow vista = (IWindow) iter.next();
517
            closeWindow(vista);
518
        }
519
    }
520

    
521
    /**
522
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
523
     */
524
    public WindowInfo getWindowInfo(IWindow v) {
525
            //ViewInfo vi = new ViewInfo();
526
            WindowInfo vi = vis.getWindowInfo(v);
527
            JInternalFrame f = fvs.getJInternalFrame(v);
528
            vi.setX(f.getX());
529
            vi.setY(f.getY());
530
            vi.setHeight(f.getHeight());
531
            vi.setWidth(f.getWidth());
532
            // isClosed() doesn't work as (I) expected, why? Using isShowing instead
533
            vi.setClosed(!f.isShowing());
534
            vi.setNormalBounds(f.getNormalBounds());
535
            vi.setMaximized(f.isMaximum());
536
            return vi;
537
    }
538

    
539
    /**
540
     * DOCUMENT ME!
541
     *
542
     * @param dialog
543
     * @throws RuntimeException
544
     *             DOCUMENT ME!
545
     */
546
    private void closeJDialog() {
547
        JDialog dlg = dss.popDialog();
548

    
549
        dlg.setVisible(false);
550

    
551
        IWindow s = (IWindow) fvs.getWindow(dlg);
552

    
553
        callWindowClosed(s);
554

    
555
        fvs.closeWindow(s);
556

    
557
        // Si es singleton se desasocia el modelo con la vista
558
        if (s instanceof SingletonWindow) {
559
            svs.closeView((SingletonWindow) s);
560
        }
561
    }
562

    
563
    /**
564
     * DOCUMENT ME!
565
     *
566
     * @param view
567
     *            DOCUMENT ME!
568
     */
569
    private void callWindowClosed(IWindow view) {
570
        if (view instanceof IWindowListener) {
571
            ((IWindowListener) view).windowClosed();
572
        }
573
    }
574

    
575
    /**
576
     * DOCUMENT ME!
577
     *
578
     * @param view
579
     *            DOCUMENT ME!
580
     */
581
    private void callWindowActivated(IWindow view) {
582
        logger.debug("View " + view.getWindowInfo().getTitle()
583
                + " activated (callViewActivated)");
584
        if (view instanceof IWindowListener) {
585
            ((IWindowListener) view).windowActivated();
586
        }
587
    }
588

    
589
    /**
590
     * DOCUMENT ME!
591
     *
592
     * @param frame
593
     */
594
    private void closeJInternalFrame(JInternalFrame frame) {
595
        try {
596
            IWindow s = (IWindow) fvs.getWindow(frame);
597

    
598
            frame.setClosed(true);
599
            callWindowClosed(s);
600
        } catch (PropertyVetoException e) {
601
            logger
602
                    .error(
603
                            "Not compatible with property veto's. Use ViewInfo instead.",
604
                            e);
605
        }
606
    }
607

    
608
    /**
609
     * @see com.iver.andami.plugins.IExtension#initialize()
610
     */
611
    public void initialize() {
612
    }
613

    
614
    /**
615
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
616
     */
617
    public void execute(String actionCommand) {
618
        if (actionCommand.equals("cascada")) {
619
        } else if (actionCommand.equals("mosaico")) {
620
        }
621
    }
622

    
623
    /**
624
     * @see com.iver.andami.plugins.IExtension#isEnabled()
625
     */
626
    public boolean isEnabled() {
627
        // TODO Auto-generated method stub
628
        return false;
629
    }
630

    
631
    /**
632
     * @see com.iver.andami.plugins.IExtension#isVisible()
633
     */
634
    public boolean isVisible() {
635
        // TODO Auto-generated method stub
636
        return true;
637
    }
638

    
639
    /**
640
     * @see com.iver.andami.ui.mdiManager.MDIManager#setWaitCursor()
641
     */
642
    public void setWaitCursor() {
643
        if (mainFrame != null) {
644
            glassPane.setVisible(true);
645
            lastCursor = mainFrame.getCursor();
646
            dss.setWaitCursor();
647
            glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
648
        }
649
    }
650

    
651
    /**
652
     * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
653
     */
654
    public void restoreCursor() {
655
        if (mainFrame != null) {
656
            glassPane.setVisible(false);
657
            dss.restoreCursor();
658
            glassPane.setCursor(lastCursor);
659
        }
660
    }
661

    
662
    /**
663
     * Listener para los eventos de cerrado de los di?logos. Tiene su raz?n de
664
     * ser en que los di?logos han de devolverse al pool cuando se cierran
665
     *
666
     * @author Fernando Gonz?lez Cort?s
667
     */
668
    public class DialogWindowListener extends WindowAdapter {
669
        /**
670
         * Captura el evento de cerrado de los di?logos con el fin de realizar
671
         * tareas de mantenimiento
672
         *
673
         * @param e
674
         *            evento
675
         */
676
        public void windowClosing(WindowEvent e) {
677
            closeJDialog();
678
        }
679
    }
680

    
681
    /**
682
     * DOCUMENT ME!
683
     */
684
    public class FrameListener implements InternalFrameListener {
685
        /**
686
         * @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
687
         */
688
        public void internalFrameActivated(InternalFrameEvent e) {
689
            // logger.debug("internalFrameActivated " +
690
            // e.getInternalFrame().getTitle());
691

    
692
            // activatedInternalFramesStack.push(e.getInternalFrame());
693

    
694
            IWindow panel = fvs.getWindow((JInternalFrame) e.getSource());
695

    
696
            WindowInfo vi = vis.getWindowInfo(panel);
697
            if (vi.isPalette())
698
                return;
699

    
700
            vss.setActive(panel);
701

    
702
            JInternalFrame frame = fvs.getJInternalFrame(panel);
703
            
704
            if (vi.isMaximizable()){
705
                    frame.setMaximizable(true);
706
            }
707
            if (!frame.isMaximizable() && frame.isMaximum()) {
708
                try {
709
                    frame.setMaximum(false);
710
                } catch (PropertyVetoException e1) {
711
                }
712
            }            
713
            mainFrame.enableControls();
714
            if (vi.getSelectedTool() != null)
715
                mainFrame.setSelectedTool(vi.getSelectedTool());
716
            callWindowActivated(panel);
717

    
718
        }
719

    
720
        /**
721
         * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
722
         */
723
        public void internalFrameClosed(InternalFrameEvent e) {
724
        }
725

    
726
        /**
727
         * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
728
         */
729
        public void internalFrameClosing(InternalFrameEvent e) {
730
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
731
            // logger.debug("internalFrameClosing " +
732
            // e.getInternalFrame().getTitle());
733

    
734
            JInternalFrame c = (JInternalFrame) e.getSource();
735
            WindowInfo vi = vis.getWindowInfo((IWindow) fvs.getWindow(c));
736

    
737
            IWindow view = fvs.getWindow(c);
738
            callWindowClosed(view);
739
            boolean alwaysLive;
740
            if (view instanceof SingletonWindow) {
741
                svs.closeView((SingletonWindow) view);
742
            }
743

    
744
            fvs.closeWindow(view);
745

    
746
            panel.remove(c);
747

    
748
            vss.remove(view);
749

    
750
            if (!vi.isPalette())
751
                mainFrame.enableControls();
752
            panel.repaint();
753

    
754
            // Para activar el JInternalFrame desde la que hemos
755
            // abierto la ventana que estamos cerrando
756
            IWindow lastView = vss.getActiveWindow();
757
            // La activamos
758
            if (lastView != null) {
759
                System.err.println("Devuelvo el foco a "
760
                        + lastView.getWindowInfo().getTitle());
761
                JInternalFrame frame = fvs.getJInternalFrame(lastView);
762
                try {
763
                    frame.setSelected(true);
764
                } catch (PropertyVetoException e1) {
765
                    // TODO Auto-generated catch block
766
                    // e1.printStackTrace();
767
                }
768
                // addView(lastView);
769
            }
770

    
771
        }
772

    
773
        /**
774
         * @see javax.swing.event.InternalFrameListener#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)
775
         */
776
        public void internalFrameDeactivated(InternalFrameEvent e) {
777
            // logger.debug("internalDeActivated " +
778
            // e.getInternalFrame().getTitle());
779
            JInternalFrame c = (JInternalFrame) e.getSource();
780
            IWindow andamiView = fvs.getWindow(c);
781
            if (andamiView != null) {
782
                WindowInfo vi = vis.getWindowInfo(andamiView);
783
                if (vi.isPalette())
784
                    return;
785
                vi.setSelectedTool(mainFrame.getSelectedTool());
786
            }
787

    
788
        }
789

    
790
        /**
791
         * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
792
         */
793
        public void internalFrameDeiconified(InternalFrameEvent e) {
794
            mainFrame.enableControls();
795
        }
796

    
797
        /**
798
         * @see javax.swing.event.InternalFrameListener#internalFrameIconified(javax.swing.event.InternalFrameEvent)
799
         */
800
        public void internalFrameIconified(InternalFrameEvent e) {
801
            mainFrame.enableControls();
802
        }
803

    
804
        /**
805
         * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
806
         */
807
        public void internalFrameOpened(InternalFrameEvent e) {
808
            // logger.debug("internalFrameOpened. Source= " +
809
            // e.getSource().toString());
810
        }
811
    }
812

    
813
    /**
814
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
815
     *      java.lang.Object)
816
     */
817
    public boolean closeSingletonWindow(Class viewClass, Object model) {
818
        JInternalFrame frame = svs.getFrame(viewClass, model);
819
        if (frame == null)
820
            return false;
821
        closeJInternalFrame(frame);
822
        return true;
823
    }
824

    
825
    /**
826
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
827
     */
828
    public boolean closeSingletonWindow(Object model) {
829
        JInternalFrame[] frames = svs.getFrames(model);
830
        if (frames.length == 0)
831
            return false;
832
        for (int i = 0; i < frames.length; i++) {
833
            closeJInternalFrame(frames[i]);
834
        }
835
        return true;
836
    }
837

    
838
    public IWindow[] getAllWindows() {
839
        ArrayList views = new ArrayList();
840
        Iterator i = fvs.getWindowIterator();
841

    
842
        while (i.hasNext()) {
843
            views.add((IWindow) i.next());
844
        }
845
        return (IWindow[]) views.toArray(new IWindow[0]);
846
    }
847

    
848
    public void setMaximum(IWindow v, boolean bMaximum) throws PropertyVetoException
849
    {
850
        JInternalFrame f = fvs.getJInternalFrame(v);
851
        f.setMaximum(bMaximum);
852
    }
853

    
854
    public void changeWindowInfo(IWindow v, WindowInfo vi){
855
            JInternalFrame f = fvs.getJInternalFrame(v);
856
                f.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
857
                JDesktopPane pnl = f.getDesktopPane();
858
                pnl.remove(f);
859
                f.setSize(new Dimension(vi.getWidth(),vi.getHeight()));
860
                f.setLocation(vi.getX(), vi.getY());
861
                if (vi.isPalette()) {
862
                        pnl.add(f, JDesktopPane.PALETTE_LAYER);
863
                        f.setFocusable(false);
864
                } else {
865
                        pnl.add(f, JDesktopPane.DEFAULT_LAYER);
866
                        f.setFocusable(true);
867
                        if (vi.isClosed()) {
868
                                closeWindow(v);
869
                        }
870
                }
871

    
872
                if (vi.isMaximized()) {
873
                        try {
874
                                f.setMaximum(true);
875
                        } catch (PropertyVetoException e) {
876
                                // TODO Auto-generated catch block
877
                                //e.printStackTrace();
878
                        }
879
                        f.setNormalBounds(vi.getNormalBounds());
880
                }
881
                activateJInternalFrame(f);
882
   }
883

    
884
}