Statistics
| Revision:

svn-document-layout / branches / usability_v2 / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / DefaultLayoutContext.java @ 145

History | View | Annotate | Download (14.5 KB)

1
/* 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
import java.lang.reflect.Array;
26
import java.util.ArrayList;
27
import java.util.Hashtable;
28

    
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.app.project.ProjectManager;
31
import org.gvsig.app.project.documents.layout.commands.FrameCommandsRecord;
32
import org.gvsig.app.project.documents.layout.commands.FrameManager;
33
import org.gvsig.app.project.documents.layout.fframes.FFrame;
34
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup;
35
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
36
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap;
37
import org.gvsig.tools.ToolsLocator;
38
import org.gvsig.tools.dynobject.DynStruct;
39
import org.gvsig.tools.observer.ObservableHelper;
40
import org.gvsig.tools.observer.Observer;
41
import org.gvsig.tools.persistence.PersistenceManager;
42
import org.gvsig.tools.persistence.PersistentState;
43
import org.gvsig.tools.persistence.exception.PersistenceException;
44

    
45
/**
46
 * Model of LayoutControl.
47
 * 
48
 * @author Vicente Caballero Navarro
49
 */
50
public class DefaultLayoutContext implements LayoutContext {
51

    
52
    public static final String PERSISTENCE_DEFINITION_NAME = "LayoutContext";
53

    
54
    private static final String ISADJUSTINGTOGRID_FIELD = "isAdjustingToGrid";
55
    private static final String IS_GRID_VISIBLE = "IS_GRID_VISIBLE";
56
    private static final String IS_RULER_VISIBLE = "IS_RULER_VISIBLE";
57
    private static final String ISEDITABLE_FIELD = "isEditable";
58
    
59
    private static final String NUMBEHIND_FIELD = "numBehind";
60
    private static final String NUMBEFORE_FIELD = "numBefore";
61
    private static final String ATTRIBUTES_FIELD = "attributes";
62
    private static final String FFRAMES_FIELD = "fframes";
63

    
64
    private static DefaultLayoutManager layoutManager = null;
65

    
66
    private Attributes m_attributes = null;
67
    private IFFrame[] fframes;
68
    private FrameCommandsRecord fcr;
69
    private static Hashtable nums = new Hashtable();
70
    private int numBefore = 0;
71
    private int numBehind = 0;
72
    private boolean isEditable = true;
73
    private Boolean adjustToGrid = null;
74
    
75
    private Boolean m_showRuler;
76
    private Boolean isGridVisible = null;
77
    
78
    private AffineTransform m_MatrizTransf;
79
    
80
    private ObservableHelper observers;
81
    
82
    /**
83
     * Create a new object of LayoutContext.
84
     */
85
    public DefaultLayoutContext() {
86
        layoutManager =
87
            (DefaultLayoutManager) ProjectManager.getInstance()
88
                .getDocumentManager(DefaultLayoutManager.TYPENAME);
89
        observers = new ObservableHelper();
90
        m_attributes = new Attributes();
91
        m_MatrizTransf = new AffineTransform();
92
        m_MatrizTransf.setToIdentity();
93
        FrameManager fm = new FrameManager();
94
        fcr = new FrameCommandsRecord(fm);
95
        updateFFrames();
96
    }
97

    
98
    public AffineTransform getAT() {
99
        return m_MatrizTransf;
100
    }
101

    
102
    public Attributes getAttributes() {
103
        return m_attributes;
104
    }
105

    
106
    public void setAtributes(Attributes attributes) {
107
        m_attributes = attributes;
108
    }
109

    
110
    public IFFrame[] getFFrames() {
111
        return fframes;
112
    }
113

    
114
    public IFFrame getFFrame(int i) {
115
        return fframes[i];
116
    }
117

    
118
    public void updateFFrames() {
119
        ArrayList frames = new ArrayList();
120
        IFFrame[] auxfframes = fcr.getFrameManager().getFFrames();
121
        for (int j = numBehind; j <= numBefore; j++) {
122
            for (int i = 0; i < auxfframes.length; i++) {
123
                if (auxfframes[i].getLevel() == j) {
124
                    frames.add(auxfframes[i]);
125
                    continue;
126
                }
127
            }
128
        }
129
        fframes = (IFFrame[]) frames.toArray(new IFFrame[0]);
130
    }
131

    
132
    public void delFFrameSelected() {
133
        fcr.startComplex(PluginServices.getText(this, "remove_elements"));
134
        IFFrame[] validFrames = fcr.getFrameManager().getFFrames();
135
        for (int i = 0; i<validFrames.length; i++) {
136
            IFFrame fframe = validFrames[i];
137

    
138
            if (fframe.isSelected()) {
139
                observers.notifyObservers(this, 
140
                        new DefaultLayoutNotification(LayoutNotification.FRAME_REMOVING));
141
                fframe.setSelected(false);
142
                fcr.delete(fframe);
143
                observers.notifyObservers(this, 
144
                        new DefaultLayoutNotification(LayoutNotification.FRAME_REMOVED));
145
            }
146
        }
147
        fcr.endComplex();
148
        updateFFrames();
149
    }
150

    
151
    public void clearSelection() {
152
        for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) {
153
            IFFrame fframe = fcr.getFrameManager().getFFrame(i);
154
            if (fframe.getSelected() != IFFrame.NOSELECT) {
155
                fframe.setSelected(false);
156
            }
157
        }
158
    }
