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