Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / ext3Dgui / src / com / iver / ai2 / gvsig3dgui / skin / JDialogSingletonWindowSupport.java @ 16096

History | View | Annotate | Download (8.85 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.ai2.gvsig3dgui.skin;
42

    
43
import java.awt.Window;
44
import java.util.ArrayList;
45

    
46
import javax.swing.JDialog;
47

    
48
import com.iver.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
49
import com.iver.andami.ui.mdiManager.SingletonWindow;
50
import com.iver.andami.ui.mdiManager.WindowInfo;
51
import com.iver.core.mdiManager.SingletonWindowSupport;
52

    
53

    
54
/**
55
 * DOCUMENT ME!
56
 *
57
 * @author $author$
58
 * @version $Revision: 1.1 $
59
 */
60
public class JDialogSingletonWindowSupport extends SingletonWindowSupport {
61

    
62

    
63
        private static int singletonViewInfoID = 0;
64
        /** Hashtable que asocia contenido con vistas */
65
        private HashMap contentWindowInfo = new HashMap();
66
        private JDialogWindowInfoSupport vis;
67
        private JDialogFrameWindowSupport frameWindowSupport;
68
        private HashMap contentFrame = new HashMap();
69

    
70
        public JDialogSingletonWindowSupport(JDialogWindowInfoSupport wis,
71
                        JDialogFrameWindowSupport fvs) {
72
                super(wis,fvs);
73
                this.vis = wis;
74
                this.frameWindowSupport = (JDialogFrameWindowSupport) fvs;
75
        }
76

    
77
        
78

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

    
95
                if (contentWindowInfo.containsKey(swi)) {
96
                        if (wi.isModal()) {
97
                                throw new SingletonDialogAlreadyShownException();
98
                        }
99

    
100
                        wi.setWindowInfo((WindowInfo)contentWindowInfo.get(swi));
101

    
102
                        return true;
103
                } else {
104
                        //La ventana singleton no estaba mostrada
105
                        //Se asocia el modelo con la vista
106
                        contentWindowInfo.put(swi, wi);
107
                        return false;
108
                }
109
        }
110

    
111
        public void openSingletonWindow(SingletonWindow sw, Window frame){
112
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
113
                contentFrame.put(swi, frame);
114
        }
115

    
116
        /**
117
         * DOCUMENT ME!
118
         *
119
         * @param sw
120
         */
121
        public void closeWindow(SingletonWindow sw) {
122
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
123
                WindowInfo windowInfo = (WindowInfo) contentWindowInfo.get(swi);
124
                if (windowInfo!=null) {
125
                        JDialog frame = (JDialog) contentFrame.get(swi);
126
                        windowInfo.setWidth(frame.getWidth());
127
                        windowInfo.setHeight(frame.getHeight());
128
                        windowInfo.setX(frame.getX());
129
                        windowInfo.setY(frame.getY());
130
                        windowInfo.setClosed(true);
131
                /*        if ((frame.getExtendedState() & JFrame.MAXIMIZED_BOTH) == JFrame.MAXIMIZED_BOTH) {
132
                                windowInfo.setMaximized(true);
133
                        }
134
                        else {
135
                                windowInfo.setMaximized(false);
136
                                windowInfo.setNormalBounds(frame.getBounds());
137
                        }*/
138
                }
139
                contentFrame.remove(swi);
140
        }
141

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

    
150
                public int id;
151

    
152
                /** Clase de la vista */
153
                public Class clase;
154

    
155
                /** Modelo que representa la vista */
156
                public Object modelo;
157

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

    
171
                /**
172
                 * @see java.lang.Object#equals(java.lang.Object)
173
                 */
174
                public boolean equals(Object obj) {
175
                        if (obj.getClass() != SingletonWindowInfo.class) {
176
                                throw new IllegalArgumentException();
177
                        }
178

    
179
                        SingletonWindowInfo s = (SingletonWindowInfo) obj;
180

    
181
                        if ((clase == s.clase) && (modelo == s.modelo)) {
182
                                return true;
183
                        } else {
184
                                return false;
185
                        }
186
                }
187
        }
188

    
189
        private JDialog getFrame(SingletonWindowInfo svi){
190
                WindowInfo vi = (WindowInfo) contentWindowInfo.get(svi);
191
                return (JDialog) contentFrame.get(svi);
192
        }
