Statistics
| Revision:

root / trunk / libraries / libCorePlugin / src / com / iver / core / mdiManager / SingletonWindowSupport.java @ 8527

History | View | Annotate | Download (9.62 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.Component;
44
import java.awt.Rectangle;
45
import java.beans.PropertyVetoException;
46
import java.util.ArrayList;
47
import java.util.Comparator;
48
import java.util.Iterator;
49
import java.util.TreeMap;
50

    
51
import javax.swing.JComponent;
52
import javax.swing.JInternalFrame;
53

    
54
import com.iver.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
55
import com.iver.andami.ui.mdiManager.SingletonWindow;
56
import com.iver.andami.ui.mdiManager.IWindow;
57
import com.iver.andami.ui.mdiManager.WindowInfo;
58

    
59

    
60
/**
61
 * DOCUMENT ME!
62
 *
63
 * @author $author$
64
 * @version $Revision: 8527 $
65
 */
66
public class SingletonWindowSupport {
67
        private static int singletonViewInfoID = 0;
68
        /** Hashtable que asocia contenido con vistas */
69
        private HashMap contentWindowInfo = new HashMap();
70
        private WindowInfoSupport vis;
71
        private FrameWindowSupport frameWindowSupport;
72
        private HashMap contentFrame = new HashMap();
73

    
74
        /**
75
         * DOCUMENT ME!
76
         *
77
         * @param vis DOCUMENT ME!
78
         * @param fvs
79
         *
80
         * @see com.iver.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
81
         */
82
        public SingletonWindowSupport(WindowInfoSupport vis, FrameWindowSupport fvs) {
83
                this.vis = vis;
84
                this.frameWindowSupport = fvs;
85
        }
86

    
87
        /**
88
         * Devuelve una referencia a la vista si ya est? mostrada o null si la
89
         * vista no ha sido a?adida o ya fue cerrada
90
         *
91
         * @param windowClass DOCUMENT ME!
92
         * @param model DOCUMENT ME!
93
         * @param wi DOCUMENT ME!
94
         *
95
         * @return true si la vista existe ya y false si la vista no existe
96
         *
97
         * @throws SingletonDialogAlreadyShownException DOCUMENT ME!
98
         */
99
        public boolean registerWindow(Class windowClass, Object model, WindowInfo wi) {
100
                //Se comprueba si la ventana est? siendo mostrada
101
                SingletonWindowInfo swi = new SingletonWindowInfo(windowClass, model);
102

    
103
                if (contentWindowInfo.containsKey(swi)) {
104
                        if (wi.isModal()) {
105
                                throw new SingletonDialogAlreadyShownException();
106
                        }
107

    
108
                        wi.setWindowInfo((WindowInfo)contentWindowInfo.get(swi));
109

    
110
                        return true;
111
                } else {
112
                        //La ventana singleton no estaba mostrada
113
                        //Se asocia el modelo con la vista
114
                        contentWindowInfo.put(swi, wi);
115
                        return false;
116
                }
117
        }
118

    
119
        public void openSingletonWindow(SingletonWindow sw, JComponent frame){
120
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
121
                contentFrame.put(swi, frame);
122
        }
123

    
124
        public boolean contains(SingletonWindow sw){
125
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
126
                return contentFrame.containsKey(swi);
127
        }
128

    
129
        /**
130
         * DOCUMENT ME!
131
         *
132
         * @param sw
133
         */
134
        public void closeWindow(SingletonWindow sw) {
135
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
136
                WindowInfo viewInfo = (WindowInfo) contentWindowInfo.get(swi);
137
                if (viewInfo!=null) {
138
                        JInternalFrame c = (JInternalFrame) contentFrame.get(swi);
139
                        viewInfo.setWidth(c.getWidth());
140
                        viewInfo.setHeight(c.getHeight());
141
                        viewInfo.setX(c.getX());
142
                        viewInfo.setY(c.getY());
143
                        viewInfo.setClosed(true);
144
                        viewInfo.setNormalBounds(c.getNormalBounds());
145
                        viewInfo.setMaximized(c.isMaximum());
146
                }
147
                contentFrame.remove(swi);
148
        }
149

    
150
        /**
151
         * Representa una vista singleton manteniendo el modelo y la clase de la
152
         * vista que lo muestra
153
         *
154
         * @author Fernando Gonz?lez Cort?s
155
         */
156
        public class SingletonWindowInfo {
157

    
158
                public int id;
159

    
160
                /** Clase de la vista */
161
                public Class clase;
162

    
163
                /** Modelo que representa la vista */
164
                public Object modelo;
165

    
166
                /**
167
                 * Creates a new SingletonView object.
168
                 *
169
                 * @param clase Clase de la vista
170
                 * @param modelo Modelo que representa la vista
171
                 */
172
                public SingletonWindowInfo(Class clase, Object modelo) {
173
                        this.clase = clase;
174
                        this.modelo = modelo;
175
                        this.id = singletonViewInfoID;
176
                        singletonViewInfoID++;
177
                }
178

    
179
                /**
180
                 * @see java.lang.Object#equals(java.lang.Object)
181
                 */
182
                public boolean equals(Object obj) {
183
                        if (obj.getClass() != SingletonWindowInfo.class) {
184
                                throw new IllegalArgumentException();
185
                        }
186

    
187
                        SingletonWindowInfo s = (SingletonWindowInfo) obj;
188

    
189
                        if ((clase == s.clase) && (modelo == s.modelo)) {
190
                                return true;
191
                        } else {
192
                                return false;
193
                        }
194
                }
195
        }
196

    
197
        private JInternalFrame getFrame(SingletonWindowInfo svi){
198
                WindowInfo vi = (WindowInfo) contentWindowInfo.get(svi);
199
                return (JInternalFrame) contentFrame.get(svi);
200
        }
201

    
202
        public JInternalFrame getFrame(Class viewClass, Object model){
203
                SingletonWindowInfo svi = new SingletonWindowInfo(viewClass, model);
204
                return getFrame(svi);
205
        }
206

    
207
        /**
208
         * @param model
209
         * @return
210
         */
211
        public JInternalFrame[] getFrames(Object model) {
212
                ArrayList ret = new ArrayList();
213

    
214
                ArrayList keys = contentFrame.getKeys();
215
                for (int i = 0; i < keys.size(); i++) {
216
                        SingletonWindowInfo svi = (SingletonWindowInfo) keys.get(i);
217

    
218
                        if (svi.modelo == model){
219
                                ret.add(contentFrame.get(svi));
220
                        }
221
                }
222

    
223
                return (JInternalFrame[]) ret.toArray(new JInternalFrame[0]);
224
        }
225

    
226
        /**
227
         * @param view
228
         * @return
229
         */
230
        public JInternalFrame getFrame(SingletonWindow sv) {
231
                SingletonWindowInfo svi = new SingletonWindowInfo(sv.getClass(), sv.getWindowModel());
232
                return getFrame(svi);
233
        }
234

    
235
        /**
236
         * @param sv
237
         * @param i
238
         */
239
        public void setX(SingletonWindow sv, int x) {
240
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
241

    
242
        if (o == null) return;
243
        o.setLocation(x, o.getY());
244
        }
245

    
246
        /**
247
         * @param sv
248
         * @param i
249
         */
250
        public void setY(SingletonWindow sv, int y) {
251
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
252

    
253
        if (o == null) return;
254

    
255
        o.setLocation(o.getX(), y);
256
        }
257

    
258
        /**
259
         * @param sv
260
         * @param i
261
         */
262
        public void setHeight(SingletonWindow sv, int height) {
263
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
264

    
265
        if (o == null) return;
266

    
267
        o.setSize(o.getWidth(), height);
268
        }
269

    
270
        /**
271
         * @param sv
272
         * @param i
273
         */
274
        public void setWidth(SingletonWindow sv, int width) {
275
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
276

    
277
        if (o == null) return;
278
        o.setSize(width, o.getHeight());
279
        }
280

    
281
        /**
282
         * @param sw
283
         * @param maximized
284
         */
285
        public void setMaximized(SingletonWindow sw, boolean maximized) {
286
                JInternalFrame frame = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
287

    
288
        if (frame == null) return;
289
        try {
290
                        frame.setMaximum(maximized);
291
                } catch (PropertyVetoException e) {
292
                        // TODO Auto-generated catch block
293
                        //e.printStackTrace();
294
                }
295
        }
296

    
297
        /**
298
         * @param sw
299
         * @param maximized
300
         */
301
        public void setNormalBounds(SingletonWindow sw, Rectangle normalBounds) {
302
                JInternalFrame frame = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
303

    
304
        if (frame == null) return;
305
        frame.setNormalBounds(normalBounds);
306
        }
307

    
308
        /**
309
         * @param sv
310
         * @param string
311
         */
312
        public void setTitle(SingletonWindow sv, String title) {
313
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
314

    
315
        if (o == null) return;
316
        o.setTitle(title);
317
        }
318

    
319
        private class HashMap {
320
            private ArrayList keys = new ArrayList();
321
            private ArrayList values = new ArrayList();
322

    
323
            public void put(SingletonWindowInfo key, Object value) {
324
                int index = -1;
325
                for (int i = 0; i < keys.size(); i++) {
326
                    if (keys.get(i).equals(key)){
327
                        index = i;
328
                        break;
329
                    }
330
            }
331

    
332
                if (index != -1){
333
                    keys.add(index, key);
334
                    values.add(index, value);
335
                }else{
336
                    keys.add(key);
337
                    values.add(value);
338
                }
339
            }
340

    
341
            public boolean containsKey(SingletonWindowInfo key){
342
                for (int i = 0; i < keys.size(); i++) {
343
                    if (keys.get(i).equals(key)){
344
                        return true;
345
                    }
346
                }
347

    
348
                return false;
349
            }
350

    
351
            public Object get(SingletonWindowInfo key){
352
                for (int i = 0; i < keys.size(); i++) {
353
                    if (keys.get(i).equals(key)){
354
                        return values.get(i);
355
                    }
356
                }
357

    
358
                return null;
359
            }
360

    
361
            public void remove(SingletonWindowInfo key){
362
                for (int i = 0; i < keys.size(); i++) {
363
                    if (keys.get(i).equals(key)){
364
                        keys.remove(i);
365
                        values.remove(i);
366
                    }
367
                }
368
            }
369

    
370
            public ArrayList getKeys(){
371
                return keys;
372
            }
373
        }
374
}