Statistics
| Revision:

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

History | View | Annotate | Download (31.8 KB)

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

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Container;
46
import java.awt.Cursor;
47
import java.awt.Dimension;
48
import java.awt.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.Iterator;
59
import java.util.TreeMap;
60

    
61
import javax.swing.DefaultDesktopManager;
62
import javax.swing.DesktopManager;
63
import javax.swing.ImageIcon;
64
import javax.swing.JDesktopPane;
65
import javax.swing.JDialog;
66
import javax.swing.JFrame;
67
import javax.swing.JInternalFrame;
68
import javax.swing.JPopupMenu;
69
import javax.swing.KeyStroke;
70
import javax.swing.event.InternalFrameEvent;
71
import javax.swing.event.InternalFrameListener;
72

    
73
import org.apache.log4j.Logger;
74

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

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

    
98

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

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

    
109
    /** Panel de la MDIFrame */
110
    private MyDesktopPane panel = new MyDesktopPane();
111

    
112
    /** MDIFrame */
113
    private MDIFrame mainFrame;
114

    
115
    private GlassPane glassPane = new GlassPane();
116

    
117
    private DialogStackSupport dss;
118

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

    
124
    private WindowInfoSupport wis;
125

    
126
    private WindowStackSupport wss;
127

    
128
    private SingletonWindowSupport sws;
129

    
130
    private Cursor lastCursor = null;
131
        private ImageIcon image;
132
        private String typeDesktop;
133

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

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

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

    
153

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

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

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

    
166

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

    
178
        });
179
    }
180

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

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

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

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

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

    
246
            return p;
247
        }
248
    }
249

    
250
    /**
251
     * Similar method as 'addView' but in this case centres the new JInternalFrame of the View in the contentPane of the MainFrame
252
     *
253
     * @see com.iver.core.mdiManager.NewSkin#addWindow(com.iver.andami.ui.mdiManager.IWindow)
254
     *
255
     * @author Pablo Piqueras Bartolom?
256
     */
257
        public IWindow addCentredWindow(IWindow p) throws SingletonDialogAlreadyShownException {
258
            // se obtiene la informaci?n de la vista
259
        WindowInfo vi = wis.getWindowInfo(p);
260

    
261
        // Se comprueban las incompatibilidades que pudieran haber en la vista
262
        MDIUtilities.checkWindowInfo(vi);
263
        if ((p instanceof SingletonWindow) && (vi.isModal())) {
264
            throw new RuntimeException("A modal view cannot be a SingletonView");
265
        }
266

    
267
        /*
268
         * Se obtiene la referencia a la vista anterior por si es una singleton
269
         * y est? siendo mostrada. Se obtiene su informaci?n si ya fue mostrada
270
         */
271
        boolean singletonPreviouslyAdded = false;
272

    
273
        if (p instanceof SingletonWindow) {
274
            SingletonWindow sv = (SingletonWindow) p;
275
            if (sws.registerWindow(sv.getClass(), sv.getWindowModel(), wis
276
                    .getWindowInfo(sv))) {
277
                singletonPreviouslyAdded = true;
278
            }
279
        }
280

    
281
        if (singletonPreviouslyAdded) {
282
            // Si la vista no est? actualmente abierta
283
            if (!sws.contains((SingletonWindow) p)) {
284
                JInternalFrame frame = fws.getJInternalFrame(p);
285
                sws.openSingletonWindow((SingletonWindow) p, frame);
286
                addJInternalFrame(frame, vi);
287
                wss.add(p, new ActionListener() {
288
                    public void actionPerformed(ActionEvent e) {
289
                        IWindow w = wis.getWindowById(Integer.parseInt(e
290
                                .getActionCommand()));
291
                        JInternalFrame f = fws.getJInternalFrame(w);
292
                        centreJInternalFrame(f); // Centre this JInternalFrame
293
                        activateJInternalFrame(f);
294
                    }
295
                });
296
                return p;
297
            } else {
298
                // La vista est? actualmente abierta
299
                JInternalFrame frame = (JInternalFrame) sws
300
                        .getFrame((SingletonWindow) p);
301
                activateJInternalFrame(frame);
302
                centreJInternalFrame(frame); // Centre this JInternalFrame
303
                wss.setActive(p);
304
                return fws.getWindow((JInternalFrame) frame);
305
            }
306
        } else {
307
                centreJInternalFrame(fws.getJInternalFrame(p)); // Centre this JInternalFrame
308
            if (vi.isModal()) {
309
                addJDialog(p);
310
            } else {
311
                // Se sit?a la vista en la pila de vistas
312
                wss.add(p, new ActionListener() {
313
                    public void actionPerformed(ActionEvent e) {
314
                        IWindow w = wis.getWindowById(Integer.parseInt(e
315
                                .getActionCommand()));
316
                        JInternalFrame f = fws.getJInternalFrame(w);
317
                        activateJInternalFrame(f);
318
                    }
319
                });
320
                addJInternalFrame(p);
321
            }
322

    
323
            return p;
324
        }
325
        }
