Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libCorePlugin / src / org / gvsig / coreplugin / mdiManager / NewSkin.java @ 31344

History | View | Annotate | Download (30.8 KB)

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

    
62
import javax.swing.DefaultDesktopManager;
63
import javax.swing.DesktopManager;
64
import javax.swing.ImageIcon;
65
import javax.swing.JDesktopPane;
66
import javax.swing.JDialog;
67
import javax.swing.JFrame;
68
import javax.swing.JInternalFrame;
69
import javax.swing.JLayeredPane;
70
import javax.swing.JPopupMenu;
71
import javax.swing.KeyStroke;
72
import javax.swing.event.InternalFrameEvent;
73
import javax.swing.event.InternalFrameListener;
74

    
75
import org.gvsig.andami.PluginServices;
76
import org.gvsig.andami.plugins.Extension;
77
import org.gvsig.andami.ui.mdiFrame.GlassPane;
78
import org.gvsig.andami.ui.mdiFrame.MDIFrame;
79
import org.gvsig.andami.ui.mdiFrame.NewStatusBar;
80
import org.gvsig.andami.ui.mdiManager.IWindow;
81
import org.gvsig.andami.ui.mdiManager.IWindowListener;
82
import org.gvsig.andami.ui.mdiManager.MDIManager;
83
import org.gvsig.andami.ui.mdiManager.MDIUtilities;
84
import org.gvsig.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
85
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
86
import org.gvsig.andami.ui.mdiManager.WindowInfo;
87
import org.gvsig.andami.ui.theme.Theme;
88
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
90

    
91

    
92
/**
93
 *
94
 */
95
public class NewSkin extends Extension implements MDIManager{
96
        private static final int DefaultXMargin = 20; // Added for the method 'centreJInternalFrame'
97
        private static final int DefaultYMargin = 20; // Added for the method 'centreJInternalFrame'
98
        private static final int MinimumXMargin = 130; // Added for the method 'centreJInternalFrame'
99
        private static final int MinimumYMargin = 60; // Added for the method 'centreJInternalFrame'
100

    
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 = LoggerFactory.getLogger(NewSkin.class.getName());
111

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

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

    
118
    private GlassPane glassPane = new GlassPane();
119

    
120
    private DialogStackSupport dss;
121

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

    
127
    private WindowInfoSupport wis;
128

    
129
    private WindowStackSupport wss;
130

    
131
    private SingletonWindowSupport sws;
132

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

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

    
146
        mainFrame.getContentPane().add(panel, BorderLayout.CENTER);
147
        panel.setDesktopManager(desktopManager);
148

    
149
        fws = new FrameWindowSupport(mainFrame);
150
        dss = new DialogStackSupport(mainFrame);
151
        sws = new SingletonWindowSupport(wis, fws);
152
        wis = new WindowInfoSupport(mainFrame, fws, sws);
153
        fws.setVis(wis);
154
        wss = new WindowStackSupport(wis);
155

    
156

    
157
        // TODO (jaume) esto no deber?a de estar aqu?...
158
        // molar?a m?s en un di?logo de preferencias
159
        // es s?lo una prueba
160
        KeyStroke controlTab = 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

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

    
181
        });
182
    }
183

    
184
    /* (non-javadoc)
185
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
186
     */
187
    public IWindow addWindow(IWindow p) throws SingletonDialogAlreadyShownException {
188
        // se obtiene la informaci?n 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 est? siendo mostrada. Se obtiene su informaci?n 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 est? actualmente abierta
212
            if (!sws.contains((SingletonWindow) p)) {
213
                JInternalFrame frame = fws.getJInternalFrame(p);
214
                sws.openSingletonWindow((SingletonWindow) p, frame);
215
                addJInternalFrame(frame, wi);
216
                wss.add(p, new ActionListener() {
217
                    public void actionPerformed(ActionEvent e) {
218
                        IWindow v = wis.getWindowById(Integer.parseInt(e
219
                                .getActionCommand()));
220
                        JInternalFrame f = fws.getJInternalFrame(v);
221
                        activateJInternalFrame(f);
222
                    }
223
                });
224
                return p;
225
            } else {
226
                // La vista est? actualmente abierta
227
                JInternalFrame frame = (JInternalFrame) sws
228
                        .getFrame((SingletonWindow) p);
229
                activateJInternalFrame(frame);
230
                wss.setActive(p);
231
                return fws.getWindow((JInternalFrame) frame);
232
            }
233
        } else {
234
            if (wi.isModal()) {
235
                addJDialog(p);
236
            } else {
237
                // Se sit?a la vista en la pila de vistas
238
                wss.add(p, new ActionListener() {
239
                    public void actionPerformed(ActionEvent e) {
240
                        IWindow v = wis.getWindowById(Integer.parseInt(e
241
                                .getActionCommand()));
242
                        JInternalFrame f = fws.getJInternalFrame(v);
243
                        activateJInternalFrame(f);
244
                    }
245
                });
246
                addJInternalFrame(p);
247
            }
248

    
249
            return p;
250
        }
