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 @ 1714

History | View | Annotate | Download (11.4 KB)

1 5 jldominguez
/* 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
import org.gvsig.andami.PluginServices;
32
import org.gvsig.app.project.Project;
33
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
34
import org.gvsig.compat.print.PrintAttributes;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.dynobject.DynStruct;
38
import org.gvsig.tools.persistence.PersistenceManager;
39
import org.gvsig.tools.persistence.PersistentState;
40
import org.gvsig.tools.persistence.exception.PersistenceException;
41
42
/**
43
 * FFrame que contiene a su vez un ArrayList de FFrames de cualquier tipo
44
 * incluso de si mismo.
45
 *
46
 * @author Vicente Caballero Navarro
47
 */
48
public class FFrameGroup extends AbstractFFrameViewDependence implements
49
    IFFrameUseProject, IFFrameViewDependence {
50
51
    public static final String PERSISTENCE_DEFINITION_NAME = "FFrameGroup";
52
53
    private static final String FFRAMES_FIELD = "fframes";
54
55 1634 fdiaz
    private List<IFFrame> m_fframes = new ArrayList<>();
56 5 jldominguez
    private Rectangle2D.Double rg = null;
57
    private AffineTransform m_at;
58
    private Project project;
59
60
    /**
61
     * Crea un nuevo FFrameGroup.
62
     */
63
    public FFrameGroup() {
64
65
    }
66
67
    /**
68
     * A?ade al Arraylist un nuevo FFrame para formar parte del grupo.
69
     *
70
     * @param fframe
71
     *            FFrame a a?adir.
72
     */
73
    public void addFFrame(IFFrame fframe) {
74
        m_fframes.add(fframe);
75
    }
76
77
    /**
78
     * Devuelve una ArrayList que contiene todos los FFrames que forman parte
79
     * del grupo.
80
     *
81
     * @return Arraylist con los fframes.
82
     */
83
    public IFFrame[] getFFrames() {
84
        return m_fframes.toArray(new IFFrame[0]);
85
    }
86
87
    /**
88
     * Devuelve el rect?ngulo que contiene a todos los fframes seleccionados.
89
     *
90
     * @param at
91
     *            Matriz de transformaci?n
92
     *
93
     * @return Rect?ngulo.
94
     */
95
    public Rectangle2D.Double getRectangle(AffineTransform at) {
96
        boolean first = true;
97
        Rectangle2D.Double rec = new Rectangle2D.Double();
98
        IFFrame[] fframes = getFFrames();
99 1634 fdiaz
        for (IFFrame fframe : fframes) {
100
            Rectangle2D.Double rs = fframe.getBoundingBox(at);
101 5 jldominguez
            if (first) {
102
                rec.setRect(rs);
103
                first = false;
104
            }
105
            rec.add(rs);
106
        }
107
108
        rg = new Rectangle2D.Double();
109 1714 fdiaz
        rg.setRect(FLayoutUtilities.toSheetRect(rec, at));
110 5 jldominguez
111
        return rec;
112
    }
113
114
    /**
115
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
116
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
117
     * de dibujar.
118
     *
119
     * @param g
120
     *            Graphics
121
     * @param at
122
     *            Transformada afin.
123
     * @param rv
124
     *            rect?ngulo sobre el que hacer un clip.
125
     * @param imgBase
126
     *            Imagen utilizada para acelerar el dibujado.
127
     */
128 1634 fdiaz
    @Override
129 5 jldominguez
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
130
        BufferedImage imgBase) {
131
        Rectangle2D.Double r = getBoundingBox(at);
132
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y
133
            + (r.height / 2));
134
        m_at = at;
135
136
        for (int i = 0; i < m_fframes.size(); i++) {
137
            m_fframes.get(i).draw(g, at, rv, imgBase);
138
        }
139
140
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
141
            + (r.height / 2));
142
    }
143
144
    /**
145
     * Rellena la transformada que se esta utilizando en el Layout.
146
     *
147
     * @param at
148
     *            Matriz de transformaci?n.
149
     */