326

    
327
        /**
328
         * Centres the JInternalFrame in the center of the contentPane of the MainFrame
329
         * If it can't be showed completely the JInternalFrame, tries that the top-left corner of the JInternalFrame would be showed
330
         *
331
         * @author Pablo Piqueras Bartolom?
332
         *
333
         * @param jInternalFrame A reference to the frame to centring
334
         */
335
        private static synchronized void centreJInternalFrame(JInternalFrame jInternalFrame) {
336

    
337
                // The top-left square of frame reference
338
                Point newReferencePoint = new Point();
339

    
340
                // A reference to the panel where the JInternalFrame will be displayed
341
                Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
342

    
343
                // Get the NewStatusBar component
344
                NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
345
                JDesktopPane jDesktopPane = ((JDesktopPane)contentPane.getComponent(2));
346

    
347
                int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The last substraction is for if there is any menu,... at left
348
                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
349
                int freeWidth = visibleWidth - jInternalFrame.getWidth();
350
                int freeHeight = visibleHeight - jInternalFrame.getHeight();
351

    
352
                // Calculate the new point reference (Assure that the top-left corner is showed)
353
                if (freeWidth < 0)
354
                {
355
                        if (visibleWidth > MinimumXMargin)
356
                                newReferencePoint.x = DefaultXMargin;
357
                        else
358
                                newReferencePoint.x = 0;
359
                }
360
                else
361
                        newReferencePoint.x = freeWidth / 2;
362

    
363
                if (freeHeight < 0)
364
                {
365
                        if (visibleHeight > MinimumYMargin)
366
                                newReferencePoint.y = DefaultYMargin;
367
                        else
368
                                newReferencePoint.y = 0;
369
                }
370
                else
371
                        newReferencePoint.y = freeHeight / 2;
372

    
373
                // Set the new location for this JInternalFrame
374
                jInternalFrame.setLocation(newReferencePoint);
375
        }
376

    
377

    
378
    /**
379
     * DOCUMENT ME!
380
     *
381
     * @param wnd
382
     *            DOCUMENT ME!
383
     * @param wi
384
     *            DOCUMENT ME!
385
     */
386
    private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
387
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
388
        wnd.addInternalFrameListener(new FrameListener());
389

    
390
        if (wi.isModeless() || wi.isPalette()) {
391
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
392
            if (wi.isPalette())
393
                wnd.setFocusable(false);
394
        } else {
395
            panel.add(wnd);
396
        }
397
        updateFrameProperties(wnd, wi);
398

    
399
        activateJInternalFrame(wnd);
400
        try{
401
                wnd.setMaximum(wi.isMaximized());
402
        }catch(Exception ex){
403
                logger.warn("Error: ", ex);
404
        }
405
    }
