From f934e9add1c007d43b41962f73a1dbcf39269c71 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 20 May 2010 07:59:35 +0000 Subject: [PATCH] 1. implement stopping thread 2. implement settings for swing git-svn-id: https://osmand.googlecode.com/svn/trunk@70 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- .../src/com/osmand/ToDoConstants.java | 15 +- .../data/preparation/DataExtraction.java | 10 +- .../osmand/swing/DataExtractionSettings.java | 81 +++++++- .../swing/OsmExtractionPreferencesDialog.java | 173 ++++++++++++++++++ .../src/com/osmand/swing/OsmExtractionUI.java | 14 ++ .../src/com/osmand/swing/ProgressDialog.java | 20 +- 6 files changed, 296 insertions(+), 17 deletions(-) create mode 100644 DataExtractionOSM/src/com/osmand/swing/OsmExtractionPreferencesDialog.java diff --git a/DataExtractionOSM/src/com/osmand/ToDoConstants.java b/DataExtractionOSM/src/com/osmand/ToDoConstants.java index 7909339b8d..121b9e367c 100644 --- a/DataExtractionOSM/src/com/osmand/ToDoConstants.java +++ b/DataExtractionOSM/src/com/osmand/ToDoConstants.java @@ -45,15 +45,12 @@ public class ToDoConstants { /// SWING version : // TODO : - // 0. Create build.xml & build product for release purposes - // 1. Add preferences dialog (use internet, Normalizing streets) !!! - // 2. Predefine before file loading what should be extracted from osm (building, poi or roads) ! - // 3. Fix TODO in files (accept amenity - way) - // 4. Interrupt thread when progress dialog is closed + // 1. Predefine before file loading what should be extracted from osm (building, poi or roads) ! + // 2. Fix TODO in files (accept amenity - way) - // 5. download tiles without using dir tiles - // 6. Config file log & see log from file - // 7. Reinvent index mechanism (save in zip file with tile indexes, save city/town addresses separately, read partially !) - // 8. Invent different file extensions for poi.index, address.index,... + // 3. download tiles without using dir tiles + // 4. Config file log & see log from file + // 5. Reinvent index mechanism (save in zip file with tile indexes, save city/town addresses separately, read partially !) + // 6. Invent different file extensions for poi.index, address.index,... } diff --git a/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java b/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java index bbed6094c8..7e295149f2 100644 --- a/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java +++ b/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java @@ -37,6 +37,7 @@ import com.osmand.osm.OSMSettings.OSMTagKey; import com.osmand.osm.io.IOsmStorageFilter; import com.osmand.osm.io.OsmBaseStorage; import com.osmand.osm.io.OsmStorageWriter; +import com.osmand.swing.DataExtractionSettings; // TO implement @@ -292,8 +293,7 @@ public class DataExtraction { country.registerCity(s); } } - String[] SUFFIXES = new String[] {"просп.", "пер.", "пр.","заул.", "проспект", "переул.", "бул.", "бульвар"}; - String[] DEFAUTL_SUFFIXES = new String[] {"улица", "ул."}; + private int checkSuffix(String name, String suffix){ int i = -1; @@ -338,6 +338,8 @@ public class DataExtraction { public void normalizingStreets(IProgress progress, Region region){ progress.startTask("Normalizing name streets...", -1); + String[] defaultSuffixes = DataExtractionSettings.getSettings().getDefaultSuffixesToNormalizeStreets(); + String[] suffixes = DataExtractionSettings.getSettings().getSuffixesToNormalizeStreets(); for(CityType t : CityType.values()){ for(City c : region.getCitiesByType(t)){ ArrayList list = new ArrayList(c.getStreets()); @@ -345,7 +347,7 @@ public class DataExtraction { String name = s.getName(); String newName = name.trim(); boolean processed = newName.length() != name.length(); - for (String ch : DEFAUTL_SUFFIXES) { + for (String ch : defaultSuffixes) { int ind = checkSuffix(newName, ch); if (ind != -1) { newName = cutSuffix(newName, ind, ch.length()); @@ -355,7 +357,7 @@ public class DataExtraction { } if (!processed) { - for (String ch : SUFFIXES) { + for (String ch : suffixes) { int ind = checkSuffix(newName, ch); if (ind != -1) { newName = putSuffixToEnd(newName, ind, ch.length()); diff --git a/DataExtractionOSM/src/com/osmand/swing/DataExtractionSettings.java b/DataExtractionOSM/src/com/osmand/swing/DataExtractionSettings.java index 17dc64b660..ebcd04296e 100644 --- a/DataExtractionOSM/src/com/osmand/swing/DataExtractionSettings.java +++ b/DataExtractionOSM/src/com/osmand/swing/DataExtractionSettings.java @@ -2,6 +2,8 @@ package com.osmand.swing; import java.awt.Rectangle; import java.io.File; +import java.util.ArrayList; +import java.util.List; import java.util.prefs.Preferences; import com.osmand.osm.LatLon; @@ -72,10 +74,85 @@ public class DataExtractionSettings { preferences.putInt("window_height", r.height); } + + public boolean useInternetToLoadImages(){ - // TODO save the property if needed - return true; + return preferences.getBoolean("use_internet", true); + } + + public void setUseInterentToLoadImages(boolean b){ + preferences.putBoolean("use_internet", b); } + String[] SUFFIXES = new String[] {"av.", "avenue", "просп.", "пер.", "пр.","заул.", "проспект", "переул.", "бул.", "бульвар", "тракт"}; + String[] DEFAUTL_SUFFIXES = new String[] {"str.", "street", "улица", "ул."}; + public String[] getSuffixesToNormalizeStreets(){ + String s = preferences.get("suffixes_normalize_streets", null); + if(s == null){ + return SUFFIXES; + } + List l = new ArrayList(); + int i = 0; + int nextI = 0; + while((nextI=s.indexOf(',',i)) >= 0){ + String t = s.substring(i, nextI).trim(); + if(t.length() > 0){ + l.add(t); + } + i = nextI + 1; + } + return l.toArray(new String[l.size()]); + } + + public String[] getDefaultSuffixesToNormalizeStreets(){ + String s = preferences.get("default_suffixes_normalize_streets", null); + if(s == null){ + return DEFAUTL_SUFFIXES; + } + List l = new ArrayList(); + int i = 0; + int nextI = 0; + while((nextI=s.indexOf(',',i)) >= 0){ + String t = s.substring(i, nextI).trim(); + if(t.length() > 0){ + l.add(t); + } + i = nextI + 1; + } + return l.toArray(new String[l.size()]); + } + + public String getSuffixesToNormalizeStreetsString(){ + String s = preferences.get("suffixes_normalize_streets", null); + if(s == null){ + StringBuilder b = new StringBuilder(); + for(String st : SUFFIXES){ + b.append(st).append(", "); + } + return b.toString(); + } + return s; + } + + public String getDefaultSuffixesToNormalizeStreetsString(){ + String s = preferences.get("default_suffixes_normalize_streets", null); + if(s == null){ + StringBuilder b = new StringBuilder(); + for(String st : DEFAUTL_SUFFIXES){ + b.append(st).append(", "); + } + return b.toString(); + } + return s; + } + + public void setDefaultSuffixesToNormalizeStreets(String s){ + preferences.put("default_suffixes_normalize_streets", s); + } + + public void setSuffixesToNormalizeStreets(String s){ + preferences.put("suffixes_normalize_streets", s); + } + } diff --git a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionPreferencesDialog.java b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionPreferencesDialog.java new file mode 100644 index 0000000000..ae1252c0d2 --- /dev/null +++ b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionPreferencesDialog.java @@ -0,0 +1,173 @@ +package com.osmand.swing; + +import java.awt.Component; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.BorderFactory; +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; + +public class OsmExtractionPreferencesDialog extends JDialog { + + private static final long serialVersionUID = -4862884032977071296L; + + private JButton okButton; + private JButton cancelButton; + + private JTextField streetSuffixes; + + private JTextField streetDefaultSuffixes; + + private JCheckBox useInternet; + + public OsmExtractionPreferencesDialog(Component parent){ + super(JOptionPane.getFrameForComponent(parent), true); + setTitle("Preferences"); + initDialog(); + + } + + public void showDialog(){ + setSize(600, 220); + double x = getParent().getBounds().getCenterX(); + double y = getParent().getBounds().getCenterY(); + setLocation((int) x - getWidth() / 2, (int) y - getHeight() / 2); + setVisible(true); + } + + private void initDialog() { + JPanel pane = new JPanel(); + pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); + pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 5, 10)); + add(pane); + + + createGeneralSection(pane); + createNormalizingStreetSection(pane); + pane.add(Box.createVerticalGlue()); + + FlowLayout l = new FlowLayout(FlowLayout.RIGHT); + JPanel buttonsPane = new JPanel(l); + okButton = new JButton("OK"); + buttonsPane.add(okButton); + cancelButton = new JButton("Cancel"); + buttonsPane.add(cancelButton); + + buttonsPane.setMaximumSize(new Dimension(Short.MAX_VALUE, (int) l.preferredLayoutSize(buttonsPane).getHeight())); + pane.add(buttonsPane); + + addListeners(); + } + + private void createGeneralSection(JPanel root) { + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(1, 1, 5, 5)); + panel.setBorder(BorderFactory.createTitledBorder("General")); + root.add(panel); + + useInternet = new JCheckBox(); + useInternet.setText("Use internet to download tiles"); + useInternet.setSelected(DataExtractionSettings.getSettings().useInternetToLoadImages()); + panel.add(useInternet); + panel.setMaximumSize(new Dimension(Short.MAX_VALUE, panel.getPreferredSize().height)); + + } + + private void createNormalizingStreetSection(JPanel root) { + JPanel panel = new JPanel(); + GridBagLayout l = new GridBagLayout(); + panel.setLayout(l); + panel.setBorder(BorderFactory.createTitledBorder("Normalizing streets")); + root.add(panel); + + JLabel label = new JLabel("Street name suffixes (av., avenue)"); + panel.add(label); + GridBagConstraints constr = new GridBagConstraints(); + constr.anchor = GridBagConstraints.WEST; + constr.ipadx = 5; + constr.gridx = 0; + constr.gridy = 0; + l.setConstraints(label, constr); + + streetSuffixes = new JTextField(); + streetSuffixes.setText(DataExtractionSettings.getSettings().getSuffixesToNormalizeStreetsString()); + panel.add(streetSuffixes); + constr = new GridBagConstraints(); + constr.fill = GridBagConstraints.HORIZONTAL; + constr.ipadx = 5; + constr.gridx = 1; + constr.gridy = 0; + l.setConstraints(streetSuffixes, constr); + + label = new JLabel("Street name default suffixes (str., street, rue)"); + panel.add(label); + constr = new GridBagConstraints(); + constr.ipadx = 5; + constr.gridx = 0; + constr.gridy = 1; + constr.anchor = GridBagConstraints.WEST; + l.setConstraints(label, constr); + + streetDefaultSuffixes = new JTextField(); + streetDefaultSuffixes.setText(DataExtractionSettings.getSettings().getDefaultSuffixesToNormalizeStreetsString()); + panel.add(streetDefaultSuffixes); + constr = new GridBagConstraints(); + constr.weightx = 1; + constr.fill = GridBagConstraints.HORIZONTAL; + constr.ipadx = 5; + constr.gridx = 1; + constr.gridy = 1; + l.setConstraints(streetDefaultSuffixes, constr); + panel.setMaximumSize(new Dimension(Short.MAX_VALUE, panel.getPreferredSize().height)); + } + + private void addListeners(){ + okButton.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + saveProperties(); + setVisible(false); + } + + }); + cancelButton.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + setVisible(false); + } + }); + + } + + + public void saveProperties(){ + DataExtractionSettings settings = DataExtractionSettings.getSettings(); + if(!settings.getSuffixesToNormalizeStreetsString().equals(streetSuffixes.getText())){ + settings.setSuffixesToNormalizeStreets(streetSuffixes.getText()); + } + if(!settings.getDefaultSuffixesToNormalizeStreetsString().equals(streetDefaultSuffixes.getText())){ + settings.setDefaultSuffixesToNormalizeStreets(streetDefaultSuffixes.getText()); + } + if(settings.useInternetToLoadImages() != useInternet.isSelected()){ + settings.setUseInterentToLoadImages(useInternet.isSelected()); + } + + } + + + +} + diff --git a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java index bdcf5a1697..95661471c2 100644 --- a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java +++ b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java @@ -470,6 +470,13 @@ public class OsmExtractionUI implements IMapLocationListener { bar.add(MapPanel.getMenuToChooseSource(mapPanel)); + menu = new JMenu("Window"); + bar.add(menu); + JMenuItem settings = new JMenuItem("Settings..."); + menu.add(settings); + + + exitMenu.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { @@ -483,6 +490,13 @@ public class OsmExtractionUI implements IMapLocationListener { setRegion(null, "Region"); frame.setTitle("OsmAnd Map Creator"); } + }); + settings.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + OsmExtractionPreferencesDialog dlg = new OsmExtractionPreferencesDialog(frame); + dlg.showDialog(); + } }); specifyWorkingDir.addActionListener(new ActionListener(){ diff --git a/DataExtractionOSM/src/com/osmand/swing/ProgressDialog.java b/DataExtractionOSM/src/com/osmand/swing/ProgressDialog.java index 4bfbfdf6ec..9504355029 100644 --- a/DataExtractionOSM/src/com/osmand/swing/ProgressDialog.java +++ b/DataExtractionOSM/src/com/osmand/swing/ProgressDialog.java @@ -31,6 +31,7 @@ public class ProgressDialog extends JDialog implements IProgress { private static final float deltaToChange = 0.01f; private String taskName; private int deltaWork; + private WorkerThread workerThread; public ProgressDialog(Component parent, String name){ @@ -43,10 +44,19 @@ public class ProgressDialog extends JDialog implements IProgress { return !isVisible(); } - public Object run() throws InvocationTargetException, InterruptedException { + @SuppressWarnings("deprecation") + public Object run() throws InvocationTargetException, InterruptedException { result = null; - new WorkerThread().start(); + workerThread = new WorkerThread(); + workerThread.start(); setVisible(true); + if(workerThread.checkIsLive()){ + // that's really bad solution unless we don't find any problems with that + // means monitor objects & we continue to use because otherwise + // we should protect all places where it is used regular checks that process is interrupted + workerThread.stop(); + throw new InterruptedException(); + } if (exception != null) { throw exception; } @@ -54,13 +64,19 @@ public class ProgressDialog extends JDialog implements IProgress { } private class WorkerThread extends Thread { + private boolean isLive = true; + + public boolean checkIsLive(){ + return isLive; + } @Override public void run() { try { if (run != null) { run.run(); } + isLive = false; } catch (RuntimeException e) { exception = new InvocationTargetException(e); } finally {