Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / ext3Dgui / src / com / iver / ai2 / gvsig3dgui / skin / JDialogSkin.java @ 16096

History | View | Annotate | Download (31.9 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.ai2.gvsig3dgui.skin;
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.awt.event.WindowListener;
57
import java.beans.PropertyVetoException;
58
import java.util.ArrayList;
59
import java.util.Iterator;
60
import java.util.TreeMap;
61
import java.util.logging.Logger;
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.KeyStroke;
71

    
72
import com.iver.andami.PluginServices;
73
import com.iver.andami.ui.mdiFrame.GlassPane;
74
import com.iver.andami.ui.mdiFrame.MDIFrame;
75
import com.iver.andami.ui.mdiFrame.NewStatusBar;
76
import com.iver.andami.ui.mdiManager.IWindow;
77
import com.iver.andami.ui.mdiManager.IWindowListener;
78
import com.iver.andami.ui.mdiManager.MDIUtilities;
79
import com.iver.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
80
import com.iver.andami.ui.mdiManager.SingletonWindow;
81
import com.iver.andami.ui.mdiManager.WindowInfo;
82
import com.iver.andami.ui.theme.Theme;
83
import com.iver.core.mdiManager.NewSkin;
84

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

    
102
    /** log */
103
    private static Logger logger = Logger.getLogger(JDialogSkin.class.getName());
104

    
105
    /** Panel de la MDIFrame */
106
    private MyDesktopPane panel = new MyDesktopPane();
107

    
108
    /** MDIFrame */
109
    private MDIFrame mainFrame;
110

    
111
    private GlassPane glassPane = new GlassPane();
112

    
113
    private JDialogStackSupport dss;
114

    
115
    /**
116
     * Associates JInternalFrames with the IWindow they contain 
117
     */
118
    private JDialogFrameWindowSupport fws;
119

    
120
    private JDialogWindowInfoSupport wis;
121

    
122
    private JDialogWindowStackSupport wss;
123

    
124
    private JDialogSingletonWindowSupport sws;
125

    
126
    private Cursor lastCursor = null;
127
        private ImageIcon image;
128
        private String typeDesktop;
129
        
130
        private IWindow lastActiveWindow = null;
131

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

    
141
        mainFrame.getContentPane().add(panel, BorderLayout.CENTER);
142
        panel.setDesktopManager(desktopManager);
143

    
144
        fws = new JDialogFrameWindowSupport(mainFrame);
145
        dss = new JDialogStackSupport(mainFrame);
146
        sws = new JDialogSingletonWindowSupport(wis,fws);
147
        wis = new JDialogWindowInfoSupport(mainFrame, fws, sws);
148
        fws.setVis(wis);
149
        wss = new JDialogWindowStackSupport(wis);
150

    
151

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

    
157
        PluginServices.registerKeyStroke(controlTab, new KeyEventDispatcher() {
158

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

    
164

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

    
176
        });
177
    }
178

    
179
    /**
180
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
181
     */
182
    public IWindow addWindow(IWindow p) throws SingletonDialogAlreadyShownException {
183
        // se obtiene la informaci?n de la vista
184
        WindowInfo wi = wis.getWindowInfo(p);
185

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

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

    
198
        if (p instanceof SingletonWindow) {
199
            SingletonWindow sw = (SingletonWindow) p;
200
            if (sws.registerWindow(sw.getClass(), sw.getWindowModel(), wi)) {
201
                singletonPreviouslyAdded = true;
202
            }
203
        }
204

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

    
244
            return p;
245
        }
246
    }
247
    
248
    /**
249
     * Situa una ventana en el centro de la pantalla
250
     *
251
     * @param d
252
     *            Di?logo que se quiere situar
253
     */
254
    private void centerWindow(IWindow win) {
255
            JDialog dialog = (JDialog) fws.getFrame(win);
256
            
257
        int offSetX = dialog.getWidth() / 2;
258
        int offSetY = dialog.getHeight() / 2;
259

    
260
        dialog.setLocation((mainFrame.getWidth() / 2) - offSetX,
261
                        (mainFrame.getHeight() / 2) - offSetY);
262
    }
