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 @ 140
History | View | Annotate | Download (13.6 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.tools.ToolsLocator; |
38 |
import org.gvsig.tools.dynobject.DynStruct; |
39 |
import org.gvsig.tools.observer.ObservableHelper; |
40 |
import org.gvsig.tools.observer.Observer; |
41 |
import org.gvsig.tools.persistence.PersistenceManager; |
42 |
import org.gvsig.tools.persistence.PersistentState; |
43 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
44 |
|
45 |
/**
|
46 |
* Model of LayoutControl.
|
47 |
*
|
48 |
* @author Vicente Caballero Navarro
|
49 |
*/
|
50 |
public class DefaultLayoutContext implements LayoutContext { |
51 |
|
52 |
public static final String PERSISTENCE_DEFINITION_NAME = "LayoutContext"; |
53 |
|
54 |
private static final String ISADJUSTINGTOGRID_FIELD = "isAdjustingToGrid"; |
55 |
private static final String IS_GRID_VISIBLE = "IS_GRID_VISIBLE"; |
56 |
private static final String IS_RULER_VISIBLE = "IS_RULER_VISIBLE"; |
57 |
private static final String ISEDITABLE_FIELD = "isEditable"; |
58 |
|
59 |
private static final String NUMBEHIND_FIELD = "numBehind"; |
60 |
private static final String NUMBEFORE_FIELD = "numBefore"; |
61 |
private static final String ATTRIBUTES_FIELD = "attributes"; |
62 |
private static final String FFRAMES_FIELD = "fframes"; |
63 |
|
64 |
private static DefaultLayoutManager layoutManager = null; |
65 |
|
66 |
private Attributes m_attributes = null; |
67 |
private IFFrame[] fframes; |
68 |
private FrameCommandsRecord fcr;
|
69 |
private static Hashtable nums = new Hashtable(); |
70 |
private int numBefore = 0; |
71 |
private int numBehind = 0; |
72 |
private boolean isEditable = true; |
73 |
private Boolean adjustToGrid = null; |
74 |
|
75 |
private Boolean m_showRuler; |
76 |
private Boolean isGridVisible = null; |
77 |
|
78 |
private AffineTransform m_MatrizTransf; |
79 |
|
80 |
private ObservableHelper observers;
|
81 |
|
82 |
/**
|
83 |
* Create a new object of LayoutContext.
|
84 |
*/
|
85 |
public DefaultLayoutContext() {
|
86 |
layoutManager = |
87 |
(DefaultLayoutManager) ProjectManager.getInstance() |
88 |
.getDocumentManager(DefaultLayoutManager.TYPENAME); |
89 |
observers = new ObservableHelper();
|
90 |
m_attributes = new Attributes(); |
91 |
m_MatrizTransf = new AffineTransform(); |
92 |
m_MatrizTransf.setToIdentity(); |
93 |
FrameManager fm = new FrameManager();
|
94 |
fcr = new FrameCommandsRecord(fm);
|
95 |
updateFFrames(); |
96 |
} |
97 |
|
98 |
public AffineTransform getAT() { |
99 |
return m_MatrizTransf;
|
100 |
} |
101 |
|
102 |
public Attributes getAttributes() { |
103 |
return m_attributes;
|
104 |
} |
105 |
|
106 |
public void setAtributes(Attributes attributes) { |
107 |
m_attributes = attributes; |
108 |
} |
109 |
|
110 |
public IFFrame[] getFFrames() { |
111 |
return fframes;
|
112 |
} |
113 |
|
114 |
public IFFrame getFFrame(int i) { |
115 |
return fframes[i];
|
116 |
} |
117 |
|
118 |
public void updateFFrames() { |
119 |
ArrayList frames = new ArrayList(); |
120 |
IFFrame[] auxfframes = fcr.getFrameManager().getFFrames();
|
121 |
for (int j = numBehind; j <= numBefore; j++) { |
122 |
for (int i = 0; i < auxfframes.length; i++) { |
123 |
if (auxfframes[i].getLevel() == j) {
|
124 |
frames.add(auxfframes[i]); |
125 |
continue;
|
126 |
} |
127 |
} |
128 |
} |
129 |
fframes = (IFFrame[]) frames.toArray(new IFFrame[0]); |
130 |
} |
131 |
|
132 |
public void delFFrameSelected() { |
133 |
fcr.startComplex(PluginServices.getText(this, "remove_elements")); |
134 |
for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) { |
135 |
IFFrame fframe = fcr.getFrameManager().getFFrame(i); |
136 |
|
137 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
138 |
fframe.setSelected(false);
|
139 |
fcr.delete(fframe); |
140 |
} |
141 |
} |
142 |
fcr.endComplex(); |
143 |
updateFFrames(); |
144 |
} |
145 |
|
146 |
public void clearSelection() { |
147 |
for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) { |
148 |
IFFrame fframe = fcr.getFrameManager().getFFrame(i); |
149 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
150 |
fframe.setSelected(false);
|
151 |
} |
152 |
} |
153 |
} |
154 |
|
155 |
public void delFFrame(int index) { |
156 |
for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) { |
157 |
IFFrame frame = getFFrame(index); |
158 |
if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
|
159 |
fcr.delete(frame); |
160 |
} |
161 |
} |
162 |
updateFFrames(); |
163 |
} |
164 |
|
165 |
public void delFFrame(IFFrame frame) { |
166 |
for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) { |
167 |
if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
|
168 |
fcr.delete(frame); |
169 |
} |
170 |
} |
171 |
updateFFrames(); |
172 |
} |
173 |
|
174 |
public FrameCommandsRecord getFrameCommandsRecord() {
|
175 |
return fcr;
|
176 |
} |
177 |
|
178 |
public void addFFrame(IFFrame frame, boolean clearSelection, boolean select) { |
179 |
IFFrame[] fframes = getFFrames();
|
180 |
if (clearSelection) {
|
181 |
for (int i = fframes.length - 1; i >= 0; i--) { |
182 |
IFFrame fframe1 = fframes[i]; |
183 |
fframe1.setSelected(false);
|
184 |
} |
185 |
} |
186 |
|
187 |
if (nums.containsKey(frame.getClass())) {
|
188 |
nums.put( |
189 |
frame.getClass(), |
190 |
new Integer(Integer.parseInt(nums.get(frame.getClass()) |
191 |
.toString()) + 1));
|
192 |
} else {
|
193 |
nums.put(frame.getClass(), new Integer(0)); |
194 |
} |
195 |
|
196 |
frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
|
197 |
fcr.insert(frame); |
198 |
frame.setSelected(select); |
199 |
frame.setLevel(getNumBefore()); |
200 |
updateFFrames(); |
201 |
} |
202 |
|
203 |
public void addFFrameSameProperties(IFFrame frame) { |
204 |
fcr.insert(frame); |
205 |
frame.setSelected(true);
|
206 |
frame.setLevel(getNumBefore()); |
207 |
updateFFrames(); |
208 |
} |
209 |
|
210 |
public int getNumBehind() { |
211 |
return --numBehind;
|
212 |
} |
213 |
|
214 |
public int getNumBefore() { |
215 |
return ++numBefore;
|
216 |
} |
217 |
|
218 |
public IFFrame[] getAllFFrames() { |
219 |
ArrayList all = new ArrayList(); |
220 |
return (IFFrame[]) allFFrames(getFFrames(), all) |
221 |
.toArray(new IFFrame[0]); |
222 |
} |
223 |
|
224 |
private ArrayList allFFrames(IFFrame[] fframes, ArrayList all) { |
225 |
for (int i = 0; i < fframes.length; i++) { |
226 |
if (fframes[i] instanceof FFrameGroup) { |
227 |
ArrayList groupFrames =
|
228 |
allFFrames(((FFrameGroup) fframes[i]).getFFrames(), all); |
229 |
if (!all.containsAll(groupFrames)) {
|
230 |
all.addAll(groupFrames); |
231 |
} |
232 |
|
233 |
} else {
|
234 |
if (!all.contains(fframes[i])) {
|
235 |
all.add(fframes[i]); |
236 |
} |
237 |
} |
238 |
} |
239 |
return all;
|
240 |
} |
241 |
|
242 |
public IFFrame[] getSelectedFFrames() { |
243 |
ArrayList selecList = new ArrayList(); |
244 |
IFFrame[] fframes = getFFrames();
|
245 |
for (int i = fframes.length - 1; i >= 0; i--) { |
246 |
IFFrame fframe = fframes[i]; |
247 |
|
248 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
249 |
selecList.add(fframe); |
250 |
} |
251 |
} |
252 |
return (IFFrame[]) selecList.toArray(new IFFrame[selecList.size()]); |
253 |
} |
254 |
|
255 |
public <T> T[] getSelectedFFrames(Class<T> clazz) { |
256 |
ArrayList<T> selecList = new ArrayList<T>(); |
257 |
IFFrame[] fframes = getFFrames();
|
258 |
for (int i = fframes.length - 1; i >= 0; i--) { |
259 |
IFFrame fframe = fframes[i]; |
260 |
if (clazz.isInstance(fframe)) {
|
261 |
if (fframe.getSelected() != IFFrame.NOSELECT) {
|
262 |
selecList.add((T)fframe); |
263 |
} |
264 |
} |
265 |
} |
266 |
T[] newInstance = (T[])Array.newInstance(clazz, selecList.size()); |
267 |
return selecList.toArray(newInstance);
|
268 |
} |
269 |
|
270 |
public boolean isEditable() { |
271 |
return isEditable;
|
272 |
} |
273 |
|
274 |
public void setEditable(boolean b) { |
275 |
if (!b) {
|
276 |
clearSelection(); |
277 |
// layoutControl.setTool("layoutzoomin");
|
278 |
PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
|
279 |
} |
280 |
isEditable = b; |
281 |
|
282 |
} |
283 |
|
284 |
public boolean isAdjustingToGrid() { |
285 |
if (adjustToGrid == null) { |
286 |
adjustToGrid = new Boolean(layoutManager.getDefaultAdjustToGrid()); |
287 |
} |
288 |
return adjustToGrid.booleanValue();
|
289 |
} |
290 |
|
291 |
public void setAdjustToGrid(boolean b) { |
292 |
adjustToGrid = new Boolean(b); |
293 |
} |
294 |
|
295 |
public void setRuler(boolean b) { |
296 |
m_showRuler = new Boolean(b); |
297 |
} |
298 |
|
299 |
public boolean getRuler() { |
300 |
if (m_showRuler == null) { |
301 |
m_showRuler = new Boolean(layoutManager.getDefaultShowRulers()); |
302 |
} |
303 |
return m_showRuler.booleanValue();
|
304 |
} |
305 |
|
306 |
public boolean isGridVisible() { |
307 |
if (isGridVisible == null) { |
308 |
isGridVisible = new Boolean(layoutManager.getDefaultShowGrid()); |
309 |
} |
310 |
return isGridVisible.booleanValue();
|
311 |
} |
312 |
|
313 |
public void setGridVisible(boolean b) { |
314 |
isGridVisible = new Boolean(b); |
315 |
} |
316 |
|
317 |
public void fullRefresh() { |
318 |
IFFrame[] fframes = getFFrames();
|
319 |
for (int i = 0; i < fframes.length; i++) { |
320 |
if (fframes[i] instanceof IFFrameUseFMap) { |
321 |
IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i]; |
322 |
fframe.refresh(); |
323 |
} |
324 |
} |
325 |
notifAllObservers(); |
326 |
} |
327 |
|
328 |
public void notifAllObservers(){ |
329 |
observers.notifyObservers(this,
|
330 |
new DefaultLayoutNotification(LayoutNotification.LAYOUT_REFRESH));
|
331 |
} |
332 |
|
333 |
public static void registerPersistent() { |
334 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
335 |
if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) { |
336 |
DynStruct definition = |
337 |
manager.addDefinition(DefaultLayoutContext.class, |
338 |
PERSISTENCE_DEFINITION_NAME, |
339 |
"Layout context persistence definition", null, null); |
340 |
|
341 |
definition.addDynFieldInt(NUMBEHIND_FIELD).setMandatory(true);
|
342 |
definition.addDynFieldInt(NUMBEFORE_FIELD).setMandatory(true);
|
343 |
definition.addDynFieldObject(ATTRIBUTES_FIELD) |
344 |
.setClassOfValue(Attributes.class).setMandatory(true); |
345 |
definition.addDynFieldArray(FFRAMES_FIELD) |
346 |
.setClassOfItems(IFFrame.class).setMandatory(true);
|
347 |
// ==================
|
348 |
definition.addDynFieldBoolean(ISADJUSTINGTOGRID_FIELD).setMandatory(true);
|
349 |
definition.addDynFieldBoolean(ISEDITABLE_FIELD).setMandatory(true);
|
350 |
definition.addDynFieldBoolean(IS_GRID_VISIBLE).setMandatory(false);
|
351 |
definition.addDynFieldBoolean(IS_RULER_VISIBLE).setMandatory(false);
|
352 |
} |
353 |
|
354 |
FFrame.registerPersistent(); |
355 |
Attributes.registerPersistent();
|
356 |
} |
357 |
|
358 |
public void loadFromState(PersistentState state) |
359 |
throws PersistenceException {
|
360 |
adjustToGrid = state.getBoolean(ISADJUSTINGTOGRID_FIELD); |
361 |
isEditable = state.getBoolean(ISEDITABLE_FIELD); |
362 |
|
363 |
if (state.hasValue(IS_GRID_VISIBLE)) {
|
364 |
isGridVisible = state.getBoolean(IS_GRID_VISIBLE); |
365 |
} |
366 |
if (state.hasValue(IS_RULER_VISIBLE)) {
|
367 |
m_showRuler = state.getBoolean(IS_RULER_VISIBLE); |
368 |
} |
369 |
|
370 |
numBehind = state.getInt(NUMBEHIND_FIELD); |
371 |
numBefore = state.getInt(NUMBEFORE_FIELD); |
372 |
m_attributes = (Attributes) state.get(ATTRIBUTES_FIELD);
|
373 |
IFFrame[] fframes =
|
374 |
(IFFrame[]) state.getArray(FFRAMES_FIELD, IFFrame.class);
|
375 |
for (int i = 0; i < fframes.length; i++) { |
376 |
addFFrame(fframes[i], true, true); |
377 |
} |
378 |
} |
379 |
|
380 |
public void saveToState(PersistentState state) throws PersistenceException { |
381 |
state.set(ISADJUSTINGTOGRID_FIELD, isAdjustingToGrid()); |
382 |
state.set(ISEDITABLE_FIELD, isEditable()); |
383 |
state.set(NUMBEHIND_FIELD, numBehind); |
384 |
state.set(NUMBEFORE_FIELD, numBefore); |
385 |
state.set(ATTRIBUTES_FIELD, m_attributes); |
386 |
state.set(FFRAMES_FIELD, fframes); |
387 |
|
388 |
state.set(IS_RULER_VISIBLE, this.getRuler());
|
389 |
state.set(IS_GRID_VISIBLE, this.isGridVisible());
|
390 |
} |
391 |
|
392 |
public void setNumBefore(int numBefore) { |
393 |
this.numBefore = numBefore;
|
394 |
|
395 |
} |
396 |
|
397 |
public void setNumBehind(int numBehind) { |
398 |
this.numBehind = numBehind;
|
399 |
} |
400 |
|
401 |
public void addObserver(Observer o) { |
402 |
observers.addObserver(o); |
403 |
} |
404 |
|
405 |
public void deleteObserver(Observer o) { |
406 |
observers.deleteObserver(o); |
407 |
} |
408 |
|
409 |
public void deleteObservers() { |
410 |
observers.deleteObservers(); |
411 |
} |
412 |
} |