Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / DefaultLayoutContext.java @ 772

History | View | Annotate | Download (16.6 KB)

1 5 jldominguez
/* 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 140 cmartinez
import java.lang.reflect.Array;
26 5 jldominguez
import java.util.ArrayList;
27 433 jjdelcerro
import java.util.Arrays;
28 5 jldominguez
import java.util.Hashtable;
29 433 jjdelcerro
import java.util.Iterator;
30
import java.util.List;
31 5 jldominguez
32
import org.gvsig.andami.PluginServices;
33
import org.gvsig.app.project.ProjectManager;
34
import org.gvsig.app.project.documents.layout.commands.FrameCommandsRecord;
35
import org.gvsig.app.project.documents.layout.commands.FrameManager;
36
import org.gvsig.app.project.documents.layout.fframes.FFrame;
37
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup;
38
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
39
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap;
40 147 cmartinez
import org.gvsig.app.project.documents.layout.toc.TocModelObserver;
41
import org.gvsig.fmap.mapcontext.MapContext;
42 5 jldominguez
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.dynobject.DynStruct;
44
import org.gvsig.tools.observer.ObservableHelper;
45
import org.gvsig.tools.observer.Observer;
46
import org.gvsig.tools.persistence.PersistenceManager;
47
import org.gvsig.tools.persistence.PersistentState;
48
import org.gvsig.tools.persistence.exception.PersistenceException;
49
50
/**
51
 * Model of LayoutControl.
52
 *
53
 * @author Vicente Caballero Navarro
54
 */
