Statistics
| Revision:

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

History | View | Annotate | Download (16.2 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.adapter.CircleAdapter;
58
import com.iver.cit.gvsig.fmap.core.adapter.GeometryAdapter;
59
import com.iver.cit.gvsig.fmap.core.adapter.PointAdapter;
60
import com.iver.cit.gvsig.fmap.core.adapter.PolyLineAdapter;
61
import com.iver.cit.gvsig.fmap.core.adapter.PolygonAdapter;
62
import com.iver.cit.gvsig.fmap.core.adapter.RectangleAdapter;
63
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
64
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
65
import com.iver.cit.gvsig.gui.layout.FLayoutUtilities;
66
import com.iver.cit.gvsig.gui.layout.Layout;
67
import com.iver.cit.gvsig.gui.project.SaveException;
68
import com.iver.utiles.XMLEntity;
69

    
70

    
71
/**
72
 * FFrame para contener un gr?fico.
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class FFrameGraphics extends FFrame implements IFFrameEditable {
77
    private static double TOL = 0.2;
78
    private int m_type = FConstant.SHAPE_TYPE_POINT;
79
    private FSymbol m_symbol = null;
80
    private Color m_color = null;
81
    private AffineTransform mT = null;
82
    private GeometryAdapter geometry;
83
    private GeometryAdapter geometryEdit;
84
    private boolean editing = false;
85
    private double size = 0.5;
86
    private BitSet index = new BitSet();
87

    
88
    /**
89
     * Crea un nuevo FFrameGraphics.
90
     */
91
    public FFrameGraphics() {
92
        mT = new AffineTransform();
93
        mT.setToIdentity();
94
    }
95

    
96
    /**
97
     * DOCUMENT ME!
98
     *
99
     * @param geom DOCUMENT ME!
100
     */
101
    public void setGeometryAdapter(GeometryAdapter geom) {
102
        geometry = geom;
103
    }
104

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

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

    
124
        //aT = at;
125
        if (m_color == null) {
126
            m_color = Color.black;
127
        }
128

    
129
        switch (m_type) {
130
            case (Layout.POINT):
131
                m_symbol = new FSymbol(FConstant.SHAPE_TYPE_POINT, m_color);
132
                m_symbol.setSize((int) FLayoutUtilities.fromSheetDistance(
133
                        size, at));
134

    
135
                break;
136

    
137
            case (Layout.RECTANGLESIMPLE):
138
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
139
                m_symbol.setColor(null);
140

    
141
                break;
142

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

    
146
                break;
147

    
148
            case (Layout.POLYLINE):
149
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE, m_color);
150

    
151
                break;
152

    
153
            case (Layout.POLYGON):
154
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
155
                m_symbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
156
                m_symbol.setOutlined(true);
157
                m_symbol.setOutlineColor(m_color);
158
                m_symbol.setColor(null);
159

    
160
                break;
161

    
162
            case (Layout.CIRCLE):
163
                m_symbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
164
                m_symbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
165
                m_symbol.setOutlined(true);
166
                m_symbol.setOutlineColor(m_color);
167
                m_symbol.setColor(null);
168

    
169
                break;
170
        }
171
    }
172

    
173
    /**
174
     * Devuelve el FSymbol que se representa.
175
     *
176
     * @return DOCUMENT ME!
177
     */
178
    public FSymbol getFSymbol() {
179
        return m_symbol;
180
    }
181

    
182
    /**
183
     * Rellena el FSymbol que se representara al dibujar.
184
     *
185
     * @param symbol
186
     */
187
    public void setFSymbol(FSymbol symbol) {
188
        m_symbol = symbol;
189
    }
190

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

    
207
        if (intersects(rv, rect)) {
208
            g.setColor(Color.black);
209

    
210
            if (m_type == Layout.POINT) {
211
                m_symbol.setSize((int) (rect.getWidth() * 0.7));
212
            }
213

    
214
            geometry.draw(g, at, m_symbol);
215

    
216
            if (editing) {
217
                g.setColor(Color.red);
218
                    geometry.drawVertex(g, at);
219
            }
220
        }
221

    
222
        g.rotate(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
223
            rect.y + (rect.height / 2));
224
    }
225

    
226
    /**
227
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
228
     */
229
    public XMLEntity getXMLEntity() throws SaveException {
230
        XMLEntity xml = super.getXMLEntity();
231

    
232
        try {
233
            xml.putProperty("type", Layout.GRAPHICS);
234
            xml.putProperty("m_type", m_type);
235
            xml.addChild(m_symbol.getXMLEntity());
236
            xml.addChild(geometry.getXMLEntity());
237
        } catch (Exception e) {
238
            throw new SaveException(e, this.getClass().getName());
239
        }
240

    
241
        return xml;
242
    }
243

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

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

    
257
    /**
258
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
259
     *      com.iver.cit.gvsig.project.Project)
260
     */