263
    
264
    /**
265
     * Similar method as 'addView' but in this case centres the new JInternalFrame of the View in the contentPane of the MainFrame
266
     * 
267
     * @see com.iver.core.mdiManager.NewSkin#addWindow(com.iver.andami.ui.mdiManager.IWindow)
268
     * 
269
     * @author Pablo Piqueras Bartolom?
270
     */
271
        public IWindow addCentredWindow(IWindow p) throws SingletonDialogAlreadyShownException {
272
                IWindow window = addWindow(p);
273
                centerWindow(window);
274
                return window;
275
        }
276
        
277
        /**
278
         * Centres the JInternalFrame in the center of the contentPane of the MainFrame
279
         * If it can't be showed completely the JInternalFrame, tries that the top-left corner of the JInternalFrame would be showed
280
         * 
281
         * @author Pablo Piqueras Bartolom?
282
         * 
283
         * @param jInternalFrame A reference to the frame to centring
284
         */
285
        private static synchronized void centreJFrame(JFrame frame) {
286
                
287
                // The top-left square of frame reference
288
                Point newReferencePoint = new Point();
289
                
290
                // A reference to the panel where the JInternalFrame will be displayed
291
                Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
292
                
293
                // Get the NewStatusBar component
294
                NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
295
                JDesktopPane jDesktopPane = ((JDesktopPane)contentPane.getComponent(2));
296
                
297
                int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The last substraction is for if there is any menu,... at left
298
                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
299
                int freeWidth = visibleWidth - frame.getWidth();
300
                int freeHeight = visibleHeight - frame.getHeight();
301

    
302
                // Calculate the new point reference (Assure that the top-left corner is showed)
303
                if (freeWidth < 0)
304
                {
305
                        if (visibleWidth > MinimumXMargin)
306
                                newReferencePoint.x = DefaultXMargin;
307
                        else
308
                                newReferencePoint.x = 0;
309
                }
310
                else
311
                        newReferencePoint.x = freeWidth / 2;
312
                        
313
                if (freeHeight < 0)
314
                {
315
                        if (visibleHeight > MinimumYMargin)
316
                                newReferencePoint.y = DefaultYMargin;
317
                        else
318
                                newReferencePoint.y = 0;
319
                }
320
                else
321
                        newReferencePoint.y = freeHeight / 2;
322

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

    
327
    /**
328
     * DOCUMENT ME!
329
     *
330
     * @param wnd
331
     *            DOCUMENT ME!
332
     * @param wi
333
     *            DOCUMENT ME!
334
     */
335
    private void addFrame(JDialog wnd, WindowInfo wi) {
336
            wnd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
337
        FrameListener listener = new FrameListener();
338
        wnd.addWindowListener(listener);
339
        wnd.addWindowStateListener(listener);
340
        wnd.addWindowFocusListener(listener);
341
        
342
        if (wi.isModeless() || wi.isPalette()) {
343
          //  panel.add(wnd, JDesktopPane.PALETTE_LAYER);
344
            if (wi.isPalette())
345
                wnd.setFocusable(false);
346
        } else {
347
        //    panel.add(wnd);
348
        }
349
        updateFrameProperties(wnd, wi);
350
        
351
        
352
        activateFrame(wnd);
353
        /*
354
            if (wi.isMaximized()) {
355
                    wnd.setExtendedState(JFrame.MAXIMIZED_BOTH);
356
            }
357
            else {
358
                int state = wnd.getExtendedState();
359
                state = (state & (~JFrame.MAXIMIZED_BOTH)); 
360
                wnd.setExtendedState(state);
361
            }*/
362
    }
363
        
364
        
365
    /**
366
     * DOCUMENT ME!
367
     *
368
     * @param wnd
369
     *            DOCUMENT ME!
370
     * @param wi
371
     *            DOCUMENT ME!
372
     */
373
   /* private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
374
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
375
        wnd.add
376
        wnd.addInternalFrameListener(new FrameListener());
377
        
378
        if (wi.isModeless() || wi.isPalette()) {
379
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
380
            if (wi.isPalette())
381
                wnd.setFocusable(false);
382
        } else {
383
            panel.add(wnd);
384
        }
385
      //  updateFrameProperties(wnd, wi);
386
        
387
        activateJInternalFrame(wnd);
388
        try{
389
                wnd.setMaximum(wi.isMaximized());
390
        }catch(Exception ex){
391
                logger.warn("Error: ", ex);
392
        }
393
    }*/
394

    
395
    private void updateFrameProperties(JDialog frame, WindowInfo wi) {
396
            int height, width;
397
            if (wi.isMaximized()) {
398
                    if (wi.getNormalWidth()!=-1)
399
                            width = wi.getNormalWidth();
400
                    else
401
                            width = frame.getWidth();
402
                    if (wi.getNormalHeight()!=-1)
403
                            height = wi.getNormalHeight();
404
                    else
405
                            height = frame.getHeight();
406
                    
407
                    frame.setSize(width, height);
408
                    frame.setLocation(wi.getNormalX(), wi.getNormalY());
409
            }
410
            else {
411
                    if (wi.getWidth()!=-1)
412
                            width = wi.getWidth();
413
                    else
414
                            width = frame.getWidth();
415
                    if (wi.getHeight()!=-1)
416
                            height = wi.getHeight();
417
                    else
418
                            height = frame.getHeight();
419
                    frame.setSize(width, height);
420
                frame.setLocation(wi.getX(), wi.getY());                    
421
            }
422
            frame.setTitle(wi.getTitle());
423
            frame.setVisible(wi.isVisible());
424
            frame.setResizable(wi.isResizable());
425
            /*if (wi.isMaximized()) {
426
                    frame.set
427
                    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
428
            }
429
            else {
430
                int state = frame.getExtendedState();
431
                state = (state & (~JFrame.MAXIMIZED_BOTH)); 
432
                frame.setExtendedState(state);
433
            }*/
434
    }
435
    
436
    /**
437
     * DOCUMENT ME!
438
     *
439
     * @param p
440
     */
441
 /*   private void addJInternalFrame(IWindow p) {
442
        WindowInfo wi = wis.getWindowInfo(p);
443

444
        JInternalFrame wnd = fws.getJInternalFrame(p);
445

446
        if (p instanceof SingletonWindow) {
447
            SingletonWindow sv = (SingletonWindow) p;
448
            sws.openSingletonWindow(sv, wnd);
449
        }
450

451
        addJInternalFrame(wnd, wi);
452
    }*/
453
    
454
    
455
    /**
456
     * DOCUMENT ME!
457
     *
458
     * @param p
459
     */
460
 /*   private void addJFrame(IWindow p) {
461
        WindowInfo wi = wis.getWindowInfo(p);
462

463
        JFrame wnd = fws.getFrame(p);
464

465
        if (p instanceof SingletonWindow) {
466
            SingletonWindow sv = (SingletonWindow) p;
467
            sws.openSingletonWindow(sv, wnd);
468
        }
469

470
        addJFrame(wnd, wi);
471
    }*/
472

    
473
    /**
474
     * DOCUMENT ME!
475
     *
476
     * @param wnd
477
     */
478
    private void activateJInternalFrame(JInternalFrame wnd) {
479
        try {
480
            wnd.moveToFront();
481
//            logger.debug("Activando " + wnd.getTitle());
482
            wnd.setSelected(true);
483
            wnd.setIcon(false);
484
        } catch (PropertyVetoException e) {
485
//            logger.error(e);
486
        }
487
    }
488
    
489
    /**
490
     * DOCUMENT ME!
491
     *
492
     * @param wnd
493
     */
494
    private void activateFrame(JDialog wnd) {
495
            wnd.setVisible(true);
496
            wnd.toFront();
497
            
498
            /*int state = wnd.getExtendedState();
499
            state = (state & (~JFrame.ICONIFIED)); 
500
            wnd.setExtendedState(state);*/
501
    }
502

    
503
    /**
504
     * Situa un di?logo modal en el centro de la pantalla
505
     *
506
     * @param d
507
     *            Di?logo que se quiere situar
508
     */
509
    private void centerDialog(JDialog d) {
510
        int offSetX = d.getWidth() / 2;
511
        int offSetY = d.getHeight() / 2;
512

    
513
        d.setLocation((mainFrame.getWidth() / 2) - offSetX, (mainFrame
514
                .getHeight() / 2)
515
                - offSetY);
516
    }
517

    
518
    /**
519
     * DOCUMENT ME!
520
     *
521
     * @param p
522
     */
523
    private void addJDialog(IWindow p) {
524
        JDialog dlg = fws.getJDialog(p);
525

    
526
        centerDialog(dlg);
527

    
528
        dlg.addWindowListener(new DialogWindowListener());
529
        dss.pushDialog(dlg);
530

    
531
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
532
    }
533
    
534
    /**
535
     * DOCUMENT ME!
536
     *
537
     * @param p
538
     */
539
    private void addFrame(IWindow p) {
540
        WindowInfo wi = wis.getWindowInfo(p);
541

    
542
        JDialog wnd = (JDialog) fws.getFrame(p);
543
        if (wnd==null)
544
                wnd=new JDialog();
545
        if (p instanceof SingletonWindow) {
546
            SingletonWindow sv = (SingletonWindow) p;
547
            sws.openSingletonWindow(sv, wnd);
548
        }
549

    
550
        addFrame(wnd, wi);
551
    }
552

    
553
    /**
554
     * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveWindow()
555
     */
556
    public IWindow getActiveWindow() {
557
            return wss.getActiveWindow();
558
        /*JFrame frame = panel.getSelectedFrame();
559

560
        if (frame != null) {
561
            IWindow theWindow = fws.getWindow(frame);
562
            if (theWindow == null)
563
                return null;
564
            if (theWindow.getWindowInfo().isPalette())
565
                return wss.getActiveWindow();
566
            else
567
                return fws.getWindow(frame);
568
        }
569
        // return vss.getActiveView();
570

571
        return null;*/
572
    }
573
    public IWindow getFocusWindow(){
574
            return wss.getActiveWindow();
575
            /*fws.getFocusWindow()
576
            JFrame.get
577
            JFrame frame = panel.getSelectedFrame();
578

579
         if (frame != null) {
580
             IWindow theView = fws.getWindow(frame);
581
             if (theView == null)
582
                 return null;
583
             return fws.getWindow(frame);
584
         }
585
         return null;*/
586
    }
587
    /**
588
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeWindow(com.iver.andami.ui.mdiManager.IWindow)
589
     */
590
    public void closeWindow(IWindow p) {
591
                // Si es un di?logo modal
592
                if (p.getWindowInfo().isModal()) {
593
                        closeJDialog();
594
                } else { // Si no es modal se cierra el JInternalFrame
595
                        closeJFrame((JDialog) fws.getFrame(p));
596
                }
597
        }
598

    
599
    /**
600
         * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllWindows()
601
         */
602
    public void closeAllWindows() {
603
        ArrayList eliminar = new ArrayList();
604
        Iterator i = fws.getWindowIterator();
605

    
606
        while (i.hasNext()) {
607
            eliminar.add((IWindow) i.next());
608
        }
609

    
610
        for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
611
            IWindow vista = (IWindow) iter.next();
612
            closeWindow(vista);
613
        }
614
    }
