Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.coreplugin.app / org.gvsig.coreplugin.app.mainplugin / src / main / java / org / gvsig / coreplugin / mdiManager / NewSkin.java @ 41314

History | View | Annotate | Download (46.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.coreplugin.mdiManager;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Color;
28
import java.awt.Component;
29
import java.awt.Container;
30
import java.awt.Cursor;
31
import java.awt.Dimension;
32
import java.awt.Graphics;
33
import java.awt.KeyEventDispatcher;
34
import java.awt.Point;
35
import java.awt.event.ActionEvent;
36
import java.awt.event.ActionListener;
37
import java.awt.event.ComponentEvent;
38
import java.awt.event.ComponentListener;
39
import java.awt.event.KeyEvent;
40
import java.awt.event.WindowAdapter;
41
import java.awt.event.WindowEvent;
42
import java.beans.PropertyVetoException;
43
import java.util.ArrayList;
44
import java.util.HashMap;
45
import java.util.Hashtable;
46
import java.util.Iterator;
47
import java.util.Locale;
48
import java.util.Map;
49
import java.util.TreeMap;
50

    
51
import javax.swing.DefaultDesktopManager;
52
import javax.swing.DesktopManager;
53
import javax.swing.ImageIcon;
54
import javax.swing.JComponent;
55
import javax.swing.JDesktopPane;
56
import javax.swing.JDialog;
57
import javax.swing.JFrame;
58
import javax.swing.JInternalFrame;
59
import javax.swing.JLayeredPane;
60
import javax.swing.JPanel;
61
import javax.swing.JPopupMenu;
62
import javax.swing.JScrollPane;
63
import javax.swing.KeyStroke;
64
import javax.swing.SwingUtilities;
65
import javax.swing.event.InternalFrameEvent;
66
import javax.swing.event.InternalFrameListener;
67

    
68
import org.gvsig.andami.IconThemeHelper;
69
import org.gvsig.andami.PluginServices;
70
import org.gvsig.andami.plugins.Extension;
71
import org.gvsig.andami.ui.mdiFrame.GlassPane;
72
import org.gvsig.andami.ui.mdiFrame.MDIFrame;
73
import org.gvsig.andami.ui.mdiFrame.NewStatusBar;
74
import org.gvsig.andami.ui.mdiManager.IWindow;
75
import org.gvsig.andami.ui.mdiManager.IWindowListener;
76
import org.gvsig.andami.ui.mdiManager.MDIManager;
77
import org.gvsig.andami.ui.mdiManager.MDIUtilities;
78
import org.gvsig.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
79
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
80
import org.gvsig.andami.ui.mdiManager.WindowInfo;
81
import org.gvsig.andami.ui.theme.Theme;
82
import org.gvsig.tools.swing.api.ToolsSwingLocator;
83
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
84
import org.gvsig.tools.task.RunnableWithParameters;
85
import org.slf4j.Logger;
86
import org.slf4j.LoggerFactory;
87

    
88

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

    
98

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

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

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

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

    
115
    private GlassPane glassPane = new GlassPane();
116

    
117
    private DialogStackSupport dss;
118

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

    
124
    private WindowInfoSupport wis;
125

    
126
    private WindowStackSupport wss;
127

    
128
    private SingletonWindowSupport sws;
129

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

    
134
        private int alignCounter = 1;
135

    
136
        //Anyade barras de scroll
137
        private void addScrolledDesktopPanel( JFrame parent, MyDesktopPane desktopPane) {
138
                JPanel toppanel;
139

    
140
                toppanel = new JPanel();
141
                toppanel.setLayout(new BorderLayout());
142
                toppanel.setPreferredSize(new Dimension(200,200));
143
                toppanel.setBackground(Color.RED);
144
                
145
                JScrollPane scrollPanel = new JScrollPane(desktopPane);
146
                
147
                toppanel.add(scrollPanel, BorderLayout.CENTER);
148

    
149
                parent.getContentPane().add(toppanel, BorderLayout.CENTER);
150
        }
151
        
152
        public JDesktopPane getDesktopPane() {
153
            return this.panel;
154
        }
155
        
156
    /*
157
     * @see com.iver.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
158
     */
159
    public void init(MDIFrame f) {
160
            
161
        // Inicializa el Frame y la consola
162
            mainFrame = f;
163
            mainFrame.setGlassPane(glassPane);
164
        panel.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
165

    
166
            //Anyade barras de scroll
167
        //mainFrame.getContentPane().add(panel, BorderLayout.CENTER);
168
        addScrolledDesktopPanel(mainFrame,panel);
169

    
170
        panel.setDesktopManager(desktopManager);
171

    
172
        fws = createFrameWindowSupport(mainFrame);
173
        dss = new DialogStackSupport(mainFrame);
174
        sws = new SingletonWindowSupport(wis, fws);
175
        wis = new WindowInfoSupport(mainFrame, fws, sws);
176
        fws.setVis(wis);
177
        wss = new WindowStackSupport(wis);
178

    
179

    
180
        // TODO (jaume) esto no deber�a de estar aqu�...
181
        // molar�a m�s en un di�logo de preferencias
182
        // es s�lo una prueba
183
        KeyStroke controlTab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.CTRL_MASK);
184

    
185
        PluginServices.registerKeyStroke(controlTab, new KeyEventDispatcher() {
186

    
187
                        public boolean dispatchKeyEvent(KeyEvent e) {
188
                                IWindow[] views = getAllWindows();
189
                                if (views.length<=0 || e.getID() == KeyEvent.KEY_PRESSED)
190
                                        return false;
191

    
192

    
193
                                int current = 0;
194
                                for (int i = 0; i < views.length; i++) {
195
                                        if (views[i].equals(getActiveWindow())) {
196
                                                current = i;
197
                                                break;
198
                                        }
199
                                }
200
                                addWindow(views[(current +1) % views.length]);
201
                                return true;
202
                        }
203

    
204
        });
205
    }
206

    
207
    /**
208
     * Create the {@link FrameWindowSupport} instance
209
     * 
210
     * @param mainFrame
211
     * 
212
     */
