1. implement stopping thread

2. implement settings for swing

git-svn-id: https://osmand.googlecode.com/svn/trunk@70 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-05-20 07:59:35 +00:00
parent 02663a32ee
commit f934e9add1
6 changed files with 296 additions and 17 deletions

View file

@ -45,15 +45,12 @@ public class ToDoConstants {
/// SWING version : /// SWING version :
// TODO : // TODO :
// 0. Create build.xml & build product for release purposes // 1. Predefine before file loading what should be extracted from osm (building, poi or roads) !
// 1. Add preferences dialog (use internet, Normalizing streets) !!! // 2. Fix TODO in files (accept amenity - way)
// 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
// 5. download tiles without using dir tiles // 3. download tiles without using dir tiles
// 6. Config file log & see log from file // 4. 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 !) // 5. 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,... // 6. Invent different file extensions for poi.index, address.index,...
} }

View file

@ -37,6 +37,7 @@ import com.osmand.osm.OSMSettings.OSMTagKey;
import com.osmand.osm.io.IOsmStorageFilter; import com.osmand.osm.io.IOsmStorageFilter;
import com.osmand.osm.io.OsmBaseStorage; import com.osmand.osm.io.OsmBaseStorage;
import com.osmand.osm.io.OsmStorageWriter; import com.osmand.osm.io.OsmStorageWriter;
import com.osmand.swing.DataExtractionSettings;
// TO implement // TO implement
@ -292,8 +293,7 @@ public class DataExtraction {
country.registerCity(s); country.registerCity(s);
} }
} }
String[] SUFFIXES = new String[] {"просп.", "пер.", "пр.","заул.", "проспект", "переул.", "бул.", "бульвар"};
String[] DEFAUTL_SUFFIXES = new String[] {"улица", "ул."};
private int checkSuffix(String name, String suffix){ private int checkSuffix(String name, String suffix){
int i = -1; int i = -1;
@ -338,6 +338,8 @@ public class DataExtraction {
public void normalizingStreets(IProgress progress, Region region){ public void normalizingStreets(IProgress progress, Region region){
progress.startTask("Normalizing name streets...", -1); progress.startTask("Normalizing name streets...", -1);
String[] defaultSuffixes = DataExtractionSettings.getSettings().getDefaultSuffixesToNormalizeStreets();
String[] suffixes = DataExtractionSettings.getSettings().getSuffixesToNormalizeStreets();
for(CityType t : CityType.values()){ for(CityType t : CityType.values()){
for(City c : region.getCitiesByType(t)){ for(City c : region.getCitiesByType(t)){
ArrayList<Street> list = new ArrayList<Street>(c.getStreets()); ArrayList<Street> list = new ArrayList<Street>(c.getStreets());
@ -345,7 +347,7 @@ public class DataExtraction {
String name = s.getName(); String name = s.getName();
String newName = name.trim(); String newName = name.trim();
boolean processed = newName.length() != name.length(); boolean processed = newName.length() != name.length();
for (String ch : DEFAUTL_SUFFIXES) { for (String ch : defaultSuffixes) {
int ind = checkSuffix(newName, ch); int ind = checkSuffix(newName, ch);
if (ind != -1) { if (ind != -1) {
newName = cutSuffix(newName, ind, ch.length()); newName = cutSuffix(newName, ind, ch.length());
@ -355,7 +357,7 @@ public class DataExtraction {
} }
if (!processed) { if (!processed) {
for (String ch : SUFFIXES) { for (String ch : suffixes) {
int ind = checkSuffix(newName, ch); int ind = checkSuffix(newName, ch);
if (ind != -1) { if (ind != -1) {
newName = putSuffixToEnd(newName, ind, ch.length()); newName = putSuffixToEnd(newName, ind, ch.length());

View file

@ -2,6 +2,8 @@ package com.osmand.swing;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences; import java.util.prefs.Preferences;
import com.osmand.osm.LatLon; import com.osmand.osm.LatLon;
@ -72,10 +74,85 @@ public class DataExtractionSettings {
preferences.putInt("window_height", r.height); preferences.putInt("window_height", r.height);
} }
public boolean useInternetToLoadImages(){ public boolean useInternetToLoadImages(){
// TODO save the property if needed return preferences.getBoolean("use_internet", true);
return 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<String> l = new ArrayList<String>();
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<String> l = new ArrayList<String>();
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);
}
} }

View file

@ -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());
}
}
}

View file

@ -470,6 +470,13 @@ public class OsmExtractionUI implements IMapLocationListener {
bar.add(MapPanel.getMenuToChooseSource(mapPanel)); bar.add(MapPanel.getMenuToChooseSource(mapPanel));
menu = new JMenu("Window");
bar.add(menu);
JMenuItem settings = new JMenuItem("Settings...");
menu.add(settings);
exitMenu.addActionListener(new ActionListener(){ exitMenu.addActionListener(new ActionListener(){
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -483,6 +490,13 @@ public class OsmExtractionUI implements IMapLocationListener {
setRegion(null, "Region"); setRegion(null, "Region");
frame.setTitle("OsmAnd Map Creator"); 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(){ specifyWorkingDir.addActionListener(new ActionListener(){

View file

@ -31,6 +31,7 @@ public class ProgressDialog extends JDialog implements IProgress {
private static final float deltaToChange = 0.01f; private static final float deltaToChange = 0.01f;
private String taskName; private String taskName;
private int deltaWork; private int deltaWork;
private WorkerThread workerThread;
public ProgressDialog(Component parent, String name){ public ProgressDialog(Component parent, String name){
@ -43,10 +44,19 @@ public class ProgressDialog extends JDialog implements IProgress {
return !isVisible(); return !isVisible();
} }
@SuppressWarnings("deprecation")
public Object run() throws InvocationTargetException, InterruptedException { public Object run() throws InvocationTargetException, InterruptedException {
result = null; result = null;
new WorkerThread().start(); workerThread = new WorkerThread();
workerThread.start();
setVisible(true); 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) { if (exception != null) {
throw exception; throw exception;
} }
@ -54,13 +64,19 @@ public class ProgressDialog extends JDialog implements IProgress {
} }
private class WorkerThread extends Thread { private class WorkerThread extends Thread {
private boolean isLive = true;
public boolean checkIsLive(){
return isLive;
}
@Override @Override
public void run() { public void run() {
try { try {
if (run != null) { if (run != null) {
run.run(); run.run();
} }
isLive = false;
} catch (RuntimeException e) { } catch (RuntimeException e) {
exception = new InvocationTargetException(e); exception = new InvocationTargetException(e);
} finally { } finally {