159

    
160
    public void delFFrame(int index) {
161
            IFFrame frame = getFFrame(index);
162
            delFFrame(frame);
163
    }
164

    
165
    public void delFFrame(IFFrame frame) {
166
        observers.notifyObservers(this, 
167
                new DefaultLayoutNotification(LayoutNotification.FRAME_REMOVING));
168
        for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) {
169
            if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
170
                    frame.setSelected(false);
171
                fcr.delete(frame);
172
            }
173
        }
174
        updateFFrames();
175
        observers.notifyObservers(this, 
176
                new DefaultLayoutNotification(LayoutNotification.FRAME_REMOVED));
177
    }
178

    
179
    public FrameCommandsRecord getFrameCommandsRecord() {
180
        return fcr;
181
    }
182

    
183
    public void addFFrame(IFFrame frame, boolean clearSelection, boolean select) {
184
            this.addObserver(frame);
185
        observers.notifyObservers(this, 
186
                new DefaultLayoutNotification(LayoutNotification.FRAME_ADDING)); 
187
        IFFrame[] fframes = getFFrames();
188
        if (clearSelection) {
189
            for (int i = fframes.length - 1; i >= 0; i--) {
190
                IFFrame fframe1 = fframes[i];
191
                fframe1.setSelected(false);
192
            }
193
        }
194

    
195
        if (nums.containsKey(frame.getClass())) {
196
            nums.put(
197
                frame.getClass(),
198
                new Integer(Integer.parseInt(nums.get(frame.getClass())
199
                    .toString()) + 1));
200
        } else {
201
            nums.put(frame.getClass(), new Integer(0));
202
        }
203

    
204
        frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
205
        fcr.insert(frame);
206
        frame.setSelected(select);
207
        frame.setLevel(getNumBefore());
208
        updateFFrames();
209
        observers.notifyObservers(this, 
210
                new DefaultLayoutNotification(LayoutNotification.FRAME_ADDED));
211
    }
212

    
213
    public void addFFrameSameProperties(IFFrame frame) {
214
            this.addObserver(frame);
215
        observers.notifyObservers(this, 
216
                new DefaultLayoutNotification(LayoutNotification.FRAME_ADDING)); 
217
        fcr.insert(frame);
218
        frame.setSelected(true);
219
        frame.setLevel(getNumBefore());
220
        updateFFrames();
221
        observers.notifyObservers(this, 
222
                new DefaultLayoutNotification(LayoutNotification.FRAME_ADDED));
223
    }
