Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libCorePlugin / src / org / gvsig / coreplugin / mdiManager / NewSkin.java @ 36749

History | View | Annotate | Download (38.1 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.coreplugin.mdiManager;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Color;
45
import java.awt.Component;
46
import java.awt.Container;
47
import java.awt.Cursor;
48
import java.awt.Dimension;
49
import java.awt.Graphics;
50
import java.awt.GridBagConstraints;
51
import java.awt.KeyEventDispatcher;
52
import java.awt.Point;
53
import java.awt.event.ActionEvent;
54
import java.awt.event.ActionListener;
55
import java.awt.event.ComponentEvent;
56
import java.awt.event.ComponentListener;
57
import java.awt.event.KeyEvent;
58
import java.awt.event.WindowAdapter;
59
import java.awt.event.WindowEvent;
60
import java.beans.PropertyVetoException;
61
import java.util.ArrayList;
62
import java.util.HashMap;
63
import java.util.Hashtable;
64
import java.util.Iterator;
65
import java.util.Map;
66
import java.util.TreeMap;
67

    
68
import javax.swing.DefaultDesktopManager;
69
import javax.swing.DesktopManager;
70
import javax.swing.ImageIcon;
71
import javax.swing.JDesktopPane;
72
import javax.swing.JDialog;
73
import javax.swing.JFrame;
74
import javax.swing.JInternalFrame;
75
import javax.swing.JLayeredPane;
76
import javax.swing.JPanel;
77
import javax.swing.JPopupMenu;
78
import javax.swing.JScrollPane;
79
import javax.swing.KeyStroke;
80
import javax.swing.event.InternalFrameEvent;
81
import javax.swing.event.InternalFrameListener;
82

    
83
import org.slf4j.Logger;
84
import org.slf4j.LoggerFactory;
85

    
86
import org.gvsig.andami.PluginServices;
87
import org.gvsig.andami.plugins.Extension;
88
import org.gvsig.andami.ui.mdiFrame.GlassPane;
89
import org.gvsig.andami.ui.mdiFrame.MDIFrame;
90
import org.gvsig.andami.ui.mdiFrame.NewStatusBar;
91
import org.gvsig.andami.ui.mdiManager.IWindow;
92
import org.gvsig.andami.ui.mdiManager.IWindowListener;
93
import org.gvsig.andami.ui.mdiManager.MDIManager;
94
import org.gvsig.andami.ui.mdiManager.MDIUtilities;
95
import org.gvsig.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
96
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
97
import org.gvsig.andami.ui.mdiManager.WindowInfo;
98
import org.gvsig.andami.ui.theme.Theme;
99
import org.gvsig.tools.swing.api.ToolsSwingLocator;
100
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
101

    
102

    
103

    
104
/**
105
 *
106
 */
107
public class NewSkin extends Extension implements MDIManager{
108
        private static final int DefaultXMargin = 20; // Added for the method 'centreJInternalFrame'
109
        private static final int DefaultYMargin = 20; // Added for the method 'centreJInternalFrame'
110
        private static final int MinimumXMargin = 130; // Added for the method 'centreJInternalFrame'
111
        private static final int MinimumYMargin = 60; // Added for the method 'centreJInternalFrame'
112

    
113

    
114
    /**
115
     * Variable privada <code>desktopManager</code> para usarlo cuando sale
116
     * una ventana que no queremos que nos restaure las que tenemos maximizadas.
117
     * Justo antes de usar el setMaximize(false), le pegamos el cambiazo.
118
     */
119
    private static DesktopManager desktopManager = new DefaultDesktopManager();
120

    
121
    /** log */
122
    private static Logger logger = LoggerFactory.getLogger(NewSkin.class.getName());
123

    
124
    /** Panel de la MDIFrame */
125
    private MyDesktopPane panel = new MyDesktopPane();
126

    
127
    /** MDIFrame */
128
    private MDIFrame mainFrame;
129

    
130
    private GlassPane glassPane = new GlassPane();
131

    
132
    private DialogStackSupport dss;
133

    
134
    /**
135
     * Associates JInternalFrames with the IWindow they contain
136
     */
137
    private FrameWindowSupport fws;
138

    
139
    private WindowInfoSupport wis;
140

    
141
    private WindowStackSupport wss;
142

    
143
    private SingletonWindowSupport sws;
144

    
145
    private Cursor lastCursor = null;
146
        private ImageIcon image;
147
        private String typeDesktop;
148
        
149
        //Anyade barras de scroll
150
        private void addScrolledDesktopPanel( JFrame parent, MyDesktopPane desktopPane) {
151
                JPanel toppanel;
152

    
153
                toppanel = new JPanel();
154
                toppanel.setLayout(new BorderLayout());
155
                toppanel.setPreferredSize(new Dimension(200,200));
156
                toppanel.setBackground(Color.RED);
157
                
158
                JScrollPane scrollPanel = new JScrollPane(desktopPane);
159
                
160
                toppanel.add(scrollPanel, BorderLayout.CENTER);
161

    
162
                parent.getContentPane().add(toppanel, BorderLayout.CENTER);
163
        }
164
        
165
        public JDesktopPane getDesktopPane() {
166
            return this.panel;
167
        }
168
        
169
    /*
170
     * @see com.iver.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
171
     */
172
    public void init(MDIFrame f) {
173
            
174
        // Inicializa el Frame y la consola
175
            mainFrame = f;
176
            mainFrame.setGlassPane(glassPane);
177
        panel.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
178

    
179
            //Anyade barras de scroll
180
        //mainFrame.getContentPane().add(panel, BorderLayout.CENTER);
181
        addScrolledDesktopPanel(mainFrame,panel);
182

    
183
        panel.setDesktopManager(desktopManager);
184

    
185
        fws = new FrameWindowSupport(mainFrame);
186
        dss = new DialogStackSupport(mainFrame);
187
        sws = new SingletonWindowSupport(wis, fws);
188
        wis = new WindowInfoSupport(mainFrame, fws, sws);
189
        fws.setVis(wis);
190
        wss = new WindowStackSupport(wis);
191

    
192

    
193
        // TODO (jaume) esto no deber�a de estar aqu�...
194
        // molar�a m�s en un di�logo de preferencias
195
        // es s�lo una prueba
196
        KeyStroke controlTab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.CTRL_MASK);
197

    
198
        PluginServices.registerKeyStroke(controlTab, new KeyEventDispatcher() {
199

    
200
                        public boolean dispatchKeyEvent(KeyEvent e) {
201
                                IWindow[] views = getAllWindows();
202
                                if (views.length<=0 || e.getID() == KeyEvent.KEY_PRESSED)
203
                                        return false;
204

    
205

    
206
                                int current = 0;
207
                                for (int i = 0; i < views.length; i++) {
208
                                        if (views[i].equals(getActiveWindow())) {
209
                                                current = i;
210
                                                break;
211
                                        }
212
                                }
213
                                addWindow(views[(current +1) % views.length]);
214
                                return true;
215
                        }
216

    
217
        });
218
    }
219

    
220
    /* (non-javadoc)
221
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
222
     */
223
    public IWindow addWindow(IWindow p) throws SingletonDialogAlreadyShownException {
224
        // se obtiene la informaci�n de la vista
225
        WindowInfo wi = wis.getWindowInfo(p);
226

    
227
        // Se comprueban las incompatibilidades que pudieran haber en la vista
228
        MDIUtilities.checkWindowInfo(wi);
229
        if ((p instanceof SingletonWindow) && (wi.isModal())) {
230
            throw new RuntimeException("A modal view cannot be a SingletonView");
231
        }
232

    
233
        /*
234
         * Se obtiene la referencia a la vista anterior por si es una singleton
235
         * y est� siendo mostrada. Se obtiene su informaci�n si ya fue mostrada
236
         */
237
        boolean singletonPreviouslyAdded = false;
238

    
239
        if (p instanceof SingletonWindow) {
240
            SingletonWindow sw = (SingletonWindow) p;
241
            if (sws.registerWindow(sw.getClass(), sw.getWindowModel(), wi)) {
242
                singletonPreviouslyAdded = true;
243
            }
244
        }
245

    
246
        if (singletonPreviouslyAdded) {
247
            // Si la vista no est� actualmente abierta
248
            if (!sws.contains((SingletonWindow) p)) {
249
                JInternalFrame frame = fws.getJInternalFrame(p);
250
                sws.openSingletonWindow((SingletonWindow) p, frame);
251
                addJInternalFrame(frame, wi);
252
                wss.add(p, new ActionListener() {
253
                    public void actionPerformed(ActionEvent e) {
254
                        IWindow v = wis.getWindowById(Integer.parseInt(e
255
                                .getActionCommand()));
256
                        JInternalFrame f = fws.getJInternalFrame(v);
257
                        activateJInternalFrame(f);
258
                    }
259
                });
260
                return p;
261
            } else {
262
                // La vista est� actualmente abierta
263
                JInternalFrame frame = (JInternalFrame) sws
264
                        .getFrame((SingletonWindow) p);
265
                activateJInternalFrame(frame);
266
                wss.setActive(p);
267
                return fws.getWindow((JInternalFrame) frame);
268
            }
269
        } else {
270
            if (wi.isModal()) {
271
                addJDialog(p);
272
            } else {
273
                // Se sit�a la vista en la pila de vistas
274
                wss.add(p, new ActionListener() {
275
                    public void actionPerformed(ActionEvent e) {
276
                        IWindow v = wis.getWindowById(Integer.parseInt(e
277
                                .getActionCommand()));
278
                        JInternalFrame f = fws.getJInternalFrame(v);
279
                        activateJInternalFrame(f);
280
                    }
281
                });
282
                addJInternalFrame(p);
283
            }
284

    
285
            return p;
286
        }
287
    }
288

    
289
    public SingletonWindow getSingletonWindow(Class windowClass, Object model) {
290
        JInternalFrame frame = (JInternalFrame) sws.getFrame(windowClass,model);
291
        if( frame == null ) {
292
                return null;
293
        }
294
        return (SingletonWindow) fws.getWindow((JInternalFrame) frame);
295
     }
296

    
297
    
298
    
299
    /* (non-javadoc)
300
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
301
     */
302
        public IWindow addCentredWindow(IWindow p) throws SingletonDialogAlreadyShownException {
303
                IWindow window = addWindow(p);
304
        if (!p.getWindowInfo().isModal()) {
305
            centreFrame(window);
306
        }
307
                return window;
308
        }
309

    
310
    public IWindow addWindow(IWindow p, int align) throws SingletonDialogAlreadyShownException {
311
        boolean singletonPreviouslyAdded = false;
312
        WindowInfo wi = wis.getWindowInfo(p);
313

    
314
        if (p instanceof SingletonWindow) {
315
            SingletonWindow sw = (SingletonWindow) p;
316
            if (sws.registerWindow(sw.getClass(), sw.getWindowModel(), wi)) {
317
                singletonPreviouslyAdded = true;
318
            }
319
        }
320
        IWindow window = addWindow(p);
321
        if( !window.getWindowInfo().isModal() ) {
322
                // if the window is modal at this oint is already closed
323
                if( !singletonPreviouslyAdded ) {
324
                    alignFrame(window,align);
325
                }
326
        }
327
        return window;
328
    }
329

    
330

    
331
        /**
332
         * Centres the Frame in the contentPane of the MainFrame.
333
         * If the frame can't be showed completely, it tries to show its top-left
334
         * corner.
335
         *
336
         * @author Pablo Piqueras Bartolom�
337
         *
338
         * @param panel The IWindow to centre
339
         */
340
        public synchronized void centreFrame(IWindow panel) {
341
                Component window = fws.getFrame(panel);
342
                if (window==null) return;
343

    
344
                // The top-left square of frame reference
345
                Point newReferencePoint = new Point();
346

    
347
                // A reference to the panel where the JInternalFrame will be displayed
348
                Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
349

    
350
                // Get the NewStatusBar component
351
                NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
352
                JDesktopPane jDesktopPane = this.getDesktopPane(); 
353

    
354
                int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The last substraction is for if there is any menu,... at left
355
                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
356
                int freeWidth = visibleWidth - window.getWidth();
357
                int freeHeight = visibleHeight - window.getHeight();
358

    
359
                // Calculate the new point reference (Assure that the top-left corner is showed)
360
                if (freeWidth < 0)
361
                {
362
                        if (visibleWidth > MinimumXMargin)
363
                                newReferencePoint.x = DefaultXMargin;
364
                        else
365
                                newReferencePoint.x = 0;
366
                }
367
                else
368
                        newReferencePoint.x = freeWidth / 2;
369

    
370
                if (freeHeight < 0)
371
                {
372
                        if (visibleHeight > MinimumYMargin)
373
                                newReferencePoint.y = DefaultYMargin;
374
                        else
375
                                newReferencePoint.y = 0;
376
                }
377
                else
378
                        newReferencePoint.y = freeHeight / 2;
379

    
380
                // Set the new location for this JInternalFrame
381
                window.setLocation(newReferencePoint);
382
        }
383

    
384
    public synchronized void alignFrame(IWindow panel, int mode) {
385
        Component window = fws.getFrame(panel);
386
        if (window==null) return;
387

    
388
        // The top-left square of frame reference
389
        Point newReferencePoint = new Point();
390

    
391
        // A reference to the panel where the JInternalFrame will be displayed
392
        Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
393

    
394
        // Get the NewStatusBar component
395
        NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
396
        JDesktopPane jDesktopPane = this.getDesktopPane(); 
397

    
398
        int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The last substraction is for if there is any menu,... at left
399
        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
400

    
401
//        -------------------------------------------------
402
//        |FIRST_LINE_START   PAGE_START     FIRST_LINE_END|
403
//        |                                                |
404
//        |                                                |
405
//        |LINE_START           CENTER             LINE_END|
406
//        |                                                |
407
//        |                                                |
408
//        |LAST_LINE_START     PAGE_END       LAST_LINE_END|
409
//        -------------------------------------------------
410
        
411
        switch (mode) {
412
        case GridBagConstraints.FIRST_LINE_START:
413
            newReferencePoint.x = DefaultXMargin;
414
            newReferencePoint.y = DefaultYMargin;
415
            break;
416
        
417
        case GridBagConstraints.PAGE_START:
418
            newReferencePoint.x = visibleWidth / 2;
419
            newReferencePoint.y = DefaultYMargin;
420
            break;
421
        
422
        case GridBagConstraints.FIRST_LINE_END:
423
            newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
424
            newReferencePoint.y = DefaultYMargin;
425
            break;
426
        
427
        case GridBagConstraints.LINE_START:
428
            newReferencePoint.x = DefaultXMargin;
429
            newReferencePoint.y = visibleHeight / 2;
430
            break;
431
        
432
        case GridBagConstraints.LINE_END:
433
            newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
434
            newReferencePoint.y = visibleHeight / 2;
435
            break;
436
        
437
        case GridBagConstraints.LAST_LINE_START:
438
            newReferencePoint.x = DefaultXMargin;
439
            newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
440
            break;
441
        
442
        case GridBagConstraints.PAGE_END:
443
            newReferencePoint.x = visibleWidth / 2;
444
            newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
445
            break;
446
        
447
        case GridBagConstraints.LAST_LINE_END:
448
            newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
449
            newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
450
            break;
451
        
452
        default:
453
        case GridBagConstraints.CENTER:
454
            newReferencePoint.x = visibleWidth / 2;
455
            newReferencePoint.y = visibleHeight / 2;
456
            break;
457
        }
458
        
459
        if( newReferencePoint.x < 0 ) {
460
            newReferencePoint.x = DefaultXMargin;
461
        }
462
        if( newReferencePoint.y < 0 ) {
463
            newReferencePoint.y = DefaultYMargin;
464
        }
465
        window.setLocation(newReferencePoint);
466
    }
467

    
468
    /**
469
     * DOCUMENT ME!
470
     *
471
     * @param wnd
472
     *            DOCUMENT ME!
473
     * @param wi
474
     *            DOCUMENT ME!
475
     */
476
    private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
477
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
478
        wnd.addInternalFrameListener(new FrameListener());
479

    
480
        if (wi.isModeless() || wi.isPalette()) {
481
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
482
            if (wi.isPalette())
483
                wnd.setFocusable(false);
484
        } else {
485
            panel.add(wnd);
486
        }
487
        updateFrameProperties(wnd, wi);
488
        activateJInternalFrame(wnd);
489
        try{
490
                wnd.setMaximum(wi.isMaximized());
491
        }catch(Exception ex){
492
                logger.warn("Error: ", ex);
493
        }
494
    }