406

    
407
    private void updateFrameProperties(JInternalFrame frame, WindowInfo wi) {
408
            int height, width;
409
            if (wi.isMaximized()) {
410
                    if (wi.getNormalWidth()!=-1)
411
                            width = wi.getNormalWidth();
412
                    else
413
                            width = frame.getNormalBounds().width;
414
                    if (wi.getNormalHeight()!=-1)
415
                            height = wi.getNormalHeight();
416
                    else
417
                            height = frame.getNormalBounds().height;
418

    
419
                    frame.setSize(width, height);
420
                    frame.setLocation(wi.getNormalX(), wi.getNormalY());
421
            }
422
            else {
423
                    if (wi.getWidth()!=-1)
424
                            width = wi.getWidth();
425
                    else
426
                            width = frame.getWidth();
427
                    if (wi.getHeight()!=-1)
428
                            height = wi.getHeight();
429
                    else
430
                            height = frame.getHeight();
431
                    frame.setSize(width, height);
432
                frame.setLocation(wi.getX(), wi.getY());
433
            }
434
            frame.setTitle(wi.getTitle());
435
            frame.setVisible(wi.isVisible());
436
            frame.setResizable(wi.isResizable());
437
            frame.setIconifiable(wi.isIconifiable());
438
            frame.setMaximizable(wi.isMaximizable());
439
            try {
440
                        frame.setMaximum(wi.isMaximized());
441
                } catch (PropertyVetoException e) {
442
                        // TODO Auto-generated catch block
443
                        //e.printStackTrace();
444
                }
445
    }
446

    
447
    /**
448
     * DOCUMENT ME!
449
     *
450
     * @param p
451
     */
452
    private void addJInternalFrame(IWindow p) {
453
        WindowInfo wi = wis.getWindowInfo(p);
454

    
455
        JInternalFrame wnd = fws.getJInternalFrame(p);
456

    
457
        if (p instanceof SingletonWindow) {
458
            SingletonWindow sv = (SingletonWindow) p;
459
            sws.openSingletonWindow(sv, wnd);
460
        }
461

    
462
        addJInternalFrame(wnd, wi);
463
    }
464

    
465
    /**
466
     * DOCUMENT ME!
467
     *
468
     * @param wnd
469
     */
470
    private void activateJInternalFrame(JInternalFrame wnd) {
471
            JPopupMenu.setDefaultLightWeightPopupEnabled(false);
472
        try {
473
            wnd.moveToFront();
474
            logger.debug("Activando " + wnd.getTitle());
475
            wnd.setSelected(true);
476
            wnd.setIcon(false);
477
        } catch (PropertyVetoException e) {
478
            logger.error(e);
479
        }
480
    }
481

    
482
    /**
483
     * Situa un di?logo modal en el centro de la pantalla
484
     *
485
     * @param d
486
     *            Di?logo que se quiere situar
487
     */
488
    private void centerDialog(JDialog d) {
489
        int offSetX = d.getWidth() / 2;
490
        int offSetY = d.getHeight() / 2;
491

    
492
        d.setLocation((mainFrame.getWidth() / 2) - offSetX, (mainFrame
493
                .getHeight() / 2)
494
                - offSetY);
495
    }
496

    
497
    /**
498
     * DOCUMENT ME!
499
     *
500
     * @param p
501
     */
502
    private void addJDialog(IWindow p) {
503
        JDialog dlg = fws.getJDialog(p);
504

    
505
        centerDialog(dlg);
506

    
507
        dlg.addWindowListener(new DialogWindowListener());
508
        dss.pushDialog(dlg);
509

    
510
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
511
    }
512

    
513
    /**
514
     * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveWindow()
515
     */
516
    public IWindow getActiveWindow() {
517
        JInternalFrame jif = panel.getSelectedFrame();
518

    
519
        if (jif != null) {
520
            IWindow theWindow = fws.getWindow(jif);
521
            if (theWindow == null)
522
                return null;
523
            if (theWindow.getWindowInfo().isPalette())
524
                return wss.getActiveWindow();
525
            else
526
                return fws.getWindow(jif);
527
        }
528
        // return vss.getActiveView();
529

    
530
        return null;
531
    }
