1 package tim.prune.gui.profile;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.Graphics;
7 import java.awt.Graphics2D;
8 import java.awt.RenderingHints;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.MouseListener;
14 import javax.swing.JLabel;
15 import javax.swing.JMenuItem;
16 import javax.swing.JPopupMenu;
18 import tim.prune.I18nManager;
19 import tim.prune.config.ColourScheme;
20 import tim.prune.config.Config;
21 import tim.prune.data.Field;
22 import tim.prune.data.FieldList;
23 import tim.prune.data.TrackInfo;
24 import tim.prune.gui.GenericDisplay;
27 * Chart component for the profile display
29 public class ProfileChart extends GenericDisplay implements MouseListener
31 /** Inner class to handle popup menu clicks */
32 class MenuClicker implements ActionListener
34 private Field _field = null;
35 MenuClicker(Field inField) {_field = inField;}
36 /** React to menu click by changing the field */
37 public void actionPerformed(ActionEvent arg0) {
42 /** Inner class to remember a single index */
45 public int index = -1;
46 public boolean hasValue = false;
52 /** Set a single value */
53 public void set(int inValue)
56 hasValue = (inValue != -1);
58 /** Add an index to the minimum calculation */
59 public void setMin(PointIndex other)
61 if (!other.hasValue) {return;}
64 hasValue = other.hasValue;
67 index = Math.min(index, other.index);
70 /** Add an index to the maximum calculation */
71 public void setMax(PointIndex other)
73 if (!other.hasValue) {return;}
76 hasValue = other.hasValue;
79 index = Math.max(index, other.index);
82 /** @return true if two Indexes are equal */
83 public boolean equals(PointIndex other)
85 if (!hasValue || !other.hasValue) {
86 return hasValue == other.hasValue;
88 return index == other.index;
92 /** Inner class to remember previous chart parameters */
95 public PointIndex selectedPoint = new PointIndex();
96 public PointIndex rangeStart = new PointIndex(), rangeEnd = new PointIndex();
99 selectedPoint.hasValue = false;
100 rangeStart.hasValue = false;
101 rangeEnd.hasValue = false;
103 /** Get the minimum index which has changed between two sets of parameters */
104 public int getMinChangedIndex(ChartParameters other)
106 PointIndex minIndex = new PointIndex();
107 if (!selectedPoint.equals(other.selectedPoint)) {
108 minIndex.setMin(selectedPoint);
109 minIndex.setMin(other.selectedPoint);
111 if (!rangeStart.equals(other.rangeStart)) {
112 minIndex.setMin(rangeStart);
113 minIndex.setMin(other.rangeStart);
115 if (!rangeEnd.equals(other.rangeEnd)) {
116 minIndex.setMin(rangeEnd);
117 minIndex.setMin(other.rangeEnd);
119 return minIndex.index;
121 /** Get the maximum index which has changed between two sets of parameters */
122 public int getMaxChangedIndex(ChartParameters other)
124 PointIndex maxIndex = new PointIndex();
125 if (!selectedPoint.equals(other.selectedPoint)) {
126 maxIndex.setMax(selectedPoint);
127 maxIndex.setMax(other.selectedPoint);
129 if (!rangeStart.equals(other.rangeStart)) {
130 maxIndex.setMax(rangeStart);
131 maxIndex.setMax(other.rangeStart);
133 if (!rangeEnd.equals(other.rangeEnd)) {
134 maxIndex.setMax(rangeEnd);
135 maxIndex.setMax(other.rangeEnd);
137 return maxIndex.index;
139 /** @return true if the parameters are completely empty (cleared) */
140 public boolean isEmpty()
142 return !selectedPoint.hasValue && !rangeStart.hasValue && !rangeEnd.hasValue;
146 /** Current scale factor in x direction*/
147 private double _xScaleFactor = 0.0;
148 /** Data to show on chart */
149 private ProfileData _data = null;
150 /** Label for chart type, units */
151 private JLabel _label = null;
152 /** Right-click popup menu */
153 private JPopupMenu _popup = null;
154 /** Parameters last time chart was drawn */
155 private ChartParameters _previousParameters = new ChartParameters();
157 /** Possible scales to use */
158 private static final int[] LINE_SCALES = {10000, 5000, 2000, 1000, 500, 200, 100, 50, 10, 5, 2, 1};
159 /** Border width around black line */
160 private static final int BORDER_WIDTH = 6;
161 /** Minimum size for profile chart in pixels */
162 private static final Dimension MINIMUM_SIZE = new Dimension(200, 110);
163 /** Colour to use for text if no data found */
164 private static final Color COLOR_NODATA_TEXT = Color.GRAY;
169 * @param inTrackInfo Track info object
171 public ProfileChart(TrackInfo inTrackInfo)
174 _data = new AltitudeData(inTrackInfo.getTrack());
175 addMouseListener(this);
176 setLayout(new FlowLayout(FlowLayout.LEFT));
177 _label = new JLabel("Altitude"); // text will be replaced later
184 * Override minimum size method to restrict slider
186 public Dimension getMinimumSize()
192 * Override paint method to draw map
193 * @param g Graphics object
195 public void paint(Graphics g)
198 // Set antialiasing depending on Config
199 ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
200 Config.getConfigBoolean(Config.KEY_ANTIALIAS) ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
201 ColourScheme colourScheme = Config.getColourScheme();
202 paintBackground(g, colourScheme);
204 if (_track == null || _track.getNumPoints() <= 0)
209 _label.setText(_data.getLabel());
210 int width = getWidth();
211 int height = getHeight();
214 final Color barColour = colourScheme.getColour(ColourScheme.IDX_POINT);
215 final Color rangeColour = colourScheme.getColour(ColourScheme.IDX_SELECTION);
216 final Color currentColour = colourScheme.getColour(ColourScheme.IDX_PRIMARY);
217 final Color secondColour = colourScheme.getColour(ColourScheme.IDX_SECONDARY);
218 final Color lineColour = colourScheme.getColour(ColourScheme.IDX_LINES);
220 // message if no data for the current field in track
221 if (!_data.hasData())
223 g.setColor(lineColour);
224 g.drawString(I18nManager.getText(_data.getNoDataKey()), 50, (height+_label.getHeight())/2);
229 // Find minimum and maximum values to plot
230 double minValue = _data.getMinValue();
231 double maxValue = _data.getMaxValue();
232 if (maxValue <= minValue) {maxValue = minValue + 1; minValue--;}
234 final int numPoints = _track.getNumPoints();
235 _xScaleFactor = 1.0 * (width - 2 * BORDER_WIDTH - 1) / numPoints;
236 int usableHeight = height - 2 * BORDER_WIDTH - _label.getHeight();
237 double yScaleFactor = 1.0 * usableHeight / (maxValue - minValue);
238 int barWidth = (int) (_xScaleFactor + 1.0);
239 int selectedPoint = _trackInfo.getSelection().getCurrentPointIndex();
240 // selection start, end
241 int selectionStart = -1, selectionEnd = -1;
242 if (_trackInfo.getSelection().hasRangeSelected()) {
243 selectionStart = _trackInfo.getSelection().getStart();
244 selectionEnd = _trackInfo.getSelection().getEnd();
249 // horizontal lines for scale - set to round numbers eg 500
250 final int lineScale = getLineScale(minValue, maxValue);
251 double scaleValue = Math.ceil(minValue/lineScale) * lineScale;
252 final int zeroY = height - BORDER_WIDTH - (int) (yScaleFactor * (0.0 - minValue));
254 g.setColor(lineColour);
257 while (scaleValue < maxValue)
259 y = height - BORDER_WIDTH - (int) (yScaleFactor * (scaleValue - minValue));
260 g.drawLine(BORDER_WIDTH + 1, y, width - BORDER_WIDTH - 1, y);
261 scaleValue += lineScale;
264 else if (minValue < 0.0)
266 // just draw zero line
268 g.drawLine(BORDER_WIDTH + 1, y, width - BORDER_WIDTH - 1, y);
273 // loop through points
274 g.setColor(barColour);
275 for (int p = 0; p < numPoints; p++)
277 if (p == selectionStart)
278 g.setColor(rangeColour);
279 else if (p == (selectionEnd+1))
280 g.setColor(barColour);
282 final int x = (int) (_xScaleFactor * p) + 1;
283 if (_data.hasData(p))
285 value = _data.getData(p);
286 // Normal case is the minimum value greater than zero
289 y = (int) (yScaleFactor * (value - minValue));
290 g.fillRect(BORDER_WIDTH+x, height-BORDER_WIDTH - y, barWidth, y);
292 else if (value >= 0.0) {
293 // Bar upwards from the zero line
294 y = height-BORDER_WIDTH - (int) (yScaleFactor * (value - minValue));
295 g.fillRect(BORDER_WIDTH+x, y, barWidth, zeroY - y);
298 // Bar downwards from the zero line
299 int barHeight = (int) (yScaleFactor * value);
300 g.fillRect(BORDER_WIDTH+x, zeroY, barWidth, -barHeight);
305 // current point (make sure it's drawn last)
306 if (selectedPoint >= 0)
308 final int sel_x = (int) (_xScaleFactor * selectedPoint) + 1;
309 g.setColor(secondColour);
310 g.fillRect(BORDER_WIDTH + sel_x, height-usableHeight-BORDER_WIDTH+1, barWidth, usableHeight-2);
311 if (_data.hasData(selectedPoint))
313 g.setColor(currentColour);
314 value = _data.getData(selectedPoint);
315 y = (int) (yScaleFactor * (value - minValue));
316 g.fillRect(BORDER_WIDTH + sel_x, height-BORDER_WIDTH - y, barWidth, y);
320 catch (NullPointerException npe) { // ignore, probably due to data being changed
322 // Draw numbers on top of the graph to mark scale
325 int textHeight = g.getFontMetrics().getHeight();
326 scaleValue = (int) (minValue / lineScale + 1) * lineScale;
327 if (minValue < 0.0) {scaleValue -= lineScale;}
329 g.setColor(currentColour);
330 while (scaleValue < maxValue)
332 y = height - BORDER_WIDTH - (int) (yScaleFactor * (scaleValue - minValue));
333 // Limit y so String isn't above border
334 if (y < (BORDER_WIDTH + textHeight)) {
335 y = BORDER_WIDTH + textHeight;
337 g.drawString(""+(int)scaleValue, BORDER_WIDTH + 5, y);
338 scaleValue += lineScale;
341 // Paint label on top
347 * Paint the background for the chart
348 * @param inG graphics object
349 * @param inColourScheme colour scheme
351 private void paintBackground(Graphics inG, ColourScheme inColourScheme)
353 final int width = getWidth();
354 final int height = getHeight();
356 final Color borderColour = inColourScheme.getColour(ColourScheme.IDX_BORDERS);
357 final Color backgroundColour = inColourScheme.getColour(ColourScheme.IDX_BACKGROUND);
359 inG.setColor(backgroundColour);
360 inG.fillRect(0, 0, width, height);
361 if (width < 2*BORDER_WIDTH || height < 2*BORDER_WIDTH) return;
362 // Display message if no data to be displayed
363 if (_track == null || _track.getNumPoints() <= 0)
365 inG.setColor(COLOR_NODATA_TEXT);
366 inG.drawString(I18nManager.getText("display.nodata"), 50, height/2);
370 inG.setColor(borderColour);
371 inG.drawRect(BORDER_WIDTH, BORDER_WIDTH + _label.getHeight(),
372 width - 2*BORDER_WIDTH, height-2*BORDER_WIDTH-_label.getHeight());
377 * Make the popup menu for right-clicking the chart
379 private synchronized void makePopup()
381 _popup = new JPopupMenu();
382 JMenuItem altItem = new JMenuItem(I18nManager.getText("fieldname.altitude"));
383 altItem.addActionListener(new ActionListener() {
384 public void actionPerformed(ActionEvent e)
386 changeView(Field.ALTITUDE);
389 JMenuItem speedItem = new JMenuItem(I18nManager.getText("fieldname.speed"));
390 speedItem.addActionListener(new ActionListener() {
391 public void actionPerformed(ActionEvent e)
393 changeView(Field.SPEED);
395 _popup.add(speedItem);
396 JMenuItem vertSpeedItem = new JMenuItem(I18nManager.getText("fieldname.verticalspeed"));
397 vertSpeedItem.addActionListener(new ActionListener() {
398 public void actionPerformed(ActionEvent e)
400 changeView(Field.VERTICAL_SPEED);
402 _popup.add(vertSpeedItem);
403 // Go through track's master field list, see if any other fields to list
404 boolean addSeparator = true;
405 FieldList fields = _track.getFieldList();
406 for (int i=0; i<fields.getNumFields(); i++)
408 Field field = fields.getField(i);
409 if (!field.isBuiltIn())
411 if (addSeparator) {_popup.addSeparator();}
412 addSeparator = false;
413 JMenuItem item = new JMenuItem(field.getName());
414 item.addActionListener(new MenuClicker(field));
421 * Work out the scale for the horizontal lines
422 * @param inMin min value of data
423 * @param inMax max value of data
424 * @return scale separation, or -1 for no scale
426 private int getLineScale(double inMin, double inMax)
428 if ((inMax - inMin) < 2.0) {
431 int numScales = LINE_SCALES.length;
432 for (int i=0; i<numScales; i++)
434 int scale = LINE_SCALES[i];
435 int numLines = (int)(inMax / scale) - (int)(inMin / scale);
436 // Check for too many lines
437 if (numLines > 10) return -1;
438 // If more than 1 line then use this scale
439 if (numLines > 1) return scale;
441 // no suitable scale found so just use minimum
442 return LINE_SCALES[numScales-1];
447 * Method to inform map that data has changed
449 public void dataUpdated(byte inUpdateType)
451 // Try not to recalculate all the values unless necessary
452 if (inUpdateType != SELECTION_CHANGED)
454 _data.init(Config.getUnitSet());
455 _previousParameters.clear();
457 // Update the menu if necessary
458 if ((inUpdateType & DATA_ADDED_OR_REMOVED) > 0) {
461 if (inUpdateType == SELECTION_CHANGED) {
462 triggerPartialRepaint();
471 * For performance reasons, only repaint the part of the graphics affected by
472 * the change in selection
474 private void triggerPartialRepaint()
476 ChartParameters currentParameters = new ChartParameters();
477 currentParameters.selectedPoint.set(_trackInfo.getSelection().getCurrentPointIndex());
478 if (_trackInfo.getSelection().hasRangeSelected())
480 currentParameters.rangeStart.set(_trackInfo.getSelection().getStart());
481 currentParameters.rangeEnd.set(_trackInfo.getSelection().getEnd());
484 int minPointIndex = currentParameters.getMinChangedIndex(_previousParameters);
485 minPointIndex = Math.max(minPointIndex, 0);
486 int maxPointIndex = currentParameters.getMaxChangedIndex(_previousParameters);
487 if (maxPointIndex < minPointIndex) {
488 maxPointIndex = _trackInfo.getTrack().getNumPoints() - 1;
490 // System.out.println("Redraw from index: " + minPointIndex + " to " + maxPointIndex);
491 _previousParameters = currentParameters;
492 final int region_x = (int) (_xScaleFactor * minPointIndex) + BORDER_WIDTH;
493 final int region_width = (int) (_xScaleFactor * (maxPointIndex-minPointIndex+2)) + 2;
494 repaint(region_x, 0, region_width, getHeight());
498 * React to click on profile display
500 public void mouseClicked(MouseEvent e)
502 if (_track == null || _track.getNumPoints() < 1) {return;}
506 int xClick = e.getX();
507 int yClick = e.getY();
508 // Check click is within main area (not in border)
509 if (xClick > BORDER_WIDTH && yClick > BORDER_WIDTH && xClick < (getWidth() - BORDER_WIDTH)
510 && yClick < (getHeight() - BORDER_WIDTH))
512 // work out which data point is nearest and select it
513 int pointNum = (int) ((e.getX() - BORDER_WIDTH) / _xScaleFactor);
514 // If shift clicked, then extend selection
515 if (e.isShiftDown()) {
516 _trackInfo.extendSelection(pointNum);
519 _trackInfo.selectPoint(pointNum);
525 _popup.show(this, e.getX(), e.getY());
530 * Called by clicking on popup menu to change the view
531 * @param inField field to show
533 private void changeView(Field inField)
535 if (inField == Field.ALTITUDE)
537 if (!(_data instanceof AltitudeData)) {
538 _data = new AltitudeData(_track);
541 else if (inField == Field.SPEED) {
542 if (!(_data instanceof SpeedData)) {
543 _data = new SpeedData(_track);
546 else if (inField == Field.VERTICAL_SPEED) {
547 if (!(_data instanceof VerticalSpeedData)) {
548 _data = new VerticalSpeedData(_track);
553 if (!(_data instanceof ArbitraryData) || ((ArbitraryData)_data).getField() != inField) {
554 _data = new ArbitraryData(_track, inField);
557 _data.init(Config.getUnitSet());
562 * mouse enter events ignored
564 public void mouseEntered(MouseEvent e)
568 * mouse exit events ignored
570 public void mouseExited(MouseEvent e)
574 * ignore mouse pressed for now too
576 public void mousePressed(MouseEvent e)
580 * and also ignore mouse released
582 public void mouseReleased(MouseEvent e)