261
    public void setXMLEntity(XMLEntity xml) {
262
        m_Selected = xml.getIntProperty("m_Selected");
263
        m_type = xml.getIntProperty("m_type");
264
        setRotation(xml.getDoubleProperty("m_rotation"));
265
        m_symbol = FSymbol.createFromXML(xml.getChild(0));
266

    
267
        if (xml.getChild(1) == null) {
268
            Rectangle2D r = (Rectangle2D) getBoundBox().clone();
269

    
270
            if (m_type == Layout.LINE) {
271
                geometry = new PolyLineAdapter();
272
            } else if (m_type == Layout.RECTANGLESIMPLE) {
273
                geometry = new RectangleAdapter();
274
                geometry.addPoint(new Point2D.Double(r.getX(), r.getY()));
275
                geometry.addPoint(new Point2D.Double(r.getMaxX(), r.getMaxY()));
276
            } else if (m_type == Layout.POLYLINE) {
277
                geometry = new PolyLineAdapter();
278
            } else if (m_type == Layout.POLYGON) {
279
                geometry = new PolygonAdapter();
280
            } else if (m_type == Layout.CIRCLE) {
281
                geometry = new CircleAdapter();
282
                geometry.addPoint(new Point2D.Double(r.getCenterX(),
283
                        r.getCenterY()));
284

    
285
                if (r.getWidth() < r.getHeight()) {
286
                    geometry.addPoint(new Point2D.Double(r.getMaxX(),
287
                            r.getCenterY()));
288
                } else {
289
                    geometry.addPoint(new Point2D.Double(r.getCenterX(),
290
                            r.getY()));
291
                }
292
            } else if (m_type == Layout.POINT) {
293
                geometry = new PointAdapter();
294
                geometry.addPoint(new Point2D.Double(r.getCenterX(),
295
                        r.getCenterY()));
296
            }
297

    
298
            geometry.end();
299
        } else {
300
            geometry = GeometryAdapter.createFromXML(xml.getChild(1));
301
        }
302
    }
303

    
304
    /**
305
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
306
     *      com.iver.cit.gvsig.project.Project)
307
     */
308
    public void setXMLEntity03(XMLEntity xml, Layout p) {
309
        m_Selected = xml.getIntProperty("m_Selected");
310
        m_type = xml.getIntProperty("m_type");
311
        m_symbol = FSymbol.createFromXML03(xml.getChild(0));
312

    
313
        Rectangle2D r = (Rectangle2D) getBoundBox().clone();
314

    
315
        if (m_type == Layout.LINE) {
316
            geometry = new PolyLineAdapter();
317
        } else if (m_type == Layout.RECTANGLESIMPLE) {
318
            geometry = new RectangleAdapter();
319
            geometry.addPoint(new Point2D.Double(r.getX(), r.getY()));
320
            geometry.addPoint(new Point2D.Double(r.getMaxX(), r.getMaxY()));
321
        } else if (m_type == Layout.POLYLINE) {
322
            geometry = new PolyLineAdapter();
323
        } else if (m_type == Layout.POLYGON) {
324
            geometry = new PolygonAdapter();
325
        } else if (m_type == Layout.CIRCLE) {
326
            geometry = new CircleAdapter();
327
            geometry.addPoint(new Point2D.Double(r.getCenterX(), r.getCenterY()));
328

    
329
            if (r.getWidth() < r.getHeight()) {
330
                geometry.addPoint(new Point2D.Double(r.getMaxX(), r.getCenterY()));
331
            } else {
332
                geometry.addPoint(new Point2D.Double(r.getCenterX(), r.getY()));
333
            }
334
        } else if (m_type == Layout.POINT) {
335
            geometry = new PointAdapter();
336
            geometry.addPoint(new Point2D.Double(r.getCenterX(), r.getCenterY()));
337
        }
338

    
339
        geometry.end();
340
    }
341

    
342
    /**
343
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
344
     */
345
    public String getNameFFrame() {
346
        return PluginServices.getText(this, "Graficos") + num;
347
    }
348

    
349
    /**
350
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
351
     *      java.awt.geom.AffineTransform)
352
     */
353
    public void print(Graphics2D g, AffineTransform at)
354
        throws DriverException {
355
        draw(g, at, null, null);
356
    }
357

    
358
    /**
359
     * Inserta el tama?o del punto.
360
     *
361
     * @param size entero que representa el tama?o del punto.
362
     */
363
    public void setSize(double size) {
364
        this.size = size;
365

    
366
        Rectangle2D r = geometry.getBounds2D();
367
        super.setBoundBox(new Rectangle2D.Double(r.getX() - size,
368
                r.getY() - size, size * 2, size * 2));
369
    }
370

    
371
    /**
372
     * Devuelve el tipo de gr?fico que contiene el fframe.
373
     *
374
     * @return tipo de
375
     */
376
    public int getType() {
377
        return m_type;
378
    }
379

    
380
    /**
381
     * DOCUMENT ME!
382
     *
383
     * @param r DOCUMENT ME!
384
     */
