/*
* Copyright (C) 2011-2013 GeoForge Project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package _org.geoforge.amd.wwwx.examples.measuretool;
import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.render.Polyline;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
/**
*
* @author Amadeus.Sowerby
*
* email: Amadeus.Sowerby_AT_gmail.com ... please remove "_AT_" from the above
* string to get the right email address
*/
@SuppressWarnings("unchecked")
public class GfrMeasureToolPanel extends JPanel
{
private final WorldWindow wwd;
private final GfrMeasureTool measureTool;
private JComboBox shapeCombo;
private JCheckBox showAnnotationCheck;
private JCheckBox rubberBandCheck;
private JCheckBox freeHandCheck;
private JButton newButton;
private JButton pauseButton;
private JButton endButton;
private JLabel lengthLabel;
private static ArrayList<Position> LINE = new ArrayList<Position>();
private static ArrayList<Position> PATH = new ArrayList<Position>();
static
{
LINE.add(Position.fromDegrees(44, 7, 0));
LINE.add(Position.fromDegrees(45, 8, 0));
PATH.addAll(LINE);
PATH.add(Position.fromDegrees(46, 6, 0));
PATH.add(Position.fromDegrees(47, 5, 0));
PATH.add(Position.fromDegrees(45, 6, 0));
}
public GfrMeasureToolPanel(WorldWindow wwdObject, GfrMeasureTool measureToolObject)
{
super(new BorderLayout());
this.wwd = wwdObject;
this.measureTool = measureToolObject;
this.makePanel(new Dimension(200, 300));
// Handle measure tool events
measureTool.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent event)
{
// Add, remove or change positions
if (event.getPropertyName().equals(GfrMeasureTool.EVENT_POSITION_ADD)
|| event.getPropertyName().equals(GfrMeasureTool.EVENT_POSITION_REMOVE)
|| event.getPropertyName().equals(GfrMeasureTool.EVENT_POSITION_REPLACE))
{
//NOTHING
}
// The tool was armed / disarmed
else if (event.getPropertyName().equals(GfrMeasureTool.EVENT_ARMED))
{
if (measureTool.isArmed())
{
newButton.setEnabled(false);
pauseButton.setText("Pause");
pauseButton.setEnabled(true);
endButton.setEnabled(true);
((Component) wwd).setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
else
{
newButton.setEnabled(true);
pauseButton.setText("Pause");
pauseButton.setEnabled(false);
endButton.setEnabled(false);
((Component) wwd).setCursor(Cursor.getDefaultCursor());
}
}
// Metric changed - sent after each render frame
else if (event.getPropertyName().equals(GfrMeasureTool.EVENT_METRIC_CHANGED))
{
updateMetric();
}
}
});
}
public GfrMeasureTool getMeasureTool()
{
return this.measureTool;
}
private void makePanel(Dimension size)
{
// Shape combo
JPanel shapePanel = new JPanel(new GridLayout(1, 2, 5, 5));
shapePanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
shapePanel.add(new JLabel("Shape:"));
shapeCombo = new JComboBox(new String[]
{
"Line", "Path"
});
shapeCombo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String item = (String) ((JComboBox) event.getSource()).getSelectedItem();
if (item.equals("Line"))
measureTool.setMeasureShapeType(GfrMeasureTool.SHAPE_LINE);
else if (item.equals("Path"))
measureTool.setMeasureShapeType(GfrMeasureTool.SHAPE_PATH);
}
});
shapePanel.add(shapeCombo);
// Check boxes panel
JPanel checkPanel = new JPanel(new GridLayout(3, 2, 5, 5));
checkPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
rubberBandCheck = new JCheckBox("Rubber band");
rubberBandCheck.setSelected(measureTool.getController().isUseRubberBand());
rubberBandCheck.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JCheckBox cb = (JCheckBox) event.getSource();
measureTool.getController().setUseRubberBand(cb.isSelected());
freeHandCheck.setEnabled(cb.isSelected());
wwd.redraw();
}
});
checkPanel.add(rubberBandCheck);
freeHandCheck = new JCheckBox("Free Hand");
freeHandCheck.setSelected(measureTool.getController().isFreeHand());
freeHandCheck.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JCheckBox cb = (JCheckBox) event.getSource();
measureTool.getController().setFreeHand(cb.isSelected());
wwd.redraw();
}
});
checkPanel.add(freeHandCheck);
showAnnotationCheck = new JCheckBox("Tooltip");
showAnnotationCheck.setSelected(measureTool.isShowAnnotation());
showAnnotationCheck.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JCheckBox cb = (JCheckBox) event.getSource();
measureTool.setShowAnnotation(cb.isSelected());
wwd.redraw();
}
});
checkPanel.add(showAnnotationCheck);
// Action buttons
JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 5));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
newButton = new JButton("New");
newButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent actionEvent)
{
measureTool.clear();
measureTool.setArmed(true);
}
});
buttonPanel.add(newButton);
newButton.setEnabled(true);
pauseButton = new JButton("Pause");
pauseButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent actionEvent)
{
measureTool.setArmed(!measureTool.isArmed());
pauseButton.setText(!measureTool.isArmed() ? "Resume" : "Pause");
pauseButton.setEnabled(true);
((Component) wwd).setCursor(!measureTool.isArmed() ? Cursor.getDefaultCursor()
: Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
});
buttonPanel.add(pauseButton);
pauseButton.setEnabled(false);
endButton = new JButton("End");
endButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent actionEvent)
{
measureTool.setArmed(false);
}
});
buttonPanel.add(endButton);
endButton.setEnabled(false);
// Preset buttons
JPanel presetPanel = new JPanel(new GridLayout(1, 2, 5, 5));
presetPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JButton bt = new JButton("Polyline");
bt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
shapeCombo.setSelectedIndex(1);
measureTool.setMeasureShape(new Polyline(PATH));
}
});
presetPanel.add(bt);
// Metric
JPanel metricPanel = new JPanel(new GridLayout(0, 2, 0, 4));
metricPanel.setBorder(new CompoundBorder(
new TitledBorder("Metric"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
metricPanel.add(new JLabel("Length:"));
lengthLabel = new JLabel();
metricPanel.add(lengthLabel);
// Add all the panels to a titled panel
JPanel outerPanel = new JPanel();
outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS));
outerPanel.setBorder(
new CompoundBorder(BorderFactory.createEmptyBorder(9, 9, 9, 9), new TitledBorder("Measure")));
outerPanel.setToolTipText("Measure tool control and info");
outerPanel.add(shapePanel);
outerPanel.add(checkPanel);
outerPanel.add(buttonPanel);
outerPanel.add(metricPanel);
this.add(outerPanel, BorderLayout.NORTH);
}
private void updateMetric()
{
// Update length label
double value = measureTool.getLength();
String s;
if (value <= 0)
s = "na";
else if (value < 1000)
s = String.format("%,7.1f m", value);
else
s = String.format("%,7.3f km", value / 1000);
lengthLabel.setText(s);
}
}