55
public class DefaultLayoutContext implements LayoutContext {
56
57
    public static final String PERSISTENCE_DEFINITION_NAME = "LayoutContext";
58
59
    private static final String ISADJUSTINGTOGRID_FIELD = "isAdjustingToGrid";
60 106 jldominguez
    private static final String IS_GRID_VISIBLE = "IS_GRID_VISIBLE";
61
    private static final String IS_RULER_VISIBLE = "IS_RULER_VISIBLE";
62 5 jldominguez
    private static final String ISEDITABLE_FIELD = "isEditable";
63 237 cmartinez
    /**
64
     * True when the document window has been opened at least once, false otherwise. This is required
65
     * to decide whether the page setting dialog has to be shown to the user, otherwise it would be
66
     * shown every time the document is opened from project persistence
67
     */
68
    private static final String HAS_WINDOW_BEEN_EVER_OPEN_FIELD = "hasDocBeenOpened";
69 106 jldominguez
70 5 jldominguez
    private static final String NUMBEHIND_FIELD = "numBehind";
71
    private static final String NUMBEFORE_FIELD = "numBefore";
72
    private static final String ATTRIBUTES_FIELD = "attributes";
73
    private static final String FFRAMES_FIELD = "fframes";
74
75
    private static DefaultLayoutManager layoutManager = null;
76
77
    private Attributes m_attributes = null;
78
    private IFFrame[] fframes;
79
    private FrameCommandsRecord fcr;
80
    private static Hashtable nums = new Hashtable();
81
    private int numBefore = 0;
82
    private int numBehind = 0;
83
    private boolean isEditable = true;
84
    private Boolean adjustToGrid = null;
85 237 cmartinez
    private boolean hasDocBeenOpened = false;
86 112 jldominguez
87 5 jldominguez
    private Boolean m_showRuler;
88
    private Boolean isGridVisible = null;
89 112 jldominguez
90 5 jldominguez
    private AffineTransform m_MatrizTransf;
91
92
    private ObservableHelper observers;
93 147 cmartinez
    private CustomizableObserverHelper tocObservers = new CustomizableObserverHelper() {
94
95
                @Override
96
                protected void doNotify(Object listener, Object data) {
97
                        TocModelObserver l = (TocModelObserver) listener;
98
                        l.tocUpdated(DefaultLayoutContext.this, (TocModelChangedNotification) data);
99
                }
100
        };
101
102
        private MapContext tocModel = null;
103 5 jldominguez
104
    /**
105
     * Create a new object of LayoutContext.
106
     */
107
    public DefaultLayoutContext() {
108
        layoutManager =
109
            (DefaultLayoutManager) ProjectManager.getInstance()
110
                .getDocumentManager(DefaultLayoutManager.TYPENAME);
111
        observers = new ObservableHelper();
112
        m_attributes = new Attributes();
113
        m_MatrizTransf = new AffineTransform();
114
        m_MatrizTransf.setToIdentity();
115
        FrameManager fm = new FrameManager();
116
        fcr = new FrameCommandsRecord(fm);
117
        updateFFrames();
118
    }
119
120
    public AffineTransform getAT() {
121
        return m_MatrizTransf;
122
    }
123
124
    public Attributes getAttributes() {
125
        return m_attributes;
126
    }
127
128
    public void setAtributes(Attributes attributes) {
129
        m_attributes = attributes;
130
    }
131
132
    public IFFrame[] getFFrames() {
133
        return fframes;
134
    }
135
136
    public IFFrame getFFrame(int i) {
137
        return fframes[i];
138
    }
139
140
    public void updateFFrames() {
141
        ArrayList frames = new ArrayList();
142
        IFFrame[] auxfframes = fcr.getFrameManager().getFFrames();
143
        for (int j = numBehind; j <= numBefore; j++) {
144
            for (int i = 0; i < auxfframes.length; i++) {
145
                if (auxfframes[i].getLevel() == j) {
146
                    frames.add(auxfframes[i]);
147
                    continue;
148
                }
149
            }
150
        }
151
        fframes = (IFFrame[]) frames.toArray(new IFFrame[0]);
152
    }
153
154
    public void delFFrameSelected() {
155
        fcr.startComplex(PluginServices.getText(this, "remove_elements"));
156 145 cmartinez
        IFFrame[] validFrames = fcr.getFrameManager().getFFrames();
157
        for (int i = 0; i<validFrames.length; i++) {
158
            IFFrame fframe = validFrames[i];
159 5 jldominguez
160 144 cmartinez
            if (fframe.isSelected()) {
161 103 jldominguez
                fframe.setSelected(false);
162 5 jldominguez
                fcr.delete(fframe);
163
            }
164
        }
165
        fcr.endComplex();
166
        updateFFrames();
167
    }
168
169
    public void clearSelection() {
170
        for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) {
171
            IFFrame fframe = fcr.getFrameManager().getFFrame(i);
172
            if (fframe.getSelected() != IFFrame.NOSELECT) {
173
                fframe.setSelected(false);
174
            }
175
        }
176
    }
177
178
    public void delFFrame(int index) {
179 144 cmartinez
            IFFrame frame = getFFrame(index);
180
            delFFrame(frame);
181 5 jldominguez
    }
182
183
    public void delFFrame(IFFrame frame) {
184
        for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) {
185
            if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
186 144 cmartinez
                    frame.setSelected(false);
187 5 jldominguez
                fcr.delete(frame);
188
            }
189
        }
190
        updateFFrames();
191
    }
192
193
    public FrameCommandsRecord getFrameCommandsRecord() {
194
        return fcr;
195
    }
196
197
    public void addFFrame(IFFrame frame, boolean clearSelection, boolean select) {
198 237 cmartinez
            frame.setLayoutContext(this);
199 5 jldominguez
        IFFrame[] fframes = getFFrames();
200
        if (clearSelection) {
201
            for (int i = fframes.length - 1; i >= 0; i--) {
202
                IFFrame fframe1 = fframes[i];
203
                fframe1.setSelected(false);
204
            }
205
        }
206
207
        if (nums.containsKey(frame.getClass())) {
208
            nums.put(
209
                frame.getClass(),
210
                new Integer(Integer.parseInt(nums.get(frame.getClass())
211
                    .toString()) + 1));
212
        } else {
213
            nums.put(frame.getClass(), new Integer(0));
214
        }
215
216
        frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
217
        fcr.insert(frame);
218
        frame.setSelected(select);
219
        frame.setLevel(getNumBefore());
220
        updateFFrames();
221
    }