213
    protected FrameWindowSupport createFrameWindowSupport(MDIFrame mainFrame) {
214
        return new FrameWindowSupport(mainFrame);
215
    }
216

    
217
    /* (non-javadoc)
218
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
219
     */
220
    public IWindow addWindow(final IWindow p) throws SingletonDialogAlreadyShownException {
221
            
222
            if( !SwingUtilities.isEventDispatchThread() ) {
223
                    RunnableWithParameters action = new RunnableWithParameters() {
224
                                public void run() {
225
                                        this.returnValue = addWindow(p);
226
                                }
227
                        };
228
                        try {
229
                                SwingUtilities.invokeAndWait(action);
230
                        } catch (Exception e) {
231
                                logger.info("Can't add window from othrer thread that EventDispatch",e );
232
                                IllegalThreadStateException e2 = new IllegalThreadStateException();
233
                                e2.initCause(e);
234
                                throw e2;
235
                        }
236
                        return (IWindow) action.getReturnValue();
237
            }
238
            
239
        // se obtiene la informaci�n de la vista
240
        WindowInfo wi = wis.getWindowInfo(p);
241

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

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

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

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

    
300
            return p;
301
        }
302
    }
303

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

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

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

    
329
        if (p instanceof SingletonWindow) {
330
            SingletonWindow sw = (SingletonWindow) p;
331
            if (sws.registerWindow(sw.getClass(), sw.getWindowModel(), wi)) {
332
                singletonPreviouslyAdded = true;
333
            }
334
        }
335
        
336
        if( !singletonPreviouslyAdded ) {
337
            Point new_loc = this.getLocationForAlignment(p, align);
338
            p.getWindowInfo().setX(new_loc.x);
339
            p.getWindowInfo().setY(new_loc.y);
340
        }
341
        
342
        IWindow window = addWindow(p);
343
        return window;
344
    }
345

    
346

    
347
        /**
348
         * Centres the Frame in the contentPane of the MainFrame.
349
         * If the frame can't be showed completely, it tries to show its top-left
350
         * corner.
351
         *
352
         * @author Pablo Piqueras Bartolom�
353
         *
354
         * @param panel The IWindow to centre
355
         */
356
        public synchronized void centreFrame(final IWindow panel) {
357
                if( !SwingUtilities.isEventDispatchThread() ) {
358
                        SwingUtilities.invokeLater( new Runnable() {
359
                                public void run() {
360
                                        centreFrame(panel);
361
                                }
362
                        });
363
                        return;
364
                }
365
                Component window = fws.getFrame(panel);
366
                if (window==null) return;
367

    
368
                // The top-left square of frame reference
369
                Point newReferencePoint = new Point();
370

    
371
                // A reference to the panel where the JInternalFrame will be displayed
372
                Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
373

    
374
                // Get the NewStatusBar component
375
                NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
376
                JDesktopPane jDesktopPane = this.getDesktopPane(); 
377

    
378
                int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The last substraction is for if there is any menu,... at left
379
                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
380
                int freeWidth = visibleWidth - window.getWidth();
381
                int freeHeight = visibleHeight - window.getHeight();
382

    
383
                // Calculate the new point reference (Assure that the top-left corner is showed)
384
                if (freeWidth < 0)
385
                {
386
                        if (visibleWidth > MinimumXMargin)
387
                                newReferencePoint.x = DefaultXMargin;
388
                        else
389
                                newReferencePoint.x = 0;
390
                }
391
                else
392
                        newReferencePoint.x = freeWidth / 2;
393

    
394
                if (freeHeight < 0)
395
                {
396
                        if (visibleHeight > MinimumYMargin)
397
                                newReferencePoint.y = DefaultYMargin;
398
                        else
399
                                newReferencePoint.y = 0;
400
                }
401
                else
402
                        newReferencePoint.y = freeHeight / 2;
403

    
404
                // Set the new location for this JInternalFrame
405
                window.setLocation(newReferencePoint);
406
        }
407

    
408
    public synchronized void alignFrame(final IWindow panel, final int mode) {
409
                if( !SwingUtilities.isEventDispatchThread() ) {
410
                        SwingUtilities.invokeLater( new Runnable() {
411
                                public void run() {
412
                                        alignFrame(panel,mode);
413
                                }
414
                        });
415
                        return;
416
                }
417
        Component window = fws.getFrame(panel);
418
        if (window==null) {
419
                return;
420
        }
421

    
422
        // The top-left square of frame reference
423
        Point newReferencePoint = new Point();
424

    
425
        // A reference to the panel where the JInternalFrame will be displayed
426
        Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
427

    
428
        // Get the NewStatusBar component
429
        NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
430
        JDesktopPane jDesktopPane = this.getDesktopPane(); 
431

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

    
435
//        ---------------------------------------------------------------
436
//        |FIRST_LINE_START(23)   PAGE_START(19)     FIRST_LINE_END(24) |
437
//        |                                                             |
438
//        |                                                             |
439
//        |LINE_START(21)           CENTER(10)              LINE_END(22)|
440
//        |                                                             |
441
//        |                                                             |
442
//        |LAST_LINE_START(25)     PAGE_END(20)       LAST_LINE_END(26) |
443
//        ---------------------------------------------------------------
444
            
445
        switch (mode) {
446
        case ALIGN_FIRST_LINE_START:
447
            newReferencePoint.x = DefaultXMargin;
448
            newReferencePoint.y = DefaultYMargin;
449
            break;
450
        
451
        case ALIGN_PAGE_START:
452
            newReferencePoint.x = visibleWidth / 2;
453
            newReferencePoint.y = DefaultYMargin;
454
            break;
455
        
456
        case ALIGN_FIRST_LINE_END:
457
            newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
458
            newReferencePoint.y = DefaultYMargin;
459
            break;
460
        
461
        case ALIGN_FIRST_LINE_END_CASCADE:
462
            newReferencePoint.x = visibleWidth - window.getWidth() - (int)(DefaultXMargin + (DefaultXMargin*1.5*this.alignCounter));
463
            newReferencePoint.y = DefaultYMargin + (int)(DefaultYMargin*1.5*this.alignCounter);
464
            if( ++this.alignCounter >5 ) {
465
                    this.alignCounter = 0;
466
            }
467
            break;
468
        
469
        case ALIGN_LINE_START:
470
            newReferencePoint.x = DefaultXMargin;
471
            newReferencePoint.y = visibleHeight / 2;
472
            break;
473
        
474
        case ALIGN_LINE_END:
475
            newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
476
            newReferencePoint.y = visibleHeight / 2;
477
            break;
478
        
479
        case ALIGN_LAST_LINE_START:
480
            newReferencePoint.x = DefaultXMargin;
481
            newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
482
            break;
483
        
484
        case ALIGN_PAGE_END:
485
            newReferencePoint.x = visibleWidth / 2;
486
            newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
487
            break;
488
        
489
        case ALIGN_LAST_LINE_END:
490
            newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
491
            newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
492
            break;
493
        
494
        default:
495
        case ALIGN_CENTER:
496
            newReferencePoint.x = visibleWidth / 2;
497
            newReferencePoint.y = visibleHeight / 2;
498
            break;
499
        }
500
        
501
        if( newReferencePoint.x < 0 ) {
502
            newReferencePoint.x = DefaultXMargin;
503
        }
504
        if( newReferencePoint.y < 0 ) {
505
            newReferencePoint.y = DefaultYMargin;
506
        }
507
        window.setLocation(newReferencePoint);
508
    }
