]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/BabelLoadFromGps.java
130ca793dd30bc118fb637fa485971619ab73241
[GpsPrune.git] / tim / prune / load / BabelLoadFromGps.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.GridLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.KeyAdapter;
10 import java.awt.event.KeyEvent;
11
12 import javax.swing.BorderFactory;
13 import javax.swing.BoxLayout;
14 import javax.swing.JButton;
15 import javax.swing.JCheckBox;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JProgressBar;
19 import javax.swing.JTextField;
20 import javax.swing.SwingConstants;
21 import javax.swing.event.ChangeEvent;
22 import javax.swing.event.ChangeListener;
23
24 import tim.prune.App;
25 import tim.prune.I18nManager;
26 import tim.prune.config.Config;
27 import tim.prune.data.SourceInfo;
28 import tim.prune.data.SourceInfo.FILE_TYPE;
29 import tim.prune.load.babel.BabelFilterPanel;
30
31 /**
32  * Class to manage the loading of data from a GPS device using GpsBabel
33  */
34 public class BabelLoadFromGps extends BabelLoader
35 {
36         // Text fields for entering device and format
37         private JTextField _deviceField = null, _formatField = null;
38
39
40         /**
41          * Constructor
42          * @param inApp Application object to inform of data load
43          */
44         public BabelLoadFromGps(App inApp) {
45                 super(inApp);
46         }
47
48         /** Get the name key */
49         public String getNameKey() {
50                 return "function.loadfromgps";
51         }
52
53         /** @return device name as file path */
54         protected String getFilePath() {
55                 return _deviceField.getText();
56         }
57
58         /** @return Source info */
59         protected SourceInfo getSourceInfo() {
60                 return new SourceInfo(_deviceField.getText(), FILE_TYPE.GPSBABEL);
61         }
62
63         /** @return input format */
64         protected String getInputFormat() {
65                 return _formatField.getText();
66         }
67
68         /** @return true if function can be run */
69         protected boolean isInputOk() {
70                 return _waypointCheckbox.isSelected() || _trackCheckbox.isSelected();
71         }
72
73         /**
74          * @return a panel containing the main dialog components
75          */
76         protected JPanel makeDialogComponents()
77         {
78                 JPanel outerPanel = new JPanel();
79                 outerPanel.setLayout(new BorderLayout());
80                 // Main panel with options etc
81                 JPanel mainPanel = new JPanel();
82                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
83
84                 // text fields for options
85                 JPanel gridPanel = new JPanel();
86                 gridPanel.setLayout(new GridLayout(0, 2, 10, 3));
87                 JLabel deviceLabel = new JLabel(I18nManager.getText("dialog.gpsload.device"));
88                 deviceLabel.setHorizontalAlignment(SwingConstants.RIGHT);
89                 gridPanel.add(deviceLabel);
90                 _deviceField = new JTextField(Config.getConfigString(Config.KEY_GPS_DEVICE), 12);
91                 KeyAdapter escapeListener = new KeyAdapter() {
92                         public void keyReleased(KeyEvent e)
93                         {
94                                 // close dialog if escape pressed
95                                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
96                                         _dialog.dispose();
97                                 }
98                         }
99                 };
100                 _deviceField.addKeyListener(escapeListener);
101                 gridPanel.add(_deviceField);
102                 JLabel formatLabel = new JLabel(I18nManager.getText("dialog.gpsload.format"));
103                 formatLabel.setHorizontalAlignment(SwingConstants.RIGHT);
104                 gridPanel.add(formatLabel);
105                 _formatField = new JTextField(Config.getConfigString(Config.KEY_GPS_FORMAT), 12);
106                 _formatField.addKeyListener(escapeListener);
107                 gridPanel.add(_formatField);
108                 gridPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
109                 gridPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 5, 20));
110                 mainPanel.add(gridPanel);
111
112                 // checkboxes
113                 ChangeListener checkboxListener = new ChangeListener() {
114                         public void stateChanged(ChangeEvent e)
115                         {
116                                 enableOkButton();
117                         }
118                 };
119                 _waypointCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.getwaypoints"), true);
120                 _waypointCheckbox.addChangeListener(checkboxListener);
121                 _waypointCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
122                 mainPanel.add(_waypointCheckbox);
123                 _trackCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.gettracks"), true);
124                 _trackCheckbox.addChangeListener(checkboxListener);
125                 _trackCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
126                 mainPanel.add(_trackCheckbox);
127                 // Checkbox for immediately saving to file
128                 _saveCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.save"));
129                 _saveCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
130                 mainPanel.add(_saveCheckbox);
131
132                 // Filter panel
133                 _filterPanel = new BabelFilterPanel(_parentFrame);
134                 // Give filter panel the contents of the config
135                 String filter = Config.getConfigString(Config.KEY_GPSBABEL_FILTER);
136                 if (filter != null) {
137                         _filterPanel.setFilterString(filter);
138                 }
139                 mainPanel.add(_filterPanel);
140
141                 // progress bar (initially invisible)
142                 _progressBar = new JProgressBar(0, 10);
143                 mainPanel.add(_progressBar);
144                 outerPanel.add(mainPanel, BorderLayout.NORTH);
145
146                 // Lower panel with ok and cancel buttons
147                 JPanel buttonPanel = new JPanel();
148                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
149                 _okButton = new JButton(I18nManager.getText("button.ok"));
150                 ActionListener okListener = new ActionListener() {
151                         public void actionPerformed(ActionEvent e)
152                         {
153                                 // start thread to call gpsbabel
154                                 _cancelled = false;
155                                 new Thread(BabelLoadFromGps.this).start();
156                         }
157                 };
158                 _okButton.addActionListener(okListener);
159                 _deviceField.addActionListener(okListener);
160                 _formatField.addActionListener(okListener);
161                 buttonPanel.add(_okButton);
162                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
163                 cancelButton.addActionListener(new ActionListener() {
164                         public void actionPerformed(ActionEvent e)
165                         {
166                                 _cancelled = true;
167                                 _dialog.dispose();
168                         }
169                 });
170                 buttonPanel.add(cancelButton);
171                 outerPanel.add(buttonPanel, BorderLayout.SOUTH);
172                 return outerPanel;
173         }
174
175         /**
176          * Save GPS settings in config
177          */
178         protected void saveConfigValues()
179         {
180                 final String device = _deviceField.getText().trim();
181                 final String format = _formatField.getText().trim();
182                 final String filter = _filterPanel.getFilterString();
183                 Config.setConfigString(Config.KEY_GPS_DEVICE, device);
184                 Config.setConfigString(Config.KEY_GPS_FORMAT, format);
185                 Config.setConfigString(Config.KEY_GPSBABEL_FILTER, filter);
186         }
187 }