Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / fframes / FFrameGroup.java @ 28368

History | View | Annotate | Download (13.5 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.iver.andami.PluginServices;
57
import com.iver.andami.messages.NotificationManager;
58
import com.iver.cit.gvsig.fmap.core.FShape;
59
import com.iver.cit.gvsig.project.Project;
60
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
61
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
62
import com.iver.cit.gvsig.project.documents.layout.FLayoutUtilities;
63
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.FFrameGroupDialog;
64
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
65
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
66
import com.iver.utiles.XMLEntity;
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 ArrayList<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
    }
86

    
87
    /**
88
     * A?ade al Arraylist un nuevo FFrame para formar parte del grupo.
89
     *
90
     * @param fframe FFrame a a?adir.
91
     */
92
    public void addFFrame(IFFrame fframe) {
93
        m_fframes.add(fframe);
94
    }
95

    
96
    /**
97
     * Devuelve una ArrayList que contiene todos los FFrames que forman parte
98
     * del grupo.
99
     *
100
     * @return Arraylist con los fframes.
101
     */
102
    public IFFrame[] getFFrames() {
103
        return m_fframes.toArray(new IFFrame[0]);
104
    }
105

    
106
    /**
107
     * Devuelve el rect?ngulo que contiene a todos los fframes seleccionados.
108
     *
109
     * @param at Matriz de transformaci?n
110
     *
111
     * @return Rect?ngulo.
112
     */
113
    public Rectangle2D.Double getRectangle(AffineTransform at) {
114
        boolean first = true;
115
        Rectangle2D.Double rec = new Rectangle2D.Double();
116
        IFFrame[] fframes=getFFrames();
117
        for (int i = 0; i < fframes.length; i++) {
118
            Rectangle2D.Double rs = fframes[i].getBoundingBox(at);
119

    
120
            if (first) {
121
                rec.setRect(rs);
122
                first = false;
123
            }
124

    
125
            rec.add(rs);
126
        }
127

    
128
        rg = new Rectangle2D.Double();
129
        rg.setRect(FLayoutUtilities.toSheetRect(rec, m_at));
130

    
131
        return rec;
132
    }
133

    
134
    /**
135
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
136
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
137
     * de dibujar.
138
     *
139
     * @param g Graphics
140
     * @param at Transformada afin.
141
     * @param rv rect?ngulo sobre el que hacer un clip.
142
     * @param imgBase Imagen utilizada para acelerar el dibujado.
143
     */
144
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
145
        BufferedImage imgBase) {
146
        Rectangle2D.Double r = getBoundingBox(at);
147
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
148
            r.y + (r.height / 2));
149
        m_at = at;
150

    
151
        for (int i = 0; i < m_fframes.size(); i++) {
152
            m_fframes.get(i).draw(g, at, rv, imgBase);
153
        }
154

    
155
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
156
            r.y + (r.height / 2));
157
    }
158

    
159
    /**
160
     * Rellena la transformada que se esta utilizando en el Layout.
161
     *
162
     * @param at Matriz de transformaci?n.
163
     */
164
    public void setAt(AffineTransform at) {
165
        m_at = at;
166
    }
167

    
168
    /**
169
     * Reimplementaci?n del m?todo papa poder modificar los BoundBox  de cada
170
     * uno de los FFrames que contiene dentro este FFrameGroup.
171
     *
172
     * @param r Rect?ngulo.
173
     */
174
    public void setBoundBox(Rectangle2D r) {
175
        getBoundBox().setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
176

    
177
        double dx = 1;
178
        double dy = 1;
179
        double dw = 1;
180
        double dh = 1;
181

    
182
        if (rg != null) {
183
            Rectangle2D.Double raux1 = new Rectangle2D.Double(rg.x, rg.y,
184
                    rg.width, rg.height);
185
            dx = r.getX() - raux1.x;
186
            dy = r.getY() - raux1.y;
187
            dw = r.getWidth() / raux1.width;
188
            dh = r.getHeight() / raux1.height;
189
            IFFrame[] fframes=getFFrames();
190
            for (int i = 0; i < fframes.length; i++) {
191
                IFFrame fframe = fframes[i];
192
                Rectangle2D.Double raux = new Rectangle2D.Double();
193
                raux.setRect(fframe.getBoundBox());
194

    
195
                AffineTransform escalado = new AffineTransform();
196

    
197
                escalado.setToScale(dw, dh);
198
                escalado.translate(dx - r.getX(), dy - r.getY());
199

    
200
                Point2D.Double pd = new Point2D.Double();
201
                escalado.transform(new Point2D.Double(raux.x, raux.y), pd);
202

    
203
                raux.x = pd.x + r.getX();
204
                raux.y = pd.y + r.getY();
205
                raux.width = raux.width * dw;
206
                raux.height = raux.height * dh;
207

    
208
                fframe.setBoundBox(raux);
209
            }
210
        } else {
211
            rg = new Rectangle2D.Double();
212
            rg.setRect(r);
213
        }
214

    
215
        rg.setRect(r);
216
    }
217

    
218
    /**
219
     * DOCUMENT ME!
220
     *
221
     * @return DOCUMENT ME!
222
     *
223
     * @throws SaveException
224
     *
225
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#getXMLEntity()
226
     */
227
    public XMLEntity getXMLEntity() throws SaveException {
228
        XMLEntity xml = super.getXMLEntity();
229
//        xml.putProperty("type", Layout.RECTANGLEGROUP);
230
        IFFrame[] fframes=getFFrames();
231
        for (int i = 0; i < fframes.length; i++) {
232
            xml.addChild(fframes[i].getXMLEntity());
233
        }
234

    
235
        return xml;
236
    }
237

    
238
    /**
239
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity)
240
     */
241
    public void setXMLEntity03(XMLEntity xml, Layout l) {
242
        if (xml.getIntProperty("m_Selected") != 0) {
243
            this.setSelected(true);
244
        } else {
245
            this.setSelected(false);
246
        }
247

    
248
        IFFrame fframechild = null;
249

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

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

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

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

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

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

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

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

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

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

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

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

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

    
348
        public void initialize() {
349
                // TODO Auto-generated method stub
350

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

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

    
392
        }
393

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

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

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

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

    
438
        }
439
}