251
    }
252

    
253
    public IWindow getSingletonWindow(Class windowClass, Object model) {
254
        JInternalFrame frame = (JInternalFrame) sws.getFrame(windowClass,model);
255
        if( frame == null ) {
256
                return null;
257
        }
258
        return fws.getWindow((JInternalFrame) frame);
259
     }
260

    
261
    
262
    
263
    /* (non-javadoc)
264
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
265
     */
266
        public IWindow addCentredWindow(IWindow p) throws SingletonDialogAlreadyShownException {
267
                IWindow window = addWindow(p);
268
                centreFrame(window);
269
                return window;
270
        }
271

    
272

    
273
        /**
274
         * Centres the Frame in the contentPane of the MainFrame.
275
         * If the frame can't be showed completely, it tries to show its top-left
276
         * corner.
277
         *
278
         * @author Pablo Piqueras Bartolom?
279
         *
280
         * @param panel The IWindow to centre
281
         */
282
        public synchronized void centreFrame(IWindow panel) {
283
                Component window = fws.getFrame(panel);
284
                if (window==null) return;
285

    
286
                // The top-left square of frame reference
287
                Point newReferencePoint = new Point();
288

    
289
                // A reference to the panel where the JInternalFrame will be displayed
290
                Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
291

    
292
                // Get the NewStatusBar component
293
                NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
294
                JDesktopPane jDesktopPane = ((JDesktopPane)contentPane.getComponent(2));
295

    
296
                int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The last substraction is for if there is any menu,... at left
297
                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
298
                int freeWidth = visibleWidth - window.getWidth();
299
                int freeHeight = visibleHeight - window.getHeight();
300

    
301
                // Calculate the new point reference (Assure that the top-left corner is showed)
302
                if (freeWidth < 0)
303
                {
304
                        if (visibleWidth > MinimumXMargin)
305
                                newReferencePoint.x = DefaultXMargin;
306
                        else
307
                                newReferencePoint.x = 0;
308
                }
309
                else
310
                        newReferencePoint.x = freeWidth / 2;
311

    
312
                if (freeHeight < 0)
313
                {
314
                        if (visibleHeight > MinimumYMargin)
315
                                newReferencePoint.y = DefaultYMargin;
316
                        else
317
                                newReferencePoint.y = 0;
318
                }
319
                else
320
                        newReferencePoint.y = freeHeight / 2;
321

    
322
                // Set the new location for this JInternalFrame
323
                window.setLocation(newReferencePoint);
324
        }
325

    
326

    
327
    /**
328
     * DOCUMENT ME!
329
     *
330
     * @param wnd
331
     *            DOCUMENT ME!
332
     * @param wi
333
     *            DOCUMENT ME!
334
     */
335
    private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
336
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
337
        wnd.addInternalFrameListener(new FrameListener());
338

    
339
        if (wi.isModeless() || wi.isPalette()) {
340
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
341
            if (wi.isPalette())
342
                wnd.setFocusable(false);
343
        } else {
344
            panel.add(wnd);
345
        }
346
        updateFrameProperties(wnd, wi);
347
        activateJInternalFrame(wnd);
348
        try{
349
                wnd.setMaximum(wi.isMaximized());
350
        }catch(Exception ex){
351
                logger.warn("Error: ", ex);
352
        }
353
    }