495

    
496
    private void updateFrameProperties(JInternalFrame frame, WindowInfo wi) {
497
            int height, width;
498
            if (wi.isMaximized()) {
499
                    if (wi.getNormalWidth()!=-1)
500
                            width = wi.getNormalWidth();
501
                    else
502
                            width = frame.getNormalBounds().width;
503
                    if (wi.getNormalHeight()!=-1)
504
                            height = wi.getNormalHeight();
505
                    else
506
                            height = frame.getNormalBounds().height;
507

    
508
                    frame.setSize(width, height);
509
                    frame.setLocation(wi.getNormalX(), wi.getNormalY());
510
            }
511
            else {
512
                    if (wi.getWidth()!=-1)
513
                            width = wi.getWidth();
514
                    else
515
                            width = frame.getWidth();
516
                    if (wi.getHeight()!=-1)
517
                            height = wi.getHeight();
518
                    else
519
                            height = frame.getHeight();
520
                    frame.setSize(width, height);
521
                frame.setLocation(wi.getX(), wi.getY());
522
            }
523
            frame.setTitle(wi.getTitle());
524
            frame.setVisible(wi.isVisible());
525
            frame.setResizable(wi.isResizable());
526
            frame.setIconifiable(wi.isIconifiable());
527
            frame.setMaximizable(wi.isMaximizable());
528
            try {
529
                        frame.setMaximum(wi.isMaximized());
530
                } catch (PropertyVetoException e) {
531
                        // TODO Auto-generated catch block
532
                        //e.printStackTrace();
533
                }
534
    }