532
    public IWindow getFocusWindow(){
533
             JInternalFrame jif = panel.getSelectedFrame();
534

    
535
         if (jif != null) {
536
             IWindow theView = fws.getWindow(jif);
537
             if (theView == null)
538
                 return null;
539
             return fws.getWindow(jif);
540
         }
541
         return null;
542
    }
543
    /**
544
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeWindow(com.iver.andami.ui.mdiManager.IWindow)
545
     */
546
    public void closeWindow(IWindow p) {
547
        // Si es un di?logo modal
548
        if (p.getWindowInfo().isModal()) {
549
            closeJDialog();
550
        } else { // Si no es modal se cierra el JInternalFrame
551
            closeJInternalFrame(fws.getJInternalFrame(p));
552
        }
553
    }
554

    
555
    /**
556
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllWindows()
557
     */
558
    public void closeAllWindows() {
559
        ArrayList eliminar = new ArrayList();
560
        Iterator i = fws.getWindowIterator();
561

    
562
        while (i.hasNext()) {
563
            eliminar.add((IWindow) i.next());
564
        }
565

    
566
        for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
567
            IWindow vista = (IWindow) iter.next();
568
            closeWindow(vista);
569
        }
570
    }
571

    
572
    /**
573
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
574
     */
575
    public WindowInfo getWindowInfo(IWindow w) {
576
            WindowInfo wi = wis.getWindowInfo(w);
577

    
578
    /*
579
     * This is done now in vis.getWindowInfo(w)
580
     *
581
     * JInternalFrame f = fws.getJInternalFrame(w);
582
            wi.setX(f.getX());
583
            wi.setY(f.getY());
584
            wi.setHeight(f.getHeight());
585
            wi.setWidth(f.getWidth());
586
            // isClosed() doesn't work as (I) expected, why? Using isShowing instead
587
            wi.setClosed(!f.isShowing());
588
            wi.setNormalBounds(f.getNormalBounds());
589
            wi.setMaximized(f.isMaximum());*/
590
            return wi;
591
    }
592

    
593
    /**
594
     * DOCUMENT ME!
595
     *
596
     * @param dialog
597
     * @throws RuntimeException
598
     *             DOCUMENT ME!
599
     */
600
    private void closeJDialog() {
601
        JDialog dlg = dss.popDialog();
602

    
603
        dlg.setVisible(false);
604

    
605
        IWindow s = (IWindow) fws.getWindow(dlg);
606

    
607
        callWindowClosed(s);
608

    
609
        fws.closeWindow(s);
610

    
611
        // Si es singleton se desasocia el modelo con la vista
612
        if (s instanceof SingletonWindow) {
613
            sws.closeWindow((SingletonWindow) s);
614
        }
615
    }
616

    
617
    /**
618
     * DOCUMENT ME!
619
     *
620
     * @param window
621
     *            DOCUMENT ME!
622
     */
623
    private void callWindowClosed(IWindow window) {
624
        if (window instanceof IWindowListener) {
625
            ((IWindowListener) window).windowClosed();
626
        }
627
    }
628

    
629
    /**
630
     * DOCUMENT ME!
631
     *
632
     * @param window
633
     *            DOCUMENT ME!
634
     */
635
    private void callWindowActivated(IWindow window) {
636
        logger.debug("View " + window.getWindowInfo().getTitle()
637
                + " activated (callViewActivated)");
638
        if (window instanceof IWindowListener) {
639
            ((IWindowListener) window).windowActivated();
640
        }
641
    }
642

    
643
    /**
644
     * DOCUMENT ME!
645
     *
646
     * @param frame
647
     */
648
    private void closeJInternalFrame(JInternalFrame frame) {
649
        try {
650
            IWindow s = (IWindow) fws.getWindow(frame);
651

    
652
            frame.setClosed(true);
653
            callWindowClosed(s);
654
        } catch (PropertyVetoException e) {
655
            logger
656
                    .error(
657
                            "Not compatible with property veto's. Use ViewInfo instead.",
658
                            e);
659
        }
660
    }
661

    
662
    /**
663
     * @see com.iver.andami.plugins.IExtension#initialize()
664
     */
665
    public void initialize() {
666
    }
