Statistics
| Revision:

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

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 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.Frame;
49
import java.awt.Graphics;
50
import java.awt.KeyEventDispatcher;
51
import java.awt.Point;
52
import java.awt.event.ActionEvent;
53
import java.awt.event.ActionListener;
54
import java.awt.event.KeyEvent;
55
import java.awt.event.WindowAdapter;
56
import java.awt.event.WindowEvent;
57
import java.beans.PropertyVetoException;
58
import java.util.ArrayList;
59
import java.util.HashMap;
60
import java.util.Iterator;
61
import java.util.TreeMap;
62

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

    
77
import org.apache.log4j.Logger;
78

    
79
import com.iver.andami.PluginServices;
80
import com.iver.andami.plugins.Extension;
81
import com.iver.andami.ui.mdiFrame.GlassPane;
82
import com.iver.andami.ui.mdiFrame.MDIFrame;
83
import com.iver.andami.ui.mdiFrame.NewStatusBar;
84
import com.iver.andami.ui.mdiManager.IWindow;
85
import com.iver.andami.ui.mdiManager.IWindowListener;
86
import com.iver.andami.ui.mdiManager.MDIManager;
87
import com.iver.andami.ui.mdiManager.MDIUtilities;
88
import com.iver.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
89
import com.iver.andami.ui.mdiManager.SingletonWindow;
90
import com.iver.andami.ui.mdiManager.WindowInfo;
91
import com.iver.andami.ui.theme.Theme;
92

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

    
102

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

    
110
    /** log */
111
    private static Logger logger = Logger.getLogger(NewSkin.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 DialogStackSupport dss;
122

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

    
128
    private WindowInfoSupport wis;
129

    
130
    private WindowStackSupport wss;
131

    
132
    private SingletonWindowSupport sws;
133

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

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

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

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

    
157

    
158
        // TODO (jaume) esto no deber?a de estar aqu?...
159
        // molar?a m?s en un di?logo de preferencias
160
        // es s?lo una prueba
161
        KeyStroke controlTab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.CTRL_MASK);
162

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

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

    
170

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

    
182
        });
183
    }
184

    
185
    /* (non-javadoc)
186
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
187
     */
188
    public IWindow addWindow(IWindow p) throws SingletonDialogAlreadyShownException {
189
        // se obtiene la informaci?n de la vista
190
        WindowInfo wi = wis.getWindowInfo(p);
191

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

    
198
        /*
199
         * Se obtiene la referencia a la vista anterior por si es una singleton
200
         * y est? siendo mostrada. Se obtiene su informaci?n si ya fue mostrada
201
         */
202
        boolean singletonPreviouslyAdded = false;
203

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

    
211
        if (singletonPreviouslyAdded) {
212
            // Si la vista no est? actualmente abierta
213
            if (!sws.contains((SingletonWindow) p)) {
214
                JInternalFrame frame = fws.getJInternalFrame(p);
215
                sws.openSingletonWindow((SingletonWindow) p, frame);
216
                addJInternalFrame(frame, wi);
217
                wss.add(p, new ActionListener() {
218
                    public void actionPerformed(ActionEvent e) {
219
                        IWindow v = wis.getWindowById(Integer.parseInt(e
220
                                .getActionCommand()));
221
                        JInternalFrame f = fws.getJInternalFrame(v);
222
                        activateJInternalFrame(f);
223
                    }
224
                });
225
                return p;
226
            } else {
227
                // La vista est? actualmente abierta
228
                JInternalFrame frame = (JInternalFrame) sws
229
                        .getFrame((SingletonWindow) p);
230
                activateJInternalFrame(frame);
231
                wss.setActive(p);
232
                return fws.getWindow((JInternalFrame) frame);
233
            }
234
        } else {
235
            if (wi.isModal()) {
236
                addJDialog(p);
237
            } else {
238
                // Se sit?a la vista en la pila de vistas
239
                wss.add(p, new ActionListener() {
240
                    public void actionPerformed(ActionEvent e) {
241
                        IWindow v = wis.getWindowById(Integer.parseInt(e
242
                                .getActionCommand()));
243
                        JInternalFrame f = fws.getJInternalFrame(v);
244
                        activateJInternalFrame(f);
245
                    }
246
                });
247
                addJInternalFrame(p);
248
            }
249

    
250
            return p;
251
        }
252
    }