385
    public void setBoundBox(Rectangle2D r) {
386
        AffineTransform aT = new AffineTransform();
387

    
388
        if (getBoundBox().getWidth() != 0) {
389
            double w = r.getWidth() / getBoundBox().getWidth();
390
            double h = r.getHeight() / getBoundBox().getHeight();
391
            AffineTransform trans2 = AffineTransform.getTranslateInstance(r.getX(),
392
                    r.getY());
393
            aT.concatenate(trans2);
394

    
395
            AffineTransform scale1 = AffineTransform.getScaleInstance(w, h);
396
            aT.concatenate(scale1);
397

    
398
            AffineTransform trans1 = AffineTransform.getTranslateInstance(-getBoundBox()
399
                                                                               .getX(),
400
                    -getBoundBox().getY());
401
            aT.concatenate(trans1);
402
            geometry.applyTransform(aT);
403
            size = aT.getScaleX() * size;
404
        }
405

    
406
        super.setBoundBox(r);
407
    }
408

    
409
    /**
410
     * DOCUMENT ME!
411
     */
412
    public void startEditing() {
413
        editing = true;
414
    }
415

    
416
    /**
417
     * DOCUMENT ME!
418
     */
419
    public void stopEditing() {
420
        editing = false;
421
    }
422

    
423
    /**
424
     * DOCUMENT ME!
425
     *
426
     * @return DOCUMENT ME!
427
     */
428
    public boolean isEditing() {
429
        return editing;
430
    }
431

    
432
    /**
433
     * DOCUMENT ME!
434
     *
435
     * @param point DOCUMENT ME!
436
     * @param geom DOCUMENT ME!
437
     */
438
    public void pointReleased(Point2D point, GeometryAdapter geom) {
439
        index.clear();
440
        geometry = geom;
441

    
442
        Rectangle2D r = geometry.getBounds2D();
443
        super.setBoundBox(r);
444
    }
445

    
446
    /**
447
     * DOCUMENT ME!
448
     *
449
     * @param point DOCUMENT ME!
450
     */
451
    public void pointPressed(Point2D point) {
452
        Rectangle2D.Double rect = getBoundBox();
453
        Point2D[] points = geometry.getPoints();
454
        geometryEdit = geometry.cloneAdapter();//GeometryAdapter.createFromXML(geometry.getXMLEntity());
455

    
456
        Point2D pAux1 = new Point2D.Double();
457

    
458
        for (int i = 0; i < points.length; i++) {
459
            if (getRotation() != 0) {
460
                AffineTransform af = AffineTransform.getRotateInstance(Math.toRadians(
461
                            -getRotation()), rect.x + (rect.width / 2),
462
                        rect.y + (rect.height / 2));
463
                af.transform(point, pAux1);
464

    
465
                if (points[i].distance(pAux1) <= TOL) {
466
                    index.set(i);
467
                }
468
            } else {
469
                if (points[i].distance(point) <= TOL) {
470
                    index.set(i);
471
                }
472
            }
473
        }
474
    }
475

    
476
    /**
477
     * DOCUMENT ME!
478
     *
479
     * @param point DOCUMENT ME!
480
     */
481
    public void pointDragged(Point2D point) {
482
        Point2D[] points = geometry.getPoints();
483

    
484
        for (int j = index.nextSetBit(0); j >= 0;
485
                j = index.nextSetBit(j + 1)) {
486
            if (getRotation() != 0) {
487
                Rectangle2D.Double rect = getBoundBox();
488
                AffineTransform af = AffineTransform.getRotateInstance(Math.toRadians(
489
                            -getRotation()), rect.x + (rect.width / 2),
490
                        rect.y + (rect.height / 2));
491
                af.transform(point, point);
492
            }
493

    
494
            points[j] = new Point2D.Double(point.getX(), point.getY());
495
            geometryEdit.changePoint(j,points[j]);
496
        }
497
        //geometryEdit.setPoints(points);
498
        geometryEdit.end();
499
    }
500

    
501
    /**
502
     * DOCUMENT ME!
503
     *
504
     * @param g DOCUMENT ME!
505
     * @param at DOCUMENT ME!
506
     */
507
    public void paint(Graphics2D g, AffineTransform at) {
508
        if (geometryEdit != null) {
509
            Rectangle2D.Double rect = getBoundingBox(at);
510
            if (getRotation()!=0){
511
            g.rotate(Math.toRadians(getRotation()), rect.x + (rect.width / 2),
512
                rect.y + (rect.height / 2));
513
            geometryEdit.paint(g,at,false);
514
          /*  FShape m_shape = null;
515
            GeneralPathX polyLine = new GeneralPathX(geometryEdit.getShape());
516
            polyLine.transform(at);
517
            m_shape = new FPolyline2D(polyLine);
518
            FGraphicUtilities.DrawShape(g, mT, m_shape, m_symbol);
519
*/
520
            g.rotate(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
521
                rect.y + (rect.height / 2));
522
            }else{
523
                     geometryEdit.paint(g,at,false);
524
            }
525
        }
526
    }
527

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

    
537
        public void initialize() {
538
                // TODO Auto-generated method stub
539

    
540
        }
541
}