Statistics
| Revision:

root / trunk / libraries / libCorePlugin / src / com / iver / core / mdiManager / SingletonViewSupport.java @ 6877

History | View | Annotate | Download (8.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.util.ArrayList;
45
import java.util.Comparator;
46
import java.util.Iterator;
47
import java.util.TreeMap;
48

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

    
52
import com.iver.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
53
import com.iver.andami.ui.mdiManager.SingletonView;
54
import com.iver.andami.ui.mdiManager.IWindow;
55
import com.iver.andami.ui.mdiManager.ViewInfo;
56

    
57

    
58
/**
59
 * DOCUMENT ME!
60
 *
61
 * @author $author$
62
 * @version $Revision: 6877 $
63
 */
64
public class SingletonViewSupport {
65
        private static int singletonViewInfoID = 0;
66
        /** Hashtable que asocia contenido con vistas */
67
        private HashMap contentViewInfo = new HashMap();
68
        private ViewInfoSupport vis;
69
        private FrameViewSupport frameViewSupport;
70
        private HashMap contentFrame = new HashMap();
71
        
72
        /**
73
         * DOCUMENT ME!
74
         *
75
         * @param vis DOCUMENT ME!
76
         * @param fvs
77
         *
78
         * @see com.iver.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
79
         */
80
        public SingletonViewSupport(ViewInfoSupport vis, FrameViewSupport fvs) {
81
                this.vis = vis;
82
                this.frameViewSupport = 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 viewClass DOCUMENT ME!
90
         * @param model DOCUMENT ME!
91
         * @param vi 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 registerView(Class viewClass, Object model, ViewInfo vi) {
98
                //Se comprueba si la vista est? siendo mostrada
99
                SingletonViewInfo svi = new SingletonViewInfo(viewClass, model);
100

    
101
                if (contentViewInfo.containsKey(svi)) {
102
                        if (vi.isModal()) {
103
                                throw new SingletonDialogAlreadyShownException();
104
                        }
105

    
106
                        vi.setViewInfo((ViewInfo)contentViewInfo.get(svi));
107
                        
108
                        return true;
109
                } else {
110
                        //La vista singleton no estaba mostrada
111
                        //Se asocia el modelo con la vista
112
                        contentViewInfo.put(svi, vi);
113
                        return false;
114
                }
115
        }
116

    
117
        public void openSingletonView(SingletonView sv, JComponent frame){
118
                SingletonViewInfo svi = new SingletonViewInfo(sv.getClass(), sv.getViewModel());
119
                contentFrame.put(svi, frame);
120
        }
121
        
122
        public boolean contains(SingletonView sv){
123
                SingletonViewInfo svi = new SingletonViewInfo(sv.getClass(), sv.getViewModel());
124
                return contentFrame.containsKey(svi);
125
        }
126
        
127
        /**
128
         * DOCUMENT ME!
129
         *
130
         * @param s
131
         */
132
        public void closeView(SingletonView s) {
133
                SingletonViewInfo svi = new SingletonViewInfo(s.getClass(), s.getViewModel());
134
                ViewInfo viewInfo = (ViewInfo) contentViewInfo.get(svi);
135
                JInternalFrame c = (JInternalFrame) contentFrame.get(svi); 
136
                viewInfo.setWidth(c.getWidth());
137
                viewInfo.setHeight(c.getHeight());
138
                viewInfo.setX(c.getX());
139
                viewInfo.setY(c.getY());
140
        
141
                contentFrame.remove(svi);
142
        }
143
        
144
        /**
145
         * Representa una vista singleton manteniendo el modelo y la clase de la
146
         * vista que lo muestra
147
         *
148
         * @author Fernando Gonz?lez Cort?s
149
         */
150
        public class SingletonViewInfo {
151
                
152
                public int id;
153
                
154
                /** Clase de la vista */
155
                public Class clase;
156

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

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

    
181
                        SingletonViewInfo s = (SingletonViewInfo) obj;
182

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

    
191
        private JInternalFrame getFrame(SingletonViewInfo svi){
192
                ViewInfo vi = (ViewInfo) contentViewInfo.get(svi);
193
                return (JInternalFrame) contentFrame.get(svi);
194
        }
195
        
196
        public JInternalFrame getFrame(Class viewClass, Object model){
197
                SingletonViewInfo svi = new SingletonViewInfo(viewClass, model);
198
                return getFrame(svi);
199
        }
200

    
201
        /**
202
         * @param model
203
         * @return
204
         */
205
        public JInternalFrame[] getFrames(Object model) {
206
                ArrayList ret = new ArrayList();
207
                
208
                ArrayList keys = contentFrame.getKeys();
209
                for (int i = 0; i < keys.size(); i++) {
210
                        SingletonViewInfo svi = (SingletonViewInfo) keys.get(i);
211
                        
212
                        if (svi.modelo == model){
213
                                ret.add(contentFrame.get(svi));
214
                        }
215
                }
216
                
217
                return (JInternalFrame[]) ret.toArray(new JInternalFrame[0]);
218
        }
219

    
220
        /**
221
         * @param view
222
         * @return
223
         */
224
        public JInternalFrame getFrame(SingletonView sv) {
225
                SingletonViewInfo svi = new SingletonViewInfo(sv.getClass(), sv.getViewModel());
226
                return getFrame(svi);
227
        }
228

    
229
        /**
230
         * @param sv
231
         * @param i
232
         */
233
        public void setX(SingletonView sv, int x) {
234
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
235

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

    
240
        /**
241
         * @param sv
242
         * @param i
243
         */
244
        public void setY(SingletonView sv, int y) {
245
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
246

    
247
        if (o == null) return;
248
        
249
        o.setLocation(o.getX(), y);
250
        }
251

    
252
        /**
253
         * @param sv
254
         * @param i
255
         */
256
        public void setHeight(SingletonView sv, int height) {
257
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
258

    
259
        if (o == null) return;
260
            
261
        o.setSize(o.getWidth(), height);
262
        }
263

    
264
        /**
265
         * @param sv
266
         * @param i
267
         */
268
        public void setWidth(SingletonView sv, int width) {
269
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
270

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

    
275
        /**
276
         * @param sv
277
         * @param string
278
         */
279
        public void setTitle(SingletonView sv, String title) {
280
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
281

    
282
        if (o == null) return;
283
        o.setTitle(title);
284
        }
285
        
286
        private class HashMap {
287
            private ArrayList keys = new ArrayList();
288
            private ArrayList values = new ArrayList();
289
            
290
            public void put(SingletonViewInfo key, Object value) {
291
                int index = -1;
292
                for (int i = 0; i < keys.size(); i++) {
293
                    if (keys.get(i).equals(key)){
294
                        index = i;
295
                        break;
296
                    }
297
            }
298
                
299
                if (index != -1){
300
                    keys.add(index, key);
301
                    values.add(index, value);
302
                }else{
303
                    keys.add(key);
304
                    values.add(value);
305
                }
306
            }
307
            
308
            public boolean containsKey(SingletonViewInfo key){
309
                for (int i = 0; i < keys.size(); i++) {
310
                    if (keys.get(i).equals(key)){
311
                        return true;
312
                    }
313
                }
314
                
315
                return false;
316
            }
317
            
318
            public Object get(SingletonViewInfo key){
319
                for (int i = 0; i < keys.size(); i++) {
320
                    if (keys.get(i).equals(key)){
321
                        return values.get(i);
322
                    }
323
                }
324
                
325
                return null;
326
            }
327
            
328
            public void remove(SingletonViewInfo key){
329
                for (int i = 0; i < keys.size(); i++) {
330
                    if (keys.get(i).equals(key)){
331
                        keys.remove(i);
332
                        values.remove(i);
333
                    }
334
                }
335
            }
336
            
337
            public ArrayList getKeys(){
338
                return keys;
339
            }
340
        }
341
}