Statistics
| Revision:

gvsig-projects-pool / org.gvsig.winmgr / trunk / org.gvsig.winmgr.app / org.gvsig.winmgr.app.mainplugin / src / main / java / org / gvsig / coreplugin / mdiManager / SingletonWindowSupport.java @ 682

History | View | Annotate | Download (9.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.coreplugin.mdiManager;
25

    
26
import java.awt.Component;
27
import java.awt.Dimension;
28
import java.awt.Rectangle;
29
import java.beans.PropertyVetoException;
30
import java.util.ArrayList;
31

    
32
import javax.swing.JComponent;
33
import javax.swing.JInternalFrame;
34

    
35
import org.gvsig.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
36
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
37
import org.gvsig.andami.ui.mdiManager.WindowInfo;
38

    
39

    
40

    
41
/**
42
 * DOCUMENT ME!
43
 *
44
 * @author $author$
45
 * @version $Revision: 31344 $
46
 */
47
public class SingletonWindowSupport {
48
        private static int singletonViewInfoID = 0;
49
        /** Hashtable que asocia contenido con vistas */
50
        private HashMap contentWindowInfo = new HashMap();
51
        private WindowInfoSupport vis;
52
        private FrameWindowSupport frameWindowSupport;
53
        private HashMap contentFrame = new HashMap();
54

    
55
        /**
56
         * DOCUMENT ME!
57
         *
58
         * @param vis DOCUMENT ME!
59
         * @param fvs
60
         *
61
         * @see org.gvsig.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
62
         */
63
        public SingletonWindowSupport(WindowInfoSupport vis, FrameWindowSupport fvs) {
64
                this.vis = vis;
65
                this.frameWindowSupport = fvs;
66
        }
67

    
68
        /**
69
         *
70
         * If the window existed, the 'wi' parameter is updated with the
71
         * previous window info (this allows to show the window in the previous
72
         * place), but the title of the window is updated using the title
73
         * from the parameter.
74
         *  
75
         * @param windowClass DOCUMENT ME!
76
         * @param model DOCUMENT ME!
77
         * @param wi DOCUMENT ME!
78
         *
79
         * @return true si la vista existe ya y false si la vista no existe
80
         *
81
         * @throws SingletonDialogAlreadyShownException DOCUMENT ME!
82
         */
83
        public boolean registerWindow(Class windowClass, Object model, WindowInfo wi) {
84
                //Se comprueba si la ventana est? siendo mostrada
85
                SingletonWindowInfo swi = new SingletonWindowInfo(windowClass, model);
86

    
87
                if (contentWindowInfo.containsKey(swi)) {
88
                        if (wi.isModal()) {
89
                                throw new SingletonDialogAlreadyShownException();
90
                        }
91

    
92
                        String new_tit = wi.getTitle();
93
                        WindowInfo previous_wi = (WindowInfo) contentWindowInfo.get(swi);
94
                        previous_wi.setTitle(new_tit);
95
                        wi.setWindowInfo(previous_wi);
96

    
97
                        return true;
98
                } else {
99
                        //La ventana singleton no estaba mostrada
100
                        //Se asocia el modelo con la vista
101
                        contentWindowInfo.put(swi, wi);
102
                        return false;
103
                }
104
        }
105

    
106
        public void openSingletonWindow(SingletonWindow sw, Component frame){
107
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
108
                contentFrame.put(swi, frame);
109
        }
110

    
111
        public boolean contains(SingletonWindow sw){
112
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
113
                return contentFrame.containsKey(swi);
114
        }
115

    
116
        public boolean contains(Class windowClass, Object model){
117
                SingletonWindowInfo swi = new SingletonWindowInfo(windowClass, model);
118
                return contentFrame.containsKey(swi);
119
        }
120

    
121
        /**
122
         * DOCUMENT ME!
123
         *
124
         * @param sw
125
         */
126
        public void closeWindow(SingletonWindow sw) {
127
                SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
128
                WindowInfo windowInfo = (WindowInfo) contentWindowInfo.get(swi);
129
                if (windowInfo!=null) {
130
                        frameWindowSupport.updateWindowInfo(sw, windowInfo);
131
                }
132
                contentFrame.remove(swi);
133
        }
134

    
135
        /**
136
         * Representa una vista singleton manteniendo el modelo y la clase de la
137
         * vista que lo muestra
138
         *
139
         * @author Fernando Gonz?lez Cort?s
140
         */
141
        public class SingletonWindowInfo {
142

    
143
                public int id;
144

    
145
                /** Clase de la vista */
146
                public Class clase;
147

    
148
                /** Modelo que representa la vista */
149
                public Object modelo;
150

    
151
                /**
152
                 * Creates a new SingletonView object.
153
                 *
154
                 * @param clase Clase de la vista
155
                 * @param modelo Modelo que representa la vista
156
                 */
157
                public SingletonWindowInfo(Class clase, Object modelo) {
158
                        this.clase = clase;
159
                        this.modelo = modelo;
160
                        this.id = singletonViewInfoID;
161
                        singletonViewInfoID++;
162
                }
163

    
164
                /**
165
                 * @see java.lang.Object#equals(java.lang.Object)
166
                 */
167
                public boolean equals(Object obj) {
168
                        if (obj.getClass() != SingletonWindowInfo.class) {
169
                                throw new IllegalArgumentException();
170
                        }
171

    
172
                        SingletonWindowInfo s = (SingletonWindowInfo) obj;
173

    
174
                        if ((clase == s.clase) && (modelo == s.modelo)) {
175
                                return true;
176
                        } else {
177
                                return false;
178
                        }
179
                }
180
        }
181

    
182
        private Component getFrame(SingletonWindowInfo svi){
183
                WindowInfo vi = (WindowInfo) contentWindowInfo.get(svi);
184
                return (JInternalFrame) contentFrame.get(svi);
185
        }
186

    
187
        public Component getFrame(Class viewClass, Object model){
188
                SingletonWindowInfo svi = new SingletonWindowInfo(viewClass, model);
189
                return getFrame(svi);
190
        }
191

    
192
        /**
193
         * @param model
194
         * @return
195
         */
196
        public Component[] getFrames(Object model) {
197
                ArrayList ret = new ArrayList();
198

    
199
                ArrayList keys = contentFrame.getKeys();
200
                for (int i = 0; i < keys.size(); i++) {
201
                        SingletonWindowInfo svi = (SingletonWindowInfo) keys.get(i);
202

    
203
                        if (svi.modelo == model){
204
                                ret.add(contentFrame.get(svi));
205
                        }
206
                }
207

    
208
                return (JInternalFrame[]) ret.toArray(new JInternalFrame[0]);
209
        }
210

    
211
        /**
212
         * @param view
213
         * @return
214
         */
215
        public Component getFrame(SingletonWindow sv) {
216
                SingletonWindowInfo svi = new SingletonWindowInfo(sv.getClass(), sv.getWindowModel());
217
                return getFrame(svi);
218
        }
219

    
220
        /**
221
         * @param sv
222
         * @param i
223
         */
224
        public void setX(SingletonWindow sv, int x) {
225
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel()));
226

    
227
        if (o == null) return;
228
        o.setLocation(x, o.getY());
229
        }
