Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / ext3Dgui / src / org / gvsig / gvsig3dgui / skin / JDialogSkin.java @ 29938

History | View | Annotate | Download (32.2 KB)

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

    
64
import javax.swing.DefaultDesktopManager;
65
import javax.swing.DesktopManager;
66
import javax.swing.ImageIcon;
67
import javax.swing.JDesktopPane;
68
import javax.swing.JDialog;
69
import javax.swing.JFrame;
70
import javax.swing.JInternalFrame;
71
import javax.swing.KeyStroke;
72

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

    
86
/**
87
 *
88
 */
89
public class JDialogSkin extends NewSkin{
90
        private static final int DefaultXMargin = 20; // Added for the method 'centreJInternalFrame'
91
        private static final int DefaultYMargin = 20; // Added for the method 'centreJInternalFrame'
92
        private static final int MinimumXMargin = 130; // Added for the method 'centreJInternalFrame'
93
        private static final int MinimumYMargin = 60; // Added for the method 'centreJInternalFrame'
94

    
95

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

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

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

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

    
112
    private GlassPane glassPane = new GlassPane();
113

    
114
    private JDialogStackSupport dss;
115

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

    
121
    private JDialogWindowInfoSupport wis;
122

    
123
    private JDialogWindowStackSupport wss;
124

    
125
    private JDialogSingletonWindowSupport sws;
126

    
127
    private Cursor lastCursor = null;
128
        private ImageIcon image;
129
        private String typeDesktop;
130

    
131
        private IWindow lastActiveWindow = null;
132

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

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

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

    
152

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

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

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

    
165

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

    
177
        });
178
    }
179

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

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

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

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

    
206
        if (singletonPreviouslyAdded) {
207
            // Si la vista no est� actualmente abierta
208
            if (!sws.contains((SingletonWindow) p)) {
209
                    JDialog frame = (JDialog)fws.getFrame(p);
210
                sws.openSingletonWindow((SingletonWindow) p,frame);
211

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

    
245
            return p;
246
        }
247
    }
248

    
249
    /**
250
     * Situa una ventana en el centro de la pantalla
251
     *
252
     * @param d
253
     *            Di�logo que se quiere situar
254
     */
255
    private void centerWindow(IWindow win) {
256
            JDialog dialog = (JDialog) fws.getFrame(win);
257

    
258
        int offSetX = dialog.getWidth() / 2;
259
        int offSetY = dialog.getHeight() / 2;
260

    
261
        dialog.setLocation((mainFrame.getWidth() / 2) - offSetX,
262
                        (mainFrame.getHeight() / 2) - offSetY);
263
    }
264

    
265
    /**
266
     * Similar method as 'addView' but in this case centres the new JInternalFrame of the View in the contentPane of the MainFrame
267
     *
268
     * @see com.iver.core.mdiManager.NewSkin#addWindow(com.iver.andami.ui.mdiManager.IWindow)
269
     *
270
     * @author Pablo Piqueras Bartolom�
271
     */
272
        public IWindow addCentredWindow(IWindow p) throws SingletonDialogAlreadyShownException {
273
                IWindow window = addWindow(p);
274
                centerWindow(window);
275
                return window;
276
        }
277

    
278
        /**
279
         * Centres the JInternalFrame in the center of the contentPane of the MainFrame
280
         * If it can't be showed completely the JInternalFrame, tries that the top-left corner of the JInternalFrame would be showed
281
         *
282
         * @author Pablo Piqueras Bartolom�
283
         *
284
         * @param jInternalFrame A reference to the frame to centring
285
         */
