Statistics
| Revision:

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

History | View | Annotate | Download (10.3 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)) {
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
        commands.setDescription(description);
235
            cr.pushCommand(commands);
236
        complex = false;
237
        PluginServices.getMainFrame().enableControls();
238
    }
239

    
240
    /**
241
     * Undo add FFrame from index.
242
     *
243
     * @param index
244
     */
245
    public void undoAddFFrame(int index) {
246
        doRemoveFFrame(index);
247
        numAdd--;
248
    }
249

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

    
261
        return fframes.size() - 1;
262
    }
263

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

    
276
    /**
277
     * Undo modify an FFrame modified.
278
     *
279
     * @param fant Previous fframe.
280
     * @param fnew New FFrame.
281
     * @param indexAnt Actual index.
282
     * @param indexLast Previous index.
283
     */
284
    public void undoModifyFFrame(IFFrame fant, IFFrame fnew, int indexAnt,
285
        int indexLast) {
286
        doAddFFrame(fant, indexAnt);
287
        undoAddFFrame(indexLast);
288
    }
289

    
290
    /**
291
     * Modify FFrame from index and new FFrame.
292
     *
293
     * @param indexAnt Actual index.
294
     * @param frameNext New FFrame.
295
     *
296
     * @return New index of FFrame.
297
     */
298
    public int doModifyFFrame(int indexAnt, IFFrame frameNext) {
299
        doRemoveFFrame(indexAnt);
300

    
301
        return doAddFFrame(frameNext);
302
    }
303

    
304
    /**
305
     * Undo Remove FFrame from index.
306
     *
307
     * @param index Actual index of FFrame.
308
     */
309
    public void undoRemoveFFrame(int index) {
310
        invalidates.set(index, false);
311
    }
312

    
313
    /**
314
     * Remove FFrame from actual index.
315
     *
316
     * @param index Actual index.
317
     */
318
    public void doRemoveFFrame(int index) {
319
        invalidates.set(index, true);
320
    }
321

    
322
    /**
323
     * Returns all the fframes that are not removed.
324
     *
325
     * @return Vector with fframes.
326
     */
327
    public IFFrame[] getFFrames() {
328
        ArrayList frames = new ArrayList();
329

    
330
        for (int i = 0; i < getFFrameCount(); i++) {
331
            if (!invalidates.get(i)) {
332
                frames.add(fframes.get(i));
333
            }
334
        }
335

    
336
        IFFrame[] fframesV = (IFFrame[]) frames.toArray(new IFFrame[0]);
337

    
338
        return fframesV;
339
    }
340

    
341
    /**
342
     * Returns all the fframes, remove and done not remove.
343
     *
344
     * @return All FFrames.
345
     */
346
    public IFFrame[] getAllFFrames() {
347
        return (IFFrame[]) fframes.toArray(new IFFrame[0]);
348
    }
349

    
350
    /**
351
     * Returns all the fframes, remove and done not remove and the ones that are in the FFrameGroups
352
     *
353
     * @return All FFrames.
354
     */
355
    public IFFrame[] getAllFFrameToDepends() {
356
        IFFrame[] fs = getAllFFrames();
357

    
358
        return getFFrameToDepends(fs);
359
    }
360

    
361
    private IFFrame[] getFFrameToDepends(IFFrame[] fs) {
362
        ArrayList result = new ArrayList();
363

    
364
        for (int i = 0; i < fs.length; i++) {
365
            result.add(fs[i]);
366

    
367
            if (fs[i] instanceof FFrameGroup) {
368
                IFFrame[] ffs = getFFrameToDepends(((FFrameGroup) fs[i]).getFFrames());
369

    
370
                for (int j = 0; j < ffs.length; j++) {
371
                    result.add(ffs[j]);
372
                }
373
            }
374
        }
375

    
376
        return (IFFrame[]) result.toArray(new IFFrame[0]);
377
    }
378

    
379
    private void refreshDependences(IFFrame fant, IFFrame fnew) {
380
        for (int i = 0; i < fframes.size(); i++) {
381
            if (fframes.get(i) instanceof IFFrameViewDependence) {
382
                IFFrameViewDependence f = (IFFrameViewDependence) fframes.get(i);
383

    
384
                if ((f.getFFrameDependence() != null) &&
385
                        f.getFFrameDependence().equals(fant)) {
386
                    f.setFFrameDependence(fnew);
387
                }
388
            }
389
        }
390
    }
391

    
392
    /**
393
     * Returns the command record.
394
     *
395
     * @return CommandRecord.
396
     */
397
    public CommandRecord getCommandRecord() {
398
        return cr;
399
    }
400
}