Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / layout / fframes / FFrameGroup.java @ 33420

History | View | Annotate | Download (13.4 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 org.gvsig.app.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
import java.util.List;
54

    
55
import org.gvsig.andami.PluginServices;
56
import org.gvsig.app.project.Project;
57
import org.gvsig.app.project.documents.exceptions.OpenException;
58
import org.gvsig.app.project.documents.exceptions.SaveException;
59
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
60
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.FFrameGroupDialog;
61
import org.gvsig.app.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
62
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
63
import org.gvsig.compat.print.PrintAttributes;
64
import org.gvsig.fmap.geom.Geometry;
65
import org.gvsig.tools.ToolsLocator;
66
import org.gvsig.tools.dynobject.DynStruct;
67
import org.gvsig.tools.persistence.PersistenceManager;
68
import org.gvsig.tools.persistence.PersistentState;
69
import org.gvsig.tools.persistence.exception.PersistenceException;
70
import org.gvsig.utils.XMLEntity;
71

    
72

    
73

    
74
/**
75
 * FFrame que contiene a su vez un ArrayList de FFrames de cualquier tipo
76
 * incluso de si mismo.
77
 *
78
 * @author Vicente Caballero Navarro
79
 */
80
public class FFrameGroup extends FFrame implements IFFrameUseProject, IFFrameViewDependence{
81
        public static final String PERSISTENCE_DEFINITION_NAME = "FFrameGroup";
82
        
83
        private static final String FFRAMES_FIELD = "fframes";
84
        
85
        private List<IFFrame> m_fframes = new ArrayList<IFFrame>();
86
    private Rectangle2D.Double rg = null;
87
    private AffineTransform m_at;
88
    private Project project;
89

    
90
    /**
91
     * Crea un nuevo FFrameGroup.
92
     */
93
    public FFrameGroup() {
94
            // do nothing
95
    }
96

    
97
    /**
98
     * A?ade al Arraylist un nuevo FFrame para formar parte del grupo.
99
     *
100
     * @param fframe FFrame a a?adir.
101
     */
102
    public void addFFrame(IFFrame fframe) {
103
        m_fframes.add(fframe);
104
    }
105

    
106
    /**
107
     * Devuelve una ArrayList que contiene todos los FFrames que forman parte
108
     * del grupo.
109
     *
110
     * @return Arraylist con los fframes.
111
     */
112
    public IFFrame[] getFFrames() {
113
        return m_fframes.toArray(new IFFrame[0]);
114
    }
115

    
116
    /**
117
     * Devuelve el rect?ngulo que contiene a todos los fframes seleccionados.
118
     *
119
     * @param at Matriz de transformaci?n
120
     *
121
     * @return Rect?ngulo.
122
     */
123
    public Rectangle2D.Double getRectangle(AffineTransform at) {
124
        boolean first = true;
125
        Rectangle2D.Double rec = new Rectangle2D.Double();
126
        IFFrame[] fframes=getFFrames();
127
        for (int i = 0; i < fframes.length; i++) {
128
            Rectangle2D.Double rs = fframes[i].getBoundingBox(at);
129

    
130
            if (first) {
131
                rec.setRect(rs);
132
                first = false;
133
            }
134

    
135
            rec.add(rs);
136
        }
137

    
138
        rg = new Rectangle2D.Double();
139
        rg.setRect(FLayoutUtilities.toSheetRect(rec, m_at));
140

    
141
        return rec;
142
    }
143

    
144
    /**
145
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
146
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
147
     * de dibujar.
148
     *
149
     * @param g Graphics
150
     * @param at Transformada afin.
151
     * @param rv rect?ngulo sobre el que hacer un clip.
152
     * @param imgBase Imagen utilizada para acelerar el dibujado.
153
     */
154
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
155
        BufferedImage imgBase) {
156
        Rectangle2D.Double r = getBoundingBox(at);
157
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
158
            r.y + (r.height / 2));
159
        m_at = at;
160

    
161
        for (int i = 0; i < m_fframes.size(); i++) {
162
            m_fframes.get(i).draw(g, at, rv, imgBase);
163
        }
164

    
165
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
166
            r.y + (r.height / 2));
167
    }
168

    
169
    /**
170
     * Rellena la transformada que se esta utilizando en el Layout.
171
     *
172
     * @param at Matriz de transformaci?n.
173
     */
174
    public void setAt(AffineTransform at) {
175
        m_at = at;
176
    }
177

    
178
    /**
179
     * Reimplementaci?n del m?todo papa poder modificar los BoundBox  de cada
180
     * uno de los FFrames que contiene dentro este FFrameGroup.
181
     *
182
     * @param r Rect?ngulo.
183
     */