150
    public void setAt(AffineTransform at) {
151
        m_at = at;
152
    }
153
154
    /**
155
     * Reimplementaci?n del m?todo papa poder modificar los BoundBox de cada
156
     * uno de los FFrames que contiene dentro este FFrameGroup.
157
     *
158
     * @param r
159
     *            Rect?ngulo.
160
     */
161 1634 fdiaz
    @Override
162 5 jldominguez
    public void setBoundBox(Rectangle2D r) {
163
        getBoundBox().setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
164
165 1634 fdiaz
        double dx; // = 1;
166
        double dy; // = 1;
167
        double dw; // = 1;
168
        double dh; // = 1;
169 5 jldominguez
170
        if (rg != null) {
171
            Rectangle2D.Double raux1 =
172
                new Rectangle2D.Double(rg.x, rg.y, rg.width, rg.height);
173
            dx = r.getX() - raux1.x;
174
            dy = r.getY() - raux1.y;
175
            dw = r.getWidth() / raux1.width;
176
            dh = r.getHeight() / raux1.height;
177
            IFFrame[] fframes = getFFrames();
178 1634 fdiaz
            for (IFFrame fframe : fframes) {
179 5 jldominguez
                Rectangle2D.Double raux = new Rectangle2D.Double();
180
                raux.setRect(fframe.getBoundBox());
181
182
                AffineTransform escalado = new AffineTransform();
183
184
                escalado.setToScale(dw, dh);
185
                escalado.translate(dx - r.getX(), dy - r.getY());
186
187
                Point2D.Double pd = new Point2D.Double();
188
                escalado.transform(new Point2D.Double(raux.x, raux.y), pd);
189
190
                raux.x = pd.x + r.getX();
191
                raux.y = pd.y + r.getY();
192
                raux.width = raux.width * dw;
193
                raux.height = raux.height * dh;
194
195
                fframe.setBoundBox(raux);
196
            }
197
        } else {
198
            rg = new Rectangle2D.Double();
199
            rg.setRect(r);
200
        }
201
202
        rg.setRect(r);
203
    }
204
205
    /**
206
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNameFFrame()
207
     */
208 1634 fdiaz
    @Override
209 5 jldominguez
    public String getNameFFrame() {
210
        return PluginServices.getText(this, "grupo") + num;
211
    }
212
213 1634 fdiaz
    @Override
214 5 jldominguez
    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 1634 fdiaz
    @Override
223 5 jldominguez
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
224
        PrintAttributes printingProperties) {
225
        Rectangle2D.Double r = getBoundingBox(at);
226
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y
227
            + (r.height / 2));
228
        IFFrame[] fframes = m_fframes.toArray(new IFFrame[0]);
229 1634 fdiaz
        for (IFFrame fframe : fframes) {
230
            fframe.print(g, at, geom, printingProperties);
231 5 jldominguez
        }
232
233
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
234
            + (r.height / 2));
235
    }
236
237
    /**
238
     * Inserta una referencia al proyecto nesecario.
239
     *
240
     * @param project
241
     *            DOCUMENT ME!
242
     */
243 1634 fdiaz
    @Override
244 5 jldominguez
    public void setProject(Project project) {
245
        this.project = project;
246
    }
247
248
    public void initialize() {
249
250
    }
251
252
    public void clearFFrames() {
253
        m_fframes.clear();
254
    }
255
256
    public IFFrame removeFFrame(int i) {
257
        return m_fframes.remove(i);
258
    }
259
260
    public void removeFFrame(IFFrame fframe) {
261
        m_fframes.remove(fframe);
262
    }
263
264 1634 fdiaz
    @Override
