Statistics
| Revision:

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

History | View | Annotate | Download (16.1 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 com.iver.andami.PluginServices;
48

    
49
import com.iver.cit.gvsig.fmap.DriverException;
50
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
51
import com.iver.cit.gvsig.fmap.core.FShape;
52
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
53
import com.iver.cit.gvsig.fmap.core.adapter.CircleAdapter;
54
import com.iver.cit.gvsig.fmap.core.adapter.GeometryAdapter;
55
import com.iver.cit.gvsig.fmap.core.adapter.PointAdapter;
56
import com.iver.cit.gvsig.fmap.core.adapter.PolyLineAdapter;
57
import com.iver.cit.gvsig.fmap.core.adapter.PolygonAdapter;
58
import com.iver.cit.gvsig.fmap.core.adapter.RectangleAdapter;
59
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
60
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
61
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
62
import com.iver.cit.gvsig.gui.layout.FLayoutUtilities;
63
import com.iver.cit.gvsig.gui.layout.Layout;
64
import com.iver.cit.gvsig.gui.project.SaveException;
65

    
66
import com.iver.utiles.XMLEntity;
67

    
68
import java.awt.Color;
69
import java.awt.Graphics2D;
70
import java.awt.geom.AffineTransform;
71
import java.awt.geom.Point2D;
72
import java.awt.geom.Rectangle2D;
73
import java.awt.image.BufferedImage;
74

    
75
import java.util.BitSet;
76

    
77

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

    
95
    /**
96
     * Crea un nuevo FFrameGraphics.
97
     */
98
    public FFrameGraphics() {
99
        mT = new AffineTransform();
100
        mT.setToIdentity();
101
    }
102

    
103
    /**
104
     * DOCUMENT ME!
105
     *
106
     * @param geom DOCUMENT ME!
107
     */
108
    public void setGeometryAdapter(GeometryAdapter geom) {
109
        geometry = geom;
110
    }
111

    
112
    /**
113
     * Rellena el color que se utlizar? para dibujar el s?mbolo.
114
     *
115
     * @param color
116
     */
117
    public void setColor(Color color) {
118
        m_color = color;
119
    }
120

    
121
    /**
122
     * Actualiza el Fsymbol a partir del tipo de Gr?fico que se pase como
123
     * par?metro.
124
     *
125
     * @param type tipo de gr?fico.
126
     * @param at Transformada.
127
     */
128
    public void update(int type, AffineTransform at) {
129
        m_type = type;
130

    
131
        //aT = at;
132
        if (m_color == null) {
133
            m_color = Color.red;
134
        }
135

    
136
        switch (m_type) {
137
            case (Layout.POINT):
138
                m_symbol = new FSymbol(FConstant.SHAPE_TYPE_POINT, m_color);
139
                m_symbol.setSize((int) FLayoutUtilities.fromSheetDistance(
140
                        size, at));
141

    
142
                break;
143

    
144
            case (Layout.RECTANGLESIMPLE):
145
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
146
                m_symbol.setColor(null);
147

    
148
                break;
149

    
150
            case (Layout.LINE):
151
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE, m_color);
152

    
153
                break;
154

    
155
            case (Layout.POLYLINE):
156
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE, m_color);
157

    
158
                break;
159

    
160
            case (Layout.POLYGON):
161
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
162
                m_symbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
163
                m_symbol.setOutlined(true);
164
                m_symbol.setOutlineColor(Color.red);
165
                m_symbol.setColor(null);
166

    
167
                break;
168

    
169
            case (Layout.CIRCLE):
170
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
171
                m_symbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
172
                m_symbol.setOutlined(true);
173
                m_symbol.setOutlineColor(Color.red);
174
                m_symbol.setColor(null);
175

    
176
                break;
177
        }
178
    }
179

    
180
    /**
181
     * Devuelve el FSymbol que se representa.
182
     *
183
     * @return DOCUMENT ME!
184
     */
185
    public FSymbol getFSymbol() {
186
        return m_symbol;
187
    }
188

    
189
    /**
190
     * Rellena el FSymbol que se representara al dibujar.
191
     *
192
     * @param symbol
193
     */
