Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / fframes / FFrameGroup.java @ 28546

History | View | Annotate | Download (12.2 KB)

1 9392 caballero
/*
2
 * Created on 15-jul-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.project.documents.layout.fframes;
46
47
import java.awt.Graphics2D;
48
import java.awt.geom.AffineTransform;
49
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51
import java.awt.image.BufferedImage;
52
import java.util.ArrayList;
53
54 10911 jaume
import javax.print.attribute.PrintRequestAttributeSet;
55
56 24759 jmvivo
import org.gvsig.fmap.dal.exception.ReadException;
57 21154 vcaballero
import org.gvsig.fmap.geom.Geometry;
58 24759 jmvivo
59 9392 caballero
import com.iver.andami.PluginServices;
60
import com.iver.cit.gvsig.project.Project;
61
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
62
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
63
import com.iver.cit.gvsig.project.documents.layout.FLayoutUtilities;
64
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.FFrameGroupDialog;
65
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
66
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
67
import com.iver.utiles.XMLEntity;
68
69
70
/**
71
 * FFrame que contiene a su vez un ArrayList de FFrames de cualquier tipo
72
 * incluso de si mismo.
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76 11800 caballero
public class FFrameGroup extends FFrame implements IFFrameUseProject, IFFrameViewDependence{
77 13947 caballero
    private ArrayList<IFFrame> m_fframes = new ArrayList<IFFrame>();
78 9392 caballero
    private Rectangle2D.Double rg = null;
79
    private AffineTransform m_at;
80
    private Project project;
81
82
    /**
83
     * Crea un nuevo FFrameGroup.
84
     */
85
    public FFrameGroup() {
86
    }
87
88
    /**
89
     * A?ade al Arraylist un nuevo FFrame para formar parte del grupo.
90
     *
91
     * @param fframe FFrame a a?adir.
92
     */
93
    public void addFFrame(IFFrame fframe) {
94
        m_fframes.add(fframe);
95
    }
96
97
    /**
98
     * Devuelve una ArrayList que contiene todos los FFrames que forman parte
99
     * del grupo.
100
     *
101
     * @return Arraylist con los fframes.
102
     */
103
    public IFFrame[] getFFrames() {
104 13947 caballero
        return m_fframes.toArray(new IFFrame[0]);
105 9392 caballero
    }
106
107
    /**
108
     * Devuelve el rect?ngulo que contiene a todos los fframes seleccionados.
109
     *
110
     * @param at Matriz de transformaci?n
111
     *
112
     * @return Rect?ngulo.
113
     */
114
    public Rectangle2D.Double getRectangle(AffineTransform at) {
115
        boolean first = true;
116
        Rectangle2D.Double rec = new Rectangle2D.Double();
117
        IFFrame[] fframes=getFFrames();
118
        for (int i = 0; i < fframes.length; i++) {
119
            Rectangle2D.Double rs = fframes[i].getBoundingBox(at);
120
121
            if (first) {
122
                rec.setRect(rs);
123
                first = false;
124
            }
125
126
            rec.add(rs);
127
        }
128
129
        rg = new Rectangle2D.Double();
130
        rg.setRect(FLayoutUtilities.toSheetRect(rec, m_at));
131
132
        return rec;
133
    }
134
135
    /**
136
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
137
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
138
     * de dibujar.
139
     *
140
     * @param g Graphics
141
     * @param at Transformada afin.
142
     * @param rv rect?ngulo sobre el que hacer un clip.
143
     * @param imgBase Imagen utilizada para acelerar el dibujado.
144
     */
145
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
146 28370 vcaballero
        BufferedImage imgBase) {
147 9392 caballero
        Rectangle2D.Double r = getBoundingBox(at);
148
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
149
            r.y + (r.height / 2));
150
        m_at = at;
151
152
        for (int i = 0; i < m_fframes.size(); i++) {
153 13947 caballero
            m_fframes.get(i).draw(g, at, rv, imgBase);
154 9392 caballero
        }
155
156
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
157
            r.y + (r.height / 2));
158
    }
159
160
    /**
161
     * Rellena la transformada que se esta utilizando en el Layout.
162
     *
163
     * @param at Matriz de transformaci?n.
164
     */
165
    public void setAt(AffineTransform at) {
166
        m_at = at;
167
    }
168
169
    /**
170
     * Reimplementaci?n del m?todo papa poder modificar los BoundBox  de cada
171
     * uno de los FFrames que contiene dentro este FFrameGroup.
172
     *
173
     * @param r Rect?ngulo.
174
     */