265 5 jldominguez
    public IFFrame clone() throws CloneNotSupportedException {
266
        FFrameGroup frame = (FFrameGroup) super.clone();
267
        frame.setSelected(this.getSelected() != IFFrame.NOSELECT);
268 1634 fdiaz
        frame.m_fframes = new ArrayList<>();
269 5 jldominguez
270
        for (int i = 0; i < m_fframes.size(); i++) {
271
            frame.addFFrame((IFFrame) m_fframes.get(i).clone());
272
        }
273 1634 fdiaz
        if(m_at != null){
274
            frame.m_at = new AffineTransform(m_at);
275
        } else {
276
            frame.m_at = new AffineTransform();
277
        }
278 1714 fdiaz
        if(rg!=null) {
279
            frame.rg = new Rectangle2D.Double(rg.x, rg.y, rg.width, rg.height);
280
        }
281 5 jldominguez
        return frame;
282
    }
283
284 1634 fdiaz
    @Override
285 5 jldominguez
    public void setFFrameDependence(IFFrame f) {
286
        IFFrame[] frames = getFFrames();
287 1634 fdiaz
        for (IFFrame frame : frames) {
288
            if (frame instanceof IFFrameViewDependence) {
289
                ((IFFrameViewDependence) frame).setFFrameDependence(f);
290 5 jldominguez
            }
291
        }
292
    }
293
294 1634 fdiaz
    @Override
295 5 jldominguez
    public IFFrame[] getFFrameDependence() {
296
        IFFrame[] frames = getFFrames();
297 1634 fdiaz
        ArrayList<IFFrame> dependences = new ArrayList<>();
298 5 jldominguez
        for (int i = 0; i < frames.length; i++) {
299
            if (frames[i] instanceof IFFrameViewDependence) {
300
                IFFrame[] framesAux =
301
                    ((IFFrameViewDependence) frames[i]).getFFrameDependence();
302 1634 fdiaz
                for (IFFrame framesAux1 : framesAux) {
303 5 jldominguez
                    dependences.add(framesAux[i]);
304
                }
305
            }
306
        }
307
        return dependences.toArray(new IFFrame[0]);
308
    }
309
310 1634 fdiaz
    @Override
311 5 jldominguez
    public void refreshDependence(IFFrame fant, IFFrame fnew) {
312
        IFFrame[] frames = getFFrames();
313 1634 fdiaz
        for (IFFrame frame : frames) {
314 5 jldominguez
            if (fnew instanceof FFrameGroup) {
315
                IFFrame[] framesGroupNew = ((FFrameGroup) fnew).getFFrames();
316 1634 fdiaz
                for (IFFrame framesGroupNew1 : framesGroupNew) {
317 5 jldominguez
                    if (fant instanceof FFrameGroup) {
318
                        IFFrame[] framesGroupAnt =
319 1634 fdiaz
                                ((FFrameGroup) fant).getFFrames();
320
                        for (IFFrame framesGroupAnt1 : framesGroupAnt) {
321
                            if (framesGroupAnt1 instanceof IFFrameViewDependence) {
322
                                refreshDependence(framesGroupAnt1, framesGroupNew1);
323 5 jldominguez
                            }
324
                        }
325
                    }
326
                }
327 1634 fdiaz
            } else if (frame instanceof IFFrameViewDependence) {
328
                ((IFFrameViewDependence) frame).refreshDependence(fant, fnew);
329
            }
330 5 jldominguez
        }
331
    }
332
333
    public static void registerPersistent() {
334
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
335
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
336
            DynStruct definition =
337
                manager.addDefinition(FFrameGroup.class,
338
                    PERSISTENCE_DEFINITION_NAME,
339
                    "FFrameGroup persistence definition", null, null);
340
341
            definition
342
                .extend(manager
343
                    .getDefinition(AbstractFFrameViewDependence.PERSISTENCE_DEFINITION_NAME));
344
345
            definition.addDynFieldList(FFRAMES_FIELD)
346
                .setClassOfItems(IFFrame.class).setMandatory(true);
347
        }
348
    }
349
350
    @Override
351
    public void loadFromState(PersistentState state)
352
        throws PersistenceException {
353
        super.loadFromState(state);
354
        m_fframes = (List<IFFrame>) state.getList(FFRAMES_FIELD);
355
    }
356
357
    @Override
358
    public void saveToState(PersistentState state) throws PersistenceException {
359
        super.saveToState(state);
360
        state.set(FFRAMES_FIELD, m_fframes);
361
    }
362
}