Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrameGroup.java @ 6117

History | View | Annotate | Download (10.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.gui.layout.fframes;
46

    
47
import com.iver.andami.PluginServices;
48
import com.iver.andami.messages.NotificationManager;
49

    
50
import com.iver.cit.gvsig.fmap.DriverException;
51
import com.iver.cit.gvsig.gui.layout.FLayoutUtilities;
52
import com.iver.cit.gvsig.gui.layout.Layout;
53
import com.iver.cit.gvsig.gui.project.OpenException;
54
import com.iver.cit.gvsig.gui.project.SaveException;
55
import com.iver.cit.gvsig.project.Project;
56

    
57
import com.iver.utiles.XMLEntity;
58

    
59
import java.awt.Graphics2D;
60
import java.awt.geom.AffineTransform;
61
import java.awt.geom.Point2D;
62
import java.awt.geom.Rectangle2D;
63
import java.awt.image.BufferedImage;
64

    
65
import java.util.ArrayList;
66

    
67

    
68
/**
69
 * FFrame que contiene a su vez un ArrayList de FFrames de cualquier tipo
70
 * incluso de si mismo.
71
 *
72
 * @author Vicente Caballero Navarro
73
 */
74
public class FFrameGroup extends FFrame implements IFFrameUseProject,
75
    IFFrameLayoutDependence {
76
    private ArrayList m_fframes = new ArrayList();
77
    private Rectangle2D.Double rg = null;
78
    private AffineTransform m_at;
79
    private Project project;
80
        private Layout layout;
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 (IFFrame[]) 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
     * @throws DriverException
146
     */
147
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
148
        BufferedImage imgBase) throws DriverException {
149
        Rectangle2D.Double r = getBoundingBox(at);
150
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
151
            r.y + (r.height / 2));
152
        m_at = at;
153

    
154
        for (int i = 0; i < m_fframes.size(); i++) {
155
            ((IFFrame) m_fframes.get(i)).draw(g, at, rv, imgBase);
156
        }
157

    
158
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
159
            r.y + (r.height / 2));
160
    }
161

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

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

    
180
        double dx = 1;
181
        double dy = 1;
182
        double dw = 1;
183
        double dh = 1;
184

    
185
        if (rg != null) {
186
            Rectangle2D.Double raux1 = new Rectangle2D.Double(rg.x, rg.y,
187
                    rg.width, rg.height);
188
            dx = r.getX() - raux1.x;
189
            dy = r.getY() - raux1.y;
190
            dw = r.getWidth() / raux1.width;
191
            dh = r.getHeight() / raux1.height;
192

    
193
            for (int i = 0; i < getFFrames().length; i++) {
194
                IFFrame fframe = (IFFrame) getFFrames()[i];
195
                Rectangle2D.Double raux = new Rectangle2D.Double();
196
                raux.setRect(fframe.getBoundBox());
197

    
198
                AffineTransform escalado = new AffineTransform();
199

    
200
                escalado.setToScale(dw, dh);
201
                escalado.translate(dx - r.getX(), dy - r.getY());
202

    
203
                Point2D.Double pd = new Point2D.Double();
204
                escalado.transform(new Point2D.Double(raux.x, raux.y), pd);
205

    
206
                raux.x = pd.x + r.getX();
207
                raux.y = pd.y + r.getY();
208
                raux.width = raux.width * dw;
209
                raux.height = raux.height * dh;
210

    
211
                fframe.setBoundBox(raux);
212
            }
213
        } else {
214
            rg = new Rectangle2D.Double();
215
            rg.setRect(r);
216
        }
217

    
218
        rg.setRect(r);
219
    }
220

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

    
238
        return xml;
239
    }
240

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

    
251
        IFFrame fframechild = null;
252

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

    
262
            fframechild.setName(xml.getStringProperty("m_name"));
263

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

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

    
285
        setRotation(xml.getDoubleProperty("m_rotation"));
286

    
287
        for (int i = 0; i < xml.getNumChild(); i++) {
288
            try {
289
                IFFrame frame = FFrame.createFFrame(xml.getChild(i), project,layout);
290
                this.addFFrame(frame);
291
            } catch (OpenException e) {
292
                e.showError();
293
            }
294
        }
295
    }
296

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

    
304
    /**
305
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
306
     *      java.awt.geom.AffineTransform)
307
     */
308
    public void print(Graphics2D g, AffineTransform at)
309
        throws DriverException {
310
        Rectangle2D.Double r = getBoundingBox(at);
311
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
312
            r.y + (r.height / 2));
313

    
314
        for (int i = 0; i < m_fframes.size(); i++) {
315
            ((IFFrame) m_fframes.get(i)).print(g, at);
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
        this.layout=layout;
338
            IFFrame[] fs = getFFrames();
339

    
340
        for (int i = 0; i < fs.length; i++) {
341
            if (fs[i] instanceof IFFrameLayoutDependence) {
342
                ((IFFrameLayoutDependence) fs[i]).setLayout(layout);
343
            }
344

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

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

    
354
        }
355
        public void clearFFrames(){
356
                m_fframes.clear();
357
        }
358
        public IFFrame removeFFrame(int i){
359
                return (IFFrame)m_fframes.remove(i);
360
        }
361
        public void removeFFrame(IFFrame fframe){
362
                m_fframes.remove(fframe);
363
        }
364

    
365
        public Layout getLayout() {
366
                return layout;
367
        }
368
}