224

    
225
    public int getNumBehind() {
226
        return --numBehind;
227
    }
228

    
229
    public int getNumBefore() {
230
        return ++numBefore;
231
    }
232

    
233
    public IFFrame[] getAllFFrames() {
234
        ArrayList all = new ArrayList();
235
        return (IFFrame[]) allFFrames(getFFrames(), all)
236
            .toArray(new IFFrame[0]);
237
    }
238

    
239
    private ArrayList allFFrames(IFFrame[] fframes, ArrayList all) {
240
        for (int i = 0; i < fframes.length; i++) {
241
            if (fframes[i] instanceof FFrameGroup) {
242
                ArrayList groupFrames =
243
                    allFFrames(((FFrameGroup) fframes[i]).getFFrames(), all);
244
                if (!all.containsAll(groupFrames)) {
245
                    all.addAll(groupFrames);
246
                }
247

    
248
            } else {
249
                if (!all.contains(fframes[i])) {
250
                    all.add(fframes[i]);
251
                }
252
            }
253
        }
254
        return all;
255
    }
256

    
257
    public IFFrame[] getSelectedFFrames() {
258
        ArrayList selecList = new ArrayList();
259
        IFFrame[] fframes = getFFrames();
260
        for (int i = fframes.length - 1; i >= 0; i--) {
261
            IFFrame fframe = fframes[i];
262

    
263
            if (fframe.getSelected() != IFFrame.NOSELECT) {
264
                selecList.add(fframe);
265
            }
266
        }
267
        return (IFFrame[]) selecList.toArray(new IFFrame[selecList.size()]);
268
    }
269

    
270
        public <T> T[] getSelectedFFrames(Class<T> clazz) {
271
        ArrayList<T> selecList = new ArrayList<T>();
272
        IFFrame[] fframes = getFFrames();
273
        for (int i = fframes.length - 1; i >= 0; i--) {
274
                IFFrame fframe = fframes[i];
275
                if (clazz.isInstance(fframe)) {
276
                if (fframe.getSelected() != IFFrame.NOSELECT) {
277
                    selecList.add((T)fframe);
278
                }
279
                }
280
        }
281
        T[] newInstance = (T[])Array.newInstance(clazz, selecList.size());
282
                return selecList.toArray(newInstance);
283
        }
284

    
285
    public boolean isEditable() {
286
        return isEditable;
287
    }
288

    
289
    public void setEditable(boolean b) {
290
        if (!b) {
291
            clearSelection();
292
            // layoutControl.setTool("layoutzoomin");
293
            PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
294
        }
295
        isEditable = b;
296

    
297
    }
298

    
299
    public boolean isAdjustingToGrid() {
300
        if (adjustToGrid == null) {
301
            adjustToGrid = new Boolean(layoutManager.getDefaultAdjustToGrid());
302
        }
303
        return adjustToGrid.booleanValue();
304
    }
305

    
306
    public void setAdjustToGrid(boolean b) {
307
        adjustToGrid = new Boolean(b);
308
    }
309

    
310
    public void setRuler(boolean b) {
311
        m_showRuler = new Boolean(b);
312
    }
313

    
314
    public boolean getRuler() {
315
        if (m_showRuler == null) {
316
            m_showRuler = new Boolean(layoutManager.getDefaultShowRulers());
317
        }
318
        return m_showRuler.booleanValue();
319
    }
320

    
321
    public boolean isGridVisible() {
322
        if (isGridVisible == null) {
323
            isGridVisible = new Boolean(layoutManager.getDefaultShowGrid());
324
        }
325
        return isGridVisible.booleanValue();
326
    }
327

    
328
    public void setGridVisible(boolean b) {
329
        isGridVisible = new Boolean(b);
330
    }
331

    
332
    public void fullRefresh() {
333
        IFFrame[] fframes = getFFrames();
334
        for (int i = 0; i < fframes.length; i++) {
335
            if (fframes[i] instanceof IFFrameUseFMap) {
336
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
337
                fframe.refresh();
338
            }
339
        }
340
        notifAllObservers();
341
    }
