Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libCorePlugin / src / org / gvsig / coreplugin / mdiManager / NewSkin.java @ 38608

History | View | Annotate | Download (39.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.List;
66
import java.util.Map;
67
import java.util.TreeMap;
68

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

    
84
import org.gvsig.andami.PluginServices;
85
import org.gvsig.andami.plugins.Extension;
86
import org.gvsig.andami.plugins.config.generate.ActionTool;
87
import org.gvsig.andami.plugins.config.generate.Menu;
88
import org.gvsig.andami.plugins.config.generate.SelectableTool;
89
import org.gvsig.andami.ui.mdiFrame.GlassPane;
90
import org.gvsig.andami.ui.mdiFrame.MDIFrame;
91
import org.gvsig.andami.ui.mdiFrame.NewStatusBar;
92
import org.gvsig.andami.ui.mdiManager.IWindow;
93
import org.gvsig.andami.ui.mdiManager.IWindowListener;
94
import org.gvsig.andami.ui.mdiManager.MDIManager;
95
import org.gvsig.andami.ui.mdiManager.MDIUtilities;
96
import org.gvsig.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
97
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
98
import org.gvsig.andami.ui.mdiManager.WindowInfo;
99
import org.gvsig.andami.ui.theme.Theme;
100
import org.gvsig.tools.swing.api.ToolsSwingLocator;
101
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
102
import org.slf4j.Logger;
103
import org.slf4j.LoggerFactory;
104

    
105

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

    
115

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

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

    
126
    /** Panel de la MDIFrame */
127
    private MyDesktopPane panel = new MyDesktopPane();
128

    
129
    /** MDIFrame */
130
    private MDIFrame mainFrame;
131

    
132
    private GlassPane glassPane = new GlassPane();
133

    
134
    private DialogStackSupport dss;
135

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

    
141
    private WindowInfoSupport wis;
142

    
143
    private WindowStackSupport wss;
144

    
145
    private SingletonWindowSupport sws;
146

    
147
    private Cursor lastCursor = null;
148
        private ImageIcon image;
149
        private String typeDesktop;
150

    
151
        private int alignCounter = 1;
152
        
153
        //Anyade barras de scroll
154
        private void addScrolledDesktopPanel( JFrame parent, MyDesktopPane desktopPane) {
155
                JPanel toppanel;
156

    
157
                toppanel = new JPanel();
158
                toppanel.setLayout(new BorderLayout());
159
                toppanel.setPreferredSize(new Dimension(200,200));
160
                toppanel.setBackground(Color.RED);
161
                
162
                JScrollPane scrollPanel = new JScrollPane(desktopPane);
163
                
164
                toppanel.add(scrollPanel, BorderLayout.CENTER);
165

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

    
183
            //Anyade barras de scroll
184
        //mainFrame.getContentPane().add(panel, BorderLayout.CENTER);
185
        addScrolledDesktopPanel(mainFrame,panel);
186

    
187
        panel.setDesktopManager(desktopManager);
188

    
189
        fws = createFrameWindowSupport(mainFrame);
190
        dss = new DialogStackSupport(mainFrame);
191
        sws = new SingletonWindowSupport(wis, fws);
192
        wis = new WindowInfoSupport(mainFrame, fws, sws);
193
        fws.setVis(wis);
194
        wss = new WindowStackSupport(wis);
195

    
196

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

    
202
        PluginServices.registerKeyStroke(controlTab, new KeyEventDispatcher() {
203

    
204
                        public boolean dispatchKeyEvent(KeyEvent e) {
205
                                IWindow[] views = getAllWindows();
206
                                if (views.length<=0 || e.getID() == KeyEvent.KEY_PRESSED)
207
                                        return false;
208

    
209

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

    
221
        });
222
    }
223

    
224
    /**
225
     * Create the {@link FrameWindowSupport} instance
226
     * 
227
     * @param mainFrame
228
     * 
229
     */
230
    protected FrameWindowSupport createFrameWindowSupport(MDIFrame mainFrame) {
231
        return new FrameWindowSupport(mainFrame);
232
    }
233

    
234
    /* (non-javadoc)
235
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
236
     */
237
    public IWindow addWindow(IWindow p) throws SingletonDialogAlreadyShownException {
238
        // se obtiene la informaci�n de la vista
239
        WindowInfo wi = wis.getWindowInfo(p);
240

    
241
        // Se comprueban las incompatibilidades que pudieran haber en la vista
242
        MDIUtilities.checkWindowInfo(wi);
243
        if ((p instanceof SingletonWindow) && (wi.isModal())) {
244
            throw new RuntimeException("A modal view cannot be a SingletonView");
245
        }
246

    
247
        /*
248
         * Se obtiene la referencia a la vista anterior por si es una singleton
249
         * y est� siendo mostrada. Se obtiene su informaci�n si ya fue mostrada
250
         */
251
        boolean singletonPreviouslyAdded = false;
252

    
253
        if (p instanceof SingletonWindow) {
254
            SingletonWindow sw = (SingletonWindow) p;
255
            if (sws.registerWindow(sw.getClass(), sw.getWindowModel(), wi)) {
256
                singletonPreviouslyAdded = true;
257
            }
258
        }
259

    
260
        if (singletonPreviouslyAdded) {
261
            // Si la vista no est� actualmente abierta
262
            if (!sws.contains((SingletonWindow) p)) {
263
                JInternalFrame frame = fws.getJInternalFrame(p);
264
                sws.openSingletonWindow((SingletonWindow) p, frame);
265
                addJInternalFrame(frame, wi);
266
                wss.add(p, new ActionListener() {
267
                    public void actionPerformed(ActionEvent e) {
268
                        IWindow v = wis.getWindowById(Integer.parseInt(e
269
                                .getActionCommand()));
270
                        JInternalFrame f = fws.getJInternalFrame(v);
271
                        activateJInternalFrame(f);
272
                    }
273
                });
274
                return p;
275
            } else {
276
                // La vista est� actualmente abierta
277
                JInternalFrame frame = (JInternalFrame) sws
278
                        .getFrame((SingletonWindow) p);
279
                activateJInternalFrame(frame);
280
                wss.setActive(p);
281
                return fws.getWindow((JInternalFrame) frame);
282
            }
283
        } else {
284
            if (wi.isModal()) {
285
                addJDialog(p);
286
            } else {
287
                // Se sit�a la vista en la pila de vistas
288
                wss.add(p, new ActionListener() {
289
                    public void actionPerformed(ActionEvent e) {
290
                        IWindow v = wis.getWindowById(Integer.parseInt(e
291
                                .getActionCommand()));
292
                        JInternalFrame f = fws.getJInternalFrame(v);
293
                        activateJInternalFrame(f);
294
                    }
295
                });
296
                addJInternalFrame(p);
297
            }
298

    
299
            return p;
300
        }
301
    }
302

    
303
    public SingletonWindow getSingletonWindow(Class windowClass, Object model) {
304
        JInternalFrame frame = (JInternalFrame) sws.getFrame(windowClass,model);
305
        if( frame == null ) {
306
                return null;
307
        }
308
        return (SingletonWindow) fws.getWindow((JInternalFrame) frame);
309
     }
310

    
311
    
312
    
313
    /* (non-javadoc)
314
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
315
     */
316
        public IWindow addCentredWindow(IWindow p) throws SingletonDialogAlreadyShownException {
317
                IWindow window = addWindow(p);
318
        if (!p.getWindowInfo().isModal()) {
319
            centreFrame(window);
320
        }
321
                return window;
322
        }
323

    
324
    public IWindow addWindow(IWindow p, int align) throws SingletonDialogAlreadyShownException {
325
        boolean singletonPreviouslyAdded = false;
326
        WindowInfo wi = wis.getWindowInfo(p);
327

    
328
        if (p instanceof SingletonWindow) {
329
            SingletonWindow sw = (SingletonWindow) p;
330
            if (sws.registerWindow(sw.getClass(), sw.getWindowModel(), wi)) {
331
                singletonPreviouslyAdded = true;
332
            }
333
        }
334
        IWindow window = addWindow(p);
335
        if( !window.getWindowInfo().isModal() ) {
336
                // if the window is modal at this oint is already closed
337
                if( !singletonPreviouslyAdded ) {
338
                    alignFrame(window,align);
339
                }
340
        }
341
        return window;
342
    }
343

    
344

    
345
        /**
346
         * Centres the Frame in the contentPane of the MainFrame.
347
         * If the frame can't be showed completely, it tries to show its top-left
348
         * corner.
349
         *
350
         * @author Pablo Piqueras Bartolom�
351
         *
352
         * @param panel The IWindow to centre
353
         */
354
        public synchronized void centreFrame(IWindow panel) {
355
                Component window = fws.getFrame(panel);
356
                if (window==null) return;
357

    
358
                // The top-left square of frame reference
359
                Point newReferencePoint = new Point();
360

    
361
                // A reference to the panel where the JInternalFrame will be displayed
362
                Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
363

    
364
                // Get the NewStatusBar component
365
                NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
366
                JDesktopPane jDesktopPane = this.getDesktopPane(); 
367

    
368
                int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The last substraction is for if there is any menu,... at left
369
                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
370
                int freeWidth = visibleWidth - window.getWidth();
371
                int freeHeight = visibleHeight - window.getHeight();
372

    
373
                // Calculate the new point reference (Assure that the top-left corner is showed)
374
                if (freeWidth < 0)
375
                {
376
                        if (visibleWidth > MinimumXMargin)
377
                                newReferencePoint.x = DefaultXMargin;
378
                        else
379
                                newReferencePoint.x = 0;
380
                }
381
                else
382
                        newReferencePoint.x = freeWidth / 2;
383

    
384
                if (freeHeight < 0)
385
                {
386
                        if (visibleHeight > MinimumYMargin)
387
                                newReferencePoint.y = DefaultYMargin;
388
                        else
389
                                newReferencePoint.y = 0;
390
                }
391
                else
392
                        newReferencePoint.y = freeHeight / 2;
393

    
394
                // Set the new location for this JInternalFrame
395
                window.setLocation(newReferencePoint);
396
        }
397

    
398
    public synchronized void alignFrame(IWindow panel, int mode) {
399
        Component window = fws.getFrame(panel);
400
        if (window==null) return;
401

    
402
        // The top-left square of frame reference
403
        Point newReferencePoint = new Point();
404

    
405
        // A reference to the panel where the JInternalFrame will be displayed
406
        Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
407

    
408
        // Get the NewStatusBar component
409
        NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
410
        JDesktopPane jDesktopPane = this.getDesktopPane(); 
411

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

    
415
//        ---------------------------------------------------------------
416
//        |FIRST_LINE_START(23)   PAGE_START(19)     FIRST_LINE_END(24) |
417
//        |                                                             |
418
//        |                                                             |
419
//        |LINE_START(21)           CENTER(10)              LINE_END(22)|
420
//        |                                                             |
421
//        |                                                             |
422
//        |LAST_LINE_START(25)     PAGE_END(20)       LAST_LINE_END(26) |
423
//        ---------------------------------------------------------------
424
            
425
        switch (mode) {
426
        case ALIGN_FIRST_LINE_START:
427
            newReferencePoint.x = DefaultXMargin;
428
            newReferencePoint.y = DefaultYMargin;
429
            break;
430
        
431
        case ALIGN_PAGE_START:
432
            newReferencePoint.x = visibleWidth / 2;
433
            newReferencePoint.y = DefaultYMargin;
434
            break;
435
        
436
        case ALIGN_FIRST_LINE_END:
437
            newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
438
            newReferencePoint.y = DefaultYMargin;
439
            break;
440
        
441
        case ALIGN_FIRST_LINE_END_CASCADE:
442
            newReferencePoint.x = visibleWidth - window.getWidth() - (int)(DefaultXMargin + (DefaultXMargin*1.5*this.alignCounter));
443
            newReferencePoint.y = DefaultYMargin + (int)(DefaultYMargin*1.5*this.alignCounter);
444
            if( ++this.alignCounter >5 ) {
445
                    this.alignCounter = 0;
446
            }
447
            break;
448
        
449
        case ALIGN_LINE_START:
450
            newReferencePoint.x = DefaultXMargin;
451
            newReferencePoint.y = visibleHeight / 2;
452
            break;
453
        
454
        case ALIGN_LINE_END:
455
            newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
456
            newReferencePoint.y = visibleHeight / 2;
457
            break;
458
        
459
        case ALIGN_LAST_LINE_START:
460
            newReferencePoint.x = DefaultXMargin;
461
            newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
462
            break;
463
        
464
        case ALIGN_PAGE_END:
465
            newReferencePoint.x = visibleWidth / 2;
466
            newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
467
            break;
468
        
469
        case ALIGN_LAST_LINE_END:
470
            newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
471
            newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
472
            break;
473
        
474
        default:
475
        case ALIGN_CENTER:
476
            newReferencePoint.x = visibleWidth / 2;
477
            newReferencePoint.y = visibleHeight / 2;
478
            break;
479
        }
480
        
481
        if( newReferencePoint.x < 0 ) {
482
            newReferencePoint.x = DefaultXMargin;
483
        }
484
        if( newReferencePoint.y < 0 ) {
485
            newReferencePoint.y = DefaultYMargin;
486
        }
487
        window.setLocation(newReferencePoint);
488
    }
489

    
490
    /**
491
     * DOCUMENT ME!
492
     *
493
     * @param wnd
494
     *            DOCUMENT ME!
495
     * @param wi
496
     *            DOCUMENT ME!
497
     */
498
    private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
499
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
500
        wnd.addInternalFrameListener(new FrameListener());
501

    
502
        if (wi.isModeless() || wi.isPalette()) {
503
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
504
            if (wi.isPalette())
505
                wnd.setFocusable(false);
506
        } else {
507
            panel.add(wnd);
508
        }
509
        updateFrameProperties(wnd, wi);
510
        activateJInternalFrame(wnd);
511
        try{
512
                wnd.setMaximum(wi.isMaximized());
513
        }catch(Exception ex){
514
                logger.warn("Error: ", ex);
515
        }
516
    }
517

    
518
    private void updateFrameProperties(JInternalFrame frame, WindowInfo wi) {
519
            int height, width;
520
            if (wi.isMaximized()) {
521
                    if (wi.getNormalWidth()!=-1)
522
                            width = wi.getNormalWidth();
523
                    else
524
                            width = frame.getNormalBounds().width;
525
                    if (wi.getNormalHeight()!=-1)
526
                            height = wi.getNormalHeight();
527
                    else
528
                            height = frame.getNormalBounds().height;
529

    
530
                    frame.setSize(width, height);
531
                    frame.setLocation(wi.getNormalX(), wi.getNormalY());
532
            }
533
            else {
534
                    if (wi.getWidth()!=-1)
535
                            width = wi.getWidth();
536
                    else
537
                            width = frame.getWidth();
538
                    if (wi.getHeight()!=-1)
539
                            height = wi.getHeight();
540
                    else
541
                            height = frame.getHeight();
542
                    frame.setSize(width, height);
543
                frame.setLocation(wi.getX(), wi.getY());
544
            }
545
            frame.setTitle(wi.getTitle());
546
            frame.setVisible(wi.isVisible());
547
            frame.setResizable(wi.isResizable());
548
            frame.setIconifiable(wi.isIconifiable());
549
            frame.setMaximizable(wi.isMaximizable());
550
            try {
551
                        frame.setMaximum(wi.isMaximized());
552
                } catch (PropertyVetoException e) {
553
                        // TODO Auto-generated catch block
554
                        //e.printStackTrace();
555
                }
556
    }
557

    
558
    /**
559
     * DOCUMENT ME!
560
     *
561
     * @param p
562
     */
563
    private void addJInternalFrame(IWindow p) {
564
        WindowInfo wi = wis.getWindowInfo(p);
565

    
566
        JInternalFrame wnd = fws.getJInternalFrame(p);
567

    
568
        if (p instanceof SingletonWindow) {
569
            SingletonWindow sv = (SingletonWindow) p;
570
            sws.openSingletonWindow(sv, wnd);
571
        }
572

    
573
        addJInternalFrame(wnd, wi);
574
    }
575

    
576
    /**
577
     * DOCUMENT ME!
578
     *
579
     * @param wnd
580
     */
581
    private void activateJInternalFrame(JInternalFrame wnd) {
582
            JPopupMenu.setDefaultLightWeightPopupEnabled(false);
583
        try {
584
            wnd.moveToFront();
585
            logger.debug("Activando " + wnd.getTitle());
586
            wnd.setSelected(true);
587
            wnd.setIcon(false);
588
        } catch (PropertyVetoException e) {
589
            logger.error(e.toString());
590
        }
591
    }
592

    
593
    /**
594
     * Situa un di�logo modal en el centro de la pantalla
595
     *
596
     * @param d
597
     *            Di�logo que se quiere situar
598
     */
599
    private void centerDialog(JDialog d) {
600
        int offSetX = d.getWidth() / 2;
601
        int offSetY = d.getHeight() / 2;
602

    
603
        d.setLocation((mainFrame.getWidth() / 2) - offSetX, (mainFrame
604
                .getHeight() / 2)
605
                - offSetY);
606
    }
607

    
608
    /**
609
     * DOCUMENT ME!
610
     *
611
     * @param p
612
     */
613
    private void addJDialog(IWindow p) {
614
        JDialog dlg = fws.getJDialog(p);
615

    
616
        centerDialog(dlg);
617

    
618
        dlg.addWindowListener(new DialogWindowListener());
619
        dss.pushDialog(dlg);
620

    
621
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
622
    }
623

    
624
    /*
625
     * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveWindow()
626
     */
627
    public IWindow getActiveWindow() {
628
        JInternalFrame jif = panel.getSelectedFrame();
629

    
630
        if (jif != null) {
631
            IWindow theWindow = fws.getWindow(jif);
632
            if (theWindow == null)
633
                return null;
634
            if (theWindow.getWindowInfo().isPalette())
635
                return wss.getActiveWindow();
636
            else
637
                return fws.getWindow(jif);
638
        }
639
        // return vss.getActiveView();
640

    
641
        return null;
642
    }
643
    public IWindow getFocusWindow(){
644
             JInternalFrame jif = panel.getSelectedFrame();
645

    
646
         if (jif != null) {
647
             IWindow theView = fws.getWindow(jif);
648
             if (theView == null)
649
                 return null;
650
             return fws.getWindow(jif);
651
         }
652
         return null;
653
    }
654
    /*
655
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeWindow(com.iver.andami.ui.mdiManager.IWindow)
656
     */
657
    public void closeWindow(IWindow p) {
658
        // Si es un di�logo modal
659
        if (p.getWindowInfo().isModal()) {
660
            closeJDialog();
661
        } else { // Si no es modal se cierra el JInternalFrame
662
            closeJInternalFrame(fws.getJInternalFrame(p));
663
        }
664
    }
665

    
666
    /*
667
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllWindows()
668
     */
669
    public void closeAllWindows() {
670
        ArrayList eliminar = new ArrayList();
671
        Iterator i = fws.getWindowIterator();
672

    
673
        while (i.hasNext()) {
674
            eliminar.add((IWindow) i.next());
675
        }
676

    
677
        for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
678
            IWindow vista = (IWindow) iter.next();
679
            closeWindow(vista);
680
        }
681
    }