535

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

    
544
        JInternalFrame wnd = fws.getJInternalFrame(p);
545

    
546
        if (p instanceof SingletonWindow) {
547
            SingletonWindow sv = (SingletonWindow) p;
548
            sws.openSingletonWindow(sv, wnd);
549
        }
550

    
551
        addJInternalFrame(wnd, wi);
552
    }
553

    
554
    /**
555
     * DOCUMENT ME!
556
     *
557
     * @param wnd
558
     */
559
    private void activateJInternalFrame(JInternalFrame wnd) {
560
            JPopupMenu.setDefaultLightWeightPopupEnabled(false);
561
        try {
562
            wnd.moveToFront();
563
            logger.debug("Activando " + wnd.getTitle());
564
            wnd.setSelected(true);
565
            wnd.setIcon(false);
566
        } catch (PropertyVetoException e) {
567
            logger.error(e.toString());
568
        }
569
    }
570

    
571
    /**
572
     * Situa un di�logo modal en el centro de la pantalla
573
     *
574
     * @param d
575
     *            Di�logo que se quiere situar
576
     */
577
    private void centerDialog(JDialog d) {
578
        int offSetX = d.getWidth() / 2;
579
        int offSetY = d.getHeight() / 2;
580

    
581
        d.setLocation((mainFrame.getWidth() / 2) - offSetX, (mainFrame
582
                .getHeight() / 2)
583
                - offSetY);
584
    }
