Statistics
| Revision:

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

History | View | Annotate | Download (13 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.observer.ObservableHelper;
39
import org.gvsig.tools.observer.Observer;
40
import org.gvsig.tools.persistence.PersistenceManager;
41
import org.gvsig.tools.persistence.PersistentState;
42
import org.gvsig.tools.persistence.exception.PersistenceException;
43

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

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

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

    
63
    private static DefaultLayoutManager layoutManager = null;
64

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

    
95
    public AffineTransform getAT() {
96
        return m_MatrizTransf;
97
    }
98

    
99
    public Attributes getAttributes() {
100
        return m_attributes;
101
    }
102

    
103
    public void setAtributes(Attributes attributes) {
104
        m_attributes = attributes;
105
    }
106

    
107
    public IFFrame[] getFFrames() {
108
        return fframes;
109
    }
110

    
111
    public IFFrame getFFrame(int i) {
112
        return fframes[i];
113
    }
114

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

    
129
    public void delFFrameSelected() {
130
        fcr.startComplex(PluginServices.getText(this, "remove_elements"));
131
        for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) {
132
            IFFrame fframe = fcr.getFrameManager().getFFrame(i);
133

    
134
            if (fframe.getSelected() != IFFrame.NOSELECT) {
135
                fframe.setSelected(false);
136
                fcr.delete(fframe);
137
            }
138
        }
139
        fcr.endComplex();
140
        updateFFrames();
141
    }
142

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

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

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

    
171
    public FrameCommandsRecord getFrameCommandsRecord() {
172
        return fcr;
173
    }
174

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

    
184
        if (nums.containsKey(frame.getClass())) {
185
            nums.put(
186
                frame.getClass(),
187
                new Integer(Integer.parseInt(nums.get(frame.getClass())
188
                    .toString()) + 1));
189
        } else {
190
            nums.put(frame.getClass(), new Integer(0));
191
        }
192

    
193
        frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
194
        fcr.insert(frame);
195
        frame.setSelected(select);
196
        frame.setLevel(getNumBefore());
197
        updateFFrames();
198
    }
199

    
200
    public void addFFrameSameProperties(IFFrame frame) {
201
        fcr.insert(frame);
202
        frame.setSelected(true);
203
        frame.setLevel(getNumBefore());
204
        updateFFrames();
205
    }
206

    
207
    public int getNumBehind() {
208
        return --numBehind;
209
    }
210

    
211
    public int getNumBefore() {
212
        return ++numBefore;
213
    }
214

    
215
    public IFFrame[] getAllFFrames() {
216
        ArrayList all = new ArrayList();
217
        return (IFFrame[]) allFFrames(getFFrames(), all)
218
            .toArray(new IFFrame[0]);
219
    }
220

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

    
230
            } else {
231
                if (!all.contains(fframes[i])) {
232
                    all.add(fframes[i]);
233
                }
234
            }
235
        }
236
        return all;
237
    }
238

    
239
    public IFFrame[] getFFrameSelected() {
240
        ArrayList selecList = new ArrayList();
241
        IFFrame[] fframes = getFFrames();
242
        for (int i = fframes.length - 1; i >= 0; i--) {
243
            IFFrame fframe = fframes[i];
244

    
245
            if (fframe.getSelected() != IFFrame.NOSELECT) {
246
                selecList.add(fframe);
247
            }
248
        }
249

    
250
        return (IFFrame[]) selecList.toArray(new IFFrame[0]);
251
    }
252

    
253
    public boolean isEditable() {
254
        return isEditable;
255
    }
256

    
257
    public void setEditable(boolean b) {
258
        if (!b) {
259
            clearSelection();
260
            // layoutControl.setTool("layoutzoomin");
261
            PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
262
        }
263
        isEditable = b;
264

    
265
    }
266

    
267
    public boolean isAdjustingToGrid() {
268
        if (adjustToGrid == null) {
269
            adjustToGrid = new Boolean(layoutManager.getDefaultAdjustToGrid());
270
        }
271
        return adjustToGrid.booleanValue();
272
    }
273

    
274
    public void setAdjustToGrid(boolean b) {
275
        adjustToGrid = new Boolean(b);
276
    }
277

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

    
282
    public boolean getRuler() {
283
        if (m_showRuler == null) {
284
            m_showRuler = new Boolean(layoutManager.getDefaultShowRulers());
285
        }
286
        return m_showRuler.booleanValue();
287
    }
