Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / layout / LayoutContext.java @ 29598

History | View | Annotate | Download (12.4 KB)

1
package org.gvsig.app.project.documents.layout;
2

    
3
import java.awt.geom.AffineTransform;
4
import java.util.ArrayList;
5
import java.util.Hashtable;
6

    
7
import org.gvsig.andami.PluginServices;
8
import org.gvsig.app.project.documents.exceptions.SaveException;
9
import org.gvsig.app.project.documents.layout.commands.FrameCommandsRecord;
10
import org.gvsig.app.project.documents.layout.commands.FrameManager;
11
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup;
12
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
13
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap;
14
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
15
import org.gvsig.app.project.documents.layout.gui.Layout;
16
import org.gvsig.utils.XMLEntity;
17
import org.gvsig.utils.XMLException;
18

    
19

    
20
/**
21
 * Model of LayoutControl.
22
 *
23
 * @author Vicente Caballero Navarro
24
 */
25
public class LayoutContext {
26
        private Attributes m_attributes = null;
27
        private IFFrame[] fframes;
28
        private FrameCommandsRecord fcr;
29
        public static Hashtable nums = new Hashtable();
30
        public int numBefore = 0;
31
    public int numBehind = 0;
32
    private boolean isEditable = true;
33
    private Boolean adjustToGrid = null;
34
    private Boolean m_showRuler;
35
    private Boolean isGridVisible = null;
36
        private ArrayList layoutDrawListeners=new ArrayList();
37
        private AffineTransform m_MatrizTransf;
38
        /**
39
     * Create a new object of LayoutContext.
40
     */
41
        public LayoutContext() {
42
                m_attributes=new Attributes();
43
                m_MatrizTransf = new AffineTransform();
44
            m_MatrizTransf.setToIdentity();
45
            FrameManager fm=new FrameManager();
46
                fcr = new FrameCommandsRecord(fm);
47
        }
48
        /**
49
         * Returns the AffineTransform that is applying in the Layout.
50
         *
51
         * @return AffineTransform
52
         */
53
        public AffineTransform getAT() {
54
                return m_MatrizTransf;
55
        }
56
        /**
57
         * Add Listener to draw Layout.
58
         * @param ldl LayoutDrawListener.
59
         */
60
        public void addLayoutDrawListener(LayoutDrawListener ldl) {
61
                if (!layoutDrawListeners.contains(ldl)) {
62
                        layoutDrawListeners.add(ldl);
63
                }
64
        }
65
        /**
66
         * Call all LayoutDrawListeners.
67
         *
68
         */
69
        public void callLayoutDrawListeners() {
70
                for (int i=0;i<layoutDrawListeners.size();i++) {
71
                        ((LayoutDrawListener)layoutDrawListeners.get(i)).refresh();
72
                }
73
        }
74
        /**
75
         * It returns the print attributes of the Layout.
76
         *
77
         * @return Attributes.
78
         */
79
    public Attributes getAttributes() {
80
        return m_attributes;
81
    }
82
    /**
83
     * Inserts the print attributes of Layout.
84
     * @param attributes Attributes.
85
     */
86
        public void setAtributes(Attributes attributes) {
87
                m_attributes=attributes;
88
        }
89
        /**
90
         * It obtains the Array with all the FFrames that have been added al Layout.
91
         *
92
         * @return Array with all the FFrames that have been added al Layout.
93
         */
94
    public IFFrame[] getFFrames() {
95
        return fframes;
96
    }
97
    /**
98
         * It obtains the FFrame from an index.
99
        **/
100
    public IFFrame getFFrame(int i) {
101
        return fframes[i];
102
    }
103
    /**
104
         * It orders the FFrames depending on its position specified by level.
105
         *
106
         */
107
    public void updateFFrames() {
108
        ArrayList frames = new ArrayList();
109
        IFFrame[] auxfframes = fcr.getFrameManager().getFFrames();
110
        for (int j = numBehind; j <= numBefore; j++) {
111
            for (int i = 0; i < auxfframes.length; i++) {
112
                if (auxfframes[i].getLevel() == j) {
113
                    frames.add(auxfframes[i]);
114
                    continue;
115
                }
116
            }
117
        }
118
        fframes = (IFFrame[]) frames.toArray(new IFFrame[0]);
119
    }
120
    /**
121
         * Remove the fframes selected.
122
         *
123
         */
124
    public void delFFrameSelected() {
125
        fcr.startComplex(PluginServices.getText(this,"remove_elements"));
126
        for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) {
127
            IFFrame fframe = fcr.getFrameManager().getFFrame(i);
128

    
129
            if (fframe.getSelected() != IFFrame.NOSELECT) {
130
                fcr.delete(fframe);
131
            }
132
        }
133
        fcr.endComplex();
134
        updateFFrames();
135
    }