667

    
668
    /**
669
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
670
     */
671
    public void execute(String actionCommand) {
672
        if (actionCommand.equals("cascada")) {
673
        } else if (actionCommand.equals("mosaico")) {
674
        }
675
    }
676

    
677
    /**
678
     * @see com.iver.andami.plugins.IExtension#isEnabled()
679
     */
680
    public boolean isEnabled() {
681
        // TODO Auto-generated method stub
682
        return false;
683
    }
684

    
685
    /**
686
     * @see com.iver.andami.plugins.IExtension#isVisible()
687
     */
688
    public boolean isVisible() {
689
        // TODO Auto-generated method stub
690
        return true;
691
    }
692

    
693
    /**
694
     * @see com.iver.andami.ui.mdiManager.MDIManager#setWaitCursor()
695
     */
696
    public void setWaitCursor() {
697
        if (mainFrame != null) {
698
            glassPane.setVisible(true);
699
            lastCursor = mainFrame.getCursor();
700
            dss.setWaitCursor();
701
            glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
702
        }
703
    }
704

    
705
    /**
706
     * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
707
     */
708
    public void restoreCursor() {
709
        if (mainFrame != null) {
710
            glassPane.setVisible(false);
711
            dss.restoreCursor();
712
            glassPane.setCursor(lastCursor);
713
        }
714
    }
715

    
716
    /**
717
     * Listener para los eventos de cerrado de los di?logos. Tiene su raz?n de
718
     * ser en que los di?logos han de devolverse al pool cuando se cierran
719
     *
720
     * @author Fernando Gonz?lez Cort?s
721
     */
722
    public class DialogWindowListener extends WindowAdapter {
723
        /**
724
         * Captura el evento de cerrado de los di?logos con el fin de realizar
725
         * tareas de mantenimiento
726
         *
727
         * @param e
728
         *            evento
729
         */
730
        public void windowClosing(WindowEvent e) {
731
            closeJDialog();
732
        }
733
    }
734

    
735
    /**
736
     * DOCUMENT ME!
737
     */
