Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libCorePlugin / src / org / gvsig / coreplugin / mdiManager / SingletonWindowSupport.java @ 31344

History | View | Annotate | Download (9.82 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 org.gvsig.coreplugin.mdiManager;
42

    
43
import java.awt.Component;
44
import java.awt.Dimension;
45
import java.awt.Rectangle;
46
import java.beans.PropertyVetoException;
47
import java.util.ArrayList;
48

    
49
import javax.swing.JComponent;
50
import javax.swing.JInternalFrame;
51

    
52
import org.gvsig.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
53
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
54
import org.gvsig.andami.ui.mdiManager.WindowInfo;
55

    
56

    
57

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

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

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

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

    
106
                        wi.setWindowInfo((WindowInfo)contentWindowInfo.get(swi));
107

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

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

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

    
127
        public boolean contains(Class windowClass, Object model){
128
                SingletonWindowInfo swi = new SingletonWindowInfo(windowClass, model);
129
                return contentFrame.containsKey(swi);
130
        }
131

    
132
        /**
133
         * DOCUMENT ME!
134
         *
135
         * @param sw
136
         */
137
        public void closeWindow(SingletonWindow sw) {
138
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
139
                WindowInfo windowInfo = (WindowInfo) contentWindowInfo.get(swi);
140
                if (windowInfo!=null) {
141
                        frameWindowSupport.updateWindowInfo(sw, windowInfo);
142
                }
143
                contentFrame.remove(swi);
144
        }
145

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

    
154
                public int id;
155

    
156
                /** Clase de la vista */
157
                public Class clase;
158

    
159
                /** Modelo que representa la vista */
160
                public Object modelo;
161

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

    
175
                /**
176
                 * @see java.lang.Object#equals(java.lang.Object)
177
                 */
178
                public boolean equals(Object obj) {
179
                        if (obj.getClass() != SingletonWindowInfo.class) {
180
                                throw new IllegalArgumentException();
181
                        }
182

    
183
                        SingletonWindowInfo s = (SingletonWindowInfo) obj;
184

    
185
                        if ((clase == s.clase) && (modelo == s.modelo)) {
186
                                return true;
187
                        } else {
188
                                return false;
189
                        }
190
                }
191
        }
192

    
193
        private Component getFrame(SingletonWindowInfo svi){
194
                WindowInfo vi = (WindowInfo) contentWindowInfo.get(svi);
195
                return (JInternalFrame) contentFrame.get(svi);
196
        }
197

    
198
        public Component getFrame(Class viewClass, Object model){
199
                SingletonWindowInfo svi = new SingletonWindowInfo(viewClass, model);
200
                return getFrame(svi);
201
        }
202

    
203
        /**
204
         * @param model
205
         * @return
206
         */
207
        public Component[] getFrames(Object model) {
208
                ArrayList ret = new ArrayList();
209

    
210
                ArrayList keys = contentFrame.getKeys();
211
                for (int i = 0; i < keys.size(); i++) {
212
                        SingletonWindowInfo svi = (SingletonWindowInfo) keys.get(i);
213

    
214
                        if (svi.modelo == model){
215
                                ret.add(contentFrame.get(svi));
216
                        }
217
                }
218

    
219
                return (JInternalFrame[]) ret.toArray(new JInternalFrame[0]);
220
        }
221

    
222
        /**
223
         * @param view
224
         * @return
225
         */
226
        public Component getFrame(SingletonWindow sv) {
227
                SingletonWindowInfo svi = new SingletonWindowInfo(sv.getClass(), sv.getWindowModel());
228
                return getFrame(svi);
229
        }
230

    
231
        /**
232
         * @param sv
233
         * @param i
234
         */
235
        public void setX(SingletonWindow sv, int x) {
236
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
237

    
238
        if (o == null) return;
239
        o.setLocation(x, o.getY());
240
        }
241

    
242
        /**
243
         * @param sv
244
         * @param i
245
         */
246
        public void setY(SingletonWindow sv, int y) {
247
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
248

    
249
        if (o == null) return;
250

    
251
        o.setLocation(o.getX(), y);
252
        }
253

    
254
        /**
255
         * @param sv
256
         * @param i
257
         */
258
        public void setHeight(SingletonWindow sv, int height) {
259
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
260

    
261
        if (o == null) return;
262

    
263
        o.setSize(o.getWidth(), height);
264
        }
265

    
266
        /**
267
         * @param sv
268
         * @param i
269
         */
270
        public void setWidth(SingletonWindow sv, int width) {
271
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
272

    
273
        if (o == null) return;
274
        o.setSize(width, o.getHeight());
275
        }
276

    
277
        /**
278
         * @param sw
279
         * @param maximized
280
         */
281
        public void setMaximized(SingletonWindow sw, boolean maximized) {
282
                JInternalFrame frame = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
283

    
284
        if (frame == null) return;
285
        try {
286
                        frame.setMaximum(maximized);
287
                } catch (PropertyVetoException e) {
288
                        // TODO Auto-generated catch block
289
                        //e.printStackTrace();
290
                }
291
        }
292

    
293
        /**
294
         * @param sw
295
         * @param maximized
296
         */
297
        public void setNormalBounds(SingletonWindow sw, Rectangle normalBounds) {
298
                JInternalFrame frame = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
299

    
300
        if (frame == null) return;
301
        frame.setNormalBounds(normalBounds);
302
        }
303

    
304
        /**
305
         * Sets the minimum allowed size for the provided singleton window.
306
         *
307
         * @param sw
308
         * @param minSize
309
         */
310
        public void setMinimumSize(SingletonWindow sw, Dimension minSize) {
311
                JInternalFrame frame = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
312

    
313
        if (frame == null) return;
314
        frame.setMinimumSize(minSize);
315
        }
316

    
317
        /**
318
         * @param sv
319
         * @param string
320
         */
321
        public void setTitle(SingletonWindow sv, String title) {
322
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
323

    
324
        if (o == null) return;
325
        o.setTitle(title);
326
        }
327

    
328
        private class HashMap {
329
            private ArrayList keys = new ArrayList();
330
            private ArrayList values = new ArrayList();
331

    
332
            public void put(SingletonWindowInfo key, Object value) {
333
                int index = -1;
334
                for (int i = 0; i < keys.size(); i++) {
335
                    if (keys.get(i).equals(key)){
336
                        index = i;
337
                        break;
338
                    }
339
            }
340

    
341
                if (index != -1){
342
                    keys.add(index, key);
343
                    values.add(index, value);
344
                }else{
345
                    keys.add(key);
346
                    values.add(value);
347
                }
348
            }
349

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

    
357
                return false;
358
            }
359

    
360
            public Object get(SingletonWindowInfo key){
361
                for (int i = 0; i < keys.size(); i++) {
362
                    if (keys.get(i).equals(key)){
363
                        return values.get(i);
364
                    }
365
                }
366

    
367
                return null;
368
            }
369

    
370
            public void remove(SingletonWindowInfo key){
371
                for (int i = 0; i < keys.size(); i++) {
372
                    if (keys.get(i).equals(key)){
373
                        keys.remove(i);
374
                        values.remove(i);
375
                    }
376
                }
377
            }
378

    
379
            public ArrayList getKeys(){
380
                return keys;
381
            }
382
        }
383
}