615

    
616
    /**
617
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
618
     */
619
    public WindowInfo getWindowInfo(IWindow w) {
620
            WindowInfo wi = wis.getWindowInfo(w);
621
            
622
    /*        
623
     * This is done now in vis.getWindowInfo(w)
624
     * 
625
     * JInternalFrame f = fws.getJInternalFrame(w);
626
            wi.setX(f.getX());
627
            wi.setY(f.getY());
628
            wi.setHeight(f.getHeight());
629
            wi.setWidth(f.getWidth());
630
            // isClosed() doesn't work as (I) expected, why? Using isShowing instead
631
            wi.setClosed(!f.isShowing());
632
            wi.setNormalBounds(f.getNormalBounds());
633
            wi.setMaximized(f.isMaximum());*/
634
            return wi;
635
    }
636

    
637
    /**
638
     * DOCUMENT ME!
639
     *
640
     * @param dialog
641
     * @throws RuntimeException
642
     *             DOCUMENT ME!
643
     */
644
    private void closeJDialog() {
645
        JDialog dlg = dss.popDialog();
646

    
647
        dlg.setVisible(false);
648

    
649
        IWindow s = (IWindow) fws.getWindow(dlg);
650

    
651
        callWindowClosed(s);
652

    
653
        fws.closeWindow(s);
654

    
655
        // Si es singleton se desasocia el modelo con la vista
656
        if (s instanceof SingletonWindow) {
657
            sws.closeWindow((SingletonWindow) s);
658
        }
659
    }
