Statistics
| Revision:

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

History | View | Annotate | Download (29.4 KB)

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

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Container;
46
import java.awt.Cursor;
47
import java.awt.Dimension;
48
import java.awt.Graphics;
49
import java.awt.KeyEventDispatcher;
50
import java.awt.Point;
51
import java.awt.event.ActionEvent;
52
import java.awt.event.ActionListener;
53
import java.awt.event.KeyEvent;
54
import java.awt.event.WindowAdapter;
55
import java.awt.event.WindowEvent;
56
import java.beans.PropertyVetoException;
57
import java.util.ArrayList;
58
import java.util.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
    /* (non-javadoc)
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
    /* (non-javadoc)
251
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
252
     */
253
        public IWindow addCentredWindow(IWindow p) throws SingletonDialogAlreadyShownException {
254
                IWindow window = addWindow(p);
255
                centreFrame(window);
256
                return window;
257
        }
258
        
259
 
260
        /**
261
         * Centres the Frame in the contentPane of the MainFrame.
262
         * If the frame can't be showed completely, it tries to show its top-left
263
         * corner.
264
         *
265
         * @author Pablo Piqueras Bartolom?
266
         *
267
         * @param panel The IWindow to centre
268
         */
269
        public synchronized void centreFrame(IWindow panel) {
270
                Component window = fws.getFrame(panel);
271
                if (window==null) return;
272
                
273
                // The top-left square of frame reference
274
                Point newReferencePoint = new Point();
275

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

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

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

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

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

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

    
313

    
314
    /**
315
     * DOCUMENT ME!
316
     *
317
     * @param wnd
318
     *            DOCUMENT ME!
319
     * @param wi
320
     *            DOCUMENT ME!
321
     */
322
    private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
323
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
324
        wnd.addInternalFrameListener(new FrameListener());
325
        
326
        updateFrameProperties(wnd, wi);
327
        
328
        if (wi.isModeless() || wi.isPalette()) {
329
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
330
            if (wi.isPalette())
331
                wnd.setFocusable(false);
332
        } else {
333
            panel.add(wnd);
334
        }
335
        activateJInternalFrame(wnd);
336
        try{
337
                wnd.setMaximum(wi.isMaximized());
338
        }catch(Exception ex){
339
                logger.warn("Error: ", ex);
340
        }
341
    }
342

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

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

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

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

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

    
398
        addJInternalFrame(wnd, wi);
399
    }
400

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

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

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

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

    
441
        centerDialog(dlg);
442

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

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

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

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

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

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

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

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

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

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

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

    
529
    /**
530
     * DOCUMENT ME!
531
     *
532
     * @param dialog
533
     * @throws RuntimeException
534
     *             DOCUMENT ME!
535
     */
536
    private void closeJDialog() {
537
        JDialog dlg = dss.popDialog();
538

    
539
        dlg.setVisible(false);
540

    
541
        IWindow s = (IWindow) fws.getWindow(dlg);
542

    
543
        callWindowClosed(s);
544

    
545
        fws.closeWindow(s);
546

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
698
            // activatedInternalFramesStack.push(e.getInternalFrame());
699

    
700
            IWindow panel = fws.getWindow((JInternalFrame) e.getSource());
701

    
702
            WindowInfo wi = wis.getWindowInfo(panel);
703
            if (wi.isPalette())
704
                return;
705

    
706
            wss.setActive(panel);
707

    
708
            JInternalFrame frame = fws.getJInternalFrame(panel);
709

    
710
            if (wi.isMaximizable()){
711
                    frame.setMaximizable(true);
712
            }
713
            if (!frame.isMaximizable() && frame.isMaximum()) {
714
                try {
715
                    frame.setMaximum(false);
716
                } catch (PropertyVetoException e1) {
717
                }
718
            }
719
            mainFrame.enableControls();
720
            if (wi.getSelectedTool() != null)
721
                mainFrame.setSelectedTool(wi.getSelectedTool());
722
            callWindowActivated(panel);
723

    
724
        }
725

    
726
        /**
727
         * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
728
         */
729
        public void internalFrameClosed(InternalFrameEvent e) {
730
        }
731

    
732
        /**
733
         * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
734
         */
735
        public void internalFrameClosing(InternalFrameEvent e) {
736
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
737
            // logger.debug("internalFrameClosing " +
738
            // e.getInternalFrame().getTitle());
739

    
740
            JInternalFrame c = (JInternalFrame) e.getSource();
741
            WindowInfo wi = wis.getWindowInfo((IWindow) fws.getWindow(c));
742

    
743
            IWindow win = fws.getWindow(c);
744
            callWindowClosed(win);
745
            boolean alwaysLive;
746
            if (win instanceof SingletonWindow) {
747
                sws.closeWindow((SingletonWindow) win);
748
            }
749

    
750
            fws.closeWindow(win);
751

    
752
            panel.remove(c);
753

    
754
            wss.remove(win);
755

    
756
            if (!wi.isPalette())
757
                mainFrame.enableControls();
758
            panel.repaint();
759

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

    
776
        }
777

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

    
793
        }
794

    
795
        /**
796
         * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
797
         */
798
        public void internalFrameDeiconified(InternalFrameEvent e) {
799
            mainFrame.enableControls();
800
        }
