From 5a66576ee7a22f0d4657963108a3c3a6443d4405 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sat, 15 May 2010 11:07:17 +0000 Subject: [PATCH] implement saving preference for swing version git-svn-id: https://osmand.googlecode.com/svn/trunk@58 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- .../com/osmand/DefaultLauncherConstants.java | 28 +------- .../src/com/osmand/ToDoConstants.java | 2 +- .../data/preparation/DataExtraction.java | 19 ++++-- .../data/preparation/DataIndexBuilder.java | 3 +- .../data/preparation/MapTileDownloader.java | 2 +- .../osmand/swing/DataExtractionSettings.java | 66 ++++++++++++++++++- .../src/com/osmand/swing/MapPanel.java | 43 ++++++++---- .../src/com/osmand/swing/OsmExtractionUI.java | 32 +++++---- OsmAnd/src/com/osmand/OsmandSettings.java | 4 +- .../com/osmand/views/OsmandMapTileView.java | 3 +- 10 files changed, 131 insertions(+), 71 deletions(-) diff --git a/DataExtractionOSM/src/com/osmand/DefaultLauncherConstants.java b/DataExtractionOSM/src/com/osmand/DefaultLauncherConstants.java index 66f7276afc..1ff15aca48 100644 --- a/DataExtractionOSM/src/com/osmand/DefaultLauncherConstants.java +++ b/DataExtractionOSM/src/com/osmand/DefaultLauncherConstants.java @@ -1,32 +1,11 @@ package com.osmand; -import com.osmand.map.ITileSource; -import com.osmand.map.TileSourceManager; /** * This is temp class where all path & machine specific properties are written */ public abstract class DefaultLauncherConstants { - // External files - public static String pathToTestDataDir = "E:\\Information\\OSM maps\\"; - - public static String pathToOsmFile = pathToTestDataDir + "minsk.osm"; - public static String pathToOsmBz2File = pathToTestDataDir + "belarus_2010_04_01.osm.bz2"; - - public static String pathToWorkingDir = pathToTestDataDir +"osmand\\"; - public static String pathToDirWithTiles = pathToWorkingDir +"tiles"; - - public static String writeTestOsmFile = "C:\\1_tmp.osm"; // could be null - wo writing - - // Initial map settings - public static double MAP_startMapLongitude = 27.56; - public static double MAP_startMapLatitude = 53.9; - public static int MAP_startMapZoom = 15; - public static int MAP_divNonLoadedImage = 16; - public static boolean loadMissingImages = true; - public static ITileSource MAP_defaultTileSource = TileSourceManager.getMapnikSource(); - public static boolean showGPSCoordinates = true; // Application constants @@ -36,10 +15,7 @@ public abstract class DefaultLauncherConstants { // Download manager tile settings public static int TILE_DOWNLOAD_THREADS = 4; - public static int TILE_DOWNLOAD_SECONTS_TO_WORK = 25; - public static final int TILE_DOWNLOAD_MAX_ERRORS = -1; - - - + public static int TILE_DOWNLOAD_SECONDS_TO_WORK = 25; + public static final int TILE_DOWNLOAD_MAX_ERRORS = -1; } diff --git a/DataExtractionOSM/src/com/osmand/ToDoConstants.java b/DataExtractionOSM/src/com/osmand/ToDoConstants.java index 7aefb77375..0c9d52b7cc 100644 --- a/DataExtractionOSM/src/com/osmand/ToDoConstants.java +++ b/DataExtractionOSM/src/com/osmand/ToDoConstants.java @@ -40,7 +40,7 @@ public class ToDoConstants { /// SWING version : // TODO : - // 1. save preferences ? where? (use internet, default dir save and other...) + // 1. Add preferences dialog (use internet, ) // 2. specify area to download tiles () // 3. download tiles without using dir tiles // 4. Config file log & see log from file diff --git a/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java b/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java index 8c0960c872..385e39f29f 100644 --- a/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java +++ b/DataExtractionOSM/src/com/osmand/data/preparation/DataExtraction.java @@ -17,7 +17,6 @@ import org.apache.tools.bzip2.CBZip2InputStream; import org.apache.tools.bzip2.CBZip2OutputStream; import org.xml.sax.SAXException; -import com.osmand.DefaultLauncherConstants; import com.osmand.IProgress; import com.osmand.data.Amenity; import com.osmand.data.City; @@ -78,7 +77,13 @@ public class DataExtraction { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XMLStreamException { new DataExtraction().testReadingOsmFile(); } - + // External files + public static String pathToTestDataDir = "E:\\Information\\OSM maps\\"; + public static String pathToOsmFile = pathToTestDataDir + "minsk.osm"; + public static String pathToOsmBz2File = pathToTestDataDir + "belarus_2010_04_01.osm.bz2"; + public static String pathToWorkingDir = pathToTestDataDir +"osmand\\"; + public static String pathToDirWithTiles = pathToWorkingDir +"tiles"; + public static String writeTestOsmFile = "C:\\1_tmp.osm"; // could be null - wo writing private static boolean parseSmallFile = true; private static boolean parseOSM = true; @@ -93,9 +98,9 @@ public class DataExtraction { public void testReadingOsmFile() throws ParserConfigurationException, SAXException, IOException, XMLStreamException { String f; if(parseSmallFile){ - f = DefaultLauncherConstants.pathToOsmFile; + f = pathToOsmFile; } else { - f = DefaultLauncherConstants.pathToOsmBz2File; + f = pathToOsmBz2File; } long st = System.currentTimeMillis(); @@ -116,10 +121,10 @@ public class DataExtraction { // MapUtils.addIdsToList(amenities, interestedObjects); MapUtils.addIdsToList(waysManager.getAllObjects(), interestedObjects); // MapUtils.addIdsToList(buildings, interestedObjects); - if (DefaultLauncherConstants.writeTestOsmFile != null) { + if (writeTestOsmFile != null) { OSMStorageWriter writer = new OSMStorageWriter(country.getStorage().getRegisteredEntities()); - OutputStream output = new FileOutputStream(DefaultLauncherConstants.writeTestOsmFile); - if (DefaultLauncherConstants.writeTestOsmFile.endsWith(".bz2")) { + OutputStream output = new FileOutputStream(writeTestOsmFile); + if (writeTestOsmFile.endsWith(".bz2")) { output.write('B'); output.write('Z'); output = new CBZip2OutputStream(output); diff --git a/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java b/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java index 9d28cfe63e..5d9c7dba1b 100644 --- a/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java +++ b/DataExtractionOSM/src/com/osmand/data/preparation/DataIndexBuilder.java @@ -11,7 +11,6 @@ import javax.xml.stream.XMLStreamException; import org.apache.tools.bzip2.CBZip2OutputStream; -import com.osmand.DefaultLauncherConstants; import com.osmand.data.Amenity; import com.osmand.data.Region; import com.osmand.osm.io.OSMStorageWriter; @@ -47,7 +46,7 @@ public class DataIndexBuilder { f.delete(); } OutputStream output = new FileOutputStream(f); - if (DefaultLauncherConstants.writeTestOsmFile.endsWith(".bz2")) { + if (name.endsWith(".bz2")) { output.write('B'); output.write('Z'); output = new CBZip2OutputStream(output); diff --git a/DataExtractionOSM/src/com/osmand/data/preparation/MapTileDownloader.java b/DataExtractionOSM/src/com/osmand/data/preparation/MapTileDownloader.java index eaa05864c3..cdaae884fd 100644 --- a/DataExtractionOSM/src/com/osmand/data/preparation/MapTileDownloader.java +++ b/DataExtractionOSM/src/com/osmand/data/preparation/MapTileDownloader.java @@ -87,7 +87,7 @@ public class MapTileDownloader { public MapTileDownloader(int numberOfThreads){ - threadPoolExecutor = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, DefaultLauncherConstants.TILE_DOWNLOAD_SECONTS_TO_WORK, + threadPoolExecutor = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, DefaultLauncherConstants.TILE_DOWNLOAD_SECONDS_TO_WORK, TimeUnit.SECONDS, new LinkedBlockingQueue()); // 1.6 method but very useful to kill non-running threads // threadPoolExecutor.allowCoreThreadTimeOut(true); diff --git a/DataExtractionOSM/src/com/osmand/swing/DataExtractionSettings.java b/DataExtractionOSM/src/com/osmand/swing/DataExtractionSettings.java index e462219841..17dc64b660 100644 --- a/DataExtractionOSM/src/com/osmand/swing/DataExtractionSettings.java +++ b/DataExtractionOSM/src/com/osmand/swing/DataExtractionSettings.java @@ -1,5 +1,11 @@ package com.osmand.swing; +import java.awt.Rectangle; +import java.io.File; +import java.util.prefs.Preferences; + +import com.osmand.osm.LatLon; + public class DataExtractionSettings { private static DataExtractionSettings settings = null; @@ -11,9 +17,65 @@ public class DataExtractionSettings { } + Preferences preferences = Preferences.userRoot(); - public void saveSettings(){ - + public File getTilesDirectory(){ + return new File(getDefaultWorkingDir(), "tiles"); } + + public File getDefaultWorkingDir(){ + String workingDir = preferences.get("working_dir", System.getProperty("user.home")); + if(workingDir.equals(System.getProperty("user.home"))){ + workingDir += "/osmand"; + new File(workingDir).mkdir(); + } + return new File(workingDir); + } + + public void saveDefaultWorkingDir(File path){ + preferences.put("working_dir", path.getAbsolutePath()); + } + + public LatLon getDefaultLocation(){ + double lat = preferences.getDouble("default_lat", 53.9); + double lon = preferences.getDouble("default_lon", 27.56); + return new LatLon(lat, lon); + } + + public void saveDefaultLocation(double lat, double lon){ + preferences.putDouble("default_lat", lat); + preferences.putDouble("default_lon", lon); + } + + public int getDefaultZoom(){ + return preferences.getInt("default_zoom", 5); + } + + public void saveDefaultZoom(int zoom){ + preferences.putInt("default_zoom", zoom); + } + + public Rectangle getWindowBounds(){ + Rectangle r = new Rectangle(); + r.x = preferences.getInt("window_x", 0); + r.y = preferences.getInt("window_y", 0); + r.width = preferences.getInt("window_width", 800); + r.height = preferences.getInt("window_height", 600); + return r; + } + + public void saveWindowBounds(Rectangle r) { + preferences.putInt("window_x", r.x); + preferences.putInt("window_y", r.y); + preferences.putInt("window_width", r.width); + preferences.putInt("window_height", r.height); + } + + public boolean useInternetToLoadImages(){ + // TODO save the property if needed + return true; + } + + } diff --git a/DataExtractionOSM/src/com/osmand/swing/MapPanel.java b/DataExtractionOSM/src/com/osmand/swing/MapPanel.java index 31ce552acc..2f2fe70ae6 100644 --- a/DataExtractionOSM/src/com/osmand/swing/MapPanel.java +++ b/DataExtractionOSM/src/com/osmand/swing/MapPanel.java @@ -15,6 +15,8 @@ import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseWheelEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -36,7 +38,6 @@ import javax.swing.UIManager; import org.apache.commons.logging.Log; -import com.osmand.DefaultLauncherConstants; import com.osmand.IMapLocationListener; import com.osmand.LogUtil; import com.osmand.data.DataTileManager; @@ -54,6 +55,7 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { private static final long serialVersionUID = 1L; protected static final Log log = LogUtil.getLog(MapPanel.class); + public static final int divNonLoadedImage = 16; public static JMenu getMenuToChooseSource(final MapPanel panel){ final JMenu tiles = new JMenu("Source tile"); @@ -74,8 +76,9 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { } }); - if(l.equals(DefaultLauncherConstants.MAP_defaultTileSource)){ + if(l.equals(TileSourceManager.getMapnikSource())){ menuItem.setSelected(true); + panel.setMapName(l); } tiles.add(menuItem); } @@ -85,19 +88,24 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { public static void main(String[] args) throws IOException { - JFrame frame = new JFrame("Tree of choose"); + JFrame frame = new JFrame("Map view"); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } - - frame.addWindowListener(new OsmExtractionUI.ExitListener()); + final MapPanel panel = new MapPanel(DataExtractionSettings.getSettings().getTilesDirectory()); + frame.addWindowListener(new WindowAdapter(){ + @Override + public void windowClosing(WindowEvent e) { + DataExtractionSettings settings = DataExtractionSettings.getSettings(); + settings.saveDefaultLocation(panel.getLatitude(), panel.getLongitude()); + settings.saveDefaultZoom(panel.getZoom()); + System.exit(0); + } + }); Container content = frame.getContentPane(); - - MapPanel panel = new MapPanel(new File(DefaultLauncherConstants.pathToDirWithTiles)); - content.add(panel, BorderLayout.CENTER); JMenuBar bar = new JMenuBar(); @@ -111,21 +119,21 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { private File tilesLocation = null; // name of source map - private ITileSource map = DefaultLauncherConstants.MAP_defaultTileSource; + private ITileSource map; // special points to draw private DataTileManager points; // zoom level - private int zoom = DefaultLauncherConstants.MAP_startMapZoom; + private int zoom = 1; // degree measurements (-180, 180) // долгота - private double longitude = DefaultLauncherConstants.MAP_startMapLongitude; + private double longitude; // широта // degree measurements (90, -90) - private double latitude = DefaultLauncherConstants.MAP_startMapLatitude; + private double latitude; private List listeners = new ArrayList(); @@ -145,6 +153,12 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { public MapPanel(File fileWithTiles) { tilesLocation = fileWithTiles; + LatLon defaultLocation = DataExtractionSettings.getSettings().getDefaultLocation(); + latitude = defaultLocation.getLatitude(); + longitude = defaultLocation.getLongitude(); + zoom = DataExtractionSettings.getSettings().getDefaultZoom(); + + downloader.setDownloaderCallback(this); setFocusable(true); addComponentListener(new ComponentAdapter(){ @@ -181,7 +195,7 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { for (int i = 0; i < images.length; i++) { for (int j = 0; j < images[i].length; j++) { if (images[i][j] == null) { - int div = DefaultLauncherConstants.MAP_divNonLoadedImage; + int div = divNonLoadedImage; int tileDiv = getTileSize() / div; for (int k1 = 0; k1 < div; k1++) { for (int k2 = 0; k2 < div; k2++) { @@ -234,6 +248,7 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { public void setTilesLocation(File tilesLocation) { this.tilesLocation = tilesLocation; + cache.clear(); prepareImage(); } @@ -299,7 +314,7 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { } public void prepareImage(){ - prepareImage(DefaultLauncherConstants.loadMissingImages); + prepareImage(DataExtractionSettings.getSettings().useInternetToLoadImages()); } public void prepareImage(boolean loadNecessaryImages){ diff --git a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java index 3358f9ab4f..b9aed6ae6b 100644 --- a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java +++ b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java @@ -59,7 +59,6 @@ import org.apache.commons.logging.LogFactory; import org.xml.sax.SAXException; import com.osmand.Algoritms; -import com.osmand.DefaultLauncherConstants; import com.osmand.ExceptionHandler; import com.osmand.IMapLocationListener; import com.osmand.data.Amenity; @@ -83,11 +82,12 @@ public class OsmExtractionUI implements IMapLocationListener { public static void main(String[] args) { OsmExtractionUI ui = new OsmExtractionUI(null); - ui.runUI(); + ui.frame.setBounds(DataExtractionSettings.getSettings().getWindowBounds()); + ui.frame.setVisible(true); } protected City selectedCity; - private MapPanel mapPanel = new MapPanel(new File(DefaultLauncherConstants.pathToDirWithTiles)); + private MapPanel mapPanel; private DataExtractionTreeNode amenitiesTree; private JTree treePlaces; @@ -98,7 +98,6 @@ public class OsmExtractionUI implements IMapLocationListener { private JLabel statusBarLabel; private Region region; - private File workingDir; private JButton generateDataButton; private JCheckBox buildPoiIndex; private JCheckBox buildAddressIndex; @@ -107,7 +106,7 @@ public class OsmExtractionUI implements IMapLocationListener { public OsmExtractionUI(final Region r){ this.region = r; - workingDir = new File(DefaultLauncherConstants.pathToWorkingDir); + mapPanel = new MapPanel(DataExtractionSettings.getSettings().getTilesDirectory()); createUI(); setRegion(r, "Region"); } @@ -181,6 +180,7 @@ public class OsmExtractionUI implements IMapLocationListener { statusBarLabel = new JLabel(); content.add(statusBarLabel, BorderLayout.SOUTH); + File workingDir = DataExtractionSettings.getSettings().getDefaultWorkingDir(); statusBarLabel.setText(workingDir == null ? "" : "Working directory : " + workingDir.getAbsolutePath()); @@ -264,7 +264,7 @@ public class OsmExtractionUI implements IMapLocationListener { } protected void updateButtonsBar() { - generateDataButton.setEnabled(workingDir != null && region != null); + generateDataButton.setEnabled(region != null); buildAddressIndex.setEnabled(generateDataButton.isEnabled() && region.getCitiesCount(null) > 0); buildPoiIndex.setEnabled(generateDataButton.isEnabled() && !region.getAmenityManager().isEmpty()); } @@ -283,7 +283,7 @@ public class OsmExtractionUI implements IMapLocationListener { @Override public void actionPerformed(ActionEvent e) { - DataIndexBuilder builder = new DataIndexBuilder(workingDir, region); + DataIndexBuilder builder = new DataIndexBuilder(DataExtractionSettings.getSettings().getDefaultWorkingDir(), region); StringBuilder msg = new StringBuilder(); try { msg.append("Indices checked for ").append(region.getName()); @@ -381,10 +381,6 @@ public class OsmExtractionUI implements IMapLocationListener { } - public void runUI(){ - frame.setSize(1024, 768); - frame.setVisible(true); - } public void fillMenuWithActions(JMenuBar bar){ JMenu menu = new JMenu("File"); @@ -412,12 +408,14 @@ public class OsmExtractionUI implements IMapLocationListener { JFileChooser fc = new JFileChooser(); fc.setDialogTitle("Choose working directory"); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + File workingDir = DataExtractionSettings.getSettings().getDefaultWorkingDir(); if(workingDir != null){ fc.setCurrentDirectory(workingDir); } if(fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION && fc.getSelectedFile() != null && fc.getSelectedFile().isDirectory()){ - workingDir = fc.getSelectedFile(); + DataExtractionSettings.getSettings().saveDefaultWorkingDir(fc.getSelectedFile()); + mapPanel.setTilesLocation(DataExtractionSettings.getSettings().getTilesDirectory()); statusBarLabel.setText("Working directory : " + fc.getSelectedFile().getAbsolutePath()); updateButtonsBar(); } @@ -433,7 +431,7 @@ public class OsmExtractionUI implements IMapLocationListener { fc.setDialogTitle("Choose osm file"); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); fc.setAcceptAllFileFilterUsed(true); - fc.setCurrentDirectory(new File(DefaultLauncherConstants.pathToTestDataDir)); + fc.setCurrentDirectory(DataExtractionSettings.getSettings().getDefaultWorkingDir()); //System.out.println("opening fc for extension " + extension); fc.setFileFilter(new FileFilter(){ @@ -599,10 +597,16 @@ public class OsmExtractionUI implements IMapLocationListener { } } - public static class ExitListener extends WindowAdapter { + public class ExitListener extends WindowAdapter { public void windowClosing(WindowEvent event) { + // save preferences + DataExtractionSettings settings = DataExtractionSettings.getSettings(); + settings.saveDefaultLocation(mapPanel.getLatitude(), mapPanel.getLongitude()); + settings.saveDefaultZoom(mapPanel.getZoom()); + settings.saveWindowBounds(frame.getBounds()); System.exit(0); } } + } diff --git a/OsmAnd/src/com/osmand/OsmandSettings.java b/OsmAnd/src/com/osmand/OsmandSettings.java index bdbcf938cb..a98e9d0962 100644 --- a/OsmAnd/src/com/osmand/OsmandSettings.java +++ b/OsmAnd/src/com/osmand/OsmandSettings.java @@ -41,7 +41,7 @@ public class OsmandSettings { } } } - return DefaultLauncherConstants.MAP_defaultTileSource; + return TileSourceManager.getMapnikSource(); } public static String getMapTileSourceName(Context ctx){ @@ -50,7 +50,7 @@ public class OsmandSettings { if(tileName != null){ return tileName; } - return DefaultLauncherConstants.MAP_defaultTileSource.getName(); + return TileSourceManager.getMapnikSource().getName(); } diff --git a/OsmAnd/src/com/osmand/views/OsmandMapTileView.java b/OsmAnd/src/com/osmand/views/OsmandMapTileView.java index 53e4f2de5f..4c235037ed 100644 --- a/OsmAnd/src/com/osmand/views/OsmandMapTileView.java +++ b/OsmAnd/src/com/osmand/views/OsmandMapTileView.java @@ -20,7 +20,6 @@ import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.SurfaceHolder.Callback; -import com.osmand.DefaultLauncherConstants; import com.osmand.IMapLocationListener; import com.osmand.LogUtil; import com.osmand.OsmandSettings; @@ -34,7 +33,7 @@ import com.osmand.views.AnimateDraggingMapThread.AnimateDraggingCallback; public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCallback, Callback, AnimateDraggingCallback{ - protected final int emptyTileDivisor = DefaultLauncherConstants.MAP_divNonLoadedImage; + protected final int emptyTileDivisor = 16; protected final int timeForDraggingAnimation = 300; protected final int minimumDistanceForDraggingAnimation = 40;