19 package org.sleuthkit.autopsy.geolocation;
21 import java.awt.Color;
23 import java.sql.SQLException;
24 import java.util.Arrays;
25 import java.util.logging.Level;
26 import javax.swing.JFileChooser;
27 import javax.swing.JOptionPane;
28 import org.apache.commons.validator.routines.UrlValidator;
29 import org.jxmapviewer.OSMTileFactoryInfo;
30 import org.jxmapviewer.viewer.TileFactoryInfo;
31 import org.jxmapviewer.viewer.util.GeoUtil;
32 import org.netbeans.spi.options.OptionsPanelController;
33 import org.openide.util.NbBundle.Messages;
44 final class GeolocationSettingsPanel
extends javax.swing.JPanel implements OptionsPanel {
46 private static final long serialVersionUID = 1L;
48 private static final Logger logger = Logger.
getLogger(GeolocationSettingsPanel.class.getName());
53 GeolocationSettingsPanel() {
60 UserPreferences.setGeolocationTileOption(getDataSourceOption().getValue());
61 UserPreferences.setGeolocationOsmZipPath(zipFilePathField.getText());
62 UserPreferences.setGeolocationOsmServerAddress(osmServerAddressField.getText());
63 UserPreferences.setGeolocationMBTilesFilePath(mbtileFileField.getText());
68 osmServerAddressField.setText(UserPreferences.getGeolocationOsmServerAddress());
69 zipFilePathField.setText(UserPreferences.getGeolocationOsmZipPath());
70 mbtileFileField.setText(UserPreferences.getGeolocationMBTilesFilePath());
71 switch (GeolocationDataSourceType.getOptionForValue(UserPreferences.getGeolocationtTileOption())) {
72 case ONLINE_USER_DEFINED_OSM_SERVER:
73 osmServerRBnt.setSelected(
true);
76 zipFileRBnt.setSelected(
true);
78 case OFFILE_MBTILES_FILE:
79 mbtilesRBtn.setSelected(
true);
82 defaultDataSource.setSelected(
true);
93 private void updateControlState() {
94 osmServerAddressField.setEnabled(osmServerRBnt.isSelected());
95 serverTestBtn.setEnabled(osmServerRBnt.isSelected());
96 zipFilePathField.setEnabled(zipFileRBnt.isSelected());
97 zipFileBrowseBnt.setEnabled(zipFileRBnt.isSelected());
98 mbtileFileField.setEnabled(mbtilesRBtn.isSelected());
99 mbtilesBrowseBtn.setEnabled(mbtilesRBtn.isSelected());
100 mbtileTestBtn.setEnabled(mbtilesRBtn.isSelected());
109 private GeolocationDataSourceType getDataSourceOption() {
110 if (osmServerRBnt.isSelected()) {
111 return GeolocationDataSourceType.ONLINE_USER_DEFINED_OSM_SERVER;
112 }
else if (zipFileRBnt.isSelected()) {
113 return GeolocationDataSourceType.OFFLINE_OSM_ZIP;
114 }
else if (mbtilesRBtn.isSelected()) {
115 return GeolocationDataSourceType.OFFILE_MBTILES_FILE;
117 return GeolocationDataSourceType.ONLINE_DEFAULT_SERVER;
132 private boolean testOSMServer(String url) {
133 TileFactoryInfo info =
new OSMTileFactoryInfo(
"User Defined Server", url);
134 return GeoUtil.isValidTile(1, 1, 1, info);
137 void cancelChanges() {
146 @SuppressWarnings(
"unchecked")
148 private
void initComponents() {
149 java.awt.GridBagConstraints gridBagConstraints;
151 javax.swing.ButtonGroup buttonGroup =
new javax.swing.ButtonGroup();
152 javax.swing.JPanel tilePane =
new javax.swing.JPanel();
153 defaultDataSource =
new javax.swing.JRadioButton();
154 osmServerRBnt =
new javax.swing.JRadioButton();
155 osmServerAddressField =
new javax.swing.JTextField();
156 zipFileRBnt =
new javax.swing.JRadioButton();
157 zipFilePathField =
new javax.swing.JTextField();
158 zipFileBrowseBnt =
new javax.swing.JButton();
159 serverTestBtn =
new javax.swing.JButton();
160 mbtilesRBtn =
new javax.swing.JRadioButton();
161 mbtileFileField =
new javax.swing.JTextField();
162 javax.swing.JPanel MBTilesBtnPanel =
new javax.swing.JPanel();
163 mbtilesBrowseBtn =
new javax.swing.JButton();
164 mbtileTestBtn =
new javax.swing.JButton();
166 setLayout(
new java.awt.GridBagLayout());
168 tilePane.setBorder(javax.swing.BorderFactory.createTitledBorder(
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.tilePane.border.title")));
169 tilePane.setLayout(
new java.awt.GridBagLayout());
171 buttonGroup.add(defaultDataSource);
172 defaultDataSource.setSelected(
true);
173 org.openide.awt.Mnemonics.setLocalizedText(defaultDataSource,
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.defaultDataSource.text"));
174 defaultDataSource.addActionListener(
new java.awt.event.ActionListener() {
175 public void actionPerformed(java.awt.event.ActionEvent evt) {
176 defaultDataSourceActionPerformed(evt);
179 gridBagConstraints =
new java.awt.GridBagConstraints();
180 gridBagConstraints.gridx = 0;
181 gridBagConstraints.gridy = 0;
182 gridBagConstraints.gridwidth = 4;
183 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
184 gridBagConstraints.weightx = 1.0;
185 gridBagConstraints.insets =
new java.awt.Insets(9, 0, 9, 0);
186 tilePane.add(defaultDataSource, gridBagConstraints);
188 buttonGroup.add(osmServerRBnt);
189 org.openide.awt.Mnemonics.setLocalizedText(osmServerRBnt,
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.osmServerRBnt.text"));
190 osmServerRBnt.addActionListener(
new java.awt.event.ActionListener() {
191 public void actionPerformed(java.awt.event.ActionEvent evt) {
192 osmServerRBntActionPerformed(evt);
195 gridBagConstraints =
new java.awt.GridBagConstraints();
196 gridBagConstraints.gridx = 0;
197 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
198 gridBagConstraints.insets =
new java.awt.Insets(0, 0, 9, 0);
199 tilePane.add(osmServerRBnt, gridBagConstraints);
201 osmServerAddressField.setText(
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.osmServerAddressField.text"));
202 osmServerAddressField.setPreferredSize(
new java.awt.Dimension(300, 26));
203 gridBagConstraints =
new java.awt.GridBagConstraints();
204 gridBagConstraints.gridx = 1;
205 gridBagConstraints.gridy = 1;
206 gridBagConstraints.insets =
new java.awt.Insets(0, 0, 9, 0);
207 tilePane.add(osmServerAddressField, gridBagConstraints);
209 buttonGroup.add(zipFileRBnt);
210 org.openide.awt.Mnemonics.setLocalizedText(zipFileRBnt,
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.zipFileRBnt.text"));
211 zipFileRBnt.setActionCommand(
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.zipFileRBnt.actionCommand"));
212 zipFileRBnt.addActionListener(
new java.awt.event.ActionListener() {
213 public void actionPerformed(java.awt.event.ActionEvent evt) {
214 zipFileRBntActionPerformed(evt);
217 gridBagConstraints =
new java.awt.GridBagConstraints();
218 gridBagConstraints.gridx = 0;
219 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
220 gridBagConstraints.insets =
new java.awt.Insets(0, 0, 9, 0);
221 tilePane.add(zipFileRBnt, gridBagConstraints);
223 zipFilePathField.setText(
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.zipFilePathField.text"));
224 zipFilePathField.setPreferredSize(
new java.awt.Dimension(300, 26));
225 gridBagConstraints =
new java.awt.GridBagConstraints();
226 gridBagConstraints.gridx = 1;
227 gridBagConstraints.gridy = 2;
228 gridBagConstraints.insets =
new java.awt.Insets(0, 0, 9, 0);
229 tilePane.add(zipFilePathField, gridBagConstraints);
231 org.openide.awt.Mnemonics.setLocalizedText(zipFileBrowseBnt,
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.zipFileBrowseBnt.text"));
232 zipFileBrowseBnt.addActionListener(
new java.awt.event.ActionListener() {
233 public void actionPerformed(java.awt.event.ActionEvent evt) {
234 zipFileBrowseBntActionPerformed(evt);
237 gridBagConstraints =
new java.awt.GridBagConstraints();
238 gridBagConstraints.gridx = 2;
239 gridBagConstraints.gridy = 2;
240 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
241 gridBagConstraints.insets =
new java.awt.Insets(0, 9, 9, 9);
242 tilePane.add(zipFileBrowseBnt, gridBagConstraints);
244 org.openide.awt.Mnemonics.setLocalizedText(serverTestBtn,
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.serverTestBtn.text"));
245 serverTestBtn.addActionListener(
new java.awt.event.ActionListener() {
246 public void actionPerformed(java.awt.event.ActionEvent evt) {
247 serverTestBtnActionPerformed(evt);
250 gridBagConstraints =
new java.awt.GridBagConstraints();
251 gridBagConstraints.gridx = 2;
252 gridBagConstraints.gridy = 1;
253 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
254 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
255 gridBagConstraints.insets =
new java.awt.Insets(0, 9, 9, 9);
256 tilePane.add(serverTestBtn, gridBagConstraints);
258 buttonGroup.add(mbtilesRBtn);
259 org.openide.awt.Mnemonics.setLocalizedText(mbtilesRBtn,
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.mbtilesRBtn.text"));
260 mbtilesRBtn.addActionListener(
new java.awt.event.ActionListener() {
261 public void actionPerformed(java.awt.event.ActionEvent evt) {
262 mbtilesRBtnActionPerformed(evt);
265 gridBagConstraints =
new java.awt.GridBagConstraints();
266 gridBagConstraints.gridx = 0;
267 gridBagConstraints.gridy = 3;
268 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
269 gridBagConstraints.insets =
new java.awt.Insets(0, 0, 9, 0);
270 tilePane.add(mbtilesRBtn, gridBagConstraints);
272 mbtileFileField.setText(
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.mbtileFileField.text"));
273 mbtileFileField.setToolTipText(
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.mbtileFileField.toolTipText"));
274 mbtileFileField.setPreferredSize(
new java.awt.Dimension(300, 26));
275 gridBagConstraints =
new java.awt.GridBagConstraints();
276 gridBagConstraints.gridx = 1;
277 gridBagConstraints.gridy = 3;
278 gridBagConstraints.insets =
new java.awt.Insets(0, 0, 9, 0);
279 tilePane.add(mbtileFileField, gridBagConstraints);
281 MBTilesBtnPanel.setLayout(
new java.awt.GridLayout(1, 0, 5, 0));
283 org.openide.awt.Mnemonics.setLocalizedText(mbtilesBrowseBtn,
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.mbtilesBrowseBtn.text"));
284 mbtilesBrowseBtn.addActionListener(
new java.awt.event.ActionListener() {
285 public void actionPerformed(java.awt.event.ActionEvent evt) {
286 mbtilesBrowseBtnActionPerformed(evt);
289 MBTilesBtnPanel.add(mbtilesBrowseBtn);
291 org.openide.awt.Mnemonics.setLocalizedText(mbtileTestBtn,
org.openide.util.NbBundle.getMessage(GeolocationSettingsPanel.class,
"GeolocationSettingsPanel.mbtileTestBtn.text"));
292 mbtileTestBtn.addActionListener(
new java.awt.event.ActionListener() {
293 public void actionPerformed(java.awt.event.ActionEvent evt) {
294 mbtileTestBtnActionPerformed(evt);
297 MBTilesBtnPanel.add(mbtileTestBtn);
299 gridBagConstraints =
new java.awt.GridBagConstraints();
300 gridBagConstraints.gridx = 2;
301 gridBagConstraints.gridy = 3;
302 gridBagConstraints.gridwidth = 2;
303 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
304 gridBagConstraints.insets =
new java.awt.Insets(0, 9, 9, 9);
305 tilePane.add(MBTilesBtnPanel, gridBagConstraints);
307 gridBagConstraints =
new java.awt.GridBagConstraints();
308 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
309 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
310 gridBagConstraints.weightx = 1.0;
311 gridBagConstraints.weighty = 1.0;
312 add(tilePane, gridBagConstraints);
315 private void zipFileBrowseBntActionPerformed(java.awt.event.ActionEvent evt) {
316 JFileChooser fileWindow =
new JFileChooser();
317 fileWindow.setFileSelectionMode(JFileChooser.FILES_ONLY);
318 GeneralFilter fileFilter =
new GeneralFilter(Arrays.asList(
".zip"),
"Zips (*.zip)");
319 fileWindow.setDragEnabled(
false);
320 fileWindow.setFileFilter(fileFilter);
321 fileWindow.setMultiSelectionEnabled(
false);
322 int returnVal = fileWindow.showSaveDialog(
this);
323 if (returnVal == JFileChooser.APPROVE_OPTION) {
324 File zipFile = fileWindow.getSelectedFile();
325 zipFilePathField.setForeground(Color.BLACK);
326 zipFilePathField.setText(zipFile.getAbsolutePath());
327 firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
331 private void defaultDataSourceActionPerformed(java.awt.event.ActionEvent evt) {
332 updateControlState();
333 firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
336 private void osmServerRBntActionPerformed(java.awt.event.ActionEvent evt) {
337 updateControlState();
338 firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
341 private void zipFileRBntActionPerformed(java.awt.event.ActionEvent evt) {
342 updateControlState();
343 firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
347 "GeolocationSettingsPanel_malformed_url_message=The supplied OSM tile server address is invalid.\nPlease supply a well formed url prefixed with http://",
348 "GeolocationSettingsPanel_malformed_url_message_tile=Malformed URL",
349 "GeolocationSettingsPanel_osm_server_test_fail_message=OSM tile server test failed.\nUnable to connect to server.",
350 "GeolocationSettingsPanel_osm_server_test_fail_message_title=Error",
351 "GeolocationSettingsPanel_osm_server_test_success_message=The provided OSM tile server address is valid.",
352 "GeolocationSettingsPanel_osm_server_test_success_message_title=Success",})
353 private void serverTestBtnActionPerformed(java.awt.event.ActionEvent evt) {
354 String address = osmServerAddressField.getText();
355 String message = Bundle.GeolocationSettingsPanel_osm_server_test_fail_message();
356 String title = Bundle.GeolocationSettingsPanel_osm_server_test_fail_message_title();
358 String[] schemes = {
"http",
"https"};
359 UrlValidator urlValidator =
new UrlValidator(schemes);
360 if (!urlValidator.isValid(address)) {
361 message = Bundle.GeolocationSettingsPanel_malformed_url_message();
362 title = Bundle.GeolocationSettingsPanel_malformed_url_message_tile();
363 }
else if (testOSMServer(address)) {
364 message = Bundle.GeolocationSettingsPanel_osm_server_test_success_message();
365 title = Bundle.GeolocationSettingsPanel_osm_server_test_success_message_title();
368 JOptionPane.showMessageDialog(
this, message, title, JOptionPane.INFORMATION_MESSAGE);
371 private void mbtilesRBtnActionPerformed(java.awt.event.ActionEvent evt) {
372 updateControlState();
373 firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
376 private void mbtilesBrowseBtnActionPerformed(java.awt.event.ActionEvent evt) {
377 JFileChooser fileWindow =
new JFileChooser();
378 fileWindow.setFileSelectionMode(JFileChooser.FILES_ONLY);
379 GeneralFilter fileFilter =
new GeneralFilter(Arrays.asList(
".mbtiles"),
"MBTiles (*.mbtiles)");
380 fileWindow.setDragEnabled(
false);
381 fileWindow.setFileFilter(fileFilter);
382 fileWindow.setMultiSelectionEnabled(
false);
383 int returnVal = fileWindow.showSaveDialog(
this);
384 if (returnVal == JFileChooser.APPROVE_OPTION) {
385 File zipFile = fileWindow.getSelectedFile();
386 mbtileFileField.setForeground(Color.BLACK);
387 mbtileFileField.setText(zipFile.getAbsolutePath());
388 firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
393 "GeolocationSettings_mbtile_does_not_exist_message=The file supplied does not exist.\nPlease verify that the file exists and try again.",
394 "GeolocationSettings_mbtile_does_not_exist_title=File Not Found",
395 "GeolocationSettings_mbtile_not_valid_message=The supplied file is not a raster tile file.",
396 "GeolocationSettings_mbtile_not_valid_title=File Not Valid",
397 "GeolocationSettings_path_not_valid_message=The supplied file path is empty.\nPlease supply a valid file path.",
398 "GeolocationSettings_path_not_valid_title=File Not Valid",
399 "GeolocationSettings_mbtile_test_success_message=The supplied file is a valid mbtile raster file.",
400 "GeolocationSettings_mbtile_test_success_title=Success",
402 private void mbtileTestBtnActionPerformed(java.awt.event.ActionEvent evt) {
403 String mbtilePath = mbtileFileField.getText();
405 if(mbtilePath.isEmpty()) {
406 JOptionPane.showMessageDialog(
this, Bundle.GeolocationSettings_path_not_valid_message(), Bundle.GeolocationSettings_path_not_valid_title(), JOptionPane.ERROR_MESSAGE);
410 File file =
new File(mbtilePath);
412 JOptionPane.showMessageDialog(
this, Bundle.GeolocationSettings_mbtile_does_not_exist_message(), Bundle.GeolocationSettings_mbtile_does_not_exist_title(), JOptionPane.ERROR_MESSAGE);
417 if(!MBTilesFileConnector.isValidMBTileRasterFile(mbtilePath)) {
418 JOptionPane.showMessageDialog(
this, Bundle.GeolocationSettings_mbtile_not_valid_message(), Bundle.GeolocationSettings_mbtile_not_valid_title(), JOptionPane.ERROR_MESSAGE);
421 }
catch (SQLException ex) {
422 JOptionPane.showMessageDialog(
this, Bundle.GeolocationSettings_mbtile_not_valid_message(), Bundle.GeolocationSettings_mbtile_not_valid_title(), JOptionPane.ERROR_MESSAGE);
423 logger.log(Level.WARNING, String.format(
"Exception thrown while testing mbtile file %s", mbtilePath), ex);
427 JOptionPane.showMessageDialog(
this, Bundle.GeolocationSettings_mbtile_test_success_message(), Bundle.GeolocationSettings_mbtile_test_success_title(), JOptionPane.INFORMATION_MESSAGE);
432 private javax.swing.JRadioButton defaultDataSource;
433 private javax.swing.JTextField mbtileFileField;
434 private javax.swing.JButton mbtileTestBtn;
435 private javax.swing.JButton mbtilesBrowseBtn;
436 private javax.swing.JRadioButton mbtilesRBtn;
437 private javax.swing.JTextField osmServerAddressField;
438 private javax.swing.JRadioButton osmServerRBnt;
439 private javax.swing.JButton serverTestBtn;
440 private javax.swing.JButton zipFileBrowseBnt;
441 private javax.swing.JTextField zipFilePathField;
442 private javax.swing.JRadioButton zipFileRBnt;
449 enum GeolocationDataSourceType{
450 ONLINE_DEFAULT_SERVER(0),
451 ONLINE_USER_DEFINED_OSM_SERVER(1),
453 OFFILE_MBTILES_FILE(3);
455 private final int value;
457 GeolocationDataSourceType(
int value) {
465 static GeolocationDataSourceType getOptionForValue(
int value) {
466 for (GeolocationDataSourceType option : GeolocationDataSourceType.values()) {
467 if (option.getValue() == value) {
472 return ONLINE_DEFAULT_SERVER;
synchronized static Logger getLogger(String name)