682

    
683
    /*
684
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
685
     */
686
    public WindowInfo getWindowInfo(IWindow w) {
687
            WindowInfo wi = wis.getWindowInfo(w);
688

    
689
    /*
690
     * This is done now in vis.getWindowInfo(w)
691
     *
692
     * JInternalFrame f = fws.getJInternalFrame(w);
693
            wi.setX(f.getX());
694
            wi.setY(f.getY());
695
            wi.setHeight(f.getHeight());
696
            wi.setWidth(f.getWidth());
697
            // isClosed() doesn't work as (I) expected, why? Using isShowing instead
698
            wi.setClosed(!f.isShowing());
699
            wi.setNormalBounds(f.getNormalBounds());
700
            wi.setMaximized(f.isMaximum());*/
701
            return wi;
702
    }
703

    
704
    /**
705
     * DOCUMENT ME!
706
     *
707
     * @param dialog
708
     * @throws RuntimeException
709
     *             DOCUMENT ME!
710
     */
711
    private void closeJDialog() {
712
        JDialog dlg = dss.popDialog();
713
        if (dlg==null)
714
                return;
715
        dlg.setVisible(false);
716

    
717
        IWindow s = fws.getWindow(dlg);
718

    
719
        callWindowClosed(s);
720

    
721
        fws.closeWindow(s);
722

    
723
        // Si es singleton se desasocia el modelo con la vista
724
        if (s instanceof SingletonWindow) {
725
            sws.closeWindow((SingletonWindow) s);
726
        }
727
    }
