Statistics
| Revision:

root / trunk / libraries / libCorePlugin / src / com / iver / core / mdiManager / NewSkin.java @ 3007

History | View | Annotate | Download (16.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.core.mdiManager;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Cursor;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.awt.event.WindowAdapter;
49
import java.awt.event.WindowEvent;
50
import java.beans.PropertyVetoException;
51
import java.util.ArrayList;
52
import java.util.Iterator;
53
import java.util.Stack;
54

    
55
import javax.swing.DefaultDesktopManager;
56
import javax.swing.DesktopManager;
57
import javax.swing.JDesktopPane;
58
import javax.swing.JDialog;
59
import javax.swing.JInternalFrame;
60
import javax.swing.JLayeredPane;
61
import javax.swing.event.InternalFrameEvent;
62
import javax.swing.event.InternalFrameListener;
63

    
64
import org.apache.log4j.Logger;
65

    
66
import com.iver.andami.plugins.Extension;
67
import com.iver.andami.ui.mdiFrame.GlassPane;
68
import com.iver.andami.ui.mdiFrame.MDIFrame;
69
import com.iver.andami.ui.mdiManager.MDIManager;
70
import com.iver.andami.ui.mdiManager.MDIUtilities;
71
import com.iver.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
72
import com.iver.andami.ui.mdiManager.SingletonView;
73
import com.iver.andami.ui.mdiManager.View;
74
import com.iver.andami.ui.mdiManager.ViewInfo;
75
import com.iver.andami.ui.mdiManager.ViewListener;
76

    
77

    
78
/**
79
 *
80
 */
81
public class NewSkin implements MDIManager, Extension {
82
        /**
83
         * Variable privada <code>desktopManager</code> para usarlo cuando sale una
84
         * ventana que no queremos que nos restaure las que tenemos maximizadas.
85
         * Justo antes de usar el setMaximize(false), le pegamos el cambiazo.
86
         */
87
        private static DesktopManager desktopManager = new DefaultDesktopManager();
88

    
89
        /** log */
90
        private static Logger logger = Logger.getLogger(NewSkin.class.getName());
91

    
92
        /** Panel de la MDIFrame */
93
        private JDesktopPane panel = new JDesktopPane();
94

    
95
        /** MDIFrame */
96
        private MDIFrame mainFrame;
97
        private GlassPane glassPane = new GlassPane();
98
        private DialogStackSupport dss;
99
        private FrameViewSupport fvs;
100
        private ViewInfoSupport vis;
101
        private ViewStackSupport vss;
102
        private SingletonViewSupport svs;
103
    private Cursor lastCursor = null;
104
    
105
        /**
106
         * @see com.iver.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
107
         */
108
        public void init(MDIFrame f) {
109
                //Inicializa el Frame y la consola
110
                mainFrame = f;
111
                mainFrame.setGlassPane(glassPane);
112
                panel.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
113

    
114
                mainFrame.getContentPane().add(panel, BorderLayout.CENTER);
115
                panel.setDesktopManager(desktopManager);
116

    
117
                fvs = new FrameViewSupport(mainFrame);
118
                dss = new DialogStackSupport(mainFrame);
119
                svs = new SingletonViewSupport(vis, fvs);
120
                vis = new ViewInfoSupport(mainFrame, fvs, svs);
121
                fvs.setVis(vis);
122
                vss = new ViewStackSupport(vis);
123
        }
124

    
125
        /**
126
         * @see com.iver.andami.ui.mdiManager.MDIManager#addView(com.iver.andami.ui.mdiManager.View)
127
         */
128
        public View addView(View p) throws SingletonDialogAlreadyShownException {
129
                //se obtiene la informaci?n de la vista
130
                ViewInfo vi = vis.getViewInfo(p);
131

    
132
                //Se comprueban las incompatibilidades que pudieran haber en la vista
133
                MDIUtilities.checkViewInfo(vi);
134
                if ((p instanceof SingletonView) && (vi.isModal())){
135
                        throw new RuntimeException("A modal view cannot be a SingletonView");                        
136
                }
137
        
138
                /*
139
                 * Se obtiene la referencia a la vista anterior por si es una singleton y est?
140
                 * siendo mostrada. Se obtiene su informaci?n si ya fue mostrada
141
                 */
142
                boolean singletonPreviouslyAdded = false;
143
                
144
                if (p instanceof SingletonView){
145
                        SingletonView sv = (SingletonView) p;
146
                        if (svs.registerView(sv.getClass(), sv.getViewModel(), vis.getViewInfo(sv))){
147
                                singletonPreviouslyAdded = true;
148
                        }
149
                }
150

    
151
                if (singletonPreviouslyAdded){
152
                        //Si la vista no est? actualmente abierta
153
                        if (!svs.contains((SingletonView)p)){
154
                                JInternalFrame frame = fvs.getJInternalFrame(p);
155
                                svs.openSingletonView((SingletonView)p, frame);
156
                                addJInternalFrame(frame, vi);
157
                                vss.add(p,
158
                                                new ActionListener() {
159
                                                        public void actionPerformed(ActionEvent e) {
160
                                                                View v = vis.getViewById(Integer.parseInt(
161
                                                                                        e.getActionCommand()));
162
                                                                JInternalFrame f = fvs.getJInternalFrame(v);
163
                                                                activateJInternalFrame(f);
164
                                                        }
165
                                                });
166
                                return p;
167
                        }else{
168
                                //La vista est? actualmente abierta
169
                                JInternalFrame frame = (JInternalFrame) svs.getFrame((SingletonView)p);
170
                                activateJInternalFrame(frame);
171
                vss.setActive(p);
172
                                return fvs.getView((JInternalFrame) frame);
173
                        }
174
                } else {
175
                        if (vi.isModal()) {
176
                                addJDialog(p);
177
                        } else {
178
                                //Se sit?a la vista en la pila de vistas
179
                                vss.add(p,
180
                                        new ActionListener() {
181
                                                public void actionPerformed(ActionEvent e) {
182
                                                        View v = vis.getViewById(Integer.parseInt(
183
                                                                                e.getActionCommand()));
184
                                                        JInternalFrame f = fvs.getJInternalFrame(v);
185
                            activateJInternalFrame(f);
186
                                                }
187
                                        });
188
                                addJInternalFrame(p);
189
                        }
190

    
191
                        return p;
192
                }
193
        }
194

    
195
        /**
196
         * DOCUMENT ME!
197
         *
198
         * @param wnd DOCUMENT ME!
199
         * @param vi DOCUMENT ME!
200
         */
201
        private void addJInternalFrame(JInternalFrame wnd, ViewInfo vi) {
202
        wnd.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
203
                wnd.addInternalFrameListener(new FrameListener());
204
                
205
                if (vi.isModeless() || vi.isPalette()) {
206
                        panel.add(wnd, JDesktopPane.PALETTE_LAYER);
207
                } else {
208
                        panel.add(wnd);
209
                }
210

    
211
                activateJInternalFrame(wnd);
212
        }
213

    
214
        /**
215
         * DOCUMENT ME!
216
         *
217
         * @param p
218
         */
219
        private void addJInternalFrame(View p) {
220
                ViewInfo vi = vis.getViewInfo(p);
221

    
222
                JInternalFrame wnd = fvs.getJInternalFrame(p);
223

    
224
                if (p instanceof SingletonView){
225
                        SingletonView sv = (SingletonView) p;
226
                        svs.openSingletonView(sv, wnd);
227
                }
228

    
229
                addJInternalFrame(wnd, vi);
230
        }
231

    
232
        /**
233
         * DOCUMENT ME!
234
         *
235
         * @param wnd
236
         */
237
        private void activateJInternalFrame(JInternalFrame wnd) {
238
                try {
239
                        wnd.moveToFront();
240
            logger.debug("Activando " + wnd.getTitle());
241
                        wnd.setSelected(true);
242
                        wnd.setIcon(false);
243
                } catch (PropertyVetoException e) {
244
                        logger.error(e);
245
                }
246
        }
247

    
248
        /**
249
         * Situa un di?logo modal en el centro de la pantalla
250
         *
251
         * @param d Di?logo que se quiere situar
252
         */
253
        private void centerDialog(JDialog d) {
254
                int offSetX = d.getWidth() / 2;
255
                int offSetY = d.getHeight() / 2;
256

    
257
                d.setLocation((mainFrame.getWidth() / 2) - offSetX,
258
                        (mainFrame.getHeight() / 2) - offSetY);
259
        }
260

    
261
        /**
262
         * DOCUMENT ME!
263
         *
264
         * @param p
265
         */
266
        private void addJDialog(View p) {
267
                JDialog dlg = fvs.getJDialog(p);
268

    
269
                centerDialog(dlg);
270

    
271
                dlg.addWindowListener(new DialogWindowListener());
272
                dss.pushDialog(dlg);
273

    
274
                dlg.setVisible(vis.getViewInfo(p).isVisible());
275
        }
276

    
277
        /**
278
         * @see com.iver.andami.ui.mdiManager.MDIManager#getActiveView()
279
         */
280
        public View getActiveView() {
281
                JInternalFrame jif = panel.getSelectedFrame();
282

    
283
                if (jif != null) {
284
                        return fvs.getView(jif);
285
                }
286

    
287
                return null;
288
        }
289

    
290
        /**
291
         * @see com.iver.andami.ui.mdiManager.MDIManager#closeView(com.iver.andami.ui.mdiManager.View)
292
         */
293
        public void closeView(View p) {
294
                //Si es un di?logo modal 
295
                if (p.getViewInfo().isModal()) {
296
                        closeJDialog();
297
                } else { //Si no es modal se cierra el JInternalFrame
298
                        closeJInternalFrame(fvs.getJInternalFrame(p));
299
                }
300
        }
301

    
302
        /**
303
         * @see com.iver.andami.ui.mdiManager.MDIManager#closeAllViews()
304
         */
305
        public void closeAllViews() {
306
                ArrayList eliminar = new ArrayList();
307
                Iterator i = fvs.getViewIterator();
308

    
309
                while (i.hasNext()) {
310
                        eliminar.add((View) i.next());
311
                }
312

    
313
                for (Iterator iter = eliminar.iterator(); iter.hasNext();) {
314
                        View vista = (View) iter.next();
315
                        closeView(vista);
316
                }
317
        }
318

    
319
        /**
320
         * @see com.iver.andami.ui.mdiManager.MDIManager#getViewInfo(com.iver.andami.ui.mdiManager.View)
321
         */
322
        public ViewInfo getViewInfo(View v) {
323
                return vis.getViewInfo(v);
324
        }
325

    
326
        /**
327
         * DOCUMENT ME!
328
         *
329
         * @param dialog
330
         *
331
         * @throws RuntimeException DOCUMENT ME!
332
         */
333
        private void closeJDialog() {
334
                JDialog dlg = dss.popDialog();
335

    
336
                dlg.setVisible(false);
337

    
338
                View s = (View) fvs.getView(dlg);
339

    
340
                callViewClosed(s);
341

    
342
                fvs.closeView(s);
343

    
344
                //Si es singleton se desasocia el modelo con la vista
345
                if (s instanceof SingletonView) {
346
                        svs.closeView((SingletonView) s);
347
                }
348
        }
349

    
350
        /**
351
         * DOCUMENT ME!
352
         *
353
         * @param view DOCUMENT ME!
354
         */
355
        private void callViewClosed(View view) {
356
                if (view instanceof ViewListener) {
357
                        ((ViewListener) view).viewClosed();
358
                }
359
        }
360

    
361
        /**
362
         * DOCUMENT ME!
363
         *
364
         * @param view DOCUMENT ME!
365
         */
366
        private void callViewActivated(View view) {
367
        logger.debug("View " + view.getViewInfo().getTitle()+ " activated (callViewActivated)");
368
                if (view instanceof ViewListener) {
369
                        ((ViewListener) view).viewActivated();
370
                }
371
        }
372

    
373
        /**
374
         * DOCUMENT ME!
375
         *
376
         * @param frame
377
         */
378
        private void closeJInternalFrame(JInternalFrame frame) {
379
                try {
380
                        View s = (View) fvs.getView(frame);
381

    
382
                        frame.setClosed(true);
383
                        callViewClosed(s);
384
                } catch (PropertyVetoException e) {
385
                        logger.error("Not compatible with property veto's. Use ViewInfo instead.",
386
                                e);
387
                }
388
        }
389

    
390
        /**
391
         * @see com.iver.andami.plugins.Extension#inicializar()
392
         */
393
        public void inicializar() {
394
        }
395

    
396
        /**
397
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
398
         */
399
        public void execute(String actionCommand) {
400
                if (actionCommand.equals("cascada")) {
401
                } else if (actionCommand.equals("mosaico")) {
402
                }
403
        }
404

    
405
        /**
406
         * @see com.iver.andami.plugins.Extension#isEnabled()
407
         */
408
        public boolean isEnabled() {
409
                // TODO Auto-generated method stub
410
                return false;
411
        }
412

    
413
        /**
414
         * @see com.iver.andami.plugins.Extension#isVisible()
415
         */
416
        public boolean isVisible() {
417
                // TODO Auto-generated method stub
418
                return true;
419
        }
420

    
421
        /**
422
         * @see com.iver.andami.ui.mdiManager.MDIManager#setWaitCursor()
423
         */
424
        public void setWaitCursor() {
425
                if (mainFrame != null) {
426
                        glassPane.setVisible(true);
427
            lastCursor = mainFrame.getCursor();                        
428
                        dss.setWaitCursor();
429
            mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
430
                }
431
        }
432

    
433
        /**
434
         * @see com.iver.andami.ui.mdiManager.MDIManager#restoreCursor()
435
         */
436
        public void restoreCursor() {
437
                if (mainFrame != null) {
438
                        glassPane.setVisible(false);                        
439
                        dss.restoreCursor();
440
            mainFrame.setCursor(lastCursor);
441
                }
442
        }
443

    
444
        /**
445
         * Listener para los eventos de cerrado de los di?logos. Tiene su raz?n de
446
         * ser en que los di?logos han de devolverse al pool cuando se cierran
447
         *
448
         * @author Fernando Gonz?lez Cort?s
449
         */
450
        public class DialogWindowListener extends WindowAdapter {
451
                /**
452
                 * Captura el evento de cerrado de los di?logos con el fin de realizar
453
                 * tareas de mantenimiento
454
                 *
455
                 * @param e evento
456
                 */
457
                public void windowClosing(WindowEvent e) {
458
                        closeJDialog();
459
                }
460
        }
461

    
462
        /**
463
         * DOCUMENT ME!
464
         */
465
        public class FrameListener implements InternalFrameListener {
466
                /**
467
                 * @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
468
                 */
469
                public void internalFrameActivated(InternalFrameEvent e) {
470
            logger.debug("internalFrameActivated " + e.getInternalFrame().getTitle());
471
            
472
            // activatedInternalFramesStack.push(e.getInternalFrame());
473
            
474
                        View panel = fvs.getView((JInternalFrame) e.getSource());
475
            
476
                        ViewInfo vi = vis.getViewInfo(panel);
477
            if (vi.isPalette()) return;
478
            
479
            vss.setActive(panel);
480
            
481
                        JInternalFrame frame = fvs.getJInternalFrame(panel);
482

    
483
                        if (!frame.isMaximizable() && frame.isMaximum()) {
484
                                try {
485
                                        frame.setMaximum(false);
486
                                } catch (PropertyVetoException e1) {
487
                                }
488
                        }
489
                        mainFrame.enableControls();
490
            if (vi.getSelectedTool() != null)
491
                mainFrame.setSelectedTool(vi.getSelectedTool());
492
                        callViewActivated(panel);
493

    
494
                        
495
                }
496

    
497
                /**
498
                 * @see javax.swing.event.InternalFrameListener#internalFrameClosed(javax.swing.event.InternalFrameEvent)
499
                 */
500
                public void internalFrameClosed(InternalFrameEvent e) {
501
                }
502

    
503
                /**
504
                 * @see javax.swing.event.InternalFrameListener#internalFrameClosing(javax.swing.event.InternalFrameEvent)
505
                 */
506
                public void internalFrameClosing(InternalFrameEvent e) {
507
                        // Se elimina la memoria del JInternalFrame si no es ALWAYS_LIVE
508
            logger.debug("internalFrameClosing " + e.getInternalFrame().getTitle());
509
            
510
                        JInternalFrame c = (JInternalFrame) e.getSource();
511
                        ViewInfo vi = vis.getViewInfo((View) fvs.getView(c));
512

    
513
                        View view = fvs.getView(c);
514

    
515
                        boolean alwaysLive;
516
                        if (view instanceof SingletonView) {
517
                                svs.closeView((SingletonView) view);
518
                        }
519

    
520
                        fvs.closeView(view);
521
            
522
                        panel.remove(c);
523

    
524
                        vss.remove(view);
525

    
526
            if (!vi.isPalette())             
527
                            mainFrame.enableControls();
528

    
529
                        panel.repaint();
530
            
531
            // Para activar el JInternalFrame desde la que hemos
532
            // abierto la ventana que estamos cerrando
533
            View lastView = vss.getActiveView();
534
            // La activamos
535
            if (lastView != null)
536
            {
537
                System.err.println("Devuelvo el foco a " + lastView.getViewInfo().getTitle());
538
                JInternalFrame frame = fvs.getJInternalFrame(lastView);
539
                try {
540
                    frame.setSelected(true);
541
                } catch (PropertyVetoException e1) {
542
                    // TODO Auto-generated catch block
543
                    // e1.printStackTrace();
544
                }
545
                // addView(lastView);
546
            }
547

    
548
                }
549

    
550
                /**
551
                 * @see javax.swing.event.InternalFrameListener#internalFrameDeactivated(javax.swing.event.InternalFrameEvent)
552
                 */
553
                public void internalFrameDeactivated(InternalFrameEvent e) {
554
            logger.debug("internalDeActivated " + e.getInternalFrame().getTitle());
555
            JInternalFrame c = (JInternalFrame) e.getSource();
556
            View andamiView = fvs.getView(c);
557
            if (andamiView != null)
558
            {
559
                ViewInfo vi = vis.getViewInfo(andamiView);
560
                if (vi.isPalette()) return;
561
                vi.setSelectedTool(mainFrame.getSelectedTool());
562
            }
563

    
564
                }
565

    
566
                /**
567
                 * @see javax.swing.event.InternalFrameListener#internalFrameDeiconified(javax.swing.event.InternalFrameEvent)
568
                 */
569
                public void internalFrameDeiconified(InternalFrameEvent e) {            
570
                        mainFrame.enableControls();
571
                }
572

    
573
                /**
574
                 * @see javax.swing.event.InternalFrameListener#internalFrameIconified(javax.swing.event.InternalFrameEvent)
575
                 */
576
                public void internalFrameIconified(InternalFrameEvent e) {
577
                        mainFrame.enableControls();
578
                }
579

    
580
                /**
581
                 * @see javax.swing.event.InternalFrameListener#internalFrameOpened(javax.swing.event.InternalFrameEvent)
582
                 */
583
                public void internalFrameOpened(InternalFrameEvent e) {
584
            logger.debug("internalFrameOpened. Source= " + e.getSource().toString());
585
                }
586
        }
587

    
588
        /**
589
         * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonView(java.lang.Class, java.lang.Object)
590
         */
591
        public boolean closeSingletonView(Class viewClass, Object model) {
592
                JInternalFrame frame = svs.getFrame(viewClass, model);
593
                if (frame == null) return false;
594
                closeJInternalFrame(frame);
595
                return true;
596
        }
597

    
598
        /**
599
         * @see com.iver.andami.ui.mdiManager.MDIManager#closeSingletonView(java.lang.Object)
600
         */
601
        public boolean closeSingletonView(Object model) {
602
                JInternalFrame[] frames = svs.getFrames(model);
603
                if (frames.length == 0) return false;
604
                for (int i = 0; i < frames.length; i++) {
605
                        closeJInternalFrame(frames[i]);
606
                }
607
                return true;
608
        }
609

    
610
        public View[] getAllViews() {
611
                ArrayList views = new ArrayList();
612
                Iterator i = fvs.getViewIterator();
613

    
614
                while (i.hasNext()) {
615
                        views.add((View) i.next());
616
                }
617
                return (View[])views.toArray(new View[0]);
618
        }
619
}