194
    public void setFSymbol(FSymbol symbol) {
195
        m_symbol = symbol;
196
    }
197

    
198
    /**
199
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
200
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
201
     * de dibujar.
202
     *
203
     * @param g Graphics
204
     * @param at Transformada afin.
205
     * @param rv rect?ngulo sobre el que hacer un clip.
206
     * @param imgBase DOCUMENT ME!
207
     */
208
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
209
        BufferedImage imgBase) {
210
        Rectangle2D.Double rect = getBoundingBox(at);
211
        g.rotate(Math.toRadians(getRotation()), rect.x + (rect.width / 2),
212
            rect.y + (rect.height / 2));
213

    
214
        if (intersects(rv, rect)) {
215
            g.setColor(Color.black);
216

    
217
            if (m_type == Layout.POINT) {
218
                m_symbol.setSize((int) (rect.getWidth() * 0.7));
219
            }
220

    
221
            geometry.draw(g, at, m_symbol);
222

    
223
            if (editing) {
224
                geometry.drawVertex(g, at);
225
            }
226
        }
227

    
228
        g.rotate(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
229
            rect.y + (rect.height / 2));
230
    }
231

    
232
    /**
233
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
234
     */
235
    public XMLEntity getXMLEntity() throws SaveException {
236
        XMLEntity xml = super.getXMLEntity();
237

    
238
        try {
239
            xml.putProperty("type", Layout.GRAPHICS);
240
            xml.putProperty("m_type", m_type);
241
            xml.addChild(m_symbol.getXMLEntity());
242
            xml.addChild(geometry.getXMLEntity());
243
        } catch (Exception e) {
244
            throw new SaveException(e, this.getClass().getName());
245
        }
246

    
247
        return xml;
248
    }
249

    
250
    /**
251
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#updateNum()
252
     */
253
    public void updateNum() {
254
    }
255

    
256
    /**
257
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNum()
258
     */
259
    public int getNum() {
260
        return 0;
261
    }
262

    
263
    /**
264
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
265
     *      com.iver.cit.gvsig.project.Project)
266
     */
267
    public void setXMLEntity(XMLEntity xml) {
268
        m_Selected = xml.getIntProperty("m_Selected");
269
        m_type = xml.getIntProperty("m_type");
270
        setRotation(xml.getDoubleProperty("m_rotation"));
271
        m_symbol = FSymbol.createFromXML(xml.getChild(0));
272

    
273
        if (xml.getChild(1) == null) {
274
            Rectangle2D r = (Rectangle2D) getBoundBox().clone();
275

    
276
            if (m_type == Layout.LINE) {
277
                geometry = new PolyLineAdapter();
278
            } else if (m_type == Layout.RECTANGLESIMPLE) {
279
                geometry = new RectangleAdapter();
280
                geometry.addPoint(new Point2D.Double(r.getX(), r.getY()));
281
                geometry.addPoint(new Point2D.Double(r.getMaxX(), r.getMaxY()));
282
            } else if (m_type == Layout.POLYLINE) {
283
                geometry = new PolyLineAdapter();
284
            } else if (m_type == Layout.POLYGON) {
285
                geometry = new PolygonAdapter();
286
            } else if (m_type == Layout.CIRCLE) {
287
                geometry = new CircleAdapter();
288
                geometry.addPoint(new Point2D.Double(r.getCenterX(),
289
                        r.getCenterY()));
290

    
291
                if (r.getWidth() < r.getHeight()) {
292
                    geometry.addPoint(new Point2D.Double(r.getMaxX(),
293
                            r.getCenterY()));
294
                } else {
295
                    geometry.addPoint(new Point2D.Double(r.getCenterX(),
296
                            r.getY()));
297
                }
298
            } else if (m_type == Layout.POINT) {
299
                geometry = new PointAdapter();
300
                geometry.addPoint(new Point2D.Double(r.getCenterX(),
301
                        r.getCenterY()));
302
            }
303

    
304
            geometry.end();
305
        } else {
306
            geometry = GeometryAdapter.createFromXML(xml.getChild(1));
307
        }
308
    }