136
    /**
137
         * Clear the selection of FFrames.
138
         *
139
         */
140
    public void clearSelection() {
141
        for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) {
142
            IFFrame fframe = fcr.getFrameManager().getFFrame(i);
143
            if (fframe.getSelected() != IFFrame.NOSELECT) {
144
                fframe.setSelected(false);
145
            }
146
        }
147
    }
148
    /**
149
         * Remove the fframe of index.
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
         * Remove the fframe of parameter.
163
         * @param frame
164
         *            FFrame to remove.
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
         * Returns the EditableFeatureSource, is the control of all change in the FFrames of Layout.
176
         * @return EditableFatureSource.
177
         */
178
    public FrameCommandsRecord getFrameCommandsRecord() {
179
        return fcr;
180
    }
181
    /**
182
         * It adds a fframe to Arraylist of FFrames .
183
         *
184
         * @param frame fframe to add.
185
         * @param clearSelection True
186
         *                          True if clean the selection of the fframes already added and
187
         *                          false if intends to maintain the same selection.
188
         * @param select
189
         *            Boolean that indicates if has to remain selected the FFrame that is added or not.
190
         */
191
    public void addFFrame(IFFrame frame, boolean clearSelection, boolean select) {
192
        IFFrame[] fframes = getFFrames();
193
        if (clearSelection) {
194
            for (int i = fframes.length - 1; i >= 0; i--) {
195
                IFFrame fframe1 = fframes[i];
196
                fframe1.setSelected(false);
197
            }
198
        }
199

    
200
        if (nums.containsKey(frame.getClass())) {
201
            nums.put(frame.getClass(), new Integer(Integer.parseInt(nums.get(
202
                    frame.getClass()).toString()) + 1));
203
        } else {
204
            nums.put(frame.getClass(), new Integer(0));
205
        }
206

    
207
        frame.setNum(Integer.parseInt(nums.get(frame.getClass()).toString()));
208
        fcr.insert(frame);
209
        frame.setSelected(select);
210
        frame.setLevel(getNumBefore());
211
        updateFFrames();
212
    }
213
    /**
214
         * It adds a fframe to Arraylist of FFrames with the same properties.
215
         *
216
         * @param frame fframe to add.
217
         */
218
        public void addFFrameSameProperties(IFFrame frame){
219
                fcr.insert(frame);
220
                frame.setSelected(true);
221
                frame.setLevel(getNumBefore());
222
                updateFFrames();
223
        }
224
    /**
225
         * Returns other number behind the current fframes.
226
         * @return new Position behind.
227
         */
228
    public int getNumBehind() {
229
        return --numBehind;
230
    }
231
    /**
232
         * Returns other number before the current fframes.
233
         * @return new Position before.
234
         */
235
    public int getNumBefore() {
236
        return ++numBefore;
237
    }
238
    /**
239
         * It returns all the fframes included them erased and modified in all
240
         * its previous forms.
241
         * @return IFFrame[] Array of FFrames.
242
         */
243
    public IFFrame[] getAllFFrames() {
244
        ArrayList all = new ArrayList();
245
        return (IFFrame[]) allFFrames(getFFrames(), all)
246
                .toArray(new IFFrame[0]);
247
    }
248
    private ArrayList allFFrames(IFFrame[] fframes, ArrayList all) {
249
        for (int i = 0; i < fframes.length; i++) {
250
            if (fframes[i] instanceof FFrameGroup) {
251
                    ArrayList groupFrames=allFFrames(((FFrameGroup) fframes[i]).getFFrames(), all);
252
                    if (!all.containsAll(groupFrames)) {
253
                                        all.addAll(groupFrames);
254
                                }
255

    
256
            }else {
257
                    if (!all.contains(fframes[i])) {
258
                                        all.add(fframes[i]);
259
                                }
260
            }
261
        }
262
        return all;
263
    }
264
    /**
265
         * It returns an array with the FFrames selected.
266
         *
267
         * @return Array with the FFrames selected.
268
         */
269
    public IFFrame[] getFFrameSelected() {
270
        ArrayList selecList = new ArrayList();
271
        IFFrame[] fframes=getFFrames();
272
        for (int i = fframes.length - 1; i >= 0; i--) {
273
            IFFrame fframe = fframes[i];
274

    
275
            if (fframe.getSelected() != IFFrame.NOSELECT) {
276
                selecList.add(fframe);
277
            }
278
        }
279

    
280
        return (IFFrame[]) selecList.toArray(new IFFrame[0]);
281
    }
282
    /**
283
         * It returns if the Layout is in edition.
284
         * @return True if Layout is in edition.
285
         */
286
    public boolean isEditable() {
287
        return isEditable;
288
    }