222
223
    public void addFFrameSameProperties(IFFrame frame) {
224
        fcr.insert(frame);
225
        frame.setSelected(true);
226
        frame.setLevel(getNumBefore());
227
        updateFFrames();
228
    }
229
230
    public int getNumBehind() {
231
        return --numBehind;
232
    }
233
234
    public int getNumBefore() {
235
        return ++numBefore;
236
    }
237
238
    public IFFrame[] getAllFFrames() {
239 433 jjdelcerro
        List all = new ArrayList();
240 5 jldominguez
        return (IFFrame[]) allFFrames(getFFrames(), all)
241
            .toArray(new IFFrame[0]);
242
    }
243
244 433 jjdelcerro
    public Iterator deepiterator() {
245
        List all = new ArrayList();
246
        allFFrames(getFFrames(), all);
247
        return all.iterator();
248
    }
249
250
    public Iterator iterator() {
251
        return Arrays.asList(fframes).iterator();
252
    }
253
254
    private List allFFrames(IFFrame[] fframes, List all) {
255 5 jldominguez
        for (int i = 0; i < fframes.length; i++) {
256
            if (fframes[i] instanceof FFrameGroup) {
257 433 jjdelcerro
                List groupFrames =
258 5 jldominguez
                    allFFrames(((FFrameGroup) fframes[i]).getFFrames(), all);
259
                if (!all.containsAll(groupFrames)) {
260
                    all.addAll(groupFrames);
261
                }
262
263
            } else {
264
                if (!all.contains(fframes[i])) {
265
                    all.add(fframes[i]);
266
                }
267
            }
268
        }
269
        return all;
270
    }
271
272 140 cmartinez
    public IFFrame[] getSelectedFFrames() {
273 5 jldominguez
        ArrayList selecList = new ArrayList();
274
        IFFrame[] fframes = getFFrames();
275
        for (int i = fframes.length - 1; i >= 0; i--) {
276
            IFFrame fframe = fframes[i];
277
278
            if (fframe.getSelected() != IFFrame.NOSELECT) {
279
                selecList.add(fframe);
280
            }
281
        }
282 140 cmartinez
        return (IFFrame[]) selecList.toArray(new IFFrame[selecList.size()]);
283 5 jldominguez
    }
284
285 140 cmartinez
        public <T> T[] getSelectedFFrames(Class<T> clazz) {
286
        ArrayList<T> selecList = new ArrayList<T>();
287
        IFFrame[] fframes = getFFrames();
288
        for (int i = fframes.length - 1; i >= 0; i--) {
289
                IFFrame fframe = fframes[i];
290
                if (clazz.isInstance(fframe)) {
291
                if (fframe.getSelected() != IFFrame.NOSELECT) {
292
                    selecList.add((T)fframe);
293
                }
294
                }
295
        }
296
        T[] newInstance = (T[])Array.newInstance(clazz, selecList.size());
297
                return selecList.toArray(newInstance);
298
        }
299
300 5 jldominguez
    public boolean isEditable() {
301
        return isEditable;
302
    }
303
304
    public void setEditable(boolean b) {
305
        if (!b) {
306
            clearSelection();
307
            // layoutControl.setTool("layoutzoomin");
308
            PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
309
        }
310
        isEditable = b;
311
312
    }
313
314
    public boolean isAdjustingToGrid() {
315
        if (adjustToGrid == null) {
316
            adjustToGrid = new Boolean(layoutManager.getDefaultAdjustToGrid());
317
        }
318
        return adjustToGrid.booleanValue();
319
    }
320
321
    public void setAdjustToGrid(boolean b) {
322
        adjustToGrid = new Boolean(b);
323
    }
