Revision 47476 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.symbology/org.gvsig.symbology.lib/org.gvsig.symbology.lib.impl/src/main/java/org/gvsig/symbology/fmap/mapcontext/rendering/symbol/marker/impl/AbstractMarkerSymbol.java

View differences:

AbstractMarkerSymbol.java
23 23
 */
24 24
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl;
25 25

  
26
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol_v2;
27 26
import java.awt.Color;
28
import java.awt.Graphics2D;
29 27
import java.awt.Point;
30 28
import java.awt.Rectangle;
31
import java.awt.geom.AffineTransform;
32 29
import java.awt.geom.Point2D;
33 30
import org.apache.commons.lang3.StringUtils;
34
import org.gvsig.compat.print.PrintAttributes;
35 31
import org.gvsig.expressionevaluator.ExpressionUtils;
36 32
import org.gvsig.expressionevaluator.SymbolTable;
37 33
import org.gvsig.fmap.dal.DALLocator;
......
39 35
import org.gvsig.fmap.dal.feature.Feature;
40 36
import org.gvsig.fmap.geom.Geometry;
41 37
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
42
import org.gvsig.fmap.geom.Geometry.TYPES;
43 38
import org.gvsig.fmap.geom.GeometryLocator;
44 39
import org.gvsig.fmap.geom.GeometryManager;
45 40
import org.gvsig.fmap.geom.exception.CreateGeometryException;
46 41
import org.gvsig.fmap.mapcontext.MapContextLocator;
47 42
import org.gvsig.fmap.mapcontext.ViewPort;
48
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
49 43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol;
50
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol_v2;
51 45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IMask;
52 46
import org.gvsig.tools.ToolsLocator;
53 47
import org.gvsig.tools.dynobject.DynStruct;
......
80 74
    private static final String FIELD_LINETOOFFSETCOLOR_EXPRESSION = "lineToOffsetColorExpression";
81 75

  
82 76
    private static final GeometryManager GEOMETRY_MANAGER = GeometryLocator.getGeometryManager();
83
    
77

  
84 78
    private Color color;
85 79
    private double rotation;
86 80
    private Point2D offset = new Point2D.Double();
......
95 89
    private String offsetYExpression;
96 90
    private String rotationExpression;
97 91
    private String lineToOffsetColorExpression;
98
    
92

  
99 93
    private transient org.gvsig.fmap.geom.primitive.Point point;
100
    private transient ViewPort viewPort;
101
    private transient double dpi;
102 94

  
103 95
    public AbstractMarkerSymbol() {
104 96
        super();
......
109 101
        try {
110 102
            this.point = GEOMETRY_MANAGER.createPoint(0, 0, SUBTYPES.GEOM2D);
111 103
        } catch (CreateGeometryException ex) {
112
            
104

  
113 105
        }
114 106
    }
115 107

  
......
118 110
        this.point.setY(y);
119 111
        return this.point;
120 112
    }
121
    
113

  
122 114
    @Override
123 115
    public final int getSymbolType() {
124 116
        return Geometry.TYPES.POINT;
125 117
    }
126
    
118

  
127 119
    @Override
128 120
    public double getRotation() {
129 121
        return rotation;
......
149 141
    }
150 142

  
151 143
    private FeatureSymbolTable symbolTable = null;
152
    