801

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

    
809
        /**
810
         * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
811
         */
812
        public void internalFrameOpened(InternalFrameEvent e) {
813
            // logger.debug("internalFrameOpened. Source= " +
814
            // e.getSource().toString());
815
        }
816
    }
817

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

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

    
843
    /**
844
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
845
     */
846
    public IWindow[] getAllWindows() {
847
        ArrayList windows = new ArrayList();
848
        Iterator i = fws.getWindowIterator();
849

    
850
        while (i.hasNext()) {
851
            windows.add((IWindow) i.next());
852
        }
853
        return (IWindow[]) windows.toArray(new IWindow[0]);
854
    }
855

    
856
    /**
857
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
858
     */
859
    public IWindow[] getOrderedWindows() {
860
        //ArrayList windows = new ArrayList();
861
        TreeMap windows = new TreeMap();
862
        Iterator winIterator = fws.getWindowIterator();
863

    
864
        Component frame;
865
        IWindow win;
866
        /**
867
         * The order of the window in the JDesktopPane. Smaller numbers
868
         * are closer to the foreground.
869
         */
870
        int zPosition;
871
        while (winIterator.hasNext()) {
872
                win = (IWindow) winIterator.next();
873
                frame = fws.getFrame(win);
874
                    zPosition = panel.getPosition(frame);
875
                    windows.put(new Integer(zPosition), win);
876
        }
877
        winIterator = windows.values().iterator();
878
        ArrayList winList = new ArrayList();
879
        while (winIterator.hasNext()) {
880
                winList.add(winIterator.next());
881
        }
882

    
883
        return (IWindow[]) winList.toArray(new IWindow[0]);
884
    }
885
    public void setMaximum(IWindow v, boolean bMaximum) throws PropertyVetoException
886
    {
887
        JInternalFrame f = fws.getJInternalFrame(v);
888
        f.setMaximum(bMaximum);
889
    }
890

    
891
    public void changeWindowInfo(IWindow w, WindowInfo wi){
892
            JInternalFrame f = fws.getJInternalFrame(w);
893
                f.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
894
                JDesktopPane pnl = f.getDesktopPane();
895
                pnl.remove(f);
896
                int width;
897
                int height;
898
                if (wi.getWidth()!=-1)
899
                        width = wi.getWidth();
900
                else
901
                        width = f.getWidth();
902
                if (wi.getHeight()!=-1)
903
                        height = wi.getHeight();
904
                else
905
                        height = f.getHeight();
906
                f.setSize(new Dimension(width, height));
907
                f.setLocation(wi.getX(), wi.getY());
908
                if (wi.isPalette()) {
909
                        pnl.add(f, JDesktopPane.PALETTE_LAYER);
910
                        f.setFocusable(false);
911
                } else {
912
                        pnl.add(f, JDesktopPane.DEFAULT_LAYER);
913
                        f.setFocusable(true);
914
                        if (wi.isClosed()) {
915
                                closeWindow(w);
916
                        }
917
                }
918

    
919
                if (wi.isMaximized()) {
920
                        try {
921
                                f.setMaximum(true);
922
                        } catch (PropertyVetoException e) {
923
                                // TODO Auto-generated catch block
924
                                //e.printStackTrace();
925
                        }
926
                        f.setNormalBounds(wi.getNormalBounds());
927
                }
928
                activateJInternalFrame(f);
929
   }
930

    
931
        public void setBackgroundImage(ImageIcon image, String typeDesktop) {
932
                this.image=image;
933
                this.typeDesktop=typeDesktop;
934

    
935
        }
936
        class MyDesktopPane extends JDesktopPane
937
            {
938
              public MyDesktopPane(){
939
              }
940
              public void paintComponent(Graphics g){
941
                super.paintComponent(g);
942
                int x=0;
943
                int y=0;
944
                int w=0;
945
                int h=0;
946
                if (image != null) {
947
                                if (typeDesktop.equals(Theme.CENTERED)) {
948
                                        w = image.getIconWidth();
949
                                        h = image.getIconHeight();
950
                                        x = (getWidth() - w) / 2;
951
                                        y = (getHeight() - h) / 2;
952
                                        g.drawImage(image.getImage(), x, y, w, h, this);
953
                                } else if (typeDesktop.equals(Theme.EXPAND)) {
954
                                        w = getWidth();
955
                                        h = getHeight();
956
                                        g.drawImage(image.getImage(), x, y, w, h, this);
957
                                } else if (typeDesktop.equals(Theme.MOSAIC)) {
958
                                        int wAux = image.getIconWidth();
959
                                        int hAux = image.getIconHeight();
960
                                        int i = 0;
961
                                        int j = 0;
962
                                        w = wAux;
963
                                        h = hAux;
964
                                        while (x < getWidth()) {
965
                                                x = wAux * i;
966
                                                while (y < getHeight()) {
967
                                                        y = hAux * j;
968
                                                        j++;
969
                                                        g.drawImage(image.getImage(), x, y, w, h, this);
970
                                                }
971
                                                y = 0;
972
                                                j = 0;
973
                                                i++;
974
                                        }
975
                                }
976
                        }
977
              }
978
            }
979
}