253

    
254
    /* (non-javadoc)
255
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
256
     */
257
        public IWindow addCentredWindow(IWindow p) throws SingletonDialogAlreadyShownException {
258
                IWindow window = addWindow(p);
259
                centreFrame(window);
260
                return window;
261
        }
262

    
263

    
264
        /**
265
         * Centres the Frame in the contentPane of the MainFrame.
266
         * If the frame can't be showed completely, it tries to show its top-left
267
         * corner.
268
         *
269
         * @author Pablo Piqueras Bartolom?
270
         *
271
         * @param panel The IWindow to centre
272
         */
273
        public synchronized void centreFrame(IWindow panel) {
274
                Component window = fws.getFrame(panel);
275
                if (window==null) return;
276

    
277
                // The top-left square of frame reference
278
                Point newReferencePoint = new Point();
279

    
280
                // A reference to the panel where the JInternalFrame will be displayed
281
                Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
282

    
283
                // Get the NewStatusBar component
284
                NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
285
                JDesktopPane jDesktopPane = ((JDesktopPane)contentPane.getComponent(2));
286

    
287
                int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The last substraction is for if there is any menu,... at left
288
                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
289
                int freeWidth = visibleWidth - window.getWidth();
290
                int freeHeight = visibleHeight - window.getHeight();
291

    
292
                // Calculate the new point reference (Assure that the top-left corner is showed)
293
                if (freeWidth < 0)
294
                {
295
                        if (visibleWidth > MinimumXMargin)
296
                                newReferencePoint.x = DefaultXMargin;
297
                        else
298
                                newReferencePoint.x = 0;
299
                }
300
                else
301
                        newReferencePoint.x = freeWidth / 2;
302

    
303
                if (freeHeight < 0)
304
                {
305
                        if (visibleHeight > MinimumYMargin)
306
                                newReferencePoint.y = DefaultYMargin;
307
                        else
308
                                newReferencePoint.y = 0;
309
                }
310
                else
311
                        newReferencePoint.y = freeHeight / 2;
312

    
313
                // Set the new location for this JInternalFrame
314
                window.setLocation(newReferencePoint);
315
        }
316

    
317

    
318
    /**
319
     * DOCUMENT ME!
320
     *
321
     * @param wnd
322
     *            DOCUMENT ME!
323
     * @param wi
324
     *            DOCUMENT ME!
325
     */
326
    private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
327
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
328
        wnd.addInternalFrameListener(new FrameListener());
329

    
330
        if (wi.isModeless() || wi.isPalette()) {
331
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
332
            if (wi.isPalette())
333
                wnd.setFocusable(false);
334
        } else {
335
            panel.add(wnd);
336
        }
337
        updateFrameProperties(wnd, wi);
338
        activateJInternalFrame(wnd);
339
        try{
340
                wnd.setMaximum(wi.isMaximized());
341
        }catch(Exception ex){
342
                logger.warn("Error: ", ex);
343
        }
344
    }
345

    
346
    private void updateFrameProperties(JInternalFrame frame, WindowInfo wi) {
347
            int height, width;
348
            if (wi.isMaximized()) {
349
                    if (wi.getNormalWidth()!=-1)
350
                            width = wi.getNormalWidth();
351
                    else
352
                            width = frame.getNormalBounds().width;
353
                    if (wi.getNormalHeight()!=-1)
354
                            height = wi.getNormalHeight();
355
                    else
356
                            height = frame.getNormalBounds().height;
357

    
358
                    frame.setSize(width, height);
359
                    frame.setLocation(wi.getNormalX(), wi.getNormalY());
360
            }
361
            else {
362
                    if (wi.getWidth()!=-1)
363
                            width = wi.getWidth();
364
                    else
365
                            width = frame.getWidth();
366
                    if (wi.getHeight()!=-1)
367
                            height = wi.getHeight();
368
                    else
369
                            height = frame.getHeight();
370
                    frame.setSize(width, height);
371
                frame.setLocation(wi.getX(), wi.getY());
372
            }
373
            frame.setTitle(wi.getTitle());
374
            frame.setVisible(wi.isVisible());
375
            frame.setResizable(wi.isResizable());
376
            frame.setIconifiable(wi.isIconifiable());
377
            frame.setMaximizable(wi.isMaximizable());
378
            try {
379
                        frame.setMaximum(wi.isMaximized());
380
                } catch (PropertyVetoException e) {
381
                        // TODO Auto-generated catch block
382
                        //e.printStackTrace();
383
                }
384
    }
