Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / fframes / FFrameGroup.java @ 27352

History | View | Annotate | Download (13.6 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 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
import javax.print.attribute.PrintRequestAttributeSet;
55

    
56
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
57
import com.iver.andami.PluginServices;
58
import com.iver.andami.messages.NotificationManager;
59
import com.iver.cit.gvsig.fmap.core.FShape;
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
public class FFrameGroup extends FFrame implements IFFrameUseProject, IFFrameViewDependence{
77
    private ArrayList<IFFrame> m_fframes = new ArrayList<IFFrame>();
78
    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
        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) throws ReadDriverException {
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 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 setXMLEntity03(XMLEntity xml, Layout l) {
243
        if (xml.getIntProperty("m_Selected") != 0) {
244
            this.setSelected(true);
245
        } else {
246
            this.setSelected(false);
247
        }
248

    
249
        IFFrame fframechild = null;
250

    
251
        for (int i = 0; i < xml.getChildrenCount(); i++) {
252
            try {
253
                Class clase = Class.forName(xml.getChild(i).getStringProperty("className"));
254
                fframechild = (IFFrame) clase.newInstance();
255
            } catch (Exception e) {
256
                NotificationManager.addError("Clase de Frame sobre el Layout no reconocida",
257
                    e);
258
            }
259

    
260
            fframechild.setName(xml.getStringProperty("m_name"));
261

    
262
            fframechild.setBoundBox(new Rectangle2D.Double(
263
                    xml.getChild(i).getDoubleProperty("x"),
264
                    xml.getChild(i).getDoubleProperty("y"),
265
                    xml.getChild(i).getDoubleProperty("w"),
266
                    xml.getChild(i).getDoubleProperty("h")));
267
            fframechild.setTag(xml.getChild(i).getStringProperty("tag"));
268
            fframechild.setXMLEntity03(xml.getChild(i), l);
269
            this.addFFrame(fframechild);
270
        }
271
    }
272

    
273
    /**
274
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity)
275
     */
276
    public void setXMLEntity(XMLEntity xml) {
277
        if (xml.getIntProperty("m_Selected") != 0) {
278
            this.setSelected(true);
279
        } else {
280
            this.setSelected(false);
281
        }
282

    
283
        setRotation(xml.getDoubleProperty("m_rotation"));
284

    
285
        for (int i = 0; i < xml.getChildrenCount(); i++) {
286
            try {
287
                IFFrame frame = FFrame.createFromXML(xml.getChild(i), project,getLayout());
288
                this.addFFrame(frame);
289
            } catch (OpenException e) {
290
                e.showError();
291
            }
292
        }
293
    }
294

    
295
    /**
296
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#getNameFFrame()
297
     */
298
    public String getNameFFrame() {
299
        return PluginServices.getText(this, "grupo")+ num;
300
    }
301

    
302
    /**
303
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#print(java.awt.Graphics2D,
304
     *      java.awt.geom.AffineTransform)
305
     */
306
    public void print(Graphics2D g, AffineTransform at,FShape shape, PrintRequestAttributeSet printingProperties)
307
        throws ReadDriverException {
308
        Rectangle2D.Double r = getBoundingBox(at);
309
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
310
            r.y + (r.height / 2));
311
        IFFrame[] fframes=m_fframes.toArray(new IFFrame[0]);
312
        for (int i = 0; i < fframes.length; i++) {
313
//            fframes[i].setPrintingProperties(printingProperties);
314
                fframes[i].print(g, at, shape, printingProperties);
315
//                fframes[i].setPrintingProperties(null);
316
        }
317

    
318
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
319
            r.y + (r.height / 2));
320
    }
321

    
322
    /**
323
     * Inserta una referencia al proyecto nesecario.
324
     *
325
     * @param project DOCUMENT ME!
326
     */
327
    public void setProject(Project project) {
328
        this.project = project;
329
    }
330

    
331
    /**
332
     * DOCUMENT ME!
333
     *
334
     * @param layout DOCUMENT ME!
335
     */
