Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / LayoutContext.java @ 24962

History | View | Annotate | Download (12.5 KB)

1 9392 caballero
package com.iver.cit.gvsig.project.documents.layout;
2
3 9805 caballero
import java.awt.geom.AffineTransform;
4 9392 caballero
import java.util.ArrayList;
5
import java.util.Hashtable;
6
7
import com.iver.andami.PluginServices;
8
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
9 21299 vcaballero
import com.iver.cit.gvsig.project.documents.layout.commands.FrameCommandsRecord;
10
import com.iver.cit.gvsig.project.documents.layout.commands.FrameManager;
11 9392 caballero
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameGroup;
12
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
13 9805 caballero
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameUseFMap;
14
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
15 9392 caballero
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
16
import com.iver.utiles.XMLEntity;
17 24759 jmvivo
import com.iver.utiles.XMLException;
18 9392 caballero
19
/**
20 10007 caballero
 * Model of LayoutControl.
21 9392 caballero
 *
22
 * @author Vicente Caballero Navarro
23
 */
24
public class LayoutContext {
25
        private Attributes m_attributes = null;
26
        private IFFrame[] fframes;
27 21299 vcaballero
        private FrameCommandsRecord fcr;
28 9392 caballero
        public static Hashtable nums = new Hashtable();
29
        public int numBefore = 0;
30
    public int numBehind = 0;
31
    private boolean isEditable = true;
32
    private Boolean adjustToGrid = null;
33
    private Boolean m_showRuler;
34
    private Boolean isGridVisible = null;
35 9805 caballero
        private ArrayList layoutDrawListeners=new ArrayList();
36
        private AffineTransform m_MatrizTransf;
37 10007 caballero
        /**
38
     * Create a new object of LayoutContext.
39
     */
40 9392 caballero
        public LayoutContext() {
41
                m_attributes=new Attributes();
42 9805 caballero
                m_MatrizTransf = new AffineTransform();
43
            m_MatrizTransf.setToIdentity();
44 21299 vcaballero
            FrameManager fm=new FrameManager();
45
                fcr = new FrameCommandsRecord(fm);
46 9392 caballero
        }
47 10007 caballero
        /**
48
         * Returns the AffineTransform that is applying in the Layout.
49
         *
50
         * @return AffineTransform
51
         */
52 9805 caballero
        public AffineTransform getAT() {
53
                return m_MatrizTransf;
54
        }
55 10007 caballero
        /**
56
         * Add Listener to draw Layout.
57
         * @param ldl LayoutDrawListener.
58
         */
59 9805 caballero
        public void addLayoutDrawListener(LayoutDrawListener ldl) {
60 24759 jmvivo
                if (!layoutDrawListeners.contains(ldl)) {
61 9805 caballero
                        layoutDrawListeners.add(ldl);
62 24759 jmvivo
                }
63 9805 caballero
        }
64 10007 caballero
        /**
65
         * Call all LayoutDrawListeners.
66
         *
67
         */
68 9805 caballero
        public void callLayoutDrawListeners() {
69
                for (int i=0;i<layoutDrawListeners.size();i++) {
70
                        ((LayoutDrawListener)layoutDrawListeners.get(i)).refresh();
71
                }
72
        }
73 9392 caballero
        /**
74 10007 caballero
         * It returns the print attributes of the Layout.
75
         *
76
         * @return Attributes.
77
         */
78 23069 vcaballero
    public Attributes getAttributes() {
79 9392 caballero
        return m_attributes;
80
    }
81 10007 caballero
    /**
82
     * Inserts the print attributes of Layout.
83
     * @param attributes Attributes.
84
     */
85 9392 caballero
        public void setAtributes(Attributes attributes) {
86
                m_attributes=attributes;
87
        }
88 10007 caballero
        /**
89 14696 jmvivo
         * It obtains the Array with all the FFrames that have been added al Layout.
90 10007 caballero
         *
91 14696 jmvivo
         * @return Array with all the FFrames that have been added al Layout.
92 10007 caballero
         */
93 9392 caballero
    public IFFrame[] getFFrames() {
94
        return fframes;
95
    }
96 10007 caballero
    /**
97
         * It obtains the FFrame from an index.
98
        **/
99 9392 caballero
    public IFFrame getFFrame(int i) {
100
        return fframes[i];
101
    }
102 10007 caballero
    /**
103
         * It orders the FFrames depending on its position specified by level.
104
         *
105
         */
106 9392 caballero
    public void updateFFrames() {
107
        ArrayList frames = new ArrayList();
108 21299 vcaballero
        IFFrame[] auxfframes = fcr.getFrameManager().getFFrames();
109 9392 caballero
        for (int j = numBehind; j <= numBefore; j++) {
110
            for (int i = 0; i < auxfframes.length; i++) {
111
                if (auxfframes[i].getLevel() == j) {
112
                    frames.add(auxfframes[i]);
113
                    continue;
114
                }
115
            }
116
        }
117
        fframes = (IFFrame[]) frames.toArray(new IFFrame[0]);
118
    }
119 10007 caballero
    /**
120
         * Remove the fframes selected.
121
         *
122
         */
123 9392 caballero
    public void delFFrameSelected() {
124 24962 vcaballero
        fcr.startComplex(PluginServices.getText(this,"remove_elements"));
125 21299 vcaballero
        for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) {
126
            IFFrame fframe = fcr.getFrameManager().getFFrame(i);
127 9392 caballero
128
            if (fframe.getSelected() != IFFrame.NOSELECT) {
129 21299 vcaballero
                fcr.delete(fframe);
130 9392 caballero
            }
131
        }
132 24962 vcaballero
        fcr.endComplex();
133 9392 caballero
        updateFFrames();
134
    }