288

    
289
    public boolean isGridVisible() {
290
        if (isGridVisible == null) {
291
            isGridVisible = new Boolean(layoutManager.getDefaultShowGrid());
292
        }
293
        return isGridVisible.booleanValue();
294
    }
295

    
296
    public void setGridVisible(boolean b) {
297
        isGridVisible = new Boolean(b);
298
    }
299

    
300
    public void fullRefresh() {
301
        IFFrame[] fframes = getFFrames();
302
        for (int i = 0; i < fframes.length; i++) {
303
            if (fframes[i] instanceof IFFrameUseFMap) {
304
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
305
                fframe.refresh();
306
            }
307
        }
308
        notifAllObservers();
309
    }
310
    
311
    public void notifAllObservers(){
312
        observers.notifyObservers(this, 
313
            new DefaultLayoutNotification(LayoutNotification.LAYOUT_REFRESH)); 
314
    }
315

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

    
324
            definition.addDynFieldInt(NUMBEHIND_FIELD).setMandatory(true);
325
            definition.addDynFieldInt(NUMBEFORE_FIELD).setMandatory(true);
326
            definition.addDynFieldObject(ATTRIBUTES_FIELD)
327
                .setClassOfValue(Attributes.class).setMandatory(true);
328
            definition.addDynFieldArray(FFRAMES_FIELD)
329
                .setClassOfItems(IFFrame.class).setMandatory(true);
330
            // ==================
331
            definition.addDynFieldBoolean(ISADJUSTINGTOGRID_FIELD).setMandatory(true);
332
            definition.addDynFieldBoolean(ISEDITABLE_FIELD).setMandatory(true);
333
            definition.addDynFieldBoolean(IS_GRID_VISIBLE).setMandatory(false);
334
            definition.addDynFieldBoolean(IS_RULER_VISIBLE).setMandatory(false);
335
        }
336

    
337
        FFrame.registerPersistent();
338
        Attributes.registerPersistent();
339
    }
340

    
341
    public void loadFromState(PersistentState state)
342
        throws PersistenceException {
343
        adjustToGrid = state.getBoolean(ISADJUSTINGTOGRID_FIELD);
344
        isEditable = state.getBoolean(ISEDITABLE_FIELD);
345
        
346
        if (state.hasValue(IS_GRID_VISIBLE)) {
347
            isGridVisible = state.getBoolean(IS_GRID_VISIBLE);
348
        }
349
        if (state.hasValue(IS_RULER_VISIBLE)) {
350
            m_showRuler = state.getBoolean(IS_RULER_VISIBLE);
351
        }
352
        
353
        numBehind = state.getInt(NUMBEHIND_FIELD);
354
        numBefore = state.getInt(NUMBEFORE_FIELD);
355
        m_attributes = (Attributes) state.get(ATTRIBUTES_FIELD);
356
        IFFrame[] fframes =
357
            (IFFrame[]) state.getArray(FFRAMES_FIELD, IFFrame.class);
358
        for (int i = 0; i < fframes.length; i++) {
359
            addFFrame(fframes[i], true, true);
360
        }
361
    }
362

    
363
    public void saveToState(PersistentState state) throws PersistenceException {
364
        state.set(ISADJUSTINGTOGRID_FIELD, isAdjustingToGrid());
365
        state.set(ISEDITABLE_FIELD, isEditable());
366
        state.set(NUMBEHIND_FIELD, numBehind);
367
        state.set(NUMBEFORE_FIELD, numBefore);
368
        state.set(ATTRIBUTES_FIELD, m_attributes);
369
        state.set(FFRAMES_FIELD, fframes);
370
        
371
        state.set(IS_RULER_VISIBLE, this.m_showRuler);
372
        state.set(IS_GRID_VISIBLE, this.isGridVisible);
373
    }
374

    
375
    public void setNumBefore(int numBefore) {
376
        this.numBefore = numBefore;
377

    
378
    }
379

    
380
    public void setNumBehind(int numBehind) {
381
        this.numBehind = numBehind;
382
    }    
383

    
384
    public void addObserver(Observer o) {
385
        observers.addObserver(o);        
386
    }
387

    
388
    public void deleteObserver(Observer o) {
389
      observers.deleteObserver(o);        
390
    }
391

    
392
    public void deleteObservers() {
393
       observers.deleteObservers();        
394
    }
395
}