Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / layout / fframes / FFrameGroup.java @ 31496

History | View | Annotate | Download (12.1 KB)

1
/*
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 org.gvsig.app.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
import java.util.List;
54

    
55
import org.gvsig.andami.PluginServices;
56
import org.gvsig.app.project.Project;
57
import org.gvsig.app.project.documents.exceptions.OpenException;
58
import org.gvsig.app.project.documents.exceptions.SaveException;
59
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
60
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameGroupDialog;
61
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
62
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
63
import org.gvsig.compat.print.PrintAttributes;
64
import org.gvsig.fmap.geom.Geometry;
65
import org.gvsig.utils.XMLEntity;
66

    
67

    
68

    
69
/**
70
 * FFrame que contiene a su vez un ArrayList de FFrames de cualquier tipo
71
 * incluso de si mismo.
72
 *
73
 * @author Vicente Caballero Navarro
74
 */
75
public class FFrameGroup extends FFrame implements IFFrameUseProject, IFFrameViewDependence{
76
    private List<IFFrame> m_fframes = new ArrayList<IFFrame>();
77
    private Rectangle2D.Double rg = null;
78
    private AffineTransform m_at;
79
    private Project project;
80

    
81
    /**
82
     * Crea un nuevo FFrameGroup.
83
     */
84
    public FFrameGroup() {
85
            // do nothing
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
        return m_fframes.toArray(new IFFrame[0]);
105
    }
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
        BufferedImage imgBase) {
147
        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
            m_fframes.get(i).draw(g, at, rv, imgBase);
154
        }
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 org.gvsig.app.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 org.gvsig.app.project.documents.layout.fframes.IFFrame#setXMLEntity(org.gvsig.utils.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
                IFFrame frame = FFrame.createFromXML(xml.getChild(i), project,getLayout());
254
                this.addFFrame(frame);
255
            } catch (OpenException e) {
256
                e.showError();
257
            }
258
        }
259
    }
260

    
261
    /**
262
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNameFFrame()
263
     */
264
    public String getNameFFrame() {
265
        return PluginServices.getText(this, "grupo")+ num;
266
    }
267

    
268
    /**
269
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#print(java.awt.Graphics2D,
270
     *      java.awt.geom.AffineTransform)
271
     */
272
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
273
                        PrintAttributes printingProperties) {
274
        Rectangle2D.Double r = getBoundingBox(at);
275
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
276
            r.y + (r.height / 2));
277
        IFFrame[] fframes=m_fframes.toArray(new IFFrame[0]);
278
        for (int i = 0; i < fframes.length; i++) {
279
//            fframes[i].setPrintingProperties(printingProperties);
280
                fframes[i].print(g, at, geom, printingProperties);
281
//                fframes[i].setPrintingProperties(null);
282
        }
283

    
284
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
285
            r.y + (r.height / 2));
286
    }
287

    
288
    /**
289
     * Inserta una referencia al proyecto nesecario.
290
     *
291
     * @param project DOCUMENT ME!
292
     */
293
    public void setProject(Project project) {
294
        this.project = project;
295
    }
296

    
297
    /**
298
     * DOCUMENT ME!
299
     *
300
     * @param layout DOCUMENT ME!
301
     */
302
    public void setLayout(LayoutPanel layout) {
303
        super.setLayout(layout);
304
            IFFrame[] fsoriginal= layout.getLayoutContext().getAllFFrames();
305
        IFFrame[] fs = getFFrames();
306

    
307
        for (int i = 0; i < fs.length; i++) {
308
                fs[i].setLayout(layout);
309

    
310
            if (fs[i] instanceof IFFrameViewDependence) {
311
                ((IFFrameViewDependence) fs[i]).initDependence(fsoriginal);
312
            }
313
        }
314
    }
315

    
316
        public void initialize() {
317
                // TODO Auto-generated method stub
318

    
319
        }
