Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrameGraphics.java @ 3590

History | View | Annotate | Download (14.6 KB)

1
/*
2
 * Created on 22-jun-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 java.awt.Color;
48
import java.awt.Graphics2D;
49
import java.awt.geom.AffineTransform;
50
import java.awt.geom.Point2D;
51
import java.awt.geom.Rectangle2D;
52
import java.awt.image.BufferedImage;
53
import java.util.BitSet;
54

    
55
import com.iver.andami.PluginServices;
56
import com.iver.cit.gvsig.fmap.DriverException;
57
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
58
import com.iver.cit.gvsig.fmap.core.FShape;
59
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
60
import com.iver.cit.gvsig.fmap.core.adapter.CircleAdapter;
61
import com.iver.cit.gvsig.fmap.core.adapter.GeometryAdapter;
62
import com.iver.cit.gvsig.fmap.core.adapter.PointAdapter;
63
import com.iver.cit.gvsig.fmap.core.adapter.PolyLineAdapter;
64
import com.iver.cit.gvsig.fmap.core.adapter.PolygonAdapter;
65
import com.iver.cit.gvsig.fmap.core.adapter.RectangleAdapter;
66
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
67
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
68
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
69
import com.iver.cit.gvsig.gui.layout.FLayoutUtilities;
70
import com.iver.cit.gvsig.gui.layout.Layout;
71
import com.iver.cit.gvsig.gui.project.SaveException;
72
import com.iver.utiles.XMLEntity;
73

    
74

    
75
/**
76
 * FFrame para contener un gr?fico.
77
 *
78
 * @author Vicente Caballero Navarro
79
 */
80
public class FFrameGraphics extends FFrame implements IFFrameEditable{
81
        private static double TOL=0.1;
82
        private int m_type = FConstant.SHAPE_TYPE_POINT;
83
    private FSymbol m_symbol = null;
84
    private Color m_color = null;
85
    private AffineTransform mT = null;
86
    private GeometryAdapter geometry;
87
    private GeometryAdapter geometryEdit;
88
    private boolean editing=false;
89
    private double size=0.5;
90
    private BitSet index=new BitSet();
91
       
92
    public FFrameGraphics(){
93
             mT = new AffineTransform();
94
         mT.setToIdentity();
95
    }
96
    public void setGeometryAdapter(GeometryAdapter geom){
97
            geometry=geom;
98
    }
99
  
100

    
101
    /**
102
     * Rellena el color que se utlizar? para dibujar el s?mbolo.
103
     *
104
     * @param color
105
     */
106
    public void setColor(Color color) {
107
        m_color = color;
108
    }
109

    
110
    /**
111
     * Actualiza el Fsymbol a partir del tipo de Gr?fico que se pase como
112
     * par?metro.
113
     *
114
     * @param type tipo de gr?fico.
115
     * @param at Transformada.
116
     */
117
    public void update(int type, AffineTransform at) {
118
        m_type = type;
119

    
120
        //aT = at;
121
        if (m_color == null) {
122
            m_color = Color.red;
123
        }
124

    
125
        switch (m_type) {
126
            case (Layout.POINT):
127
                m_symbol = new FSymbol(FConstant.SHAPE_TYPE_POINT, m_color);
128
                    m_symbol.setSize((int)FLayoutUtilities.fromSheetDistance(size,at));
129
                break;
130

    
131
            case (Layout.RECTANGLESIMPLE):
132
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
133
                m_symbol.setColor(null);
134

    
135
                break;
136

    
137
            case (Layout.LINE):
138
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE, m_color);
139
                    
140
                break;
141

    
142
            case (Layout.POLYLINE):
143
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE, m_color);
144

    
145
                break;
146

    
147
            case (Layout.POLYGON):
148
                    m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
149
                    m_symbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
150
                    m_symbol.setOutlined(true);
151
                    m_symbol.setOutlineColor(Color.red);
152
                    m_symbol.setColor(null);
153

    
154
                break;
155

    
156
            case (Layout.CIRCLE):
157
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
158
                m_symbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
159
                m_symbol.setOutlined(true);
160
                m_symbol.setOutlineColor(Color.red);
161
                m_symbol.setColor(null);
162

    
163
                break;
164
        }