660

    
661
    /**
662
     * DOCUMENT ME!
663
     *
664
     * @param window
665
     *            DOCUMENT ME!
666
     */
667
    private void callWindowClosed(IWindow window) {
668
        if (window instanceof IWindowListener) {
669
            ((IWindowListener) window).windowClosed();
670
        }
671
    }
672

    
673
    /**
674
     * DOCUMENT ME!
675
     *
676
     * @param window
677
     *            DOCUMENT ME!
678
     */
679
    private void callWindowActivated(IWindow window) {
680
//        logger.debug("View " + window.getWindowInfo().getTitle()
681
//                + " activated (callViewActivated)");
682
        if (window instanceof IWindowListener) {
683
            ((IWindowListener) window).windowActivated();
684
        }
685
    }
686

    
687
    /**
688
     * DOCUMENT ME!
689
     *
690
     * @param frame
691
     */
692
    private void closeJInternalFrame(JInternalFrame frame) {
693
        try {
694
            IWindow s = (IWindow) fws.getWindow(frame);
695

    
696
            frame.setClosed(true);
697
            callWindowClosed(s);
698
        } catch (PropertyVetoException e) {
699
//            logger
700
//                    .error(
701
//                            "Not compatible with property veto's. Use ViewInfo instead.",
702
//                            e);
703
        }
704
    }