135 10007 caballero
    /**
136
         * Clear the selection of FFrames.
137
         *
138
         */
139 9392 caballero
    public void clearSelection() {
140 21299 vcaballero
        for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) {
141
            IFFrame fframe = fcr.getFrameManager().getFFrame(i);
142 9392 caballero
            if (fframe.getSelected() != IFFrame.NOSELECT) {
143
                fframe.setSelected(false);
144
            }
145
        }
146
    }
147 10007 caballero
    /**
148
         * Remove the fframe of index.
149
         *
150
         */
151 9392 caballero
    public void delFFrame(int index) {
152 21299 vcaballero
        for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) {
153
            IFFrame frame=getFFrame(index);
154
                if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
155
                fcr.delete(frame);
156 9392 caballero
            }
157
        }
158
        updateFFrames();
159
    }
160 10007 caballero
    /**
161
         * Remove the fframe of parameter.
162
         * @param frame
163
         *            FFrame to remove.
164
         */
165 9392 caballero
    public void delFFrame(IFFrame frame) {
166 21299 vcaballero
        for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) {
167
            if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
168
                fcr.delete(frame);
169 9392 caballero
            }
170
        }
171
        updateFFrames();
172
    }
173 10007 caballero
    /**
174
         * Returns the EditableFeatureSource, is the control of all change in the FFrames of Layout.
175
         * @return EditableFatureSource.
176
         */
177 21358 vcaballero
    public FrameCommandsRecord getFrameCommandsRecord() {
178 21299 vcaballero
        return fcr;
179 9392 caballero
    }
180
    /**
181 10007 caballero
         * It adds a fframe to Arraylist of FFrames .
182
         *
183
         * @param frame fframe to add.
184
         * @param clearSelection True
185
         *                          True if clean the selection of the fframes already added and
186
         *                          false if intends to maintain the same selection.
187
         * @param select
188
         *            Boolean that indicates if has to remain selected the FFrame that is added or not.
189
         */
190 9392 caballero
    public void addFFrame(IFFrame frame, boolean clearSelection, boolean select) {
191
        IFFrame[] fframes = getFFrames();
192
        if (clearSelection) {
193
            for (int i = fframes.length - 1; i >= 0; i--) {
194
                IFFrame fframe1 = fframes[i];
195
                fframe1.setSelected(false);
196
            }
197
        }
198
199
        if (nums.containsKey(frame.getClass())) {
200
            nums.put(frame.getClass(), new Integer(Integer.parseInt(nums.get(
201
                    frame.getClass()).toString()) + 1));
202
        } else {
203
            nums.put(frame.getClass(), new Integer(0));
204
        }
205
206
        frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
207 21299 vcaballero
        fcr.insert(frame);
208 9392 caballero
        frame.setSelected(select);
209
        frame.setLevel(getNumBefore());
210
        updateFFrames();
211
    }