289
    /**
290
         * It inserts if the Layout is in edition.
291
         * @param b
292
         *            True if Layout is in edition.
293
         */
294
    public void setEditable(boolean b) {
295
        if (!b) {
296
            clearSelection();
297
            //layoutControl.setTool("layoutzoomin");
298
            PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
299
        }
300
        isEditable = b;
301

    
302
    }
303
    /**
304
         * It returns if has been applying in the fframes that are added to Layout the grid, or not.
305
         *
306
         * @return true if has been applying the grid.
307
         */
308
    public boolean isAdjustingToGrid() {
309
        if (adjustToGrid == null) {
310
            adjustToGrid = new Boolean(Layout.getDefaultAdjustToGrid());
311
        }
312
        return adjustToGrid.booleanValue();
313
    }
314
    /**
315
         * It inserts if has been applying in the fframes that are added to Layout the grid, or not.
316
         *
317
         * @param b
318
         *            true  if has been applying the grid.
319
         */
320
    public void setAdjustToGrid(boolean b) {
321
        adjustToGrid = new Boolean(b);
322
    }
323
    /**
324
         * It returns an Object XMLEntity with the information the necessary attributes
325
         * to be able later to create again the original object.
326
         *
327
         * @return XMLEntity.
328
         *
329
         * @throws XMLException
330
         */
331
    public XMLEntity getXMLEntity() {
332
        XMLEntity xml = new XMLEntity();
333
        xml.putProperty("className", this.getClass().getName());
334
        xml.setName("layout");
335
        xml.putProperty("isCuadricula", isAdjustingToGrid());
336
//        xml.putProperty("m_name", this.getName());
337
        xml.putProperty("isEditable", isEditable());
338
        xml.putProperty("numBehind", numBehind);
339
        xml.putProperty("numBefore", numBefore);
340
        xml.addChild(getAttributes().getXMLEntity());
341
        IFFrame[] fframes=getFFrames();
342
        for (int i = 0; i < fframes.length; i++) {
343
            try {
344
                XMLEntity xmlAux = fframes[i].getXMLEntity();
345
                xml.addChild(xmlAux);
346
            } catch (SaveException e) {
347
                e.showError();
348
            }
349
        }
350
        return xml;
351
    }
352
    /**
353
         * It inserts if is shown or not the rule of the Layout.
354
         *
355
         * @param b
356
         *            True if is shown or not the rule of the Layout.
357
         */
358
    public void setRuler(boolean b) {
359
        m_showRuler = new Boolean(b);
360
    }
361

    
362
    /**
363
         * Returns if is shown or not the rule of the Layout.
364
         *
365
         * @return True si se muestra la regla.
366
         */
367
    public boolean getRuler() {
368
        if (m_showRuler == null) {
369
            m_showRuler = new Boolean(Layout.getDefaultShowRulers());
370
        }
371
        return m_showRuler.booleanValue();
372
    }
373

    
374

    
375

    
376

    
377

    
378
    /**
379
         * It returns if has been showing the grid of Layout, or not.
380
         *
381
         * @return true if has been showing the grid of Layout.
382
         */
383
    public boolean isGridVisible() {
384
        if (isGridVisible== null) {
385
            isGridVisible = new Boolean(Layout.getDefaultShowGrid());
386
        }
387
        return isGridVisible.booleanValue();
388
    }
389
    /**
390
         * It inserts if draws the Grid in the Layout or not.
391
         *
392
         * @param b
393
         *            True if draws the Grid in the Layout.
394
         */
395
    public void setGridVisible(boolean b) {
396
        isGridVisible = new Boolean(b);
397
    }
398
    /**
399
         * The dialogs are created here each time that are needed.
400
         *
401
         * @param fframe
402
         *            Rectangle that represents the place that occupied the element added.
403
         *
404
         * @return IFFrame Returns the FFrame added or null if the fframe has not been added.
405
         */
406
        public IFFrame openFFrameDialog(IFFrame frame) {
407
                IFFrameDialog fframedialog = frame.getPropertyDialog();
408
                if (fframedialog != null) {
409
                        fframedialog.setRectangle(frame.getBoundingBox(getAT()));
410
                        PluginServices.getMDIManager().addWindow(fframedialog);
411
                }
412
                return fframedialog.getFFrame();
413
        }
414
        /**
415
         * Refresh all FFrames of Layout.
416
         */
417
        public void fullRefresh() {
418
                IFFrame[] fframes = getFFrames();
419
                for (int i = 0; i < fframes.length; i++) {
420
                        if (fframes[i] instanceof IFFrameUseFMap) {
421
                                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
422
                                fframe.refresh();
423
                        }
424
                }
425
                callLayoutDrawListeners();
426
        }
427

    
428
}