svn-document-layout / branches / usability_v2 / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / DefaultLayoutContext.java @ 147
History | View | Annotate | Download (15.9 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
package org.gvsig.app.project.documents.layout; |
23 |
|
24 |
import java.awt.geom.AffineTransform; |
25 |
import java.lang.reflect.Array; |
26 |
import java.util.ArrayList; |
27 |
import java.util.Hashtable; |
28 |
|
29 |
import org.gvsig.andami.PluginServices; |
30 |
import org.gvsig.app.project.ProjectManager; |
31 |
import org.gvsig.app.project.documents.layout.commands.FrameCommandsRecord; |
32 |
import org.gvsig.app.project.documents.layout.commands.FrameManager; |
33 |
import org.gvsig.app.project.documents.layout.fframes.FFrame; |
34 |
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup; |
35 |
import org.gvsig.app.project.documents.layout.fframes.IFFrame; |
36 |
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap; |
37 |
import org.gvsig.app.project.documents.layout.toc.TocModelObserver; |
38 |
import org.gvsig.fmap.mapcontext.MapContext; |
39 |
import org.gvsig.tools.ToolsLocator; |
40 |
import org.gvsig.tools.dynobject.DynStruct; |
41 |
import org.gvsig.tools.observer.ObservableHelper; |
42 |
import org.gvsig.tools.observer.Observer; |
43 |
import org.gvsig.tools.persistence.PersistenceManager; |
44 |
import org.gvsig.tools.persistence.PersistentState; |
45 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
46 |
|
47 |
/**
|
48 |
* Model of LayoutControl.
|
49 |
*
|
50 |
* @author Vicente Caballero Navarro
|
51 |
*/
|
52 |
public class DefaultLayoutContext implements LayoutContext { |
53 |
|
54 |
public static final String PERSISTENCE_DEFINITION_NAME = "LayoutContext"; |
55 |
|
56 |
private static final String ISADJUSTINGTOGRID_FIELD = "isAdjustingToGrid"; |
57 |
private static final String IS_GRID_VISIBLE = "IS_GRID_VISIBLE"; |
58 |
private static final String IS_RULER_VISIBLE = "IS_RULER_VISIBLE"; |
59 |
private static final String ISEDITABLE_FIELD = "isEditable"; |
60 |
|
61 |
private static final String NUMBEHIND_FIELD = "numBehind"; |
62 |
private static final String NUMBEFORE_FIELD = "numBefore"; |
63 |
private static final String ATTRIBUTES_FIELD = "attributes"; |
64 |
private static final String FFRAMES_FIELD = "fframes"; |
65 |
|
66 |
private static DefaultLayoutManager layoutManager = null; |
67 |
|
68 |
private Attributes m_attributes = null; |
69 |
private IFFrame[] fframes; |
70 |
private FrameCommandsRecord fcr;
|
71 |
private static Hashtable nums = new Hashtable(); |
72 |
private int numBefore = 0; |
73 |
private int numBehind = 0; |
74 |
private boolean isEditable = true; |
75 |
private Boolean adjustToGrid = null; |
76 |
|
77 |
private Boolean m_showRuler; |
78 |
private Boolean isGridVisible = null; |
79 |
|
80 |
private AffineTransform m_MatrizTransf; |
81 |
|
82 |
private ObservableHelper observers;
|
83 |
private CustomizableObserverHelper tocObservers = new CustomizableObserverHelper() { |
84 |
|
85 |
@Override
|
86 |
protected void doNotify(Object listener, Object data) { |
87 |
TocModelObserver l = (TocModelObserver) listener; |
88 |
l.tocUpdated(DefaultLayoutContext.this, (TocModelChangedNotification) data); |
89 |
} |
90 |
}; |
91 |
|
92 |
private MapContext tocModel = null; |
93 |
|
94 |
/**
|
95 |
* Create a new object of LayoutContext.
|
96 |
*/
|
97 |
public DefaultLayoutContext() {
|
98 |
layoutManager = |
99 |
(DefaultLayoutManager) ProjectManager.getInstance() |
100 |
.getDocumentManager(DefaultLayoutManager.TYPENAME); |
101 |
observers = new ObservableHelper();
|
102 |
m_attributes = new Attributes(); |
103 |
m_MatrizTransf = new AffineTransform(); |
104 |
m_MatrizTransf.setToIdentity(); |
105 |
FrameManager fm = new FrameManager();
|
106 |
fcr = new FrameCommandsRecord(fm);
|
107 |
updateFFrames(); |
108 |
} |
109 |
|
110 |
public AffineTransform getAT() { |
111 |
return m_MatrizTransf;
|
112 |
} |
113 |
|
114 |
public Attributes getAttributes() { |
115 |
return m_attributes;
|
116 |
} |
117 |
|
118 |
public void setAtributes(Attributes attributes) { |
119 |
m_attributes = attributes; |
120 |
} |
121 |
|
122 |
public IFFrame[] getFFrames() { |
123 |
return fframes;
|
124 |
} |
125 |
|
126 |
public IFFrame getFFrame(int i) { |
127 |
return fframes[i];
|
128 |
} |
129 |
|
130 |
public void updateFFrames() { |
131 |
ArrayList frames = new ArrayList(); |
132 |
IFFrame[] auxfframes = fcr.getFrameManager().getFFrames();
|
133 |
for (int j = numBehind; j <= numBefore; j++) { |
134 |
for (int i = 0; i < auxfframes.length; i++) { |
135 |
if (auxfframes[i].getLevel() == j) {
|
136 |
frames.add(auxfframes[i]); |
137 |
continue;
|
138 |
} |
139 |
} |
140 |
} |
141 |
fframes = (IFFrame[]) frames.toArray(new IFFrame[0]); |
142 |
} |
143 |
|
144 |
public void delFFrameSelected() { |
145 |
fcr.startComplex(PluginServices.getText(this, "remove_elements")); |
146 |
IFFrame[] validFrames = fcr.getFrameManager().getFFrames();
|
147 |
for (int i = 0; i<validFrames.length; i++) { |
148 |
IFFrame fframe = validFrames[i]; |
149 |
|
150 |
if (fframe.isSelected()) {
|
151 |
observers.notifyObservers(this,
|
152 |
new FrameChangedNotificationImpl(FrameChangedNotificationImpl.FRAME_REMOVING, fframe));
|
153 |
fframe.setSelected(false);
|
154 |
fcr.delete(fframe); |
155 |
observers.notifyObservers(this,
|
156 |
new FrameChangedNotificationImpl(FrameChangedNotificationImpl.FRAME_REMOVED, fframe));
|
157 |
} |
158 |
} |
159 |
fcr.endComplex(); |
160 |
updateFFrames(); |
161 |
} |
162 |
|
163 |
public void clearSelection() { |
164 |
for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) { |
165 |
IFFrame fframe = fcr.getFrameManager().getFFrame(i); |
166 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
167 |
fframe.setSelected(false);
|
168 |
} |
169 |
} |
170 |
} |
171 |
|
172 |
public void delFFrame(int index) { |
173 |
IFFrame frame = getFFrame(index); |
174 |
delFFrame(frame); |
175 |
} |
176 |
|
177 |
public void delFFrame(IFFrame frame) { |
178 |
observers.notifyObservers(this,
|
179 |
new FrameChangedNotificationImpl(FrameChangedNotificationImpl.FRAME_REMOVING, frame));
|
180 |
for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) { |
181 |
if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
|
182 |
frame.setSelected(false);
|
183 |
fcr.delete(frame); |
184 |
} |
185 |
} |
186 |
updateFFrames(); |
187 |
observers.notifyObservers(this,
|
188 |
new FrameChangedNotificationImpl(FrameChangedNotificationImpl.FRAME_REMOVED, frame));
|
189 |
} |
190 |
|
191 |
public FrameCommandsRecord getFrameCommandsRecord() {
|
192 |
return fcr;
|
193 |
} |
194 |
|
195 |
public void addFFrame(IFFrame frame, boolean clearSelection, boolean select) { |
196 |
this.addObserver(frame);
|
197 |
observers.notifyObservers(this,
|
198 |
new FrameChangedNotificationImpl(FrameChangedNotification.FRAME_ADDING, frame));
|
199 |
IFFrame[] fframes = getFFrames();
|
200 |
if (clearSelection) {
|
201 |
for (int i = fframes.length - 1; i >= 0; i--) { |
202 |
IFFrame fframe1 = fframes[i]; |
203 |
fframe1.setSelected(false);
|
204 |
} |
205 |
} |
206 |
|
207 |
if (nums.containsKey(frame.getClass())) {
|
208 |
nums.put( |
209 |
frame.getClass(), |
210 |
new Integer(Integer.parseInt(nums.get(frame.getClass()) |
211 |
.toString()) + 1));
|
212 |
} else {
|
213 |
nums.put(frame.getClass(), new Integer(0)); |
214 |
} |
215 |
|
216 |
frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
|
217 |
fcr.insert(frame); |
218 |
frame.setSelected(select); |
219 |
frame.setLevel(getNumBefore()); |
220 |
updateFFrames(); |
221 |
observers.notifyObservers(this,
|
222 |
new FrameChangedNotificationImpl(FrameChangedNotificationImpl.FRAME_ADDED, frame));
|
223 |
} |
224 |
|
225 |
public void addFFrameSameProperties(IFFrame frame) { |
226 |
this.addObserver(frame);
|
227 |
observers.notifyObservers(this,
|
228 |
new FrameChangedNotificationImpl(FrameChangedNotificationImpl.FRAME_ADDING, frame));
|
229 |
fcr.insert(frame); |
230 |
frame.setSelected(true);
|
231 |
frame.setLevel(getNumBefore()); |
232 |
updateFFrames(); |
233 |
observers.notifyObservers(this,
|
234 |
new FrameChangedNotificationImpl(FrameChangedNotificationImpl.FRAME_ADDED, frame));
|
235 |
} |
236 |
|
237 |
public int getNumBehind() { |
238 |
return --numBehind;
|
239 |
} |
240 |
|
241 |
public int getNumBefore() { |
242 |
return ++numBefore;
|
243 |
} |
244 |
|
245 |
public IFFrame[] getAllFFrames() { |
246 |
ArrayList all = new ArrayList(); |
247 |
return (IFFrame[]) allFFrames(getFFrames(), all) |
248 |
.toArray(new IFFrame[0]); |
249 |
} |
250 |
|
251 |
private ArrayList allFFrames(IFFrame[] fframes, ArrayList all) { |
252 |
for (int i = 0; i < fframes.length; i++) { |
253 |
if (fframes[i] instanceof FFrameGroup) { |
254 |
ArrayList groupFrames =
|
255 |
allFFrames(((FFrameGroup) fframes[i]).getFFrames(), all); |
256 |
if (!all.containsAll(groupFrames)) {
|
257 |
all.addAll(groupFrames); |
258 |
} |
259 |
|
260 |
} else {
|
261 |
if (!all.contains(fframes[i])) {
|
262 |
all.add(fframes[i]); |
263 |
} |
264 |
} |
265 |
} |
266 |
return all;
|
267 |
} |
268 |
|
269 |
public IFFrame[] getSelectedFFrames() { |
270 |
ArrayList selecList = new ArrayList(); |
271 |
IFFrame[] fframes = getFFrames();
|
272 |
for (int i = fframes.length - 1; i >= 0; i--) { |
273 |
IFFrame fframe = fframes[i]; |
274 |
|
275 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
276 |
selecList.add(fframe); |
277 |
} |
278 |
} |
279 |
return (IFFrame[]) selecList.toArray(new IFFrame[selecList.size()]); |
280 |
} |
281 |
|
282 |
public <T> T[] getSelectedFFrames(Class<T> clazz) { |
283 |
ArrayList<T> selecList = new ArrayList<T>(); |
284 |
IFFrame[] fframes = getFFrames();
|
285 |
for (int i = fframes.length - 1; i >= 0; i--) { |
286 |
IFFrame fframe = fframes[i]; |
287 |
if (clazz.isInstance(fframe)) {
|
288 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
289 |
selecList.add((T)fframe); |
290 |
} |
291 |
} |
292 |
} |
293 |
T[] newInstance = (T[])Array.newInstance(clazz, selecList.size()); |
294 |
return selecList.toArray(newInstance);
|
295 |
} |
296 |
|
297 |
public boolean isEditable() { |
298 |
return isEditable;
|
299 |
} |
300 |
|
301 |
public void setEditable(boolean b) { |
302 |
if (!b) {
|
303 |
clearSelection(); |
304 |
// layoutControl.setTool("layoutzoomin");
|
305 |
PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
|
306 |
} |
307 |
isEditable = b; |
308 |
|
309 |
} |
310 |
|
311 |
public boolean isAdjustingToGrid() { |
312 |
if (adjustToGrid == null) { |
313 |
adjustToGrid = new Boolean(layoutManager.getDefaultAdjustToGrid()); |
314 |
} |
315 |
return adjustToGrid.booleanValue();
|
316 |
} |
317 |
|
318 |
public void setAdjustToGrid(boolean b) { |
319 |
adjustToGrid = new Boolean(b); |
320 |
} |
321 |
|
322 |
public void setRuler(boolean b) { |
323 |
m_showRuler = new Boolean(b); |
324 |
} |
325 |
|
326 |
public boolean getRuler() { |
327 |
if (m_showRuler == null) { |
328 |
m_showRuler = new Boolean(layoutManager.getDefaultShowRulers()); |
329 |
} |
330 |
return m_showRuler.booleanValue();
|
331 |
} |
332 |
|
333 |
public boolean isGridVisible() { |
334 |
if (isGridVisible == null) { |
335 |
isGridVisible = new Boolean(layoutManager.getDefaultShowGrid()); |
336 |
} |
337 |
return isGridVisible.booleanValue();
|
338 |
} |
339 |
|
340 |
public void setGridVisible(boolean b) { |
341 |
isGridVisible = new Boolean(b); |
342 |
} |
343 |
|
344 |
public void fullRefresh() { |
345 |
IFFrame[] fframes = getFFrames();
|
346 |
for (int i = 0; i < fframes.length; i++) { |
347 |
if (fframes[i] instanceof IFFrameUseFMap) { |
348 |
IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i]; |
349 |
fframe.refresh(); |
350 |
} |
351 |
} |
352 |
notifAllObservers(); |
353 |
} |
354 |
|
355 |
public void notifAllObservers(){ |
356 |
observers.notifyObservers(this,
|
357 |
new DefaultLayoutNotification(LayoutNotification.LAYOUT_INVALIDATED));
|
358 |
} |
359 |
|
360 |
public static void registerPersistent() { |
361 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
362 |
if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) { |
363 |
DynStruct definition = |
364 |
manager.addDefinition(DefaultLayoutContext.class, |
365 |
PERSISTENCE_DEFINITION_NAME, |
366 |
"Layout context persistence definition", null, null); |
367 |
|
368 |
definition.addDynFieldInt(NUMBEHIND_FIELD).setMandatory(true);
|
369 |
definition.addDynFieldInt(NUMBEFORE_FIELD).setMandatory(true);
|
370 |
definition.addDynFieldObject(ATTRIBUTES_FIELD) |
371 |
.setClassOfValue(Attributes.class).setMandatory(true); |
372 |
definition.addDynFieldArray(FFRAMES_FIELD) |
373 |
.setClassOfItems(IFFrame.class).setMandatory(true);
|
374 |
// ==================
|
375 |
definition.addDynFieldBoolean(ISADJUSTINGTOGRID_FIELD).setMandatory(true);
|
376 |
definition.addDynFieldBoolean(ISEDITABLE_FIELD).setMandatory(true);
|
377 |
definition.addDynFieldBoolean(IS_GRID_VISIBLE).setMandatory(false);
|
378 |
definition.addDynFieldBoolean(IS_RULER_VISIBLE).setMandatory(false);
|
379 |
} |
380 |
|
381 |
FFrame.registerPersistent(); |
382 |
Attributes.registerPersistent();
|
383 |
} |
384 |
|
385 |
public void loadFromState(PersistentState state) |
386 |
throws PersistenceException {
|
387 |
adjustToGrid = state.getBoolean(ISADJUSTINGTOGRID_FIELD); |
388 |
isEditable = state.getBoolean(ISEDITABLE_FIELD); |
389 |
|
390 |
if (state.hasValue(IS_GRID_VISIBLE)) {
|
391 |
isGridVisible = state.getBoolean(IS_GRID_VISIBLE); |
392 |
} |
393 |
if (state.hasValue(IS_RULER_VISIBLE)) {
|
394 |
m_showRuler = state.getBoolean(IS_RULER_VISIBLE); |
395 |
} |
396 |
|
397 |
numBehind = state.getInt(NUMBEHIND_FIELD); |
398 |
numBefore = state.getInt(NUMBEFORE_FIELD); |
399 |
m_attributes = (Attributes) state.get(ATTRIBUTES_FIELD);
|
400 |
IFFrame[] fframes =
|
401 |
(IFFrame[]) state.getArray(FFRAMES_FIELD, IFFrame.class);
|
402 |
for (int i = 0; i < fframes.length; i++) { |
403 |
addFFrame(fframes[i], true, true); |
404 |
} |
405 |
} |
406 |
|
407 |
public void saveToState(PersistentState state) throws PersistenceException { |
408 |
state.set(ISADJUSTINGTOGRID_FIELD, isAdjustingToGrid()); |
409 |
state.set(ISEDITABLE_FIELD, isEditable()); |
410 |
state.set(NUMBEHIND_FIELD, numBehind); |
411 |
state.set(NUMBEFORE_FIELD, numBefore); |
412 |
state.set(ATTRIBUTES_FIELD, m_attributes); |
413 |
state.set(FFRAMES_FIELD, fframes); |
414 |
|
415 |
state.set(IS_RULER_VISIBLE, this.getRuler());
|
416 |
state.set(IS_GRID_VISIBLE, this.isGridVisible());
|
417 |
} |
418 |
|
419 |
public void setNumBefore(int numBefore) { |
420 |
this.numBefore = numBefore;
|
421 |
|
422 |
} |
423 |
|
424 |
public void setNumBehind(int numBehind) { |
425 |
this.numBehind = numBehind;
|
426 |
} |
427 |
|
428 |
public void addObserver(Observer o) { |
429 |
observers.addObserver(o); |
430 |
} |
431 |
|
432 |
public void deleteObserver(Observer o) { |
433 |
observers.deleteObserver(o); |
434 |
} |
435 |
|
436 |
public void deleteObservers() { |
437 |
observers.deleteObservers(); |
438 |
} |
439 |
|
440 |
public void setTocModel(MapContext tocModel) { |
441 |
MapContext oldModel = this.tocModel;
|
442 |
this.tocModel = tocModel;
|
443 |
if (tocModel==oldModel){
|
444 |
// no real change
|
445 |
return;
|
446 |
} |
447 |
else {
|
448 |
tocObservers.notifyObservers(this,
|
449 |
new TocModelChangedImpl(TocModelChangedNotification.MODEL_SET, tocModel));
|
450 |
} |
451 |
} |
452 |
|
453 |
public void notifyTocUpdated() { |
454 |
tocObservers.notifyObservers(this,
|
455 |
new TocModelChangedImpl(TocModelChangedNotification.MODEL_CHANGED, tocModel));
|
456 |
|
457 |
} |
458 |
|
459 |
public MapContext getTocModel() {
|
460 |
return this.tocModel; |
461 |
} |
462 |
|
463 |
public void addTocObserver(TocModelObserver observer) { |
464 |
this.tocObservers.addObserver(observer);
|
465 |
} |
466 |
|
467 |
public void removeTocObserver(TocModelObserver observer) { |
468 |
this.tocObservers.deleteObserver(observer);
|
469 |
} |
470 |
|
471 |
public void removeTocObservers() { |
472 |
this.tocObservers.deleteObservers();
|
473 |
} |
474 |
} |