1 package tim.prune.function.gpsies;
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 gpsies or wikipedia
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 JLabel _statusLabel = null;
45 /** Description box */
46 private JTextArea _descriptionBox = null;
48 private JButton _loadButton = null;
50 private JButton _showButton = null;
55 * @param inApp App object
57 public GenericDownloaderFunction(App inApp) {
66 // Initialise dialog, show empty list
69 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
70 _dialog.setLocationRelativeTo(_parentFrame);
71 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
72 // add closing listener
73 _dialog.addWindowListener(new WindowAdapter() {
74 public void windowClosing(WindowEvent e) {
78 _dialog.getContentPane().add(makeDialogComponents());
82 _trackListModel.clear();
83 _loadButton.setEnabled(false);
84 _showButton.setEnabled(false);
86 _descriptionBox.setText("");
87 // Start new thread to load list asynchronously
88 new Thread(this).start();
91 _dialog.setVisible(true);
96 * Create dialog components
97 * @return Panel containing all gui elements in dialog
99 private Component makeDialogComponents()
101 JPanel dialogPanel = new JPanel();
102 dialogPanel.setLayout(new BorderLayout());
105 _statusLabel = new JLabel(I18nManager.getText("confirm.running"));
106 dialogPanel.add(_statusLabel, BorderLayout.NORTH);
107 // Main panel with track list
108 _trackListModel = new TrackListModel(getColumnKey(0), getColumnKey(1));
109 _trackTable = new JTable(_trackListModel);
110 _trackTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
111 public void valueChanged(ListSelectionEvent e) {
112 if (!e.getValueIsAdjusting())
114 final int numSelected = _trackTable.getSelectedRowCount();
117 setDescription(_trackListModel.getTrack(_trackTable.getSelectedRow()).getDescription());
118 _descriptionBox.setCaretPosition(0);
121 _descriptionBox.setText("");
123 _loadButton.setEnabled(numSelected > 0);
124 _showButton.setEnabled(numSelected == 1);
128 _trackTable.getColumnModel().getColumn(0).setPreferredWidth(300);
129 if (_trackListModel.getColumnCount() > 1) {
130 _trackTable.getColumnModel().getColumn(1).setPreferredWidth(70);
132 JScrollPane tablePane = new JScrollPane(_trackTable);
133 tablePane.setPreferredSize(new Dimension(450, 200));
134 // Panel to hold description label and box
135 JPanel descPanel = new JPanel();
136 descPanel.setLayout(new BorderLayout());
137 JLabel descLabel = new JLabel(I18nManager.getText("dialog.gpsies.description") + " :");
138 descPanel.add(descLabel, BorderLayout.NORTH);
139 _descriptionBox = new JTextArea(5, 20);
140 _descriptionBox.setEditable(false);
141 _descriptionBox.setLineWrap(true);
142 _descriptionBox.setWrapStyleWord(true);
143 JScrollPane descPane = new JScrollPane(_descriptionBox);
144 descPane.setPreferredSize(new Dimension(400, 80));
145 descPanel.add(descPane, BorderLayout.CENTER);
146 // Use split pane to split table from description
147 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tablePane, descPanel);
148 splitPane.setResizeWeight(1.0);
149 dialogPanel.add(splitPane, BorderLayout.CENTER);
151 // button panel at bottom
152 JPanel buttonPanel = new JPanel();
153 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
154 _loadButton = new JButton(I18nManager.getText("button.load"));
155 _loadButton.setEnabled(false);
156 _loadButton.addActionListener(new ActionListener() {
157 public void actionPerformed(ActionEvent e)
162 buttonPanel.add(_loadButton);
163 _showButton = new JButton(I18nManager.getText("button.showwebpage"));
164 _showButton.setEnabled(false);
165 _showButton.addActionListener(new ActionListener() {
166 public void actionPerformed(ActionEvent e)
168 showSelectedWebpage();
171 buttonPanel.add(_showButton);
172 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
173 cancelButton.addActionListener(new ActionListener() {
174 public void actionPerformed(ActionEvent e)
180 buttonPanel.add(cancelButton);
181 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
182 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
187 * @param inColNum index of column, 0 or 1
188 * @return key for this column
190 protected abstract String getColumnKey(int inColNum);
193 * Set the description in the box
194 * @param inDesc description to set, or null for no description
196 private void setDescription(String inDesc)
198 String text = inDesc;
199 if (inDesc == null || inDesc.length() < 2) {
200 text = I18nManager.getText("dialog.gpsies.nodescription");
202 _descriptionBox.setText(text);
207 * Load the selected track or point
209 protected abstract void loadSelected();
213 * Show the webpage for the selected item
215 private void showSelectedWebpage()
217 // Find the row selected in the table and show the corresponding url
218 int rowNum = _trackTable.getSelectedRow();
219 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
221 String url = _trackListModel.getTrack(rowNum).getWebUrl();
222 BrowserLauncher.launchBrowser(url);
224 // Don't close the dialog