]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/BabelLoadFromFile.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / load / BabelLoadFromFile.java
1 package tim.prune.load;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.io.File;
9
10 import javax.swing.BorderFactory;
11 import javax.swing.BoxLayout;
12 import javax.swing.JButton;
13 import javax.swing.JCheckBox;
14 import javax.swing.JComboBox;
15 import javax.swing.JFileChooser;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JProgressBar;
19 import javax.swing.event.ChangeEvent;
20 import javax.swing.event.ChangeListener;
21
22 import tim.prune.App;
23 import tim.prune.I18nManager;
24 import tim.prune.config.Config;
25 import tim.prune.data.SourceInfo;
26 import tim.prune.data.SourceInfo.FILE_TYPE;
27 import tim.prune.gui.GuiGridLayout;
28 import tim.prune.load.babel.BabelFilterPanel;
29
30
31 /**
32  * Class to manage the loading of data from a file using GpsBabel.
33  * This allows the use of Gpsbabel's importing functions to convert to gpx.
34  */
35 public class BabelLoadFromFile extends BabelLoader
36 {
37         // file chooser
38         private JFileChooser _fileChooser = null;
39         // Input file
40         private File _inputFile = null;
41         // Label for filename
42         private JLabel _inputFileLabel = null;
43         // Dropdown for format of file
44         private JComboBox<String> _formatDropdown = null;
45         // Last used file suffix
46         private String _lastSuffix = null;
47
48         /**
49          * Constructor
50          * @param inApp Application object to inform of data load
51          */
52         public BabelLoadFromFile(App inApp) {
53                 super(inApp);
54         }
55
56         /** Get the name key */
57         public String getNameKey() {
58                 return "function.importwithgpsbabel";
59         }
60
61         /** @return complete input file path for gpsbabel call */
62         protected String getFilePath() {
63                 return _inputFile.getAbsolutePath();
64         }
65
66         /** @return Source info */
67         protected SourceInfo getSourceInfo() {
68                 return new SourceInfo(_inputFile, FILE_TYPE.GPSBABEL);
69         }
70
71         /** @return input format */
72         protected String getInputFormat() {
73                 return BabelFileFormats.getFormat(_formatDropdown.getSelectedIndex());
74         }
75
76         /** @return true if function can be run */
77         protected boolean isInputOk() {
78                 return _inputFile.exists() && _inputFile.canRead();
79         }
80
81         /**
82          * Override the begin method to specify input file first
83          */
84         public void begin()
85         {
86                 // Construct file chooser if necessary
87                 if (_fileChooser == null)
88                 {
89                         _fileChooser = new JFileChooser();
90                         // start from directory in config if already set
91                         String configDir = Config.getConfigString(Config.KEY_TRACK_DIR);
92                         if (configDir == null) {configDir = Config.getConfigString(Config.KEY_PHOTO_DIR);}
93                         if (configDir != null) {_fileChooser.setCurrentDirectory(new File(configDir));}
94                         _fileChooser.setMultiSelectionEnabled(false); // Single files only
95                 }
96                 // Show the open dialog
97                 if (_fileChooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
98                 {
99                         _inputFile = _fileChooser.getSelectedFile();
100                         if (_inputFile != null && isInputOk()) {
101                                 super.begin();
102                         }
103                 }
104         }
105
106         /**
107          * Begin the load function with a previously-specified file
108          * @param inFile file to load
109          */
110         public void beginWithFile(File inFile)
111         {
112                 _inputFile = inFile;
113                 super.begin();
114         }
115
116         /**
117          * @return a panel containing the main dialog components
118          */
119         protected JPanel makeDialogComponents()
120         {
121                 JPanel outerPanel = new JPanel();
122                 outerPanel.setLayout(new BorderLayout());
123                 // Main panel with options etc
124                 JPanel mainPanel = new JPanel();
125                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
126
127                 // text fields for options
128                 JPanel gridPanel = new JPanel();
129                 GuiGridLayout grid = new GuiGridLayout(gridPanel);
130                 JLabel nameLabel = new JLabel(I18nManager.getText("details.track.file"));
131                 grid.add(nameLabel);
132                 _inputFileLabel = new JLabel("------------");
133                 grid.add(_inputFileLabel);
134                 JLabel formatLabel = new JLabel(I18nManager.getText("dialog.gpsload.format"));
135                 grid.add(formatLabel);
136                 _formatDropdown = new JComboBox<String>(BabelFileFormats.getDescriptions());
137                 grid.add(_formatDropdown);
138                 gridPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
139                 gridPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 5, 20));
140                 mainPanel.add(gridPanel);
141
142                 // checkboxes
143                 ChangeListener checkboxListener = new ChangeListener() {
144                         public void stateChanged(ChangeEvent e)
145                         {
146                                 enableOkButton();
147                         }
148                 };
149                 _waypointCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.getwaypoints"), true);
150                 _waypointCheckbox.addChangeListener(checkboxListener);
151                 _waypointCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
152                 mainPanel.add(_waypointCheckbox);
153                 _trackCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.gettracks"), true);
154                 _trackCheckbox.addChangeListener(checkboxListener);
155                 _trackCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
156                 mainPanel.add(_trackCheckbox);
157                 // Checkbox for immediately saving to file
158                 _saveCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.save"));
159                 _saveCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
160                 mainPanel.add(_saveCheckbox);
161
162                 // Filter panel
163                 _filterPanel = new BabelFilterPanel(_parentFrame);
164                 // Give filter panel the contents of the config
165                 String filter = Config.getConfigString(Config.KEY_GPSBABEL_FILTER);
166                 if (filter != null) {
167                         _filterPanel.setFilterString(filter);
168                 }
169                 mainPanel.add(_filterPanel);
170
171                 // progress bar (initially invisible)
172                 _progressBar = new JProgressBar(0, 10);
173                 mainPanel.add(_progressBar);
174                 outerPanel.add(mainPanel, BorderLayout.NORTH);
175
176                 // Lower panel with ok and cancel buttons
177                 JPanel buttonPanel = new JPanel();
178                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
179                 _okButton = new JButton(I18nManager.getText("button.ok"));
180                 ActionListener okListener = new ActionListener() {
181                         public void actionPerformed(ActionEvent e)
182                         {
183                                 // start thread to call gpsbabel
184                                 _cancelled = false;
185                                 new Thread(BabelLoadFromFile.this).start();
186                         }
187                 };
188                 _okButton.addActionListener(okListener);
189                 buttonPanel.add(_okButton);
190                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
191                 cancelButton.addActionListener(new ActionListener() {
192                         public void actionPerformed(ActionEvent e)
193                         {
194                                 _cancelled = true;
195                                 _dialog.dispose();
196                         }
197                 });
198                 buttonPanel.add(cancelButton);
199                 outerPanel.add(buttonPanel, BorderLayout.SOUTH);
200                 return outerPanel;
201         }
202
203         /**
204          * Initialise dialog
205          */
206         protected void initDialog()
207         {
208                 _inputFileLabel.setText(_inputFile.getName());
209                 // Get suffix of filename and compare with previous one
210                 String filename = _inputFile.getName();
211                 int dotPos = filename.lastIndexOf('.');
212                 String suffix = (dotPos > 0 ? filename.substring(dotPos) : null);
213                 if (suffix != null && !suffix.equals(".") && (_lastSuffix == null || !suffix.equalsIgnoreCase(_lastSuffix)))
214                 {
215                         // New suffix chosen, so select first appropriate format (if any)
216                         int selIndex = BabelFileFormats.getIndexForFileSuffix(suffix);
217                         if (selIndex >= 0) {
218                                 _formatDropdown.setSelectedIndex(selIndex);
219                         }
220                 }
221                 _lastSuffix = suffix;
222         }
223
224         /**
225          * Save settings in config
226          */
227         protected void saveConfigValues()
228         {
229                 // Save the filter string, clear it if it's now blank
230                 final String filter = _filterPanel.getFilterString();
231                 Config.setConfigString(Config.KEY_GPSBABEL_FILTER, filter);
232         }
233 }