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 @ 42775

History | View | Annotate | Download (47.9 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.coreplugin.mdiManager;
24

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

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

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

    
87
/**
88
 *
89
 */
90
public class NewSkin extends Extension implements MDIManager {
91

    
92
    private static final int DefaultXMargin = 20; // Added for the method 'centreJInternalFrame'
93
    private static final int DefaultYMargin = 20; // Added for the method 'centreJInternalFrame'
94
    private static final int MinimumXMargin = 130; // Added for the method 'centreJInternalFrame'
95
    private static final int MinimumYMargin = 60; // Added for the method 'centreJInternalFrame'
96

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

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

    
109
    /**
110
     * Panel de la MDIFrame
111
     */
112
    private JDesktopPane panel = new MDIDesktopPane();
113

    
114
    /**
115
     * MDIFrame
116
     */
117
    private MDIFrame mainFrame;
118

    
119
    private GlassPane glassPane = new GlassPane();
120

    
121
    private DialogStackSupport dss;
122

    
123
    /**
124
     * Associates JInternalFrames with the IWindow they contain
125
     */
126
    private FrameWindowSupport fws;
127

    
128
    private WindowInfoSupport wis;
129

    
130
    private WindowStackSupport wss;
131

    
132
    private SingletonWindowSupport sws;
133

    
134
    private Cursor lastCursor = null;
135
    private ImageIcon image;
136
    private String typeDesktop;
137

    
138
    private int alignCounter = 1;
139

    
140
    //Anyade barras de scroll
141
    private void addScrolledDesktopPanel(JFrame parent, JDesktopPane desktopPane) {
142
        JPanel toppanel;
143

    
144
        toppanel = new JPanel();
145
        toppanel.setLayout(new BorderLayout());
146
        toppanel.setPreferredSize(new Dimension(200, 200));
147
        toppanel.setBackground(Color.RED);
148

    
149
        JScrollPane scrollPanel = new JScrollPane(desktopPane);
150

    
151
        toppanel.add(scrollPanel, BorderLayout.CENTER);
152

    
153
        parent.getContentPane().add(toppanel, BorderLayout.CENTER);
154
    }
155

    
156
    public JDesktopPane getDesktopPane() {
157
        return this.panel;
158
    }
159

    
160
    /*
161
     * @see com.iver.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
162
     */
163
    public void init(MDIFrame f) {
164

    
165
        // Inicializa el Frame y la consola
166
        mainFrame = f;
167
        mainFrame.setGlassPane(glassPane);
168
        panel.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
169

    
170
        //Anyade barras de scroll
171
        //mainFrame.getContentPane().add(panel, BorderLayout.CENTER);
172
        addScrolledDesktopPanel(mainFrame, panel);
173

    
174
        panel.setDesktopManager(desktopManager);
175

    
176
        fws = createFrameWindowSupport(mainFrame);
177
        dss = new DialogStackSupport(mainFrame);
178
        sws = new SingletonWindowSupport(wis, fws);
179
        wis = new WindowInfoSupport(mainFrame, fws, sws);
180
        fws.setVis(wis);
181
        wss = new WindowStackSupport(wis);
182

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

    
188
        PluginServices.registerKeyStroke(controlTab, new KeyEventDispatcher() {
189

    
190
            public boolean dispatchKeyEvent(KeyEvent e) {
191
                IWindow[] views = getAllWindows();
192
                if (views.length <= 0 || e.getID() == KeyEvent.KEY_PRESSED) {
193
                    return false;
194
                }
195

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

    
207
        });
208
    }
209

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

    
220
    /* (non-javadoc)
221
     * @see com.iver.andami.ui.mdiManager.MDIManager#addWindow(com.iver.andami.ui.mdiManager.IWindow)
222
     */
223
    public IWindow addWindow(final IWindow p) throws SingletonDialogAlreadyShownException {
224

    
225
        if (!SwingUtilities.isEventDispatchThread()) {
226
            RunnableWithParameters action = new RunnableWithParameters() {
227
                public void run() {
228
                    this.returnValue = addWindow(p);
229
                }
230
            };
231
            try {
232
                SwingUtilities.invokeAndWait(action);
233
            } catch (Exception e) {
234
                logger.info("Can't add window from othrer thread that EventDispatch", e);
235
                IllegalThreadStateException e2 = new IllegalThreadStateException();
236
                e2.initCause(e);
237
                throw e2;
238
            }
239
            return (IWindow) action.getReturnValue();
240
        }
241

    
242
        // se obtiene la informaci�n de la vista
243
        WindowInfo wi = wis.getWindowInfo(p);
244

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

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

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

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

    
303
            return p;
304
        }
305
    }
306

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

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

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

    
330
        if (p instanceof SingletonWindow) {
331
            SingletonWindow sw = (SingletonWindow) p;
332
            if (sws.registerWindow(sw.getClass(), sw.getWindowModel(), wi)) {
333
                singletonPreviouslyAdded = true;
334
            }
335
        }
336

    
337
        if (!singletonPreviouslyAdded) {
338
            Point new_loc = this.getLocationForAlignment(p, align);
339
            p.getWindowInfo().setX(new_loc.x);
340
            p.getWindowInfo().setY(new_loc.y);
341
        }
342

    
343
        IWindow window = addWindow(p);
344
        return window;
345
    }
346

    
347
    @Override
348
    public void move(final IWindow panel, final int x, final int y) {
349
        if (!SwingUtilities.isEventDispatchThread()) {
350
            SwingUtilities.invokeLater(new Runnable() {
351
                @Override
352
                public void run() {
353
                    move(panel, x, y);
354
                }
355
            });
356
            return;
357
        }
358
        Component window = fws.getFrame(panel);
359
        if (window == null) {
360
            return;
361
        }
362
        Point p = new Point();
363
        p.setLocation(x, y);
364
        window.setLocation(p);
365
    }
366
    
367
    /**
368
     * Centres the Frame in the contentPane of the MainFrame. If the frame can't
369
     * be showed completely, it tries to show its top-left corner.
370
     *
371
     * @author Pablo Piqueras Bartolom�
372
     *
373
     * @param panel The IWindow to centre
374
     */
375
    public synchronized void centreFrame(final IWindow panel) {
376
        if (!SwingUtilities.isEventDispatchThread()) {
377
            SwingUtilities.invokeLater(new Runnable() {
378
                public void run() {
379
                    centreFrame(panel);
380
                }
381
            });
382
            return;
383
        }
384
        Component window = fws.getFrame(panel);
385
        if (window == null) {
386
            return;
387
        }
388

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

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

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

    
399
        int visibleWidth = contentPane.getWidth() - contentPane.getX(); // The last substraction is for if there is any menu,... at left
400
        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
401
        int freeWidth = visibleWidth - window.getWidth();
402
        int freeHeight = visibleHeight - window.getHeight();
403

    
404
        // Calculate the new point reference (Assure that the top-left corner is showed)
405
        if (freeWidth < 0) {
406
            if (visibleWidth > MinimumXMargin) {
407
                newReferencePoint.x = DefaultXMargin;
408
            } else {
409
                newReferencePoint.x = 0;
410
            }
411
        } else {
412
            newReferencePoint.x = freeWidth / 2;
413
        }
414

    
415
        if (freeHeight < 0) {
416
            if (visibleHeight > MinimumYMargin) {
417
                newReferencePoint.y = DefaultYMargin;
418
            } else {
419
                newReferencePoint.y = 0;
420
            }
421
        } else {
422
            newReferencePoint.y = freeHeight / 2;
423
        }
424

    
425
        // Set the new location for this JInternalFrame
426
        window.setLocation(newReferencePoint);
427
    }
428

    
429
    public synchronized void alignFrame(final IWindow panel, final int mode) {
430
        if (!SwingUtilities.isEventDispatchThread()) {
431
            SwingUtilities.invokeLater(new Runnable() {
432
                public void run() {
433
                    alignFrame(panel, mode);
434
                }
435
            });
436
            return;
437
        }
438
        Component window = fws.getFrame(panel);
439
        if (window == null) {
440
            return;
441
        }
442

    
443
        // The top-left square of frame reference
444
        Point newReferencePoint = new Point();
445

    
446
        // A reference to the panel where the JInternalFrame will be displayed
447
        Container contentPane = ((JFrame) PluginServices.getMainFrame()).getContentPane();
448

    
449
        // Get the NewStatusBar component
450
        NewStatusBar newStatusBar = ((NewStatusBar) contentPane.getComponent(1));
451
        JDesktopPane jDesktopPane = this.getDesktopPane();
452

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

    
456
//        ---------------------------------------------------------------
457
//        |FIRST_LINE_START(23)   PAGE_START(19)     FIRST_LINE_END(24) |
458
//        |                                                             |
459
//        |                                                             |
460
//        |LINE_START(21)           CENTER(10)              LINE_END(22)|
461
//        |                                                             |
462
//        |                                                             |
463
//        |LAST_LINE_START(25)     PAGE_END(20)       LAST_LINE_END(26) |
464
//        ---------------------------------------------------------------
465
        switch (mode) {
466
            case ALIGN_FIRST_LINE_START:
467
                newReferencePoint.x = DefaultXMargin;
468
                newReferencePoint.y = DefaultYMargin;
469
                break;
470

    
471
            case ALIGN_PAGE_START:
472
                newReferencePoint.x = visibleWidth / 2;
473
                newReferencePoint.y = DefaultYMargin;
474
                break;
475

    
476
            case ALIGN_FIRST_LINE_END:
477
                newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
478
                newReferencePoint.y = DefaultYMargin;
479
                break;
480

    
481
            case ALIGN_FIRST_LINE_END_CASCADE:
482
                newReferencePoint.x = visibleWidth - window.getWidth() - (int) (DefaultXMargin + (DefaultXMargin * 1.5 * this.alignCounter));
483
                newReferencePoint.y = DefaultYMargin + (int) (DefaultYMargin * 1.5 * this.alignCounter);
484
                if (++this.alignCounter > 5) {
485
                    this.alignCounter = 0;
486
                }
487
                break;
488

    
489
            case ALIGN_LINE_START:
490
                newReferencePoint.x = DefaultXMargin;
491
                newReferencePoint.y = visibleHeight / 2;
492
                break;
493

    
494
            case ALIGN_LINE_END:
495
                newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
496
                newReferencePoint.y = visibleHeight / 2;
497
                break;
498

    
499
            case ALIGN_LAST_LINE_START:
500
                newReferencePoint.x = DefaultXMargin;
501
                newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
502
                break;
503

    
504
            case ALIGN_PAGE_END:
505
                newReferencePoint.x = visibleWidth / 2;
506
                newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
507
                break;
508

    
509
            case ALIGN_LAST_LINE_END:
510
                newReferencePoint.x = visibleWidth - window.getWidth() - DefaultXMargin;
511
                newReferencePoint.y = visibleHeight - window.getHeight() - DefaultYMargin;
512
                break;
513

    
514
            default:
515
            case ALIGN_CENTER:
516
                newReferencePoint.x = visibleWidth / 2;
517
                newReferencePoint.y = visibleHeight / 2;
518
                break;
519
        }
520

    
521
        if (newReferencePoint.x < 0) {
522
            newReferencePoint.x = DefaultXMargin;
523
        }
524
        if (newReferencePoint.y < 0) {
525
            newReferencePoint.y = DefaultYMargin;
526
        }
527
        window.setLocation(newReferencePoint);
528
    }
529

    
530
    /**
531
     * DOCUMENT ME!
532
     *
533
     * @param wnd DOCUMENT ME!
534
     * @param wi DOCUMENT ME!
535
     */
536
    private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
537
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
538
        wnd.addInternalFrameListener(new FrameListener());
539

    
540
        if (wi.isModeless() || wi.isPalette()) {
541
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
542
            if (wi.isPalette()) {
543
                wnd.setFocusable(false);
544
            }
545
        } else {
546
            panel.add(wnd);
547
        }
548
        updateFrameProperties(wnd, wi);
549
        activateJInternalFrame(wnd);
550
        try {
551
            wnd.setMaximum(wi.isMaximized());
552
        } catch (Exception ex) {
553
            String title = "unknow";
554
            try {
555
                title = wi.getTitle();
556
            } catch (Throwable ex2) {
557
                // Ignore error.
558
            }
559
            logger.warn("Can't maximize window (" + title + ").", ex);
560
        }
561
    }
