Statistics
| Revision:

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

History | View | Annotate | Download (30.4 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.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.apache.log4j.Logger;
76

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

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

    
100

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

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

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

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

    
117
    private GlassPane glassPane = new GlassPane();
118

    
119
    private DialogStackSupport dss;
120

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

    
126
    private WindowInfoSupport wis;
127

    
128
    private WindowStackSupport wss;
129

    
130
    private SingletonWindowSupport sws;
131

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

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

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

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

    
155

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

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

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

    
168

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

    
180
        });
181
    }
182

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

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

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

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

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

    
248
            return p;
249
        }
250
    }
251

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

    
261

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

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

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

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

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

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

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

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

    
315

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

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

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

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

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

    
392
        JInternalFrame wnd = fws.getJInternalFrame(p);
393

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

    
399
        addJInternalFrame(wnd, wi);
400
    }
401

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

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

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

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

    
442
        centerDialog(dlg);
443

    
444
        dlg.addWindowListener(new DialogWindowListener());
445
        dss.pushDialog(dlg);
446

    
447
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
448
    }
449

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

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

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

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

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

    
499
        while (i.hasNext()) {
500
            eliminar.add((IWindow) i.next());
501
        }
502

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

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

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

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

    
543
        IWindow s = fws.getWindow(dlg);
544

    
545
        callWindowClosed(s);
546

    
547
        fws.closeWindow(s);
548

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

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

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

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

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

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

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

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

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

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

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

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

    
674
        }
675

    
676

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

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

    
700
            // activatedInternalFramesStack.push(e.getInternalFrame());
701

    
702
            IWindow panel = fws.getWindow((JInternalFrame) e.getSource());
703

    
704
            WindowInfo wi = wis.getWindowInfo(panel);
705
            if (wi.isPalette())
706
                return;
707

    
708
            wss.setActive(panel);
709

    
710
            JInternalFrame frame = fws.getJInternalFrame(panel);
711

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

    
729
        }
730

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

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

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

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

    
755
            fws.closeWindow(win);
756

    
757
            panel.remove(c);
758

    
759
            wss.remove(win);
760

    
761
            if (!wi.isPalette())
762
                mainFrame.enableControls();
763
            panel.repaint();
764

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

    
781
        }
782

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

    
796
            }
797

    
798
        }
799

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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