]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/search/GenericDownloaderFunction.java
fa8d4c33d90bf43838f19f039d9b510226fccf51
[GpsPrune.git] / src / tim / prune / function / search / GenericDownloaderFunction.java
1 package tim.prune.function.search;
2
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;
11
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;
23
24 import tim.prune.App;
25 import tim.prune.GenericFunction;
26 import tim.prune.I18nManager;
27 import tim.prune.function.browser.BrowserLauncher;
28 import tim.prune.function.gpsies.TrackListModel;
29
30 /**
31  * Function to load track information from any source,
32  * subclassed for special cases like gpsies, wikipedia or OSM
33  */
34 public abstract class GenericDownloaderFunction extends GenericFunction implements Runnable
35 {
36         /** Dialog object */
37         protected JDialog _dialog = null;
38         /** list model */
39         protected TrackListModel _trackListModel = null;
40         /** track table */
41         protected JTable _trackTable = null;
42         /** Cancelled flag */
43         protected boolean _cancelled = false;
44         /** error message */
45         protected String _errorMessage = null;
46         /** Status label */
47         protected JLabel _statusLabel = null;
48         /** Description box */
49         private JTextArea _descriptionBox = null;
50         /** Load button */
51         private JButton _loadButton = null;
52         /** Show button */
53         private JButton _showButton = null;
54
55
56         /**
57          * Constructor
58          * @param inApp App object
59          */
60         public GenericDownloaderFunction(App inApp) {
61                 super(inApp);
62         }
63
64         /**
65          * Begin the function
66          */
67         public void begin()
68         {
69                 // Initialise dialog, show empty list
70                 if (_dialog == null)
71                 {
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) {
78                                         _cancelled = true;
79                                 }
80                         });
81                         _dialog.getContentPane().add(makeDialogComponents());
82                         _dialog.pack();
83                 }
84                 // Clear list
85                 _trackListModel.clear();
86                 _loadButton.setEnabled(false);
87                 _showButton.setEnabled(false);
88                 _cancelled = false;
89                 _descriptionBox.setText("");
90                 _errorMessage = null;
91                 // Start new thread to load list asynchronously
92                 new Thread(this).start();
93
94                 // Show dialog
95                 _dialog.setVisible(true);
96         }
97
98
99         /**
100          * Create dialog components
101          * @return Panel containing all gui elements in dialog
102          */
103         private Component makeDialogComponents()
104         {
105                 JPanel dialogPanel = new JPanel();
106                 dialogPanel.setLayout(new BorderLayout());
107
108                 // Status label
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())
117                                 {
118                                         final int numSelected = _trackTable.getSelectedRowCount();
119                                         boolean foundUrl = false;
120                                         if (numSelected > 0)
121                                         {
122                                                 setDescription(_trackListModel.getTrack(_trackTable.getSelectedRow()).getDescription());
123                                                 _descriptionBox.setCaretPosition(0);
124                                                 foundUrl = _trackListModel.getTrack(_trackTable.getSelectedRow()).getWebUrl() != null;
125                                         }
126                                         else {
127                                                 _descriptionBox.setText("");
128                                         }
129                                         _loadButton.setEnabled(numSelected > 0);
130                                         _showButton.setEnabled(numSelected == 1 && foundUrl);
131                                 }
132                         }
133                 });
134                 _trackTable.getColumnModel().getColumn(0).setPreferredWidth(300);
135                 if (_trackListModel.getColumnCount() > 1) {
136                         _trackTable.getColumnModel().getColumn(1).setPreferredWidth(70);
137                 }
138                 JScrollPane tablePane = new JScrollPane(_trackTable);
139                 tablePane.setPreferredSize(new Dimension(450, 200));
140                 // Panel to hold description label and box
141                 JPanel descPanel = new JPanel();
142                 descPanel.setLayout(new BorderLayout());
143                 JLabel descLabel = new JLabel(I18nManager.getText("dialog.gpsies.description") + " :");
144                 descPanel.add(descLabel, BorderLayout.NORTH);
145                 _descriptionBox = new JTextArea(5, 20);
146                 _descriptionBox.setEditable(false);
147                 _descriptionBox.setLineWrap(true);
148                 _descriptionBox.setWrapStyleWord(true);
149                 JScrollPane descPane = new JScrollPane(_descriptionBox);
150                 descPane.setPreferredSize(new Dimension(400, 80));
151                 descPanel.add(descPane, BorderLayout.CENTER);
152                 // Use split pane to split table from description
153                 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tablePane, descPanel);
154                 splitPane.setResizeWeight(1.0);
155                 dialogPanel.add(splitPane, BorderLayout.CENTER);
156
157                 // button panel at bottom
158                 JPanel buttonPanel = new JPanel();
159                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
160                 _loadButton = new JButton(I18nManager.getText("button.load"));
161                 _loadButton.setEnabled(false);
162                 _loadButton.addActionListener(new ActionListener() {
163                         public void actionPerformed(ActionEvent e)
164                         {
165                                 loadSelected();
166                         }
167                 });
168                 buttonPanel.add(_loadButton);
169                 _showButton = new JButton(I18nManager.getText("button.showwebpage"));
170                 _showButton.setEnabled(false);
171                 _showButton.addActionListener(new ActionListener() {
172                         public void actionPerformed(ActionEvent e)
173                         {
174                                 showSelectedWebpage();
175                         }
176                 });
177                 buttonPanel.add(_showButton);
178                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
179                 cancelButton.addActionListener(new ActionListener() {
180                         public void actionPerformed(ActionEvent e)
181                         {
182                                 _cancelled = true;
183                                 _dialog.dispose();
184                         }
185                 });
186                 buttonPanel.add(cancelButton);
187                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
188                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
189                 return dialogPanel;
190         }
191
192         /**
193          * @param inColNum index of column, 0 or 1
194          * @return key for this column
195          */
196         protected abstract String getColumnKey(int inColNum);
197
198         /**
199          * Set the description in the box
200          * @param inDesc description to set, or null for no description
201          */
202         private void setDescription(String inDesc)
203         {
204                 String text = inDesc;
205                 if (inDesc == null || inDesc.length() < 2) {
206                         text = I18nManager.getText("dialog.gpsies.nodescription");
207                 }
208                 _descriptionBox.setText(text);
209         }
210
211
212         /**
213          * Load the selected track or point
214          */
215         protected abstract void loadSelected();
216
217
218         /**
219          * Show the webpage for the selected item
220          */
221         private void showSelectedWebpage()
222         {
223                 // Find the row selected in the table and show the corresponding url
224                 int rowNum = _trackTable.getSelectedRow();
225                 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
226                 {
227                         String url = _trackListModel.getTrack(rowNum).getWebUrl();
228                         BrowserLauncher.launchBrowser(url);
229                 }
230                 // Don't close the dialog
231         }
232 }