1 package tim.prune.load;
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;
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;
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;
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.
35 public class BabelLoadFromFile extends BabelLoader
38 private JFileChooser _fileChooser = null;
40 private File _inputFile = null;
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;
50 * @param inApp Application object to inform of data load
52 public BabelLoadFromFile(App inApp) {
56 /** Get the name key */
57 public String getNameKey() {
58 return "function.importwithgpsbabel";
61 /** @return complete input file path for gpsbabel call */
62 protected String getFilePath() {
63 return _inputFile.getAbsolutePath();
66 /** @return Source info */
67 protected SourceInfo getSourceInfo() {
68 return new SourceInfo(_inputFile, FILE_TYPE.GPSBABEL);
71 /** @return input format */
72 protected String getInputFormat() {
73 return BabelFileFormats.getFormat(_formatDropdown.getSelectedIndex());
76 /** @return true if function can be run */
77 protected boolean isInputOk() {
78 return _inputFile.exists() && _inputFile.canRead();
82 * Override the begin method to specify input file first
86 // Construct file chooser if necessary
87 if (_fileChooser == null)
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
96 // Show the open dialog
97 if (_fileChooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
99 _inputFile = _fileChooser.getSelectedFile();
100 if (_inputFile != null && isInputOk()) {
107 * Begin the load function with a previously-specified file
108 * @param inFile file to load
110 public void beginWithFile(File inFile)
117 * @return a panel containing the main dialog components
119 protected JPanel makeDialogComponents()
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));
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"));
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);
143 ChangeListener checkboxListener = new ChangeListener() {
144 public void stateChanged(ChangeEvent e)
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);
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);
169 mainPanel.add(_filterPanel);
171 // progress bar (initially invisible)
172 _progressBar = new JProgressBar(0, 10);
173 mainPanel.add(_progressBar);
174 outerPanel.add(mainPanel, BorderLayout.NORTH);
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)
183 // start thread to call gpsbabel
185 new Thread(BabelLoadFromFile.this).start();
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)
198 buttonPanel.add(cancelButton);
199 outerPanel.add(buttonPanel, BorderLayout.SOUTH);
206 protected void initDialog()
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)))
215 // New suffix chosen, so select first appropriate format (if any)
216 int selIndex = BabelFileFormats.getIndexForFileSuffix(suffix);
218 _formatDropdown.setSelectedIndex(selIndex);
221 _lastSuffix = suffix;
225 * Save settings in config
227 protected void saveConfigValues()
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);