Changeset 7340 in josm


Ignore:
Timestamp:
2014-07-28T00:15:43+02:00 (10 years ago)
Author:
Don-vip
Message:

see #10269 - fix NPE (robustness. trkPnt.customColoring should probably not be null)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelper.java

    r7323 r7340  
    33package org.openstreetmap.josm.gui.layer.gpx;
    44
     5import static org.openstreetmap.josm.tools.I18n.marktr;
    56import static org.openstreetmap.josm.tools.I18n.tr;
    6 import static org.openstreetmap.josm.tools.I18n.marktr;
    77
    88import java.awt.BasicStroke;
     
    1515import java.util.Date;
    1616import java.util.List;
     17
    1718import org.openstreetmap.josm.Main;
    1819import org.openstreetmap.josm.data.coor.LatLon;
     
    2425
    2526/**
    26  * Class that helps to draw large set of GPS tracks with different colors and options 
     27 * Class that helps to draw large set of GPS tracks with different colors and options
    2728 * @since 7319
    2829 */
    2930public class GpxDrawHelper {
    3031    private GpxData data;
    31    
     32
    3233    // draw lines between points belonging to different segments
    3334    private boolean forceLines;
     
    5152
    5253    private static final double PHI = Math.toRadians(15);
    53    
     54
    5455    //// Variables used only to check cache validity
    5556    private boolean computeCacheInSync = false;
     
    6263    //// Color-related fields
    6364    /** Mode of the line coloring **/
    64     private ColorMode colored; 
     65    private ColorMode colored;
    6566    /** max speed for coloring - allows to tweak line coloring for different speed levels. **/
    6667    private int colorTracksTune;
     
    7576    ColorScale dateScale;
    7677    ColorScale directionScale;
    77    
     78
    7879    /** Opacity for hdop points **/
    7980    private int hdopAlpha;
    80    
     81
    8182
    8283    // lookup array to draw arrows without doing any math
     
    8889        { +ll0, -sl4, +sl4, -ll0 }, { +ll0, +sl9, +ll0, -sl9 }, { +sl4, +ll0, +ll0, +sl4 },
    8990        { -sl9, +ll0, +sl9, +ll0 }, { -ll0, +sl4, -sl4, +ll0 }, { -ll0, -sl9, -ll0, +sl9 } };
    90    
     91
    9192    private void setupColors() {
    9293        hdopAlpha = Main.pref.getInteger("hdop.color.alpha", -1);
     
    104105        NONE, VELOCITY, HDOP, DIRECTION, TIME
    105106    }
    106    
     107
    107108    public GpxDrawHelper(GpxData gpxData) {
    108109        data = gpxData;
    109110        setupColors();
    110111    }
    111    
     112
    112113    /**
    113      * Get the default color for gps tracks for specified layer 
     114     * Get the default color for gps tracks for specified layer
    114115     * @param layerName name of the GpxLayer
    115116     * @param ignoreCustom do not use preferences
    116      * @return the color or null if the color is not constant 
     117     * @return the color or null if the color is not constant
    117118     */
    118119    public Color getColor(String layerName, boolean ignoreCustom) {
     
    142143        return Main.pref.getColor(marktr("gps point"), Color.gray);
    143144    }
    144    
     145
    145146    /**
    146147     * Read all drawing-related settings from preferences
     
    177178        hdopScale.setNoDataColor(neutralColor);
    178179        directionScale.setNoDataColor(neutralColor);
    179        
     180
    180181        largesize += lineWidth;
    181182    }
    182    
    183    
     183
     184
    184185    public void drawAll(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
    185186
    186187        checkCache();
    187        
     188
    188189        // STEP 2b - RE-COMPUTE CACHE DATA *********************
    189190        if (!computeCacheInSync) { // don't compute if the cache is good
     
    192193
    193194        Stroke storedStroke = g.getStroke();
    194        
     195
    195196        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    196197            Main.pref.getBoolean("mappaint.gpx.use-antialiasing", false) ?
    197198                    RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
    198        
     199
    199200        if(lineWidth != 0) {
    200201           g.setStroke(new BasicStroke(lineWidth,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
     
    207208        }
    208209    }
    209    
     210
    210211    public void calculateColors() {
    211212        double minval = +1e10;
    212213        double maxval = -1e10;
    213214        WayPoint oldWp = null;
    214        
     215
    215216        if (colorModeDynamic) {
    216217            if (colored == ColorMode.VELOCITY) {
     
    280281        }
    281282
    282        
     283
    283284        // Now the colors for all the points will be assigned
    284285        for (Collection<WayPoint> segment : data.getLinesIterable(null)) {
     
    293294                }
    294295                 // now we are sure some color will be assigned
    295                 Color color = null; 
    296            
     296                Color color = null;
     297
    297298                if (colored == ColorMode.HDOP) {
    298299                    Float hdop = ((Float) trkPnt.attr.get("hdop"));
     
    329330                    } else {
    330331                        trkPnt.drawLine = false;
    331                     } 
     332                    }
    332333                } else { // make sure we reset outdated data
    333334                    trkPnt.drawLine = false;
     
    340341            }
    341342        }
    342        
     343
    343344        computeCacheInSync = true;
    344345    }
    345346
    346347
    347  
     348
    348349    private void drawLines(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
    349350        if (lines) {
     
    396397                }
    397398            } // end for trkpnt
    398         } 
    399        
     399        }
     400
    400401        /****************************************************************
    401402         ********** STEP 3c - DRAW FAST ARROWS **************************
     
    425426                }
    426427            } // end for trkpnt
    427         } 
     428        }
    428429    }
    429430
     
    440441                }
    441442                Point screen = mv.getPoint(trkPnt.getEastNorth());
    442                
    443                    
     443
     444
    444445                if (hdopCircle && trkPnt.attr.get("hdop") != null) {
    445446                    // hdop value
     
    457458                if (large) {
    458459                    // color the large GPS points like the gps lines
    459                     Color customColoringTransparent = largePointAlpha<0 ? trkPnt.customColoring:
    460                         new Color(trkPnt.customColoring.getRGB() & 0x00ffffff | largePointAlpha<<24, true);
    461                    
    462                     g.setColor(customColoringTransparent);
     460                    if (trkPnt.customColoring != null) {
     461                        Color customColoringTransparent = largePointAlpha<0 ? trkPnt.customColoring:
     462                            new Color(trkPnt.customColoring.getRGB() & 0x00ffffff | largePointAlpha<<24, true);
     463
     464                        g.setColor(customColoringTransparent);
     465                    }
    463466                    g.fillRect(screen.x-halfSize, screen.y-halfSize, largesize, largesize);
    464467                }
     
    519522        computeCacheInSync = false;
    520523    }
    521    
     524
    522525    public void drawColorBar(Graphics2D g, MapView mv) {
    523526        int w = mv.getWidth();
     
    531534        }
    532535    }
    533    
     536
    534537}
Note: See TracChangeset for help on using the changeset viewer.