728

    
729
    /**
730
     * If <code>window</code> implements IWindowListener, sent it
731
     * the windowActivated event.
732
     *
733
     * @param window
734
     *            The IWindow which has to be notified.
735
     */
736
    private void callWindowClosed(IWindow window) {
737
        if (window instanceof IWindowListener) {
738
            ((IWindowListener) window).windowClosed();
739
        }
740
    }
741

    
742
    /**
743
     * If <code>window</code> implements IWindowListener, sent it
744
     * the windowActivated event.
745
     *
746
     * @param window
747
     *            The IWindow which has to be notified.
748
     */
749
    private void callWindowActivated(IWindow window) {
750
//        logger.debug("View '" + window.getWindowInfo().getTitle()
751
//               + "' activated (callViewActivated)");
752
        if (window instanceof IWindowListener) {
753
            ((IWindowListener) window).windowActivated();
754
        }
755
    }
756

    
757
    /**
758
     * DOCUMENT ME!
759
     *
760
     * @param frame
761
     */
762
    private void closeJInternalFrame(JInternalFrame frame) {
763
        try {
764
            IWindow s = (IWindow) fws.getWindow(frame);
765

    
766
            frame.setClosed(true);
767
            callWindowClosed(s);
768
        } catch (PropertyVetoException e) {
769
            logger
770
                    .error(
771
                            "Not compatible with property veto's. Use ViewInfo instead.",
772
                            e);
773
        }
774
    }