509

    
510
    /**
511
     * DOCUMENT ME!
512
     *
513
     * @param wnd
514
     *            DOCUMENT ME!
515
     * @param wi
516
     *            DOCUMENT ME!
517
     */
518
    private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
519
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
520
        wnd.addInternalFrameListener(new FrameListener());
521

    
522
        if (wi.isModeless() || wi.isPalette()) {
523
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
524
            if (wi.isPalette())
525
                wnd.setFocusable(false);
526
        } else {
527
            panel.add(wnd);
528
        }
529
        updateFrameProperties(wnd, wi);
530
        activateJInternalFrame(wnd);
531
        try{
532
                wnd.setMaximum(wi.isMaximized());
533
        }catch(Exception ex){
534
                logger.warn("Error: ", ex);
535
        }
536
    }
537

    
538
    private void updateFrameProperties(JInternalFrame frame, WindowInfo wi) {
539
            int height, width;
540
            if (wi.isMaximized()) {
541
                    if (wi.getNormalWidth()!=-1)
542
                            width = wi.getNormalWidth();
543
                    else
544
                            width = frame.getNormalBounds().width;
545
                    if (wi.getNormalHeight()!=-1)
546
                            height = wi.getNormalHeight();
547
                    else
548
                            height = frame.getNormalBounds().height;
549

    
550
                    frame.setSize(width, height);
551
                    frame.setLocation(wi.getNormalX(), wi.getNormalY());
552
            }
553
            else {
554
                    if (wi.getWidth()!=-1)
555
                            width = wi.getWidth();
556
                    else
557
                            width = frame.getWidth();
558
                    if (wi.getHeight()!=-1)
559
                            height = wi.getHeight();
560
                    else
561
                            height = frame.getHeight();
562
                    frame.setSize(width, height);
563
                frame.setLocation(wi.getX(), wi.getY());
564
            }
565
            frame.setTitle(wi.getTitle());
566
            frame.setVisible(wi.isVisible());
567
            frame.setResizable(wi.isResizable());
568
            frame.setIconifiable(wi.isIconifiable());
569
            frame.setMaximizable(wi.isMaximizable());
570
            try {
571
                        frame.setMaximum(wi.isMaximized());
572
                } catch (PropertyVetoException e) {
573
                        // TODO Auto-generated catch block
574
                        //e.printStackTrace();
575
                }
576
    }
577

    
578
    /**
579
     * DOCUMENT ME!
580
     *
581
     * @param p
582
     */
583
    private void addJInternalFrame(IWindow p) {
584
        WindowInfo wi = wis.getWindowInfo(p);
585

    
586
        JInternalFrame wnd = fws.getJInternalFrame(p);
587

    
588
        if (p instanceof SingletonWindow) {
589
            SingletonWindow sv = (SingletonWindow) p;
590
            sws.openSingletonWindow(sv, wnd);
591
        }
592

    
593
        addJInternalFrame(wnd, wi);
594
    }
595

    
596
    /**
597
     * DOCUMENT ME!
598
     *
599
     * @param wnd
600
     */
601
    private void activateJInternalFrame(JInternalFrame wnd) {
602
            JPopupMenu.setDefaultLightWeightPopupEnabled(false);
603
        try {
604
            wnd.moveToFront();
605
            logger.debug("Activando " + wnd.getTitle());
606
            wnd.setSelected(true);
607
            wnd.setIcon(false);
608
        } catch (PropertyVetoException e) {
609
            logger.error(e.toString());
610
        }
611
    }
612

    
613
    /**
614
     * Situa un di�logo modal en el centro de la pantalla
615
     *
616
     * @param d
617
     *            Di�logo que se quiere situar
618
     */
619
    private void centerDialog(JDialog d) {
620
        int offSetX = d.getWidth() / 2;
621
        int offSetY = d.getHeight() / 2;
622

    
623
        d.setLocation((mainFrame.getWidth() / 2) - offSetX, (mainFrame
624
                .getHeight() / 2)
625
                - offSetY);
626
    }
627

    
628
    /**
629
     * DOCUMENT ME!
630
     *
631
     * @param p
632
     */
633
    private void addJDialog(IWindow p) {
634
        JDialog dlg = fws.getJDialog(p);
635

    
636
        centerDialog(dlg);
637

    
638
        dlg.addWindowListener(new DialogWindowListener());
639
        dss.pushDialog(dlg);
640

    
641
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
642
    }
643

    
644
    /*
645
     * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveWindow()
646
     */
