Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / DefaultLayoutContext.java @ 36648

History | View | Annotate | Download (12.1 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.util.ArrayList;
26
import java.util.Hashtable;
27

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

    
42
/**
43
 * Model of LayoutControl.
44
 * 
45
 * @author Vicente Caballero Navarro
46
 */
47
public class DefaultLayoutContext implements LayoutContext {
48

    
49
    public static final String PERSISTENCE_DEFINITION_NAME = "LayoutContext";
50

    
51
    private static final String ISADJUSTINGTOGRID_FIELD = "isAdjustingToGrid";
52
    private static final String ISEDITABLE_FIELD = "isEditable";
53
    private static final String NUMBEHIND_FIELD = "numBehind";
54
    private static final String NUMBEFORE_FIELD = "numBefore";
55
    private static final String ATTRIBUTES_FIELD = "attributes";
56
    private static final String FFRAMES_FIELD = "fframes";
57

    
58
    private static DefaultLayoutManager layoutManager = null;
59

    
60
    private Attributes m_attributes = null;
61
    private IFFrame[] fframes;
62
    private FrameCommandsRecord fcr;
63
    private static Hashtable nums = new Hashtable();
64
    private int numBefore = 0;
65
    private int numBehind = 0;
66
    private boolean isEditable = true;
67
    private Boolean adjustToGrid = null;
68
    private Boolean m_showRuler;
69
    private Boolean isGridVisible = null;
70
    private ArrayList layoutDrawListeners = new ArrayList();
71
    private AffineTransform m_MatrizTransf;
72

    
73
    /**
74
     * Create a new object of LayoutContext.
75
     */
76
    public DefaultLayoutContext() {
77
        layoutManager =
78
            (DefaultLayoutManager) ProjectManager.getInstance()
79
                .getDocumentManager(DefaultLayoutManager.TYPENAME);
80
        m_attributes = new Attributes();
81
        m_MatrizTransf = new AffineTransform();
82
        m_MatrizTransf.setToIdentity();
83
        FrameManager fm = new FrameManager();
84
        fcr = new FrameCommandsRecord(fm);
85
        updateFFrames();
86
    }
87

    
88
    public AffineTransform getAT() {
89
        return m_MatrizTransf;
90
    }
91

    
92
    public void addLayoutDrawListener(LayoutDrawListener ldl) {
93
        if (!layoutDrawListeners.contains(ldl)) {
94
            layoutDrawListeners.add(ldl);
95
        }
96
    }
97

    
98
    public void callLayoutDrawListeners() {
99
        for (int i = 0; i < layoutDrawListeners.size(); i++) {
100
            ((LayoutDrawListener) layoutDrawListeners.get(i)).refresh();
101
        }
102
    }
103

    
104
    public Attributes getAttributes() {
105
        return m_attributes;
106
    }
107

    
108
    public void setAtributes(Attributes attributes) {
109
        m_attributes = attributes;
110
    }
111

    
112
    public IFFrame[] getFFrames() {
113
        return fframes;
114
    }
115

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

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

    
134
    public void delFFrameSelected() {
135
        fcr.startComplex(PluginServices.getText(this, "remove_elements"));
136
        for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) {
137
            IFFrame fframe = fcr.getFrameManager().getFFrame(i);
138

    
139
            if (fframe.getSelected() != IFFrame.NOSELECT) {
140
                fcr.delete(fframe);
141
            }
142
        }
143
        fcr.endComplex();
144
        updateFFrames();
145
    }
146

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

    
156
    public void delFFrame(int index) {
157
        for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) {
158
            IFFrame frame = getFFrame(index);
159
            if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
160
                fcr.delete(frame);
161
            }
162
        }
163
        updateFFrames();
164
    }
165

    
166
    public void delFFrame(IFFrame frame) {
167
        for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) {
168
            if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
169
                fcr.delete(frame);
170
            }
171
        }
172
        updateFFrames();
173
    }
174

    
175
    public FrameCommandsRecord getFrameCommandsRecord() {
176
        return fcr;
177
    }
178

    
179
    public void addFFrame(IFFrame frame, boolean clearSelection, boolean select) {
180
        IFFrame[] fframes = getFFrames();
181
        if (clearSelection) {
182
            for (int i = fframes.length - 1; i >= 0; i--) {
183
                IFFrame fframe1 = fframes[i];
184
                fframe1.setSelected(false);
185
            }
186
        }
187

    
188
        if (nums.containsKey(frame.getClass())) {
189
            nums.put(
190
                frame.getClass(),
191
                new Integer(Integer.parseInt(nums.get(frame.getClass())
192
                    .toString()) + 1));
193
        } else {
194
            nums.put(frame.getClass(), new Integer(0));
195
        }
196

    
197
        frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
198
        fcr.insert(frame);
199
        frame.setSelected(select);
200
        frame.setLevel(getNumBefore());
201
        updateFFrames();
202
    }
203

    
204
    public void addFFrameSameProperties(IFFrame frame) {
205
        fcr.insert(frame);
206
        frame.setSelected(true);
207
        frame.setLevel(getNumBefore());
208
        updateFFrames();
209
    }
210

    
211
    public int getNumBehind() {
212
        return --numBehind;
213
    }
214

    
215
    public int getNumBefore() {
216
        return ++numBefore;
217
    }
218

    
219
    public IFFrame[] getAllFFrames() {
220
        ArrayList all = new ArrayList();
221
        return (IFFrame[]) allFFrames(getFFrames(), all)
222
            .toArray(new IFFrame[0]);
223
    }
