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
package com.iver.cit.gvsig.project.documents.layout;
2

    
3
import java.awt.geom.AffineTransform;
4
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
import com.iver.cit.gvsig.project.documents.layout.commands.FrameCommandsRecord;
10
import com.iver.cit.gvsig.project.documents.layout.commands.FrameManager;
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.fframes.IFFrameUseFMap;
14
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
15
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
16
import com.iver.utiles.XMLEntity;
17
import com.iver.utiles.XMLException;
18

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

    
128
            if (fframe.getSelected() != IFFrame.NOSELECT) {
129
                fcr.delete(fframe);
130
            }
131
        }
132
        fcr.endComplex();
133
        updateFFrames();
134
    }
135
    /**
136
         * Clear the selection of FFrames.
137
         *
138
         */
139
    public void clearSelection() {
140
        for (int i = fcr.getFrameManager().getAllFFrames().length - 1; i >= 0; i--) {
141
            IFFrame fframe = fcr.getFrameManager().getFFrame(i);
142
            if (fframe.getSelected() != IFFrame.NOSELECT) {
143
                fframe.setSelected(false);
144
            }
145
        }
146
    }
147
    /**
148
         * Remove the fframe of index.
149
         *
150
         */
151
    public void delFFrame(int index) {
152
        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
            }
157
        }
158
        updateFFrames();
159
    }
160
    /**
161
         * Remove the fframe of parameter.
162
         * @param frame
163
         *            FFrame to remove.
164
         */
165
    public void delFFrame(IFFrame frame) {
166
        for (int i = 0; i < fcr.getFrameManager().getAllFFrames().length; i++) {
167
            if (fcr.getFrameManager().getFFrame(i).equals(frame)) {
168
                fcr.delete(frame);
169
            }
170
        }
171
        updateFFrames();
172
    }
173
    /**
174
         * Returns the EditableFeatureSource, is the control of all change in the FFrames of Layout.
175
         * @return EditableFatureSource.
176
         */
177
    public FrameCommandsRecord getFrameCommandsRecord() {
178
        return fcr;
179
    }
180
    /**
181
         * 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
    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
        fcr.insert(frame);
208
        frame.setSelected(select);
209
        frame.setLevel(getNumBefore());
210
        updateFFrames();
211
    }
212
    /**
213
         * 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
                fcr.insert(frame);
219
                frame.setSelected(true);
220
                frame.setLevel(getNumBefore());
221
                updateFFrames();
222
        }
223
    /**
224
         * Returns other number behind the current fframes.
225
         * @return new Position behind.
226
         */
227
    public int getNumBehind() {
228
        return --numBehind;
229
    }
230
    /**
231
         * Returns other number before the current fframes.
232
         * @return new Position before.
233
         */
234
    public int getNumBefore() {
235
        return ++numBefore;
236
    }
237
    /**
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
    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
                    ArrayList groupFrames=allFFrames(((FFrameGroup) fframes[i]).getFFrames(), all);
251
                    if (!all.containsAll(groupFrames)) {
252
                                        all.addAll(groupFrames);
253
                                }
254

    
255
            }else {
256
                    if (!all.contains(fframes[i])) {
257
                                        all.add(fframes[i]);
258
                                }
259
            }
260
        }
261
        return all;
262
    }
263
    /**
264
         * It returns an array with the FFrames selected.
265
         *
266
         * @return Array with the FFrames selected.
267
         */
268
    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
    /**
282
         * It returns if the Layout is in edition.
283
         * @return True if Layout is in edition.
284
         */
285
    public boolean isEditable() {
286
        return isEditable;
287
    }
288
    /**
289
         * It inserts if the Layout is in edition.
290
         * @param b
291
         *            True if Layout is in edition.
292
         */
293
    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
         * 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
    public boolean isAdjustingToGrid() {
308
        if (adjustToGrid == null) {
309
            adjustToGrid = new Boolean(Layout.getDefaultAdjustToGrid());
310
        }
311
        return adjustToGrid.booleanValue();
312
    }
313
    /**
314
         * 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
    public void setAdjustToGrid(boolean b) {
320
        adjustToGrid = new Boolean(b);
321
    }
322
    /**
323
         * 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
    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
        xml.addChild(getAttributes().getXMLEntity());
340
        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
         * 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
    public void setRuler(boolean b) {
358
        m_showRuler = new Boolean(b);
359
    }
360

    
361
    /**
362
         * Returns if is shown or not the rule of the Layout.
363
         *
364
         * @return True si se muestra la regla.
365
         */
366
    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
         * 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
    public boolean isGridVisible() {
383
        if (isGridVisible== null) {
384
            isGridVisible = new Boolean(Layout.getDefaultShowGrid());
385
        }
386
        return isGridVisible.booleanValue();
387
    }
388
    /**
389
         * 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
    public void setGridVisible(boolean b) {
395
        isGridVisible = new Boolean(b);
396
    }
397
    /**
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
        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
        /**
414
         * Refresh all FFrames of Layout.
415
         */
416
        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
}