562

    
563
    private void updateFrameProperties(JInternalFrame frame, WindowInfo wi) {
564
        int height, width;
565
        if (wi.isMaximized()) {
566
            if (wi.getNormalWidth() != -1) {
567
                width = wi.getNormalWidth();
568
            } else {
569
                width = frame.getNormalBounds().width;
570
            }
571
            if (wi.getNormalHeight() != -1) {
572
                height = wi.getNormalHeight();
573
            } else {
574
                height = frame.getNormalBounds().height;
575
            }
576

    
577
            frame.setSize(width, height);
578
            frame.setLocation(wi.getNormalX(), wi.getNormalY());
579
        } else {
580
            if (wi.getWidth() != -1) {
581
                width = wi.getWidth();
582
            } else {
583
                width = frame.getWidth();
584
            }
585
            if (wi.getHeight() != -1) {
586
                height = wi.getHeight();
587
            } else {
588
                height = frame.getHeight();
589
            }
590
            frame.setSize(width, height);
591
            frame.setLocation(wi.getX(), wi.getY());
592
        }
593
        frame.setTitle(wi.getTitle());
594
        frame.setVisible(wi.isVisible());
595
        frame.setResizable(wi.isResizable());
596
        frame.setIconifiable(wi.isIconifiable());
597
        frame.setMaximizable(wi.isMaximizable());
598
        try {
599
            frame.setMaximum(wi.isMaximized());
600
        } catch (PropertyVetoException e) {
601
                        // TODO Auto-generated catch block
602
            //e.printStackTrace();
603
        }
604
    }
