1 package tim.prune.function.search;
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.WindowAdapter;
10 import java.awt.event.WindowEvent;
12 import javax.swing.BorderFactory;
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JScrollPane;
18 import javax.swing.JSplitPane;
19 import javax.swing.JTable;
20 import javax.swing.JTextArea;
21 import javax.swing.event.ListSelectionEvent;
22 import javax.swing.event.ListSelectionListener;
25 import tim.prune.GenericFunction;
26 import tim.prune.I18nManager;
27 import tim.prune.function.browser.BrowserLauncher;
30 * Function to load track information from any source,
31 * subclassed for special cases like wikipedia or OSM
33 public abstract class GenericDownloaderFunction extends GenericFunction implements Runnable
36 protected JDialog _dialog = null;
38 protected TrackListModel _trackListModel = null;
40 protected JTable _trackTable = null;
42 protected boolean _cancelled = false;
44 protected String _errorMessage = null;
46 protected JLabel _statusLabel = null;
47 /** Description box */
48 private JTextArea _descriptionBox = null;
50 private JButton _loadButton = null;
52 private JButton _showButton = null;
57 * @param inApp App object
59 public GenericDownloaderFunction(App inApp) {
68 // Initialise dialog, show empty list
71 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
72 _dialog.setLocationRelativeTo(_parentFrame);
73 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
74 // add closing listener
75 _dialog.addWindowListener(new WindowAdapter() {
76 public void windowClosing(WindowEvent e) {
80 _dialog.getContentPane().add(makeDialogComponents());
84 _trackListModel.clear();
85 _loadButton.setEnabled(false);
86 _showButton.setEnabled(false);
88 _descriptionBox.setText("");
90 // Start new thread to load list asynchronously
91 new Thread(this).start();
94 _dialog.setVisible(true);
99 * Create dialog components
100 * @return Panel containing all gui elements in dialog
102 private Component makeDialogComponents()
104 JPanel dialogPanel = new JPanel();
105 dialogPanel.setLayout(new BorderLayout());
108 _statusLabel = new JLabel(I18nManager.getText("confirm.running"));
109 dialogPanel.add(_statusLabel, BorderLayout.NORTH);
110 // Main panel with track list
111 _trackListModel = new TrackListModel(getColumnKey(0), getColumnKey(1));
112 _trackTable = new JTable(_trackListModel);
113 _trackTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
114 public void valueChanged(ListSelectionEvent e) {
115 if (!e.getValueIsAdjusting())
117 final int numSelected = _trackTable.getSelectedRowCount();
118 boolean foundUrl = false;
121 setDescription(_trackListModel.getTrack(_trackTable.getSelectedRow()).getDescription());
122 _descriptionBox.setCaretPosition(0);
123 foundUrl = _trackListModel.getTrack(_trackTable.getSelectedRow()).getWebUrl() != null;
126 _descriptionBox.setText("");
128 _loadButton.setEnabled(numSelected > 0);
129 _showButton.setEnabled(numSelected == 1 && foundUrl);
133 _trackTable.getColumnModel().getColumn(0).setPreferredWidth(300);
134 if (_trackListModel.getColumnCount() > 1) {
135 _trackTable.getColumnModel().getColumn(1).setPreferredWidth(70);
137 JScrollPane tablePane = new JScrollPane(_trackTable);
138 tablePane.setPreferredSize(new Dimension(450, 200));
139 // Panel to hold description label and box
140 JPanel descPanel = new JPanel();
141 descPanel.setLayout(new BorderLayout());
142 JLabel descLabel = new JLabel(I18nManager.getText("dialog.gpsies.description") + " :");
143 descPanel.add(descLabel, BorderLayout.NORTH);
144 _descriptionBox = new JTextArea(5, 20);
145 _descriptionBox.setEditable(false);
146 _descriptionBox.setLineWrap(true);
147 _descriptionBox.setWrapStyleWord(true);
148 JScrollPane descPane = new JScrollPane(_descriptionBox);
149 descPane.setPreferredSize(new Dimension(400, 80));
150 descPanel.add(descPane, BorderLayout.CENTER);
151 // Use split pane to split table from description
152 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tablePane, descPanel);
153 splitPane.setResizeWeight(1.0);
154 dialogPanel.add(splitPane, BorderLayout.CENTER);
156 // button panel at bottom
157 JPanel buttonPanel = new JPanel();
158 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
159 _loadButton = new JButton(I18nManager.getText("button.load"));
160 _loadButton.setEnabled(false);
161 _loadButton.addActionListener(new ActionListener() {
162 public void actionPerformed(ActionEvent e)
167 buttonPanel.add(_loadButton);
168 _showButton = new JButton(I18nManager.getText("button.showwebpage"));
169 _showButton.setEnabled(false);
170 _showButton.addActionListener(new ActionListener() {
171 public void actionPerformed(ActionEvent e)
173 showSelectedWebpage();
176 buttonPanel.add(_showButton);
177 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
178 cancelButton.addActionListener(new ActionListener() {
179 public void actionPerformed(ActionEvent e)
185 buttonPanel.add(cancelButton);
186 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
187 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
192 * @param inColNum index of column, 0 or 1
193 * @return key for this column
195 protected abstract String getColumnKey(int inColNum);
198 * Set the description in the box
199 * @param inDesc description to set, or null for no description
201 private void setDescription(String inDesc)
203 String text = inDesc;
204 if (inDesc == null || inDesc.length() < 2) {
205 text = I18nManager.getText("dialog.gpsies.nodescription");
207 _descriptionBox.setText(text);
212 * Load the selected track or point
214 protected abstract void loadSelected();
218 * Show the webpage for the selected item
220 private void showSelectedWebpage()
222 // Find the row selected in the table and show the corresponding url
223 int rowNum = _trackTable.getSelectedRow();
224 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
226 String url = _trackListModel.getTrack(rowNum).getWebUrl();
227 BrowserLauncher.launchBrowser(url);
229 // Don't close the dialog