324
325
    public void setRuler(boolean b) {
326
        m_showRuler = new Boolean(b);
327
    }
328
329
    public boolean getRuler() {
330
        if (m_showRuler == null) {
331
            m_showRuler = new Boolean(layoutManager.getDefaultShowRulers());
332
        }
333
        return m_showRuler.booleanValue();
334
    }
335
336
    public boolean isGridVisible() {
337
        if (isGridVisible == null) {
338
            isGridVisible = new Boolean(layoutManager.getDefaultShowGrid());
339
        }
340
        return isGridVisible.booleanValue();
341
    }
342
343
    public void setGridVisible(boolean b) {
344
        isGridVisible = new Boolean(b);
345
    }
346
347
    public void fullRefresh() {
348
        IFFrame[] fframes = getFFrames();
349
        for (int i = 0; i < fframes.length; i++) {
350
            if (fframes[i] instanceof IFFrameUseFMap) {
351
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
352
                fframe.refresh();
353
            }
354
        }
355
        notifAllObservers();
356
    }
357
358
    public void notifAllObservers(){
359
        observers.notifyObservers(this,
360 167 cmartinez
            new DefaultLayoutNotification(LayoutNotification.LAYOUT_REFRESH));
361 5 jldominguez
    }
362
363
    public static void registerPersistent() {
364
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
365
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
366
            DynStruct definition =
367
                manager.addDefinition(DefaultLayoutContext.class,
368
                    PERSISTENCE_DEFINITION_NAME,
369
                    "Layout context persistence definition", null, null);
370
371
            definition.addDynFieldInt(NUMBEHIND_FIELD).setMandatory(true);
372
            definition.addDynFieldInt(NUMBEFORE_FIELD).setMandatory(true);
373
            definition.addDynFieldObject(ATTRIBUTES_FIELD)
374
                .setClassOfValue(Attributes.class).setMandatory(true);
375
            definition.addDynFieldArray(FFRAMES_FIELD)
376
                .setClassOfItems(IFFrame.class).setMandatory(true);
377 106 jldominguez
            // ==================
378
            definition.addDynFieldBoolean(ISADJUSTINGTOGRID_FIELD).setMandatory(true);
379
            definition.addDynFieldBoolean(ISEDITABLE_FIELD).setMandatory(true);
380
            definition.addDynFieldBoolean(IS_GRID_VISIBLE).setMandatory(false);
381
            definition.addDynFieldBoolean(IS_RULER_VISIBLE).setMandatory(false);
382 237 cmartinez
            definition.addDynFieldBoolean(HAS_WINDOW_BEEN_EVER_OPEN_FIELD).setMandatory(false);
383 5 jldominguez
        }
384
385
        FFrame.registerPersistent();
386
        Attributes.registerPersistent();
387
    }
388
389
    public void loadFromState(PersistentState state)
390
        throws PersistenceException {
391
        adjustToGrid = state.getBoolean(ISADJUSTINGTOGRID_FIELD);
392
        isEditable = state.getBoolean(ISEDITABLE_FIELD);
393 106 jldominguez
394
        if (state.hasValue(IS_GRID_VISIBLE)) {
395
            isGridVisible = state.getBoolean(IS_GRID_VISIBLE);
396
        }
397
        if (state.hasValue(IS_RULER_VISIBLE)) {
398
            m_showRuler = state.getBoolean(IS_RULER_VISIBLE);
399
        }
400 237 cmartinez
        if (state.hasValue(HAS_WINDOW_BEEN_EVER_OPEN_FIELD)) {
401
                setHasDocBeenOpened(state.getBoolean(HAS_WINDOW_BEEN_EVER_OPEN_FIELD));
402
        }
403
        else {
404
                // if not present, assume it was opened (as we are loading from persistence)
405
                setHasDocBeenOpened(true);
406
        }
407 106 jldominguez
408 5 jldominguez
        numBehind = state.getInt(NUMBEHIND_FIELD);
409
        numBefore = state.getInt(NUMBEFORE_FIELD);
410
        m_attributes = (Attributes) state.get(ATTRIBUTES_FIELD);
411
        IFFrame[] fframes =
412
            (IFFrame[]) state.getArray(FFRAMES_FIELD, IFFrame.class);
413
        for (int i = 0; i < fframes.length; i++) {
414
            addFFrame(fframes[i], true, true);
415
        }
416
    }