385

    
386
    /**
387
     * DOCUMENT ME!
388
     *
389
     * @param p
390
     */
391
    private void addJInternalFrame(IWindow p) {
392
        WindowInfo wi = wis.getWindowInfo(p);
393

    
394
        JInternalFrame wnd = fws.getJInternalFrame(p);
395

    
396
        if (p instanceof SingletonWindow) {
397
            SingletonWindow sv = (SingletonWindow) p;
398
            sws.openSingletonWindow(sv, wnd);
399
        }
400

    
401
        addJInternalFrame(wnd, wi);
402
    }
403

    
404
    /**
405
     * DOCUMENT ME!
406
     *
407
     * @param wnd
408
     */
409
    private void activateJInternalFrame(JInternalFrame wnd) {
410
            JPopupMenu.setDefaultLightWeightPopupEnabled(false);
411
        try {
412
            wnd.moveToFront();
413
            logger.debug("Activando " + wnd.getTitle());
414
            wnd.setSelected(true);
415
            wnd.setIcon(false);
416
        } catch (PropertyVetoException e) {
417
            logger.error(e);
418
        }
419
    }
420

    
421
    /**
422
     * Situa un di?logo modal en el centro de la pantalla
423
     *
424
     * @param d
425
     *            Di?logo que se quiere situar
426
     */
427
    private void centerDialog(JDialog d) {
428
        int offSetX = d.getWidth() / 2;
429
        int offSetY = d.getHeight() / 2;
430

    
431
        d.setLocation((mainFrame.getWidth() / 2) - offSetX, (mainFrame
432
                .getHeight() / 2)
433
                - offSetY);
434
    }
435

    
436
    /**
437
     * DOCUMENT ME!
438
     *
439
     * @param p
440
     */
441
    private void addJDialog(IWindow p) {
442
        JDialog dlg = fws.getJDialog(p);
443

    
444
        centerDialog(dlg);
445

    
446
        dlg.addWindowListener(new DialogWindowListener());
447
        dss.pushDialog(dlg);
448

    
449
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
450
    }
451

    
452
    /*
453
     * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveWindow()
454
     */
455
    public IWindow getActiveWindow() {
456
        JInternalFrame jif = panel.getSelectedFrame();
457

    
458
        if (jif != null) {
459
            IWindow theWindow = fws.getWindow(jif);
460
            if (theWindow == null)
461
                return null;
462
            if (theWindow.getWindowInfo().isPalette())
463
                return wss.getActiveWindow();
464
            else
465
                return fws.getWindow(jif);
466
        }
467
        // return vss.getActiveView();
468

    
469
        return null;
470
    }
471
    public IWindow getFocusWindow(){
472
             JInternalFrame jif = panel.getSelectedFrame();
473

    
474
         if (jif != null) {
475
             IWindow theView = fws.getWindow(jif);
476
             if (theView == null)
477
                 return null;
478
             return fws.getWindow(jif);
479
         }
480
         return null;
481
    }
482
    /*
483
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeWindow(com.iver.andami.ui.mdiManager.IWindow)
484
     */
485
    public void closeWindow(IWindow p) {
486
        // Si es un di?logo modal
487
        if (p.getWindowInfo().isModal()) {
488
            closeJDialog();
489
        } else { // Si no es modal se cierra el JInternalFrame
490
            closeJInternalFrame(fws.getJInternalFrame(p));
491
        }
492
    }
493

    
494
    /*
495
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllWindows()
496
     */
497
    public void closeAllWindows() {
498
        ArrayList eliminar = new ArrayList();
499
        Iterator i = fws.getWindowIterator();
500

    
501
        while (i.hasNext()) {
502
            eliminar.add((IWindow) i.next());
503
        }
504

    
505
        for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
506
            IWindow vista = (IWindow) iter.next();
507
            closeWindow(vista);
508
        }
509
    }
510

    
511
    /*
512
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
513
     */
