Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / commands / DefaultEditableFeatureSource.java @ 10626

History | View | Annotate | Download (10.4 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 com.iver.andami.PluginServices;
48
import com.iver.cit.gvsig.exceptions.commands.EditionCommandException;
49
import com.iver.cit.gvsig.fmap.edition.commands.Command;
50
import com.iver.cit.gvsig.fmap.edition.commands.CommandCollection;
51
import com.iver.cit.gvsig.fmap.edition.commands.CommandRecord;
52
import com.iver.cit.gvsig.fmap.edition.commands.MemoryCommandRecord;
53
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameGroup;
54
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
55
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameViewDependence;
56

    
57

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

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

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

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

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

    
112
    /**
113
     * Undo the last command added.
114
     * @throws EditionCommandException
115
     */
116
    public void undo() throws EditionCommandException {
117
        if (moreUndoCommands()) {
118
           cr.undoCommand();
119
        }
120
    }
121

    
122
    /**
123
     * Redo the last command undid.
124
     * @throws EditionCommandException
125
     */
126
    public void redo() throws EditionCommandException {
127
        if (moreRedoCommands()) {
128
           cr.redoCommand();
129
        }
130
    }
131

    
132
    /**
133
     * Returns if there are more commands to undo
134
     *
135
     * @return True if there are more commands to undo
136
     */
137
    public boolean moreUndoCommands() {
138
        return cr.moreUndoCommands();
139
    }
140

    
141
    /**
142
     * Returns if there are more commands to redo
143
     *
144
     * @return True if there are more commands to redo
145
     */
146
    public boolean moreRedoCommands() {
147
        return cr.moreRedoCommands();
148
    }
149

    
150
    /**
151
     * Remove the FFrame by the index.
152
     *
153
     * @param index
154
     */
155
    public void removeFFrame(int index) {
156
        if (invalidates.get(index)) {
157
            return;
158
        }
159
        IFFrame frame=getFFrame(index);
160
        doRemoveFFrame(index);
161
        Command command=new RemoveFFrameCommand(this, index);
162
        command.setDescription(frame.getNameFFrame());
163
        if (complex) {
164
            commands.add(command);
165
        } else {
166
            cr.pushCommand(command);
167
            PluginServices.getMainFrame().enableControls();
168
        }
169
    }
170

    
171
    /**
172
     * Modify a fframe to another fframe new.
173
     *
174
     * @param fant Previous Fframe.
175
     * @param fnew New FFrame.
176
     */
177
    public boolean modifyFFrame(IFFrame fant, IFFrame fnew) {
178
        int posAnt = -1;
179

    
180
        for (int i = 0; i < fframes.size(); i++) {
181
            if (fframes.get(i).equals(fant) && !invalidates.get(i)) {
182
                    posAnt = i;
183
            }
184
        }
185
        if (posAnt==-1)
186
                return false;
187
        int pos = doModifyFFrame(posAnt, fnew);
188
        Command command=new ModifyFFrameCommand(this, fnew, posAnt, pos);
189
        command.setDescription(fant.getNameFFrame());
190
        if (complex) {
191
            commands.add(command);
192
        } else {
193
            cr.pushCommand(command);
194
            PluginServices.getMainFrame().enableControls();
195
        }
196

    
197
        refreshDependences(fant, fnew);
198
        return true;
199
    }
200

    
201
    public void setImage(Image i) {
202
        // TODO Auto-generated method stub
203
    }
204

    
205
    public Image getImage() {
206
        // TODO Auto-generated method stub
207
        return null;
208
    }
209

    
210
    /**
211
     * Start a composed command of other simpler commands.
212
     * Create an only one command to reproduce if all at once.
213
     */
214
    public void startComplexCommand() {
215
        complex = true;
216
        commands = new CommandCollection();
217
    }
218

    
219
    /**
220
     * Terminate a composed command.
221
     */
222
    public void endComplexCommand(String description) {
223
        if (commands.isEmpty()) {
224
                complex = false;
225
                return;
226
        }
227
            commands.setDescription(description);
228
            cr.pushCommand(commands);
229
        complex = false;
230
        PluginServices.getMainFrame().enableControls();
231
    }
232

    
233
    /**
234
     * Undo add FFrame from index.
235
     *
236
     * @param index
237
     */
238
    public void undoAddFFrame(int index) {
239
        doRemoveFFrame(index);
240
        numAdd--;
241
    }
242

    
243
    /**
244
     * Add FFrame.
245
     *
246
     * @param frame
247
     *
248
     * @return index of new fframe.
249
     */
250
    public int doAddFFrame(IFFrame frame) {
251
        fframes.add(frame);
252
        numAdd++;
253

    
254
        return fframes.size() - 1;
255
    }
256

    
257
    /**
258
     * Add FFrame from index.
259
     *
260
     * @param frame New FFrame.
261
     * @param index Index of new FFrame.
262
     */
263
    public void doAddFFrame(IFFrame frame, int index) {
264
        invalidates.set(index, false);
265
        fframes.set(index, frame);
266
        numAdd++;
267
    }
268

    
269
    /**
270
     * Undo modify an FFrame modified.
271
     *
272
     * @param fant Previous fframe.
273
     * @param fnew New FFrame.
274
     * @param indexAnt Actual index.
275
     * @param indexLast Previous index.
276
     */
277
    public void undoModifyFFrame(int index,
278
        int newIndex) {
279
            undoRemoveFFrame(index);
280
                undoAddFFrame(newIndex);
281
                IFFrame fant=(IFFrame)fframes.get(newIndex);
282
                IFFrame fnew=(IFFrame)fframes.get(index);
283
                refreshDependences(fant,fnew);
284
    }
285

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

    
297
        return doAddFFrame(frameNext);
298
    }