705
    
706
    /**
707
     * DOCUMENT ME!
708
     *
709
     * @param frame
710
     */
711
    private void closeJFrame(JDialog frame) {
712
            WindowListener[] listeners = frame.getWindowListeners();
713
            for (int i=0; i<listeners.length; i++) {
714
                    listeners[i].windowClosing(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING        ));
715
            }
716
            IWindow s = (IWindow) fws.getWindow(frame);
717
            frame.dispose();
718
            callWindowClosed(s);
719
    }
720

    
721
    /**
722
     * @see com.iver.andami.plugins.IExtension#initialize()
723
     */
724
    public void initialize() {
725
    }
726

    
727
    /**
728
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
729
     */
730
    public void execute(String actionCommand) {
731
        if (actionCommand.equals("cascada")) {
732
        } else if (actionCommand.equals("mosaico")) {
733
        }
734
    }
735

    
736
    /**
737
     * @see com.iver.andami.plugins.IExtension#isEnabled()
738
     */
739
    public boolean isEnabled() {
740
        // TODO Auto-generated method stub
741
        return false;
742
    }
743

    
744
    /**
745
     * @see com.iver.andami.plugins.IExtension#isVisible()
746
     */
747
    public boolean isVisible() {
748
        // TODO Auto-generated method stub
749
        return true;
750
    }
751

    
752
    /**
753
     * @see com.iver.andami.ui.mdiManager.MDIManager#setWaitCursor()
754
     */