647
    public IWindow getActiveWindow() {
648
        JInternalFrame jif = panel.getSelectedFrame();
649

    
650
        if (jif != null) {
651
            IWindow theWindow = fws.getWindow(jif);
652
            if (theWindow == null)
653
                return null;
654
            if (theWindow.getWindowInfo().isPalette())
655
                return wss.getActiveWindow();
656
            else
657
                return fws.getWindow(jif);
658
        }
659
        // return vss.getActiveView();
660

    
661
        return null;
662
    }
663
    public IWindow getFocusWindow(){
664
             JInternalFrame jif = panel.getSelectedFrame();
665

    
666
         if (jif != null) {
667
             IWindow theView = fws.getWindow(jif);
668
             if (theView == null)
669
                 return null;
670
             return fws.getWindow(jif);
671
         }
672
         return null;
673
    }
674
    /*
675
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeWindow(com.iver.andami.ui.mdiManager.IWindow)
676
     */
677
    public void closeWindow(final IWindow p) {
678
                if( !SwingUtilities.isEventDispatchThread() ) {
679
                        SwingUtilities.invokeLater( new Runnable() {
680
                                public void run() {
681
                                        closeWindow(p);
682
                                }
683
                        });
684
                        return;
685
                }
686
        // Si es un di�logo modal
687
        if (p.getWindowInfo().isModal()) {
688
            closeJDialog();
689
        } else { // Si no es modal se cierra el JInternalFrame
690
            closeJInternalFrame(fws.getJInternalFrame(p));
691
        }
692
    }
693

    
694
    /*
695
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllWindows()
696
     */
697
    public void closeAllWindows() {
698
                if( !SwingUtilities.isEventDispatchThread() ) {
699
                        SwingUtilities.invokeLater( new Runnable() {
700
                                public void run() {
701
                                        closeAllWindows();
702
                                }
703
                        });
704
                        return;
705
                }
706
        ArrayList eliminar = new ArrayList();
707
        Iterator i = fws.getWindowIterator();
708

    
709
        while (i.hasNext()) {
710
            eliminar.add((IWindow) i.next());
711
        }
712

    
713
        for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
714
            IWindow vista = (IWindow) iter.next();
715
            closeWindow(vista);
716
        }
717
    }
718

    
719
    /*
720
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
721
     */
722
    public WindowInfo getWindowInfo(IWindow w) {
723
            WindowInfo wi = wis.getWindowInfo(w);
724

    
725
    /*
726
     * This is done now in vis.getWindowInfo(w)
727
     *
728
     * JInternalFrame f = fws.getJInternalFrame(w);
729
            wi.setX(f.getX());
730
            wi.setY(f.getY());
731
            wi.setHeight(f.getHeight());
732
            wi.setWidth(f.getWidth());
733
            // isClosed() doesn't work as (I) expected, why? Using isShowing instead
734
            wi.setClosed(!f.isShowing());
735
            wi.setNormalBounds(f.getNormalBounds());
736
            wi.setMaximized(f.isMaximum());*/
737
            return wi;
738
    }
739

    
740
    /**
741
     * DOCUMENT ME!
742
     *
743
     * @param dialog
744
     * @throws RuntimeException
745
     *             DOCUMENT ME!
746
     */
747
    private void closeJDialog() {
748
        JDialog dlg = dss.popDialog();
749
        if (dlg==null)
750
                return;
751
        dlg.setVisible(false);
752

    
753
        IWindow s = fws.getWindow(dlg);
754

    
755
        callWindowClosed(s);
756

    
757
        fws.closeWindow(s);
758

    
759
        // Si es singleton se desasocia el modelo con la vista
760
        if (s instanceof SingletonWindow) {
761
            sws.closeWindow((SingletonWindow) s);
762
        }
763
    }
764

    
765
    /**
766
     * If <code>window</code> implements IWindowListener, sent it
767
     * the windowActivated event.
768
     *
769
     * @param window
770
     *            The IWindow which has to be notified.
771
     */
772
    private void callWindowClosed(IWindow window) {
773
        if (window instanceof IWindowListener) {
774
            ((IWindowListener) window).windowClosed();
775
        }
776
    }
777

    
778
    /**
779
     * If <code>window</code> implements IWindowListener, sent it
780
     * the windowActivated event.
781
     *
782
     * @param window
783
     *            The IWindow which has to be notified.
784
     */
785
    private void callWindowActivated(IWindow window) {
786
//        logger.debug("View '" + window.getWindowInfo().getTitle()
787
//               + "' activated (callViewActivated)");
788
        if (window instanceof IWindowListener) {
789
            ((IWindowListener) window).windowActivated();
790
        }
791
    }
792

    
793
    /**
794
     * DOCUMENT ME!
795
     *
796
     * @param frame
797
     */
798
    private void closeJInternalFrame(final JInternalFrame frame) {
799
                if( !SwingUtilities.isEventDispatchThread() ) {
800
                        SwingUtilities.invokeLater( new Runnable() {
801
                                public void run() {
802
                                        closeJInternalFrame(frame);
803
                                }
804
                        });
805
                        return;
806
                }
807
        try {
808
            IWindow s = (IWindow) fws.getWindow(frame);
809

    
810
            frame.setClosed(true);
811
            callWindowClosed(s);
812
        } catch (PropertyVetoException e) {
813
            logger
814
                    .error(
815
                            "Not compatible with property veto's. Use ViewInfo instead.",
816
                            e);
817
        }
818
    }
819

    
820
    /*
821
     * @see com.iver.andami.plugins.IExtension#initialize()
822
     */
823
    public void initialize() {
824
    }
825

    
826
    /*
827
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
828
     */
829
    public void execute(String actionCommand) {
830
        if ("window-cascade".equalsIgnoreCase(actionCommand)) {
831
                logger.info("action window-cascade not implemented.");
832

    
833
        } else if ("window-tile".equalsIgnoreCase(actionCommand)) {
834
                logger.info("action window-tile not implemented.");
835
        }
836
    }
837

    
838
    /*
839
     * @see com.iver.andami.plugins.IExtension#isEnabled()
840
     */