193

    
194
        public JDialog getFrame(Class viewClass, Object model){
195
                SingletonWindowInfo svi = new SingletonWindowInfo(viewClass, model);
196
                return getFrame(svi);
197
        }
198

    
199
        /**
200
         * @param model
201
         * @return
202
         */
203
        public JDialog[] getFrames(Object model) {
204
                ArrayList ret = new ArrayList();
205

    
206
                ArrayList keys = contentFrame.getKeys();
207
                for (int i = 0; i < keys.size(); i++) {
208
                        SingletonWindowInfo svi = (SingletonWindowInfo) keys.get(i);
209

    
210
                        if (svi.modelo == model){
211
                                ret.add(contentFrame.get(svi));
212
                        }
213
                }
214

    
215
                return (JDialog[]) ret.toArray(new JDialog[0]);
216
        }
217
        
218
        /**
219
         * @param sv
220
         * @param i
221
         */
222
        public void setX(SingletonWindow sw, int x) {
223
                JDialog frame = (JDialog) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
224

    
225
        if (frame == null) return;
226
        frame.setLocation(x, frame.getY());
227
        }
228
        
229
        
230
        /**
231
         * @param sv
232
         * @param i
233
         */
234
        public void setY(SingletonWindow sw, int y) {
235
                JDialog frame = (JDialog) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
236

    
237
        if (frame == null) return;
238

    
239
        frame.setLocation(frame.getX(), y);
240
        }
241

    
242
        /**
243
         * @param sv
244
         * @param i
245
         */
246
        public void setHeight(SingletonWindow sw, int height) {
247
                JDialog frame = (JDialog) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
248

    
249
        if (frame == null) return;
250

    
251
        frame.setSize(frame.getWidth(), height);
252
        }
253

    
254
        /**
255
         * @param sv
256
         * @param i
257
         */
258
        public void setWidth(SingletonWindow sw, int width) {
259
                JDialog frame = (JDialog) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
260

    
261
        if (frame == null) return;
262
        frame.setSize(width, frame.getHeight());
263
        }
264

    
265
        /**
266
         * @param sw
267
         * @param maximized
268
         */
269
        public void setMaximized(SingletonWindow sw, boolean maximized) {
270
                JDialog frame = (JDialog) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
271

    
272
     /*   if (frame == null) return;
273
     
274
        if (maximized) {
275
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
276
        }
277
        else {
278
                frame.setExtendedState(JFrame.NORMAL);
279
        } */
280
        }
281

    
282

    
283
        /**
284
         * @param sv
285
         * @param string
286
         */
287
        public void setTitle(SingletonWindow sw, String title) {
288
                JDialog frame = (JDialog) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
289

    
290
        if (frame == null) return;
291
        frame.setTitle(title);
292
        }
293

    
294
        private class HashMap {
295
            private ArrayList keys = new ArrayList();
296
            private ArrayList values = new ArrayList();
297

    
298
            public void put(SingletonWindowInfo key, Object value) {
299
                int index = -1;
300
                for (int i = 0; i < keys.size(); i++) {
301
                    if (keys.get(i).equals(key)){
302
                        index = i;
303
                        break;
304
                    }
305
            }
306

    
307
                if (index != -1){
308
                    keys.add(index, key);
309
                    values.add(index, value);
310
                }else{
311
                    keys.add(key);
312
                    values.add(value);
313
                }
314
            }
315

    
316
            public boolean containsKey(SingletonWindowInfo key){
317
                for (int i = 0; i < keys.size(); i++) {
318
                    if (keys.get(i).equals(key)){
319
                        return true;
320
                    }
321
                }
322

    
323
                return false;
324
            }
325

    
326
            public Object get(SingletonWindowInfo key){
327
                for (int i = 0; i < keys.size(); i++) {
328
                    if (keys.get(i).equals(key)){
329
                        return values.get(i);
330
                    }
331
                }
332

    
333
                return null;
334
            }
335

    
336
            public void remove(SingletonWindowInfo key){
337
                for (int i = 0; i < keys.size(); i++) {
338
                    if (keys.get(i).equals(key)){
339
                        keys.remove(i);
340
                        values.remove(i);
341
                    }
342
                }
343
            }
344

    
345
            public ArrayList getKeys(){
346
                return keys;
347
            }
348
        }
349
}