755
    public void setWaitCursor() {
756
        if (mainFrame != null) {
757
            glassPane.setVisible(true);
758
            lastCursor = mainFrame.getCursor();
759
            dss.setWaitCursor();
760
            glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
761
        }
762
    }
763

    
764
    /**
765
     * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
766
     */
767
    public void restoreCursor() {
768
        if (mainFrame != null) {
769
            glassPane.setVisible(false);
770
            dss.restoreCursor();
771
            glassPane.setCursor(lastCursor);
772
        }
773
    }
774

    
775
    /**
776
     * Listener para los eventos de cerrado de los di?logos. Tiene su raz?n de
777
     * ser en que los di?logos han de devolverse al pool cuando se cierran
778
     *
779
     * @author Fernando Gonz?lez Cort?s
780
     */
781
    public class DialogWindowListener extends WindowAdapter {
782
        /**
783
         * Captura el evento de cerrado de los di?logos con el fin de realizar
784
         * tareas de mantenimiento
785
         *
786
         * @param e
787
         *            evento
788
         */
789
        public void windowClosing(WindowEvent e) {
790
            closeJDialog();
791
        }
792
    }
793

    
794
    /**
795
     * DOCUMENT ME!
796
     */
797
    public class FrameListener extends WindowAdapter {
798
        /**
799
         * @see java.awt.event.WindowAdapter#windowActivated(java.awt.event.WindowEvent)
800
         */
801
        public void windowActivated(WindowEvent e) {
802
                JDialog frame = (JDialog) e.getSource();
803
            IWindow panel = fws.getWindow(frame);
804
            
805
            if (panel!=null) {
806

    
807
                    WindowInfo wi = wis.getWindowInfo(panel);
808
                    if (wi.isPalette())
809
                            return;
810

    
811
                    wss.setActive(panel);
812
                
813
                    if (lastActiveWindow!=null && !lastActiveWindow.equals(panel)) {
814
                            wis.getWindowInfo(lastActiveWindow).setSelectedTools(mainFrame.getSelectedTools());
815
                            mainFrame.setSelectedTools(wi.getSelectedTools());
816
                    }
817
                    
818
                    mainFrame.enableControls();
819
                    callWindowActivated(panel);
820
            }
821

    
822
        }
823
        
824
        /**
825
         * @see java.awt.event.WindowAdapter#windowClosed(java.awt.event.WindowEvent)
826
         */
827
        public void windowClosed(WindowEvent e) {
828
        }
829

    
830

    
831
        /**
832
         * @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
833
         */
834
        public void windowClosing(WindowEvent e) {
835
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
836
            // logger.debug("internalFrameClosing " +
837
            // e.getInternalFrame().getTitle());
838

    
839
                JDialog         c = (JDialog) e.getSource();
840
                IWindow win = (IWindow) fws.getWindow(c);
841
                if (win!=null) {
842
                
843
                    WindowInfo wi = wis.getWindowInfo(win);
844
        
845
                    callWindowClosed(win);
846
                    boolean alwaysLive;
847
                    if (win instanceof SingletonWindow) {
848
                        sws.closeWindow((SingletonWindow) win);
849
                    }
850
        
851
                    fws.closeWindow(win);
852
        
853
                    panel.remove(c);
854
        
855
                    wss.remove(win);
856
        
857
                    if (!wi.isPalette())
858
                        mainFrame.enableControls();
859
                    panel.repaint();
860
                }
861
                
862
            // Para activar el JInternalFrame desde la que hemos
863
            // abierto la ventana que estamos cerrando
864
            IWindow lastWindow = wss.getActiveWindow();
865
            // La activamos
866
            if (lastWindow != null) {
867
//                    logger.debug(PluginServices.getText(this, "Devuelvo_el_foco_a_")+lastWindow.getWindowInfo().getTitle());
868
                JDialog frame = (JDialog) fws.getFrame(lastWindow);
869
                frame.toFront();
870
            }        
871
        }
872

    
873
        /**
874
         * @see java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent)
875
         */
876
        public void windowDeactivated(WindowEvent e) {
877
                JDialog frame = (JDialog) e.getSource();
878
            IWindow win = fws.getWindow(frame);
879
            if (win != null) {
880
                WindowInfo wi = wis.getWindowInfo(win);
881
                if (wi.isPalette())
882
                    return;
883
                wi.setSelectedTools(mainFrame.getSelectedTools());
884
                lastActiveWindow = win;
885
            }
886
        }
887

    
888
        /**
889
         * @see java.awt.event.WindowAdapter#windowDeiconified(java.awt.event.WindowEvent)
890
         */
891
        public void windowDeiconified(WindowEvent e) {
892
            mainFrame.enableControls();
893
        }
894
        
895
        /**
896
         * @see java.awt.event.WindowAdapter#windowGainedFocus(java.awt.event.WindowEvent)
897
         */
898
        public void windowGainedFocus(WindowEvent e) {
899
                
900
        }
901

    
902
        /**
903
         * @see java.awt.event.WindowAdapter#windowIconified(java.awt.event.WindowEvent)
904
         */
905
        public void windowIconified(WindowEvent e) {
906
            mainFrame.enableControls();
907
        }
908
        
909
        /**
910
         * @see java.awt.event.WindowAdapter#windowLostFocus(java.awt.event.WindowEvent)n
911
         */
912
        public void windowLostFocus(WindowEvent e) {
913
                
914
        }
915

    
916
        /**
917
         * @see java.awt.event.WindowAdapter#windowOpened(java.awt.event.WindowEvent)
918
         */
919
        public void windowOpened(WindowEvent e) {
920
        }
921
        
922
        /**
923
         * @see java.awt.event.WindowAdapter#windowStateChanged(java.awt.event.WindowEvent)
924
         */
925
        public void windowStateChanged(WindowEvent e) {
926
                
927
        }
928
    }