841
    public boolean isEnabled() {
842
        // TODO Auto-generated method stub
843
        return false;
844
    }
845

    
846
    /*
847
     * @see com.iver.andami.plugins.IExtension#isVisible()
848
     */
849
    public boolean isVisible() {
850
        // TODO Auto-generated method stub
851
        return true;
852
    }
853

    
854
    /*
855
     * @see com.iver.andami.ui.mdiManager.MDIManager#setWaitCursor()
856
     */
857
    public void setWaitCursor() {
858
        if (mainFrame != null) {
859
                    if( !SwingUtilities.isEventDispatchThread() ) {
860
                            SwingUtilities.invokeLater( new Runnable() {
861
                                    public void run() {
862
                                            setWaitCursor();
863
                                    }
864
                            });
865
                            return;
866
                    }
867
            glassPane.setVisible(true);
868
            lastCursor = mainFrame.getCursor();
869
            dss.setWaitCursor();
870
            glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
871
        }
872
    }
873

    
874
    /*
875
     * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
876
     */
877
    public void restoreCursor() {
878
        if (mainFrame != null) {
879
                    if( !SwingUtilities.isEventDispatchThread() ) {
880
                            SwingUtilities.invokeLater( new Runnable() {
881
                                    public void run() {
882
                                            restoreCursor();
883
                                    }
884
                            });
885
                            return;
886
                    }
887
            glassPane.setVisible(false);
888
            dss.restoreCursor();
889
            glassPane.setCursor(lastCursor);
890
        }
891
    }
892

    
893
    /**
894
     * Listener para los eventos de cerrado de los di�logos. Tiene su raz�n de
895
     * ser en que los di�logos han de devolverse al pool cuando se cierran
896
     *
897
     * @author Fernando Gonz�lez Cort�s
898
     */
899
    public class DialogWindowListener extends WindowAdapter {
900
        /**
901
         * Captura el evento de cerrado de los di�logos con el fin de realizar
902
         * tareas de mantenimiento
903
         *
904
         * @param e
905
         *            evento
906
         */
907
        public void windowActivated(WindowEvent e) {
908
                IWindow window = fws.getWindow((Component) e.getSource());
909
                callWindowActivated(window);
910

    
911
        }
912

    
913

    
914
        /**
915
         * Captura el evento de cerrado de los di�logos con el fin de realizar
916
         * tareas de mantenimiento
917
         *
918
         * @param e
919
         *            evento
920
         */
921
        public void windowClosing(WindowEvent e) {
922
            closeJDialog();
923
        }
924
    }
925

    
926
    /**
927
     * DOCUMENT ME!
928
     */
929
    public class FrameListener implements InternalFrameListener {
930
        /*
931
         * @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
932
         */
933
        public void internalFrameActivated(InternalFrameEvent e) {
934
            // logger.debug("internalFrameActivated " +
935
            // e.getInternalFrame().getTitle());
936

    
937
            // activatedInternalFramesStack.push(e.getInternalFrame());
938

    
939
            IWindow panel = fws.getWindow((JInternalFrame) e.getSource());
940

    
941
            WindowInfo wi = wis.getWindowInfo(panel);
942
            if (wi.isPalette())
943
                return;
944

    
945
            wss.setActive(panel);
946

    
947
            JInternalFrame frame = fws.getJInternalFrame(panel);
948

    
949
            if (wi.isMaximizable()){
950
                    frame.setMaximizable(true);
951
            }
952
            if (!frame.isMaximizable() && frame.isMaximum()) {
953
                try {
954
                    frame.setMaximum(false);
955
                } catch (PropertyVetoException e1) {
956
                }
957
            }
958
            mainFrame.enableControls();
959
            if (wi.getSelectedTools()==null) {
960
                    // this is the first time this window is activated
961
                    wi.setSelectedTools(new HashMap(mainFrame.getInitialSelectedTools()));
962
            }
963
            mainFrame.setSelectedTools(wi.getSelectedTools());
964
            callWindowActivated(panel);
965

    
966
        }
967

    
968
        /*
969
         * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
970
         */
971
        public void internalFrameClosed(InternalFrameEvent e) {
972
        }
973

    
974
        /*
975
         * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
976
         */
977
        public void internalFrameClosing(InternalFrameEvent e) {
978
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
979
            // logger.debug("internalFrameClosing " +
980
            // e.getInternalFrame().getTitle());
981

    
982
            JInternalFrame c = (JInternalFrame) e.getSource();
983
            WindowInfo wi = wis.getWindowInfo((IWindow) fws.getWindow(c));
984

    
985
            IWindow win = fws.getWindow(c);
986
            callWindowClosed(win);
987
            boolean alwaysLive;
988
            if (win instanceof SingletonWindow) {
989
                sws.closeWindow((SingletonWindow) win);
990
            }
991

    
992
            fws.closeWindow(win);
993

    
994
            panel.remove(c);
995

    
996
            wss.remove(win);
997

    
998
            if (!wi.isPalette())
999
                mainFrame.enableControls();
1000
            panel.repaint();
1001

    
1002
            // Para activar el JInternalFrame desde la que hemos
1003
            // abierto la ventana que estamos cerrando
1004
            IWindow lastWindow = wss.getActiveWindow();
1005
            // La activamos
1006
            if (lastWindow != null) {
1007
                    logger.debug(PluginServices.getText(this, "Devuelvo_el_foco_a_")+lastWindow.getWindowInfo().getTitle());
1008
                JInternalFrame frame = fws.getJInternalFrame(lastWindow);
1009
                try {
1010
                    frame.setSelected(true);
1011
                } catch (PropertyVetoException e1) {
1012
                    // TODO Auto-generated catch block
1013
                    // e1.printStackTrace();
1014
                }
1015
                // addView(lastView);
1016
            }
1017

    
1018
        }
1019

    
1020
        /*
1021
         * @see javax.swing.event.InternalFrameListener#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)
1022
         */
1023
        public void internalFrameDeactivated(InternalFrameEvent e) {
1024
            // logger.debug("internalDeActivated " +
1025
            // e.getInternalFrame().getTitle());
1026
            JInternalFrame c = (JInternalFrame) e.getSource();
1027
            IWindow win = fws.getWindow(c);
1028
            if (win != null) {
1029
                WindowInfo wi = wis.getWindowInfo(win);
1030
                if (wi.isPalette())
1031
                    return;
1032

    
1033
            }
1034

    
1035
        }
1036

    
1037
        /*
1038
         * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
1039
         */
1040
        public void internalFrameDeiconified(InternalFrameEvent e) {
1041
            mainFrame.enableControls();
1042
        }
1043

    
1044
        /*
1045
         * @see javax.swing.event.InternalFrameListener#internalFrameIconified(javax.swing.event.InternalFrameEvent)
1046
         */
1047
        public void internalFrameIconified(InternalFrameEvent e) {
1048
            mainFrame.enableControls();
1049
        }
1050

    
1051
        /*
1052
         * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
1053
         */
1054
        public void internalFrameOpened(InternalFrameEvent e) {
1055
            // logger.debug("internalFrameOpened. Source= " +
1056
            // e.getSource().toString());
1057
        }
1058
    }