175
    public void setBoundBox(Rectangle2D r) {
176
        getBoundBox().setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
177
178
        double dx = 1;
179
        double dy = 1;
180
        double dw = 1;
181
        double dh = 1;
182
183
        if (rg != null) {
184
            Rectangle2D.Double raux1 = new Rectangle2D.Double(rg.x, rg.y,
185
                    rg.width, rg.height);
186
            dx = r.getX() - raux1.x;
187
            dy = r.getY() - raux1.y;
188
            dw = r.getWidth() / raux1.width;
189
            dh = r.getHeight() / raux1.height;
190
            IFFrame[] fframes=getFFrames();
191
            for (int i = 0; i < fframes.length; i++) {
192
                IFFrame fframe = fframes[i];
193
                Rectangle2D.Double raux = new Rectangle2D.Double();
194
                raux.setRect(fframe.getBoundBox());
195
196
                AffineTransform escalado = new AffineTransform();
197
198
                escalado.setToScale(dw, dh);
199
                escalado.translate(dx - r.getX(), dy - r.getY());
200
201
                Point2D.Double pd = new Point2D.Double();
202
                escalado.transform(new Point2D.Double(raux.x, raux.y), pd);
203
204
                raux.x = pd.x + r.getX();
205
                raux.y = pd.y + r.getY();
206
                raux.width = raux.width * dw;
207
                raux.height = raux.height * dh;
208
209
                fframe.setBoundBox(raux);
210
            }
211
        } else {
212
            rg = new Rectangle2D.Double();
213
            rg.setRect(r);
214
        }
215
216
        rg.setRect(r);
217
    }
218
219
    /**
220
     * DOCUMENT ME!
221
     *
222
     * @return DOCUMENT ME!
223
     *
224
     * @throws SaveException
225
     *
226
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#getXMLEntity()
227
     */
228
    public XMLEntity getXMLEntity() throws SaveException {
229
        XMLEntity xml = super.getXMLEntity();
230
//        xml.putProperty("type", Layout.RECTANGLEGROUP);
231
        IFFrame[] fframes=getFFrames();
232
        for (int i = 0; i < fframes.length; i++) {
233
            xml.addChild(fframes[i].getXMLEntity());
234
        }
235
236
        return xml;
237
    }
238
239
    /**
240
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity)
241
     */
242
    public void setXMLEntity(XMLEntity xml) {
243
        if (xml.getIntProperty("m_Selected") != 0) {
244
            this.setSelected(true);
245
        } else {
246
            this.setSelected(false);
247
        }
248
249
        setRotation(xml.getDoubleProperty("m_rotation"));
250
251
        for (int i = 0; i < xml.getChildrenCount(); i++) {
252
            try {
253 9516 caballero
                IFFrame frame = FFrame.createFromXML(xml.getChild(i), project,getLayout());
254 9392 caballero
                this.addFFrame(frame);
255
            } catch (OpenException e) {
256
                e.showError();
257
            }
258
        }
259
    }
260
261
    /**
262
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#getNameFFrame()
263
     */
264
    public String getNameFFrame() {
265
        return PluginServices.getText(this, "grupo")+ num;
266
    }
267
268
    /**
269
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#print(java.awt.Graphics2D,
270
     *      java.awt.geom.AffineTransform)
271
     */
272 28370 vcaballero
    public void print(Graphics2D g, AffineTransform at,Geometry geom, PrintRequestAttributeSet printingProperties){
273 9392 caballero
        Rectangle2D.Double r = getBoundingBox(at);
274
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
275
            r.y + (r.height / 2));
276 13947 caballero
        IFFrame[] fframes=m_fframes.toArray(new IFFrame[0]);
277 9593 caballero
        for (int i = 0; i < fframes.length; i++) {
278 10911 jaume
//            fframes[i].setPrintingProperties(printingProperties);
279 21299 vcaballero
                fframes[i].print(g, at, geom, printingProperties);
280 10911 jaume
//                fframes[i].setPrintingProperties(null);
281 9392 caballero
        }
282
283
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
284
            r.y + (r.height / 2));
285
    }
286
287
    /**
288
     * Inserta una referencia al proyecto nesecario.
289
     *
290
     * @param project DOCUMENT ME!
291
     */
292
    public void setProject(Project project) {
293
        this.project = project;
294
    }
295
296
    /**
297
     * DOCUMENT ME!
298
     *
299
     * @param layout DOCUMENT ME!
300
     */