929

    
930
    /**
931
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
932
     *      java.lang.Object)
933
     */
934
    public boolean closeSingletonWindow(Class viewClass, Object model) {
935
        JDialog frame = (JDialog)sws.getFrame(viewClass, model);
936
        if (frame == null)
937
            return false;
938
        closeJFrame(frame);
939
        return true;
940
    }
941

    
942
    /**
943
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
944
     */
945
    public boolean closeSingletonWindow(Object model) {
946
        JDialog[] frames = (JDialog[])sws.getFrames(model);
947
        if (frames.length == 0)
948
            return false;
949
        for (int i = 0; i < frames.length; i++) {
950
            closeJFrame(frames[i]);
951
        }
952
        return true;
953
    }
954

    
955
    /**
956
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
957
     */
958
    public IWindow[] getAllWindows() {
959
        ArrayList windows = new ArrayList();
960
        Iterator i = fws.getWindowIterator();
961

    
962
        while (i.hasNext()) {
963
            windows.add((IWindow) i.next());
964
        }
965
        return (IWindow[]) windows.toArray(new IWindow[0]);
966
    }
967
    
968
    /**
969
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
970
     */
971
    public IWindow[] getOrderedWindows() {
972
        //ArrayList windows = new ArrayList();
973
        TreeMap windows = new TreeMap();
974
        Iterator winIterator = fws.getWindowIterator();
975
        
976
        Component frame;
977
        IWindow win;
978
        /**
979
         * The order of the window in the JDesktopPane. Smaller numbers
980
         * are closer to the foreground.
981
         */
982
        int zPosition = 0;
983
        while (winIterator.hasNext()) {
984
                win = (IWindow) winIterator.next();
985
                // unfortunately we can't use this now, and I don't know how to retrieve the zPosition for JDialogs
986
                //frame = fws.getComponent(win);
987
                    //zPosition = panel.getPosition(frame); 
988
                    windows.put(new Integer(zPosition++), win);
989
        }
990
        winIterator = windows.values().iterator();
991
        ArrayList winList = new ArrayList();
992
        while (winIterator.hasNext()) {
993
                winList.add(winIterator.next());
994
        }
995

    
996
        return (IWindow[]) winList.toArray(new IWindow[0]);
997
    }