354

    
355
    private void updateFrameProperties(JInternalFrame frame, WindowInfo wi) {
356
            int height, width;
357
            if (wi.isMaximized()) {
358
                    if (wi.getNormalWidth()!=-1)
359
                            width = wi.getNormalWidth();
360
                    else
361
                            width = frame.getNormalBounds().width;
362
                    if (wi.getNormalHeight()!=-1)
363
                            height = wi.getNormalHeight();
364
                    else
365
                            height = frame.getNormalBounds().height;
366

    
367
                    frame.setSize(width, height);
368
                    frame.setLocation(wi.getNormalX(), wi.getNormalY());
369
            }
370
            else {
371
                    if (wi.getWidth()!=-1)
372
                            width = wi.getWidth();
373
                    else
374
                            width = frame.getWidth();
375
                    if (wi.getHeight()!=-1)
376
                            height = wi.getHeight();
377
                    else
378
                            height = frame.getHeight();
379
                    frame.setSize(width, height);
380
                frame.setLocation(wi.getX(), wi.getY());
381
            }
382
            frame.setTitle(wi.getTitle());
383
            frame.setVisible(wi.isVisible());
384
            frame.setResizable(wi.isResizable());
385
            frame.setIconifiable(wi.isIconifiable());
386
            frame.setMaximizable(wi.isMaximizable());
387
            try {
388
                        frame.setMaximum(wi.isMaximized());
389
                } catch (PropertyVetoException e) {
390
                        // TODO Auto-generated catch block
391
                        //e.printStackTrace();
392
                }
393
    }
394

    
395
    /**
396
     * DOCUMENT ME!
397
     *
398
     * @param p
399
     */
400
    private void addJInternalFrame(IWindow p) {
401
        WindowInfo wi = wis.getWindowInfo(p);
402

    
403
        JInternalFrame wnd = fws.getJInternalFrame(p);
404

    
405
        if (p instanceof SingletonWindow) {
406
            SingletonWindow sv = (SingletonWindow) p;
407
            sws.openSingletonWindow(sv, wnd);
408
        }
409

    
410
        addJInternalFrame(wnd, wi);
411
    }
412

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

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

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

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

    
453
        centerDialog(dlg);
454

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

    
458
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
459
    }
460

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

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

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

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

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

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

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

    
520
    /*
521
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
522
     */
523
    public WindowInfo getWindowInfo(IWindow w) {
524
            WindowInfo wi = wis.getWindowInfo(w);
525

    
526
    /*
527
     * This is done now in vis.getWindowInfo(w)
528
     *
529
     * JInternalFrame f = fws.getJInternalFrame(w);
530
            wi.setX(f.getX());
531
            wi.setY(f.getY());
532
            wi.setHeight(f.getHeight());
533
            wi.setWidth(f.getWidth());
534
            // isClosed() doesn't work as (I) expected, why? Using isShowing instead
535
            wi.setClosed(!f.isShowing());
536
            wi.setNormalBounds(f.getNormalBounds());
537
            wi.setMaximized(f.isMaximum());*/
538
            return wi;
539
    }
540

    
541
    /**
542
     * DOCUMENT ME!
543
     *
544
     * @param dialog
545
     * @throws RuntimeException
546
     *             DOCUMENT ME!
547
     */
548
    private void closeJDialog() {
549
        JDialog dlg = dss.popDialog();
550
        if (dlg==null)
551
                return;
552
        dlg.setVisible(false);
553

    
554
        IWindow s = fws.getWindow(dlg);
555

    
556
        callWindowClosed(s);
557

    
558
        fws.closeWindow(s);
559

    
560
        // Si es singleton se desasocia el modelo con la vista
561
        if (s instanceof SingletonWindow) {
562
            sws.closeWindow((SingletonWindow) s);
563
        }
564
    }
565

    
566
    /**
567
     * If <code>window</code> implements IWindowListener, sent it
568
     * the windowActivated event.
569
     *
570
     * @param window
571
     *            The IWindow which has to be notified.
572
     */
573
    private void callWindowClosed(IWindow window) {
574
        if (window instanceof IWindowListener) {
575
            ((IWindowListener) window).windowClosed();
576
        }
577
    }
578

    
579
    /**
580
     * If <code>window</code> implements IWindowListener, sent it
581
     * the windowActivated event.
582
     *
583
     * @param window
584
     *            The IWindow which has to be notified.
585
     */
586
    private void callWindowActivated(IWindow window) {
587
//        logger.debug("View '" + window.getWindowInfo().getTitle()
588
//               + "' activated (callViewActivated)");
589
        if (window instanceof IWindowListener) {
590
            ((IWindowListener) window).windowActivated();
591
        }
592
    }
593

    
594
    /**
595
     * DOCUMENT ME!
596
     *
597
     * @param frame
598
     */
599
    private void closeJInternalFrame(JInternalFrame frame) {
600
        try {
601
            IWindow s = (IWindow) fws.getWindow(frame);
602

    
603
            frame.setClosed(true);
604
            callWindowClosed(s);
605
        } catch (PropertyVetoException e) {
606
            logger
607
                    .error(
608
                            "Not compatible with property veto's. Use ViewInfo instead.",
609
                            e);
610
        }
611
    }
