]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/gpsies/GenericDownloaderFunction.java
Version 14, October 2012
[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.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
29 /**
30  * Function to load track information from any source,
31  * subclassed for special cases gpsies or wikipedia
32  */
33 public abstract class GenericDownloaderFunction extends GenericFunction implements Runnable
34 {
35         /** Dialog object */
36         protected JDialog _dialog = null;
37         /** list model */
38         protected TrackListModel _trackListModel = null;
39         /** track table */
40         protected JTable _trackTable = null;
41         /** Cancelled flag */
42         protected boolean _cancelled = false;
43         /** Status label */
44         protected JLabel _statusLabel = null;
45         /** Description box */
46         private JTextArea _descriptionBox = null;
47         /** Load button */
48         private JButton _loadButton = null;
49         /** Show button */
50         private JButton _showButton = null;
51
52
53         /**
54          * Constructor
55          * @param inApp App object
56          */
57         public GenericDownloaderFunction(App inApp) {
58                 super(inApp);
59         }
60
61         /**
62          * Begin the function
63          */
64         public void begin()
65         {
66                 // Initialise dialog, show empty list
67                 if (_dialog == null)
68                 {
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) {
75                                         _cancelled = true;
76                                 }
77                         });
78                         _dialog.getContentPane().add(makeDialogComponents());
79                         _dialog.pack();
80                 }
81                 // Clear list
82                 _trackListModel.clear();
83                 _loadButton.setEnabled(false);
84                 _showButton.setEnabled(false);
85                 _cancelled = false;
86                 _descriptionBox.setText("");
87                 // Start new thread to load list asynchronously
88                 new Thread(this).start();
89
90                 // Show dialog
91                 _dialog.setVisible(true);
92         }
93
94
95         /**
96          * Create dialog components
97          * @return Panel containing all gui elements in dialog
98          */
99         private Component makeDialogComponents()
100         {
101                 JPanel dialogPanel = new JPanel();
102                 dialogPanel.setLayout(new BorderLayout());
103
104                 // Status label
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())
113                                 {
114                                         final int numSelected = _trackTable.getSelectedRowCount();
115                                         if (numSelected > 0)
116                                         {
117                                                 setDescription(_trackListModel.getTrack(_trackTable.getSelectedRow()).getDescription());
118                                                 _descriptionBox.setCaretPosition(0);
119                                         }
120                                         else {
121                                                 _descriptionBox.setText("");
122                                         }
123                                         _loadButton.setEnabled(numSelected > 0);
124                                         _showButton.setEnabled(numSelected == 1);
125                                 }
126                         }
127                 });
128                 _trackTable.getColumnModel().getColumn(0).setPreferredWidth(300);
129                 if (_trackListModel.getColumnCount() > 1) {
130                         _trackTable.getColumnModel().getColumn(1).setPreferredWidth(70);
131                 }
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);
150
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)
158                         {
159                                 loadSelected();
160                         }
161                 });
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)
167                         {
168                                 showSelectedWebpage();
169                         }
170                 });
171                 buttonPanel.add(_showButton);
172                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
173                 cancelButton.addActionListener(new ActionListener() {
174                         public void actionPerformed(ActionEvent e)
175                         {
176                                 _cancelled = true;
177                                 _dialog.dispose();
178                         }
179                 });
180                 buttonPanel.add(cancelButton);
181                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
182                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
183                 return dialogPanel;
184         }
185
186         /**
187          * @param inColNum index of column, 0 or 1
188          * @return key for this column
189          */
190         protected abstract String getColumnKey(int inColNum);
191
192         /**
193          * Set the description in the box
194          * @param inDesc description to set, or null for no description
195          */
196         private void setDescription(String inDesc)
197         {
198                 String text = inDesc;
199                 if (inDesc == null || inDesc.length() < 2) {
200                         text = I18nManager.getText("dialog.gpsies.nodescription");
201                 }
202                 _descriptionBox.setText(text);
203         }
204
205
206         /**
207          * Load the selected track or point
208          */
209         protected abstract void loadSelected();
210
211
212         /**
213          * Show the webpage for the selected item
214          */
215         private void showSelectedWebpage()
216         {
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())
220                 {
221                         String url = _trackListModel.getTrack(rowNum).getWebUrl();
222                         BrowserLauncher.launchBrowser(url);
223                 }
224                 // Don't close the dialog
225         }
226 }