Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / FFrameGroup.java @ 1017

History | View | Annotate | Download (11.3 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.app.project.documents.layout.fframes;
23

    
24
import java.awt.Graphics2D;
25
import java.awt.geom.AffineTransform;
26
import java.awt.geom.Point2D;
27
import java.awt.geom.Rectangle2D;
28
import java.awt.image.BufferedImage;
29
import java.util.ArrayList;
30
import java.util.List;
31

    
32
import org.gvsig.andami.PluginServices;
33
import org.gvsig.app.project.Project;
34
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
35
import org.gvsig.compat.print.PrintAttributes;
36
import org.gvsig.fmap.geom.Geometry;
37
import org.gvsig.tools.ToolsLocator;
38
import org.gvsig.tools.dynobject.DynStruct;
39
import org.gvsig.tools.persistence.PersistenceManager;
40
import org.gvsig.tools.persistence.PersistentState;
41
import org.gvsig.tools.persistence.exception.PersistenceException;
42

    
43
/**
44
 * FFrame que contiene a su vez un ArrayList de FFrames de cualquier tipo
45
 * incluso de si mismo.
46
 * 
47
 * @author Vicente Caballero Navarro
48
 */
49
public class FFrameGroup extends AbstractFFrameViewDependence implements
50
    IFFrameUseProject, IFFrameViewDependence {
51

    
52
    public static final String PERSISTENCE_DEFINITION_NAME = "FFrameGroup";
53

    
54
    private static final String FFRAMES_FIELD = "fframes";
55

    
56
    private List<IFFrame> m_fframes = new ArrayList<IFFrame>();
57
    private Rectangle2D.Double rg = null;
58
    private AffineTransform m_at;
59
    private Project project;
60

    
61
    /**
62
     * Crea un nuevo FFrameGroup.
63
     */
64
    public FFrameGroup() {
65

    
66
    }
67

    
68
    /**
69
     * A?ade al Arraylist un nuevo FFrame para formar parte del grupo.
70
     * 
71
     * @param fframe
72
     *            FFrame a a?adir.
73
     */
74
    public void addFFrame(IFFrame fframe) {
75
        m_fframes.add(fframe);
76
    }
77

    
78
    /**
79
     * Devuelve una ArrayList que contiene todos los FFrames que forman parte
80
     * del grupo.
81
     * 
82
     * @return Arraylist con los fframes.
83
     */
84
    public IFFrame[] getFFrames() {
85
        return m_fframes.toArray(new IFFrame[0]);
86
    }
87

    
88
    /**
89
     * Devuelve el rect?ngulo que contiene a todos los fframes seleccionados.
90
     * 
91
     * @param at
92
     *            Matriz de transformaci?n
93
     * 
94
     * @return Rect?ngulo.
95
     */
96
    public Rectangle2D.Double getRectangle(AffineTransform at) {
97
        boolean first = true;
98
        Rectangle2D.Double rec = new Rectangle2D.Double();
99
        IFFrame[] fframes = getFFrames();
100
        for (int i = 0; i < fframes.length; i++) {
101
            Rectangle2D.Double rs = fframes[i].getBoundingBox(at);
102

    
103
            if (first) {
104
                rec.setRect(rs);
105
                first = false;
106
            }
107

    
108
            rec.add(rs);
109
        }
110

    
111
        rg = new Rectangle2D.Double();
112
        rg.setRect(FLayoutUtilities.toSheetRect(rec, m_at));
113

    
114
        return rec;
115
    }
116

    
117
    /**
118
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
119
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
120
     * de dibujar.
121
     * 
122
     * @param g
123
     *            Graphics
124
     * @param at
125
     *            Transformada afin.
126
     * @param rv
127
     *            rect?ngulo sobre el que hacer un clip.
128
     * @param imgBase
129
     *            Imagen utilizada para acelerar el dibujado.
130
     */
131
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
132
        BufferedImage imgBase) {
133
        Rectangle2D.Double r = getBoundingBox(at);
134
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y
135
            + (r.height / 2));
136
        m_at = at;
137

    
138
        for (int i = 0; i < m_fframes.size(); i++) {
139
            m_fframes.get(i).draw(g, at, rv, imgBase);
140
        }
141

    
142
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
143
            + (r.height / 2));
