Statistics
| Revision:

root / tags / v1_1_Build_1005 / libraries / libCorePlugin / src / com / iver / core / mdiManager / SingletonWindowSupport.java @ 12355

History | View | Annotate | Download (9.49 KB)

1 6892 cesar
/* 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 8765 jjdelcerro
import java.awt.Rectangle;
44
import java.beans.PropertyVetoException;
45 6892 cesar
import java.util.ArrayList;
46
47
import javax.swing.JComponent;
48
import javax.swing.JInternalFrame;
49
50
import com.iver.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
51
import com.iver.andami.ui.mdiManager.SingletonWindow;
52
import com.iver.andami.ui.mdiManager.WindowInfo;
53
54
55
/**
56
 * DOCUMENT ME!
57
 *
58
 * @author $author$
59
 * @version $Revision$
60
 */
61
public class SingletonWindowSupport {
62
        private static int singletonViewInfoID = 0;
63
        /** Hashtable que asocia contenido con vistas */
64
        private HashMap contentWindowInfo = new HashMap();
65
        private WindowInfoSupport vis;
66
        private FrameWindowSupport frameWindowSupport;
67
        private HashMap contentFrame = new HashMap();
68 8765 jjdelcerro
69 6892 cesar
        /**
70
         * DOCUMENT ME!
71
         *
72
         * @param vis DOCUMENT ME!
73
         * @param fvs
74
         *
75
         * @see com.iver.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
76
         */
77
        public SingletonWindowSupport(WindowInfoSupport vis, FrameWindowSupport fvs) {
78
                this.vis = vis;
79
                this.frameWindowSupport = fvs;
80
        }
81
82
        /**
83
         * Devuelve una referencia a la vista si ya est? mostrada o null si la
84
         * vista no ha sido a?adida o ya fue cerrada
85
         *
86
         * @param windowClass DOCUMENT ME!
87
         * @param model DOCUMENT ME!
88
         * @param wi DOCUMENT ME!
89
         *
90
         * @return true si la vista existe ya y false si la vista no existe
91
         *
92
         * @throws SingletonDialogAlreadyShownException DOCUMENT ME!
93
         */
94
        public boolean registerWindow(Class windowClass, Object model, WindowInfo wi) {
95 8765 jjdelcerro
                //Se comprueba si la ventana est? siendo mostrada
96
                SingletonWindowInfo swi = new SingletonWindowInfo(windowClass, model);
97 6892 cesar
98 8765 jjdelcerro
                if (contentWindowInfo.containsKey(swi)) {
99 6892 cesar
                        if (wi.isModal()) {
100
                                throw new SingletonDialogAlreadyShownException();
101
                        }
102
103 8765 jjdelcerro
                        wi.setWindowInfo((WindowInfo)contentWindowInfo.get(swi));
104
105 6892 cesar
                        return true;
106
                } else {
107 8765 jjdelcerro
                        //La ventana singleton no estaba mostrada
108 6892 cesar
                        //Se asocia el modelo con la vista
109 8765 jjdelcerro
                        contentWindowInfo.put(swi, wi);
110 6892 cesar
                        return false;
111
                }
112
        }
113
114 8765 jjdelcerro
        public void openSingletonWindow(SingletonWindow sw, JComponent frame){
115
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
116
                contentFrame.put(swi, frame);
117 6892 cesar
        }
118 8765 jjdelcerro
119 6892 cesar
        public boolean contains(SingletonWindow sw){
120 8765 jjdelcerro
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
121
                return contentFrame.containsKey(swi);
122 6892 cesar
        }
123 8765 jjdelcerro
124 6892 cesar
        /**
125
         * DOCUMENT ME!
126
         *
127 8765 jjdelcerro
         * @param sw
128 6892 cesar
         */
129 8765 jjdelcerro
        public void closeWindow(SingletonWindow sw) {
130
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
131
                WindowInfo windowInfo = (WindowInfo) contentWindowInfo.get(swi);
132
                if (windowInfo!=null) {
133
                        JInternalFrame c = (JInternalFrame) contentFrame.get(swi);
134
                        windowInfo.setWidth(c.getWidth());
135
                        windowInfo.setHeight(c.getHeight());
136
                        windowInfo.setX(c.getX());
137
                        windowInfo.setY(c.getY());
138
                        windowInfo.setClosed(true);
139
                        windowInfo.setNormalBounds(c.getNormalBounds());
140
                        windowInfo.setMaximized(c.isMaximum());
141
                }
142
                contentFrame.remove(swi);
143 6892 cesar
        }
144 8765 jjdelcerro
145 6892 cesar
        /**
146
         * Representa una vista singleton manteniendo el modelo y la clase de la
147
         * vista que lo muestra
148
         *
149
         * @author Fernando Gonz?lez Cort?s
150
         */
151 8765 jjdelcerro
        public class SingletonWindowInfo {
152
153 6892 cesar
                public int id;
154 8765 jjdelcerro
155 6892 cesar
                /** Clase de la vista */
156
                public Class clase;
157
158
                /** Modelo que representa la vista */
159
                public Object modelo;
160 8765 jjdelcerro
161 6892 cesar
                /**
162
                 * Creates a new SingletonView object.
163
                 *
164
                 * @param clase Clase de la vista
165
                 * @param modelo Modelo que representa la vista
166
                 */
167 8765 jjdelcerro
                public SingletonWindowInfo(Class clase, Object modelo) {
168 6892 cesar
                        this.clase = clase;
169
                        this.modelo = modelo;
170
                        this.id = singletonViewInfoID;
171
                        singletonViewInfoID++;
172
                }
173
174
                /**
175
                 * @see java.lang.Object#equals(java.lang.Object)
176
                 */
177
                public boolean equals(Object obj) {
178 8765 jjdelcerro
                        if (obj.getClass() != SingletonWindowInfo.class) {
179 6892 cesar
                                throw new IllegalArgumentException();
180
                        }
181
182 8765 jjdelcerro
                        SingletonWindowInfo s = (SingletonWindowInfo) obj;
183 6892 cesar
184
                        if ((clase == s.clase) && (modelo == s.modelo)) {
185
                                return true;
186
                        } else {
187
                                return false;
188
                        }
189
                }
190
        }
191
192 8765 jjdelcerro
        private JInternalFrame getFrame(SingletonWindowInfo svi){
193 6892 cesar
                WindowInfo vi = (WindowInfo) contentWindowInfo.get(svi);
194
                return (JInternalFrame) contentFrame.get(svi);
195
        }
196 8765 jjdelcerro
197 6892 cesar
        public JInternalFrame getFrame(Class viewClass, Object model){
198 8765 jjdelcerro
                SingletonWindowInfo svi = new SingletonWindowInfo(viewClass, model);
199 6892 cesar
                return getFrame(svi);
200
        }
201
202
        /**
203
         * @param model
204
         * @return
205
         */
206
        public JInternalFrame[] getFrames(Object model) {
207
                ArrayList ret = new ArrayList();
208 8765 jjdelcerro
209 6892 cesar
                ArrayList keys = contentFrame.getKeys();
210
                for (int i = 0; i < keys.size(); i++) {
211 8765 jjdelcerro
                        SingletonWindowInfo svi = (SingletonWindowInfo) keys.get(i);
212
213 6892 cesar
                        if (svi.modelo == model){
214
                                ret.add(contentFrame.get(svi));
215
                        }
216
                }
217 8765 jjdelcerro
218 6892 cesar
                return (JInternalFrame[]) ret.toArray(new JInternalFrame[0]);
219
        }
220
221
        /**
222
         * @param view
223
         * @return
224
         */
225
        public JInternalFrame getFrame(SingletonWindow sv) {
226 8765 jjdelcerro
                SingletonWindowInfo svi = new SingletonWindowInfo(sv.getClass(), sv.getWindowModel());
227 6892 cesar
                return getFrame(svi);
228
        }
229
230
        /**
231
         * @param sv
232
         * @param i
233
         */
234
        public void setX(SingletonWindow sv, int x) {
235 8765 jjdelcerro
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
236 6892 cesar
237
        if (o == null) return;
238
        o.setLocation(x, o.getY());
239
        }
240
241
        /**
242
         * @param sv
243
         * @param i
244
         */
245
        public void setY(SingletonWindow sv, int y) {
246 8765 jjdelcerro
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
247 6892 cesar
248
        if (o == null) return;
249 8765 jjdelcerro
250 6892 cesar
        o.setLocation(o.getX(), y);
251
        }
252
253
        /**
254
         * @param sv
255
         * @param i
256
         */
257
        public void setHeight(SingletonWindow sv, int height) {
258 8765 jjdelcerro
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
259 6892 cesar
260
        if (o == null) return;
261 8765 jjdelcerro
262 6892 cesar
        o.setSize(o.getWidth(), height);
263
        }
264
265
        /**
266
         * @param sv
267
         * @param i
268
         */
269
        public void setWidth(SingletonWindow sv, int width) {
270 8765 jjdelcerro
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
271 6892 cesar
272
        if (o == null) return;
273
        o.setSize(width, o.getHeight());
274
        }
275
276
        /**
277 8765 jjdelcerro
         * @param sw
278
         * @param maximized
279
         */
280
        public void setMaximized(SingletonWindow sw, boolean maximized) {
281
                JInternalFrame frame = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
282
283
        if (frame == null) return;
284
        try {
285
                        frame.setMaximum(maximized);
286
                } catch (PropertyVetoException e) {
287
                        // TODO Auto-generated catch block
288
                        //e.printStackTrace();
289
                }
290
        }
291
292
        /**
293
         * @param sw
294
         * @param maximized
295
         */
296
        public void setNormalBounds(SingletonWindow sw, Rectangle normalBounds) {
297
                JInternalFrame frame = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
298
299
        if (frame == null) return;
300
        frame.setNormalBounds(normalBounds);
301
        }
302
303
        /**
304 6892 cesar
         * @param sv
305
         * @param string
306
         */
307
        public void setTitle(SingletonWindow sv, String title) {
308 8765 jjdelcerro
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
309 6892 cesar
310
        if (o == null) return;
311
        o.setTitle(title);
312
        }
313 8765 jjdelcerro
314 6892 cesar
        private class HashMap {
315
            private ArrayList keys = new ArrayList();
316
            private ArrayList values = new ArrayList();
317 8765 jjdelcerro
318
            public void put(SingletonWindowInfo key, Object value) {
319 6892 cesar
                int index = -1;
320
                for (int i = 0; i < keys.size(); i++) {
321
                    if (keys.get(i).equals(key)){
322
                        index = i;
323
                        break;
324
                    }
325
            }
326 8765 jjdelcerro
327 6892 cesar
                if (index != -1){
328
                    keys.add(index, key);
329
                    values.add(index, value);
330
                }else{
331
                    keys.add(key);
332
                    values.add(value);
333
                }
334
            }
335 8765 jjdelcerro
336
            public boolean containsKey(SingletonWindowInfo key){
337 6892 cesar
                for (int i = 0; i < keys.size(); i++) {
338
                    if (keys.get(i).equals(key)){
339
                        return true;
340
                    }
341
                }
342 8765 jjdelcerro
343 6892 cesar
                return false;
344
            }
345 8765 jjdelcerro
346
            public Object get(SingletonWindowInfo key){
347 6892 cesar
                for (int i = 0; i < keys.size(); i++) {
348
                    if (keys.get(i).equals(key)){
349
                        return values.get(i);
350
                    }
351
                }
352 8765 jjdelcerro
353 6892 cesar
                return null;
354
            }
355 8765 jjdelcerro
356
            public void remove(SingletonWindowInfo key){
357 6892 cesar
                for (int i = 0; i < keys.size(); i++) {
358
                    if (keys.get(i).equals(key)){
359
                        keys.remove(i);
360
                        values.remove(i);
361
                    }
362
                }
363
            }
364 8765 jjdelcerro
365 6892 cesar
            public ArrayList getKeys(){
366
                return keys;
367
            }
368
        }
369
}