605

    
606
    /**
607
     * DOCUMENT ME!
608
     *
609
     * @param p
610
     */
611
    private void addJInternalFrame(IWindow p) {
612
        WindowInfo wi = wis.getWindowInfo(p);
613

    
614
        JInternalFrame wnd = fws.getJInternalFrame(p);
615

    
616
        if (p instanceof SingletonWindow) {
617
            SingletonWindow sv = (SingletonWindow) p;
618
            sws.openSingletonWindow(sv, wnd);
619
        }
620

    
621
        addJInternalFrame(wnd, wi);
622
    }
623

    
624
    /**
625
     * DOCUMENT ME!
626
     *
627
     * @param wnd
628
     */
629
    private void activateJInternalFrame(JInternalFrame wnd) {
630
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
631
        try {
632
            wnd.moveToFront();
633
            logger.debug("Activando " + wnd.getTitle());
634
            wnd.setSelected(true);
635
            wnd.setIcon(false);
636
        } catch (PropertyVetoException e) {
637
            logger.error(e.toString());
638
        }
639
    }
640

    
641
    @Override
642
    public void moveToFrom(IWindow win) {
643
        Component frame = fws.getFrame(win);
644
        if( frame instanceof JInternalFrame ) {
645
            activateJInternalFrame((JInternalFrame)frame);
646
        }
647
    }
648

    
649
    /**
650
     * Situa un di�logo modal en el centro de la pantalla
651
     *
652
     * @param d Di�logo que se quiere situar
653
     */
654
    private void centerDialog(JDialog d) {
655
        int offSetX = d.getWidth() / 2;
656
        int offSetY = d.getHeight() / 2;
657
        Point o = mainFrame.getLocation();
658
        int x = o.x + (mainFrame.getWidth() / 2) - offSetX;
659
        int y = o.y + (mainFrame.getHeight() / 2) - offSetY;
660
        if( x<0 ) {
661
            x = 0;
662
        }
663
        if( y<0 ) {
664
            y=0;
665
        }
666
        d.setLocation(x,y);
667
    }
668

    
669
    /**
670
     * DOCUMENT ME!
671
     *
672
     * @param p
673
     */