612

    
613
    /*
614
     * @see com.iver.andami.plugins.IExtension#initialize()
615
     */
616
    public void initialize() {
617
    }
618

    
619
    /*
620
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
621
     */
622
    public void execute(String actionCommand) {
623
        if (actionCommand.equals("cascada")) {
624
        } else if (actionCommand.equals("mosaico")) {
625
        }
626
    }
627

    
628
    /*
629
     * @see com.iver.andami.plugins.IExtension#isEnabled()
630
     */
631
    public boolean isEnabled() {
632
        // TODO Auto-generated method stub
633
        return false;
634
    }
635

    
636
    /*
637
     * @see com.iver.andami.plugins.IExtension#isVisible()
638
     */
639
    public boolean isVisible() {
640
        // TODO Auto-generated method stub
641
        return true;
642
    }
643

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

    
656
    /*
657
     * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
658
     */
659
    public void restoreCursor() {
660
        if (mainFrame != null) {
661
            glassPane.setVisible(false);
662
            dss.restoreCursor();
663
            glassPane.setCursor(lastCursor);
664
        }
665
    }
666

    
667
    /**
668
     * Listener para los eventos de cerrado de los di?logos. Tiene su raz?n de
669
     * ser en que los di?logos han de devolverse al pool cuando se cierran
670
     *
671
     * @author Fernando Gonz?lez Cort?s
672
     */
673
    public class DialogWindowListener extends WindowAdapter {
674
        /**
675
         * Captura el evento de cerrado de los di?logos con el fin de realizar
676
         * tareas de mantenimiento
677
         *
678
         * @param e
679
         *            evento
680
         */
681
        public void windowActivated(WindowEvent e) {
682
                IWindow window = fws.getWindow((Component) e.getSource());
683
                callWindowActivated(window);
684

    
685
        }
686

    
687

    
688
        /**
689
         * Captura el evento de cerrado de los di?logos con el fin de realizar
690
         * tareas de mantenimiento
691
         *
692
         * @param e
693
         *            evento
694
         */
695
        public void windowClosing(WindowEvent e) {
696
            closeJDialog();
697
        }
698
    }
699

    
700
    /**
701
     * DOCUMENT ME!
702
     */
703
    public class FrameListener implements InternalFrameListener {
704
        /*
705
         * @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
706
         */
707
        public void internalFrameActivated(InternalFrameEvent e) {
708
            // logger.debug("internalFrameActivated " +
709
            // e.getInternalFrame().getTitle());
710

    
711
            // activatedInternalFramesStack.push(e.getInternalFrame());
712

    
713
            IWindow panel = fws.getWindow((JInternalFrame) e.getSource());
714

    
715
            WindowInfo wi = wis.getWindowInfo(panel);
716
            if (wi.isPalette())
717
                return;
718

    
719
            wss.setActive(panel);
720

    
721
            JInternalFrame frame = fws.getJInternalFrame(panel);
722

    
723
            if (wi.isMaximizable()){
724
                    frame.setMaximizable(true);
725
            }
726
            if (!frame.isMaximizable() && frame.isMaximum()) {
727
                try {
728
                    frame.setMaximum(false);
729
                } catch (PropertyVetoException e1) {
730
                }
731
            }
732
            mainFrame.enableControls();
733
            if (wi.getSelectedTools()==null) {
734
                    // this is the first time this window is activated
735
                    wi.setSelectedTools(new HashMap(mainFrame.getInitialSelectedTools()));
736
            }
737
            mainFrame.setSelectedTools(wi.getSelectedTools());
738
            callWindowActivated(panel);
739

    
740
        }
741

    
742
        /*
743
         * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
744
         */
745
        public void internalFrameClosed(InternalFrameEvent e) {
746
        }
747

    
748
        /*
749
         * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
750
         */
751
        public void internalFrameClosing(InternalFrameEvent e) {
752
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
753
            // logger.debug("internalFrameClosing " +
754
            // e.getInternalFrame().getTitle());
755

    
756
            JInternalFrame c = (JInternalFrame) e.getSource();
757
            WindowInfo wi = wis.getWindowInfo((IWindow) fws.getWindow(c));
758

    
759
            IWindow win = fws.getWindow(c);
760
            callWindowClosed(win);
761
            boolean alwaysLive;
762
            if (win instanceof SingletonWindow) {
763
                sws.closeWindow((SingletonWindow) win);
764
            }
765

    
766
            fws.closeWindow(win);
767

    
768
            panel.remove(c);
769

    
770
            wss.remove(win);
771

    
772
            if (!wi.isPalette())
773
                mainFrame.enableControls();
774
            panel.repaint();
775

    
776
            // Para activar el JInternalFrame desde la que hemos
777
            // abierto la ventana que estamos cerrando
778
            IWindow lastWindow = wss.getActiveWindow();
779
            // La activamos
780
            if (lastWindow != null) {
781
                    logger.debug(PluginServices.getText(this, "Devuelvo_el_foco_a_")+lastWindow.getWindowInfo().getTitle());
782
                JInternalFrame frame = fws.getJInternalFrame(lastWindow);
783
                try {
784
                    frame.setSelected(true);
785
                } catch (PropertyVetoException e1) {
786
                    // TODO Auto-generated catch block
787
                    // e1.printStackTrace();
788
                }
789
                // addView(lastView);
790
            }
791

    
792
        }
793

    
794
        /*
795
         * @see javax.swing.event.InternalFrameListener#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)
796
         */
797
        public void internalFrameDeactivated(InternalFrameEvent e) {
798
            // logger.debug("internalDeActivated " +
799
            // e.getInternalFrame().getTitle());
800
            JInternalFrame c = (JInternalFrame) e.getSource();
801
            IWindow win = fws.getWindow(c);
802
            if (win != null) {
803
                WindowInfo wi = wis.getWindowInfo(win);
804
                if (wi.isPalette())
805
                    return;
806

    
807
            }
808

    
809
        }
810

    
811
        /*
812
         * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
813
         */
814
        public void internalFrameDeiconified(InternalFrameEvent e) {
815
            mainFrame.enableControls();
816
        }
817

    
818
        /*
819
         * @see javax.swing.event.InternalFrameListener#internalFrameIconified(javax.swing.event.InternalFrameEvent)
820
         */
821
        public void internalFrameIconified(InternalFrameEvent e) {
822
            mainFrame.enableControls();
823
        }
824

    
825
        /*
826
         * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
827
         */
828
        public void internalFrameOpened(InternalFrameEvent e) {
829
            // logger.debug("internalFrameOpened. Source= " +
830
            // e.getSource().toString());
831
        }
832
    }
