]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/BabelLoadFromFile.java
Version 13, August 2011
[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
29
30 /**
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.
33  */
34 public class BabelLoadFromFile extends BabelLoader
35 {
36         // file chooser
37         private JFileChooser _fileChooser = null;
38         // Input file
39         private File _inputFile = null;
40         // Label for filename
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;
46
47         /**
48          * Constructor
49          * @param inApp Application object to inform of data load
50          */
51         public BabelLoadFromFile(App inApp) {
52                 super(inApp);
53         }
54
55         /** Get the name key */
56         public String getNameKey() {
57                 return "function.importwithgpsbabel";
58         }
59
60         /** @return complete input file path for gpsbabel call */
61         protected String getFilePath() {
62                 return _inputFile.getAbsolutePath();
63         }
64
65         /** @return Source info */
66         protected SourceInfo getSourceInfo() {
67                 return new SourceInfo(_inputFile, FILE_TYPE.GPSBABEL);
68         }
69
70         /** @return input format */
71         protected String getInputFormat() {
72                 return BabelFileFormats.getFormat(_formatDropdown.getSelectedIndex());
73         }
74
75         /** @return true if function can be run */
76         protected boolean isInputOk() {
77                 return _inputFile.exists() && _inputFile.canRead();
78         }
79
80         /**
81          * Override the begin method to specify input file first
82          */
83         public void begin()
84         {
85                 // Construct file chooser if necessary
86                 if (_fileChooser == null)
87                 {
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
94                 }
95                 // Show the open dialog
96                 if (_fileChooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
97                 {
98                         _inputFile = _fileChooser.getSelectedFile();
99                         if (_inputFile != null && isInputOk()) {
100                                 super.begin();
101                         }
102                 }
103         }
104
105         /**
106          * Begin the load function with a previously-specified file
107          * @param inFile file to load
108          */
109         public void beginWithFile(File inFile)
110         {
111                 _inputFile = inFile;
112                 super.begin();
113         }
114
115         /**
116          * @return a panel containing the main dialog components
117          */
118         protected JPanel makeDialogComponents()
119         {
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));
125
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"));
130                 grid.add(nameLabel);
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);
140
141                 // checkboxes
142                 ChangeListener checkboxListener = new ChangeListener() {
143                         public void stateChanged(ChangeEvent e)
144                         {
145                                 enableOkButton();
146                         }
147                 };
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);
160
161                 // progress bar (initially invisible)
162                 _progressBar = new JProgressBar(0, 10);
163                 mainPanel.add(_progressBar);
164                 outerPanel.add(mainPanel, BorderLayout.NORTH);
165
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)
172                         {
173                                 // start thread to call gpsbabel
174                                 _cancelled = false;
175                                 new Thread(BabelLoadFromFile.this).start();
176                         }
177                 };
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)
183                         {
184                                 _cancelled = true;
185                                 _dialog.dispose();
186                         }
187                 });
188                 buttonPanel.add(cancelButton);
189                 outerPanel.add(buttonPanel, BorderLayout.SOUTH);
190                 return outerPanel;
191         }
192
193         /**
194          * Initialise dialog
195          */
196         protected void initDialog()
197         {
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)))
204                 {
205                         // New suffix chosen, so select first appropriate format (if any)
206                         int selIndex = BabelFileFormats.getIndexForFileSuffix(suffix);
207                         if (selIndex >= 0) {
208                                 _formatDropdown.setSelectedIndex(selIndex);
209                         }
210                 }
211                 _lastSuffix = suffix;
212         }
213
214         /**
215          * Save settings in config
216          */
217         protected void saveConfigValues()
218         {
219                 // nothing needed
220         }
221 }