309

    
310
    /**
311
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
312
     *      com.iver.cit.gvsig.project.Project)
313
     */
314
    public void setXMLEntity03(XMLEntity xml, Layout p) {
315
        m_Selected = xml.getIntProperty("m_Selected");
316
        m_type = xml.getIntProperty("m_type");
317
        m_symbol = FSymbol.createFromXML03(xml.getChild(0));
318

    
319
        Rectangle2D r = (Rectangle2D) getBoundBox().clone();
320

    
321
        if (m_type == Layout.LINE) {
322
            geometry = new PolyLineAdapter();
323
        } else if (m_type == Layout.RECTANGLESIMPLE) {
324
            geometry = new RectangleAdapter();
325
            geometry.addPoint(new Point2D.Double(r.getX(), r.getY()));
326
            geometry.addPoint(new Point2D.Double(r.getMaxX(), r.getMaxY()));
327
        } else if (m_type == Layout.POLYLINE) {
328
            geometry = new PolyLineAdapter();
329
        } else if (m_type == Layout.POLYGON) {
330
            geometry = new PolygonAdapter();
331
        } else if (m_type == Layout.CIRCLE) {
332
            geometry = new CircleAdapter();
333
            geometry.addPoint(new Point2D.Double(r.getCenterX(), r.getCenterY()));
334

    
335
            if (r.getWidth() < r.getHeight()) {
336
                geometry.addPoint(new Point2D.Double(r.getMaxX(), r.getCenterY()));
337
            } else {
338
                geometry.addPoint(new Point2D.Double(r.getCenterX(), r.getY()));
339
            }
340
        } else if (m_type == Layout.POINT) {
341
            geometry = new PointAdapter();
342
            geometry.addPoint(new Point2D.Double(r.getCenterX(), r.getCenterY()));
343
        }
344

    
345
        geometry.end();
346
    }
347

    
348
    /**
349
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
350
     */
351
    public String getNameFFrame() {
352
        return PluginServices.getText(this, "Gr?ficos") + num;
353
    }
354

    
355
    /**
356
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
357
     *      java.awt.geom.AffineTransform)
358
     */
359
    public void print(Graphics2D g, AffineTransform at)
360
        throws DriverException {
361
        draw(g, at, null, null);
362
    }
363

    
364
    /**
365
     * Inserta el tama?o del punto.
366
     *
367
     * @param size entero que representa el tama?o del punto.
368
     */
369
    public void setSize(double size) {
370
        this.size = size;
371

    
372
        Rectangle2D r = geometry.getShape().getBounds2D();
373
        super.setBoundBox(new Rectangle2D.Double(r.getX() - size,
374
                r.getY() - size, size * 2, size * 2));
375
    }
376

    
377
    /**
378
     * Devuelve el tipo de gr?fico que contiene el fframe.
379
     *
380
     * @return tipo de
381
     */
382
    public int getType() {
383
        return m_type;
384
    }
385

    
386
    /**
387
     * DOCUMENT ME!
388
     *
389
     * @param r DOCUMENT ME!
390
     */
391
    public void setBoundBox(Rectangle2D r) {
392
        AffineTransform aT = new AffineTransform();
393

    
394
        if (getBoundBox().getWidth() != 0) {
395
            double w = r.getWidth() / getBoundBox().getWidth();
396
            double h = r.getHeight() / getBoundBox().getHeight();
397
            AffineTransform trans2 = AffineTransform.getTranslateInstance(r.getX(),
398
                    r.getY());
399
            aT.concatenate(trans2);
400

    
401
            AffineTransform scale1 = AffineTransform.getScaleInstance(w, h);
402
            aT.concatenate(scale1);
403

    
404
            AffineTransform trans1 = AffineTransform.getTranslateInstance(-getBoundBox()
405
                                                                               .getX(),
406
                    -getBoundBox().getY());
407
            aT.concatenate(trans1);
408
            geometry.applyTransform(aT);
409
            size = aT.getScaleX() * size;
410
        }
411

    
412
        super.setBoundBox(r);
413
    }