775

    
776
    /*
777
     * @see com.iver.andami.plugins.IExtension#initialize()
778
     */
779
    public void initialize() {
780
    }
781

    
782
    /*
783
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
784
     */
785
    public void execute(String actionCommand) {
786
        if ("window-cascade".equalsIgnoreCase(actionCommand)) {
787
                logger.info("action window-cascade not implemented.");
788

    
789
        } else if ("window-tile".equalsIgnoreCase(actionCommand)) {
790
                logger.info("action window-tile not implemented.");
791
        }
792
    }
793

    
794
    /*
795
     * @see com.iver.andami.plugins.IExtension#isEnabled()
796
     */
797
    public boolean isEnabled() {
798
        // TODO Auto-generated method stub
799
        return false;
800
    }
801

    
802
    /*
803
     * @see com.iver.andami.plugins.IExtension#isVisible()
804
     */
805
    public boolean isVisible() {
806
        // TODO Auto-generated method stub
807
        return true;
808
    }
809

    
810
    /*
811
     * @see com.iver.andami.ui.mdiManager.MDIManager#setWaitCursor()
812
     */
813
    public void setWaitCursor() {
814
        if (mainFrame != null) {
815
            glassPane.setVisible(true);
816
            lastCursor = mainFrame.getCursor();
817
            dss.setWaitCursor();
818
            glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
819
        }
820
    }