833

    
834
    /*
835
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
836
     *      java.lang.Object)
837
     */
838
    public boolean closeSingletonWindow(Class viewClass, Object model) {
839
        JInternalFrame frame = (JInternalFrame) sws.getFrame(viewClass, model);
840
        if (frame == null)
841
            return false;
842
        closeJInternalFrame(frame);
843
        return true;
844
    }
845

    
846
    /*
847
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
848
     */
849
    public boolean closeSingletonWindow(Object model) {
850
        JInternalFrame[] frames = (JInternalFrame[]) sws.getFrames(model);
851
        if (frames.length == 0)
852
            return false;
853
        for (int i = 0; i < frames.length; i++) {
854
            closeJInternalFrame(frames[i]);
855
        }
856
        return true;
857
    }
858

    
859
    /*
860
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
861
     */
862
    public IWindow[] getAllWindows() {
863
        ArrayList windows = new ArrayList();
864
        Iterator i = fws.getWindowIterator();
865

    
866
        while (i.hasNext()) {
867
            windows.add((IWindow) i.next());
868
        }
869
        return (IWindow[]) windows.toArray(new IWindow[0]);
870
    }
871

    
872
    /*
873
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
874
     */
875
    public IWindow[] getOrderedWindows() {
876
        TreeMap windows = new TreeMap();
877
        Iterator winIterator = fws.getWindowIterator();
878

    
879
        Component frame;
880
        IWindow win;
881
        /**
882
         * The order of the window in the JDesktopPane. Smaller numbers
883
         * are closer to the foreground.
884
         */
885
        int zPosition;
886
        while (winIterator.hasNext()) {
887
                win = (IWindow) winIterator.next();
888
                frame = fws.getFrame(win);
889
                    zPosition = panel.getPosition(frame);
890
                    int layer = panel.getLayer(frame);
891

    
892
                    if (! (frame instanceof JDialog)) { //JDialogs are not in inside the LayeredPane
893
                            // flatten all the layers
894
                            if (layer == JLayeredPane.DEFAULT_LAYER.intValue()) {
895
                                    zPosition+=50000;
896
                            }
897
                            else if (layer == JLayeredPane.PALETTE_LAYER.intValue()) {
898
                                    zPosition+=40000;
899
                            }
900
                            else if (layer == JLayeredPane.MODAL_LAYER.intValue()) {
901
                                    zPosition+=30000;
902
                            }
903
                            else if  (layer == JLayeredPane.POPUP_LAYER.intValue()) {
904
                                    zPosition+=20000;
905
                            }
906
                            else if  (layer == JLayeredPane.DRAG_LAYER.intValue()) {
907
                                    zPosition+=10000;
908
                            }
909
                    }
910
                    windows.put(new Integer(zPosition), win);
911
        }
912
        winIterator = windows.values().iterator();
913
        ArrayList winList = new ArrayList();
914
        while (winIterator.hasNext()) {
915
                winList.add(winIterator.next());
916
        }
917

    
918
        return (IWindow[]) winList.toArray(new IWindow[0]);
919
    }