224

    
225
    private ArrayList allFFrames(IFFrame[] fframes, ArrayList all) {
226
        for (int i = 0; i < fframes.length; i++) {
227
            if (fframes[i] instanceof FFrameGroup) {
228
                ArrayList groupFrames =
229
                    allFFrames(((FFrameGroup) fframes[i]).getFFrames(), all);
230
                if (!all.containsAll(groupFrames)) {
231
                    all.addAll(groupFrames);
232
                }
233

    
234
            } else {
235
                if (!all.contains(fframes[i])) {
236
                    all.add(fframes[i]);
237
                }
238
            }
239
        }
240
        return all;
241
    }
242

    
243
    public IFFrame[] getFFrameSelected() {
244
        ArrayList selecList = new ArrayList();
245
        IFFrame[] fframes = getFFrames();
246
        for (int i = fframes.length - 1; i >= 0; i--) {
247
            IFFrame fframe = fframes[i];
248

    
249
            if (fframe.getSelected() != IFFrame.NOSELECT) {
250
                selecList.add(fframe);
251
            }
252
        }
253

    
254
        return (IFFrame[]) selecList.toArray(new IFFrame[0]);
255
    }
256

    
257
    public boolean isEditable() {
258
        return isEditable;
259
    }
260

    
261
    public void setEditable(boolean b) {
262
        if (!b) {
263
            clearSelection();
264
            // layoutControl.setTool("layoutzoomin");
265
            PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
266
        }
267
        isEditable = b;
268

    
269
    }
270

    
271
    public boolean isAdjustingToGrid() {
272
        if (adjustToGrid == null) {
273
            adjustToGrid = new Boolean(layoutManager.getDefaultAdjustToGrid());
274
        }
275
        return adjustToGrid.booleanValue();
276
    }
277

    
278
    public void setAdjustToGrid(boolean b) {
279
        adjustToGrid = new Boolean(b);
280
    }
281

    
282
    public void setRuler(boolean b) {
283
        m_showRuler = new Boolean(b);
284
    }
285

    
286
    public boolean getRuler() {
287
        if (m_showRuler == null) {
288
            m_showRuler = new Boolean(layoutManager.getDefaultShowRulers());
289
        }
290
        return m_showRuler.booleanValue();
291
    }
292

    
293
    public boolean isGridVisible() {
294
        if (isGridVisible == null) {
295
            isGridVisible = new Boolean(layoutManager.getDefaultShowGrid());
296
        }
297
        return isGridVisible.booleanValue();
298
    }
299

    
300
    public void setGridVisible(boolean b) {
301
        isGridVisible = new Boolean(b);
302
    }
303

    
304
    public void fullRefresh() {
305
        IFFrame[] fframes = getFFrames();
306
        for (int i = 0; i < fframes.length; i++) {
307
            if (fframes[i] instanceof IFFrameUseFMap) {
308
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
309
                fframe.refresh();
310
            }
311
        }
312
        callLayoutDrawListeners();
313
    }
314

    
315
    public static void registerPersistent() {
316
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
317
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
318
            DynStruct definition =
319
                manager.addDefinition(DefaultLayoutContext.class,
320
                    PERSISTENCE_DEFINITION_NAME,
321
                    "Layout context persistence definition", null, null);
322

    
323
            definition.addDynFieldBoolean(ISADJUSTINGTOGRID_FIELD)
324
                .setMandatory(true);
325
            definition.addDynFieldBoolean(ISEDITABLE_FIELD).setMandatory(true);
326
            definition.addDynFieldInt(NUMBEHIND_FIELD).setMandatory(true);
327
            definition.addDynFieldInt(NUMBEFORE_FIELD).setMandatory(true);
328
            definition.addDynFieldObject(ATTRIBUTES_FIELD)
329
                .setClassOfValue(Attributes.class).setMandatory(true);
330
            definition.addDynFieldArray(FFRAMES_FIELD)
331
                .setClassOfItems(IFFrame.class).setMandatory(true);
332
        }
333

    
334
        FFrame.registerPersistent();
335
        Attributes.registerPersistent();
336
    }
337

    
338
    public void loadFromState(PersistentState state)
339
        throws PersistenceException {
340
        adjustToGrid = state.getBoolean(ISADJUSTINGTOGRID_FIELD);
341
        isEditable = state.getBoolean(ISEDITABLE_FIELD);
342
        numBehind = state.getInt(NUMBEHIND_FIELD);
343
        numBefore = state.getInt(NUMBEFORE_FIELD);
344
        m_attributes = (Attributes) state.get(ATTRIBUTES_FIELD);
345
        IFFrame[] fframes =
346
            (IFFrame[]) state.getArray(FFRAMES_FIELD, IFFrame.class);
347
        for (int i = 0; i < fframes.length; i++) {
348
            addFFrame(fframes[i], true, true);
349
        }
350
    }
351

    
352
    public void saveToState(PersistentState state) throws PersistenceException {
353
        state.set(ISADJUSTINGTOGRID_FIELD, isAdjustingToGrid());
354
        state.set(ISEDITABLE_FIELD, isEditable());
355
        state.set(NUMBEHIND_FIELD, numBehind);
356
        state.set(NUMBEFORE_FIELD, numBefore);
357
        state.set(ATTRIBUTES_FIELD, m_attributes);
358
        state.set(FFRAMES_FIELD, fframes);
359

    
360
    }
361

    
362
    public void setNumBefore(int numBefore) {
363
        this.numBefore = numBefore;
364

    
365
    }
366

    
367
    public void setNumBehind(int numBehind) {
368
        this.numBehind = numBehind;
369
    }
370

    
371
}