root / trunk / libraries / libCorePlugin / src / com / iver / core / mdiManager / SingletonWindowSupport.java @ 8527
History | View | Annotate | Download (9.62 KB)
1 | 6892 | cesar | /* 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 | 8321 | cesar | import java.awt.Rectangle; |
45 | import java.beans.PropertyVetoException; |
||
46 | 6892 | cesar | import java.util.ArrayList; |
47 | import java.util.Comparator; |
||
48 | import java.util.Iterator; |
||
49 | import java.util.TreeMap; |
||
50 | |||
51 | import javax.swing.JComponent; |
||
52 | import javax.swing.JInternalFrame; |
||
53 | |||
54 | import com.iver.andami.ui.mdiManager.SingletonDialogAlreadyShownException; |
||
55 | import com.iver.andami.ui.mdiManager.SingletonWindow; |
||
56 | import com.iver.andami.ui.mdiManager.IWindow; |
||
57 | import com.iver.andami.ui.mdiManager.WindowInfo; |
||
58 | |||
59 | |||
60 | /**
|
||
61 | * DOCUMENT ME!
|
||
62 | *
|
||
63 | * @author $author$
|
||
64 | * @version $Revision$
|
||
65 | */
|
||
66 | public class SingletonWindowSupport { |
||
67 | private static int singletonViewInfoID = 0; |
||
68 | /** Hashtable que asocia contenido con vistas */
|
||
69 | private HashMap contentWindowInfo = new HashMap(); |
||
70 | private WindowInfoSupport vis;
|
||
71 | private FrameWindowSupport frameWindowSupport;
|
||
72 | private HashMap contentFrame = new HashMap(); |
||
73 | 8527 | jaume | |
74 | 6892 | cesar | /**
|
75 | * DOCUMENT ME!
|
||
76 | *
|
||
77 | * @param vis DOCUMENT ME!
|
||
78 | * @param fvs
|
||
79 | *
|
||
80 | * @see com.iver.andami.ui.mdiManager.MDIManager#init(com.iver.andami.ui.mdiFrame.MDIFrame)
|
||
81 | */
|
||
82 | public SingletonWindowSupport(WindowInfoSupport vis, FrameWindowSupport fvs) {
|
||
83 | this.vis = vis;
|
||
84 | this.frameWindowSupport = fvs;
|
||
85 | } |
||
86 | |||
87 | /**
|
||
88 | * Devuelve una referencia a la vista si ya est? mostrada o null si la
|
||
89 | * vista no ha sido a?adida o ya fue cerrada
|
||
90 | *
|
||
91 | * @param windowClass DOCUMENT ME!
|
||
92 | * @param model DOCUMENT ME!
|
||
93 | * @param wi DOCUMENT ME!
|
||
94 | *
|
||
95 | * @return true si la vista existe ya y false si la vista no existe
|
||
96 | *
|
||
97 | * @throws SingletonDialogAlreadyShownException DOCUMENT ME!
|
||
98 | */
|
||
99 | public boolean registerWindow(Class windowClass, Object model, WindowInfo wi) { |
||
100 | 8190 | cesar | //Se comprueba si la ventana est? siendo mostrada
|
101 | SingletonWindowInfo swi = new SingletonWindowInfo(windowClass, model);
|
||
102 | 6892 | cesar | |
103 | 8190 | cesar | if (contentWindowInfo.containsKey(swi)) {
|
104 | 6892 | cesar | if (wi.isModal()) {
|
105 | throw new SingletonDialogAlreadyShownException(); |
||
106 | } |
||
107 | |||
108 | 8190 | cesar | wi.setWindowInfo((WindowInfo)contentWindowInfo.get(swi)); |
109 | 8527 | jaume | |
110 | 6892 | cesar | return true; |
111 | } else {
|
||
112 | 8190 | cesar | //La ventana singleton no estaba mostrada
|
113 | 6892 | cesar | //Se asocia el modelo con la vista
|
114 | 8190 | cesar | contentWindowInfo.put(swi, wi); |
115 | 6892 | cesar | return false; |
116 | } |
||
117 | } |
||
118 | |||
119 | 8190 | cesar | public void openSingletonWindow(SingletonWindow sw, JComponent frame){ |
120 | SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
|
||
121 | contentFrame.put(swi, frame); |
||
122 | 6892 | cesar | } |
123 | 8527 | jaume | |
124 | 6892 | cesar | public boolean contains(SingletonWindow sw){ |
125 | 8190 | cesar | SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
|
126 | return contentFrame.containsKey(swi);
|
||
127 | 6892 | cesar | } |
128 | 8527 | jaume | |
129 | 6892 | cesar | /**
|
130 | * DOCUMENT ME!
|
||
131 | *
|
||
132 | 8190 | cesar | * @param sw
|
133 | 6892 | cesar | */
|
134 | 8190 | cesar | public void closeWindow(SingletonWindow sw) { |
135 | SingletonWindowInfo swi = new SingletonWindowInfo(sw.getClass(), sw.getWindowModel());
|
||
136 | WindowInfo viewInfo = (WindowInfo) contentWindowInfo.get(swi); |
||
137 | 8527 | jaume | if (viewInfo!=null) { |
138 | JInternalFrame c = (JInternalFrame) contentFrame.get(swi); |
||
139 | viewInfo.setWidth(c.getWidth()); |
||
140 | viewInfo.setHeight(c.getHeight()); |
||
141 | viewInfo.setX(c.getX()); |
||
142 | viewInfo.setY(c.getY()); |
||
143 | viewInfo.setClosed(true);
|
||
144 | viewInfo.setNormalBounds(c.getNormalBounds()); |
||
145 | viewInfo.setMaximized(c.isMaximum()); |
||
146 | } |
||
147 | 8190 | cesar | contentFrame.remove(swi); |
148 | 6892 | cesar | } |
149 | 8527 | jaume | |
150 | 6892 | cesar | /**
|
151 | * Representa una vista singleton manteniendo el modelo y la clase de la
|
||
152 | * vista que lo muestra
|
||
153 | *
|
||
154 | * @author Fernando Gonz?lez Cort?s
|
||
155 | */
|
||
156 | 8190 | cesar | public class SingletonWindowInfo { |
157 | 8527 | jaume | |
158 | 6892 | cesar | public int id; |
159 | 8527 | jaume | |
160 | 6892 | cesar | /** Clase de la vista */
|
161 | public Class clase; |
||
162 | |||
163 | /** Modelo que representa la vista */
|
||
164 | public Object modelo; |
||
165 | 8527 | jaume | |
166 | 6892 | cesar | /**
|
167 | * Creates a new SingletonView object.
|
||
168 | *
|
||
169 | * @param clase Clase de la vista
|
||
170 | * @param modelo Modelo que representa la vista
|
||
171 | */
|
||
172 | 8190 | cesar | public SingletonWindowInfo(Class clase, Object modelo) { |
173 | 6892 | cesar | this.clase = clase;
|
174 | this.modelo = modelo;
|
||
175 | this.id = singletonViewInfoID;
|
||
176 | singletonViewInfoID++; |
||
177 | } |
||
178 | |||
179 | /**
|
||
180 | * @see java.lang.Object#equals(java.lang.Object)
|
||
181 | */
|
||
182 | public boolean equals(Object obj) { |
||
183 | 8190 | cesar | if (obj.getClass() != SingletonWindowInfo.class) {
|
184 | 6892 | cesar | throw new IllegalArgumentException(); |
185 | } |
||
186 | |||
187 | 8190 | cesar | SingletonWindowInfo s = (SingletonWindowInfo) obj; |
188 | 6892 | cesar | |
189 | if ((clase == s.clase) && (modelo == s.modelo)) {
|
||
190 | return true; |
||
191 | } else {
|
||
192 | return false; |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | 8190 | cesar | private JInternalFrame getFrame(SingletonWindowInfo svi){ |
198 | 6892 | cesar | WindowInfo vi = (WindowInfo) contentWindowInfo.get(svi); |
199 | return (JInternalFrame) contentFrame.get(svi); |
||
200 | } |
||
201 | 8527 | jaume | |
202 | 6892 | cesar | public JInternalFrame getFrame(Class viewClass, Object model){ |
203 | 8190 | cesar | SingletonWindowInfo svi = new SingletonWindowInfo(viewClass, model);
|
204 | 6892 | cesar | return getFrame(svi);
|
205 | } |
||
206 | |||
207 | /**
|
||
208 | * @param model
|
||
209 | * @return
|
||
210 | */
|
||
211 | public JInternalFrame[] getFrames(Object model) { |
||
212 | ArrayList ret = new ArrayList(); |
||
213 | 8527 | jaume | |
214 | 6892 | cesar | ArrayList keys = contentFrame.getKeys();
|
215 | for (int i = 0; i < keys.size(); i++) { |
||
216 | 8190 | cesar | SingletonWindowInfo svi = (SingletonWindowInfo) keys.get(i); |
217 | 8527 | jaume | |
218 | 6892 | cesar | if (svi.modelo == model){
|
219 | ret.add(contentFrame.get(svi)); |
||
220 | } |
||
221 | } |
||
222 | 8527 | jaume | |
223 | 6892 | cesar | return (JInternalFrame[]) ret.toArray(new JInternalFrame[0]); |
224 | } |
||
225 | |||
226 | /**
|
||
227 | * @param view
|
||
228 | * @return
|
||
229 | */
|
||
230 | public JInternalFrame getFrame(SingletonWindow sv) { |
||
231 | 8190 | cesar | SingletonWindowInfo svi = new SingletonWindowInfo(sv.getClass(), sv.getWindowModel());
|
232 | 6892 | cesar | return getFrame(svi);
|
233 | } |
||
234 | |||
235 | /**
|
||
236 | * @param sv
|
||
237 | * @param i
|
||
238 | */
|
||
239 | public void setX(SingletonWindow sv, int x) { |
||
240 | 8190 | cesar | JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel())); |
241 | 6892 | cesar | |
242 | if (o == null) return; |
||
243 | o.setLocation(x, o.getY()); |
||
244 | } |
||
245 | |||
246 | /**
|
||
247 | * @param sv
|
||
248 | * @param i
|
||
249 | */
|
||
250 | public void setY(SingletonWindow sv, int y) { |
||
251 | 8190 | cesar | JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel())); |
252 | 6892 | cesar | |
253 | if (o == null) return; |
||
254 | 8527 | jaume | |
255 | 6892 | cesar | o.setLocation(o.getX(), y); |
256 | } |
||
257 | |||
258 | /**
|
||
259 | * @param sv
|
||
260 | * @param i
|
||
261 | */
|
||
262 | public void setHeight(SingletonWindow sv, int height) { |
||
263 | 8190 | cesar | JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel())); |
264 | 6892 | cesar | |
265 | if (o == null) return; |
||
266 | 8527 | jaume | |
267 | 6892 | cesar | o.setSize(o.getWidth(), height); |
268 | } |
||
269 | |||
270 | /**
|
||
271 | * @param sv
|
||
272 | * @param i
|
||
273 | */
|
||
274 | public void setWidth(SingletonWindow sv, int width) { |
||
275 | 8190 | cesar | JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel())); |
276 | 6892 | cesar | |
277 | if (o == null) return; |
||
278 | o.setSize(width, o.getHeight()); |
||
279 | } |
||
280 | |||
281 | /**
|
||
282 | 8321 | cesar | * @param sw
|
283 | * @param maximized
|
||
284 | */
|
||
285 | public void setMaximized(SingletonWindow sw, boolean maximized) { |
||
286 | JInternalFrame frame = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel())); |
||
287 | |||
288 | if (frame == null) return; |
||
289 | try {
|
||
290 | frame.setMaximum(maximized); |
||
291 | } catch (PropertyVetoException e) { |
||
292 | // TODO Auto-generated catch block
|
||
293 | //e.printStackTrace();
|
||
294 | } |
||
295 | } |
||
296 | 8527 | jaume | |
297 | 8321 | cesar | /**
|
298 | * @param sw
|
||
299 | * @param maximized
|
||
300 | */
|
||
301 | public void setNormalBounds(SingletonWindow sw, Rectangle normalBounds) { |
||
302 | JInternalFrame frame = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sw.getClass(), sw.getWindowModel())); |
||
303 | |||
304 | if (frame == null) return; |
||
305 | frame.setNormalBounds(normalBounds); |
||
306 | } |
||
307 | 8527 | jaume | |
308 | 8321 | cesar | /**
|
309 | 6892 | cesar | * @param sv
|
310 | * @param string
|
||
311 | */
|
||
312 | public void setTitle(SingletonWindow sv, String title) { |
||
313 | 8190 | cesar | JInternalFrame o = (JInternalFrame) contentFrame.get(new SingletonWindowInfo(sv.getClass(), sv.getWindowModel())); |
314 | 6892 | cesar | |
315 | if (o == null) return; |
||
316 | o.setTitle(title); |
||
317 | } |
||
318 | 8527 | jaume | |
319 | 6892 | cesar | private class HashMap { |
320 | private ArrayList keys = new ArrayList(); |
||
321 | private ArrayList values = new ArrayList(); |
||
322 | 8527 | jaume | |
323 | 8190 | cesar | public void put(SingletonWindowInfo key, Object value) { |
324 | 6892 | cesar | int index = -1; |
325 | for (int i = 0; i < keys.size(); i++) { |
||
326 | if (keys.get(i).equals(key)){
|
||
327 | index = i; |
||
328 | break;
|
||
329 | } |
||
330 | } |
||
331 | 8527 | jaume | |
332 | 6892 | cesar | if (index != -1){ |
333 | keys.add(index, key); |
||
334 | values.add(index, value); |
||
335 | }else{
|
||
336 | keys.add(key); |
||
337 | values.add(value); |
||
338 | } |
||
339 | } |
||
340 | 8527 | jaume | |
341 | 8190 | cesar | public boolean containsKey(SingletonWindowInfo key){ |
342 | 6892 | cesar | for (int i = 0; i < keys.size(); i++) { |
343 | if (keys.get(i).equals(key)){
|
||
344 | return true; |
||
345 | } |
||
346 | } |
||
347 | 8527 | jaume | |
348 | 6892 | cesar | return false; |
349 | } |
||
350 | 8527 | jaume | |
351 | 8190 | cesar | public Object get(SingletonWindowInfo key){ |
352 | 6892 | cesar | for (int i = 0; i < keys.size(); i++) { |
353 | if (keys.get(i).equals(key)){
|
||
354 | return values.get(i);
|
||
355 | } |
||
356 | } |
||
357 | 8527 | jaume | |
358 | 6892 | cesar | return null; |
359 | } |
||
360 | 8527 | jaume | |
361 | 8190 | cesar | public void remove(SingletonWindowInfo key){ |
362 | 6892 | cesar | for (int i = 0; i < keys.size(); i++) { |
363 | if (keys.get(i).equals(key)){
|
||
364 | keys.remove(i); |
||
365 | values.remove(i); |
||
366 | } |
||
367 | } |
||
368 | } |
||
369 | 8527 | jaume | |
370 | 6892 | cesar | public ArrayList getKeys(){ |
371 | return keys;
|
||
372 | } |
||
373 | } |
||
374 | } |