585

    
586
    /**
587
     * DOCUMENT ME!
588
     *
589
     * @param p
590
     */
591
    private void addJDialog(IWindow p) {
592
        JDialog dlg = fws.getJDialog(p);
593

    
594
        centerDialog(dlg);
595

    
596
        dlg.addWindowListener(new DialogWindowListener());
597
        dss.pushDialog(dlg);
598

    
599
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
600
    }
601

    
602
    /*
603
     * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveWindow()
604
     */
605
    public IWindow getActiveWindow() {
606
        JInternalFrame jif = panel.getSelectedFrame();
607

    
608
        if (jif != null) {
609
            IWindow theWindow = fws.getWindow(jif);
610
            if (theWindow == null)
611
                return null;
612
            if (theWindow.getWindowInfo().isPalette())
613
                return wss.getActiveWindow();
614
            else
615
                return fws.getWindow(jif);
616
        }
617
        // return vss.getActiveView();
618

    
619
        return null;
620
    }
621
    public IWindow getFocusWindow(){
622
             JInternalFrame jif = panel.getSelectedFrame();
623

    
624
         if (jif != null) {
625
             IWindow theView = fws.getWindow(jif);
626
             if (theView == null)
627
                 return null;
628
             return fws.getWindow(jif);
629
         }
630
         return null;
631
    }