1059

    
1060
    /*
1061
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
1062
     *      java.lang.Object)
1063
     */
1064
    public boolean closeSingletonWindow(Class viewClass, Object model) {
1065
        JInternalFrame frame = (JInternalFrame) sws.getFrame(viewClass, model);
1066
        if (frame == null)
1067
            return false;
1068
        closeJInternalFrame(frame);
1069
        return true;
1070
    }
1071

    
1072
    /*
1073
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
1074
     */
1075
    public boolean closeSingletonWindow(Object model) {
1076
        JInternalFrame[] frames = (JInternalFrame[]) sws.getFrames(model);
1077
        if (frames.length == 0)
1078
            return false;
1079
        for (int i = 0; i < frames.length; i++) {
1080
            closeJInternalFrame(frames[i]);
1081
        }
1082
        return true;
1083
    }
1084

    
1085
    /*
1086
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
1087
     */
1088
    public IWindow[] getAllWindows() {
1089
        if( fws == null ) {
1090
            return null;
1091
        }
1092
        ArrayList windows = new ArrayList();
1093
        Iterator i = fws.getWindowIterator();
1094

    
1095
        while (i.hasNext()) {
1096
            windows.add((IWindow) i.next());
1097
        }
1098
        return (IWindow[]) windows.toArray(new IWindow[0]);
1099
    }
1100

    
1101
    /*
1102
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
1103
     */
1104
    public IWindow[] getOrderedWindows() {
1105
        TreeMap windows = new TreeMap();
1106
        Iterator winIterator = fws.getWindowIterator();
1107

    
1108
        Component frame;
1109
        IWindow win;
1110
        /**
1111
         * The order of the window in the JDesktopPane. Smaller numbers
1112
         * are closer to the foreground.
1113
         */
1114
        int zPosition;
1115
        while (winIterator.hasNext()) {
1116
                win = (IWindow) winIterator.next();
1117
                frame = fws.getFrame(win);
1118
                    zPosition = panel.getPosition(frame);
1119
                    
1120
                    if (zPosition == -1) {
1121
                        /*
1122
                         * This is a minimized window.
1123
                         * It will keep the -1 (first position) if it does not have the focus.
1124
                         * (I think this never happens. Even if visually the minimized window
1125
                         * appears selected, it does not have the focus), so minimized
1126
                         * windows will lose the first position.
1127
                         */
1128
                        if (!frame.isFocusOwner()) {
1129
                            zPosition = 1000;
1130
                        }
1131
                    }
1132
                    int layer = panel.getLayer(frame);
1133

    
1134
                    if (! (frame instanceof JDialog)) { //JDialogs are not in inside the LayeredPane
1135
                            // flatten all the layers
1136
                            if (layer == JLayeredPane.DEFAULT_LAYER.intValue()) {
1137
                                    zPosition+=50000;
1138
                            }
1139
                            else if (layer == JLayeredPane.PALETTE_LAYER.intValue()) {
1140
                                    zPosition+=40000;
1141
                            }
1142
                            else if (layer == JLayeredPane.MODAL_LAYER.intValue()) {
1143
                                    zPosition+=30000;
1144
                            }
1145
                            else if  (layer == JLayeredPane.POPUP_LAYER.intValue()) {
1146
                                    zPosition+=20000;
1147
                            }
1148
                            else if  (layer == JLayeredPane.DRAG_LAYER.intValue()) {
1149
                                    zPosition+=10000;
1150
                            }
1151
                    }
1152
                    windows.put(new Integer(zPosition), win);
1153
        }
1154
        winIterator = windows.values().iterator();
1155
        ArrayList winList = new ArrayList();
1156
        while (winIterator.hasNext()) {
1157
                winList.add(winIterator.next());
1158
        }
1159

    
1160
        return (IWindow[]) winList.toArray(new IWindow[0]);
1161
    }
1162
    public void setMaximum(final IWindow v, final boolean bMaximum) throws PropertyVetoException
1163
    {
1164
                if( !SwingUtilities.isEventDispatchThread() ) {
1165
                        SwingUtilities.invokeLater( new Runnable() {
1166
                                public void run() {
1167
                                        try {
1168
                                                setMaximum(v,bMaximum);
1169
                                        } catch (PropertyVetoException e) {
1170
                                                logger.info("Error not in event dispatch thread",e);
1171
                                        }
1172
                                }
1173
                        });
1174
                        return;
1175
                }
1176
        JInternalFrame f = fws.getJInternalFrame(v);
1177
        f.setMaximum(bMaximum);
1178
    }