674
    private void addJDialog(final IWindow p) {
675
        JDialog dlg = fws.getJDialog(p);
676
        dlg.addWindowListener(new DialogWindowListener());
677
        centerDialog(dlg);
678
        dss.pushDialog(dlg);
679

    
680
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
681
    }
682

    
683
    /*
684
     * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveWindow()
685
     */
686
    public IWindow getActiveWindow() {
687
        JInternalFrame jif = panel.getSelectedFrame();
688

    
689
        if (jif != null) {
690
            IWindow theWindow = fws.getWindow(jif);
691
            if (theWindow == null) {
692
                return null;
693
            }
694
            if (theWindow.getWindowInfo().isPalette()) {
695
                return wss.getActiveWindow();
696
            } else {
697
                return fws.getWindow(jif);
698
            }
699
        }
700
        // return vss.getActiveView();
701

    
702
        return null;
703
    }
704

    
705
    public IWindow getFocusWindow() {
706
        JInternalFrame jif = panel.getSelectedFrame();
707

    
708
        if (jif != null) {
709
            IWindow theView = fws.getWindow(jif);
710
            if (theView == null) {
711
                return null;
712
            }
713
            return fws.getWindow(jif);
714
        }
715
        return null;
716
    }
717
    /*
718
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeWindow(com.iver.andami.ui.mdiManager.IWindow)
719
     */
720

    
721
    public void closeWindow(final IWindow p) {
722
        if (!SwingUtilities.isEventDispatchThread()) {
723
            SwingUtilities.invokeLater(new Runnable() {
724
                public void run() {
725
                    closeWindow(p);
726
                }
727
            });
728
            return;
729
        }
730
        // Si es un di�logo modal
731
        if (p.getWindowInfo().isModal()) {
732
            closeJDialog();
733
        } else { // Si no es modal se cierra el JInternalFrame
734
            closeJInternalFrame(fws.getJInternalFrame(p));
735
        }
736
    }
737

    
738
    /*
739
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllWindows()
740
     */
741
    public void closeAllWindows() {
742
        if (!SwingUtilities.isEventDispatchThread()) {
743
            SwingUtilities.invokeLater(new Runnable() {
744
                public void run() {
745
                    closeAllWindows();
746
                }
747
            });
748
            return;
749
        }
750
        ArrayList eliminar = new ArrayList();
751
        Iterator i = fws.getWindowIterator();
752

    
753
        while (i.hasNext()) {
754
            eliminar.add((IWindow) i.next());
755
        }
756

    
757
        for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
758
            IWindow vista = (IWindow) iter.next();
759
            closeWindow(vista);
760
        }
761
    }
762

    
763
    /*
764
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
765
     */
766
    public WindowInfo getWindowInfo(IWindow w) {
767
        WindowInfo wi = wis.getWindowInfo(w);
768

    
769
        /*
770
         * This is done now in vis.getWindowInfo(w)
771
         *
772
         * JInternalFrame f = fws.getJInternalFrame(w);
773
         wi.setX(f.getX());
774
         wi.setY(f.getY());
775
         wi.setHeight(f.getHeight());
776
         wi.setWidth(f.getWidth());
777
         // isClosed() doesn't work as (I) expected, why? Using isShowing instead
778
         wi.setClosed(!f.isShowing());
779
         wi.setNormalBounds(f.getNormalBounds());
780
         wi.setMaximized(f.isMaximum());*/
781
        return wi;
782
    }
783

    
784
    /**
785
     * DOCUMENT ME!
786
     *
787
     * @param dialog
788
     * @throws RuntimeException DOCUMENT ME!
789
     */
790
    private void closeJDialog() {
791
        JDialog dlg = dss.popDialog();
792
        if (dlg == null) {
793
            return;
794
        }
795
        dlg.setVisible(false);
796

    
797
        IWindow s = fws.getWindow(dlg);
798

    
799
        callWindowClosed(s);
800

    
801
        fws.closeWindow(s);
802

    
803
        // Si es singleton se desasocia el modelo con la vista
804
        if (s instanceof SingletonWindow) {
805
            sws.closeWindow((SingletonWindow) s);
806
        }
807
    }
808

    
809
    /**
810
     * If <code>window</code> implements IWindowListener, sent it the
811
     * windowActivated event.
812
     *
813
     * @param window The IWindow which has to be notified.
814
     */
815
    private void callWindowClosed(IWindow window) {
816
        if (window instanceof IWindowListener) {
817
            ((IWindowListener) window).windowClosed();
818
        }
819
    }
820

    
821
    /**
822
     * If <code>window</code> implements IWindowListener, sent it the
823
     * windowActivated event.
824
     *
825
     * @param window The IWindow which has to be notified.
826
     */
827
    private void callWindowActivated(IWindow window) {
828
//        logger.debug("View '" + window.getWindowInfo().getTitle()
829
//               + "' activated (callViewActivated)");
830
        if (window instanceof IWindowListener) {
831
            ((IWindowListener) window).windowActivated();
832
        }
833
    }
834

    
835
    /**
836
     * DOCUMENT ME!
837
     *
838
     * @param frame
839
     */