514
    public WindowInfo getWindowInfo(IWindow w) {
515
            WindowInfo wi = wis.getWindowInfo(w);
516

    
517
    /*
518
     * This is done now in vis.getWindowInfo(w)
519
     *
520
     * JInternalFrame f = fws.getJInternalFrame(w);
521
            wi.setX(f.getX());
522
            wi.setY(f.getY());
523
            wi.setHeight(f.getHeight());
524
            wi.setWidth(f.getWidth());
525
            // isClosed() doesn't work as (I) expected, why? Using isShowing instead
526
            wi.setClosed(!f.isShowing());
527
            wi.setNormalBounds(f.getNormalBounds());
528
            wi.setMaximized(f.isMaximum());*/
529
            return wi;
530
    }
531

    
532
    /**
533
     * DOCUMENT ME!
534
     *
535
     * @param dialog
536
     * @throws RuntimeException
537
     *             DOCUMENT ME!
538
     */
539
    private void closeJDialog() {
540
        JDialog dlg = dss.popDialog();
541
        if (dlg==null)
542
                return;
543
        dlg.setVisible(false);
544

    
545
        IWindow s = fws.getWindow(dlg);
546

    
547
        callWindowClosed(s);
548

    
549
        fws.closeWindow(s);
550

    
551
        // Si es singleton se desasocia el modelo con la vista
552
        if (s instanceof SingletonWindow) {
553
            sws.closeWindow((SingletonWindow) s);
554
        }
555
    }
556

    
557
    /**
558
     * If <code>window</code> implements IWindowListener, sent it
559
     * the windowActivated event.
560
     *
561
     * @param window
562
     *            The IWindow which has to be notified.
563
     */
564
    private void callWindowClosed(IWindow window) {
565
        if (window instanceof IWindowListener) {
566
            ((IWindowListener) window).windowClosed();
567
        }
568
    }
569

    
570
    /**
571
     * If <code>window</code> implements IWindowListener, sent it
572
     * the windowActivated event.
573
     *
574
     * @param window
575
     *            The IWindow which has to be notified.
576
     */
577
    private void callWindowActivated(IWindow window) {
578
//        logger.debug("View '" + window.getWindowInfo().getTitle()
579
//               + "' activated (callViewActivated)");
580
        if (window instanceof IWindowListener) {
581
            ((IWindowListener) window).windowActivated();
582
        }
583
    }
584

    
585
    /**
586
     * DOCUMENT ME!
587
     *
588
     * @param frame
589
     */
590
    private void closeJInternalFrame(JInternalFrame frame) {
591
        try {
592
            IWindow s = (IWindow) fws.getWindow(frame);
593

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

    
604
    /*
605
     * @see com.iver.andami.plugins.IExtension#initialize()
606
     */
607
    public void initialize() {
608
    }
609

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

    
619
    /*
620
     * @see com.iver.andami.plugins.IExtension#isEnabled()
621
     */
622
    public boolean isEnabled() {
623
        // TODO Auto-generated method stub
624
        return false;
625
    }
626

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

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

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

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

    
676
        }
677

    
678

    
679
        /**
680
         * Captura el evento de cerrado de los di?logos con el fin de realizar
681
         * tareas de mantenimiento
682
         *
683
         * @param e
684
         *            evento
685
         */
686
        public void windowClosing(WindowEvent e) {
687
            closeJDialog();
688
        }
689
    }
690

    
691
    /**
692
     * DOCUMENT ME!
693
     */