286
        private static synchronized void centreJFrame(JFrame frame) {
287

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

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

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

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

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

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

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

    
328
    /**
329
     * DOCUMENT ME!
330
     *
331
     * @param wnd
332
     *            DOCUMENT ME!
333
     * @param wi
334
     *            DOCUMENT ME!
335
     */
336
    private void addFrame(JDialog wnd, WindowInfo wi) {
337
            wnd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
338
        FrameListener listener = new FrameListener();
339
        wnd.addWindowListener(listener);
340
        wnd.addWindowStateListener(listener);
341
        wnd.addWindowFocusListener(listener);
342

    
343
        if (wi.isModeless() || wi.isPalette()) {
344
          //  panel.add(wnd, JDesktopPane.PALETTE_LAYER);
345
            if (wi.isPalette())
346
                wnd.setFocusable(false);
347
        } else {
348
        //    panel.add(wnd);
349
        }
350
        updateFrameProperties(wnd, wi);
351

    
352

    
353
        activateFrame(wnd);
354
        /*
355
            if (wi.isMaximized()) {
356
                    wnd.setExtendedState(JFrame.MAXIMIZED_BOTH);
357
            }
358
            else {
359
                int state = wnd.getExtendedState();
360
                state = (state & (~JFrame.MAXIMIZED_BOTH));
361
                wnd.setExtendedState(state);
362
            }*/
363
    }
364

    
365

    
366
    /**
367
     * DOCUMENT ME!
368
     *
369
     * @param wnd
370
     *            DOCUMENT ME!
371
     * @param wi
372
     *            DOCUMENT ME!
373
     */
374
   /* private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
375
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
376
        wnd.add
377
        wnd.addInternalFrameListener(new FrameListener());
378

379
        if (wi.isModeless() || wi.isPalette()) {
380
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
381
            if (wi.isPalette())
382
                wnd.setFocusable(false);
383
        } else {
384
            panel.add(wnd);
385
        }
386
      //  updateFrameProperties(wnd, wi);
387

388
        activateJInternalFrame(wnd);
389
        try{
390
                wnd.setMaximum(wi.isMaximized());
391
        }catch(Exception ex){
392
                logger.warn("Error: ", ex);
393
        }
394
    }*/
395

    
396
    private void updateFrameProperties(JDialog frame, WindowInfo wi) {
397
            int height, width;
398
            if (wi.isMaximized()) {
399
                    if (wi.getNormalWidth()!=-1)
400
                            width = wi.getNormalWidth();
401
                    else
402
                            width = frame.getWidth();
403
                    if (wi.getNormalHeight()!=-1)
404
                            height = wi.getNormalHeight();
405
                    else
406
                            height = frame.getHeight();
407

    
408
                    frame.setSize(width, height);
409
                    frame.setLocation(wi.getNormalX(), wi.getNormalY());
410
            }
411
            else {
412
                    if (wi.getWidth()!=-1)
413
                            width = wi.getWidth();
414
                    else
415
                            width = frame.getWidth();
416
                    if (wi.getHeight()!=-1)
417
                            height = wi.getHeight();
418
                    else
419
                            height = frame.getHeight();
420
                    frame.setSize(width, height);
421
                frame.setLocation(wi.getX(), wi.getY());
422
            }
423
            frame.setTitle(wi.getTitle());
424
            frame.setVisible(wi.isVisible());
425
            frame.setResizable(wi.isResizable());
426
            /*if (wi.isMaximized()) {
427
                    frame.set
428
                    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
429
            }
430
            else {
431
                int state = frame.getExtendedState();
432
                state = (state & (~JFrame.MAXIMIZED_BOTH));
433
                frame.setExtendedState(state);
434
            }*/
435
    }
436

    
437
    /**
438
     * DOCUMENT ME!
439
     *
440
     * @param p
441
     */
442
 /*   private void addJInternalFrame(IWindow p) {
443
        WindowInfo wi = wis.getWindowInfo(p);
444

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

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

452
        addJInternalFrame(wnd, wi);
453
    }*/
454

    
455

    
456
    /**
457
     * DOCUMENT ME!
458
     *
459
     * @param p
460
     */
461
 /*   private void addJFrame(IWindow p) {
462
        WindowInfo wi = wis.getWindowInfo(p);
463

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

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

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

    
474
    /**
475
     * DOCUMENT ME!
476
     *
477
     * @param wnd
478
     */
479
    private void activateJInternalFrame(JInternalFrame wnd) {
480
        try {
481
            wnd.moveToFront();
482
//            logger.debug("Activando " + wnd.getTitle());
483
            wnd.setSelected(true);
484
            wnd.setIcon(false);
485
        } catch (PropertyVetoException e) {
486
//            logger.error(e);
487
        }
488
    }
489

    
490
    /**
491
     * DOCUMENT ME!
492
     *
493
     * @param wnd
494
     */
495
    private void activateFrame(JDialog wnd) {
496
            wnd.setVisible(true);
497
            wnd.toFront();
498

    
499
            /*int state = wnd.getExtendedState();
500
            state = (state & (~JFrame.ICONIFIED));
501
            wnd.setExtendedState(state);*/
502
    }
503

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

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

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

    
527
        centerDialog(dlg);
528

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

    
532
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
533
    }