821

    
822
    /*
823
     * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
824
     */
825
    public void restoreCursor() {
826
        if (mainFrame != null) {
827
            glassPane.setVisible(false);
828
            dss.restoreCursor();
829
            glassPane.setCursor(lastCursor);
830
        }
831
    }
832

    
833
    /**
834
     * Listener para los eventos de cerrado de los di�logos. Tiene su raz�n de
835
     * ser en que los di�logos han de devolverse al pool cuando se cierran
836
     *
837
     * @author Fernando Gonz�lez Cort�s
838
     */
839
    public class DialogWindowListener extends WindowAdapter {
840
        /**
841
         * Captura el evento de cerrado de los di�logos con el fin de realizar
842
         * tareas de mantenimiento
843
         *
844
         * @param e
845
         *            evento
846
         */
847
        public void windowActivated(WindowEvent e) {
848
                IWindow window = fws.getWindow((Component) e.getSource());
849
                callWindowActivated(window);
850

    
851
        }
852

    
853

    
854
        /**
855
         * Captura el evento de cerrado de los di�logos con el fin de realizar
856
         * tareas de mantenimiento
857
         *
858
         * @param e
859
         *            evento
860
         */
861
        public void windowClosing(WindowEvent e) {
862
            closeJDialog();
863
        }
864
    }