920
    public void setMaximum(IWindow v, boolean bMaximum) throws PropertyVetoException
921
    {
922
        JInternalFrame f = fws.getJInternalFrame(v);
923
        f.setMaximum(bMaximum);
924
    }
925

    
926
    public void changeWindowInfo(IWindow w, WindowInfo wi){
927
            JInternalFrame f = fws.getJInternalFrame(w);
928
            f.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
929
            JDesktopPane pnl = f.getDesktopPane();
930
            pnl.remove(f);
931
            int width;
932
            int height;
933
            if (wi.getWidth()!=-1)
934
                    width = wi.getWidth();
935
            else
936
                    width = f.getWidth();
937
            if (wi.getHeight()!=-1)
938
                    height = wi.getHeight();
939
            else
940
                    height = f.getHeight();
941
            f.setSize(new Dimension(width, height));
942
            f.setLocation(wi.getX(), wi.getY());
943
            if (wi.isPalette()) {
944
                    pnl.add(f, JDesktopPane.PALETTE_LAYER);
945
                    f.setFocusable(false);
946
            } else {
947
                    pnl.add(f, JDesktopPane.DEFAULT_LAYER);
948
                    f.setFocusable(true);
949
                    if (wi.isClosed()) {
950
                            closeWindow(w);
951
                    }
952
            }
953

    
954
            if (wi.isMaximized()) {
955
                    try {
956
                            f.setMaximum(true);
957
                    } catch (PropertyVetoException e) {
958
                            // TODO Auto-generated catch block
959
                            //e.printStackTrace();
960
                    }
961
                    f.setNormalBounds(wi.getNormalBounds());
962
            }
963
            activateJInternalFrame(f);
964
    }
965

    
966
    public void refresh(IWindow win) {
967
            Component frame = fws.getFrame(win);
968
            if (frame!=null)
969
                    frame.setVisible(true);
970
    }
971

    
972
        public void setBackgroundImage(ImageIcon image, String typeDesktop) {
973
                this.image=image;
974
                this.typeDesktop=typeDesktop;
975

    
976
        }
977
        class MyDesktopPane extends JDesktopPane
978
            {
979
              public MyDesktopPane(){
980
              }
981
              public void paintComponent(Graphics g){
982
                super.paintComponent(g);
983
                int x=0;
984
                int y=0;
985
                int w=0;
986
                int h=0;
987
                if (image != null) {
988
                                if (typeDesktop.equals(Theme.CENTERED)) {
989
                                        w = image.getIconWidth();
990
                                        h = image.getIconHeight();
991
                                        x = (getWidth() - w) / 2;
992
                                        y = (getHeight() - h) / 2;
993
                                        g.drawImage(image.getImage(), x, y, w, h, this);
994
                                } else if (typeDesktop.equals(Theme.EXPAND)) {
995
                                        w = getWidth();
996
                                        h = getHeight();
997
                                        g.drawImage(image.getImage(), x, y, w, h, this);
998
                                } else if (typeDesktop.equals(Theme.MOSAIC)) {
999
                                        int wAux = image.getIconWidth();
1000
                                        int hAux = image.getIconHeight();
1001
                                        int i = 0;
1002
                                        int j = 0;
1003
                                        w = wAux;
1004
                                        h = hAux;
1005
                                        while (x < getWidth()) {
1006
                                                x = wAux * i;
1007
                                                while (y < getHeight()) {
1008
                                                        y = hAux * j;
1009
                                                        j++;
1010
                                                        g.drawImage(image.getImage(), x, y, w, h, this);
1011
                                                }
1012
                                                y = 0;
1013
                                                j = 0;
1014
                                                i++;
1015
                                        }
1016
                                }
1017
                        }
1018
              }
1019
            }
1020
}