299

    
300
    /**
301
     * Undo Remove FFrame from index.
302
     *
303
     * @param index Actual index of FFrame.
304
     */
305
    public void undoRemoveFFrame(int index) {
306
        invalidates.set(index, false);
307
    }
308

    
309
    /**
310
     * Remove FFrame from actual index.
311
     *
312
     * @param index Actual index.
313
     */
314
    public void doRemoveFFrame(int index) {
315
        invalidates.set(index, true);
316
    }
317

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

    
326
        for (int i = 0; i < getFFrameCount(); i++) {
327
            if (!invalidates.get(i)) {
328
                frames.add(fframes.get(i));
329
            }
330
        }
331

    
332
        IFFrame[] fframesV = (IFFrame[]) frames.toArray(new IFFrame[0]);
333

    
334
        return fframesV;
335
    }
336

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

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

    
354
        return getFFrameToDepends(fs);
355
    }
356

    
357
    private IFFrame[] getFFrameToDepends(IFFrame[] fs) {
358
        ArrayList result = new ArrayList();
359

    
360
        for (int i = 0; i < fs.length; i++) {
361
            result.add(fs[i]);
362

    
363
            if (fs[i] instanceof FFrameGroup) {
364
                IFFrame[] ffs = getFFrameToDepends(((FFrameGroup) fs[i]).getFFrames());
365

    
366
                for (int j = 0; j < ffs.length; j++) {
367
                    result.add(ffs[j]);
368
                }
369
            }
370
        }
371

    
372
        return (IFFrame[]) result.toArray(new IFFrame[0]);
373
    }
374

    
375
    private void refreshDependences(IFFrame fant, IFFrame fnew) {
376
        for (int i = 0; i < fframes.size(); i++) {
377
            if (fframes.get(i) instanceof IFFrameViewDependence) {
378
                IFFrameViewDependence f = (IFFrameViewDependence) fframes.get(i);
379

    
380
                if ((f.getFFrameDependence() != null) &&
381
                        f.getFFrameDependence().equals(fant)) {
382
                    f.setFFrameDependence(fnew);
383
                }
384
            }
385
        }
386
    }
387

    
388
    /**
389
     * Returns the command record.
390
     *
391
     * @return CommandRecord.
392
     */
393
    public CommandRecord getCommandRecord() {
394
        return cr;
395
    }
396

    
397
        public void redoModifyFFrame(int index, int newIndex, IFFrame frame) {
398
                doRemoveFFrame(index);
399
                doAddFFrame(frame,newIndex);
400
                IFFrame fant=(IFFrame)fframes.get(newIndex);
401
                refreshDependences(fant,frame);
402
        }
403
}