865

    
866
    /**
867
     * DOCUMENT ME!
868
     */
869
    public class FrameListener implements InternalFrameListener {
870
        /*
871
         * @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
872
         */
873
        public void internalFrameActivated(InternalFrameEvent e) {
874
            // logger.debug("internalFrameActivated " +
875
            // e.getInternalFrame().getTitle());
876

    
877
            // activatedInternalFramesStack.push(e.getInternalFrame());
878

    
879
            IWindow panel = fws.getWindow((JInternalFrame) e.getSource());
880

    
881
            WindowInfo wi = wis.getWindowInfo(panel);
882
            if (wi.isPalette())
883
                return;
884

    
885
            wss.setActive(panel);
886

    
887
            JInternalFrame frame = fws.getJInternalFrame(panel);
888

    
889
            if (wi.isMaximizable()){
890
                    frame.setMaximizable(true);
891
            }
892
            if (!frame.isMaximizable() && frame.isMaximum()) {
893
                try {
894
                    frame.setMaximum(false);
895
                } catch (PropertyVetoException e1) {
896
                }
897
            }
898
            mainFrame.enableControls();
899
            if (wi.getSelectedTools()==null) {
900
                    // this is the first time this window is activated
901
                    wi.setSelectedTools(new HashMap(mainFrame.getInitialSelectedTools()));
902
            }
903
            mainFrame.setSelectedTools(wi.getSelectedTools());
904
            callWindowActivated(panel);
905

    
906
        }
907

    
908
        /*
909
         * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
910
         */
911
        public void internalFrameClosed(InternalFrameEvent e) {
912
        }
913

    
914
        /*
915
         * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
916
         */
917
        public void internalFrameClosing(InternalFrameEvent e) {
918
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
919
            // logger.debug("internalFrameClosing " +
920
            // e.getInternalFrame().getTitle());
921

    
922
            JInternalFrame c = (JInternalFrame) e.getSource();
923
            WindowInfo wi = wis.getWindowInfo((IWindow) fws.getWindow(c));
924

    
925
            IWindow win = fws.getWindow(c);
926
            callWindowClosed(win);
927
            boolean alwaysLive;
928
            if (win instanceof SingletonWindow) {
929
                sws.closeWindow((SingletonWindow) win);
930
            }
931

    
932
            fws.closeWindow(win);
933

    
934
            panel.remove(c);
935

    
936
            wss.remove(win);
937

    
938
            if (!wi.isPalette())
939
                mainFrame.enableControls();
940
            panel.repaint();
941

    
942
            // Para activar el JInternalFrame desde la que hemos
943
            // abierto la ventana que estamos cerrando
944
            IWindow lastWindow = wss.getActiveWindow();
945
            // La activamos
946
            if (lastWindow != null) {
947
                    logger.debug(PluginServices.getText(this, "Devuelvo_el_foco_a_")+lastWindow.getWindowInfo().getTitle());
948
                JInternalFrame frame = fws.getJInternalFrame(lastWindow);
949
                try {
950
                    frame.setSelected(true);
951
                } catch (PropertyVetoException e1) {
952
                    // TODO Auto-generated catch block
953
                    // e1.printStackTrace();
954
                }
955
                // addView(lastView);
956
            }
957

    
958
        }
959

    
960
        /*
961
         * @see javax.swing.event.InternalFrameListener#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)
962
         */
963
        public void internalFrameDeactivated(InternalFrameEvent e) {
964
            // logger.debug("internalDeActivated " +
965
            // e.getInternalFrame().getTitle());
966
            JInternalFrame c = (JInternalFrame) e.getSource();
967
            IWindow win = fws.getWindow(c);
968
            if (win != null) {
969
                WindowInfo wi = wis.getWindowInfo(win);
970
                if (wi.isPalette())
971
                    return;
972

    
973
            }
974

    
975
        }
976

    
977
        /*
978
         * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
979
         */
980
        public void internalFrameDeiconified(InternalFrameEvent e) {
981
            mainFrame.enableControls();
982
        }
983

    
984
        /*
985
         * @see javax.swing.event.InternalFrameListener#internalFrameIconified(javax.swing.event.InternalFrameEvent)
986
         */
987
        public void internalFrameIconified(InternalFrameEvent e) {
988
            mainFrame.enableControls();
989
        }
990

    
991
        /*
992
         * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
993
         */
994
        public void internalFrameOpened(InternalFrameEvent e) {
995
            // logger.debug("internalFrameOpened. Source= " +
996
            // e.getSource().toString());
997
        }
998
    }
999

    
1000
    /*
1001
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
1002
     *      java.lang.Object)
1003
     */
