svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / DefaultLayoutContext.java @ 167
History | View | Annotate | Download (14.7 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 |
fframe.setSelected(false);
|
152 |
fcr.delete(fframe); |
153 |
} |
154 |
} |
155 |
fcr.endComplex(); |
156 |
updateFFrames(); |
157 |
} |
158 |
|
159 |
public void clearSelection() { |
160 |
for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) { |
161 |
IFFrame fframe = fcr.getFrameManager().getFFrame(i); |
162 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
163 |
fframe.setSelected(false);
|
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
public void delFFrame(int index) { |
169 |
IFFrame frame = getFFrame(index); |
170 |
delFFrame(frame); |
171 |
} |
172 |
|
173 |
public void delFFrame(IFFrame frame) { |
174 |
for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) { |
175 |
if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
|
176 |
frame.setSelected(false);
|
177 |
fcr.delete(frame); |
178 |
} |
179 |
} |
180 |
updateFFrames(); |
181 |
} |
182 |
|
183 |
public FrameCommandsRecord getFrameCommandsRecord() {
|
184 |
return fcr;
|
185 |
} |
186 |
|
187 |
public void addFFrame(IFFrame frame, boolean clearSelection, boolean select) { |
188 |
IFFrame[] fframes = getFFrames();
|
189 |
if (clearSelection) {
|
190 |
for (int i = fframes.length - 1; i >= 0; i--) { |
191 |
IFFrame fframe1 = fframes[i]; |
192 |
fframe1.setSelected(false);
|
193 |
} |
194 |
} |
195 |
|
196 |
if (nums.containsKey(frame.getClass())) {
|
197 |
nums.put( |
198 |
frame.getClass(), |
199 |
new Integer(Integer.parseInt(nums.get(frame.getClass()) |
200 |
.toString()) + 1));
|
201 |
} else {
|
202 |
nums.put(frame.getClass(), new Integer(0)); |
203 |
} |
204 |
|
205 |
frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
|
206 |
fcr.insert(frame); |
207 |
frame.setSelected(select); |
208 |
frame.setLevel(getNumBefore()); |
209 |
updateFFrames(); |
210 |
} |
211 |
|
212 |
public void addFFrameSameProperties(IFFrame frame) { |
213 |
fcr.insert(frame); |
214 |
frame.setSelected(true);
|
215 |
frame.setLevel(getNumBefore()); |
216 |
updateFFrames(); |
217 |
} |
218 |
|
219 |
public int getNumBehind() { |
220 |
return --numBehind;
|
221 |
} |
222 |
|
223 |
public int getNumBefore() { |
224 |
return ++numBefore;
|
225 |
} |
226 |
|
227 |
public IFFrame[] getAllFFrames() { |
228 |
ArrayList all = new ArrayList(); |
229 |
return (IFFrame[]) allFFrames(getFFrames(), all) |
230 |
.toArray(new IFFrame[0]); |
231 |
} |
232 |
|
233 |
private ArrayList allFFrames(IFFrame[] fframes, ArrayList all) { |
234 |
for (int i = 0; i < fframes.length; i++) { |
235 |
if (fframes[i] instanceof FFrameGroup) { |
236 |
ArrayList groupFrames =
|
237 |
allFFrames(((FFrameGroup) fframes[i]).getFFrames(), all); |
238 |
if (!all.containsAll(groupFrames)) {
|
239 |
all.addAll(groupFrames); |
240 |
} |
241 |
|
242 |
} else {
|
243 |
if (!all.contains(fframes[i])) {
|
244 |
all.add(fframes[i]); |
245 |
} |
246 |
} |
247 |
} |
248 |
return all;
|
249 |
} |
250 |
|
251 |
public IFFrame[] getSelectedFFrames() { |
252 |
ArrayList selecList = new ArrayList(); |
253 |
IFFrame[] fframes = getFFrames();
|
254 |
for (int i = fframes.length - 1; i >= 0; i--) { |
255 |
IFFrame fframe = fframes[i]; |
256 |
|
257 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
258 |
selecList.add(fframe); |
259 |
} |
260 |
} |
261 |
return (IFFrame[]) selecList.toArray(new IFFrame[selecList.size()]); |
262 |
} |
263 |
|
264 |
public <T> T[] getSelectedFFrames(Class<T> clazz) { |
265 |
ArrayList<T> selecList = new ArrayList<T>(); |
266 |
IFFrame[] fframes = getFFrames();
|
267 |
for (int i = fframes.length - 1; i >= 0; i--) { |
268 |
IFFrame fframe = fframes[i]; |
269 |
if (clazz.isInstance(fframe)) {
|
270 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
271 |
selecList.add((T)fframe); |
272 |
} |
273 |
} |
274 |
} |
275 |
T[] newInstance = (T[])Array.newInstance(clazz, selecList.size()); |
276 |
return selecList.toArray(newInstance);
|
277 |
} |
278 |
|
279 |
public boolean isEditable() { |
280 |
return isEditable;
|
281 |
} |
282 |
|
283 |
public void setEditable(boolean b) { |
284 |
if (!b) {
|
285 |
clearSelection(); |
286 |
// layoutControl.setTool("layoutzoomin");
|
287 |
PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
|
288 |
} |
289 |
isEditable = b; |
290 |
|
291 |
} |
292 |
|
293 |
public boolean isAdjustingToGrid() { |
294 |
if (adjustToGrid == null) { |
295 |
adjustToGrid = new Boolean(layoutManager.getDefaultAdjustToGrid()); |
296 |
} |
297 |
return adjustToGrid.booleanValue();
|
298 |
} |
299 |
|
300 |
public void setAdjustToGrid(boolean b) { |
301 |
adjustToGrid = new Boolean(b); |
302 |
} |
303 |
|
304 |
public void setRuler(boolean b) { |
305 |
m_showRuler = new Boolean(b); |
306 |
} |
307 |
|
308 |
public boolean getRuler() { |
309 |
if (m_showRuler == null) { |
310 |
m_showRuler = new Boolean(layoutManager.getDefaultShowRulers()); |
311 |
} |
312 |
return m_showRuler.booleanValue();
|
313 |
} |
314 |
|
315 |
public boolean isGridVisible() { |
316 |
if (isGridVisible == null) { |
317 |
isGridVisible = new Boolean(layoutManager.getDefaultShowGrid()); |
318 |
} |
319 |
return isGridVisible.booleanValue();
|
320 |
} |
321 |
|
322 |
public void setGridVisible(boolean b) { |
323 |
isGridVisible = new Boolean(b); |
324 |
} |
325 |
|
326 |
public void fullRefresh() { |
327 |
IFFrame[] fframes = getFFrames();
|
328 |
for (int i = 0; i < fframes.length; i++) { |
329 |
if (fframes[i] instanceof IFFrameUseFMap) { |
330 |
IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i]; |
331 |
fframe.refresh(); |
332 |
} |
333 |
} |
334 |
notifAllObservers(); |
335 |
} |
336 |
|
337 |
public void notifAllObservers(){ |
338 |
observers.notifyObservers(this,
|
339 |
new DefaultLayoutNotification(LayoutNotification.LAYOUT_REFRESH));
|
340 |
} |
341 |
|
342 |
public static void registerPersistent() { |
343 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
344 |
if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) { |
345 |
DynStruct definition = |
346 |
manager.addDefinition(DefaultLayoutContext.class, |
347 |
PERSISTENCE_DEFINITION_NAME, |
348 |
"Layout context persistence definition", null, null); |
349 |
|
350 |
definition.addDynFieldInt(NUMBEHIND_FIELD).setMandatory(true);
|
351 |
definition.addDynFieldInt(NUMBEFORE_FIELD).setMandatory(true);
|
352 |
definition.addDynFieldObject(ATTRIBUTES_FIELD) |
353 |
.setClassOfValue(Attributes.class).setMandatory(true); |
354 |
definition.addDynFieldArray(FFRAMES_FIELD) |
355 |
.setClassOfItems(IFFrame.class).setMandatory(true);
|
356 |
// ==================
|
357 |
definition.addDynFieldBoolean(ISADJUSTINGTOGRID_FIELD).setMandatory(true);
|
358 |
definition.addDynFieldBoolean(ISEDITABLE_FIELD).setMandatory(true);
|
359 |
definition.addDynFieldBoolean(IS_GRID_VISIBLE).setMandatory(false);
|
360 |
definition.addDynFieldBoolean(IS_RULER_VISIBLE).setMandatory(false);
|
361 |
} |
362 |
|
363 |
FFrame.registerPersistent(); |
364 |
Attributes.registerPersistent();
|
365 |
} |
366 |
|
367 |
public void loadFromState(PersistentState state) |
368 |
throws PersistenceException {
|
369 |
adjustToGrid = state.getBoolean(ISADJUSTINGTOGRID_FIELD); |
370 |
isEditable = state.getBoolean(ISEDITABLE_FIELD); |
371 |
|
372 |
if (state.hasValue(IS_GRID_VISIBLE)) {
|
373 |
isGridVisible = state.getBoolean(IS_GRID_VISIBLE); |
374 |
} |
375 |
if (state.hasValue(IS_RULER_VISIBLE)) {
|
376 |
m_showRuler = state.getBoolean(IS_RULER_VISIBLE); |
377 |
} |
378 |
|
379 |
numBehind = state.getInt(NUMBEHIND_FIELD); |
380 |
numBefore = state.getInt(NUMBEFORE_FIELD); |
381 |
m_attributes = (Attributes) state.get(ATTRIBUTES_FIELD);
|
382 |
IFFrame[] fframes =
|
383 |
(IFFrame[]) state.getArray(FFRAMES_FIELD, IFFrame.class);
|
384 |
for (int i = 0; i < fframes.length; i++) { |
385 |
addFFrame(fframes[i], true, true); |
386 |
} |
387 |
} |
388 |
|
389 |
public void saveToState(PersistentState state) throws PersistenceException { |
390 |
state.set(ISADJUSTINGTOGRID_FIELD, isAdjustingToGrid()); |
391 |
state.set(ISEDITABLE_FIELD, isEditable()); |
392 |
state.set(NUMBEHIND_FIELD, numBehind); |
393 |
state.set(NUMBEFORE_FIELD, numBefore); |
394 |
state.set(ATTRIBUTES_FIELD, m_attributes); |
395 |
state.set(FFRAMES_FIELD, fframes); |
396 |
|
397 |
state.set(IS_RULER_VISIBLE, this.getRuler());
|
398 |
state.set(IS_GRID_VISIBLE, this.isGridVisible());
|
399 |
} |
400 |
|
401 |
public void setNumBefore(int numBefore) { |
402 |
this.numBefore = numBefore;
|
403 |
|
404 |
} |
405 |
|
406 |
public void setNumBehind(int numBehind) { |
407 |
this.numBehind = numBehind;
|
408 |
} |
409 |
|
410 |
public void addObserver(Observer o) { |
411 |
observers.addObserver(o); |
412 |
} |
413 |
|
414 |
public void deleteObserver(Observer o) { |
415 |
observers.deleteObserver(o); |
416 |
} |
417 |
|
418 |
public void deleteObservers() { |
419 |
observers.deleteObservers(); |
420 |
} |
421 |
|
422 |
public void setTocModel(MapContext tocModel) { |
423 |
MapContext oldModel = this.tocModel;
|
424 |
this.tocModel = tocModel;
|
425 |
if (tocModel==oldModel){
|
426 |
// no real change
|
427 |
return;
|
428 |
} |
429 |
else {
|
430 |
tocObservers.notifyObservers(this,
|
431 |
new TocModelChangedImpl(TocModelChangedNotification.MODEL_SET, tocModel));
|
432 |
} |
433 |
} |
434 |
|
435 |
public void notifyTocUpdated() { |
436 |
tocObservers.notifyObservers(this,
|
437 |
new TocModelChangedImpl(TocModelChangedNotification.MODEL_CHANGED, tocModel));
|
438 |
|
439 |
} |
440 |
|
441 |
public MapContext getTocModel() {
|
442 |
return this.tocModel; |
443 |
} |
444 |
|
445 |
public void addTocObserver(TocModelObserver observer) { |
446 |
this.tocObservers.addObserver(observer);
|
447 |
} |
448 |
|
449 |
public void removeTocObserver(TocModelObserver observer) { |
450 |
this.tocObservers.deleteObserver(observer);
|
451 |
} |
452 |
|
453 |
public void removeTocObservers() { |
454 |
this.tocObservers.deleteObservers();
|
455 |
} |
456 |
} |