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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
436
//        ---------------------------------------------------------------
437
//        |FIRST_LINE_START(23)   PAGE_START(19)     FIRST_LINE_END(24) |
438
//        |                                                             |
439
//        |                                                             |
440
//        |LINE_START(21)           CENTER(10)              LINE_END(22)|
441
//        |                                                             |
442
//        |                                                             |
443
//        |LAST_LINE_START(25)     PAGE_END(20)       LAST_LINE_END(26) |
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 DOCUMENT ME!
514
     * @param wi DOCUMENT ME!
515
     */
516
    private void addJInternalFrame(JInternalFrame wnd, WindowInfo wi) {
517
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
518
        wnd.addInternalFrameListener(new FrameListener());
519

    
520
        if (wi.isModeless() || wi.isPalette()) {
521
            panel.add(wnd, JDesktopPane.PALETTE_LAYER);
522
            if (wi.isPalette()) {
523
                wnd.setFocusable(false);
524
            }
525
        } else {
526
            panel.add(wnd);
527
        }
528
        updateFrameProperties(wnd, wi);
529
        activateJInternalFrame(wnd);
530
        try {
531
            wnd.setMaximum(wi.isMaximized());
532
        } catch (Exception ex) {
533
            String title = "unknow";
534
            try {
535
                title = wi.getTitle();
536
            } catch (Throwable ex2) {
537
                // Ignore error.
538
            }
539
            logger.warn("Can't maximize window (" + title + ").", ex);
540
        }
541
    }