144
    }
145

    
146
    /**
147
     * Rellena la transformada que se esta utilizando en el Layout.
148
     * 
149
     * @param at
150
     *            Matriz de transformaci?n.
151
     */
152
    public void setAt(AffineTransform at) {
153
        m_at = at;
154
    }
155

    
156
    /**
157
     * Reimplementaci?n del m?todo papa poder modificar los BoundBox de cada
158
     * uno de los FFrames que contiene dentro este FFrameGroup.
159
     * 
160
     * @param r
161
     *            Rect?ngulo.
162
     */
163
    public void setBoundBox(Rectangle2D r) {
164
        getBoundBox().setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
165

    
166
        double dx = 1;
167
        double dy = 1;
168
        double dw = 1;
169
        double dh = 1;
170

    
171
        if (rg != null) {
172
            Rectangle2D.Double raux1 =
173
                new Rectangle2D.Double(rg.x, rg.y, rg.width, rg.height);
174
            dx = r.getX() - raux1.x;
175
            dy = r.getY() - raux1.y;
176
            dw = r.getWidth() / raux1.width;
177
            dh = r.getHeight() / raux1.height;
178
            IFFrame[] fframes = getFFrames();
179
            for (int i = 0; i < fframes.length; i++) {
180
                IFFrame fframe = fframes[i];
181
                Rectangle2D.Double raux = new Rectangle2D.Double();
182
                raux.setRect(fframe.getBoundBox());
183

    
184
                AffineTransform escalado = new AffineTransform();
185

    
186
                escalado.setToScale(dw, dh);
187
                escalado.translate(dx - r.getX(), dy - r.getY());
188

    
189
                Point2D.Double pd = new Point2D.Double();
190
                escalado.transform(new Point2D.Double(raux.x, raux.y), pd);
191

    
192
                raux.x = pd.x + r.getX();
193
                raux.y = pd.y + r.getY();
194
                raux.width = raux.width * dw;
195
                raux.height = raux.height * dh;
196

    
197
                fframe.setBoundBox(raux);
198
            }
199
        } else {
200
            rg = new Rectangle2D.Double();
201
            rg.setRect(r);
202
        }
203

    
204
        rg.setRect(r);
205
    }
206

    
207
    /**
208
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNameFFrame()
209
     */
210
    public String getNameFFrame() {
211
        return PluginServices.getText(this, "grupo") + num;
212
    }
213

    
214
    public String getName() {
215
        return PERSISTENCE_DEFINITION_NAME;
216
    }
217

    
218
    /**
219
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#print(java.awt.Graphics2D,
220
     *      java.awt.geom.AffineTransform)
221
     */
222
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
223
        PrintAttributes printingProperties) {
224
        Rectangle2D.Double r = getBoundingBox(at);
225
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y
226
            + (r.height / 2));
227
        IFFrame[] fframes = m_fframes.toArray(new IFFrame[0]);
228
        for (int i = 0; i < fframes.length; i++) {
229
            fframes[i].print(g, at, geom, printingProperties);
230
        }
231

    
232
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
233
            + (r.height / 2));
234
    }
235

    
236
    /**
237
     * Inserta una referencia al proyecto nesecario.
238
     * 
239
     * @param project
240
     *            DOCUMENT ME!
241
     */
242
    public void setProject(Project project) {
243
        this.project = project;
244
    }
245

    
246
    public void initialize() {
247

    
248
    }
249

    
250
    public void clearFFrames() {
251
        m_fframes.clear();
252
    }
253

    
254
    public IFFrame removeFFrame(int i) {
255
        return m_fframes.remove(i);
256
    }
257

    
258
    public void removeFFrame(IFFrame fframe) {
259
        m_fframes.remove(fframe);
260
    }