417
418
    public void saveToState(PersistentState state) throws PersistenceException {
419
        state.set(ISADJUSTINGTOGRID_FIELD, isAdjustingToGrid());
420
        state.set(ISEDITABLE_FIELD, isEditable());
421
        state.set(NUMBEHIND_FIELD, numBehind);
422
        state.set(NUMBEFORE_FIELD, numBefore);
423
        state.set(ATTRIBUTES_FIELD, m_attributes);
424
        state.set(FFRAMES_FIELD, fframes);
425 237 cmartinez
        state.set(HAS_WINDOW_BEEN_EVER_OPEN_FIELD, new Boolean(hasDocBeenOpened()));
426 112 jldominguez
        state.set(IS_RULER_VISIBLE, this.getRuler());
427
        state.set(IS_GRID_VISIBLE, this.isGridVisible());
428 5 jldominguez
    }
429
430
    public void setNumBefore(int numBefore) {
431
        this.numBefore = numBefore;
432
433
    }
434
435
    public void setNumBehind(int numBehind) {
436
        this.numBehind = numBehind;
437
    }
438
439
    public void addObserver(Observer o) {
440
        observers.addObserver(o);
441
    }
442
443
    public void deleteObserver(Observer o) {
444
      observers.deleteObserver(o);
445
    }
446
447
    public void deleteObservers() {
448
       observers.deleteObservers();
449
    }
450 147 cmartinez
451
        public void setTocModel(MapContext tocModel) {
452
                MapContext oldModel = this.tocModel;
453
                this.tocModel = tocModel;
454
                if (tocModel==oldModel){
455
                        // no real change
456
                        return;
457
                }
458
                else {
459
                    tocObservers.notifyObservers(this,
460 168 cmartinez
                        new TocModelChangedImpl(TocModelChangedNotification.Type.MODEL_SET, tocModel));
461 147 cmartinez
                }
462
        }
463
464 168 cmartinez
        public void notifyTocUpdated(TocModelChangedNotification.Type type) {
465 147 cmartinez
            tocObservers.notifyObservers(this,
466 168 cmartinez
                            new TocModelChangedImpl(type, tocModel));
467 147 cmartinez
468
        }
469
470
        public MapContext getTocModel() {
471
                return this.tocModel;
472
        }
473
474
        public void addTocObserver(TocModelObserver observer) {
475
                this.tocObservers.addObserver(observer);
476
        }
477
478
        public void removeTocObserver(TocModelObserver observer) {
479
                this.tocObservers.deleteObserver(observer);
480
        }
481
482
        public void removeTocObservers() {
483
                this.tocObservers.deleteObservers();
484
        }
485 237 cmartinez
486
    /**
487
     * True when the document window has been opened at least once, false otherwise. This is required
488
     * to decide whether the page setting dialog has to be shown to the user, otherwise it would be
489
     * shown every time the document is opened from project persistence
490
     */
491
        public boolean hasDocBeenOpened() {
492
                return hasDocBeenOpened;
493
        }
494
495
    /**
496
     * True when the document window has been opened at least once, false otherwise. This is required
497
     * to decide whether the page setting dialog has to be shown to the user, otherwise it would be
498
     * shown every time the document is opened from project persistence
499
     */
500
        public void setHasDocBeenOpened(boolean hasBeenOpened) {
501
                this.hasDocBeenOpened = hasBeenOpened;
502
        }
503 5 jldominguez
}