1179

    
1180
    public void changeWindowInfo(IWindow w, WindowInfo wi){
1181
            JInternalFrame f = fws.getJInternalFrame(w);
1182
            f.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
1183
            JDesktopPane pnl = f.getDesktopPane();
1184
            pnl.remove(f);
1185
            int width;
1186
            int height;
1187
            if (wi.getWidth()!=-1)
1188
                    width = wi.getWidth();
1189
            else
1190
                    width = f.getWidth();
1191
            if (wi.getHeight()!=-1)
1192
                    height = wi.getHeight();
1193
            else
1194
                    height = f.getHeight();
1195
            f.setSize(new Dimension(width, height));
1196
            f.setLocation(wi.getX(), wi.getY());
1197
            if (wi.isPalette()) {
1198
                    pnl.add(f, JDesktopPane.PALETTE_LAYER);
1199
                    f.setFocusable(false);
1200
            } else {
1201
                    pnl.add(f, JDesktopPane.DEFAULT_LAYER);
1202
                    f.setFocusable(true);
1203
                    if (wi.isClosed()) {
1204
                            closeWindow(w);
1205
                    }
1206
            }
1207

    
1208
            if (wi.isMaximized()) {
1209
                    try {
1210
                            f.setMaximum(true);
1211
                    } catch (PropertyVetoException e) {
1212
                            // TODO Auto-generated catch block
1213
                            //e.printStackTrace();
1214
                    }
1215
                    f.setNormalBounds(wi.getNormalBounds());
1216
            }
1217
            activateJInternalFrame(f);
1218
    }
1219

    
1220
    public void refresh(final IWindow win) {
1221
                if( !SwingUtilities.isEventDispatchThread() ) {
1222
                        SwingUtilities.invokeLater( new Runnable() {
1223
                                public void run() {
1224
                                        refresh(win);
1225
                                }
1226
                        });
1227
                        return;
1228
                }
1229
            Component frame = fws.getFrame(win);
1230
            if (frame!=null)
1231
                    frame.setVisible(true);
1232
    }
1233

    
1234
        public void setBackgroundImage(ImageIcon image, String typeDesktop) {
1235
                this.image=image;
1236
                this.typeDesktop=typeDesktop;
1237

    
1238
        }
1239
        class MyDesktopPane extends JDesktopPane
1240
            {
1241
                  protected Map listeners = new Hashtable();
1242
                
1243
                
1244
                  public MyDesktopPane(){
1245
              }
1246
                  
1247
                  public Dimension getPreferredSize() {
1248
                                JInternalFrame[] array = this.getAllFrames();
1249
                                int maxX = 0;
1250
                                int maxY = 0;
1251
                                for (int i = 0; i < array.length; i++) {
1252
                                        int x = array[i].getX() + array[i].getWidth();
1253
                                        if (x > maxX)
1254
                                                maxX = x;
1255
                                        int y = array[i].getY() + array[i].getHeight();
1256
                                        if (y > maxY)
1257
                                                maxY = y;
1258
                                }
1259
                                return new Dimension(maxX, maxY);
1260
                        }
1261

    
1262
                        /**
1263
                         * Add an internal-frame to the desktop. Sets a component-listener on
1264
                         * it, which resizes the desktop if a frame is resized.
1265
                         */
1266
                  public Component add(Component comp) {
1267
                                super.add(comp);
1268
                                ComponentListener listener = new ComponentListener() {
1269
                                        public void componentResized(ComponentEvent e) { // Layout the
1270
                                                // JScrollPane
1271
                                                getParent().getParent().validate();
1272
                                        }
1273

    
1274
                                        public void componentMoved(ComponentEvent e) {
1275
                                            
1276
                                            if (e.getSource() instanceof JComponent) {
1277
                                JComponent comp = (JComponent) e.getSource();
1278
                                
1279
                                //no deja que la ventana se vaya hacia la izquierda o arriba
1280
                                if( comp.getX()<0 ) {
1281
                                    comp.setLocation(0, comp.getY());
1282
                                }
1283
                                if (comp.getY()<0){
1284
                                    comp.setLocation(comp.getX(), 0);
1285
                                }
1286
                                componentResized(e);
1287
                                            }
1288
                                        }
1289
                                        public void componentShown(ComponentEvent e) {
1290
                                        }
1291

    
1292
                                        public void componentHidden(ComponentEvent e) {
1293
                                        }
1294

    
1295
                                };
1296
                                comp.addComponentListener(listener);
1297
                                listeners.put(comp, listener);
1298
                                return comp;
1299
                        }
1300

    
1301
                        /**
1302
                         * Remove an internal-frame from the desktop. Removes the
1303
                         * component-listener and resizes the desktop.
1304
                         */
1305
                        public void remove(Component comp) {
1306
                                comp.removeComponentListener((ComponentListener) listeners
1307
                                                .get(comp));
1308
                                super.remove(comp);
1309
                                getParent().getParent().validate(); // Layout the JScrollPane
1310
                        }
1311

    
1312
                 public void paintComponent(Graphics g){
1313
                super.paintComponent(g);
1314
                int x=0;
1315
                int y=0;
1316
                int w=0;
1317
                int h=0;
1318
                if (image != null) {
1319
                                if (typeDesktop.equals(Theme.CENTERED)) {
1320
                                        w = image.getIconWidth();
1321
                                        h = image.getIconHeight();
1322
                                        x = (getWidth() - w) / 2;
1323
                                        y = (getHeight() - h) / 2;
1324
                                        g.drawImage(image.getImage(), x, y, w, h, this);
1325
                                } else if (typeDesktop.equals(Theme.EXPAND)) {
1326
                                        w = getWidth();
1327
                                        h = getHeight();
1328
                                        g.drawImage(image.getImage(), x, y, w, h, this);
1329
                                } else if (typeDesktop.equals(Theme.MOSAIC)) {
1330
                                        int wAux = image.getIconWidth();
1331
                                        int hAux = image.getIconHeight();
1332
                                        int i = 0;
1333
                                        int j = 0;
1334
                                        w = wAux;
1335
                                        h = hAux;
1336
                                        while (x < getWidth()) {
1337
                                                x = wAux * i;
1338
                                                while (y < getHeight()) {
1339
                                                        y = hAux * j;
1340
                                                        j++;
1341
                                                        g.drawImage(image.getImage(), x, y, w, h, this);
1342
                                                }
1343
                                                y = 0;
1344
                                                j = 0;
1345
                                                i++;
1346
                                        }
1347
                                }
1348
                        }
1349
              }
1350
            }
