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