Statistics
| Revision:

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

History | View | Annotate | Download (8.62 KB)

1 1104 fjp
/* 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 592 fernando
package com.iver.core.mdiManager;
42
43 1836 fernando
import java.awt.Component;
44 1325 fernando
import java.util.ArrayList;
45 592 fernando
import java.util.Comparator;
46 1325 fernando
import java.util.Iterator;
47 592 fernando
import java.util.TreeMap;
48
49 975 fernando
import javax.swing.JComponent;
50 878 fjp
import javax.swing.JInternalFrame;
51
52 592 fernando
import com.iver.andami.ui.mdiManager.SingletonDialogAlreadyShownException;
53
import com.iver.andami.ui.mdiManager.SingletonView;
54 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
55 592 fernando
import com.iver.andami.ui.mdiManager.ViewInfo;
56
57
58
/**
59
 * DOCUMENT ME!
60
 *
61
 * @author $author$
62
 * @version $Revision$
63
 */
64
public class SingletonViewSupport {
65 1836 fernando
        private static int singletonViewInfoID = 0;
66 975 fernando
        /** Hashtable que asocia contenido con vistas */
67 2457 fernando
        private HashMap contentViewInfo = new HashMap();
68 975 fernando
        private ViewInfoSupport vis;
69
        private FrameViewSupport frameViewSupport;
70 2457 fernando
        private HashMap contentFrame = new HashMap();
71 975 fernando
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 592 fernando
85 975 fernando
        /**
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 592 fernando
101 975 fernando
                if (contentViewInfo.containsKey(svi)) {
102
                        if (vi.isModal()) {
103
                                throw new SingletonDialogAlreadyShownException();
104
                        }
105 592 fernando
106 975 fernando
                        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 592 fernando
117 975 fernando
        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 1325 fernando
                JInternalFrame c = (JInternalFrame) contentFrame.get(svi);
136 975 fernando
                viewInfo.setWidth(c.getWidth());
137
                viewInfo.setHeight(c.getHeight());
138
                viewInfo.setX(c.getX());
139
                viewInfo.setY(c.getY());
140 2626 fjp
141 975 fernando
                contentFrame.remove(svi);
142
        }
143 1325 fernando
144 975 fernando
        /**
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 1836 fernando
152
                public int id;
153
154 975 fernando
                /** Clase de la vista */
155
                public Class clase;
156 592 fernando
157 975 fernando
                /** 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 1836 fernando
                        this.id = singletonViewInfoID;
170
                        singletonViewInfoID++;
171 975 fernando
                }
172 592 fernando
173 2457 fernando
                /**
174 975 fernando
                 * @see java.lang.Object#equals(java.lang.Object)
175 2457 fernando
                 */
176 975 fernando
                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 1325 fernando
        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 975 fernando
        /**
202 1325 fernando
         * @param model
203
         * @return
204
         */
205
        public JInternalFrame[] getFrames(Object model) {
206
                ArrayList ret = new ArrayList();
207
208 2457 fernando
                ArrayList keys = contentFrame.getKeys();
209
                for (int i = 0; i < keys.size(); i++) {
210
                        SingletonViewInfo svi = (SingletonViewInfo) keys.get(i);
211 1325 fernando
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 975 fernando
         * @param view
222
         * @return
223
         */
224 1325 fernando
        public JInternalFrame getFrame(SingletonView sv) {
225 975 fernando
                SingletonViewInfo svi = new SingletonViewInfo(sv.getClass(), sv.getViewModel());
226 1325 fernando
                return getFrame(svi);
227 975 fernando
        }
228
229
        /**
230
         * @param sv
231
         * @param i
232
         */
233
        public void setX(SingletonView sv, int x) {
234 1325 fernando
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
235 975 fernando
236 1044 fernando
        if (o == null) return;
237 1325 fernando
        o.setLocation(x, o.getY());
238 975 fernando
        }
239 592 fernando
240 975 fernando
        /**
241
         * @param sv
242
         * @param i
243
         */
244
        public void setY(SingletonView sv, int y) {
245 1325 fernando
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
246 592 fernando
247 1044 fernando
        if (o == null) return;
248
249 1325 fernando
        o.setLocation(o.getX(), y);
250 975 fernando
        }
251 592 fernando
252 975 fernando
        /**
253
         * @param sv
254
         * @param i
255
         */
256
        public void setHeight(SingletonView sv, int height) {
257 1325 fernando
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
258 975 fernando
259 1044 fernando
        if (o == null) return;
260 1325 fernando
261
        o.setSize(o.getWidth(), height);
262 975 fernando
        }
263 592 fernando
264 975 fernando
        /**
265
         * @param sv
266
         * @param i
267
         */
268
        public void setWidth(SingletonView sv, int width) {
269 1325 fernando
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
270 592 fernando
271 1044 fernando
        if (o == null) return;
272 1325 fernando
        o.setSize(width, o.getHeight());
273 975 fernando
        }
274 592 fernando
275
        /**
276 975 fernando
         * @param sv
277
         * @param string
278 592 fernando
         */
279 975 fernando
        public void setTitle(SingletonView sv, String title) {
280 1325 fernando
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
281 975 fernando
282 1044 fernando
        if (o == null) return;
283 1325 fernando
        o.setTitle(title);
284 592 fernando
        }
285 2457 fernando
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 592 fernando
}