1 package tim.prune.function;
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.KeyAdapter;
9 import java.awt.event.KeyEvent;
11 import javax.swing.BorderFactory;
12 import javax.swing.BoxLayout;
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JLabel;
16 import javax.swing.JList;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.JTextField;
20 import javax.swing.event.ListSelectionEvent;
21 import javax.swing.event.ListSelectionListener;
24 import tim.prune.GenericFunction;
25 import tim.prune.I18nManager;
26 import tim.prune.data.DataPoint;
27 import tim.prune.gui.WaypointNameMatcher;
30 * Class to provide the function to find a waypoint by name
32 public class FindWaypoint extends GenericFunction
34 private WaypointNameMatcher _nameMatcher = null;
35 private JDialog _dialog = null;
36 private JTextField _searchField = null;
37 private JList<String> _pointList = null;
38 private JButton _okButton = null;
43 * @param inApp application object for callback
45 public FindWaypoint(App inApp)
50 /** Get the name key */
51 public String getNameKey() {
52 return "function.findwaypoint";
63 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
64 _dialog.setLocationRelativeTo(_parentFrame);
65 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
66 _dialog.getContentPane().add(makeDialogComponents());
69 // Initialise waypoint list from track
70 _nameMatcher.init(_app.getTrackInfo().getTrack());
72 _searchField.setText("");
73 _dialog.setVisible(true);
78 * Create dialog components
79 * @return Panel containing all gui elements in dialog
81 private Component makeDialogComponents()
83 JPanel dialogPanel = new JPanel();
84 dialogPanel.setLayout(new BorderLayout());
85 JPanel topPanel = new JPanel();
86 topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
87 topPanel.add(new JLabel(I18nManager.getText("dialog.findwaypoint.intro")));
88 JPanel subPanel = new JPanel();
89 subPanel.add(new JLabel(I18nManager.getText("dialog.findwaypoint.search")));
90 _searchField = new JTextField(12);
91 _searchField.addKeyListener(new KeyAdapter() {
92 public void keyReleased(KeyEvent e)
94 _nameMatcher.findMatches(_searchField.getText());
95 if (_nameMatcher.getSize() == 0 || _nameMatcher.getSize() < _pointList.getSelectedIndex())
96 _okButton.setEnabled(false);
97 else if (_pointList.getSelectedIndex() >= 0)
98 _okButton.setEnabled(true);
99 // close dialog if escape pressed
100 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
105 subPanel.add(_searchField);
106 topPanel.add(subPanel);
107 dialogPanel.add(topPanel, BorderLayout.NORTH);
109 // middle panel with list
110 _nameMatcher = new WaypointNameMatcher();
111 _pointList = new JList<String>(_nameMatcher);
112 _pointList.addListSelectionListener(new ListSelectionListener() {
113 public void valueChanged(ListSelectionEvent e)
115 if (!e.getValueIsAdjusting()) _okButton.setEnabled(true);
117 JScrollPane listPane = new JScrollPane(_pointList);
118 dialogPanel.add(listPane, BorderLayout.CENTER);
120 // button panel at bottom
121 JPanel buttonPanel = new JPanel();
122 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
123 _okButton = new JButton(I18nManager.getText("button.ok"));
124 ActionListener okListener = new ActionListener() {
125 public void actionPerformed(ActionEvent e)
130 _okButton.addActionListener(okListener);
131 _okButton.setEnabled(false);
132 buttonPanel.add(_okButton);
133 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
134 cancelButton.addActionListener(new ActionListener() {
135 public void actionPerformed(ActionEvent e)
140 buttonPanel.add(cancelButton);
141 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
142 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
147 * Finish the dialog when OK pressed
148 * Select the point and close the dialog
150 private void finish()
152 DataPoint selectedPoint = _nameMatcher.getWaypoint(_pointList.getSelectedIndex());
153 _app.getTrackInfo().selectPoint(selectedPoint);