301
    public void setLayout(Layout layout) {
302
        super.setLayout(layout);
303 9631 caballero
            IFFrame[] fsoriginal= layout.getLayoutContext().getAllFFrames();
304
        IFFrame[] fs = getFFrames();
305 9392 caballero
306
        for (int i = 0; i < fs.length; i++) {
307
                fs[i].setLayout(layout);
308
309
            if (fs[i] instanceof IFFrameViewDependence) {
310 9631 caballero
                ((IFFrameViewDependence) fs[i]).initDependence(fsoriginal);
311 9392 caballero
            }
312
        }
313
    }
314
315
        public void initialize() {
316
                // TODO Auto-generated method stub
317
318
        }
319
        public void clearFFrames(){
320
                m_fframes.clear();
321
        }
322
        public IFFrame removeFFrame(int i){
323 13947 caballero
                return m_fframes.remove(i);
324 9392 caballero
        }
325
        public void removeFFrame(IFFrame fframe){
326
                m_fframes.remove(fframe);
327
        }
328
        public void cloneActions(IFFrame frame) {
329
                // TODO Auto-generated method stub
330
        }
331 9631 caballero
        public IFFrame cloneFFrame(Layout layout) {
332
                FFrameGroup frame =(FFrameGroup)FrameFactory.createFrameFromName(FFrameGroupFactory.registerName);
333
                frame.setSelected(this.getSelected()!=IFFrame.NOSELECT);
334
                frame.setLevel(this.getLevel());
335
            frame.setNum(this.num);
336
            frame.setName(this.getName());
337
            frame.setBoundBox(this.getBoundBox());
338
            frame.setTag(this.getTag());
339
            frame.setRotation(this.getRotation());
340
            frame.setLayout(layout);
341
            frame.m_at=m_at;
342
            for(int i=0;i<m_fframes.size();i++) {
343 13947 caballero
                    frame.addFFrame(m_fframes.get(i).cloneFFrame(layout));
344 9631 caballero
            }
345
            return frame;
346
        }
347 9392 caballero
        public IFFrameDialog getPropertyDialog() {
348
                return new FFrameGroupDialog(getLayout(),this);
349
        }
350 11800 caballero
351
        public void setFFrameDependence(IFFrame f) {
352
                IFFrame[] frames=getFFrames();
353
                for (int i =0;i<frames.length;i++){
354
                        if (frames[i] instanceof IFFrameViewDependence){
355
                                ((IFFrameViewDependence)frames[i]).setFFrameDependence(f);
356
                        }
357
                }
358
359
        }
360
361
        public IFFrame[] getFFrameDependence() {
362
                IFFrame[] frames=getFFrames();
363 13947 caballero
                ArrayList<IFFrame> dependences=new ArrayList<IFFrame>();
364 11800 caballero
                for (int i =0;i<frames.length;i++){
365
                        if (frames[i] instanceof IFFrameViewDependence){
366 13947 caballero
                                IFFrame[] framesAux=((IFFrameViewDependence)frames[i]).getFFrameDependence();
367
                                        for (int j =0;j<framesAux.length;j++){
368
                                                dependences.add(framesAux[i]);
369
                                        }
370 11800 caballero
                        }
371
                }
372 13947 caballero
                return dependences.toArray(new IFFrame[0]);
373 11800 caballero
        }
374
375
        public void initDependence(IFFrame[] fframes) {
376
                IFFrame[] frames=getFFrames();
377
                for (int i =0;i<frames.length;i++){
378
                        if (frames[i] instanceof IFFrameViewDependence){
379
                                ((IFFrameViewDependence)frames[i]).initDependence(fframes);
380
                        }
381
                }
382
        }
383
384
        public void refreshDependence(IFFrame fant, IFFrame fnew) {
385
                IFFrame[] frames=getFFrames();
386
                for (int i =0;i<frames.length;i++){
387 12130 caballero
388 12056 caballero
                        if (fnew instanceof FFrameGroup){
389
                                IFFrame[] framesGroupNew=((FFrameGroup)fnew).getFFrames();
390
                                for (int j=0;j<framesGroupNew.length;j++){
391
                                        if (fant instanceof FFrameGroup){
392
                                                IFFrame[] framesGroupAnt=((FFrameGroup)fant).getFFrames();
393
                                                for (int k=0;k<framesGroupAnt.length;k++){
394
                                                        if (framesGroupAnt[k] instanceof IFFrameViewDependence){
395
                                                                refreshDependence(framesGroupAnt[k],framesGroupNew[j]);
396
                                                        }
397
                                                }
398
                                        }
399
                                }
400 12130 caballero
                        }else if (frames[i] instanceof IFFrameViewDependence){
401
                                ((IFFrameViewDependence)frames[i]).refreshDependence(fant,fnew);
402 12056 caballero
                        }
403 11800 caballero
                }
404 12130 caballero
405 11800 caballero
        }
406 9392 caballero
}