632
    /*
633
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeWindow(com.iver.andami.ui.mdiManager.IWindow)
634
     */
635
    public void closeWindow(IWindow p) {
636
        // Si es un di�logo modal
637
        if (p.getWindowInfo().isModal()) {
638
            closeJDialog();
639
        } else { // Si no es modal se cierra el JInternalFrame
640
            closeJInternalFrame(fws.getJInternalFrame(p));
641
        }
642
    }
643

    
644
    /*
645
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllWindows()
646
     */
647
    public void closeAllWindows() {
648
        ArrayList eliminar = new ArrayList();
649
        Iterator i = fws.getWindowIterator();
650

    
651
        while (i.hasNext()) {
652
            eliminar.add((IWindow) i.next());
653
        }
654

    
655
        for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
656
            IWindow vista = (IWindow) iter.next();
657
            closeWindow(vista);
658
        }
659
    }
660

    
661
    /*
662
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
663
     */
664
    public WindowInfo getWindowInfo(IWindow w) {
665
            WindowInfo wi = wis.getWindowInfo(w);
666

    
667
    /*
668
     * This is done now in vis.getWindowInfo(w)
669
     *
670
     * JInternalFrame f = fws.getJInternalFrame(w);
671
            wi.setX(f.getX());
672
            wi.setY(f.getY());
673
            wi.setHeight(f.getHeight());
674
            wi.setWidth(f.getWidth());
675
            // isClosed() doesn't work as (I) expected, why? Using isShowing instead
676
            wi.setClosed(!f.isShowing());
677
            wi.setNormalBounds(f.getNormalBounds());
678
            wi.setMaximized(f.isMaximum());*/
679
            return wi;
680
    }
681

    
682
    /**
683
     * DOCUMENT ME!
684
     *
685
     * @param dialog
686
     * @throws RuntimeException
687
     *             DOCUMENT ME!
688
     */
689
    private void closeJDialog() {
690
        JDialog dlg = dss.popDialog();
691
        if (dlg==null)
692
                return;
693
        dlg.setVisible(false);
694

    
695
        IWindow s = fws.getWindow(dlg);
696

    
697
        callWindowClosed(s);
698

    
699
        fws.closeWindow(s);
700

    
701
        // Si es singleton se desasocia el modelo con la vista
702
        if (s instanceof SingletonWindow) {
703
            sws.closeWindow((SingletonWindow) s);
704
        }
705
    }
706

    
707
    /**
708
     * If <code>window</code> implements IWindowListener, sent it
709
     * the windowActivated event.
710
     *
711
     * @param window
712
     *            The IWindow which has to be notified.
713
     */
714
    private void callWindowClosed(IWindow window) {
715
        if (window instanceof IWindowListener) {
716
            ((IWindowListener) window).windowClosed();
717
        }
718
    }
719

    
720
    /**
721
     * If <code>window</code> implements IWindowListener, sent it
722
     * the windowActivated event.
723
     *
724
     * @param window
725
     *            The IWindow which has to be notified.
726
     */
727
    private void callWindowActivated(IWindow window) {
728
//        logger.debug("View '" + window.getWindowInfo().getTitle()
729
//               + "' activated (callViewActivated)");
730
        if (window instanceof IWindowListener) {
731
            ((IWindowListener) window).windowActivated();
732
        }
733
    }
734

    
735
    /**
736
     * DOCUMENT ME!
737
     *
738
     * @param frame
739
     */
740
    private void closeJInternalFrame(JInternalFrame frame) {
741
        try {
742
            IWindow s = (IWindow) fws.getWindow(frame);
743

    
744
            frame.setClosed(true);
745
            callWindowClosed(s);
746
        } catch (PropertyVetoException e) {
747
            logger
748
                    .error(
749
                            "Not compatible with property veto's. Use ViewInfo instead.",
750
                            e);
751
        }
752
    }
753

    
754
    /*
755
     * @see com.iver.andami.plugins.IExtension#initialize()
756
     */
757
    public void initialize() {
758
    }
759

    
760
    /*
761
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
762
     */
763
    public void execute(String actionCommand) {
764
        if (actionCommand.equals("cascada")) {
765
        } else if (actionCommand.equals("mosaico")) {
766
        }
767
    }
768

    
769
    /*
770
     * @see com.iver.andami.plugins.IExtension#isEnabled()
771
     */
772
    public boolean isEnabled() {
773
        // TODO Auto-generated method stub
774
        return false;
775
    }
776

    
777
    /*
778
     * @see com.iver.andami.plugins.IExtension#isVisible()
779
     */
780
    public boolean isVisible() {
781
        // TODO Auto-generated method stub
782
        return true;
783
    }
784

    
785
    /*
786
     * @see com.iver.andami.ui.mdiManager.MDIManager#setWaitCursor()
787
     */
788
    public void setWaitCursor() {
789
        if (mainFrame != null) {
790
            glassPane.setVisible(true);
791
            lastCursor = mainFrame.getCursor();
792
            dss.setWaitCursor();
793
            glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
794
        }
795
    }
796

    
797
    /*
798
     * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
799
     */