261

    
262
    public IFFrame clone() throws CloneNotSupportedException {
263
        FFrameGroup frame = (FFrameGroup) super.clone();
264
        frame.setSelected(this.getSelected() != IFFrame.NOSELECT);
265
        frame.m_fframes = new ArrayList<IFFrame>();
266

    
267
        for (int i = 0; i < m_fframes.size(); i++) {
268
            frame.addFFrame((IFFrame) m_fframes.get(i).clone());
269
        }
270
        frame.m_at = new AffineTransform(m_at);
271
        frame.rg = new Rectangle2D.Double(rg.x, rg.y, rg.width, rg.height);
272
        return frame;
273
    }
274

    
275
    public void setFFrameDependence(IFFrame f) {
276
        IFFrame[] frames = getFFrames();
277
        for (int i = 0; i < frames.length; i++) {
278
            if (frames[i] instanceof IFFrameViewDependence) {
279
                ((IFFrameViewDependence) frames[i]).setFFrameDependence(f);
280
            }
281
        }
282
    }
283

    
284
    public IFFrame[] getFFrameDependence() {
285
        IFFrame[] frames = getFFrames();
286
        ArrayList<IFFrame> dependences = new ArrayList<IFFrame>();
287
        for (int i = 0; i < frames.length; i++) {
288
            if (frames[i] instanceof IFFrameViewDependence) {
289
                IFFrame[] framesAux =
290
                    ((IFFrameViewDependence) frames[i]).getFFrameDependence();
291
                for (int j = 0; j < framesAux.length; j++) {
292
                    dependences.add(framesAux[i]);
293
                }
294
            }
295
        }
296
        return dependences.toArray(new IFFrame[0]);
297
    }
298

    
299
    public void refreshDependence(IFFrame fant, IFFrame fnew) {
300
        IFFrame[] frames = getFFrames();
301
        for (int i = 0; i < frames.length; i++) {
302

    
303
            if (fnew instanceof FFrameGroup) {
304
                IFFrame[] framesGroupNew = ((FFrameGroup) fnew).getFFrames();
305
                for (int j = 0; j < framesGroupNew.length; j++) {
306
                    if (fant instanceof FFrameGroup) {
307
                        IFFrame[] framesGroupAnt =
308
                            ((FFrameGroup) fant).getFFrames();
309
                        for (int k = 0; k < framesGroupAnt.length; k++) {
310
                            if (framesGroupAnt[k] instanceof IFFrameViewDependence) {
311
                                refreshDependence(framesGroupAnt[k],
312
                                    framesGroupNew[j]);
313
                            }
314
                        }
315
                    }
316
                }
317
            } else
318
                if (frames[i] instanceof IFFrameViewDependence) {
319
                    ((IFFrameViewDependence) frames[i]).refreshDependence(fant,
320
                        fnew);
321
                }
322
        }
323
    }
324

    
325
    public static void registerPersistent() {
326
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
327
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
328
            DynStruct definition =
329
                manager.addDefinition(FFrameGroup.class,
330
                    PERSISTENCE_DEFINITION_NAME,
331
                    "FFrameGroup persistence definition", null, null);
332

    
333
            definition
334
                .extend(manager
335
                    .getDefinition(AbstractFFrameViewDependence.PERSISTENCE_DEFINITION_NAME));
336

    
337
            definition.addDynFieldList(FFRAMES_FIELD)
338
                .setClassOfItems(IFFrame.class).setMandatory(true);
339
        }
340
    }
341

    
342
    @Override
343
    public void loadFromState(PersistentState state)
344
        throws PersistenceException {
345
        super.loadFromState(state);
346
        m_fframes = (List<IFFrame>) state.getList(FFRAMES_FIELD);
347
    }
348

    
349
    @Override
350
    public void saveToState(PersistentState state) throws PersistenceException {
351
        super.saveToState(state);
352
        state.set(FFRAMES_FIELD, m_fframes);
353
    }
354
}