534

    
535
    /**
536
     * DOCUMENT ME!
537
     *
538
     * @param p
539
     */
540
    private void addFrame(IWindow p) {
541
        WindowInfo wi = wis.getWindowInfo(p);
542

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

    
551
        addFrame(wnd, wi);
552
    }
553

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

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

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

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

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

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

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

    
617
    /**
618
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
619
     */
620
    public WindowInfo getWindowInfo(IWindow w) {
621
            WindowInfo wi = wis.getWindowInfo(w);
622

    
623
    /*
624
     * This is done now in vis.getWindowInfo(w)
625
     *
626
     * JInternalFrame f = fws.getJInternalFrame(w);
627
            wi.setX(f.getX());
628
            wi.setY(f.getY());
629
            wi.setHeight(f.getHeight());
630
            wi.setWidth(f.getWidth());
631
            // isClosed() doesn't work as (I) expected, why? Using isShowing instead
632
            wi.setClosed(!f.isShowing());
633
            wi.setNormalBounds(f.getNormalBounds());
634
            wi.setMaximized(f.isMaximum());*/
635
            return wi;
636
    }
637

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

    
648
        dlg.setVisible(false);
649

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

    
652
        callWindowClosed(s);
653

    
654
        fws.closeWindow(s);
655

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

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

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

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

    
697
            frame.setClosed(true);
698
            callWindowClosed(s);
699
        } catch (PropertyVetoException e) {
700
//            logger
701
//                    .error(
702
//                            "Not compatible with property veto's. Use ViewInfo instead.",
703
//                            e);
704
        }
705
    }
706

    
707
    /**
708
     * DOCUMENT ME!
709
     *
710
     * @param frame
711
     */
712
    private void closeJFrame(JDialog frame) {
713
            WindowListener[] listeners = frame.getWindowListeners();
714
            for (int i=0; i<listeners.length; i++) {
715
                    listeners[i].windowClosing(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING        ));
716
            }
717
            IWindow s = (IWindow) fws.getWindow(frame);
718
            frame.dispose();
719
            callWindowClosed(s);
720
    }
721

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

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

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

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

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

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

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

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

    
806
            if (panel!=null) {
807

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

    
812
                    wss.setActive(panel);
813

    
814
//                    if (lastActiveWindow!=null && !lastActiveWindow.equals(panel)) {
815
//                            wis.getWindowInfo(lastActiveWindow).setSelectedTools(mainFrame.getSelectedTools());
816
//                            mainFrame.setSelectedTools(wi.getSelectedTools());
817
//                    }
818
                    if (wi.getSelectedTools()==null) {
819
                        // this is the first time this window is activated
820
                        wi.setSelectedTools(new HashMap(mainFrame.getInitialSelectedTools()));
821
                }
822
                    if (lastActiveWindow!=null && !lastActiveWindow.equals(panel)) {
823
                            wis.getWindowInfo(lastActiveWindow).setSelectedTools(mainFrame.getSelectedTools());
824
                            mainFrame.setSelectedTools(wi.getSelectedTools());
825
                    }
826
                    mainFrame.enableControls();
827
                    callWindowActivated(panel);
828
            }
829

    
830
        }
831

    
832
        /**
833
         * @see java.awt.event.WindowAdapter#windowClosed(java.awt.event.WindowEvent)
834
         */
835
        public void windowClosed(WindowEvent e) {
836
        }
837

    
838

    
839
        /**
840
         * @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
841
         */