800
    public void restoreCursor() {
801
        if (mainFrame != null) {
802
            glassPane.setVisible(false);
803
            dss.restoreCursor();
804
            glassPane.setCursor(lastCursor);
805
        }
806
    }
807

    
808
    /**
809
     * Listener para los eventos de cerrado de los di�logos. Tiene su raz�n de
810
     * ser en que los di�logos han de devolverse al pool cuando se cierran
811
     *
812
     * @author Fernando Gonz�lez Cort�s
813
     */
814
    public class DialogWindowListener extends WindowAdapter {
815
        /**
816
         * Captura el evento de cerrado de los di�logos con el fin de realizar
817
         * tareas de mantenimiento
818
         *
819
         * @param e
820
         *            evento
821
         */
822
        public void windowActivated(WindowEvent e) {
823
                IWindow window = fws.getWindow((Component) e.getSource());
824
                callWindowActivated(window);
825

    
826
        }
827

    
828

    
829
        /**
830
         * Captura el evento de cerrado de los di�logos con el fin de realizar
831
         * tareas de mantenimiento
832
         *
833
         * @param e
834
         *            evento
835
         */
836
        public void windowClosing(WindowEvent e) {
837
            closeJDialog();
838
        }
839
    }
840

    
841
    /**
842
     * DOCUMENT ME!
843
     */
844
    public class FrameListener implements InternalFrameListener {
845
        /*
846
         * @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
847
         */
848
        public void internalFrameActivated(InternalFrameEvent e) {
849
            // logger.debug("internalFrameActivated " +
850
            // e.getInternalFrame().getTitle());
851

    
852
            // activatedInternalFramesStack.push(e.getInternalFrame());
853

    
854
            IWindow panel = fws.getWindow((JInternalFrame) e.getSource());
855

    
856
            WindowInfo wi = wis.getWindowInfo(panel);
857
            if (wi.isPalette())
858
                return;
859

    
860
            wss.setActive(panel);
861

    
862
            JInternalFrame frame = fws.getJInternalFrame(panel);
863

    
864
            if (wi.isMaximizable()){
865
                    frame.setMaximizable(true);
866
            }
867
            if (!frame.isMaximizable() && frame.isMaximum()) {
868
                try {
869
                    frame.setMaximum(false);
870
                } catch (PropertyVetoException e1) {
871
                }
872
            }
873
            mainFrame.enableControls();
874
            if (wi.getSelectedTools()==null) {
875
                    // this is the first time this window is activated
876
                    wi.setSelectedTools(new HashMap(mainFrame.getInitialSelectedTools()));
877
            }
878
            mainFrame.setSelectedTools(wi.getSelectedTools());
879
            callWindowActivated(panel);
880

    
881
        }
882

    
883
        /*
884
         * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
885
         */
886
        public void internalFrameClosed(InternalFrameEvent e) {
887
        }
888

    
889
        /*
890
         * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
891
         */
892
        public void internalFrameClosing(InternalFrameEvent e) {
893
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
894
            // logger.debug("internalFrameClosing " +
895
            // e.getInternalFrame().getTitle());
896

    
897
            JInternalFrame c = (JInternalFrame) e.getSource();
898
            WindowInfo wi = wis.getWindowInfo((IWindow) fws.getWindow(c));
899

    
900
            IWindow win = fws.getWindow(c);
901
            callWindowClosed(win);
902
            boolean alwaysLive;
903
            if (win instanceof SingletonWindow) {
904
                sws.closeWindow((SingletonWindow) win);
905
            }
906

    
907
            fws.closeWindow(win);
908

    
909
            panel.remove(c);
910

    
911
            wss.remove(win);
912

    
913
            if (!wi.isPalette())
914
                mainFrame.enableControls();
915
            panel.repaint();
916

    
917
            // Para activar el JInternalFrame desde la que hemos
918
            // abierto la ventana que estamos cerrando
919
            IWindow lastWindow = wss.getActiveWindow();
920
            // La activamos
921
            if (lastWindow != null) {
922
                    logger.debug(PluginServices.getText(this, "Devuelvo_el_foco_a_")+lastWindow.getWindowInfo().getTitle());
923
                JInternalFrame frame = fws.getJInternalFrame(lastWindow);
924
                try {
925
                    frame.setSelected(true);
926
                } catch (PropertyVetoException e1) {
927
                    // TODO Auto-generated catch block
928
                    // e1.printStackTrace();
929
                }
930
                // addView(lastView);
931
            }
932

    
933
        }
934

    
935
        /*
936
         * @see javax.swing.event.InternalFrameListener#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)
937
         */
938
        public void internalFrameDeactivated(InternalFrameEvent e) {
939
            // logger.debug("internalDeActivated " +
940
            // e.getInternalFrame().getTitle());
941
            JInternalFrame c = (JInternalFrame) e.getSource();
942
            IWindow win = fws.getWindow(c);
943
            if (win != null) {
944
                WindowInfo wi = wis.getWindowInfo(win);
945
                if (wi.isPalette())
946
                    return;
947

    
948
            }
949

    
950
        }
951

    
952
        /*
953
         * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
954
         */
955
        public void internalFrameDeiconified(InternalFrameEvent e) {
956
            mainFrame.enableControls();
957
        }
958

    
959
        /*
960
         * @see javax.swing.event.InternalFrameListener#internalFrameIconified(javax.swing.event.InternalFrameEvent)
961
         */
962
        public void internalFrameIconified(InternalFrameEvent e) {
963
            mainFrame.enableControls();
964
        }
965

    
966
        /*
967
         * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
968
         */
969
        public void internalFrameOpened(InternalFrameEvent e) {
970
            // logger.debug("internalFrameOpened. Source= " +
971
            // e.getSource().toString());
972
        }
973
    }