320
        public void clearFFrames(){
321
                m_fframes.clear();
322
        }
323
        public IFFrame removeFFrame(int i){
324
                return m_fframes.remove(i);
325
        }
326
        public void removeFFrame(IFFrame fframe){
327
                m_fframes.remove(fframe);
328
        }
329
        public void cloneActions(IFFrame frame) {
330
                // TODO Auto-generated method stub
331
        }
332
        public IFFrame cloneFFrame(LayoutPanel layout) {
333
                FFrameGroup frame =(FFrameGroup)FrameFactory.createFrameFromName(FFrameGroupFactory.registerName);
334
                frame.setSelected(this.getSelected()!=IFFrame.NOSELECT);
335
                frame.setLevel(this.getLevel());
336
            frame.setNum(this.num);
337
            frame.setName(this.getName());
338
            frame.setBoundBox(this.getBoundBox());
339
            frame.setTag(this.getTag());
340
            frame.setRotation(this.getRotation());
341
            frame.setLayout(layout);
342
            frame.m_at=m_at;
343
            for(int i=0;i<m_fframes.size();i++) {
344
                    frame.addFFrame(m_fframes.get(i).cloneFFrame(layout));
345
            }
346
            return frame;
347
        }
348
        public IFFrameDialog getPropertyDialog() {
349
                return new FFrameGroupDialog(getLayout(),this);
350
        }
351

    
352
        public void setFFrameDependence(IFFrame f) {
353
                IFFrame[] frames=getFFrames();
354
                for (int i =0;i<frames.length;i++){
355
                        if (frames[i] instanceof IFFrameViewDependence){
356
                                ((IFFrameViewDependence)frames[i]).setFFrameDependence(f);
357
                        }
358
                }
359

    
360
        }
361

    
362
        public IFFrame[] getFFrameDependence() {
363
                IFFrame[] frames=getFFrames();
364
                ArrayList<IFFrame> dependences=new ArrayList<IFFrame>();
365
                for (int i =0;i<frames.length;i++){
366
                        if (frames[i] instanceof IFFrameViewDependence){
367
                                IFFrame[] framesAux=((IFFrameViewDependence)frames[i]).getFFrameDependence();
368
                                        for (int j =0;j<framesAux.length;j++){
369
                                                dependences.add(framesAux[i]);
370
                                        }
371
                        }
372
                }
373
                return dependences.toArray(new IFFrame[0]);
374
        }
375

    
376
        public void initDependence(IFFrame[] fframes) {
377
                IFFrame[] frames=getFFrames();
378
                for (int i =0;i<frames.length;i++){
379
                        if (frames[i] instanceof IFFrameViewDependence){
380
                                ((IFFrameViewDependence)frames[i]).initDependence(fframes);
381
                        }
382
                }
383
        }
384

    
385
        public void refreshDependence(IFFrame fant, IFFrame fnew) {
386
                IFFrame[] frames=getFFrames();
387
                for (int i =0;i<frames.length;i++){
388

    
389
                        if (fnew instanceof FFrameGroup){
390
                                IFFrame[] framesGroupNew=((FFrameGroup)fnew).getFFrames();
391
                                for (int j=0;j<framesGroupNew.length;j++){
392
                                        if (fant instanceof FFrameGroup){
393
                                                IFFrame[] framesGroupAnt=((FFrameGroup)fant).getFFrames();
394
                                                for (int k=0;k<framesGroupAnt.length;k++){
395
                                                        if (framesGroupAnt[k] instanceof IFFrameViewDependence){
396
                                                                refreshDependence(framesGroupAnt[k],framesGroupNew[j]);
397
                                                        }
398
                                                }
399
                                        }
400
                                }
401
                        }else if (frames[i] instanceof IFFrameViewDependence){
402
                                ((IFFrameViewDependence)frames[i]).refreshDependence(fant,fnew);
403
                        }
404
                }
405

    
406
        }
407
}