840
    private void closeJInternalFrame(final JInternalFrame frame) {
841
        if (!SwingUtilities.isEventDispatchThread()) {
842
            SwingUtilities.invokeLater(new Runnable() {
843
                public void run() {
844
                    closeJInternalFrame(frame);
845
                }
846
            });
847
            return;
848
        }
849
        try {
850
            IWindow s = (IWindow) fws.getWindow(frame);
851

    
852
            frame.setClosed(true);
853
            callWindowClosed(s);
854
        } catch (PropertyVetoException e) {
855
            logger
856
                    .error(
857
                            "Not compatible with property veto's. Use ViewInfo instead.",
858
                            e);
859
        }
860
    }
861

    
862
    /*
863
     * @see com.iver.andami.plugins.IExtension#initialize()
864
     */
865
    public void initialize() {
866
    }
867

    
868
    /*
869
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
870
     */
871
    public void execute(String actionCommand) {
872
        if ("window-cascade".equalsIgnoreCase(actionCommand)) {
873
            logger.info("action window-cascade not implemented.");
874

    
875
        } else if ("window-tile".equalsIgnoreCase(actionCommand)) {
876
            logger.info("action window-tile not implemented.");
877
        }
878
    }
879

    
880
    /*
881
     * @see com.iver.andami.plugins.IExtension#isEnabled()
882
     */
883
    public boolean isEnabled() {
884
        // TODO Auto-generated method stub
885
        return false;
886
    }
887

    
888
    /*
889
     * @see com.iver.andami.plugins.IExtension#isVisible()
890
     */
891
    public boolean isVisible() {
892
        // TODO Auto-generated method stub
893
        return true;
894
    }
895

    
896
    /*
897
     * @see com.iver.andami.ui.mdiManager.MDIManager#setWaitCursor()
898
     */
899
    public void setWaitCursor() {
900
        if (mainFrame != null) {
901
            if (!SwingUtilities.isEventDispatchThread()) {
902
                SwingUtilities.invokeLater(new Runnable() {
903
                    public void run() {
904
                        setWaitCursor();
905
                    }
906
                });
907
                return;
908
            }
909
            glassPane.setVisible(true);
910
            lastCursor = mainFrame.getCursor();
911
            dss.setWaitCursor();
912
            glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
913
        }
914
    }
915

    
916
    /*
917
     * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
918
     */
919
    public void restoreCursor() {
920
        if (mainFrame != null) {
921
            if (!SwingUtilities.isEventDispatchThread()) {
922
                SwingUtilities.invokeLater(new Runnable() {
923
                    public void run() {
924
                        restoreCursor();
925
                    }
926
                });
927
                return;
928
            }
929
            glassPane.setVisible(false);
930
            dss.restoreCursor();
931
            glassPane.setCursor(lastCursor);
932
        }
933
    }
934

    
935
    /**
936
     * Listener para los eventos de cerrado de los di�logos. Tiene su raz�n
937
     * de ser en que los di�logos han de devolverse al pool cuando se cierran
938
     *
939
     * @author Fernando Gonz�lez Cort�s
940
     */
941
    public class DialogWindowListener extends WindowAdapter {
942

    
943
        /**
944
         * Captura el evento de cerrado de los di�logos con el fin de realizar
945
         * tareas de mantenimiento
946
         *
947
         * @param e evento
948
         */
949
        public void windowActivated(WindowEvent e) {
950
            IWindow window = fws.getWindow((Component) e.getSource());
951
            callWindowActivated(window);
952

    
953
        }
954

    
955
        /**
956
         * Captura el evento de cerrado de los di�logos con el fin de realizar
957
         * tareas de mantenimiento
958
         *
959
         * @param e evento
960
         */
961
        public void windowClosing(WindowEvent e) {
962
            closeJDialog();
963
        }
964
    }
965

    
966
    /**
967
     * DOCUMENT ME!
968
     */