738
    public class FrameListener implements InternalFrameListener {
739
        /**
740
         * @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
741
         */
742
        public void internalFrameActivated(InternalFrameEvent e) {
743
            // logger.debug("internalFrameActivated " +
744
            // e.getInternalFrame().getTitle());
745

    
746
            // activatedInternalFramesStack.push(e.getInternalFrame());
747

    
748
            IWindow panel = fws.getWindow((JInternalFrame) e.getSource());
749

    
750
            WindowInfo wi = wis.getWindowInfo(panel);
751
            if (wi.isPalette())
752
                return;
753

    
754
            wss.setActive(panel);
755

    
756
            JInternalFrame frame = fws.getJInternalFrame(panel);
757

    
758
            if (wi.isMaximizable()){
759
                    frame.setMaximizable(true);
760
            }
761
            if (!frame.isMaximizable() && frame.isMaximum()) {
762
                try {
763
                    frame.setMaximum(false);
764
                } catch (PropertyVetoException e1) {
765
                }
766
            }
767
            mainFrame.enableControls();
768
            if (wi.getSelectedTool() != null)
769
                mainFrame.setSelectedTool(wi.getSelectedTool());
770
            callWindowActivated(panel);
771

    
772
        }
773

    
774
        /**
775
         * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
776
         */
777
        public void internalFrameClosed(InternalFrameEvent e) {
778
        }
779

    
780
        /**
781
         * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
782
         */
783
        public void internalFrameClosing(InternalFrameEvent e) {
784
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
785
            // logger.debug("internalFrameClosing " +
786
            // e.getInternalFrame().getTitle());
787

    
788
            JInternalFrame c = (JInternalFrame) e.getSource();
789
            WindowInfo wi = wis.getWindowInfo((IWindow) fws.getWindow(c));
790

    
791
            IWindow win = fws.getWindow(c);
792
            callWindowClosed(win);
793
            boolean alwaysLive;
794
            if (win instanceof SingletonWindow) {
795
                sws.closeWindow((SingletonWindow) win);
796
            }
797

    
798
            fws.closeWindow(win);
799

    
800
            panel.remove(c);
801

    
802
            wss.remove(win);
803

    
804
            if (!wi.isPalette())
805
                mainFrame.enableControls();
806
            panel.repaint();
807

    
808
            // Para activar el JInternalFrame desde la que hemos
809
            // abierto la ventana que estamos cerrando
810
            IWindow lastWindow = wss.getActiveWindow();
811
            // La activamos
812
            if (lastWindow != null) {
813
                    logger.debug(PluginServices.getText(this, "Devuelvo_el_foco_a_")+lastWindow.getWindowInfo().getTitle());
814
                JInternalFrame frame = fws.getJInternalFrame(lastWindow);
815
                try {
816
                    frame.setSelected(true);
817
                } catch (PropertyVetoException e1) {
818
                    // TODO Auto-generated catch block
819
                    // e1.printStackTrace();
820
                }
821
                // addView(lastView);
822
            }
823

    
824
        }
825

    
826
        /**
827
         * @see javax.swing.event.InternalFrameListener#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)
828
         */
829
        public void internalFrameDeactivated(InternalFrameEvent e) {
830
            // logger.debug("internalDeActivated " +
831
            // e.getInternalFrame().getTitle());
832
            JInternalFrame c = (JInternalFrame) e.getSource();
833
            IWindow win = fws.getWindow(c);
834
            if (win != null) {
835
                WindowInfo wi = wis.getWindowInfo(win);
836
                if (wi.isPalette())
837
                    return;
838
                wi.setSelectedTool(mainFrame.getSelectedTool());
839
            }
840

    
841
        }
842

    
843
        /**
844
         * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
845
         */
846
        public void internalFrameDeiconified(InternalFrameEvent e) {
847
            mainFrame.enableControls();
848
        }
849

    
850
        /**
851
         * @see javax.swing.event.InternalFrameListener#internalFrameIconified(javax.swing.event.InternalFrameEvent)
852
         */
853
        public void internalFrameIconified(InternalFrameEvent e) {
854
            mainFrame.enableControls();
855
        }
856

    
857
        /**
858
         * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
859
         */
860
        public void internalFrameOpened(InternalFrameEvent e) {
861
            // logger.debug("internalFrameOpened. Source= " +
862
            // e.getSource().toString());
863
        }
864
    }
865

    
866
    /**
867
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
868
     *      java.lang.Object)
869
     */
870
    public boolean closeSingletonWindow(Class viewClass, Object model) {
871
        JInternalFrame frame = sws.getFrame(viewClass, model);
872
        if (frame == null)
873
            return false;
874
        closeJInternalFrame(frame);
875
        return true;
876
    }
877

    
878
    /**
879
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
880
     */
881
    public boolean closeSingletonWindow(Object model) {
882
        JInternalFrame[] frames = sws.getFrames(model);
883
        if (frames.length == 0)
884
            return false;
885
        for (int i = 0; i < frames.length; i++) {
886
            closeJInternalFrame(frames[i]);
887
        }
888
        return true;
889
    }
890

    
891
    /**
892
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
893
     */
894
    public IWindow[] getAllWindows() {
895
        ArrayList windows = new ArrayList();
896
        Iterator i = fws.getWindowIterator();
897

    
898
        while (i.hasNext()) {
899
            windows.add((IWindow) i.next());
900
        }
901
        return (IWindow[]) windows.toArray(new IWindow[0]);
902
    }
903

    
904
    /**
905
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
906
     */
907
    public IWindow[] getOrderedWindows() {
908
        //ArrayList windows = new ArrayList();
909
        TreeMap windows = new TreeMap();
910
        Iterator winIterator = fws.getWindowIterator();
911

    
912
        Component frame;
913
        IWindow win;
914
        /**
915
         * The order of the window in the JDesktopPane. Smaller numbers
916
         * are closer to the foreground.
917
         */
918
        int zPosition;
919
        while (winIterator.hasNext()) {
920
                win = (IWindow) winIterator.next();
921
                frame = fws.getFrame(win);
922
                    zPosition = panel.getPosition(frame);
923
                    windows.put(new Integer(zPosition), win);
924
        }
925
        winIterator = windows.values().iterator();
926
        ArrayList winList = new ArrayList();
927
        while (winIterator.hasNext()) {
928
                winList.add(winIterator.next());
929
        }
930

    
931
        return (IWindow[]) winList.toArray(new IWindow[0]);
932
    }