336
    public void setLayout(Layout layout) {
337
        super.setLayout(layout);
338
            IFFrame[] fsoriginal= layout.getLayoutContext().getAllFFrames();
339
        IFFrame[] fs = getFFrames();
340

    
341
        for (int i = 0; i < fs.length; i++) {
342
                fs[i].setLayout(layout);
343

    
344
            if (fs[i] instanceof IFFrameViewDependence) {
345
                ((IFFrameViewDependence) fs[i]).initDependence(fsoriginal);
346
            }
347
        }
348
    }
349

    
350
        public void initialize() {
351
                // TODO Auto-generated method stub
352

    
353
        }
354
        public void clearFFrames(){
355
                m_fframes.clear();
356
        }
357
        public IFFrame removeFFrame(int i){
358
                return m_fframes.remove(i);
359
        }
360
        public void removeFFrame(IFFrame fframe){
361
                m_fframes.remove(fframe);
362
        }
363
        public void cloneActions(IFFrame frame) {
364
                // TODO Auto-generated method stub
365
        }
366
        public IFFrame cloneFFrame(Layout layout) {
367
                FFrameGroup frame =(FFrameGroup)FrameFactory.createFrameFromName(FFrameGroupFactory.registerName);
368
                frame.setSelected(this.getSelected()!=IFFrame.NOSELECT);
369
                frame.setLevel(this.getLevel());
370
            frame.setNum(this.num);
371
            frame.setName(this.getName());
372
            frame.setBoundBox(this.getBoundBox());
373
            frame.setTag(this.getTag());
374
            frame.setRotation(this.getRotation());
375
            frame.setLayout(layout);
376
            frame.m_at=m_at;
377
            for(int i=0;i<m_fframes.size();i++) {
378
                    frame.addFFrame(m_fframes.get(i).cloneFFrame(layout));
379
            }
380
            return frame;
381
        }
382
        public IFFrameDialog getPropertyDialog() {
383
                return new FFrameGroupDialog(getLayout(),this);
384
        }
385

    
386
        public void setFFrameDependence(IFFrame f) {
387
                IFFrame[] frames=getFFrames();
388
                for (int i =0;i<frames.length;i++){
389
                        if (frames[i] instanceof IFFrameViewDependence){
390
                                ((IFFrameViewDependence)frames[i]).setFFrameDependence(f);
391
                        }
392
                }
393

    
394
        }
395

    
396
        public IFFrame[] getFFrameDependence() {
397
                IFFrame[] frames=getFFrames();
398
                ArrayList<IFFrame> dependences=new ArrayList<IFFrame>();
399
                for (int i =0;i<frames.length;i++){
400
                        if (frames[i] instanceof IFFrameViewDependence){
401
                                IFFrame[] framesAux=((IFFrameViewDependence)frames[i]).getFFrameDependence();
402
                                        for (int j =0;j<framesAux.length;j++){
403
                                                dependences.add(framesAux[i]);
404
                                        }
405
                        }
406
                }
407
                return dependences.toArray(new IFFrame[0]);
408
        }
409

    
410
        public void initDependence(IFFrame[] fframes) {
411
                IFFrame[] frames=getFFrames();
412
                for (int i =0;i<frames.length;i++){
413
                        if (frames[i] instanceof IFFrameViewDependence){
414
                                ((IFFrameViewDependence)frames[i]).initDependence(fframes);
415
                        }
416
                }
417
        }
418

    
419
        public void refreshDependence(IFFrame fant, IFFrame fnew) {
420
                IFFrame[] frames=getFFrames();
421
                for (int i =0;i<frames.length;i++){
422

    
423
                        if (fnew instanceof FFrameGroup){
424
                                IFFrame[] framesGroupNew=((FFrameGroup)fnew).getFFrames();
425
                                for (int j=0;j<framesGroupNew.length;j++){
426
                                        if (fant instanceof FFrameGroup){
427
                                                IFFrame[] framesGroupAnt=((FFrameGroup)fant).getFFrames();
428
                                                for (int k=0;k<framesGroupAnt.length;k++){
429
                                                        if (framesGroupAnt[k] instanceof IFFrameViewDependence){
430
                                                                refreshDependence(framesGroupAnt[k],framesGroupNew[j]);
431
                                                        }
432
                                                }
433
                                        }
434
                                }
435
                        }else if (frames[i] instanceof IFFrameViewDependence){
436
                                ((IFFrameViewDependence)frames[i]).refreshDependence(fant,fnew);
437
                        }
438
                }
439

    
440
        }
441
}