Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCorePlugin / src / com / iver / core / mdiManager / SingletonViewSupport.java @ 1836

History | View | Annotate | Download (7.91 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.View;
55
import com.iver.andami.ui.mdiManager.ViewInfo;
56

    
57

    
58
/**
59
 * DOCUMENT ME!
60
 *
61
 * @author $author$
62
 * @version $Revision: 1836 $
63
 */
64
public class SingletonViewSupport {
65
        private static int singletonViewInfoID = 0;
66
        /** Hashtable que asocia contenido con vistas */
67
        private TreeMap contentViewInfo = new TreeMap(new SingletonViewInfoComparator());
68
        private ViewInfoSupport vis;
69
        private FrameViewSupport frameViewSupport;
70
        private TreeMap contentFrame = new TreeMap(new SingletonViewInfoComparator());
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
        /**
192
         * DOCUMENT ME!
193
         *
194
         * @author $author$
195
         * @version $Revision: 1836 $
196
         */
197
        public class SingletonViewInfoComparator implements Comparator {
198
                /**
199
                 * @see java.util.Comparator#compare(java.lang.Object,
200
                 *                 java.lang.Object)
201
                 */
202
                public int compare(Object o1, Object o2) {
203
                        try{
204
                        SingletonViewInfo s1 = (SingletonViewInfo) o1;
205
                        SingletonViewInfo s2 = (SingletonViewInfo) o2;
206

    
207
                        if ((s1.clase.getName().equals(s2.clase.getName())) &&
208
                                        (s1.modelo == s2.modelo)) {
209
                                return 0;
210
                        } else {
211
                                return (s1.id < s2.id)?-1:1;
212
                        }
213
                        }catch(ClassCastException e){
214
                                e.printStackTrace();
215
                                return 0;
216
                        }
217
                }
218
        }
219

    
220
        private JInternalFrame getFrame(SingletonViewInfo svi){
221
                ViewInfo vi = (ViewInfo) contentViewInfo.get(svi);
222
                return (JInternalFrame) contentFrame.get(svi);
223
        }
224
        
225
        public JInternalFrame getFrame(Class viewClass, Object model){
226
                SingletonViewInfo svi = new SingletonViewInfo(viewClass, model);
227
                return getFrame(svi);
228
        }
229

    
230
        /**
231
         * @param model
232
         * @return
233
         */
234
        public JInternalFrame[] getFrames(Object model) {
235
                ArrayList ret = new ArrayList();
236
                
237
                Iterator keys = contentFrame.keySet().iterator();
238
                while (keys.hasNext()) {
239
                        SingletonViewInfo svi = (SingletonViewInfo) keys.next();
240
                        
241
                        if (svi.modelo == model){
242
                                ret.add(contentFrame.get(svi));
243
                        }
244
                }
245
                
246
                return (JInternalFrame[]) ret.toArray(new JInternalFrame[0]);
247
        }
248

    
249
        /**
250
         * @param view
251
         * @return
252
         */
253
        public JInternalFrame getFrame(SingletonView sv) {
254
                SingletonViewInfo svi = new SingletonViewInfo(sv.getClass(), sv.getViewModel());
255
                return getFrame(svi);
256
        }
257

    
258
        /**
259
         * @param sv
260
         * @param i
261
         */
262
        public void setX(SingletonView sv, int x) {
263
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
264

    
265
        if (o == null) return;
266
        o.setLocation(x, o.getY());
267
        }
268

    
269
        /**
270
         * @param sv
271
         * @param i
272
         */
273
        public void setY(SingletonView sv, int y) {
274
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
275

    
276
        if (o == null) return;
277
        
278
        o.setLocation(o.getX(), y);
279
        }
280

    
281
        /**
282
         * @param sv
283
         * @param i
284
         */
285
        public void setHeight(SingletonView sv, int height) {
286
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
287

    
288
        if (o == null) return;
289
            
290
        o.setSize(o.getWidth(), height);
291
        }
292

    
293
        /**
294
         * @param sv
295
         * @param i
296
         */
297
        public void setWidth(SingletonView sv, int width) {
298
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
299

    
300
        if (o == null) return;
301
        o.setSize(width, o.getHeight());
302
        }
303

    
304
        /**
305
         * @param sv
306
         * @param string
307
         */
308
        public void setTitle(SingletonView sv, String title) {
309
                JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonViewInfo(sv.getClass(), sv.getViewModel()));
310

    
311
        if (o == null) return;
312
        o.setTitle(title);
313
        }
314
}