969
    public class FrameListener implements InternalFrameListener {
970
        /*
971
         * @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
972
         */
973

    
974
        public void internalFrameActivated(InternalFrameEvent e) {
975
            // logger.debug("internalFrameActivated " +
976
            // e.getInternalFrame().getTitle());
977

    
978
            // activatedInternalFramesStack.push(e.getInternalFrame());
979
            IWindow panel = fws.getWindow((JInternalFrame) e.getSource());
980

    
981
            WindowInfo wi = wis.getWindowInfo(panel);
982
            if (wi.isPalette()) {
983
                return;
984
            }
985

    
986
            wss.setActive(panel);
987

    
988
            JInternalFrame frame = fws.getJInternalFrame(panel);
989

    
990
            if (wi.isMaximizable()) {
991
                frame.setMaximizable(true);
992
            }
993
            if (!frame.isMaximizable() && frame.isMaximum()) {
994
                try {
995
                    frame.setMaximum(false);
996
                } catch (PropertyVetoException e1) {
997
                }
998
            }
999
            mainFrame.enableControls();
1000
            if (wi.getSelectedTools() == null) {
1001
                // this is the first time this window is activated
1002
                wi.setSelectedTools(new HashMap(mainFrame.getInitialSelectedTools()));
1003
            }
1004
            mainFrame.setSelectedTools(wi.getSelectedTools());
1005
            callWindowActivated(panel);
1006

    
1007
        }
1008

    
1009
        /*
1010
         * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
1011
         */
1012
        public void internalFrameClosed(InternalFrameEvent e) {
1013
        }
1014

    
1015
        /*
1016
         * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
1017
         */
1018
        public void internalFrameClosing(InternalFrameEvent e) {
1019
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
1020
            // logger.debug("internalFrameClosing " +
1021
            // e.getInternalFrame().getTitle());
1022

    
1023
            JInternalFrame c = (JInternalFrame) e.getSource();
1024
            try {
1025
                ToolsWindowManager.Window wwin = (ToolsWindowManager.Window) c.getContentPane().getComponent(0);
1026
                wwin.fireClosingWindow();
1027
            } catch (Throwable ex) {
1028

    
1029
            }
1030

    
1031
            WindowInfo wi = wis.getWindowInfo((IWindow) fws.getWindow(c));
1032

    
1033
            IWindow win = fws.getWindow(c);
1034
            callWindowClosed(win);
1035
//            boolean alwaysLive;
1036
            if (win instanceof SingletonWindow) {
1037
                sws.closeWindow((SingletonWindow) win);
1038
            }
1039

    
1040
            fws.closeWindow(win);
1041

    
1042
            panel.remove(c);
1043

    
1044
            wss.remove(win);
1045

    
1046
            if (!wi.isPalette()) {
1047
                mainFrame.enableControls();
1048
            }
1049
            panel.repaint();
1050

    
1051
            // Para activar el JInternalFrame desde la que hemos
1052
            // abierto la ventana que estamos cerrando
1053
            IWindow lastWindow = wss.getActiveWindow();
1054
            // La activamos
1055
            if (lastWindow != null) {
1056
                logger.debug(PluginServices.getText(this, "Devuelvo_el_foco_a_") + lastWindow.getWindowInfo().getTitle());
1057
                JInternalFrame frame = fws.getJInternalFrame(lastWindow);
1058
                try {
1059
                    frame.setSelected(true);
1060
                } catch (PropertyVetoException e1) {
1061
                    // TODO Auto-generated catch block
1062
                    // e1.printStackTrace();
1063
                }
1064
                // addView(lastView);
1065
            }
1066

    
1067
        }
1068

    
1069
        /*
1070
         * @see javax.swing.event.InternalFrameListener#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)
1071
         */
1072
        public void internalFrameDeactivated(InternalFrameEvent e) {
1073
            // logger.debug("internalDeActivated " +
1074
            // e.getInternalFrame().getTitle());
1075
            JInternalFrame c = (JInternalFrame) e.getSource();
1076
            IWindow win = fws.getWindow(c);
1077
            if (win != null) {
1078
                WindowInfo wi = wis.getWindowInfo(win);
1079
                if (wi.isPalette()) {
1080
                    return;
1081
                }
1082

    
1083
            }
1084

    
1085
        }
1086

    
1087
        /*
1088
         * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
1089
         */
1090
        public void internalFrameDeiconified(InternalFrameEvent e) {
1091
            mainFrame.enableControls();
1092
        }
1093

    
1094
        /*
1095
         * @see javax.swing.event.InternalFrameListener#internalFrameIconified(javax.swing.event.InternalFrameEvent)
1096
         */
1097
        public void internalFrameIconified(InternalFrameEvent e) {
1098
            mainFrame.enableControls();
1099
        }
1100

    
1101
        /*
1102
         * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
1103
         */
1104
        public void internalFrameOpened(InternalFrameEvent e) {
1105
            // logger.debug("internalFrameOpened. Source= " +
1106
            // e.getSource().toString());
1107
        }
1108
    }
1109

    
1110
    /*
1111
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
1112
     *      java.lang.Object)
1113
     */
1114
    public boolean closeSingletonWindow(Class viewClass, Object model) {
1115
        JInternalFrame frame = (JInternalFrame) sws.getFrame(viewClass, model);
1116
        if (frame == null) {
1117
            return false;
1118
        }
1119
        closeJInternalFrame(frame);
1120
        return true;
1121
    }
1122

    
1123
    /*
1124
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
1125
     */
1126
    public boolean closeSingletonWindow(Object model) {
1127
        JInternalFrame[] frames = (JInternalFrame[]) sws.getFrames(model);
1128
        if (frames.length == 0) {
1129
            return false;
1130
        }
1131
        for (int i = 0; i < frames.length; i++) {
1132
            closeJInternalFrame(frames[i]);
1133
        }
1134
        return true;
1135
    }
1136

    
1137
    /*
1138
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
1139
     */
1140
    public IWindow[] getAllWindows() {
1141
        if (fws == null) {
1142
            return null;
1143
        }
1144
        ArrayList windows = new ArrayList();
1145
        Iterator i = fws.getWindowIterator();
1146

    
1147
        while (i.hasNext()) {
1148
            windows.add((IWindow) i.next());
1149
        }
1150
        return (IWindow[]) windows.toArray(new IWindow[0]);
1151
    }
1152

    
1153
    /*
1154
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
1155
     */