842
        public void windowClosing(WindowEvent e) {
843
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
844
            // logger.debug("internalFrameClosing " +
845
            // e.getInternalFrame().getTitle());
846

    
847
                JDialog         c = (JDialog) e.getSource();
848
                IWindow win = (IWindow) fws.getWindow(c);
849
                if (win!=null) {
850

    
851
                    WindowInfo wi = wis.getWindowInfo(win);
852

    
853
                    callWindowClosed(win);
854
                    boolean alwaysLive;
855
                    if (win instanceof SingletonWindow) {
856
                        sws.closeWindow((SingletonWindow) win);
857
                    }
858

    
859
                    fws.closeWindow(win);
860

    
861
                    panel.remove(c);
862

    
863
                    wss.remove(win);
864

    
865
                    if (!wi.isPalette())
866
                        mainFrame.enableControls();
867
                    panel.repaint();
868
                }
869

    
870
            // Para activar el JInternalFrame desde la que hemos
871
            // abierto la ventana que estamos cerrando
872
            IWindow lastWindow = wss.getActiveWindow();
873
            // La activamos
874
            if (lastWindow != null) {
875
//                    logger.debug(PluginServices.getText(this, "Devuelvo_el_foco_a_")+lastWindow.getWindowInfo().getTitle());
876
                JDialog frame = (JDialog) fws.getFrame(lastWindow);
877
                frame.toFront();
878
            }
879
        }
880

    
881
        /**
882
         * @see java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent)
883
         */
884
        public void windowDeactivated(WindowEvent e) {
885
                JDialog frame = (JDialog) e.getSource();
886
            IWindow win = fws.getWindow(frame);
887
            if (win != null) {
888
                WindowInfo wi = wis.getWindowInfo(win);
889
                if (wi.isPalette())
890
                    return;
891
                wi.setSelectedTools(mainFrame.getSelectedTools());
892
                lastActiveWindow = win;
893
            }
894
        }
895

    
896
        /**
897
         * @see java.awt.event.WindowAdapter#windowDeiconified(java.awt.event.WindowEvent)
898
         */
899
        public void windowDeiconified(WindowEvent e) {
900
            mainFrame.enableControls();
901
        }
902

    
903
        /**
904
         * @see java.awt.event.WindowAdapter#windowGainedFocus(java.awt.event.WindowEvent)
905
         */
906
        public void windowGainedFocus(WindowEvent e) {
907

    
908
        }
909

    
910
        /**
911
         * @see java.awt.event.WindowAdapter#windowIconified(java.awt.event.WindowEvent)
912
         */
913
        public void windowIconified(WindowEvent e) {
914
            mainFrame.enableControls();
915
        }
916

    
917
        /**
918
         * @see java.awt.event.WindowAdapter#windowLostFocus(java.awt.event.WindowEvent)n
919
         */
920
        public void windowLostFocus(WindowEvent e) {
921

    
922
        }
923

    
924
        /**
925
         * @see java.awt.event.WindowAdapter#windowOpened(java.awt.event.WindowEvent)
926
         */
927
        public void windowOpened(WindowEvent e) {
928
        }
929

    
930
        /**
931
         * @see java.awt.event.WindowAdapter#windowStateChanged(java.awt.event.WindowEvent)
932
         */
933
        public void windowStateChanged(WindowEvent e) {
934

    
935
        }
936
    }
937

    
938
    /**
939
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
940
     *      java.lang.Object)
941
     */
942
    public boolean closeSingletonWindow(Class viewClass, Object model) {
943
        JDialog frame = (JDialog)sws.getFrame(viewClass, model);
944
        if (frame == null)
945
            return false;
946
        closeJFrame(frame);
947
        return true;
948
    }
949

    
950
    /**
951
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
952
     */
953
    public boolean closeSingletonWindow(Object model) {
954
        JDialog[] frames = (JDialog[])sws.getFrames(model);
955
        if (frames.length == 0)
956
            return false;
957
        for (int i = 0; i < frames.length; i++) {
958
            closeJFrame(frames[i]);
959
        }
960
        return true;
961
    }
962

    
963
    /**
964
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
965
     */