342
    
343
    public void notifAllObservers(){
344
        observers.notifyObservers(this, 
345
            new DefaultLayoutNotification(LayoutNotification.LAYOUT_REFRESH)); 
346
    }
347

    
348
    public static void registerPersistent() {
349
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
350
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
351
            DynStruct definition =
352
                manager.addDefinition(DefaultLayoutContext.class,
353
                    PERSISTENCE_DEFINITION_NAME,
354
                    "Layout context persistence definition", null, null);
355

    
356
            definition.addDynFieldInt(NUMBEHIND_FIELD).setMandatory(true);
357
            definition.addDynFieldInt(NUMBEFORE_FIELD).setMandatory(true);
358
            definition.addDynFieldObject(ATTRIBUTES_FIELD)
359
                .setClassOfValue(Attributes.class).setMandatory(true);
360
            definition.addDynFieldArray(FFRAMES_FIELD)
361
                .setClassOfItems(IFFrame.class).setMandatory(true);
362
            // ==================
363
            definition.addDynFieldBoolean(ISADJUSTINGTOGRID_FIELD).setMandatory(true);
364
            definition.addDynFieldBoolean(ISEDITABLE_FIELD).setMandatory(true);
365
            definition.addDynFieldBoolean(IS_GRID_VISIBLE).setMandatory(false);
366
            definition.addDynFieldBoolean(IS_RULER_VISIBLE).setMandatory(false);
367
        }
368

    
369
        FFrame.registerPersistent();
370
        Attributes.registerPersistent();
371
    }
372

    
373
    public void loadFromState(PersistentState state)
374
        throws PersistenceException {
375
        adjustToGrid = state.getBoolean(ISADJUSTINGTOGRID_FIELD);
376
        isEditable = state.getBoolean(ISEDITABLE_FIELD);
377
        
378
        if (state.hasValue(IS_GRID_VISIBLE)) {
379
            isGridVisible = state.getBoolean(IS_GRID_VISIBLE);
380
        }
381
        if (state.hasValue(IS_RULER_VISIBLE)) {
382
            m_showRuler = state.getBoolean(IS_RULER_VISIBLE);
383
        }
384
        
385
        numBehind = state.getInt(NUMBEHIND_FIELD);
386
        numBefore = state.getInt(NUMBEFORE_FIELD);
387
        m_attributes = (Attributes) state.get(ATTRIBUTES_FIELD);
388
        IFFrame[] fframes =
389
            (IFFrame[]) state.getArray(FFRAMES_FIELD, IFFrame.class);
390
        for (int i = 0; i < fframes.length; i++) {
391
            addFFrame(fframes[i], true, true);
392
        }
393
    }
394

    
395
    public void saveToState(PersistentState state) throws PersistenceException {
396
        state.set(ISADJUSTINGTOGRID_FIELD, isAdjustingToGrid());
397
        state.set(ISEDITABLE_FIELD, isEditable());
398
        state.set(NUMBEHIND_FIELD, numBehind);
399
        state.set(NUMBEFORE_FIELD, numBefore);
400
        state.set(ATTRIBUTES_FIELD, m_attributes);
401
        state.set(FFRAMES_FIELD, fframes);
402
        
403
        state.set(IS_RULER_VISIBLE, this.getRuler());
404
        state.set(IS_GRID_VISIBLE, this.isGridVisible());
405
    }
406

    
407
    public void setNumBefore(int numBefore) {
408
        this.numBefore = numBefore;
409

    
410
    }
411

    
412
    public void setNumBehind(int numBehind) {
413
        this.numBehind = numBehind;
414
    }    
415

    
416
    public void addObserver(Observer o) {
417
        observers.addObserver(o);        
418
    }
419

    
420
    public void deleteObserver(Observer o) {
421
      observers.deleteObserver(o);        
422
    }
423

    
424
    public void deleteObservers() {
425
       observers.deleteObservers();        
426
    }
427
}