694
    public class FrameListener implements InternalFrameListener {
695
        /*
696
         * @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
697
         */
698
        public void internalFrameActivated(InternalFrameEvent e) {
699
            // logger.debug("internalFrameActivated " +
700
            // e.getInternalFrame().getTitle());
701

    
702
            // activatedInternalFramesStack.push(e.getInternalFrame());
703

    
704
            IWindow panel = fws.getWindow((JInternalFrame) e.getSource());
705

    
706
            WindowInfo wi = wis.getWindowInfo(panel);
707
            if (wi.isPalette())
708
                return;
709

    
710
            wss.setActive(panel);
711

    
712
            JInternalFrame frame = fws.getJInternalFrame(panel);
713

    
714
            if (wi.isMaximizable()){
715
                    frame.setMaximizable(true);
716
            }
717
            if (!frame.isMaximizable() && frame.isMaximum()) {
718
                try {
719
                    frame.setMaximum(false);
720
                } catch (PropertyVetoException e1) {
721
                }
722
            }
723
            mainFrame.enableControls();
724
            if (wi.getSelectedTools()==null) {
725
                    // this is the first time this window is activated
726
                    wi.setSelectedTools(new HashMap(mainFrame.getInitialSelectedTools()));
727
            }
728
            mainFrame.setSelectedTools(wi.getSelectedTools());
729
            callWindowActivated(panel);
730

    
731
        }
732

    
733
        /*
734
         * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
735
         */
736
        public void internalFrameClosed(InternalFrameEvent e) {
737
        }
738

    
739
        /*
740
         * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
741
         */
742
        public void internalFrameClosing(InternalFrameEvent e) {
743
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
744
            // logger.debug("internalFrameClosing " +
745
            // e.getInternalFrame().getTitle());
746

    
747
            JInternalFrame c = (JInternalFrame) e.getSource();
748
            WindowInfo wi = wis.getWindowInfo((IWindow) fws.getWindow(c));
749

    
750
            IWindow win = fws.getWindow(c);
751
            callWindowClosed(win);
752
            boolean alwaysLive;
753
            if (win instanceof SingletonWindow) {
754
                sws.closeWindow((SingletonWindow) win);
755
            }
756

    
757
            fws.closeWindow(win);
758

    
759
            panel.remove(c);
760

    
761
            wss.remove(win);
762

    
763
            if (!wi.isPalette())
764
                mainFrame.enableControls();
765
            panel.repaint();
766

    
767
            // Para activar el JInternalFrame desde la que hemos
768
            // abierto la ventana que estamos cerrando
769
            IWindow lastWindow = wss.getActiveWindow();
770
            // La activamos
771
            if (lastWindow != null) {
772
                    logger.debug(PluginServices.getText(this, "Devuelvo_el_foco_a_")+lastWindow.getWindowInfo().getTitle());
773
                JInternalFrame frame = fws.getJInternalFrame(lastWindow);
774
                try {
775
                    frame.setSelected(true);
776
                } catch (PropertyVetoException e1) {
777
                    // TODO Auto-generated catch block
778
                    // e1.printStackTrace();
779
                }
780
                // addView(lastView);
781
            }
782

    
783
        }
784

    
785
        /*
786
         * @see javax.swing.event.InternalFrameListener#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)
787
         */
788
        public void internalFrameDeactivated(InternalFrameEvent e) {
789
            // logger.debug("internalDeActivated " +
790
            // e.getInternalFrame().getTitle());
791
            JInternalFrame c = (JInternalFrame) e.getSource();
792
            IWindow win = fws.getWindow(c);
793
            if (win != null) {
794
                WindowInfo wi = wis.getWindowInfo(win);
795
                if (wi.isPalette())
796
                    return;
797

    
798
            }
799

    
800
        }
801

    
802
        /*
803
         * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
804
         */
805
        public void internalFrameDeiconified(InternalFrameEvent e) {
806
            mainFrame.enableControls();
807
        }
808

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

    
816
        /*
817
         * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
818
         */
819
        public void internalFrameOpened(InternalFrameEvent e) {
820
            // logger.debug("internalFrameOpened. Source= " +
821
            // e.getSource().toString());
822
        }
823
    }
824

    
825
    /*
826
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
827
     *      java.lang.Object)
828
     */
829
    public boolean closeSingletonWindow(Class viewClass, Object model) {
830
        JInternalFrame frame = (JInternalFrame) sws.getFrame(viewClass, model);
831
        if (frame == null)
832
            return false;
833
        closeJInternalFrame(frame);
834
        return true;
835
    }
836

    
837
    /*
838
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
839
     */
840
    public boolean closeSingletonWindow(Object model) {
841
        JInternalFrame[] frames = (JInternalFrame[]) sws.getFrames(model);
842
        if (frames.length == 0)
843
            return false;
844
        for (int i = 0; i < frames.length; i++) {
845
            closeJInternalFrame(frames[i]);
846
        }
847
        return true;
848
    }
849

    
850
    /*
851
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
852
     */
853
    public IWindow[] getAllWindows() {
854
        ArrayList windows = new ArrayList();
855
        Iterator i = fws.getWindowIterator();
856

    
857
        while (i.hasNext()) {
858
            windows.add((IWindow) i.next());
859
        }
860
        return (IWindow[]) windows.toArray(new IWindow[0]);
861
    }
862

    
863
    /*
864
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
865
     */