1004
    public boolean closeSingletonWindow(Class viewClass, Object model) {
1005
        JInternalFrame frame = (JInternalFrame) sws.getFrame(viewClass, model);
1006
        if (frame == null)
1007
            return false;
1008
        closeJInternalFrame(frame);
1009
        return true;
1010
    }
1011

    
1012
    /*
1013
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
1014
     */
1015
    public boolean closeSingletonWindow(Object model) {
1016
        JInternalFrame[] frames = (JInternalFrame[]) sws.getFrames(model);
1017
        if (frames.length == 0)
1018
            return false;
1019
        for (int i = 0; i < frames.length; i++) {
1020
            closeJInternalFrame(frames[i]);
1021
        }
1022
        return true;
1023
    }
1024

    
1025
    /*
1026
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
1027
     */
1028
    public IWindow[] getAllWindows() {
1029
        ArrayList windows = new ArrayList();
1030
        Iterator i = fws.getWindowIterator();
1031

    
1032
        while (i.hasNext()) {
1033
            windows.add((IWindow) i.next());
1034
        }
1035
        return (IWindow[]) windows.toArray(new IWindow[0]);
1036
    }
1037

    
1038
    /*
1039
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
1040
     */
1041
    public IWindow[] getOrderedWindows() {
1042
        TreeMap windows = new TreeMap();
1043
        Iterator winIterator = fws.getWindowIterator();
1044

    
1045
        Component frame;
1046
        IWindow win;
1047
        /**
1048
         * The order of the window in the JDesktopPane. Smaller numbers
1049
         * are closer to the foreground.
1050
         */
1051
        int zPosition;
1052
        while (winIterator.hasNext()) {
1053
                win = (IWindow) winIterator.next();
1054
                frame = fws.getFrame(win);
1055
                    zPosition = panel.getPosition(frame);
1056
                    int layer = panel.getLayer(frame);
1057

    
1058
                    if (! (frame instanceof JDialog)) { //JDialogs are not in inside the LayeredPane
1059
                            // flatten all the layers
1060
                            if (layer == JLayeredPane.DEFAULT_LAYER.intValue()) {
1061
                                    zPosition+=50000;
1062
                            }
1063
                            else if (layer == JLayeredPane.PALETTE_LAYER.intValue()) {
1064
                                    zPosition+=40000;
1065
                            }
1066
                            else if (layer == JLayeredPane.MODAL_LAYER.intValue()) {
1067
                                    zPosition+=30000;
1068
                            }
1069
                            else if  (layer == JLayeredPane.POPUP_LAYER.intValue()) {
1070
                                    zPosition+=20000;
1071
                            }
1072
                            else if  (layer == JLayeredPane.DRAG_LAYER.intValue()) {
1073
                                    zPosition+=10000;
1074
                            }
1075
                    }
1076
                    windows.put(new Integer(zPosition), win);
1077
        }
1078
        winIterator = windows.values().iterator();
1079
        ArrayList winList = new ArrayList();
1080
        while (winIterator.hasNext()) {
1081
                winList.add(winIterator.next());
1082
        }
1083

    
1084
        return (IWindow[]) winList.toArray(new IWindow[0]);
1085
    }
1086
    public void setMaximum(IWindow v, boolean bMaximum) throws PropertyVetoException
1087
    {
1088
        JInternalFrame f = fws.getJInternalFrame(v);
1089
        f.setMaximum(bMaximum);
1090
    }
1091

    
1092
    public void changeWindowInfo(IWindow w, WindowInfo wi){
1093
            JInternalFrame f = fws.getJInternalFrame(w);
1094
            f.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
1095
            JDesktopPane pnl = f.getDesktopPane();
1096
            pnl.remove(f);
1097
            int width;
1098
            int height;
1099
            if (wi.getWidth()!=-1)
1100
                    width = wi.getWidth();
1101
            else
1102
                    width = f.getWidth();
1103
            if (wi.getHeight()!=-1)
1104
                    height = wi.getHeight();
1105
            else
1106
                    height = f.getHeight();
1107
            f.setSize(new Dimension(width, height));
1108
            f.setLocation(wi.getX(), wi.getY());
1109
            if (wi.isPalette()) {
1110
                    pnl.add(f, JDesktopPane.PALETTE_LAYER);
1111
                    f.setFocusable(false);
1112
            } else {
1113
                    pnl.add(f, JDesktopPane.DEFAULT_LAYER);
1114
                    f.setFocusable(true);
1115
                    if (wi.isClosed()) {
1116
                            closeWindow(w);
1117
                    }
1118
            }
1119

    
1120
            if (wi.isMaximized()) {
1121
                    try {
1122
                            f.setMaximum(true);
1123
                    } catch (PropertyVetoException e) {
1124
                            // TODO Auto-generated catch block
1125
                            //e.printStackTrace();
1126
                    }
1127
                    f.setNormalBounds(wi.getNormalBounds());
1128
            }
1129
            activateJInternalFrame(f);
1130
    }