165
    }
166

    
167
    /**
168
     * Devuelve el FSymbol que se representa.
169
     *
170
     * @return DOCUMENT ME!
171
     */
172
    public FSymbol getFSymbol() {
173
        return m_symbol;
174
    }
175

    
176
    /**
177
     * Rellena el FSymbol que se representara al dibujar.
178
     *
179
     * @param symbol
180
     */
181
    public void setFSymbol(FSymbol symbol) {
182
        m_symbol = symbol;
183
    }
184

    
185
    /**
186
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
187
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
188
     * de dibujar.
189
     *
190
     * @param g Graphics
191
     * @param at Transformada afin.
192
     * @param rv rect?ngulo sobre el que hacer un clip.
193
     * @param imgBase DOCUMENT ME!
194
     */
195
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
196
        BufferedImage imgBase) {
197
        Rectangle2D.Double rect = getBoundingBox(at);
198
        g.rotate(Math.toRadians(getRotation()), rect.x + (rect.width / 2),
199
            rect.y + (rect.height / 2));
200

    
201
        if (intersects(rv, rect)) {
202
            g.setColor(Color.black);
203
           if (m_type==Layout.POINT){
204
                          m_symbol.setSize((int)(rect.getWidth()*0.7));
205
           }
206
           geometry.draw(g,at,m_symbol);
207
           if (editing){
208
                           geometry.drawVertex(g,at);
209
           }
210
        }
211

    
212
        g.rotate(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
213
            rect.y + (rect.height / 2));
214
    }
215

    
216
    /**
217
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
218
     */
219
    public XMLEntity getXMLEntity() throws SaveException{
220
            XMLEntity xml = new XMLEntity();
221
            try{
222
            xml.putProperty("className", this.getClass().getName());
223
        xml.putProperty("m_name", m_name);
224
        xml.putProperty("x", getBoundBox().x);
225
        xml.putProperty("y", getBoundBox().y);
226
        xml.putProperty("w", getBoundBox().width);
227
        xml.putProperty("h", getBoundBox().height);
228
        xml.putProperty("m_Selected", m_Selected);
229
        xml.putProperty("type", Layout.GRAPHICS);
230
        xml.putProperty("m_type", m_type);
231
        xml.putProperty("tag", getTag());
232
        xml.putProperty("m_rotation", getRotation());
233
        xml.addChild(m_symbol.getXMLEntity());
234
        xml.addChild(geometry.getXMLEntity());
235
            }catch (Exception e) {
236
                        throw new SaveException(e,this.getClass().getName());
237
                }
238
        return xml;
239
    }
240

    
241
    /**
242
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#updateNum()
243
     */
244
    public void updateNum() {
245
    }
246

    
247
    /**
248
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNum()
249
     */
250
    public int getNum() {
251
        return 0;
252
    }
253

    
254
    /**
255
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
256
     *      com.iver.cit.gvsig.project.Project)
257
     */
