Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / commands / DefaultEditableFeatureSource.java @ 9532

History | View | Annotate | Download (10.7 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.io.IOException;
45
import java.util.ArrayList;
46
import java.util.BitSet;
47

    
48
import com.iver.andami.PluginServices;
49
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
50
import com.iver.cit.gvsig.fmap.edition.commands.Command;
51
import com.iver.cit.gvsig.fmap.edition.commands.CommandCollection;
52
import com.iver.cit.gvsig.fmap.edition.commands.CommandRecord;
53
import com.iver.cit.gvsig.fmap.edition.commands.MemoryCommandRecord;
54
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameGroup;
55
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
56
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameViewDependence;
57

    
58

    
59
/**
60
 * Class responsible for the edition of the FFrames.
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class DefaultEditableFeatureSource implements EditableFeatureSource {
65
    private ArrayList fframes = new ArrayList();
66
    private CommandRecord cr;
67
    private BitSet invalidates = new BitSet();
68
    private boolean complex = false;
69
    private CommandCollection commands = null;
70
    private int numAdd = 0;
71

    
72
    public DefaultEditableFeatureSource() {
73
        this.cr = new MemoryCommandRecord();
74
    }
75

    
76
    /**
77
     * Returns from an index the FFrame.
78
     *
79
     * @param index
80
     *
81
     * @return FFrame.
82
     */
83
    public IFFrame getFFrame(int index) {
84
        return (IFFrame) fframes.get(index);
85
    }
86

    
87
    /**
88
     * Returns the number of FFrame.
89
     *
90
     * @return Number of FFrames
91
     */
92
    public int getFFrameCount() {
93
        return fframes.size();
94
    }
95

    
96
    /**
97
     * Add a FFrame in the mechanism of control creating a command.
98
     *
99
     * @param f FFrame to add
100
     */
101
    public void addFFrame(IFFrame f) {
102
        int virtualIndex = doAddFFrame(f);
103
        Command command=new AddFFrameCommand(this, f, virtualIndex);
104
        command.setDescription(f.getNameFFrame());
105
        if (complex) {
106
            commands.add(command);
107
        } else {
108
            cr.pushCommand(command);
109
            PluginServices.getMainFrame().enableControls();
110
        }
111
    }
112

    
113
    /**
114
     * Undo the last command added.
115
     */
116
    public void undo() {
117
        if (moreUndoCommands()) {
118
            try {
119
                cr.undoCommand();
120
            } catch (DriverIOException e) {
121
                e.printStackTrace();
122
            } catch (IOException e) {
123
                e.printStackTrace();
124
            }
125
        }
126
    }
127

    
128
    /**
129
     * Redo the last command undid.
130
     */
131
    public void redo() {
132
        if (moreRedoCommands()) {
133
            try {
134
                cr.redoCommand();
135
            } catch (DriverIOException e) {
136
                e.printStackTrace();
137
            } catch (IOException e) {
138
                e.printStackTrace();
139
            }
140
        }
141
    }
142

    
143
    /**
144
     * Returns if there are more commands to undo
145
     *
146
     * @return True if there are more commands to undo
147
     */
148
    public boolean moreUndoCommands() {
149
        return cr.moreUndoCommands();
150
    }
151

    
152
    /**
153
     * Returns if there are more commands to redo
154
     *
155
     * @return True if there are more commands to redo
156
     */
157
    public boolean moreRedoCommands() {
158
        return cr.moreRedoCommands();
159
    }
160

    
161
    /**
162
     * Remove the FFrame by the index.
163
     *
164
     * @param index
165
     */
166
    public void removeFFrame(int index) {
167
        if (invalidates.get(index)) {
168
            return;
169
        }
170
        IFFrame frame=getFFrame(index);
171
        doRemoveFFrame(index);
172
        Command command=new RemoveFFrameCommand(this, index);
173
        command.setDescription(frame.getNameFFrame());
174
        if (complex) {
175
            commands.add(command);
176
        } else {
177
            cr.pushCommand(command);
178
            PluginServices.getMainFrame().enableControls();
179
        }
180
    }
181

    
182
    /**
183
     * Modify a fframe to another fframe new.
184
     *
185
     * @param fant Previous Fframe.
186
     * @param fnew New FFrame.
187
     */
188
    public boolean modifyFFrame(IFFrame fant, IFFrame fnew) {
189
        int posAnt = -1;
190

    
191
        for (int i = 0; i < fframes.size(); i++) {
192
            if (fframes.get(i).equals(fant) && !invalidates.get(i)) {
193
                    posAnt = i;
194
            }
195
        }
196
        if (posAnt==-1)
197
                return false;
198
        int pos = doModifyFFrame(posAnt, fnew);
199
        Command command=new ModifyFFrameCommand(this, fnew, posAnt, pos);
200
        command.setDescription(fant.getNameFFrame());
201
        if (complex) {
202
            commands.add(command);
203
        } else {
204
            cr.pushCommand(command);
205
            PluginServices.getMainFrame().enableControls();
206
        }
207

    
208
        refreshDependences(fant, fnew);
209
        return true;
210
    }
211

    
212
    public void setImage(Image i) {
213
        // TODO Auto-generated method stub
214
    }
215

    
216
    public Image getImage() {
217
        // TODO Auto-generated method stub
218
        return null;
219
    }
220

    
221
    /**
222
     * Start a composed command of other simpler commands.
223
     * Create an only one command to reproduce if all at once.
224
     */
225
    public void startComplexCommand() {
226
        complex = true;
227
        commands = new CommandCollection();
228
    }
229

    
230
    /**
231
     * Terminate a composed command.
232
     */
233
    public void endComplexCommand(String description) {
234
        if (commands.isEmpty()) {
235
                complex = false;
236
                return;
237
        }
238
            commands.setDescription(description);
239
            cr.pushCommand(commands);
240
        complex = false;
241
        PluginServices.getMainFrame().enableControls();
242
    }
243

    
244
    /**
245
     * Undo add FFrame from index.
246
     *
247
     * @param index
248
     */
249
    public void undoAddFFrame(int index) {
250
        doRemoveFFrame(index);
251
        numAdd--;
252
    }
253

    
254
    /**
255
     * Add FFrame.
256
     *
257
     * @param frame
258
     *
259
     * @return index of new fframe.
260
     */
261
    public int doAddFFrame(IFFrame frame) {
262
        fframes.add(frame);
263
        numAdd++;
264

    
265
        return fframes.size() - 1;
266
    }
267

    
268
    /**
269
     * Add FFrame from index.
270
     *
271
     * @param frame New FFrame.
272
     * @param index Index of new FFrame.
273
     */
274
    public void doAddFFrame(IFFrame frame, int index) {
275
        invalidates.set(index, false);
276
        fframes.set(index, frame);
277
        numAdd++;
278
    }
279

    
280
    /**
281
     * Undo modify an FFrame modified.
282
     *
283
     * @param fant Previous fframe.
284
     * @param fnew New FFrame.
285
     * @param indexAnt Actual index.
286
     * @param indexLast Previous index.
287
     */
288
    public void undoModifyFFrame(int index,
289
        int newIndex) {
290
            undoRemoveFFrame(index);
291
                undoAddFFrame(newIndex);
292
                IFFrame fant=(IFFrame)fframes.get(newIndex);
293
                IFFrame fnew=(IFFrame)fframes.get(index);
294
                refreshDependences(fant,fnew);
295
    }
296

    
297
    /**
298
     * Modify FFrame from index and new FFrame.
299
     *
300
     * @param indexAnt Actual index.
301
     * @param frameNext New FFrame.
302
     *
303
     * @return New index of FFrame.
304
     */
305
    public int doModifyFFrame(int indexAnt, IFFrame frameNext) {
306
        doRemoveFFrame(indexAnt);
307

    
308
        return doAddFFrame(frameNext);
309
    }
310

    
311
    /**
312
     * Undo Remove FFrame from index.
313
     *
314
     * @param index Actual index of FFrame.
315
     */
316
    public void undoRemoveFFrame(int index) {
317
        invalidates.set(index, false);
318
    }
319

    
320
    /**
321
     * Remove FFrame from actual index.
322
     *
323
     * @param index Actual index.
324
     */
325
    public void doRemoveFFrame(int index) {
326
        invalidates.set(index, true);
327
    }
328

    
329
    /**
330
     * Returns all the fframes that are not removed.
331
     *
332
     * @return Vector with fframes.
333
     */
334
    public IFFrame[] getFFrames() {
335
        ArrayList frames = new ArrayList();
336

    
337
        for (int i = 0; i < getFFrameCount(); i++) {
338
            if (!invalidates.get(i)) {
339
                frames.add(fframes.get(i));
340
            }
341
        }
342

    
343
        IFFrame[] fframesV = (IFFrame[]) frames.toArray(new IFFrame[0]);
344

    
345
        return fframesV;
346
    }
347

    
348
    /**
349
     * Returns all the fframes, remove and done not remove.
350
     *
351
     * @return All FFrames.
352
     */
353
    public IFFrame[] getAllFFrames() {
354
        return (IFFrame[]) fframes.toArray(new IFFrame[0]);
355
    }
356

    
357
    /**
358
     * Returns all the fframes, remove and done not remove and the ones that are in the FFrameGroups
359
     *
360
     * @return All FFrames.
361
     */
362
    public IFFrame[] getAllFFrameToDepends() {
363
        IFFrame[] fs = getAllFFrames();
364

    
365
        return getFFrameToDepends(fs);
366
    }
367

    
368
    private IFFrame[] getFFrameToDepends(IFFrame[] fs) {
369
        ArrayList result = new ArrayList();
370

    
371
        for (int i = 0; i < fs.length; i++) {
372
            result.add(fs[i]);
373

    
374
            if (fs[i] instanceof FFrameGroup) {
375
                IFFrame[] ffs = getFFrameToDepends(((FFrameGroup) fs[i]).getFFrames());
376

    
377
                for (int j = 0; j < ffs.length; j++) {
378
                    result.add(ffs[j]);
379
                }
380
            }
381
        }
382

    
383
        return (IFFrame[]) result.toArray(new IFFrame[0]);
384
    }
385

    
386
    private void refreshDependences(IFFrame fant, IFFrame fnew) {
387
        for (int i = 0; i < fframes.size(); i++) {
388
            if (fframes.get(i) instanceof IFFrameViewDependence) {
389
                IFFrameViewDependence f = (IFFrameViewDependence) fframes.get(i);
390

    
391
                if ((f.getFFrameDependence() != null) &&
392
                        f.getFFrameDependence().equals(fant)) {
393
                    f.setFFrameDependence(fnew);
394
                }
395
            }
396
        }
397
    }
398

    
399
    /**
400
     * Returns the command record.
401
     *
402
     * @return CommandRecord.
403
     */
404
    public CommandRecord getCommandRecord() {
405
        return cr;
406
    }
407

    
408
        public void redoModifyFFrame(int index, int newIndex, IFFrame frame) {
409
                doRemoveFFrame(index);
410
                doAddFFrame(frame,newIndex);
411
                IFFrame fant=(IFFrame)fframes.get(newIndex);
412
                refreshDependences(fant,frame);
413
        }
414
}