1131

    
1132
    public void refresh(IWindow win) {
1133
            Component frame = fws.getFrame(win);
1134
            if (frame!=null)
1135
                    frame.setVisible(true);
1136
    }
1137

    
1138
        public void setBackgroundImage(ImageIcon image, String typeDesktop) {
1139
                this.image=image;
1140
                this.typeDesktop=typeDesktop;
1141

    
1142
        }
1143
        class MyDesktopPane extends JDesktopPane
1144
            {
1145
                  protected Map listeners = new Hashtable();
1146
                
1147
                
1148
                  public MyDesktopPane(){
1149
              }
1150
                  
1151
                  public Dimension getPreferredSize() {
1152
                                JInternalFrame[] array = this.getAllFrames();
1153
                                int maxX = 0;
1154
                                int maxY = 0;
1155
                                for (int i = 0; i < array.length; i++) {
1156
                                        int x = array[i].getX() + array[i].getWidth();
1157
                                        if (x > maxX)
1158
                                                maxX = x;
1159
                                        int y = array[i].getY() + array[i].getHeight();
1160
                                        if (y > maxY)
1161
                                                maxY = y;
1162
                                }
1163
                                return new Dimension(maxX, maxY);
1164
                        }
1165

    
1166
                        /**
1167
                         * Add an internal-frame to the desktop. Sets a component-listener on
1168
                         * it, which resizes the desktop if a frame is resized.
1169
                         */
1170
                  public Component add(Component comp) {
1171
                                super.add(comp);
1172
                                ComponentListener listener = new ComponentListener() {
1173
                                        public void componentResized(ComponentEvent e) { // Layout the
1174
                                                // JScrollPane
1175
                                                getParent().getParent().validate();
1176
                                        }
1177

    
1178
                                        public void componentMoved(ComponentEvent e) {
1179
                                                JInternalFrame comp = (JInternalFrame) e.getSource();
1180
                                                
1181
                                                //no deja que la ventana se vaya hacia la izquierda o arriba
1182
                                                if( comp.getX()<0 ) {
1183
                                                        comp.setLocation(0, comp.getY());
1184
                                                }
1185
                                                if (comp.getY()<0){
1186
                                                        comp.setLocation(comp.getX(), 0);
1187
                                                }
1188
                                                componentResized(e);
1189
                                        }
1190
                                        public void componentShown(ComponentEvent e) {
1191
                                        }
1192

    
1193
                                        public void componentHidden(ComponentEvent e) {
1194
                                        }
1195

    
1196
                                };
1197
                                comp.addComponentListener(listener);
1198
                                listeners.put(comp, listener);
1199
                                return comp;
1200
                        }
1201

    
1202
                        /**
1203
                         * Remove an internal-frame from the desktop. Removes the
1204
                         * component-listener and resizes the desktop.
1205
                         */
1206
                        public void remove(Component comp) {
1207
                                comp.removeComponentListener((ComponentListener) listeners
1208
                                                .get(comp));
1209
                                super.remove(comp);
1210
                                getParent().getParent().validate(); // Layout the JScrollPane
1211
                        }
1212

    
1213
                 public void paintComponent(Graphics g){
1214
                super.paintComponent(g);
1215
                int x=0;
1216
                int y=0;
1217
                int w=0;
1218
                int h=0;
1219
                if (image != null) {
1220
                                if (typeDesktop.equals(Theme.CENTERED)) {
1221
                                        w = image.getIconWidth();
1222
                                        h = image.getIconHeight();
1223
                                        x = (getWidth() - w) / 2;
1224
                                        y = (getHeight() - h) / 2;
1225
                                        g.drawImage(image.getImage(), x, y, w, h, this);
1226
                                } else if (typeDesktop.equals(Theme.EXPAND)) {
1227
                                        w = getWidth();
1228
                                        h = getHeight();
1229
                                        g.drawImage(image.getImage(), x, y, w, h, this);
1230
                                } else if (typeDesktop.equals(Theme.MOSAIC)) {
1231
                                        int wAux = image.getIconWidth();
1232
                                        int hAux = image.getIconHeight();
1233
                                        int i = 0;
1234
                                        int j = 0;
1235
                                        w = wAux;
1236
                                        h = hAux;
1237
                                        while (x < getWidth()) {
1238
                                                x = wAux * i;
1239
                                                while (y < getHeight()) {
1240
                                                        y = hAux * j;
1241
                                                        j++;
1242
                                                        g.drawImage(image.getImage(), x, y, w, h, this);
1243
                                                }
1244
                                                y = 0;
1245
                                                j = 0;
1246
                                                i++;
1247
                                        }
1248
                                }
1249
                        }
1250
              }
1251
            }
1252

    
1253
    public void showWindow(JPanel panel, String title, MODE mode) {
1254
        // This prepare a default JPanel that implements IWindow 
1255
        // for the passed panel and call to the addWindow
1256
        // to show it.
1257
        ToolsSwingLocator.getWindowManager().showWindow(panel, title, mode);
1258
    }
1259
    
1260
}