212 10007 caballero
    /**
213 12130 caballero
         * It adds a fframe to Arraylist of FFrames with the same properties.
214
         *
215
         * @param frame fframe to add.
216
         */
217
        public void addFFrameSameProperties(IFFrame frame){
218 21299 vcaballero
                fcr.insert(frame);
219 12130 caballero
                frame.setSelected(true);
220
                frame.setLevel(getNumBefore());
221
                updateFFrames();
222
        }
223
    /**
224 10007 caballero
         * Returns other number behind the current fframes.
225
         * @return new Position behind.
226
         */
227 9392 caballero
    public int getNumBehind() {
228
        return --numBehind;
229
    }
230 10007 caballero
    /**
231
         * Returns other number before the current fframes.
232
         * @return new Position before.
233
         */
234 9392 caballero
    public int getNumBefore() {
235
        return ++numBefore;
236
    }
237 10007 caballero
    /**
238
         * It returns all the fframes included them erased and modified in all
239
         * its previous forms.
240
         * @return IFFrame[] Array of FFrames.
241
         */
242 9392 caballero
    public IFFrame[] getAllFFrames() {
243
        ArrayList all = new ArrayList();
244
        return (IFFrame[]) allFFrames(getFFrames(), all)
245
                .toArray(new IFFrame[0]);
246
    }
247
    private ArrayList allFFrames(IFFrame[] fframes, ArrayList all) {
248
        for (int i = 0; i < fframes.length; i++) {
249
            if (fframes[i] instanceof FFrameGroup) {
250 9631 caballero
                    ArrayList groupFrames=allFFrames(((FFrameGroup) fframes[i]).getFFrames(), all);
251 24759 jmvivo
                    if (!all.containsAll(groupFrames)) {
252
                                        all.addAll(groupFrames);
253
                                }
254 9392 caballero
255 9631 caballero
            }else {
256 24759 jmvivo
                    if (!all.contains(fframes[i])) {
257
                                        all.add(fframes[i]);
258
                                }
259 9392 caballero
            }
260
        }
261
        return all;
262
    }
263
    /**
264 10007 caballero
         * It returns an array with the FFrames selected.
265
         *
266
         * @return Array with the FFrames selected.
267
         */
268 9392 caballero
    public IFFrame[] getFFrameSelected() {
269
        ArrayList selecList = new ArrayList();
270
        IFFrame[] fframes=getFFrames();
271
        for (int i = fframes.length - 1; i >= 0; i--) {
272
            IFFrame fframe = fframes[i];
273
274
            if (fframe.getSelected() != IFFrame.NOSELECT) {
275
                selecList.add(fframe);
276
            }
277
        }
278
279
        return (IFFrame[]) selecList.toArray(new IFFrame[0]);
280
    }
281 10007 caballero
    /**
282
         * It returns if the Layout is in edition.
283
         * @return True if Layout is in edition.
284
         */
285 9392 caballero
    public boolean isEditable() {
286
        return isEditable;
287
    }
288 10007 caballero
    /**
289
         * It inserts if the Layout is in edition.
290
         * @param b
291
         *            True if Layout is in edition.
292
         */
293 9392 caballero
    public void setEditable(boolean b) {
294
        if (!b) {
295
            clearSelection();
296
            //layoutControl.setTool("layoutzoomin");
297
            PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
298
        }
299
        isEditable = b;
300
301
    }
302
    /**
303 10007 caballero
         * It returns if has been applying in the fframes that are added to Layout the grid, or not.
304
         *
305
         * @return true if has been applying the grid.
306
         */
307 9392 caballero
    public boolean isAdjustingToGrid() {
308
        if (adjustToGrid == null) {
309
            adjustToGrid = new Boolean(Layout.getDefaultAdjustToGrid());
310
        }
311
        return adjustToGrid.booleanValue();
312
    }
