root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / commands / DefaultEditableFeatureSource.java @ 21299
History | View | Annotate | Download (9.97 KB)
1 |
/* 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.cit.gvsig.project.documents.layout.commands; |
42 |
|
43 |
import java.awt.Image; |
44 |
import java.util.ArrayList; |
45 |
import java.util.BitSet; |
46 |
|
47 |
import org.gvsig.data.commands.AbstractCommand; |
48 |
import org.gvsig.data.commands.Command; |
49 |
import org.gvsig.data.commands.CommandsRecord; |
50 |
|
51 |
import com.iver.andami.PluginServices; |
52 |
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameGroup; |
53 |
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame; |
54 |
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameViewDependence; |
55 |
|
56 |
|
57 |
/**
|
58 |
* Class responsible for the edition of the FFrames.
|
59 |
*
|
60 |
* @author Vicente Caballero Navarro
|
61 |
*/
|
62 |
public class DefaultEditableFeatureSource implements EditableFeatureSource { |
63 |
private ArrayList<IFFrame> fframes = new ArrayList<IFFrame>(); |
64 |
private CommandsRecord cr;
|
65 |
private BitSet invalidates = new BitSet(); |
66 |
private boolean complex = false; |
67 |
private int numAdd = 0; |
68 |
|
69 |
public DefaultEditableFeatureSource() {
|
70 |
this.cr = new FrameCommandsRecord(); |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Returns from an index the FFrame.
|
75 |
*
|
76 |
* @param index
|
77 |
*
|
78 |
* @return FFrame.
|
79 |
*/
|
80 |
public IFFrame getFFrame(int index) { |
81 |
return fframes.get(index);
|
82 |
} |
83 |
|
84 |
/**
|
85 |
* Returns the number of FFrame.
|
86 |
*
|
87 |
* @return Number of FFrames
|
88 |
*/
|
89 |
public int getFFrameCount() { |
90 |
return fframes.size();
|
91 |
} |
92 |
|
93 |
/**
|
94 |
* Add a FFrame in the mechanism of control creating a command.
|
95 |
*
|
96 |
* @param f FFrame to add
|
97 |
*/
|
98 |
public void addFFrame(IFFrame f) { |
99 |
int virtualIndex = doAddFFrame(f);
|
100 |
AbstractCommand command=new InsertFrameCommand(this, f, virtualIndex); |
101 |
command.setDescription(f.getNameFFrame()); |
102 |
if (complex) {
|
103 |
commands.add(command); |
104 |
} else {
|
105 |
cr.add(command); |
106 |
PluginServices.getMainFrame().enableControls(); |
107 |
} |
108 |
} |
109 |
|
110 |
/**
|
111 |
* Undo the last command added.
|
112 |
* @throws EditionCommandException
|
113 |
*/
|
114 |
public void undo() { |
115 |
if (moreUndoCommands()) {
|
116 |
cr.undo(); |
117 |
} |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Redo the last command undid.
|
122 |
* @throws EditionCommandException
|
123 |
*/
|
124 |
public void redo() { |
125 |
if (moreRedoCommands()) {
|
126 |
cr.redo(); |
127 |
} |
128 |
} |
129 |
|
130 |
/**
|
131 |
* Returns if there are more commands to undo
|
132 |
*
|
133 |
* @return True if there are more commands to undo
|
134 |
*/
|
135 |
public boolean moreUndoCommands() { |
136 |
return cr.moreUndoCommands();
|
137 |
} |
138 |
|
139 |
/**
|
140 |
* Returns if there are more commands to redo
|
141 |
*
|
142 |
* @return True if there are more commands to redo
|
143 |
*/
|
144 |
public boolean moreRedoCommands() { |
145 |
return cr.moreRedoCommands();
|
146 |
} |
147 |
|
148 |
/**
|
149 |
* Remove the FFrame by the index.
|
150 |
*
|
151 |
* @param index
|
152 |
*/
|
153 |
public void removeFFrame(int index) { |
154 |
if (invalidates.get(index)) {
|
155 |
return;
|
156 |
} |
157 |
IFFrame frame=getFFrame(index); |
158 |
doRemoveFFrame(index); |
159 |
AbstractCommand command=new DeleteFrameCommand(this, index); |
160 |
command.setDescription(frame.getNameFFrame()); |
161 |
if (complex) {
|
162 |
commands.add(command); |
163 |
} else {
|
164 |
cr.add(command); |
165 |
PluginServices.getMainFrame().enableControls(); |
166 |
} |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Modify a fframe to another fframe new.
|
171 |
*
|
172 |
* @param fant Previous Fframe.
|
173 |
* @param fnew New FFrame.
|
174 |
*/
|
175 |
public boolean modifyFFrame(IFFrame fant, IFFrame fnew) { |
176 |
int posAnt = -1; |
177 |
|
178 |
for (int i = 0; i < fframes.size(); i++) { |
179 |
if (fframes.get(i).equals(fant) && !invalidates.get(i)) {
|
180 |
posAnt = i; |
181 |
} |
182 |
} |
183 |
if (posAnt==-1) |
184 |
return false; |
185 |
int pos = doModifyFFrame(posAnt, fnew);
|
186 |
AbstractCommand command=new UpdateFrameCommand(this, fnew, posAnt, pos); |
187 |
command.setDescription(fant.getNameFFrame()); |
188 |
if (complex) {
|
189 |
commands.add(command); |
190 |
} else {
|
191 |
cr.add(command); |
192 |
PluginServices.getMainFrame().enableControls(); |
193 |
} |
194 |
|
195 |
refreshDependences(fant, fnew); |
196 |
return true; |
197 |
} |
198 |
|
199 |
public void setImage(Image i) { |
200 |
// TODO Auto-generated method stub
|
201 |
} |
202 |
|
203 |
public Image getImage() { |
204 |
// TODO Auto-generated method stub
|
205 |
return null; |
206 |
} |
207 |
|
208 |
/**
|
209 |
* Start a composed command of other simpler commands.
|
210 |
* Create an only one command to reproduce if all at once.
|
211 |
*/
|
212 |
public void startComplexCommand() { |
213 |
complex = true;
|
214 |
commands = new CommandCollection();
|
215 |
} |
216 |
|
217 |
/**
|
218 |
* Terminate a composed command.
|
219 |
*/
|
220 |
public void endComplexCommand(String description) { |
221 |
if (commands.isEmpty()) {
|
222 |
complex = false;
|
223 |
return;
|
224 |
} |
225 |
commands.setDescription(description); |
226 |
cr.pushCommand(commands); |
227 |
complex = false;
|
228 |
PluginServices.getMainFrame().enableControls(); |
229 |
} |
230 |
|
231 |
/**
|
232 |
* Undo add FFrame from index.
|
233 |
*
|
234 |
* @param index
|
235 |
*/
|
236 |
public void undoAddFFrame(int index) { |
237 |
doRemoveFFrame(index); |
238 |
numAdd--; |
239 |
} |
240 |
|
241 |
/**
|
242 |
* Add FFrame.
|
243 |
*
|
244 |
* @param frame
|
245 |
*
|
246 |
* @return index of new fframe.
|
247 |
*/
|
248 |
public int doAddFFrame(IFFrame frame) { |
249 |
fframes.add(frame); |
250 |
numAdd++; |
251 |
|
252 |
return fframes.size() - 1; |
253 |
} |
254 |
|
255 |
/**
|
256 |
* Add FFrame from index.
|
257 |
*
|
258 |
* @param frame New FFrame.
|
259 |
* @param index Index of new FFrame.
|
260 |
*/
|
261 |
public void doAddFFrame(IFFrame frame, int index) { |
262 |
invalidates.set(index, false);
|
263 |
fframes.set(index, frame); |
264 |
numAdd++; |
265 |
} |
266 |
|
267 |
/**
|
268 |
* Undo modify an FFrame modified.
|
269 |
*
|
270 |
* @param fant Previous fframe.
|
271 |
* @param fnew New FFrame.
|
272 |
* @param indexAnt Actual index.
|
273 |
* @param indexLast Previous index.
|
274 |
*/
|
275 |
public void undoModifyFFrame(int index, |
276 |
int newIndex) {
|
277 |
undoRemoveFFrame(index); |
278 |
undoAddFFrame(newIndex); |
279 |
IFFrame fant=fframes.get(newIndex); |
280 |
IFFrame fnew=fframes.get(index); |
281 |
refreshDependences(fant,fnew); |
282 |
} |
283 |
|
284 |
/**
|
285 |
* Modify FFrame from index and new FFrame.
|
286 |
*
|
287 |
* @param indexAnt Actual index.
|
288 |
* @param frameNext New FFrame.
|
289 |
*
|
290 |
* @return New index of FFrame.
|
291 |
*/
|
292 |
public int doModifyFFrame(int indexAnt, IFFrame frameNext) { |
293 |
doRemoveFFrame(indexAnt); |
294 |
|
295 |
return doAddFFrame(frameNext);
|
296 |
} |
297 |
|
298 |
/**
|
299 |
* Undo Remove FFrame from index.
|
300 |
*
|
301 |
* @param index Actual index of FFrame.
|
302 |
*/
|
303 |
public void undoRemoveFFrame(int index) { |
304 |
invalidates.set(index, false);
|
305 |
} |
306 |
|
307 |
/**
|
308 |
* Remove FFrame from actual index.
|
309 |
*
|
310 |
* @param index Actual index.
|
311 |
*/
|
312 |
public void doRemoveFFrame(int index) { |
313 |
invalidates.set(index, true);
|
314 |
} |
315 |
|
316 |
/**
|
317 |
* Returns all the fframes that are not removed.
|
318 |
*
|
319 |
* @return Vector with fframes.
|
320 |
*/
|
321 |
public IFFrame[] getFFrames() { |
322 |
ArrayList<IFFrame> frames = new ArrayList<IFFrame>(); |
323 |
|
324 |
for (int i = 0; i < getFFrameCount(); i++) { |
325 |
if (!invalidates.get(i)) {
|
326 |
frames.add(fframes.get(i)); |
327 |
} |
328 |
} |
329 |
|
330 |
IFFrame[] fframesV = frames.toArray(new IFFrame[0]); |
331 |
|
332 |
return fframesV;
|
333 |
} |
334 |
|
335 |
/**
|
336 |
* Returns all the fframes, remove and done not remove.
|
337 |
*
|
338 |
* @return All FFrames.
|
339 |
*/
|
340 |
public IFFrame[] getAllFFrames() { |
341 |
return fframes.toArray(new IFFrame[0]); |
342 |
} |
343 |
|
344 |
/**
|
345 |
* Returns all the fframes, remove and done not remove and the ones that are in the FFrameGroups
|
346 |
*
|
347 |
* @return All FFrames.
|
348 |
*/
|
349 |
public IFFrame[] getAllFFrameToDepends() { |
350 |
IFFrame[] fs = getAllFFrames();
|
351 |
|
352 |
return getFFrameToDepends(fs);
|
353 |
} |
354 |
|
355 |
private IFFrame[] getFFrameToDepends(IFFrame[] fs) { |
356 |
ArrayList<IFFrame> result = new ArrayList<IFFrame>(); |
357 |
|
358 |
for (int i = 0; i < fs.length; i++) { |
359 |
result.add(fs[i]); |
360 |
|
361 |
if (fs[i] instanceof FFrameGroup) { |
362 |
IFFrame[] ffs = getFFrameToDepends(((FFrameGroup) fs[i]).getFFrames());
|
363 |
|
364 |
for (int j = 0; j < ffs.length; j++) { |
365 |
result.add(ffs[j]); |
366 |
} |
367 |
} |
368 |
} |
369 |
|
370 |
return result.toArray(new IFFrame[0]); |
371 |
} |
372 |
|
373 |
private void refreshDependences(IFFrame fant, IFFrame fnew) { |
374 |
for (int i = 0; i < fframes.size(); i++) { |
375 |
if (fframes.get(i) instanceof IFFrameViewDependence) { |
376 |
IFFrameViewDependence f = (IFFrameViewDependence) fframes.get(i); |
377 |
f.refreshDependence(fant, fnew); |
378 |
} |
379 |
} |
380 |
} |
381 |
|
382 |
/**
|
383 |
* Returns the command record.
|
384 |
*
|
385 |
* @return CommandRecord.
|
386 |
*/
|
387 |
public CommandsRecord getCommandRecord() {
|
388 |
return cr;
|
389 |
} |
390 |
|
391 |
public void redoModifyFFrame(int index, int newIndex, IFFrame frame) { |
392 |
doRemoveFFrame(index); |
393 |
doAddFFrame(frame,newIndex); |
394 |
IFFrame fant=fframes.get(newIndex); |
395 |
refreshDependences(fant,frame); |
396 |
} |
397 |
} |