258
    public void setXMLEntity(XMLEntity xml) {
259
        m_Selected = xml.getIntProperty("m_Selected");
260
        m_type = xml.getIntProperty("m_type");
261
        setRotation(xml.getDoubleProperty("m_rotation"));
262
        m_symbol = FSymbol.createFromXML(xml.getChild(0));
263
        if (xml.getChild(1)==null){
264
                Rectangle2D r=(Rectangle2D)getBoundBox().clone();
265
            if (m_type == Layout.LINE) {
266
                            geometry=new PolyLineAdapter();
267
            } else if (m_type == Layout.RECTANGLESIMPLE) {
268
                    geometry=new RectangleAdapter();
269
                    geometry.addPoint(new Point2D.Double(r.getX(),r.getY()));
270
                    geometry.addPoint(new Point2D.Double(r.getMaxX(),r.getMaxY()));
271
            } else if (m_type == Layout.POLYLINE) {
272
                geometry=new PolyLineAdapter();
273
            } else if (m_type == Layout.POLYGON) {
274
                geometry=new PolygonAdapter();
275
            } else if (m_type == Layout.CIRCLE) {
276
                    geometry=new CircleAdapter();
277
                    geometry.addPoint(new Point2D.Double(r.getCenterX(),r.getCenterY()));
278
                    if (r.getWidth()<r.getHeight()){
279
                    geometry.addPoint(new Point2D.Double(r.getMaxX(),r.getCenterY()));
280
                    }else{
281
                            geometry.addPoint(new Point2D.Double(r.getCenterX(),r.getY()));
282
                    }
283
            } else if (m_type == Layout.POINT) {
284
                    geometry=new PointAdapter();
285
                    geometry.addPoint(new Point2D.Double(r.getCenterX(),r.getCenterY()));
286
                    
287
            } 
288
            geometry.end();
289
        }else{
290
        geometry=GeometryAdapter.createFromXML(xml.getChild(1));
291
        }
292
    }
293

    
294
    /**
295
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
296
     *      com.iver.cit.gvsig.project.Project)
297
     */
298
     public void setXMLEntity03(XMLEntity xml, Layout p) {
299
        m_Selected = xml.getIntProperty("m_Selected");
300
        m_type = xml.getIntProperty("m_type");
301
        m_symbol = FSymbol.createFromXML03(xml.getChild(0));
302
        Rectangle2D r=(Rectangle2D)getBoundBox().clone();
303
        if (m_type == Layout.LINE) {
304
                        geometry=new PolyLineAdapter();
305
        } else if (m_type == Layout.RECTANGLESIMPLE) {
306
                geometry=new RectangleAdapter();
307
                geometry.addPoint(new Point2D.Double(r.getX(),r.getY()));
308
                geometry.addPoint(new Point2D.Double(r.getMaxX(),r.getMaxY()));
309
        } else if (m_type == Layout.POLYLINE) {
310
            geometry=new PolyLineAdapter();
311
        } else if (m_type == Layout.POLYGON) {
312
            geometry=new PolygonAdapter();
313
        } else if (m_type == Layout.CIRCLE) {
314
                geometry=new CircleAdapter();
315
                geometry.addPoint(new Point2D.Double(r.getCenterX(),r.getCenterY()));
316
                if (r.getWidth()<r.getHeight()){
317
                geometry.addPoint(new Point2D.Double(r.getMaxX(),r.getCenterY()));
318
                }else{
319
                        geometry.addPoint(new Point2D.Double(r.getCenterX(),r.getY()));
320
                }
321
        } else if (m_type == Layout.POINT) {
322
                geometry=new PointAdapter();
323
                geometry.addPoint(new Point2D.Double(r.getCenterX(),r.getCenterY()));
324
                
325
        } 
326
        geometry.end();
327
    }
328

    
329
    /**
330
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
331
     */
332
    public String getNameFFrame() {
333
        return PluginServices.getText(this, "Gr?ficos") + num;
334
    }
335

    
336
    /**
337
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
338
     *      java.awt.geom.AffineTransform)
339
     */
340
    public void print(Graphics2D g, AffineTransform at)
341
        throws DriverException {
342
        draw(g, at, null, null);
343
    }
344

    
345
    /**
346
     * Inserta el tama?o del punto.
347
     *
348
     * @param size entero que representa el tama?o del punto.
349
     */
350
    public void setSize(double size) {
351
            this.size=size;
352
            Rectangle2D r=geometry.getShape().getBounds2D();
353
            super.setBoundBox(new Rectangle2D.Double(r.getX()-size,r.getY()-size,size*2,size*2));
354
    }