230

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

    
238
        if (o == null) return;
239

    
240
        o.setLocation(o.getX(), y);
241
        }
242

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

    
250
        if (o == null) return;
251

    
252
        o.setSize(o.getWidth(), height);
253
        }
254

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

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

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

    
273
        if (frame == null) return;
274
        try {
275
                        frame.setMaximum(maximized);
276
                } catch (PropertyVetoException e) {
277
                        // TODO Auto-generated catch block
278
                        //e.printStackTrace();
279
                }
280
        }
281

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

    
289
        if (frame == null) return;
290
        frame.setNormalBounds(normalBounds);
291
        }
292

    
293
        /**
294
         * Sets the minimum allowed size for the provided singleton window.
295
         *
296
         * @param sw
297
         * @param minSize
298
         */
299
        public void setMinimumSize(SingletonWindow sw, Dimension minSize) {
300
                JInternalFrame frame = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel()));
301

    
302
        if (frame == null) return;
303
        frame.setMinimumSize(minSize);
304
        }
305

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

    
313
        if (o == null) return;
314
        o.setTitle(title);
315
        }
316

    
317
        private class HashMap {
318
            private ArrayList keys = new ArrayList();
319
            private ArrayList values = new ArrayList();
320

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

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

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

    
346
                return false;
347
            }
348

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

    
356
                return null;
357
            }
358

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

    
368
            public ArrayList getKeys(){
369
                return keys;
370
            }
371
        }
372
}