184
    public void setBoundBox(Rectangle2D r) {
185
        getBoundBox().setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
186

    
187
        double dx = 1;
188
        double dy = 1;
189
        double dw = 1;
190
        double dh = 1;
191

    
192
        if (rg != null) {
193
            Rectangle2D.Double raux1 = new Rectangle2D.Double(rg.x, rg.y,
194
                    rg.width, rg.height);
195
            dx = r.getX() - raux1.x;
196
            dy = r.getY() - raux1.y;
197
            dw = r.getWidth() / raux1.width;
198
            dh = r.getHeight() / raux1.height;
199
            IFFrame[] fframes=getFFrames();
200
            for (int i = 0; i < fframes.length; i++) {
201
                IFFrame fframe = fframes[i];
202
                Rectangle2D.Double raux = new Rectangle2D.Double();
203
                raux.setRect(fframe.getBoundBox());
204

    
205
                AffineTransform escalado = new AffineTransform();
206

    
207
                escalado.setToScale(dw, dh);
208
                escalado.translate(dx - r.getX(), dy - r.getY());
209

    
210
                Point2D.Double pd = new Point2D.Double();
211
                escalado.transform(new Point2D.Double(raux.x, raux.y), pd);
212

    
213
                raux.x = pd.x + r.getX();
214
                raux.y = pd.y + r.getY();
215
                raux.width = raux.width * dw;
216
                raux.height = raux.height * dh;
217

    
218
                fframe.setBoundBox(raux);
219
            }
220
        } else {
221
            rg = new Rectangle2D.Double();
222
            rg.setRect(r);
223
        }
224

    
225
        rg.setRect(r);
226
    }
227

    
228
    /**
229
     * DOCUMENT ME!
230
     *
231
     * @return DOCUMENT ME!
232
     *
233
     * @throws SaveException
234
     *
235
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getXMLEntity()
236
     */
237
    public XMLEntity getXMLEntity() throws SaveException {
238
        XMLEntity xml = super.getXMLEntity();
239
//        xml.putProperty("type", Layout.RECTANGLEGROUP);
240
        IFFrame[] fframes=getFFrames();
241
        for (int i = 0; i < fframes.length; i++) {
242
            xml.addChild(fframes[i].getXMLEntity());
243
        }
244

    
245
        return xml;
246
    }
247

    
248
    /**
249
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#setXMLEntity(org.gvsig.utils.XMLEntity)
250
     */
251
    public void setXMLEntity(XMLEntity xml) {
252
        if (xml.getIntProperty("m_Selected") != 0) {
253
            this.setSelected(true);
254
        } else {
255
            this.setSelected(false);
256
        }
257

    
258
        setRotation(xml.getDoubleProperty("m_rotation"));
259

    
260
        for (int i = 0; i < xml.getChildrenCount(); i++) {
261
            try {
262
                IFFrame frame = FFrame.createFromXML(xml.getChild(i), project,getLayout());
263
                this.addFFrame(frame);
264
            } catch (OpenException e) {
265
                e.showError();
266
            }
267
        }
268
    }
269

    
270
    /**
271
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNameFFrame()
272
     */
273
    public String getNameFFrame() {
274
        return PluginServices.getText(this, "grupo")+ num;
275
    }
276

    
277
    /**
278
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#print(java.awt.Graphics2D,
279
     *      java.awt.geom.AffineTransform)
280
     */
281
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
282
                        PrintAttributes printingProperties) {
283
        Rectangle2D.Double r = getBoundingBox(at);
284
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
285
            r.y + (r.height / 2));
286
        IFFrame[] fframes=m_fframes.toArray(new IFFrame[0]);
287
        for (int i = 0; i < fframes.length; i++) {
288
//            fframes[i].setPrintingProperties(printingProperties);
289
                fframes[i].print(g, at, geom, printingProperties);
290
//                fframes[i].setPrintingProperties(null);
291
        }
292

    
293
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
294
            r.y + (r.height / 2));
295
    }
296

    
297
    /**
298
     * Inserta una referencia al proyecto nesecario.
299
     *
300
     * @param project DOCUMENT ME!
301
     */
302
    public void setProject(Project project) {
303
        this.project = project;
304
    }
305

    
306
    /**
307
     * DOCUMENT ME!
308
     *
309
     * @param layout DOCUMENT ME!
310
     */
311
    public void setLayout(LayoutPanel layout) {
312
        super.setLayout(layout);
313
            IFFrame[] fsoriginal= layout.getLayoutContext().getAllFFrames();
314
        IFFrame[] fs = getFFrames();
315

    
316
        for (int i = 0; i < fs.length; i++) {
317
                fs[i].setLayout(layout);
318

    
319
            if (fs[i] instanceof IFFrameViewDependence) {
320
                ((IFFrameViewDependence) fs[i]).initDependence(fsoriginal);
321
            }
322
        }
323
    }