355

    
356
    /**
357
     * Devuelve el tipo de gr?fico que contiene el fframe.
358
     *
359
     * @return tipo de
360
     */
361
    public int getType() {
362
        return m_type;
363
    }
364
    public void setBoundBox(Rectangle2D r) {
365
            AffineTransform aT=new AffineTransform();
366
            if (getBoundBox().getWidth() != 0){
367
                    double w=r.getWidth()/getBoundBox().getWidth();
368
                    double h=r.getHeight()/getBoundBox().getHeight();
369
                    AffineTransform trans2=AffineTransform.getTranslateInstance(r.getX(),r.getY());
370
                    aT.concatenate(trans2);
371
                    AffineTransform scale1=AffineTransform.getScaleInstance(w,h);
372
                    aT.concatenate(scale1);
373
                    AffineTransform trans1=AffineTransform.getTranslateInstance(-getBoundBox().getX(),-getBoundBox().getY());
374
                    aT.concatenate(trans1);
375
                    geometry.applyTransform(aT);
376
                    size=aT.getScaleX()*size;
377
                }
378
            super.setBoundBox(r);
379
        
380
    }
381
        
382
        public void startEditing() {
383
                editing=true;
384
        }
385
        public void stopEditing() {
386
                editing=false;        
387
        }
388
        public boolean isEditing() {
389
                return editing;
390
        }
391
        public void pointReleased(Point2D point,GeometryAdapter geom) {
392
                index.clear();
393
                geometry=geom;
394
                Rectangle2D r=geometry.getShape().getBounds2D();
395
                super.setBoundBox(r);
396
        }
397

    
398
        public void pointPressed(Point2D point) {
399
                Rectangle2D.Double rect = getBoundBox();
400
                Point2D[] points=geometry.getPoints();
401
                geometryEdit=GeometryAdapter.createFromXML(geometry.getXMLEntity());
402
                Point2D pAux1=new Point2D.Double();
403
                for (int i=0;i<points.length;i++){
404
                        if (getRotation()!=0){
405
                                AffineTransform af=AffineTransform.getRotateInstance(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
406
                                    rect.y + (rect.height / 2));
407
                                af.transform(point,pAux1);
408
                                if (points[i].distance(pAux1)<=TOL){
409
                                        index.set(i);
410
                                }
411
                        }else{
412
                                if (points[i].distance(point)<=TOL){
413
                                        index.set(i);
414
                                }
415
                        }
416
                }
417
        }
418

    
419
        public void pointDragged(Point2D point) {
420
                Point2D[] points=geometry.getPoints();
421
                for (int j = index.nextSetBit(0);
422
                j >= 0;
423
                j = index.nextSetBit(j + 1)){
424
                        if (getRotation()!=0){
425
                                Rectangle2D.Double rect=getBoundBox();
426
                                AffineTransform af=AffineTransform.getRotateInstance(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
427
                                    rect.y + (rect.height / 2));
428
                                af.transform(point,point);
429
                        }
430
                        points[j]=new Point2D.Double(point.getX(),point.getY());
431
                }
432
                geometryEdit.setPoints(points);
433
                geometryEdit.end();
434
        }
435
        public void paint(Graphics2D g, AffineTransform at) {
436
                if (geometryEdit!=null){
437
                Rectangle2D.Double rect=getBoundingBox(at);
438
                g.rotate(Math.toRadians(getRotation()), rect.x + (rect.width / 2),
439
                    rect.y + (rect.height / 2));
440
                FShape m_shape = null;
441
                GeneralPathX polyLine=new GeneralPathX(geometryEdit.getShape());
442
            polyLine.transform(at);
443
            m_shape = new FPolyline2D(polyLine);
444
            FGraphicUtilities.DrawShape(g, mT,m_shape, m_symbol);
445
            
446
            g.rotate(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
447
                            rect.y + (rect.height / 2));
448
                }
449
        }
450
        public GeometryAdapter getGeometry() {
451
                return geometryEdit;
452
        }
453
                
454
}