974

    
975
    /*
976
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
977
     *      java.lang.Object)
978
     */
979
    public boolean closeSingletonWindow(Class viewClass, Object model) {
980
        JInternalFrame frame = (JInternalFrame) sws.getFrame(viewClass, model);
981
        if (frame == null)
982
            return false;
983
        closeJInternalFrame(frame);
984
        return true;
985
    }
986

    
987
    /*
988
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
989
     */
990
    public boolean closeSingletonWindow(Object model) {
991
        JInternalFrame[] frames = (JInternalFrame[]) sws.getFrames(model);
992
        if (frames.length == 0)
993
            return false;
994
        for (int i = 0; i < frames.length; i++) {
995
            closeJInternalFrame(frames[i]);
996
        }
997
        return true;
998
    }
999

    
1000
    /*
1001
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
1002
     */
1003
    public IWindow[] getAllWindows() {
1004
        ArrayList windows = new ArrayList();
1005
        Iterator i = fws.getWindowIterator();
1006

    
1007
        while (i.hasNext()) {
1008
            windows.add((IWindow) i.next());
1009
        }
1010
        return (IWindow[]) windows.toArray(new IWindow[0]);
1011
    }
1012

    
1013
    /*
1014
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
1015
     */
1016
    public IWindow[] getOrderedWindows() {
1017
        TreeMap windows = new TreeMap();
1018
        Iterator winIterator = fws.getWindowIterator();
1019

    
1020
        Component frame;
1021
        IWindow win;
1022
        /**
1023
         * The order of the window in the JDesktopPane. Smaller numbers
1024
         * are closer to the foreground.
1025
         */
1026
        int zPosition;
1027
        while (winIterator.hasNext()) {
1028
                win = (IWindow) winIterator.next();
1029
                frame = fws.getFrame(win);
1030
                    zPosition = panel.getPosition(frame);
1031
                    int layer = panel.getLayer(frame);
1032

    
1033
                    if (! (frame instanceof JDialog)) { //JDialogs are not in inside the LayeredPane
1034
                            // flatten all the layers
1035
                            if (layer == JLayeredPane.DEFAULT_LAYER.intValue()) {
1036
                                    zPosition+=50000;
1037
                            }
1038
                            else if (layer == JLayeredPane.PALETTE_LAYER.intValue()) {
1039
                                    zPosition+=40000;
1040
                            }
1041
                            else if (layer == JLayeredPane.MODAL_LAYER.intValue()) {
1042
                                    zPosition+=30000;
1043
                            }
1044
                            else if  (layer == JLayeredPane.POPUP_LAYER.intValue()) {
1045
                                    zPosition+=20000;
1046
                            }
1047
                            else if  (layer == JLayeredPane.DRAG_LAYER.intValue()) {
1048
                                    zPosition+=10000;
1049
                            }
1050
                    }
1051
                    windows.put(new Integer(zPosition), win);
1052
        }
1053
        winIterator = windows.values().iterator();
1054
        ArrayList winList = new ArrayList();
1055
        while (winIterator.hasNext()) {
1056
                winList.add(winIterator.next());
1057
        }
1058

    
1059
        return (IWindow[]) winList.toArray(new IWindow[0]);
1060
    }
1061
    public void setMaximum(IWindow v, boolean bMaximum) throws PropertyVetoException
1062
    {
1063
        JInternalFrame f = fws.getJInternalFrame(v);
1064
        f.setMaximum(bMaximum);
1065
    }
1066

    
1067
    public void changeWindowInfo(IWindow w, WindowInfo wi){
1068
            JInternalFrame f = fws.getJInternalFrame(w);
1069
            f.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
1070
            JDesktopPane pnl = f.getDesktopPane();
1071
            pnl.remove(f);
1072
            int width;
1073
            int height;
1074
            if (wi.getWidth()!=-1)
1075
                    width = wi.getWidth();
1076
            else
1077
                    width = f.getWidth();
1078
            if (wi.getHeight()!=-1)
1079
                    height = wi.getHeight();
1080
            else
1081
                    height = f.getHeight();
1082
            f.setSize(new Dimension(width, height));
1083
            f.setLocation(wi.getX(), wi.getY());
1084
            if (wi.isPalette()) {
1085
                    pnl.add(f, JDesktopPane.PALETTE_LAYER);
1086
                    f.setFocusable(false);
1087
            } else {
1088
                    pnl.add(f, JDesktopPane.DEFAULT_LAYER);
1089
                    f.setFocusable(true);
1090
                    if (wi.isClosed()) {
1091
                            closeWindow(w);
1092
                    }
1093
            }
1094

    
1095
            if (wi.isMaximized()) {
1096
                    try {
1097
                            f.setMaximum(true);
1098
                    } catch (PropertyVetoException e) {
1099
                            // TODO Auto-generated catch block
1100
                            //e.printStackTrace();
1101
                    }
1102
                    f.setNormalBounds(wi.getNormalBounds());
1103
            }
1104
            activateJInternalFrame(f);
1105
    }
