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;
31 * Class to manage the loading of data from a file using GpsBabel.
32 * This allows the use of Gpsbabel's importing functions to convert to gpx.
34 public class BabelLoadFromFile extends BabelLoader
37 private JFileChooser _fileChooser = null;
39 private File _inputFile = null;
41 private JLabel _inputFileLabel = null;
42 // Dropdown for format of file
43 private JComboBox _formatDropdown = null;
44 // Last used file suffix
45 private String _lastSuffix = null;
49 * @param inApp Application object to inform of data load
51 public BabelLoadFromFile(App inApp) {
55 /** Get the name key */
56 public String getNameKey() {
57 return "function.importwithgpsbabel";
60 /** @return complete input file path for gpsbabel call */
61 protected String getFilePath() {
62 return _inputFile.getAbsolutePath();
65 /** @return Source info */
66 protected SourceInfo getSourceInfo() {
67 return new SourceInfo(_inputFile, FILE_TYPE.GPSBABEL);
70 /** @return input format */
71 protected String getInputFormat() {
72 return BabelFileFormats.getFormat(_formatDropdown.getSelectedIndex());
75 /** @return true if function can be run */
76 protected boolean isInputOk() {
77 return _inputFile.exists() && _inputFile.canRead();
81 * Override the begin method to specify input file first
85 // Construct file chooser if necessary
86 if (_fileChooser == null)
88 _fileChooser = new JFileChooser();
89 // start from directory in config if already set
90 String configDir = Config.getConfigString(Config.KEY_TRACK_DIR);
91 if (configDir == null) {configDir = Config.getConfigString(Config.KEY_PHOTO_DIR);}
92 if (configDir != null) {_fileChooser.setCurrentDirectory(new File(configDir));}
93 _fileChooser.setMultiSelectionEnabled(false); // Single files only
95 // Show the open dialog
96 if (_fileChooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
98 _inputFile = _fileChooser.getSelectedFile();
99 if (_inputFile != null && isInputOk()) {
106 * Begin the load function with a previously-specified file
107 * @param inFile file to load
109 public void beginWithFile(File inFile)
116 * @return a panel containing the main dialog components
118 protected JPanel makeDialogComponents()
120 JPanel outerPanel = new JPanel();
121 outerPanel.setLayout(new BorderLayout());
122 // Main panel with options etc
123 JPanel mainPanel = new JPanel();
124 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
126 // text fields for options
127 JPanel gridPanel = new JPanel();
128 GuiGridLayout grid = new GuiGridLayout(gridPanel);
129 JLabel nameLabel = new JLabel(I18nManager.getText("details.track.file"));
131 _inputFileLabel = new JLabel("------------");
132 grid.add(_inputFileLabel);
133 JLabel formatLabel = new JLabel(I18nManager.getText("dialog.gpsload.format"));
134 grid.add(formatLabel);
135 _formatDropdown = new JComboBox(BabelFileFormats.getDescriptions());
136 grid.add(_formatDropdown);
137 gridPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
138 gridPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 5, 20));
139 mainPanel.add(gridPanel);
142 ChangeListener checkboxListener = new ChangeListener() {
143 public void stateChanged(ChangeEvent e)
148 _waypointCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.getwaypoints"), true);
149 _waypointCheckbox.addChangeListener(checkboxListener);
150 _waypointCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
151 mainPanel.add(_waypointCheckbox);
152 _trackCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.gettracks"), true);
153 _trackCheckbox.addChangeListener(checkboxListener);
154 _trackCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
155 mainPanel.add(_trackCheckbox);
156 // Checkbox for immediately saving to file
157 _saveCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.save"));
158 _saveCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
159 mainPanel.add(_saveCheckbox);
161 // progress bar (initially invisible)
162 _progressBar = new JProgressBar(0, 10);
163 mainPanel.add(_progressBar);
164 outerPanel.add(mainPanel, BorderLayout.NORTH);
166 // Lower panel with ok and cancel buttons
167 JPanel buttonPanel = new JPanel();
168 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
169 _okButton = new JButton(I18nManager.getText("button.ok"));
170 ActionListener okListener = new ActionListener() {
171 public void actionPerformed(ActionEvent e)
173 // start thread to call gpsbabel
175 new Thread(BabelLoadFromFile.this).start();
178 _okButton.addActionListener(okListener);
179 buttonPanel.add(_okButton);
180 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
181 cancelButton.addActionListener(new ActionListener() {
182 public void actionPerformed(ActionEvent e)
188 buttonPanel.add(cancelButton);
189 outerPanel.add(buttonPanel, BorderLayout.SOUTH);
196 protected void initDialog()
198 _inputFileLabel.setText(_inputFile.getName());
199 // Get suffix of filename and compare with previous one
200 String filename = _inputFile.getName();
201 int dotPos = filename.lastIndexOf('.');
202 String suffix = (dotPos > 0 ? filename.substring(dotPos) : null);
203 if (suffix != null && !suffix.equals(".") && (_lastSuffix == null || !suffix.equalsIgnoreCase(_lastSuffix)))
205 // New suffix chosen, so select first appropriate format (if any)
206 int selIndex = BabelFileFormats.getIndexForFileSuffix(suffix);
208 _formatDropdown.setSelectedIndex(selIndex);
211 _lastSuffix = suffix;
215 * Save settings in config
217 protected void saveConfigValues()