1156
    public IWindow[] getOrderedWindows() {
1157
        TreeMap windows = new TreeMap();
1158
        Iterator winIterator = fws.getWindowIterator();
1159

    
1160
        Component frame;
1161
        IWindow win;
1162
        /**
1163
         * The order of the window in the JDesktopPane. Smaller numbers are
1164
         * closer to the foreground.
1165
         */
1166
        int zPosition;
1167
        while (winIterator.hasNext()) {
1168
            win = (IWindow) winIterator.next();
1169
            frame = fws.getFrame(win);
1170
            zPosition = panel.getPosition(frame);
1171

    
1172
            if (zPosition == -1) {
1173
                /*
1174
                 * This is a minimized window.
1175
                 * It will keep the -1 (first position) if it does not have the focus.
1176
                 * (I think this never happens. Even if visually the minimized window
1177
                 * appears selected, it does not have the focus), so minimized
1178
                 * windows will lose the first position.
1179
                 */
1180
                if (!frame.isFocusOwner()) {
1181
                    zPosition = 1000;
1182
                }
1183
            }
1184
            int layer = panel.getLayer(frame);
1185

    
1186
            if (!(frame instanceof JDialog)) { //JDialogs are not in inside the LayeredPane
1187
                // flatten all the layers
1188
                if (layer == JLayeredPane.DEFAULT_LAYER.intValue()) {
1189
                    zPosition += 50000;
1190
                } else if (layer == JLayeredPane.PALETTE_LAYER.intValue()) {
1191
                    zPosition += 40000;
1192
                } else if (layer == JLayeredPane.MODAL_LAYER.intValue()) {
1193
                    zPosition += 30000;
1194
                } else if (layer == JLayeredPane.POPUP_LAYER.intValue()) {
1195
                    zPosition += 20000;
1196
                } else if (layer == JLayeredPane.DRAG_LAYER.intValue()) {
1197
                    zPosition += 10000;
1198
                }
1199
            }
1200
            windows.put(new Integer(zPosition), win);
1201
        }
1202
        winIterator = windows.values().iterator();
1203
        ArrayList winList = new ArrayList();
1204
        while (winIterator.hasNext()) {
1205
            winList.add(winIterator.next());
1206
        }
1207

    
1208
        return (IWindow[]) winList.toArray(new IWindow[0]);
1209
    }
1210

    
1211
    public void setMaximum(final IWindow v, final boolean bMaximum) throws PropertyVetoException {
1212
        if (!SwingUtilities.isEventDispatchThread()) {
1213
            SwingUtilities.invokeLater(new Runnable() {
1214
                public void run() {
1215
                    try {
1216
                        setMaximum(v, bMaximum);
1217
                    } catch (PropertyVetoException e) {
1218
                        logger.info("Error not in event dispatch thread", e);
1219
                    }
1220
                }
1221
            });
1222
            return;
1223
        }
1224
        JInternalFrame f = fws.getJInternalFrame(v);
1225
        f.setMaximum(bMaximum);
1226
    }
1227

    
1228
    public void changeWindowInfo(IWindow w, WindowInfo wi) {
1229
        JInternalFrame f = fws.getJInternalFrame(w);
1230
        f.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
1231
        JDesktopPane pnl = f.getDesktopPane();
1232
        pnl.remove(f);
1233
        int width;
1234
        int height;
1235
        if (wi.getWidth() != -1) {
1236
            width = wi.getWidth();
1237
        } else {
1238
            width = f.getWidth();
1239
        }
1240
        if (wi.getHeight() != -1) {
1241
            height = wi.getHeight();
1242
        } else {
1243
            height = f.getHeight();
1244
        }
1245
        f.setSize(new Dimension(width, height));
1246
        f.setLocation(wi.getX(), wi.getY());
1247
        if (wi.isPalette()) {
1248
            pnl.add(f, JDesktopPane.PALETTE_LAYER);
1249
            f.setFocusable(false);
1250
        } else {
1251
            pnl.add(f, JDesktopPane.DEFAULT_LAYER);
1252
            f.setFocusable(true);
1253
            if (wi.isClosed()) {
1254
                closeWindow(w);
1255
            }
1256
        }
1257

    
1258
        if (wi.isMaximized()) {
1259
            try {
1260
                f.setMaximum(true);
1261
            } catch (PropertyVetoException e) {
1262
                            // TODO Auto-generated catch block
1263
                //e.printStackTrace();
1264
            }
1265
            f.setNormalBounds(wi.getNormalBounds());
1266
        }
1267
        activateJInternalFrame(f);
1268
    }
1269

    
1270
    public void refresh(final IWindow win) {
1271
        if (!SwingUtilities.isEventDispatchThread()) {
1272
            SwingUtilities.invokeLater(new Runnable() {
1273
                public void run() {
1274
                    refresh(win);
1275
                }
1276
            });
1277
            return;
1278
        }
1279
        Component frame = fws.getFrame(win);
1280
        if (frame != null) {
1281
            frame.setVisible(true);
1282
        }
1283
    }
1284

    
1285
    public void setBackgroundImage(ImageIcon image, String typeDesktop) {
1286
        this.image = image;
1287
        this.typeDesktop = typeDesktop;
1288

    
1289
    }
1290

    
1291
    public void showWindow(final JPanel panel, final String title, final MODE mode) {
1292
        // This prepare a default JPanel that implements IWindow
1293
        // for the passed panel and call to the addWindow
1294
        // to show it.
1295
        if (!SwingUtilities.isEventDispatchThread()) {
1296
            SwingUtilities.invokeLater(new Runnable() {
1297
                public void run() {
1298
                    showWindow(panel, title, mode);
1299
                }
1300
            });
1301
            return;
1302
        }
1303
        ToolsSwingLocator.getWindowManager().showWindow(panel, title, mode);
1304
    }
1305

    
1306
    public IWindow getWindow(JPanel panel){
1307
        Iterator<IWindow> i = fws.getWindowIterator();
1308

    
1309
        while (i.hasNext()) {
1310
            IWindow window = (IWindow) i.next();
1311
            if(window instanceof JPanel){
1312
                JPanel p = (JPanel)window;
1313
                if(p.getComponentCount()==1){
1314
                    if(p.getComponent(0) == panel){
1315
                        return window;
1316
                    }
1317
                }
1318
            }
1319
        }
1320
        return null;
1321
    }