966
    public IWindow[] getAllWindows() {
967
        ArrayList windows = new ArrayList();
968
        Iterator i = fws.getWindowIterator();
969

    
970
        while (i.hasNext()) {
971
            windows.add((IWindow) i.next());
972
        }
973
        return (IWindow[]) windows.toArray(new IWindow[0]);
974
    }
975

    
976
    /**
977
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
978
     */
979
    public IWindow[] getOrderedWindows() {
980
        //ArrayList windows = new ArrayList();
981
        TreeMap windows = new TreeMap();
982
        Iterator winIterator = fws.getWindowIterator();
983

    
984
        Component frame;
985
        IWindow win;
986
        /**
987
         * The order of the window in the JDesktopPane. Smaller numbers
988
         * are closer to the foreground.
989
         */
990
        int zPosition = 0;
991
        while (winIterator.hasNext()) {
992
                win = (IWindow) winIterator.next();
993
                // unfortunately we can't use this now, and I don't know how to retrieve the zPosition for JDialogs
994
                //frame = fws.getComponent(win);
995
                    //zPosition = panel.getPosition(frame);
996
                    windows.put(new Integer(zPosition++), win);
997
        }
998
        winIterator = windows.values().iterator();
999
        ArrayList winList = new ArrayList();
1000
        while (winIterator.hasNext()) {
1001
                winList.add(winIterator.next());
1002
        }
1003

    
1004
        return (IWindow[]) winList.toArray(new IWindow[0]);
1005
    }
1006

    
1007
    public void setMaximum(IWindow v, boolean bMaximum) throws PropertyVetoException
1008
    {
1009
        /*JDialog frame = fws.getFrame(v);
1010

1011
            if (bMaximum) {
1012

1013
                frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
1014
            }
1015
            else {
1016
                    int state = frame.getExtendedState();
1017
                    state = (state & (~JFrame.MAXIMIZED_BOTH));
1018
                    frame.setExtendedState(state);
1019
            }*/
1020
    }
1021

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

    
1048
                if (wi.isMaximized()) {
1049
                        frame.setBounds(wi.getNormalBounds());
1050
                //        frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
1051
                }
1052
                activateFrame(frame);
1053
   }
1054

    
1055
    public void refresh(IWindow win) {
1056
            Component frame = fws.getFrame(win);
1057
            if (frame!=null)
1058
                    frame.setVisible(true);
1059
    }
1060

    
1061
        public void setBackgroundImage(ImageIcon image, String typeDesktop) {
1062
                this.image=image;
1063
                this.typeDesktop=typeDesktop;
1064

    
1065
        }
1066
        class MyDesktopPane extends JDesktopPane
1067
    {
1068
      public MyDesktopPane(){
1069
      }
1070
      public void paintComponent(Graphics g){
1071
        super.paintComponent(g);
1072
        int x=0;
1073
        int y=0;
1074
        int w=0;
1075
        int h=0;
1076
        if (image != null) {
1077
                        if (typeDesktop.equals(Theme.CENTERED)) {
1078
                                w = image.getIconWidth();
1079
                                h = image.getIconHeight();
1080
                                x = (getWidth() - w) / 2;
1081
                                y = (getHeight() - h) / 2;
1082
                                g.drawImage(image.getImage(), x, y, w, h, this);
1083
                        } else if (typeDesktop.equals(Theme.EXPAND)) {
1084
                                w = getWidth();
1085
                                h = getHeight();
1086
                                g.drawImage(image.getImage(), x, y, w, h, this);
1087
                        } else if (typeDesktop.equals(Theme.MOSAIC)) {
1088
                                int wAux = image.getIconWidth();
1089
                                int hAux = image.getIconHeight();
1090
                                int i = 0;
1091
                                int j = 0;
1092
                                w = wAux;
1093
                                h = hAux;
1094
                                while (x < getWidth()) {
1095
                                        x = wAux * i;
1096
                                        while (y < getHeight()) {
1097
                                                y = hAux * j;
1098
                                                j++;
1099
                                                g.drawImage(image.getImage(), x, y, w, h, this);
1100
                                        }
1101
                                        y = 0;
1102
                                        j = 0;
1103
                                        i++;
1104
                                }
1105
                        }
1106
                }
1107
      }
1108
    }
1109

    
1110

    
1111
}