998

    
999
    public void setMaximum(IWindow v, boolean bMaximum) throws PropertyVetoException
1000
    {
1001
        /*JDialog frame = fws.getFrame(v);
1002
        
1003
            if (bMaximum) {
1004

1005
                frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
1006
            }
1007
            else {
1008
                    int state = frame.getExtendedState();
1009
                    state = (state & (~JFrame.MAXIMIZED_BOTH));
1010
                    frame.setExtendedState(state);
1011
            }*/
1012
    }
1013

    
1014
    public void changeWindowInfo(IWindow w, WindowInfo wi){
1015
            JDialog frame = fws.getJDialog(w);
1016
                frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1017
                int width;
1018
                int height;
1019
                if (wi.getWidth()!=-1)
1020
                        width = wi.getWidth();
1021
                else
1022
                        width = frame.getWidth();
1023
                if (wi.getHeight()!=-1)
1024
                        height = wi.getHeight();
1025
                else
1026
                        height = frame.getHeight();
1027
                frame.setSize(new Dimension(width, height));
1028
                frame.setLocation(wi.getX(), wi.getY());
1029
                if (wi.isPalette()) {
1030
                        //panel.add(frame, JDesktopPane.PALETTE_LAYER);
1031
                        frame.setFocusable(false);
1032
                } else {
1033
                        //panel.add(frame, JDesktopPane.DEFAULT_LAYER);
1034
                        frame.setFocusable(true);
1035
                        if (wi.isClosed()) {
1036
                                closeWindow(w);
1037
                        }
1038
                }
1039

    
1040
                if (wi.isMaximized()) {
1041
                        frame.setBounds(wi.getNormalBounds());
1042
                //        frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
1043
                }
1044
                activateFrame(frame);
1045
   }
1046
    
1047
    public void refresh(IWindow win) {
1048
            Component frame = fws.getFrame(win);
1049
            if (frame!=null)
1050
                    frame.setVisible(true);
1051
    }
1052

    
1053
        public void setBackgroundImage(ImageIcon image, String typeDesktop) {
1054
                this.image=image;
1055
                this.typeDesktop=typeDesktop;
1056
                
1057
        }
1058
        class MyDesktopPane extends JDesktopPane
1059
    {
1060
      public MyDesktopPane(){
1061
      }
1062
      public void paintComponent(Graphics g){
1063
        super.paintComponent(g);
1064
        int x=0;
1065
        int y=0;
1066
        int w=0;
1067
        int h=0;
1068
        if (image != null) {
1069
                        if (typeDesktop.equals(Theme.CENTERED)) {
1070
                                w = image.getIconWidth();
1071
                                h = image.getIconHeight();
1072
                                x = (getWidth() - w) / 2;
1073
                                y = (getHeight() - h) / 2;
1074
                                g.drawImage(image.getImage(), x, y, w, h, this);
1075
                        } else if (typeDesktop.equals(Theme.EXPAND)) {
1076
                                w = getWidth();
1077
                                h = getHeight();
1078
                                g.drawImage(image.getImage(), x, y, w, h, this);
1079
                        } else if (typeDesktop.equals(Theme.MOSAIC)) {
1080
                                int wAux = image.getIconWidth();
1081
                                int hAux = image.getIconHeight();
1082
                                int i = 0;
1083
                                int j = 0;
1084
                                w = wAux;
1085
                                h = hAux;
1086
                                while (x < getWidth()) {
1087
                                        x = wAux * i;
1088
                                        while (y < getHeight()) {
1089
                                                y = hAux * j;
1090
                                                j++;
1091
                                                g.drawImage(image.getImage(), x, y, w, h, this);
1092
                                        }
1093
                                        y = 0;
1094
                                        j = 0;
1095
                                        i++;
1096
                                }
1097
                        }
1098
                }
1099
      }
1100
    }
1101

    
1102

    
1103
}