414

    
415
    /**
416
     * DOCUMENT ME!
417
     */
418
    public void startEditing() {
419
        editing = true;
420
    }
421

    
422
    /**
423
     * DOCUMENT ME!
424
     */
425
    public void stopEditing() {
426
        editing = false;
427
    }
428

    
429
    /**
430
     * DOCUMENT ME!
431
     *
432
     * @return DOCUMENT ME!
433
     */
434
    public boolean isEditing() {
435
        return editing;
436
    }
437

    
438
    /**
439
     * DOCUMENT ME!
440
     *
441
     * @param point DOCUMENT ME!
442
     * @param geom DOCUMENT ME!
443
     */
444
    public void pointReleased(Point2D point, GeometryAdapter geom) {
445
        index.clear();
446
        geometry = geom;
447

    
448
        Rectangle2D r = geometry.getShape().getBounds2D();
449
        super.setBoundBox(r);
450
    }
451

    
452
    /**
453
     * DOCUMENT ME!
454
     *
455
     * @param point DOCUMENT ME!
456
     */
457
    public void pointPressed(Point2D point) {
458
        Rectangle2D.Double rect = getBoundBox();
459
        Point2D[] points = geometry.getPoints();
460
        geometryEdit = GeometryAdapter.createFromXML(geometry.getXMLEntity());
461

    
462
        Point2D pAux1 = new Point2D.Double();
463

    
464
        for (int i = 0; i < points.length; i++) {
465
            if (getRotation() != 0) {
466
                AffineTransform af = AffineTransform.getRotateInstance(Math.toRadians(
467
                            -getRotation()), rect.x + (rect.width / 2),
468
                        rect.y + (rect.height / 2));
469
                af.transform(point, pAux1);
470

    
471
                if (points[i].distance(pAux1) <= TOL) {
472
                    index.set(i);
473
                }
474
            } else {
475
                if (points[i].distance(point) <= TOL) {
476
                    index.set(i);
477
                }
478
            }
479
        }
480
    }
481

    
482
    /**
483
     * DOCUMENT ME!
484
     *
485
     * @param point DOCUMENT ME!
486
     */
487
    public void pointDragged(Point2D point) {
488
        Point2D[] points = geometry.getPoints();
489

    
490
        for (int j = index.nextSetBit(0); j >= 0;
491
                j = index.nextSetBit(j + 1)) {
492
            if (getRotation() != 0) {
493
                Rectangle2D.Double rect = getBoundBox();
494
                AffineTransform af = AffineTransform.getRotateInstance(Math.toRadians(
495
                            -getRotation()), rect.x + (rect.width / 2),
496
                        rect.y + (rect.height / 2));
497
                af.transform(point, point);
498
            }
499

    
500
            points[j] = new Point2D.Double(point.getX(), point.getY());
501
        }
502

    
503
        geometryEdit.setPoints(points);
504
        geometryEdit.end();
505
    }
506

    
507
    /**
508
     * DOCUMENT ME!
509
     *
510
     * @param g DOCUMENT ME!
511
     * @param at DOCUMENT ME!
512
     */
513
    public void paint(Graphics2D g, AffineTransform at) {
514
        if (geometryEdit != null) {
515
            Rectangle2D.Double rect = getBoundingBox(at);
516
            g.rotate(Math.toRadians(getRotation()), rect.x + (rect.width / 2),
517
                rect.y + (rect.height / 2));
518

    
519
            FShape m_shape = null;
520
            GeneralPathX polyLine = new GeneralPathX(geometryEdit.getShape());
521
            polyLine.transform(at);
522
            m_shape = new FPolyline2D(polyLine);
523
            FGraphicUtilities.DrawShape(g, mT, m_shape, m_symbol);
524

    
525
            g.rotate(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
526
                rect.y + (rect.height / 2));
527
        }
528
    }
529

    
530
    /**
531
     * DOCUMENT ME!
532
     *
533
     * @return DOCUMENT ME!
534
     */
535
    public GeometryAdapter getGeometry() {
536
        return geometryEdit;
537
    }
538
}