144

  
153 145
    protected SymbolTable getSymbolTable(Feature f) {
154
        if( symbolTable==null ) {
146
        if (symbolTable == null) {
155 147
            this.symbolTable = DALLocator.getManager().createFeatureSymbolTable();
156 148
            this.symbolTable.addSymbolTable(ExpressionUtils.createSymbolTable());
157 149
        }
158 150
        symbolTable.setFeature(f);
159 151
        return symbolTable;
160 152
    }
161
        
153

  
162 154
    @Override
163 155
    public Color getLineToOffsetColor() {
164 156
        return this.lineToOffsetColor;
......
231 223

  
232 224
    @Override
233 225
    public Point2D getEfectiveOffset(Feature f) {
234
        if( f == null ) {
226
        if (f == null) {
235 227
            f = this.getFeature();
236 228
        }
237 229
        Point2D p;
238
        if( f==null || StringUtils.isBlank(this.offsetXExpression) || StringUtils.isBlank(this.offsetYExpression) ) {
230
        if (f == null || StringUtils.isBlank(this.offsetXExpression) || StringUtils.isBlank(this.offsetYExpression)) {
239 231
            p = this.getOffset();
240 232
            p = new Point2D.Double(
241
                    toCartographicUnits(p.getX()), 
233
                    toCartographicUnits(p.getX()),
242 234
                    toCartographicUnits(p.getY())
243 235
            );
244 236
        } else {
245 237
            double offsetX = toCartographicUnits(ExpressionUtils.parseDouble(
246
                    this.getSymbolTable(f), 
247
                    offsetXExpression, 
238
                    this.getSymbolTable(f),
239
                    offsetXExpression,
248 240
                    (int) this.getOffset().getX()
249 241
            ));
250 242
            double offsetY = toCartographicUnits(ExpressionUtils.parseDouble(
251
                    this.getSymbolTable(f), 
252
                    offsetYExpression, 
243
                    this.getSymbolTable(f),
244
                    offsetYExpression,
253 245
                    (int) this.getOffset().getY()
254 246
            ));
255 247
            p = new Point2D.Double(offsetX, offsetY);
......
259 251

  
260 252
    @Override
261 253
    public double getEfectiveRotationInDegres(Feature f) {
262
        if( StringUtils.isBlank(this.rotationExpression) ) {
254
        if (StringUtils.isBlank(this.rotationExpression)) {
263 255
            return Math.toDegrees(this.getRotation());
264 256
        }
265 257
        // Por defecto vedra en grados.
266
        if( f == null ) {
258
        if (f == null) {
267 259
            f = this.getFeature();
268 260
        }
269
        if( f==null ) {
261
        if (f == null) {
270 262
            return Math.toDegrees(this.getRotation());
271 263
        }
272 264
        double r = ExpressionUtils.parseDouble(
273
                this.getSymbolTable(f), 
274
                rotationExpression, 
265
                this.getSymbolTable(f),
266
                rotationExpression,
275 267
                this.getRotation()
276 268
        );
277 269
        return r;
......
280 272
    @Override
281 273
    public double getEfectiveRotationInRadians(Feature f) {
282 274
        // Pillamos el valor por defecto que es en grados y lo transformamos a radianes.
283
        if( StringUtils.isBlank(this.rotationExpression) ) {
275
        if (StringUtils.isBlank(this.rotationExpression)) {
284 276
            return this.getRotation();
285 277
        }
286 278
        double r = this.getEfectiveRotationInDegres(f);
287
        if( r == 0 ) {
279
        if (r == 0) {
288 280
            return 0;
289 281
        }
290 282
        r = Math.toRadians(r);
......
293 285

  
294 286
    @Override
295 287
    public Color getEfectiveLineToOffsetColor(Feature f) {
296
        if( f == null ) {
288
        if (f == null) {
297 289
            f = this.getFeature();
298 290
        }
299
        if( f==null || StringUtils.isBlank(this.lineToOffsetColorExpression) ) {
291
        if (f == null || StringUtils.isBlank(this.lineToOffsetColorExpression)) {
300 292
            return this.getLineToOffsetColor();
301 293
        }
302 294
        Color theColor = ExpressionUtils.parseColor(
303
                this.getSymbolTable(f), 
304
                this.lineToOffsetColorExpression, 
295
                this.getSymbolTable(f),
296
                this.lineToOffsetColorExpression,
305 297
                this.getLineToOffsetColor()
306 298
        );
307 299
        return theColor;
......
309 301

  
310 302
    @Override
311 303
    public double getEfectiveSize(Feature f) {
312
        if(StringUtils.isBlank(this.sizeExpression) ) {
313
            return this.getSize();
304
        if (StringUtils.isBlank(this.sizeExpression)) {
305
            return this.getCartographicSize();
314 306
        }
315
        if( f == null ) {
307
        if (f == null) {
316 308
            f = this.getFeature();
317 309
        }
318
        if( f==null ) {
319
            return this.getSize();
310
        if (f == null) {
311
            return this.getCartographicSize();
320 312
        }
321 313
        double sz = ExpressionUtils.parseDouble(
322
                this.getSymbolTable(f), 
323
                this.sizeExpression, 
314
                this.getSymbolTable(f),
315
                this.sizeExpression,
324 316
                -1
325 317
        );
326
        if(sz<0){
327
            return this.getSize();
318
        if (sz < 0) {
319
            return this.getCartographicSize();
328 320
        }
329 321
        return toCartographicUnits(sz);
330 322
    }
......
334 326
        return geom.getType() == Geometry.TYPES.POINT;
335 327
    }
336 328

  
337
    @Override
338
    public int getOnePointRgb() {
339
        return color.getRGB();
340
    }
329
//    @Override
330
//    public int getOnePointRgb() {
331
//        return color.getRGB();
332
//    }
341 333

  
342 334
    @Override
343 335
    public double getSize() {
......
367 359
                outlineAlpha));
368 360
    }
369 361

  
370
    @Override
371
    public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
372
        float cs = (float) getCartographicSize(viewPort, dpi, geom);
373
        distances[0] = cs;
374
        distances[1] = cs;
375
    }
362
//    @Override
363
//    public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
364
//        setCartographicContext(viewPort, dpi, geom);
365
//        float cs = (float) getCartographicSize();
366
//        distances[0] = cs;
367
//        distances[1] = cs;
368
//    }
376 369

  
377 370
    @Override
378
    public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
379
        double originalSize = getEfectiveSize(getFeature());
380
        double theSize = originalSize;
381
        // scale it to size
382
        int pq = properties.getPrintQuality();
383
        switch (pq) {
384
            case PrintAttributes.PRINT_QUALITY_NORMAL:
385
                theSize *= (double) 300 / 72;
386
                break;
387
            case PrintAttributes.PRINT_QUALITY_HIGH:
388
                theSize *= (double) 600 / 72;
389
                break;
390
            case PrintAttributes.PRINT_QUALITY_DRAFT:
391
                // size *= 72/72; // (which is the same than doing nothing)
392
                break;
393
            default:
394
                break;
395
        }
396
//		setSize(theSize);
397
        draw(g, at, geom, getFeature(), null);
398
//		setSize(originalSize);
399
    }
400

  
401
    @Override
402 371
    public final IMask getMask() {
403 372
        return mask;
404 373
    }
405 374

  
406 375
    @Override
407
    public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
408
        try {
409
            if (properties == null) {
410
                draw(
411
                        g, 
412
                        scaleInstance, 
413
                        this.getPoint(r.getCenterX(), r.getCenterY()),
414
//                        GEOMETRY_MANAGER.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), 
415
                        this.getFeature(), 
416
                        null
417
                );
418
            } else {
419
                double originalSize = getEfectiveSize(getFeature());
420
                double theSize = originalSize;
421
                int pq = properties.getPrintQuality();
422
                switch (pq) {
423
                    case PrintAttributes.PRINT_QUALITY_NORMAL:
424
                        theSize *= (double) 300 / 72;
425
                        break;
426
                    case PrintAttributes.PRINT_QUALITY_HIGH:
427
                        theSize *= (double) 600 / 72;
428
                        break;
429
                    case PrintAttributes.PRINT_QUALITY_DRAFT:
430
                        // d *= 72/72; // (which is the same than doing nothing)
431
                        break;
432
                    default:
433
                        break;
434
                }
435
                setSize(theSize);
436
                print(
437
                        g, 
438
                        scaleInstance, 
439
                        this.getPoint(r.getCenterX(), r.getCenterY()),
440
//                        GEOMETRY_MANAGER.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), 
441
                        properties
442
                );
443
                setSize(originalSize);
444
            }
445

  
446
        } catch (Exception e) {
447
            SymbolDrawingException sde = new SymbolDrawingException(TYPES.POINT);
448
            sde.initCause(e);
449
            throw sde;
450
        }
451
    }
452

  
453
    @Override
454 376
    public final void setMask(IMask mask) {
455 377
        this.mask = mask;
456 378
    }
457 379

  
458
    @Override
459
    public void setCartographicSize(double cartographicSize, Geometry geom) {
460
        setSize(cartographicSize);
380
    protected double getCartographicSize() {
381
        return toCartographicUnits(getSize());
461 382
    }
462 383

  
463 384
    @Override
464
    public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
465
        this.viewPort = viewPort;
466
        this.dpi  = dpi;
467

  
468
        double oldSize = getSize();
469
        setCartographicSize(getCartographicSize(
470
                viewPort,
471
                dpi,
472
                geom),
473
                geom);
474
        return oldSize;
475
    }
476

  
477
    @Override
478
    public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
479
        return CartographicSupportToolkit.
480
                getCartographicLength(this,
481
                        getSize(),
482
                        viewPort,
483
                        dpi);
484
    }
485
    
486
    protected double toCartographicUnits(double value){
487
        if(viewPort == null){
488
            return value;
489
        }
490
        return CartographicSupportToolkit.
491
                getCartographicLength(this,
492
                        value,
493
                        viewPort,
494
                        dpi);
495
    }
496

  
497
    @Override
498 385
    public Object clone() throws CloneNotSupportedException {
499 386
        AbstractMarkerSymbol copy = (AbstractMarkerSymbol) super.clone();
500 387

  
......
523 410
        setSize(state.getDouble(FIELD_SIZE));
524 411
        setDrawLineToOffset(state.getBoolean(FIELD_DRAWLINETOOFFSET, false));
525 412
        setLineToOffsetColor((Color) state.get(FIELD_LINETOOFFSETCOLOR));
526
        
413

  
527 414
        setSizeExpression(state.getString(FIELD_SIZE_EXPRESSION));
528 415
        setOffsetXExpression(state.getString(FIELD_OFFSETX_EXPRESSION));
529 416
        setOffsetYExpression(state.getString(FIELD_OFFSETY_EXPRESSION));
......
582 469
                // Size
583 470
                definition.addDynFieldDouble(FIELD_SIZE).setMandatory(false);
584 471

  
585

  
586 472
                definition.addDynFieldBoolean(FIELD_DRAWLINETOOFFSET).setMandatory(false);
587 473
                definition.addDynFieldObject(FIELD_LINETOOFFSETCOLOR).setClassOfValue(Color.class).setMandatory(false);
588
                
474

  
589 475
                definition.addDynFieldString(FIELD_OFFSETX_EXPRESSION).setMandatory(false);
590 476
                definition.addDynFieldString(FIELD_OFFSETY_EXPRESSION).setMandatory(false);
591 477
                definition.addDynFieldString(FIELD_LINETOOFFSETCOLOR_EXPRESSION).setMandatory(false);
......
597 483

  
598 484
    }
599 485

  
486
    protected double getAdjustedSize(Rectangle r, double size) {
487
        if (r == null) {
488
            return size;
489
        }
490
        double min = Math.min(r.getHeight(), r.getWidth());
491
        if (size > min) {
492
            size = min;
493
        }
494
        return size;
495
    }
496

  
497
    protected Geometry getSampleGeometry(Rectangle r) {
498
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
499
        try {
500
            return geomManager.createEnvelope(
501
                    r.x + 1,
502
                    r.y + 1,
503
                    r.x + r.width - 2,
504
                    r.y + r.height - 2,
505
                    Geometry.SUBTYPES.GEOM2D).getGeometry();
506
        } catch (Exception e) {
507
            throw new RuntimeException("Can't create smple geometry.", e);
508
        }
509

  
510
    }
511

  
600 512
}

Also available in: Unified diff