542

    
543
    private void updateFrameProperties(JInternalFrame frame, WindowInfo wi) {
544
        int height, width;
545
        if (wi.isMaximized()) {
546
            if (wi.getNormalWidth() != -1) {
547
                width = wi.getNormalWidth();
548
            } else {
549
                width = frame.getNormalBounds().width;
550
            }
551
            if (wi.getNormalHeight() != -1) {
552
                height = wi.getNormalHeight();
553
            } else {
554
                height = frame.getNormalBounds().height;
555
            }
556

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

    
586
    /**
587
     * DOCUMENT ME!
588
     *
589
     * @param p
590
     */
591
    private void addJInternalFrame(IWindow p) {
592
        WindowInfo wi = wis.getWindowInfo(p);
593

    
594
        JInternalFrame wnd = fws.getJInternalFrame(p);
595

    
596
        if (p instanceof SingletonWindow) {
597
            SingletonWindow sv = (SingletonWindow) p;
598
            sws.openSingletonWindow(sv, wnd);
599
        }
600

    
601
        addJInternalFrame(wnd, wi);
602
    }
603

    
604
    /**
605
     * DOCUMENT ME!
606
     *
607
     * @param wnd
608
     */
609
    private void activateJInternalFrame(JInternalFrame wnd) {
610
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
611
        try {
612
            wnd.moveToFront();
613
            logger.debug("Activando " + wnd.getTitle());
614
            wnd.setSelected(true);
615
            wnd.setIcon(false);
616
        } catch (PropertyVetoException e) {
617
            logger.error(e.toString());
618
        }
619
    }
620

    
621
    /**
622
     * Situa un di�logo modal en el centro de la pantalla
623
     *
624
     * @param d Di�logo que se quiere situar
625
     */
626
    private void centerDialog(JDialog d) {
627
        int offSetX = d.getWidth() / 2;
628
        int offSetY = d.getHeight() / 2;
629

    
630
        d.setLocation((mainFrame.getWidth() / 2) - offSetX, (mainFrame
631
                .getHeight() / 2)
632
                - offSetY);
633
    }
634

    
635
    /**
636
     * DOCUMENT ME!
637
     *
638
     * @param p
639
     */
640
    private void addJDialog(final IWindow p) {
641
        JDialog dlg = fws.getJDialog(p);
642
        dlg.addComponentListener(new ComponentListener() {
643

    
644
            @Override
645
            public void componentResized(ComponentEvent ce) {
646
            }
647

    
648
            @Override
649
            public void componentMoved(ComponentEvent ce) {
650
            }
651

    
652
            @Override
653
            public void componentShown(ComponentEvent ce) {
654
            }
655

    
656
            @Override
657
            public void componentHidden(ComponentEvent ce) {
658
                closeWindow(p);
659
            }
660
        });
661
        centerDialog(dlg);
662

    
663
        dlg.addWindowListener(new DialogWindowListener());
664
        dss.pushDialog(dlg);
665

    
666
        dlg.setVisible(wis.getWindowInfo(p).isVisible());
667
    }
668

    
669
    /*
670
     * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveWindow()
671
     */
672
    public IWindow getActiveWindow() {
673
        JInternalFrame jif = panel.getSelectedFrame();
674

    
675
        if (jif != null) {
676
            IWindow theWindow = fws.getWindow(jif);
677
            if (theWindow == null) {
678
                return null;
679
            }
680
            if (theWindow.getWindowInfo().isPalette()) {
681
                return wss.getActiveWindow();
682
            } else {
683
                return fws.getWindow(jif);
684
            }
685
        }
686
        // return vss.getActiveView();
687

    
688
        return null;
689
    }
690

    
691
    public IWindow getFocusWindow() {
692
        JInternalFrame jif = panel.getSelectedFrame();
693

    
694
        if (jif != null) {
695
            IWindow theView = fws.getWindow(jif);
696
            if (theView == null) {
697
                return null;
698
            }
699
            return fws.getWindow(jif);
700
        }
701
        return null;
702
    }
703
    /*
704
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeWindow(com.iver.andami.ui.mdiManager.IWindow)
705
     */
706

    
707
    public void closeWindow(final IWindow p) {
708
        if (!SwingUtilities.isEventDispatchThread()) {
709
            SwingUtilities.invokeLater(new Runnable() {
710
                public void run() {
711
                    closeWindow(p);
712
                }
713
            });
714
            return;
715
        }
716
        // Si es un di�logo modal
717
        if (p.getWindowInfo().isModal()) {
718
            closeJDialog();
719
        } else { // Si no es modal se cierra el JInternalFrame
720
            closeJInternalFrame(fws.getJInternalFrame(p));
721
        }
722
    }
723

    
724
    /*
725
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllWindows()
726
     */
727
    public void closeAllWindows() {
728
        if (!SwingUtilities.isEventDispatchThread()) {
729
            SwingUtilities.invokeLater(new Runnable() {
730
                public void run() {
731
                    closeAllWindows();
732
                }
733
            });
734
            return;
735
        }
736
        ArrayList eliminar = new ArrayList();
737
        Iterator i = fws.getWindowIterator();
738

    
739
        while (i.hasNext()) {
740
            eliminar.add((IWindow) i.next());
741
        }
742

    
743
        for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
744
            IWindow vista = (IWindow) iter.next();
745
            closeWindow(vista);
746
        }
747
    }
748

    
749
    /*
750
     * @see com.iver.andami.ui.mdiManager.MDIManager#getWindowInfo(com.iver.andami.ui.mdiManager.IWindow)
751
     */
752
    public WindowInfo getWindowInfo(IWindow w) {
753
        WindowInfo wi = wis.getWindowInfo(w);
754

    
755
        /*
756
         * This is done now in vis.getWindowInfo(w)
757
         *
758
         * JInternalFrame f = fws.getJInternalFrame(w);
759
         wi.setX(f.getX());
760
         wi.setY(f.getY());
761
         wi.setHeight(f.getHeight());
762
         wi.setWidth(f.getWidth());
763
         // isClosed() doesn't work as (I) expected, why? Using isShowing instead
764
         wi.setClosed(!f.isShowing());
765
         wi.setNormalBounds(f.getNormalBounds());
766
         wi.setMaximized(f.isMaximum());*/
767
        return wi;
768
    }
769

    
770
    /**
771
     * DOCUMENT ME!
772
     *
773
     * @param dialog
774
     * @throws RuntimeException DOCUMENT ME!
775
     */
776
    private void closeJDialog() {
777
        JDialog dlg = dss.popDialog();
778
        if (dlg == null) {
779
            return;
780
        }
781
        dlg.setVisible(false);
782

    
783
        IWindow s = fws.getWindow(dlg);
784

    
785
        callWindowClosed(s);
786

    
787
        fws.closeWindow(s);
788

    
789
        // Si es singleton se desasocia el modelo con la vista
790
        if (s instanceof SingletonWindow) {
791
            sws.closeWindow((SingletonWindow) s);
792
        }
793
    }
794

    
795
    /**
796
     * If <code>window</code> implements IWindowListener, sent it the
797
     * windowActivated event.
798
     *
799
     * @param window The IWindow which has to be notified.
800
     */
801
    private void callWindowClosed(IWindow window) {
802
        if (window instanceof IWindowListener) {
803
            ((IWindowListener) window).windowClosed();
804
        }
805
    }
806

    
807
    /**
808
     * If <code>window</code> implements IWindowListener, sent it the
809
     * windowActivated event.
810
     *
811
     * @param window The IWindow which has to be notified.
812
     */
813
    private void callWindowActivated(IWindow window) {
814
//        logger.debug("View '" + window.getWindowInfo().getTitle()
815
//               + "' activated (callViewActivated)");
816
        if (window instanceof IWindowListener) {
817
            ((IWindowListener) window).windowActivated();
818
        }
819
    }
820

    
821
    /**
822
     * DOCUMENT ME!
823
     *
824
     * @param frame
825
     */
826
    private void closeJInternalFrame(final JInternalFrame frame) {
827
        if (!SwingUtilities.isEventDispatchThread()) {
828
            SwingUtilities.invokeLater(new Runnable() {
829
                public void run() {
830
                    closeJInternalFrame(frame);
831
                }
832
            });
833
            return;
834
        }
835
        try {
836
            IWindow s = (IWindow) fws.getWindow(frame);
837

    
838
            frame.setClosed(true);
839
            callWindowClosed(s);
840
        } catch (PropertyVetoException e) {
841
            logger
842
                    .error(
843
                            "Not compatible with property veto's. Use ViewInfo instead.",
844
                            e);
845
        }
846
    }
847

    
848
    /*
849
     * @see com.iver.andami.plugins.IExtension#initialize()
850
     */
851
    public void initialize() {
852
    }
853

    
854
    /*
855
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
856
     */
857
    public void execute(String actionCommand) {
858
        if ("window-cascade".equalsIgnoreCase(actionCommand)) {
859
            logger.info("action window-cascade not implemented.");
860

    
861
        } else if ("window-tile".equalsIgnoreCase(actionCommand)) {
862
            logger.info("action window-tile not implemented.");
863
        }
864
    }
865

    
866
    /*
867
     * @see com.iver.andami.plugins.IExtension#isEnabled()
868
     */
869
    public boolean isEnabled() {
870
        // TODO Auto-generated method stub
871
        return false;
872
    }
873

    
874
    /*
875
     * @see com.iver.andami.plugins.IExtension#isVisible()
876
     */
877
    public boolean isVisible() {
878
        // TODO Auto-generated method stub
879
        return true;
880
    }
881

    
882
    /*
883
     * @see com.iver.andami.ui.mdiManager.MDIManager#setWaitCursor()
884
     */
885
    public void setWaitCursor() {
886
        if (mainFrame != null) {
887
            if (!SwingUtilities.isEventDispatchThread()) {
888
                SwingUtilities.invokeLater(new Runnable() {
889
                    public void run() {
890
                        setWaitCursor();
891
                    }
892
                });
893
                return;
894
            }
895
            glassPane.setVisible(true);
896
            lastCursor = mainFrame.getCursor();
897
            dss.setWaitCursor();
898
            glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
899
        }
900
    }
901

    
902
    /*
903
     * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
904
     */
905
    public void restoreCursor() {
906
        if (mainFrame != null) {
907
            if (!SwingUtilities.isEventDispatchThread()) {
908
                SwingUtilities.invokeLater(new Runnable() {
909
                    public void run() {
910
                        restoreCursor();
911
                    }
912
                });
913
                return;
914
            }
915
            glassPane.setVisible(false);
916
            dss.restoreCursor();
917
            glassPane.setCursor(lastCursor);
918
        }
919
    }
920

    
921
    /**
922
     * Listener para los eventos de cerrado de los di�logos. Tiene su raz�n
923
     * de ser en que los di�logos han de devolverse al pool cuando se cierran
924
     *
925
     * @author Fernando Gonz�lez Cort�s
926
     */
927
    public class DialogWindowListener extends WindowAdapter {
928

    
929
        /**
930
         * Captura el evento de cerrado de los di�logos con el fin de realizar
931
         * tareas de mantenimiento
932
         *
933
         * @param e evento
934
         */
935
        public void windowActivated(WindowEvent e) {
936
            IWindow window = fws.getWindow((Component) e.getSource());
937
            callWindowActivated(window);
938

    
939
        }
940

    
941
        /**
942
         * Captura el evento de cerrado de los di�logos con el fin de realizar
943
         * tareas de mantenimiento
944
         *
945
         * @param e evento
946
         */
947
        public void windowClosing(WindowEvent e) {
948
            closeJDialog();
949
        }
950
    }
951

    
952
    /**
953
     * DOCUMENT ME!
954
     */
955
    public class FrameListener implements InternalFrameListener {
956
        /*
957
         * @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
958
         */
959

    
960
        public void internalFrameActivated(InternalFrameEvent e) {
961
            // logger.debug("internalFrameActivated " +
962
            // e.getInternalFrame().getTitle());
963

    
964
            // activatedInternalFramesStack.push(e.getInternalFrame());
965
            IWindow panel = fws.getWindow((JInternalFrame) e.getSource());
966

    
967
            WindowInfo wi = wis.getWindowInfo(panel);
968
            if (wi.isPalette()) {
969
                return;
970
            }
971

    
972
            wss.setActive(panel);
973

    
974
            JInternalFrame frame = fws.getJInternalFrame(panel);
975

    
976
            if (wi.isMaximizable()) {
977
                frame.setMaximizable(true);
978
            }
979
            if (!frame.isMaximizable() && frame.isMaximum()) {
980
                try {
981
                    frame.setMaximum(false);
982
                } catch (PropertyVetoException e1) {
983
                }
984
            }
985
            mainFrame.enableControls();
986
            if (wi.getSelectedTools() == null) {
987
                // this is the first time this window is activated
988
                wi.setSelectedTools(new HashMap(mainFrame.getInitialSelectedTools()));
989
            }
990
            mainFrame.setSelectedTools(wi.getSelectedTools());
991
            callWindowActivated(panel);
992

    
993
        }
994

    
995
        /*
996
         * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
997
         */
998
        public void internalFrameClosed(InternalFrameEvent e) {
999
        }
1000

    
1001
        /*
1002
         * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
1003
         */
1004
        public void internalFrameClosing(InternalFrameEvent e) {
1005
            // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
1006
            // logger.debug("internalFrameClosing " +
1007
            // e.getInternalFrame().getTitle());
1008

    
1009
            JInternalFrame c = (JInternalFrame) e.getSource();
1010
            try {
1011
                ToolsWindowManager.Window wwin = (ToolsWindowManager.Window) c.getContentPane().getComponent(0);
1012
                wwin.fireClosingWindow();
1013
            } catch (Throwable ex) {
1014

    
1015
            }
1016

    
1017
            WindowInfo wi = wis.getWindowInfo((IWindow) fws.getWindow(c));
1018

    
1019
            IWindow win = fws.getWindow(c);
1020
            callWindowClosed(win);
1021
//            boolean alwaysLive;
1022
            if (win instanceof SingletonWindow) {
1023
                sws.closeWindow((SingletonWindow) win);
1024
            }
1025

    
1026
            fws.closeWindow(win);
1027

    
1028
            panel.remove(c);
1029

    
1030
            wss.remove(win);
1031

    
1032
            if (!wi.isPalette()) {
1033
                mainFrame.enableControls();
1034
            }
1035
            panel.repaint();
1036

    
1037
            // Para activar el JInternalFrame desde la que hemos
1038
            // abierto la ventana que estamos cerrando
1039
            IWindow lastWindow = wss.getActiveWindow();
1040
            // La activamos
1041
            if (lastWindow != null) {
1042
                logger.debug(PluginServices.getText(this, "Devuelvo_el_foco_a_") + lastWindow.getWindowInfo().getTitle());
1043
                JInternalFrame frame = fws.getJInternalFrame(lastWindow);
1044
                try {
1045
                    frame.setSelected(true);
1046
                } catch (PropertyVetoException e1) {
1047
                    // TODO Auto-generated catch block
1048
                    // e1.printStackTrace();
1049
                }
1050
                // addView(lastView);
1051
            }
1052

    
1053
        }
1054

    
1055
        /*
1056
         * @see javax.swing.event.InternalFrameListener#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)
1057
         */
1058
        public void internalFrameDeactivated(InternalFrameEvent e) {
1059
            // logger.debug("internalDeActivated " +
1060
            // e.getInternalFrame().getTitle());
1061
            JInternalFrame c = (JInternalFrame) e.getSource();
1062
            IWindow win = fws.getWindow(c);
1063
            if (win != null) {
1064
                WindowInfo wi = wis.getWindowInfo(win);
1065
                if (wi.isPalette()) {
1066
                    return;
1067
                }
1068

    
1069
            }
1070

    
1071
        }
1072

    
1073
        /*
1074
         * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
1075
         */
1076
        public void internalFrameDeiconified(InternalFrameEvent e) {
1077
            mainFrame.enableControls();
1078
        }
1079

    
1080
        /*
1081
         * @see javax.swing.event.InternalFrameListener#internalFrameIconified(javax.swing.event.InternalFrameEvent)
1082
         */
1083
        public void internalFrameIconified(InternalFrameEvent e) {
1084
            mainFrame.enableControls();
1085
        }
1086

    
1087
        /*
1088
         * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
1089
         */
1090
        public void internalFrameOpened(InternalFrameEvent e) {
1091
            // logger.debug("internalFrameOpened. Source= " +
1092
            // e.getSource().toString());
1093
        }
1094
    }
1095

    
1096
    /*
1097
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Class,
1098
     *      java.lang.Object)
1099
     */
1100
    public boolean closeSingletonWindow(Class viewClass, Object model) {
1101
        JInternalFrame frame = (JInternalFrame) sws.getFrame(viewClass, model);
1102
        if (frame == null) {
1103
            return false;
1104
        }
1105
        closeJInternalFrame(frame);
1106
        return true;
1107
    }
1108

    
1109
    /*
1110
     * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonWindow(java.lang.Object)
1111
     */
1112
    public boolean closeSingletonWindow(Object model) {
1113
        JInternalFrame[] frames = (JInternalFrame[]) sws.getFrames(model);
1114
        if (frames.length == 0) {
1115
            return false;
1116
        }
1117
        for (int i = 0; i < frames.length; i++) {
1118
            closeJInternalFrame(frames[i]);
1119
        }
1120
        return true;
1121
    }
1122

    
1123
    /*
1124
     * @see com.iver.andami.ui.mdiManager.MDIManager#getAllWindows()
1125
     */
1126
    public IWindow[] getAllWindows() {
1127
        if (fws == null) {
1128
            return null;
1129
        }
1130
        ArrayList windows = new ArrayList();
1131
        Iterator i = fws.getWindowIterator();
1132

    
1133
        while (i.hasNext()) {
1134
            windows.add((IWindow) i.next());
1135
        }
1136
        return (IWindow[]) windows.toArray(new IWindow[0]);
1137
    }
1138

    
1139
    /*
1140
     * @see com.iver.andami.ui.mdiManager.MDIManager#getOrderedWindows()
1141
     */
1142
    public IWindow[] getOrderedWindows() {
1143
        TreeMap windows = new TreeMap();
1144
        Iterator winIterator = fws.getWindowIterator();
1145

    
1146
        Component frame;
1147
        IWindow win;
1148
        /**
1149
         * The order of the window in the JDesktopPane. Smaller numbers are
1150
         * closer to the foreground.
1151
         */
1152
        int zPosition;
1153
        while (winIterator.hasNext()) {
1154
            win = (IWindow) winIterator.next();
1155
            frame = fws.getFrame(win);
1156
            zPosition = panel.getPosition(frame);
1157

    
1158
            if (zPosition == -1) {
1159
                /*
1160
                 * This is a minimized window.
1161
                 * It will keep the -1 (first position) if it does not have the focus.
1162
                 * (I think this never happens. Even if visually the minimized window
1163
                 * appears selected, it does not have the focus), so minimized
1164
                 * windows will lose the first position.
1165
                 */
1166
                if (!frame.isFocusOwner()) {
1167
                    zPosition = 1000;
1168
                }
1169
            }
1170
            int layer = panel.getLayer(frame);
1171

    
1172
            if (!(frame instanceof JDialog)) { //JDialogs are not in inside the LayeredPane
1173
                // flatten all the layers
1174
                if (layer == JLayeredPane.DEFAULT_LAYER.intValue()) {
1175
                    zPosition += 50000;
1176
                } else if (layer == JLayeredPane.PALETTE_LAYER.intValue()) {
1177
                    zPosition += 40000;
1178
                } else if (layer == JLayeredPane.MODAL_LAYER.intValue()) {
1179
                    zPosition += 30000;
1180
                } else if (layer == JLayeredPane.POPUP_LAYER.intValue()) {
1181
                    zPosition += 20000;
1182
                } else if (layer == JLayeredPane.DRAG_LAYER.intValue()) {
1183
                    zPosition += 10000;
1184
                }
1185
            }
1186
            windows.put(new Integer(zPosition), win);
1187
        }
1188
        winIterator = windows.values().iterator();
1189
        ArrayList winList = new ArrayList();
1190
        while (winIterator.hasNext()) {
1191
            winList.add(winIterator.next());
1192
        }
1193

    
1194
        return (IWindow[]) winList.toArray(new IWindow[0]);
1195
    }
1196

    
1197
    public void setMaximum(final IWindow v, final boolean bMaximum) throws PropertyVetoException {
1198
        if (!SwingUtilities.isEventDispatchThread()) {
1199
            SwingUtilities.invokeLater(new Runnable() {
1200
                public void run() {
1201
                    try {
1202
                        setMaximum(v, bMaximum);
1203
                    } catch (PropertyVetoException e) {
1204
                        logger.info("Error not in event dispatch thread", e);
1205
                    }
1206
                }
1207
            });
1208
            return;
1209
        }
1210
        JInternalFrame f = fws.getJInternalFrame(v);
1211
        f.setMaximum(bMaximum);
1212
    }
1213

    
1214
    public void changeWindowInfo(IWindow w, WindowInfo wi) {
1215
        JInternalFrame f = fws.getJInternalFrame(w);
1216
        f.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
1217
        JDesktopPane pnl = f.getDesktopPane();
1218
        pnl.remove(f);
1219
        int width;
1220
        int height;
1221
        if (wi.getWidth() != -1) {
1222
            width = wi.getWidth();
1223
        } else {
1224
            width = f.getWidth();
1225
        }
1226
        if (wi.getHeight() != -1) {
1227
            height = wi.getHeight();
1228
        } else {
1229
            height = f.getHeight();
1230
        }
1231
        f.setSize(new Dimension(width, height));
1232
        f.setLocation(wi.getX(), wi.getY());
1233
        if (wi.isPalette()) {
1234
            pnl.add(f, JDesktopPane.PALETTE_LAYER);
1235
            f.setFocusable(false);
1236
        } else {
1237
            pnl.add(f, JDesktopPane.DEFAULT_LAYER);
1238
            f.setFocusable(true);
1239
            if (wi.isClosed()) {
1240
                closeWindow(w);
1241
            }
1242
        }
1243

    
1244
        if (wi.isMaximized()) {
1245
            try {
1246
                f.setMaximum(true);
1247
            } catch (PropertyVetoException e) {
1248
                            // TODO Auto-generated catch block
1249
                //e.printStackTrace();
1250
            }
1251
            f.setNormalBounds(wi.getNormalBounds());
1252
        }
1253
        activateJInternalFrame(f);
1254
    }
1255

    
1256
    public void refresh(final IWindow win) {
1257
        if (!SwingUtilities.isEventDispatchThread()) {
1258
            SwingUtilities.invokeLater(new Runnable() {
1259
                public void run() {
1260
                    refresh(win);
1261
                }
1262
            });
1263
            return;
1264
        }
1265
        Component frame = fws.getFrame(win);
1266
        if (frame != null) {
1267
            frame.setVisible(true);
1268
        }
1269
    }
1270

    
1271
    public void setBackgroundImage(ImageIcon image, String typeDesktop) {
1272
        this.image = image;
1273
        this.typeDesktop = typeDesktop;
1274

    
1275
    }
1276

    
1277
    public void showWindow(final JPanel panel, final String title, final MODE mode) {
1278
        // This prepare a default JPanel that implements IWindow 
1279
        // for the passed panel and call to the addWindow
1280
        // to show it.
1281
        if (!SwingUtilities.isEventDispatchThread()) {
1282
            SwingUtilities.invokeLater(new Runnable() {
1283
                public void run() {
1284
                    showWindow(panel, title, mode);
1285
                }
1286
            });
1287
            return;
1288
        }
1289
        ToolsSwingLocator.getWindowManager().showWindow(panel, title, mode);
1290
    }
1291

    
1292
    private Point getLocationForAlignment(IWindow panel, int mode) {
1293

    
1294
        // The top-left square of frame reference
1295
        Point newReferencePoint = new Point();
1296

    
1297
        // A reference to the panel where the JInternalFrame will be displayed
1298
        Container contentPane = ((JFrame) PluginServices.getMainFrame()).getContentPane();
1299

    
1300
        // Get the NewStatusBar component
1301
        NewStatusBar newStatusBar = ((NewStatusBar) contentPane.getComponent(1));
1302

    
1303
        JDesktopPane dpane = this.getDesktopPane();
1304
        // The last substraction is for case when there is any menu,... at left
1305
        int visibleWidth = contentPane.getWidth() - contentPane.getX();
1306
        // The last substraction is for case when there is any menu,... at top
1307
        int visibleHeight = contentPane.getHeight() - newStatusBar.getHeight() - contentPane.getY() - Math.abs(dpane.getY() - contentPane.getY());
1308

    
1309
//        ---------------------------------------------------------------
1310
//        |FIRST_LINE_START(23)   PAGE_START(19)     FIRST_LINE_END(24) |
1311
//        |                                                             |
1312
//        |                                                             |
1313
//        |LINE_START(21)           CENTER(10)              LINE_END(22)|
1314
//        |                                                             |
1315
//        |                                                             |
1316
//        |LAST_LINE_START(25)     PAGE_END(20)       LAST_LINE_END(26) |
1317
//        ---------------------------------------------------------------
1318
        int win_h = panel.getWindowInfo().getHeight();
1319
        int win_w = panel.getWindowInfo().getWidth();
1320

    
1321
        switch (mode) {
1322
            case ALIGN_FIRST_LINE_START:
1323
                newReferencePoint.x = DefaultXMargin;
1324
                newReferencePoint.y = DefaultYMargin;
1325
                break;
1326

    
1327
            case ALIGN_PAGE_START:
1328
                newReferencePoint.x = (visibleWidth / 2) - (win_w / 2);
1329
                newReferencePoint.y = DefaultYMargin;
1330
                break;
1331

    
1332
            case ALIGN_FIRST_LINE_END:
1333
                newReferencePoint.x = visibleWidth - win_w - DefaultXMargin;
1334
                newReferencePoint.y = DefaultYMargin;
1335
                break;
1336

    
1337
            case ALIGN_FIRST_LINE_END_CASCADE:
1338
                newReferencePoint.x = visibleWidth - win_w - (int) (DefaultXMargin + (DefaultXMargin * 1.5 * this.alignCounter));
1339
                newReferencePoint.y = DefaultYMargin + (int) (DefaultYMargin * 1.5 * this.alignCounter);
1340
                if (++this.alignCounter > 5) {
1341
                    this.alignCounter = 0;
1342
                }
1343
                break;
1344

    
1345
            case ALIGN_LINE_START:
1346
                newReferencePoint.x = DefaultXMargin;
1347
                newReferencePoint.y = (visibleHeight / 2) - (win_h / 2);
1348
                break;
1349

    
1350
            case ALIGN_LINE_END:
1351
                newReferencePoint.x = visibleWidth - win_w - DefaultXMargin;
1352
                newReferencePoint.y = (visibleHeight / 2) - (win_h / 2);
1353
                break;
1354

    
1355
            case ALIGN_LAST_LINE_START:
1356
                newReferencePoint.x = DefaultXMargin;
1357
                newReferencePoint.y = visibleHeight - win_h - DefaultYMargin;
1358
                break;
1359

    
1360
            case ALIGN_PAGE_END:
1361
                newReferencePoint.x = (visibleWidth / 2) - (win_w / 2);
1362
                newReferencePoint.y = visibleHeight - win_h - DefaultYMargin;
1363
                break;
1364

    
1365
            case ALIGN_LAST_LINE_END:
1366
                newReferencePoint.x = visibleWidth - win_w - DefaultXMargin;
1367
                newReferencePoint.y = visibleHeight - win_h - DefaultYMargin;
1368
                break;
1369

    
1370
            default:
1371
            case ALIGN_CENTER:
1372
                newReferencePoint.x = (visibleWidth / 2) - (win_w / 2);
1373
                newReferencePoint.y = (visibleHeight / 2) - (win_h / 2);
1374
                break;
1375
        }
1376

    
1377
        if (newReferencePoint.x < 0) {
1378
            newReferencePoint.x = DefaultXMargin;
1379
        }
1380
        if (newReferencePoint.y < 0) {
1381
            newReferencePoint.y = DefaultYMargin;
1382
        }
1383

    
1384
        return newReferencePoint;
1385
    }
1386

    
1387
    public void setLocale(Locale locale) {
1388
        IWindow[] win = this.getAllWindows();
1389
        if (win == null) {
1390
            return;
1391
        }
1392
        for (int i = 0; i < win.length; i++) {
1393
            if (win[i] instanceof Component) {
1394
                try {
1395
                    ((Component) win[i]).setLocale(locale);
1396
                } catch (Exception ex) {
1397
                    // Ignore errors and try others windows.
1398
                }
1399
            }
1400
        }
1401
    }
1402

    
1403
}