313
    /**
314 10007 caballero
         * It inserts if has been applying in the fframes that are added to Layout the grid, or not.
315
         *
316
         * @param b
317
         *            true  if has been applying the grid.
318
         */
319 9392 caballero
    public void setAdjustToGrid(boolean b) {
320
        adjustToGrid = new Boolean(b);
321
    }
322
    /**
323 10007 caballero
         * It returns an Object XMLEntity with the information the necessary attributes
324
         * to be able later to create again the original object.
325
         *
326
         * @return XMLEntity.
327
         *
328
         * @throws XMLException
329
         */
330 9392 caballero
    public XMLEntity getXMLEntity() {
331
        XMLEntity xml = new XMLEntity();
332
        xml.putProperty("className", this.getClass().getName());
333
        xml.setName("layout");
334
        xml.putProperty("isCuadricula", isAdjustingToGrid());
335
//        xml.putProperty("m_name", this.getName());
336
        xml.putProperty("isEditable", isEditable());
337
        xml.putProperty("numBehind", numBehind);
338
        xml.putProperty("numBefore", numBefore);
339 23069 vcaballero
        xml.addChild(getAttributes().getXMLEntity());
340 9392 caballero
        IFFrame[] fframes=getFFrames();
341
        for (int i = 0; i < fframes.length; i++) {
342
            try {
343
                XMLEntity xmlAux = fframes[i].getXMLEntity();
344
                xml.addChild(xmlAux);
345
            } catch (SaveException e) {
346
                e.showError();
347
            }
348
        }
349
        return xml;
350
    }
351
    /**
352 10007 caballero
         * It inserts if is shown or not the rule of the Layout.
353
         *
354
         * @param b
355
         *            True if is shown or not the rule of the Layout.
356
         */
357 9392 caballero
    public void setRuler(boolean b) {
358
        m_showRuler = new Boolean(b);
359
    }
360
361
    /**
362 10007 caballero
         * Returns if is shown or not the rule of the Layout.
363
         *
364
         * @return True si se muestra la regla.
365
         */
366 9392 caballero
    public boolean getRuler() {
367
        if (m_showRuler == null) {
368
            m_showRuler = new Boolean(Layout.getDefaultShowRulers());
369
        }
370
        return m_showRuler.booleanValue();
371
    }
372
373
374
375
376
377
    /**
378 10007 caballero
         * It returns if has been showing the grid of Layout, or not.
379
         *
380
         * @return true if has been showing the grid of Layout.
381
         */
382 9392 caballero
    public boolean isGridVisible() {
383
        if (isGridVisible== null) {
384
            isGridVisible = new Boolean(Layout.getDefaultShowGrid());
385
        }
386
        return isGridVisible.booleanValue();
387
    }
388
    /**
389 10007 caballero
         * It inserts if draws the Grid in the Layout or not.
390
         *
391
         * @param b
392
         *            True if draws the Grid in the Layout.
393
         */
394 9392 caballero
    public void setGridVisible(boolean b) {
395
        isGridVisible = new Boolean(b);
396
    }
397 10007 caballero
    /**
398
         * The dialogs are created here each time that are needed.
399
         *
400
         * @param fframe
401
         *            Rectangle that represents the place that occupied the element added.
402
         *
403
         * @return IFFrame Returns the FFrame added or null if the fframe has not been added.
404
         */
405 9805 caballero
        public IFFrame openFFrameDialog(IFFrame frame) {
406
                IFFrameDialog fframedialog = frame.getPropertyDialog();
407
                if (fframedialog != null) {
408
                        fframedialog.setRectangle(frame.getBoundingBox(getAT()));
409
                        PluginServices.getMDIManager().addWindow(fframedialog);
410
                }
411
                return fframedialog.getFFrame();
412
        }
413 10007 caballero
        /**
414
         * Refresh all FFrames of Layout.
415
         */
416 9805 caballero
        public void fullRefresh() {
417
                IFFrame[] fframes = getFFrames();
418
                for (int i = 0; i < fframes.length; i++) {
419
                        if (fframes[i] instanceof IFFrameUseFMap) {
420
                                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
421
                                fframe.refresh();
422
                        }
423
                }
424
                callLayoutDrawListeners();
425
        }
426
427 9392 caballero
}