866
    public IWindow[] getOrderedWindows() {
867
        TreeMap windows = new TreeMap();
868
        Iterator winIterator = fws.getWindowIterator();
869

    
870
        Component frame;
871
        IWindow win;
872
        /**
873
         * The order of the window in the JDesktopPane. Smaller numbers
874
         * are closer to the foreground.
875
         */
876
        int zPosition;
877
        while (winIterator.hasNext()) {
878
                win = (IWindow) winIterator.next();
879
                frame = fws.getFrame(win);
880
                    zPosition = panel.getPosition(frame);
881
                    int layer = panel.getLayer(frame);
882

    
883
                    if (! (frame instanceof JDialog)) { //JDialogs are not in inside the LayeredPane
884
                            // flatten all the layers
885
                            if (layer == JLayeredPane.DEFAULT_LAYER.intValue()) {
886
                                    zPosition+=50000;
887
                            }
888
                            else if (layer == JLayeredPane.PALETTE_LAYER.intValue()) {
889
                                    zPosition+=40000;
890
                            }
891
                            else if (layer == JLayeredPane.MODAL_LAYER.intValue()) {
892
                                    zPosition+=30000;
893
                            }
894
                            else if  (layer == JLayeredPane.POPUP_LAYER.intValue()) {
895
                                    zPosition+=20000;
896
                            }
897
                            else if  (layer == JLayeredPane.DRAG_LAYER.intValue()) {
898
                                    zPosition+=10000;
899
                            }
900
                    }
901
                    windows.put(new Integer(zPosition), win);
902
        }
903
        winIterator = windows.values().iterator();
904
        ArrayList winList = new ArrayList();
905
        while (winIterator.hasNext()) {
906
                winList.add(winIterator.next());
907
        }
908

    
909
        return (IWindow[]) winList.toArray(new IWindow[0]);
910
    }
911
    public void setMaximum(IWindow v, boolean bMaximum) throws PropertyVetoException
912
    {
913
        JInternalFrame f = fws.getJInternalFrame(v);
914
        f.setMaximum(bMaximum);
915
    }
916

    
917
    public void changeWindowInfo(IWindow w, WindowInfo wi){
918
            JInternalFrame f = fws.getJInternalFrame(w);
919
            f.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
920
            JDesktopPane pnl = f.getDesktopPane();
921
            pnl.remove(f);
922
            int width;
923
            int height;
924
            if (wi.getWidth()!=-1)
925
                    width = wi.getWidth();
926
            else
927
                    width = f.getWidth();
928
            if (wi.getHeight()!=-1)
929
                    height = wi.getHeight();
930
            else
931
                    height = f.getHeight();
932
            f.setSize(new Dimension(width, height));
933
            f.setLocation(wi.getX(), wi.getY());
934
            if (wi.isPalette()) {
935
                    pnl.add(f, JDesktopPane.PALETTE_LAYER);
936
                    f.setFocusable(false);
937
            } else {
938
                    pnl.add(f, JDesktopPane.DEFAULT_LAYER);
939
                    f.setFocusable(true);
940
                    if (wi.isClosed()) {
941
                            closeWindow(w);
942
                    }
943
            }
944

    
945
            if (wi.isMaximized()) {
946
                    try {
947
                            f.setMaximum(true);
948
                    } catch (PropertyVetoException e) {
949
                            // TODO Auto-generated catch block
950
                            //e.printStackTrace();
951
                    }
952
                    f.setNormalBounds(wi.getNormalBounds());
953
            }
954
            activateJInternalFrame(f);
955
    }
956

    
957
    public void refresh(IWindow win) {
958
            Component frame = fws.getFrame(win);
959
            if (frame!=null)
960
                    frame.setVisible(true);
961
    }
962

    
963
        public void setBackgroundImage(ImageIcon image, String typeDesktop) {
964
                this.image=image;
965
                this.typeDesktop=typeDesktop;
966

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

    
1012
        /**
1013
         * We need this in order to addMenuBar to windows, for example
1014
         * @return
1015
         */
1016
        public void addJMenuBarToWindow(IWindow w, JMenuBar menuBar) {
1017
                Component c = fws.getFrame(w);
1018
                if (c instanceof JDialog) {
1019
                        ((JDialog) c).setJMenuBar(menuBar);
1020
                }
1021
                if (c instanceof JInternalFrame) {
1022
                        ((JInternalFrame) c).setJMenuBar(menuBar);
1023
                }
1024
        }
1025
}