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;
28 import tim.prune.function.gpsies.TrackListModel;
31 * Function to load track information from any source,
32 * subclassed for special cases gpsies or wikipedia
34 public abstract class GenericDownloaderFunction extends GenericFunction implements Runnable
37 protected JDialog _dialog = null;
39 protected TrackListModel _trackListModel = null;
41 protected JTable _trackTable = null;
43 protected boolean _cancelled = false;
45 protected String _errorMessage = null;
47 protected JLabel _statusLabel = null;
48 /** Description box */
49 private JTextArea _descriptionBox = null;
51 private JButton _loadButton = null;
53 private JButton _showButton = null;
58 * @param inApp App object
60 public GenericDownloaderFunction(App inApp) {
69 // Initialise dialog, show empty list
72 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
73 _dialog.setLocationRelativeTo(_parentFrame);
74 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
75 // add closing listener
76 _dialog.addWindowListener(new WindowAdapter() {
77 public void windowClosing(WindowEvent e) {
81 _dialog.getContentPane().add(makeDialogComponents());
85 _trackListModel.clear();
86 _loadButton.setEnabled(false);
87 _showButton.setEnabled(false);
89 _descriptionBox.setText("");
91 // Start new thread to load list asynchronously
92 new Thread(this).start();
95 _dialog.setVisible(true);
100 * Create dialog components
101 * @return Panel containing all gui elements in dialog
103 private Component makeDialogComponents()
105 JPanel dialogPanel = new JPanel();
106 dialogPanel.setLayout(new BorderLayout());
109 _statusLabel = new JLabel(I18nManager.getText("confirm.running"));
110 dialogPanel.add(_statusLabel, BorderLayout.NORTH);
111 // Main panel with track list
112 _trackListModel = new TrackListModel(getColumnKey(0), getColumnKey(1));
113 _trackTable = new JTable(_trackListModel);
114 _trackTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
115 public void valueChanged(ListSelectionEvent e) {
116 if (!e.getValueIsAdjusting())
118 final int numSelected = _trackTable.getSelectedRowCount();
121 setDescription(_trackListModel.getTrack(_trackTable.getSelectedRow()).getDescription());
122 _descriptionBox.setCaretPosition(0);
125 _descriptionBox.setText("");
127 _loadButton.setEnabled(numSelected > 0);
128 _showButton.setEnabled(numSelected == 1);
132 _trackTable.getColumnModel().getColumn(0).setPreferredWidth(300);
133 if (_trackListModel.getColumnCount() > 1) {
134 _trackTable.getColumnModel().getColumn(1).setPreferredWidth(70);
136 JScrollPane tablePane = new JScrollPane(_trackTable);
137 tablePane.setPreferredSize(new Dimension(450, 200));
138 // Panel to hold description label and box
139 JPanel descPanel = new JPanel();
140 descPanel.setLayout(new BorderLayout());
141 JLabel descLabel = new JLabel(I18nManager.getText("dialog.gpsies.description") + " :");
142 descPanel.add(descLabel, BorderLayout.NORTH);
143 _descriptionBox = new JTextArea(5, 20);
144 _descriptionBox.setEditable(false);
145 _descriptionBox.setLineWrap(true);
146 _descriptionBox.setWrapStyleWord(true);
147 JScrollPane descPane = new JScrollPane(_descriptionBox);
148 descPane.setPreferredSize(new Dimension(400, 80));
149 descPanel.add(descPane, BorderLayout.CENTER);
150 // Use split pane to split table from description
151 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tablePane, descPanel);
152 splitPane.setResizeWeight(1.0);
153 dialogPanel.add(splitPane, BorderLayout.CENTER);
155 // button panel at bottom
156 JPanel buttonPanel = new JPanel();
157 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
158 _loadButton = new JButton(I18nManager.getText("button.load"));
159 _loadButton.setEnabled(false);
160 _loadButton.addActionListener(new ActionListener() {
161 public void actionPerformed(ActionEvent e)
166 buttonPanel.add(_loadButton);
167 _showButton = new JButton(I18nManager.getText("button.showwebpage"));
168 _showButton.setEnabled(false);
169 _showButton.addActionListener(new ActionListener() {
170 public void actionPerformed(ActionEvent e)
172 showSelectedWebpage();
175 buttonPanel.add(_showButton);
176 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
177 cancelButton.addActionListener(new ActionListener() {
178 public void actionPerformed(ActionEvent e)
184 buttonPanel.add(cancelButton);
185 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
186 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
191 * @param inColNum index of column, 0 or 1
192 * @return key for this column
194 protected abstract String getColumnKey(int inColNum);
197 * Set the description in the box
198 * @param inDesc description to set, or null for no description
200 private void setDescription(String inDesc)
202 String text = inDesc;
203 if (inDesc == null || inDesc.length() < 2) {
204 text = I18nManager.getText("dialog.gpsies.nodescription");
206 _descriptionBox.setText(text);
211 * Load the selected track or point
213 protected abstract void loadSelected();
217 * Show the webpage for the selected item
219 private void showSelectedWebpage()
221 // Find the row selected in the table and show the corresponding url
222 int rowNum = _trackTable.getSelectedRow();
223 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
225 String url = _trackListModel.getTrack(rowNum).getWebUrl();
226 BrowserLauncher.launchBrowser(url);
228 // Don't close the dialog