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