Statistics
| Revision:

root / trunk / libraries / libCorePlugin / src / com / iver / core / mdiManager / NewSkin.java @ 12443

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
        updateFrameProperties(wnd, wi);
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
        activateJInternalFrame(wnd);
338
        try{
339
                wnd.setMaximum(wi.isMaximized());
340
        }catch(Exception ex){
341
                logger.warn("Error: ", ex);
342
        }
343
    }
344

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

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

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

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

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

    
400
        addJInternalFrame(wnd, wi);
401
    }
402

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

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

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

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

    
443
        centerDialog(dlg);
444

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

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

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

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

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

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

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

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

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

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

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

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

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

    
546
        callWindowClosed(s);
547

    
548
        fws.closeWindow(s);
549

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

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

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

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

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

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

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

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

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

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

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

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

    
675
        }
676

    
677

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

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

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

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

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

    
709
            wss.setActive(panel);
710

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

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

    
730
        }
731

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

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

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

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

    
756
            fws.closeWindow(win);
757

    
758
            panel.remove(c);
759

    
760
            wss.remove(win);
761

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

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

    
782
        }
783

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

    
797
            }
798

    
799
        }
800

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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