324

    
325
        public void initialize() {
326
                // TODO Auto-generated method stub
327

    
328
        }
329
        public void clearFFrames(){
330
                m_fframes.clear();
331
        }
332
        public IFFrame removeFFrame(int i){
333
                return m_fframes.remove(i);
334
        }
335
        public void removeFFrame(IFFrame fframe){
336
                m_fframes.remove(fframe);
337
        }
338
        public void cloneActions(IFFrame frame) {
339
                // TODO Auto-generated method stub
340
        }
341
        public IFFrame cloneFFrame(LayoutPanel layout) {
342
                FFrameGroup frame =(FFrameGroup)FrameFactory.createFrameFromName(FFrameGroupFactory.registerName);
343
                frame.setSelected(this.getSelected()!=IFFrame.NOSELECT);
344
                frame.setLevel(this.getLevel());
345
            frame.setNum(this.num);
346
            frame.setName(this.getName());
347
            frame.setBoundBox(this.getBoundBox());
348
            frame.setTag(this.getTag());
349
            frame.setRotation(this.getRotation());
350
            frame.setLayout(layout);
351
            frame.m_at=m_at;
352
            for(int i=0;i<m_fframes.size();i++) {
353
                    frame.addFFrame(m_fframes.get(i).cloneFFrame(layout));
354
            }
355
            return frame;
356
        }
357
        public IFFrameDialog getPropertyDialog() {
358
                return new FFrameGroupDialog(getLayout(),this);
359
        }
360

    
361
        public void setFFrameDependence(IFFrame f) {
362
                IFFrame[] frames=getFFrames();
363
                for (int i =0;i<frames.length;i++){
364
                        if (frames[i] instanceof IFFrameViewDependence){
365
                                ((IFFrameViewDependence)frames[i]).setFFrameDependence(f);
366
                        }
367
                }
368

    
369
        }
370

    
371
        public IFFrame[] getFFrameDependence() {
372
                IFFrame[] frames=getFFrames();
373
                ArrayList<IFFrame> dependences=new ArrayList<IFFrame>();
374
                for (int i =0;i<frames.length;i++){
375
                        if (frames[i] instanceof IFFrameViewDependence){
376
                                IFFrame[] framesAux=((IFFrameViewDependence)frames[i]).getFFrameDependence();
377
                                        for (int j =0;j<framesAux.length;j++){
378
                                                dependences.add(framesAux[i]);
379
                                        }
380
                        }
381
                }
382
                return dependences.toArray(new IFFrame[0]);
383
        }
384

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

    
394
        public void refreshDependence(IFFrame fant, IFFrame fnew) {
395
                IFFrame[] frames=getFFrames();
396
                for (int i =0;i<frames.length;i++){
397

    
398
                        if (fnew instanceof FFrameGroup){
399
                                IFFrame[] framesGroupNew=((FFrameGroup)fnew).getFFrames();
400
                                for (int j=0;j<framesGroupNew.length;j++){
401
                                        if (fant instanceof FFrameGroup){
402
                                                IFFrame[] framesGroupAnt=((FFrameGroup)fant).getFFrames();
403
                                                for (int k=0;k<framesGroupAnt.length;k++){
404
                                                        if (framesGroupAnt[k] instanceof IFFrameViewDependence){
405
                                                                refreshDependence(framesGroupAnt[k],framesGroupNew[j]);
406
                                                        }
407
                                                }
408
                                        }
409
                                }
410
                        }else if (frames[i] instanceof IFFrameViewDependence){
411
                                ((IFFrameViewDependence)frames[i]).refreshDependence(fant,fnew);
412
                        }
413
                }
414
        }
415
        
416
        public static void registerPersistent() {
417
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
418
                if( manager.getDefinition(PERSISTENCE_DEFINITION_NAME)==null ) {
419
                        DynStruct definition = manager.addDefinition(
420
                                        FFrameGroup.class,
421
                                        PERSISTENCE_DEFINITION_NAME,
422
                                        "FFrameGroup persistence definition",
423
                                        null, 
424
                                        null
425
                        ); 
426
                        
427
                        definition.extend(manager.getDefinition(FFrame.PERSISTENCE_DEFINITION_NAME));        
428
                        
429
                        definition.addDynFieldList(FFRAMES_FIELD).setClassOfItems(IFFrame.class).setMandatory(true);                
430
                }
431
        }
432

    
433
        @Override
434
        public void loadFromState(PersistentState state)
435
                        throws PersistenceException {
436
                super.loadFromState(state);
437
                m_fframes = (List<IFFrame>)state.getList(FFRAMES_FIELD);                
438
        }
439

    
440
        @Override
441
        public void saveToState(PersistentState state) throws PersistenceException {
442
        super.saveToState(state);
443
                state.set(FFRAMES_FIELD, m_fframes);        
444
        }
445
}