1322

    
1323
    private Point getLocationForAlignment(IWindow panel, int mode) {
1324

    
1325
        // The top-left square of frame reference
1326
        Point newReferencePoint = new Point();
1327

    
1328
        // A reference to the panel where the JInternalFrame will be displayed
1329
        Container contentPane = ((JFrame) PluginServices.getMainFrame()).getContentPane();
1330

    
1331
        // Get the NewStatusBar component
1332
        NewStatusBar newStatusBar = ((NewStatusBar) contentPane.getComponent(1));
1333

    
1334
        JDesktopPane dpane = this.getDesktopPane();
1335
        // The last substraction is for case when there is any menu,... at left
1336
        int visibleWidth = contentPane.getWidth() - contentPane.getX();
1337
        // The last substraction is for case when there is any menu,... at top
1338
        int visibleHeight = contentPane.getHeight() - newStatusBar.getHeight() - contentPane.getY() - Math.abs(dpane.getY() - contentPane.getY());
1339

    
1340
//        ---------------------------------------------------------------
1341
//        |FIRST_LINE_START(23)   PAGE_START(19)     FIRST_LINE_END(24) |
1342
//        |                                                             |
1343
//        |                                                             |
1344
//        |LINE_START(21)           CENTER(10)              LINE_END(22)|
1345
//        |                                                             |
1346
//        |                                                             |
1347
//        |LAST_LINE_START(25)     PAGE_END(20)       LAST_LINE_END(26) |
1348
//        ---------------------------------------------------------------
1349
        int win_h = panel.getWindowInfo().getHeight();
1350
        int win_w = panel.getWindowInfo().getWidth();
1351

    
1352
        switch (mode) {
1353
            case ALIGN_FIRST_LINE_START:
1354
                newReferencePoint.x = DefaultXMargin;
1355
                newReferencePoint.y = DefaultYMargin;
1356
                break;
1357

    
1358
            case ALIGN_PAGE_START:
1359
                newReferencePoint.x = (visibleWidth / 2) - (win_w / 2);
1360
                newReferencePoint.y = DefaultYMargin;
1361
                break;
1362

    
1363
            case ALIGN_FIRST_LINE_END:
1364
                newReferencePoint.x = visibleWidth - win_w - DefaultXMargin;
1365
                newReferencePoint.y = DefaultYMargin;
1366
                break;
1367

    
1368
            case ALIGN_FIRST_LINE_END_CASCADE:
1369
                newReferencePoint.x = visibleWidth - win_w - (int) (DefaultXMargin + (DefaultXMargin * 1.5 * this.alignCounter));
1370
                newReferencePoint.y = DefaultYMargin + (int) (DefaultYMargin * 1.5 * this.alignCounter);
1371
                if (++this.alignCounter > 5) {
1372
                    this.alignCounter = 0;
1373
                }
1374
                break;
1375

    
1376
            case ALIGN_LINE_START:
1377
                newReferencePoint.x = DefaultXMargin;
1378
                newReferencePoint.y = (visibleHeight / 2) - (win_h / 2);
1379
                break;
1380

    
1381
            case ALIGN_LINE_END:
1382
                newReferencePoint.x = visibleWidth - win_w - DefaultXMargin;
1383
                newReferencePoint.y = (visibleHeight / 2) - (win_h / 2);
1384
                break;
1385

    
1386
            case ALIGN_LAST_LINE_START:
1387
                newReferencePoint.x = DefaultXMargin;
1388
                newReferencePoint.y = visibleHeight - win_h - DefaultYMargin;
1389
                break;
1390

    
1391
            case ALIGN_PAGE_END:
1392
                newReferencePoint.x = (visibleWidth / 2) - (win_w / 2);
1393
                newReferencePoint.y = visibleHeight - win_h - DefaultYMargin;
1394
                break;
1395

    
1396
            case ALIGN_LAST_LINE_END:
1397
                newReferencePoint.x = visibleWidth - win_w - DefaultXMargin;
1398
                newReferencePoint.y = visibleHeight - win_h - DefaultYMargin;
1399
                break;
1400

    
1401
            default:
1402
            case ALIGN_CENTER:
1403
                newReferencePoint.x = (visibleWidth / 2) - (win_w / 2);
1404
                newReferencePoint.y = (visibleHeight / 2) - (win_h / 2);
1405
                break;
1406
        }
1407

    
1408
        if (newReferencePoint.x < 0) {
1409
            newReferencePoint.x = DefaultXMargin;
1410
        }
1411
        if (newReferencePoint.y < 0) {
1412
            newReferencePoint.y = DefaultYMargin;
1413
        }
1414

    
1415
        return newReferencePoint;
1416
    }
1417

    
1418
    public void setLocale(Locale locale) {
1419
        IWindow[] win = this.getAllWindows();
1420
        if (win == null) {
1421
            return;
1422
        }
1423
        for (int i = 0; i < win.length; i++) {
1424
            if (win[i] instanceof Component) {
1425
                try {
1426
                    ((Component) win[i]).setLocale(locale);
1427
                } catch (Exception ex) {
1428
                    // Ignore errors and try others windows.
1429
                }
1430
            }
1431
        }
1432
    }
1433

    
1434
}