933
    public void setMaximum(IWindow v, boolean bMaximum) throws PropertyVetoException
934
    {
935
        JInternalFrame f = fws.getJInternalFrame(v);
936
        f.setMaximum(bMaximum);
937
    }
938

    
939
    public void changeWindowInfo(IWindow w, WindowInfo wi){
940
            JInternalFrame f = fws.getJInternalFrame(w);
941
                f.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
942
                JDesktopPane pnl = f.getDesktopPane();
943
                pnl.remove(f);
944
                int width;
945
                int height;
946
                if (wi.getWidth()!=-1)
947
                        width = wi.getWidth();
948
                else
949
                        width = f.getWidth();
950
                if (wi.getHeight()!=-1)
951
                        height = wi.getHeight();
952
                else
953
                        height = f.getHeight();
954
                f.setSize(new Dimension(width, height));
955
                f.setLocation(wi.getX(), wi.getY());
956
                if (wi.isPalette()) {
957
                        pnl.add(f, JDesktopPane.PALETTE_LAYER);
958
                        f.setFocusable(false);
959
                } else {
960
                        pnl.add(f, JDesktopPane.DEFAULT_LAYER);
961
                        f.setFocusable(true);
962
                        if (wi.isClosed()) {
963
                                closeWindow(w);
964
                        }
965
                }
966

    
967
                if (wi.isMaximized()) {
968
                        try {
969
                                f.setMaximum(true);
970
                        } catch (PropertyVetoException e) {
971
                                // TODO Auto-generated catch block
972
                                //e.printStackTrace();
973
                        }
974
                        f.setNormalBounds(wi.getNormalBounds());
975
                }
976
                activateJInternalFrame(f);
977
   }
978

    
979
        public void setBackgroundImage(ImageIcon image, String typeDesktop) {
980
                this.image=image;
981
                this.typeDesktop=typeDesktop;
982

    
983
        }
984
        class MyDesktopPane extends JDesktopPane
985
            {
986
              public MyDesktopPane(){
987
              }
988
              public void paintComponent(Graphics g){
989
                super.paintComponent(g);
990
                int x=0;
991
                int y=0;
992
                int w=0;
993
                int h=0;
994
                if (image != null) {
995
                                if (typeDesktop.equals(Theme.CENTERED)) {
996
                                        w = image.getIconWidth();
997
                                        h = image.getIconHeight();
998
                                        x = (getWidth() - w) / 2;
999
                                        y = (getHeight() - h) / 2;
1000
                                        g.drawImage(image.getImage(), x, y, w, h, this);
1001
                                } else if (typeDesktop.equals(Theme.EXPAND)) {
1002
                                        w = getWidth();
1003
                                        h = getHeight();
1004
                                        g.drawImage(image.getImage(), x, y, w, h, this);
1005
                                } else if (typeDesktop.equals(Theme.MOSAIC)) {
1006
                                        int wAux = image.getIconWidth();
1007
                                        int hAux = image.getIconHeight();
1008
                                        int i = 0;
1009
                                        int j = 0;
1010
                                        w = wAux;
1011
                                        h = hAux;
1012
                                        while (x < getWidth()) {
1013
                                                x = wAux * i;
1014
                                                while (y < getHeight()) {
1015
                                                        y = hAux * j;
1016
                                                        j++;
1017
                                                        g.drawImage(image.getImage(), x, y, w, h, this);
1018
                                                }
1019
                                                y = 0;
1020
                                                j = 0;
1021
                                                i++;
1022
                                        }
1023
                                }
1024
                        }
1025
              }
1026
            }
1027
}