]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/FindWaypoint.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / function / FindWaypoint.java
1 package tim.prune.function;
2
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;
10
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;
22
23 import tim.prune.App;
24 import tim.prune.GenericFunction;
25 import tim.prune.I18nManager;
26 import tim.prune.data.DataPoint;
27 import tim.prune.gui.WaypointNameMatcher;
28
29 /**
30  * Class to provide the function to find a waypoint by name
31  */
32 public class FindWaypoint extends GenericFunction
33 {
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;
39
40
41         /**
42          * Constructor
43          * @param inApp application object for callback
44          */
45         public FindWaypoint(App inApp)
46         {
47                 super(inApp);
48         }
49
50         /** Get the name key */
51         public String getNameKey() {
52                 return "function.findwaypoint";
53         }
54
55         /**
56          * Begin the function
57          */
58         public void begin()
59         {
60                 // Make dialog window
61                 if (_dialog == null)
62                 {
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());
67                         _dialog.pack();
68                 }
69                 // Initialise waypoint list from track
70                 _nameMatcher.init(_app.getTrackInfo().getTrack());
71                 // Show dialog
72                 _searchField.setText("");
73                 _dialog.setVisible(true);
74         }
75
76
77         /**
78          * Create dialog components
79          * @return Panel containing all gui elements in dialog
80          */
81         private Component makeDialogComponents()
82         {
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)
93                         {
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) {
101                                         _dialog.dispose();
102                                 }
103                         }
104                 });
105                 subPanel.add(_searchField);
106                 topPanel.add(subPanel);
107                 dialogPanel.add(topPanel, BorderLayout.NORTH);
108
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)
114                         {
115                                 if (!e.getValueIsAdjusting()) _okButton.setEnabled(true);
116                         }});
117                 JScrollPane listPane = new JScrollPane(_pointList);
118                 dialogPanel.add(listPane, BorderLayout.CENTER);
119
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)
126                         {
127                                 finish();
128                         }
129                 };
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)
136                         {
137                                 _dialog.dispose();
138                         }
139                 });
140                 buttonPanel.add(cancelButton);
141                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
142                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
143                 return dialogPanel;
144         }
145
146         /**
147          * Finish the dialog when OK pressed
148          * Select the point and close the dialog
149          */
150         private void finish()
151         {
152                 DataPoint selectedPoint = _nameMatcher.getWaypoint(_pointList.getSelectedIndex());
153                 _app.getTrackInfo().selectPoint(selectedPoint);
154                 _dialog.dispose();
155         }
156 }