Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / layout / fframes / FFrameGraphics.java @ 33647

History | View | Annotate | Download (19.7 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 org.gvsig.app.project.documents.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 org.gvsig.andami.PluginServices;
56
import org.gvsig.app.project.documents.layout.geometryadapters.CircleAdapter;
57
import org.gvsig.app.project.documents.layout.geometryadapters.GeometryAdapter;
58
import org.gvsig.compat.print.PrintAttributes;
59
import org.gvsig.fmap.geom.Geometry;
60
import org.gvsig.fmap.mapcontext.MapContextLocator;
61
import org.gvsig.fmap.mapcontext.MapContextManager;
62
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
63
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
64
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
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

    
71

    
72

    
73
/**
74
 * FFrame para contener un gr?fico.
75
 *
76
 * @author Vicente Caballero Navarro
77
 */
78
public class FFrameGraphics extends FFrame implements IFFrameEditableVertex {
79
        public static final String PERSISTENCE_DEFINITION_NAME = "FFrameGraphics";
80
        
81
        private static final String TYPE_FIELD = "type";
82
        private static final String SYMBOL_FIELD = "symbol";
83
        private static final String GEOMETRYADAPTER_FIELD = "geometryAdapter";
84
        
85
        private static double TOL = 0.2;
86
    public static final int POINT = 21;
87
    public static final int LINE = 22;
88
    public static final int POLYLINE = 23;
89
    public static final int RECTANGLE = 24;
90
    public static final int CIRCLE = 25;
91
    public static final int POLYGON = 26;
92
    public static final int RECTANGLEGROUP = 16;
93
    public static final int RECTANGLESYMBOL = 17;
94
    protected int m_type = POINT;
95
    private ISymbol m_symbol = null;
96
    private Color m_color = null;
97
    private GeometryAdapter geometryAdapter;
98
    private GeometryAdapter geometryEdit;
99
    private boolean editing = false;
100
    private double size = 0.5;
101
    private BitSet index = new BitSet();
102
    
103
    private MapContextManager mapContextManager = MapContextLocator
104
                        .getMapContextManager();
105

    
106
    /**
107
     * Crea un nuevo FFrameGraphics.
108
     */
109
    public FFrameGraphics() {
110
    }
111

    
112
    /**
113
     * DOCUMENT ME!
114
     *
115
     * @param geom DOCUMENT ME!
116
     */
117
    public void setGeometryAdapter(GeometryAdapter geom) {
118
        geometryAdapter = geom;
119
    }
120

    
121
    /**
122
     * Rellena el color que se utlizar? para dibujar el s?mbolo.
123
     *
124
     * @param color
125
     */
126
    public void setColor(Color color) {
127
        m_color = color;
128
    }
129

    
130
    /**
131
     * Actualiza el Fsymbol a partir del tipo de Gr?fico que se pase como
132
     * par?metro.
133
     *
134
     * @param type tipo de gr?fico.
135
     * @param at Transformada.
136
     */
137
    public void update(int type, AffineTransform at) {
138
        m_type = type;
139

    
140
        //aT = at;
141
        if (m_color == null) {
142
            m_color = Color.black;
143
        }
144
        int shapeType = -1;
145
        /*
146
         * (jaume) moved to ISymbol
147
         * switch (m_type) {
148
            case (POINT):
149

150
                    FSymbol symbolPoint= new FSymbol(FConstant.SHAPE_TYPE_POINT, m_color);
151
                       symbolPoint.setSize((int) FLayoutUtilities.fromSheetDistance(
152
                        size, at));
153
                       m_symbol=symbolPoint;
154
                break;
155

156
            case (RECTANGLE):
157
                    FSymbol symbolRectangle= new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
158
                symbolRectangle.setColor(null);
159
                m_symbol=symbolRectangle;
160
                break;
161

162
            case (LINE):
163
                    FSymbol symbolLine= new FSymbol(FConstant.SYMBOL_TYPE_LINE, m_color);
164
                    m_symbol=symbolLine;
165
                break;
166

167
            case (POLYLINE):
168
                    FSymbol symbolPolyline= new FSymbol(FConstant.SYMBOL_TYPE_LINE, m_color);
169
                    m_symbol=symbolPolyline;
170
                break;
171

172
            case (POLYGON):
173
                    FSymbol symbolPolygon= new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
174
                symbolPolygon.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
175
                symbolPolygon.setOutlined(true);
176
                symbolPolygon.setOutlineColor(m_color);
177
                symbolPolygon.setColor(null);
178
                m_symbol=symbolPolygon;
179
                break;
180

181
            case (CIRCLE):
182
                    FSymbol symbolCircle= new FSymbol(FConstant.SYMBOL_TYPE_FILL, m_color);
183
                symbolCircle.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
184
                symbolCircle.setOutlined(true);
185
                symbolCircle.setOutlineColor(m_color);
186
                symbolCircle.setColor(null);
187
                m_symbol=symbolCircle;
188
                break;
189
        }
190
        */
191
        switch (m_type) {
192
        case (POINT):
193
                shapeType = Geometry.TYPES.POINT;
194
            break;
195

    
196
        case (RECTANGLE):
197
        case (POLYGON):
198
        case (CIRCLE):
199
                shapeType = Geometry.TYPES.SURFACE;
200
            break;
201

    
202
        case (LINE):
203
        case (POLYLINE):
204
                shapeType = Geometry.TYPES.CURVE;
205
            break;
206
        }
207

    
208
                m_symbol = mapContextManager.getSymbolManager().createSymbol(shapeType);
209
        if (shapeType==Geometry.TYPES.SURFACE){
210
                        ((IFillSymbol) m_symbol).setFillColor(null);
211
        }
212
    }
213

    
214
    /**
215
     * Devuelve el FSymbol que se representa.
216
     *
217
     * @return DOCUMENT ME!
218
     */
219
    public ISymbol getFSymbol() {
220
        return m_symbol;
221
    }
222

    
223
    /**
224
     * Rellena el FSymbol que se representara al dibujar.
225
     *
226
     * @param fs2d
227
     */
228
    public void setSymbol(ISymbol symbol) {
229
        m_symbol = symbol;
230
    }
231

    
232
    /**
233
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
234
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
235
     * de dibujar.
236
     *
237
     * @param g Graphics
238
     * @param at Transformada afin.
239
     * @param rv rect?ngulo sobre el que hacer un clip.
240
     * @param imgBase DOCUMENT ME!
241
     */
242
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
243
        BufferedImage imgBase) {
244
        Rectangle2D.Double rect = getBoundingBox(at);
245
        g.rotate(Math.toRadians(getRotation()), rect.x + (rect.width / 2),
246
            rect.y + (rect.height / 2));
247
        if (intersects(rv, rect)) {
248
            g.setColor(Color.black);
249

    
250
            if (m_type == POINT) {
251
                ((IMarkerSymbol)m_symbol).setSize((int) (rect.getWidth() * 0.7));
252
            }
253
//            float stroke=0;
254
//            if (((FSymbol)m_symbol).getStroke() != null) {
255
//                    stroke=((BasicStroke)((FSymbol)m_symbol).getStroke()).getLineWidth();
256
//                BasicStroke basicStroke = new BasicStroke((float)FLayoutUtilities.fromSheetDistance(stroke,at)/100);
257
//                ((FSymbol)m_symbol).setStroke(basicStroke);
258
//            }
259
            geometryAdapter.draw(g, at, m_symbol);
260

    
261
//            if (((FSymbol)m_symbol).getStroke() != null) {
262
//                BasicStroke basicStroke = new BasicStroke(stroke);
263
//                ((FSymbol)m_symbol).setStroke(basicStroke);
264
//            }
265

    
266
            if (editing) {
267
                g.setColor(Color.red);
268
                geometryAdapter.drawVertex(g, at);
269
            }
270
        }
271

    
272
        g.rotate(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
273
            rect.y + (rect.height / 2));
274
    }
275

    
276
    /**
277
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#updateNum()
278
     */
279
    public void updateNum() {
280
    }
281

    
282
    /**
283
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNum()
284
     */
285
    public int getNum() {
286
        return 0;
287
    }
288
   
289
    /**
290
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNameFFrame()
291
     */
292
    public String getNameFFrame() {
293
        return PluginServices.getText(this, "Graficos")+ num;
294
    }
295
    
296
    public String getName() {
297
        return PERSISTENCE_DEFINITION_NAME;
298
    }
299

    
300
    /**
301
     * Inserta el tama?o del punto.
302
     *
303
     * @param size entero que representa el tama?o del punto.
304
     */
305
    public void setSize(double size) {
306
        this.size = size;
307
        if (m_type==RECTANGLESYMBOL) {
308
            return;
309
        }
310
        Rectangle2D r = geometryAdapter.getBounds2D();
311
        super.setBoundBox(new Rectangle2D.Double(r.getX() - size,
312
                r.getY() - size, size * 2, size * 2));
313
    }
314

    
315
    /**
316
     * Devuelve el tipo de gr?fico que contiene el fframe.
317
     *
318
     * @return tipo de
319
     */
320
    public int getType() {
321
        return m_type;
322
    }
323
    public int getShapeType(){
324
            int shapeType=0;
325
            switch (m_type) {
326
        case (POINT):
327
                shapeType = Geometry.TYPES.POINT;
328
            break;
329

    
330
        case (RECTANGLE):
331
        case (POLYGON):
332
        case (CIRCLE):
333
                shapeType = Geometry.TYPES.SURFACE;
334
            break;
335

    
336
        case (LINE):
337
        case (POLYLINE):
338
                shapeType = Geometry.TYPES.CURVE;
339
            break;
340
        }
341
            return shapeType;
342
    }
343
    public void setType(int type) {
344
        m_type = type;
345
    }
346
    /**
347
     * DOCUMENT ME!
348
     *
349
     * @param r DOCUMENT ME!
350
     */
351
    public void setBoundBox(Rectangle2D r) {
352
        if (m_type==RECTANGLESYMBOL) {
353
            super.setBoundBox(r);
354
            return;
355
        }
356
        AffineTransform aT = new AffineTransform();
357

    
358
        if (getBoundBox().getWidth() != 0) {
359
            double w = r.getWidth() / getBoundBox().getWidth();
360
            double h = r.getHeight() / getBoundBox().getHeight();
361

    
362
            AffineTransform trans2 = AffineTransform.getTranslateInstance(r.getX(),
363
                    r.getY());
364
            aT.concatenate(trans2);
365

    
366
            AffineTransform scale1 = AffineTransform.getScaleInstance(w, h);
367
            aT.concatenate(scale1);
368

    
369
            AffineTransform trans1 = AffineTransform.getTranslateInstance(-getBoundBox()
370
                                                                               .getX(),
371
                    -getBoundBox().getY());
372
            aT.concatenate(trans1);
373
            geometryAdapter.applyTransform(aT);
374

    
375
            size = aT.getScaleX() * size;
376
        }
377

    
378
        super.setBoundBox(r);
379
    }
380

    
381
    /**
382
     * DOCUMENT ME!
383
     */
384
    public void startEditing() {
385
        editing = true;
386
    }
387

    
388
    /**
389
     * DOCUMENT ME!
390
     */
391
    public void stopEditing() {
392
        editing = false;
393
    }
394

    
395
    /**
396
     * DOCUMENT ME!
397
     *
398
     * @return DOCUMENT ME!
399
     */
400
    public boolean isEditing() {
401
        return editing;
402
    }
403

    
404
    /**
405
     * DOCUMENT ME!
406
     *
407
     * @param point DOCUMENT ME!
408
     * @param geom DOCUMENT ME!
409
     */
410
    public void pointReleased(Point2D point, GeometryAdapter geom) {
411
        index.clear();
412
        geometryAdapter = geom;
413

    
414
        Rectangle2D r = geometryAdapter.getBounds2D();
415
        super.setBoundBox(r);
416
    }
417

    
418
    /**
419
     * DOCUMENT ME!
420
     *
421
     * @param point DOCUMENT ME!
422
     */
423
    public void pointPressed(Point2D point) {
424
        Rectangle2D.Double rect = getBoundBox();
425
        Point2D[] points = geometryAdapter.getPoints();
426
        try {
427
            geometryEdit = (GeometryAdapter)geometryAdapter.clone();
428
        } catch (CloneNotSupportedException e) {
429
           LOG.error("Error clonning the geometry", e);
430
        }
431
        
432
        Point2D pAux1 = new Point2D.Double();
433
        index.clear();
434
        for (int i = 0; i < points.length; i++) {
435
            if (getRotation() != 0) {
436
                AffineTransform af = AffineTransform.getRotateInstance(Math.toRadians(
437
                            -getRotation()), rect.x + (rect.width / 2),
438
                        rect.y + (rect.height / 2));
439
                af.transform(point, pAux1);
440

    
441
                if (points[i].distance(pAux1) <= TOL) {
442
                    index.set(i);
443
                }
444
            } else {
445
                if (points[i].distance(point) <= TOL) {
446
                    index.set(i);
447
                }
448
            }
449
        }
450
    }
451

    
452
    /**
453
     * DOCUMENT ME!
454
     *
455
     * @param point DOCUMENT ME!
456
     */
457
    public void pointDragged(Point2D point) {
458
        //Point2D[] points = geometry.getPoints();
459

    
460
        for (int j = index.nextSetBit(0); j >= 0;
461
                j = index.nextSetBit(j + 1)) {
462
            if (getRotation() != 0) {
463
                Rectangle2D.Double rect = getBoundBox();
464
                AffineTransform af = AffineTransform.getRotateInstance(Math.toRadians(
465
                            -getRotation()), rect.x + (rect.width / 2),
466
                        rect.y + (rect.height / 2));
467
                af.transform(point, point);
468
            }
469

    
470
            //points[j] = new Point2D.Double(point.getX(), point.getY());
471
            geometryEdit.changePoint(j,point);
472
        }
473
        //geometryEdit.setPoints(points);
474
        geometryEdit.end();
475
    }
476

    
477
    /**
478
     * DOCUMENT ME!
479
     *
480
     * @param g DOCUMENT ME!
481
     * @param at DOCUMENT ME!
482
     */
483
    public void paint(Graphics2D g, AffineTransform at) {
484
        if (geometryEdit != null) {
485
            Rectangle2D.Double rect = getBoundingBox(at);
486
            if (getRotation()!=0){
487
            g.rotate(Math.toRadians(getRotation()), rect.x + (rect.width / 2),
488
                rect.y + (rect.height / 2));
489
            geometryEdit.paint(g,at,false);
490
          /*  FShape m_shape = null;
491
            GeneralPathX polyLine = new GeneralPathX(geometryEdit.getShape());
492
            polyLine.transform(at);
493
            m_shape = new FPolyline2D(polyLine);
494
            FGraphicUtilities.DrawShape(g, mT, m_shape, m_symbol);
495
*/
496
            g.rotate(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
497
                rect.y + (rect.height / 2));
498
            }else{
499
                 geometryEdit.paint(g,at,false);
500
            }
501
        }
502
    }
503

    
504
    /**
505
     * DOCUMENT ME!
506
     *
507
     * @return DOCUMENT ME!
508
     */
509
    public GeometryAdapter getGeometry() {
510
        return geometryEdit;
511
    }
512

    
513
    public void initialize() {
514
        // TODO Auto-generated method stub
515

    
516
    }
517
     /**
518
     * Devuelve un entero que representa donde esta contenido el punto que se
519
     * pasa como par?metro.
520
     *
521
     * @param p punto a comparar.
522
     *
523
     * @return entero que representa como esta contenido el punto.
524
     */
525
    public int getContains(Point2D p) {
526
        if (geometryAdapter instanceof CircleAdapter) {
527
            if (ne.contains(p.getX(), p.getY())) {
528
                return NE;
529
            } else if (se.contains(p.getX(), p.getY())) {
530
                return SE;
531
            } else if (so.contains(p.getX(), p.getY())) {
532
                return SO;
533
            } else if (no.contains(p.getX(), p.getY())) {
534
                return NO;
535
            } else if (getBoundingBox(null).contains(p.getX(), p.getY())) {
536
                return RECT;
537
            }
538
            return NOSELECT;
539
        }
540
            return super.getContains(p);
541
    }
542

    
543
    /**
544
     * Dibuja los handlers sobre el boundingBox en el graphics que se pasa como
545
     * par?metro.
546
     *
547
     * @param g
548
     *            Graphics sobre el que dibujar.
549
     */
550
    public void drawHandlers(Graphics2D g) {
551
        if (geometryAdapter instanceof CircleAdapter) {
552
            int size = 10;
553
            Rectangle2D r = getBoundingBox(null);
554
            Point2D p = new Point2D.Double();
555
            g.rotate(Math.toRadians(getRotation()), r.getX()
556
                    + (r.getWidth() / 2), r.getY() + (r.getHeight() / 2));
557

    
558
            AffineTransform atRotate = new AffineTransform();
559
            atRotate.rotate(Math.toRadians(getRotation()), r.getX()
560
                    + (r.getWidth() / 2), r.getY() + (r.getHeight() / 2));
561

    
562
            g.fillRect((int) r.getX() - size, (int) r.getY() - size,
563
                            size, size);
564
            atRotate.transform(new Point2D.Double(r.getX() - size, r.getY()
565
                    - size), p);
566
            no.setRect((int) p.getX(), (int) p.getY(), size, size);
567

    
568
            g.fillRect((int) r.getMaxX(), (int) r.getY() - size, size, size);
569
            atRotate.transform(
570
                    new Point2D.Double(r.getMaxX(), r.getY() - size), p);
571
            ne.setRect((int) p.getX(), (int) p.getY(), size, size);
572

    
573
            g.fillRect((int) r.getX() - size, (int) r.getMaxY(), size, size);
574
            atRotate.transform(
575
                    new Point2D.Double(r.getX() - size, r.getMaxY()), p);
576
            so.setRect((int) p.getX(), (int) p.getY(), size, size);
577

    
578
            g.fillRect((int) r.getMaxX(), (int) r.getMaxY(), size, size);
579
            atRotate.transform(new Point2D.Double(r.getMaxX(), r.getMaxY()), p);
580
            se.setRect((int) p.getX(), (int) p.getY(), size, size);
581

    
582
            g.rotate(Math.toRadians(-getRotation()), r.getX()
583
                    + (r.getWidth() / 2), r.getY() + (r.getHeight() / 2));
584
        } else {
585
                        super.drawHandlers(g);
586
                }
587
    }
588

    
589
        public void print(Graphics2D g, AffineTransform at, Geometry geom,
590
                        PrintAttributes printingProperties) {
591
                Rectangle2D.Double rect = getBoundingBox(at);
592
                g.rotate(Math.toRadians(getRotation()), rect.x + (rect.width / 2),
593
                                rect.y + (rect.height / 2));
594
                g.setColor(Color.black);
595

    
596
//                if (m_type == POINT) {
597
//                        ((IMarkerSymbol)m_symbol).setSize((int) (rect.getWidth() * 0.7));
598
//                }
599
                geometryAdapter.print(g, at, m_symbol,printingProperties);
600

    
601

    
602
                if (editing) {
603
                        g.setColor(Color.red);
604
                        geometryAdapter.drawVertex(g, at);
605
                }
606

    
607
                g.rotate(Math.toRadians(-getRotation()), rect.x + (rect.width / 2),
608
                                rect.y + (rect.height / 2));
609
        }
610
        
611
        public static void registerPersistent() {
612
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
613
                if( manager.getDefinition(PERSISTENCE_DEFINITION_NAME)==null ) {
614
                        DynStruct definition = manager.addDefinition(
615
                                        FFrameGraphics.class,
616
                                        PERSISTENCE_DEFINITION_NAME,
617
                                        "FFrameGraphics persistence definition",
618
                                        null, 
619
                                        null
620
                        ); 
621
                        
622
                        definition.extend(manager.getDefinition(FFrame.PERSISTENCE_DEFINITION_NAME));        
623
                        
624
                        definition.addDynFieldInt(TYPE_FIELD).setMandatory(true);
625
                        definition.addDynFieldObject(SYMBOL_FIELD).setMandatory(true).setClassOfValue(ISymbol.class);
626
                        definition.addDynFieldObject(GEOMETRYADAPTER_FIELD).setClassOfValue(GeometryAdapter.class);
627
                }
628
                
629
                GeometryAdapter.registerPersistent();
630
        }
631
        
632
        @Override
633
        public void loadFromState(PersistentState state)
634
                        throws PersistenceException {
635
                super.loadFromState(state);
636
                m_type = state.getInt(TYPE_FIELD);
637
                m_symbol = (ISymbol)state.get(SYMBOL_FIELD);
638
                geometryAdapter = (GeometryAdapter)state.get(GEOMETRYADAPTER_FIELD);        
639
        }
640

    
641
        @Override
642
        public void saveToState(PersistentState state) throws PersistenceException {
643
                super.saveToState(state);
644
                state.set(TYPE_FIELD, m_type);
645
                state.set(SYMBOL_FIELD, m_symbol);
646
                state.set(GEOMETRYADAPTER_FIELD, geometryAdapter);
647
        }
648
}