1106

    
1107
    public void refresh(IWindow win) {
1108
            Component frame = fws.getFrame(win);
1109
            if (frame!=null)
1110
                    frame.setVisible(true);
1111
    }
1112

    
1113
        public void setBackgroundImage(ImageIcon image, String typeDesktop) {
1114
                this.image=image;
1115
                this.typeDesktop=typeDesktop;
1116

    
1117
        }
1118
        class MyDesktopPane extends JDesktopPane
1119
            {
1120
                  protected Map listeners = new Hashtable();
1121
                
1122
                
1123
                  public MyDesktopPane(){
1124
              }
1125
                  
1126
                  public Dimension getPreferredSize() {
1127
                                JInternalFrame[] array = this.getAllFrames();
1128
                                int maxX = 0;
1129
                                int maxY = 0;
1130
                                for (int i = 0; i < array.length; i++) {
1131
                                        int x = array[i].getX() + array[i].getWidth();
1132
                                        if (x > maxX)
1133
                                                maxX = x;
1134
                                        int y = array[i].getY() + array[i].getHeight();
1135
                                        if (y > maxY)
1136
                                                maxY = y;
1137
                                }
1138
                                return new Dimension(maxX, maxY);
1139
                        }
1140

    
1141
                        /**
1142
                         * Add an internal-frame to the desktop. Sets a component-listener on
1143
                         * it, which resizes the desktop if a frame is resized.
1144
                         */
1145
                  public Component add(Component comp) {
1146
                                super.add(comp);
1147
                                ComponentListener listener = new ComponentListener() {
1148
                                        public void componentResized(ComponentEvent e) { // Layout the
1149
                                                // JScrollPane
1150
                                                getParent().getParent().validate();
1151
                                        }
1152

    
1153
                                        public void componentMoved(ComponentEvent e) {
1154
                                                JInternalFrame comp = (JInternalFrame) e.getSource();
1155
                                                
1156
                                                //no deja que la ventana se vaya hacia la izquierda o arriba
1157
                                                if( comp.getX()<0 ) {
1158
                                                        comp.setLocation(0, comp.getY());
1159
                                                }
1160
                                                if (comp.getY()<0){
1161
                                                        comp.setLocation(comp.getX(), 0);
1162
                                                }
1163
                                                componentResized(e);
1164
                                        }
1165
                                        public void componentShown(ComponentEvent e) {
1166
                                        }
1167

    
1168
                                        public void componentHidden(ComponentEvent e) {
1169
                                        }
1170

    
1171
                                };
1172
                                comp.addComponentListener(listener);
1173
                                listeners.put(comp, listener);
1174
                                return comp;
1175
                        }
1176

    
1177
                        /**
1178
                         * Remove an internal-frame from the desktop. Removes the
1179
                         * component-listener and resizes the desktop.
1180
                         */
1181
                        public void remove(Component comp) {
1182
                                comp.removeComponentListener((ComponentListener) listeners
1183
                                                .get(comp));
1184
                                super.remove(comp);
1185
                                getParent().getParent().validate(); // Layout the JScrollPane
1186
                        }
1187

    
1188
                 public void paintComponent(Graphics g){
1189
                super.paintComponent(g);
1190
                int x=0;
1191
                int y=0;
1192
                int w=0;
1193
                int h=0;
1194
                if (image != null) {
1195
                                if (typeDesktop.equals(Theme.CENTERED)) {
1196
                                        w = image.getIconWidth();
1197
                                        h = image.getIconHeight();
1198
                                        x = (getWidth() - w) / 2;
1199
                                        y = (getHeight() - h) / 2;
1200
                                        g.drawImage(image.getImage(), x, y, w, h, this);
1201
                                } else if (typeDesktop.equals(Theme.EXPAND)) {
1202
                                        w = getWidth();
1203
                                        h = getHeight();
1204
                                        g.drawImage(image.getImage(), x, y, w, h, this);
1205
                                } else if (typeDesktop.equals(Theme.MOSAIC)) {
1206
                                        int wAux = image.getIconWidth();
1207
                                        int hAux = image.getIconHeight();
1208
                                        int i = 0;
1209
                                        int j = 0;
1210
                                        w = wAux;
1211
                                        h = hAux;
1212
                                        while (x < getWidth()) {
1213
                                                x = wAux * i;
1214
                                                while (y < getHeight()) {
1215
                                                        y = hAux * j;
1216
                                                        j++;
1217
                                                        g.drawImage(image.getImage(), x, y, w, h, this);
1218
                                                }
1219
                                                y = 0;
1220
                                                j = 0;
1221
                                                i++;
1222
                                        }
1223
                                }
1224
                        }
1225
              }
1226
            }
1227

    
1228
    public void showWindow(JPanel panel, String title, MODE mode) {
1229
        // This prepare a default JPanel that implements IWindow 
1230
        // for the passed panel and call to the addWindow
1231
        // to show it.
1232
        ToolsSwingLocator.getWindowManager().showWindow(panel, title, mode);
1233
    }
1234
}