1351

    
1352
    public void showWindow(final JPanel panel, final String title, final MODE mode) {
1353
        // This prepare a default JPanel that implements IWindow 
1354
        // for the passed panel and call to the addWindow
1355
        // to show it.
1356
                if( !SwingUtilities.isEventDispatchThread() ) {
1357
                        SwingUtilities.invokeLater( new Runnable() {
1358
                                public void run() {
1359
                                        showWindow(panel,title,mode);
1360
                                }
1361
                        });
1362
                        return;
1363
                }
1364
        ToolsSwingLocator.getWindowManager().showWindow(panel, title, mode);
1365
    }
1366
    
1367
    
1368
    private Point getLocationForAlignment(IWindow panel, int mode) {
1369
        
1370
        // The top-left square of frame reference
1371
        Point newReferencePoint = new Point();
1372

    
1373
        // A reference to the panel where the JInternalFrame will be displayed
1374
        Container contentPane = ((JFrame)PluginServices.getMainFrame()).getContentPane();
1375

    
1376
        // Get the NewStatusBar component
1377
        NewStatusBar newStatusBar = ((NewStatusBar)contentPane.getComponent(1));
1378

    
1379
        JDesktopPane dpane = this.getDesktopPane();
1380
        // The last substraction is for case when there is any menu,... at left
1381
        int visibleWidth = contentPane.getWidth() - contentPane.getX();
1382
        // The last substraction is for case when there is any menu,... at top
1383
        int visibleHeight = contentPane.getHeight() - newStatusBar.getHeight() - contentPane.getY() - Math.abs(dpane.getY() - contentPane.getY());
1384

    
1385
//        ---------------------------------------------------------------
1386
//        |FIRST_LINE_START(23)   PAGE_START(19)     FIRST_LINE_END(24) |
1387
//        |                                                             |
1388
//        |                                                             |
1389
//        |LINE_START(21)           CENTER(10)              LINE_END(22)|
1390
//        |                                                             |
1391
//        |                                                             |
1392
//        |LAST_LINE_START(25)     PAGE_END(20)       LAST_LINE_END(26) |
1393
//        ---------------------------------------------------------------
1394
        
1395
        int win_h = panel.getWindowInfo().getHeight();
1396
        int win_w = panel.getWindowInfo().getWidth();
1397
        
1398
        switch (mode) {
1399
        case ALIGN_FIRST_LINE_START:
1400
            newReferencePoint.x = DefaultXMargin;
1401
            newReferencePoint.y = DefaultYMargin;
1402
            break;
1403
        
1404
        case ALIGN_PAGE_START:
1405
            newReferencePoint.x = (visibleWidth / 2) - (win_w / 2);
1406
            newReferencePoint.y = DefaultYMargin;
1407
            break;
1408
        
1409
        case ALIGN_FIRST_LINE_END:
1410
            newReferencePoint.x = visibleWidth - win_w - DefaultXMargin;
1411
            newReferencePoint.y = DefaultYMargin;
1412
            break;
1413
        
1414
        case ALIGN_FIRST_LINE_END_CASCADE:
1415
            newReferencePoint.x = visibleWidth - win_w - (int)(DefaultXMargin + (DefaultXMargin*1.5*this.alignCounter));
1416
            newReferencePoint.y = DefaultYMargin + (int)(DefaultYMargin*1.5*this.alignCounter);
1417
            if( ++this.alignCounter >5 ) {
1418
                this.alignCounter = 0;
1419
            }
1420
            break;
1421
        
1422
        case ALIGN_LINE_START:
1423
            newReferencePoint.x = DefaultXMargin;
1424
            newReferencePoint.y = (visibleHeight / 2) - (win_h / 2);
1425
            break;
1426
        
1427
        case ALIGN_LINE_END:
1428
            newReferencePoint.x = visibleWidth - win_w - DefaultXMargin;
1429
            newReferencePoint.y = (visibleHeight / 2) - (win_h / 2);
1430
            break;
1431
        
1432
        case ALIGN_LAST_LINE_START:
1433
            newReferencePoint.x = DefaultXMargin;
1434
            newReferencePoint.y = visibleHeight - win_h - DefaultYMargin;
1435
            break;
1436
        
1437
        case ALIGN_PAGE_END:
1438
            newReferencePoint.x = (visibleWidth / 2) - (win_w / 2);
1439
            newReferencePoint.y = visibleHeight - win_h - DefaultYMargin;
1440
            break;
1441
        
1442
        case ALIGN_LAST_LINE_END:
1443
            newReferencePoint.x = visibleWidth - win_w - DefaultXMargin;
1444
            newReferencePoint.y = visibleHeight - win_h - DefaultYMargin;
1445
            break;
1446
        
1447
        default:
1448
        case ALIGN_CENTER:
1449
            newReferencePoint.x = (visibleWidth / 2) - (win_w / 2);
1450
            newReferencePoint.y = (visibleHeight / 2) - (win_h / 2);
1451
            break;
1452
        }
1453
        
1454
        if( newReferencePoint.x < 0 ) {
1455
            newReferencePoint.x = DefaultXMargin;
1456
        }
1457
        if( newReferencePoint.y < 0 ) {
1458
            newReferencePoint.y = DefaultYMargin;
1459
        }
1460
        
1461
        return newReferencePoint;
1462
    }
1463

    
1464
    public void setLocale(Locale locale) {
1465
        IWindow[] win = this.getAllWindows();
1466
        if( win == null ) {
1467
            return;
1468
        }
1469
        for( int i=0; i<win.length; i++ ) {
1470
            if( win[i] instanceof Component ) {
1471
                try {
1472
                    ((Component)win[i]).setLocale(locale);
1473
                } catch(Exception ex) {
1474
                    // Ignore errors and try others windows.
1475
                }
1476
            }
1477
        }
1478
    }
1479

    
1480
    
1481
}