Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / LayoutContext.java @ 9532

History | View | Annotate | Download (9.08 KB)

1
package com.iver.cit.gvsig.project.documents.layout;
2

    
3
import java.util.ArrayList;
4
import java.util.Hashtable;
5

    
6
import com.iver.andami.PluginServices;
7
import com.iver.cit.gvsig.fmap.layers.XMLException;
8
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
9
import com.iver.cit.gvsig.project.documents.layout.commands.DefaultEditableFeatureSource;
10
import com.iver.cit.gvsig.project.documents.layout.commands.EditableFeatureSource;
11
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameGroup;
12
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
13
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
14
import com.iver.utiles.XMLEntity;
15

    
16
/**
17
 * Modelo del Layout.
18
 *
19
 * @author Vicente Caballero Navarro
20
 */
21
public class LayoutContext {
22
        private Attributes m_attributes = null;
23
        private IFFrame[] fframes;
24
        private DefaultEditableFeatureSource efs;
25
        public static Hashtable nums = new Hashtable();
26
        public int numBefore = 0;
27
    public int numBehind = 0;
28
    private boolean isEditable = true;
29
    private Boolean adjustToGrid = null;
30
    private Boolean m_showRuler;
31
    private Boolean isGridVisible = null;
32

    
33
        public LayoutContext() {
34
                m_attributes=new Attributes();
35
                efs = new DefaultEditableFeatureSource();
36
        }
37

    
38
        /**
39
     * Devuelve los atributos del Mapa.
40
     *
41
     * @return Attributes.
42
     */
43
    public Attributes getAtributes() {
44
        return m_attributes;
45
    }
46

    
47
        public void setAtributes(Attributes attributes) {
48
                m_attributes=attributes;
49
        }
50
         /**
51
     * Obtiene el ArrayList con todos los FFrames que se han a?adido al Layout.
52
     *
53
     * @return Array con todos los fframes que contiene el Layout.
54
     */
55
    public IFFrame[] getFFrames() {
56
        return fframes;
57
    }
58

    
59
    public IFFrame getFFrame(int i) {
60
        return fframes[i];
61
    }
62

    
63
    public void updateFFrames() {
64
        ArrayList frames = new ArrayList();
65
        IFFrame[] auxfframes = efs.getFFrames();
66
        for (int j = numBehind; j <= numBefore; j++) {
67
            for (int i = 0; i < auxfframes.length; i++) {
68
                if (auxfframes[i].getLevel() == j) {
69
                    frames.add(auxfframes[i]);
70
                    continue;
71
                }
72
            }
73
        }
74
        fframes = (IFFrame[]) frames.toArray(new IFFrame[0]);
75
    }
76
    public void delFFrameSelected() {
77
        efs.startComplexCommand();
78
        for (int i = efs.getAllFFrames().length - 1; i >= 0; i--) {
79
            IFFrame fframe = efs.getFFrame(i);
80

    
81
            if (fframe.getSelected() != IFFrame.NOSELECT) {
82
                efs.removeFFrame(i);
83
            }
84
        }
85
        efs.endComplexCommand(PluginServices.getText(this,"remove_elements"));
86
        updateFFrames();
87
    }
88

    
89
    public void clearSelection() {
90
        for (int i = efs.getAllFFrames().length - 1; i >= 0; i--) {
91
            IFFrame fframe = efs.getFFrame(i);
92
            if (fframe.getSelected() != IFFrame.NOSELECT) {
93
                fframe.setSelected(false);
94
            }
95
        }
96
    }
97
    public void delFFrame(int index) {
98
        for (int i = 0; i < getEFS().getAllFFrames().length; i++) {
99
            if (getEFS().getFFrame(i).equals(getFFrame(index))) {
100
                getEFS().removeFFrame(i);
101
            }
102
        }
103
        updateFFrames();
104
    }
105

    
106
    public void delFFrame(IFFrame frame) {
107
        for (int i = 0; i < getEFS().getAllFFrames().length; i++) {
108
            if (getEFS().getFFrame(i).equals(frame)) {
109
                getEFS().removeFFrame(i);
110
            }
111
        }
112
        updateFFrames();
113
    }
114
    public EditableFeatureSource getEFS() {
115
        return efs;
116
    }
117
    /**
118
     * A?ade un fframe al Arraylist m_fframes.
119
     *
120
     * @param frame
121
     *            fframe a a?adir.
122
     * @param clearSelection
123
     *            para que se quede seleccionado ?nicamente el que a?adimos y
124
     *            false si lo que se pretende es que no se deseleccionen lo que
125
     *            ya est?n seleccionados.
126
     * @param select
127
     *            Booleano que indica si se tiene que quedar seleccionado el
128
     *            FFrame que se a?ade o no.
129
     */
130
    public void addFFrame(IFFrame frame, boolean clearSelection, boolean select) {
131
        IFFrame[] fframes = getFFrames();
132
        if (clearSelection) {
133
            for (int i = fframes.length - 1; i >= 0; i--) {
134
                IFFrame fframe1 = fframes[i];
135
                fframe1.setSelected(false);
136
            }
137
        }
138

    
139
        if (nums.containsKey(frame.getClass())) {
140
            nums.put(frame.getClass(), new Integer(Integer.parseInt(nums.get(
141
                    frame.getClass()).toString()) + 1));
142
        } else {
143
            nums.put(frame.getClass(), new Integer(0));
144
        }
145

    
146
        frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
147
        efs.addFFrame(frame);
148
        frame.setSelected(select);
149
        frame.setLevel(getNumBefore());
150
        updateFFrames();
151
    }
152

    
153
    public int getNumBehind() {
154
        return --numBehind;
155
    }
156

    
157
    public int getNumBefore() {
158
        return ++numBefore;
159
    }
160
    public IFFrame[] getAllFFrames() {
161
        ArrayList all = new ArrayList();
162
        return (IFFrame[]) allFFrames(getFFrames(), all)
163
                .toArray(new IFFrame[0]);
164
    }
165
    private ArrayList allFFrames(IFFrame[] fframes, ArrayList all) {
166
        for (int i = 0; i < fframes.length; i++) {
167
            if (fframes[i] instanceof FFrameGroup) {
168
                return allFFrames(((FFrameGroup) fframes[i]).getFFrames(), all);
169

    
170
            }
171
                all.add(fframes[i]);
172
        }
173
        return all;
174
    }
175
    /**
176
     * Devuelve un array con los FFrames seleccionados.
177
     *
178
     * @return Array con los FFrames seleccionados.
179
     */
180
    public IFFrame[] getFFrameSelected() {
181
        ArrayList selecList = new ArrayList();
182
        IFFrame[] fframes=getFFrames();
183
        for (int i = fframes.length - 1; i >= 0; i--) {
184
            IFFrame fframe = fframes[i];
185

    
186
            if (fframe.getSelected() != IFFrame.NOSELECT) {
187
                selecList.add(fframe);
188
            }
189
        }
190

    
191
        return (IFFrame[]) selecList.toArray(new IFFrame[0]);
192
    }
193
    public boolean isEditable() {
194
        return isEditable;
195
    }
196

    
197
    public void setEditable(boolean b) {
198
        if (!b) {
199
            clearSelection();
200
            //layoutControl.setTool("layoutzoomin");
201
            PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
202
        }
203
        isEditable = b;
204

    
205
    }
206
    /**
207
     * Devuelve si se esta aplicando en los fframes que se a?den al Layout la
208
     * cuadr?cula, o no.
209
     *
210
     * @return true si se esta aplicando la cuadr?cula.
211
     */
212
    public boolean isAdjustingToGrid() {
213
        if (adjustToGrid == null) {
214
            adjustToGrid = new Boolean(Layout.getDefaultAdjustToGrid());
215
        }
216
        return adjustToGrid.booleanValue();
217
    }
218
    /**
219
     * Se actualiza el estado de la cuadr?cula, si se aplica o no.
220
     *
221
     * @param b
222
     *            true si se aplica la cuadr?cula.
223
     */
224
    public void setAdjustToGrid(boolean b) {
225
        adjustToGrid = new Boolean(b);
226
    }
227
    /**
228
     * Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
229
     * para poder despu?s volver a crear el objeto original.
230
     *
231
     * @return XMLEntity.
232
     *
233
     * @throws XMLException
234
     */
235
    public XMLEntity getXMLEntity() {
236
        XMLEntity xml = new XMLEntity();
237
        xml.putProperty("className", this.getClass().getName());
238
        xml.setName("layout");
239
        xml.putProperty("isCuadricula", isAdjustingToGrid());
240
//        xml.putProperty("m_name", this.getName());
241
        xml.putProperty("isEditable", isEditable());
242
        xml.putProperty("numBehind", numBehind);
243
        xml.putProperty("numBefore", numBefore);
244
        xml.addChild(getAtributes().getXMLEntity());
245
        IFFrame[] fframes=getFFrames();
246
        for (int i = 0; i < fframes.length; i++) {
247
            try {
248
                XMLEntity xmlAux = fframes[i].getXMLEntity();
249
                xml.addChild(xmlAux);
250
            } catch (SaveException e) {
251
                e.showError();
252
            }
253
        }
254
        return xml;
255
    }
256
    /**
257
     * Inserta si se muestra o no la regla del Layout.
258
     *
259
     * @param b
260
     *            True si se muestra la regla.
261
     */
262
    public void setRuler(boolean b) {
263
        m_showRuler = new Boolean(b);
264
    }
265

    
266
    /**
267
     * Devuelve si se muestra la regla del Layout.
268
     *
269
     * @return True si se muestra la regla.
270
     */
271
    public boolean getRuler() {
272
        if (m_showRuler == null) {
273
            m_showRuler = new Boolean(Layout.getDefaultShowRulers());
274
        }
275
        return m_showRuler.booleanValue();
276
    }
277

    
278

    
279

    
280

    
281

    
282
    /**
283
     * Devuelve si se esta aplicando en los fframes que se a?den al Layout la
284
     * cuadr?cula, o no.
285
     *
286
     * @return true si se esta aplicando la cuadr?cula.
287
     */
288
    public boolean isGridVisible() {
289
        if (isGridVisible== null) {
290
            isGridVisible = new Boolean(Layout.getDefaultShowGrid());
291
        }
292
        return isGridVisible.booleanValue();
293
    }
294
    /**
295
     * Inserta si se muestra el Grid del Layout.
296
     *
297
     * @param b
298
     *            True si se muestra el Grid del Layout.
299
     */
300
    public void setGridVisible(boolean b) {
301
        isGridVisible = new Boolean(b);
302
    }
303

    
304
}