fix small bugs for swing version

git-svn-id: https://osmand.googlecode.com/svn/trunk@50 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-05-09 13:06:13 +00:00
parent acd99e37e6
commit 0a146f6088
5 changed files with 231 additions and 120 deletions

View file

@ -130,6 +130,11 @@ public class Amenity {
return getType().toString() +" : " + getSubType() + " " +(name == null ? node.getId() : name);
}
public String getStringWithoutType(){
String name = node.getTag(OSMTagKey.NAME);
return getSubType() + " " +(name == null ? node.getId() : name);
}
@Override
public String toString() {
return getSimpleFormat();

View file

@ -9,6 +9,7 @@ import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
@ -16,6 +17,7 @@ import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@ -27,6 +29,7 @@ import java.util.Map;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
@ -116,6 +119,9 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
private List<IMapLocationListener> listeners = new ArrayList<IMapLocationListener>();
private MapSelectionArea selectionArea = new MapSelectionArea();
// cached data to draw image
private Image[][] images;
private int xStartingImage = 0;
@ -136,9 +142,13 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
prepareImage();
}
});
setOpaque(false);
MapMouseAdapter mouse = new MapMouseAdapter();
addMouseListener(mouse);
addMouseMotionListener(mouse);
addMouseWheelListener(mouse);
addZoomButtons();
}
@ -188,12 +198,21 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
g.fillOval(p.x, p.y, 3, 3);
}
if(selectionArea.isVisible()){
g.setColor(new Color(0, 0, 230, 50));
Rectangle r = selectionArea.getSelectedArea();
g.fillRect(r.x, r.y, r.width, r.height);
}
g.setColor(Color.black);
String s = MessageFormat.format("Lat : {0}, lon : {1}, zoom : {2}", latitude, longitude, zoom);
g.drawString(s, 5, 20);
g.fillOval(getWidth() / 2 - 2, getHeight() / 2 - 2, 4, 4);
g.drawOval(getWidth() / 2 - 2, getHeight() / 2 - 2, 4, 4);
g.drawOval(getWidth() / 2 - 5, getHeight() / 2 - 5, 10, 10);
super.paintComponent(g);
}
@ -331,6 +350,9 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
public void setZoom(int zoom){
if(map != null && (zoom > map.getMaximumZoomSupported() || zoom < map.getMinimumZoomSupported())){
return;
}
this.zoom = zoom;
prepareImage();
}
@ -354,6 +376,10 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
return zoom;
}
public MapSelectionArea getSelectionArea() {
return selectionArea;
}
public ITileSource getMap(){
return map;
}
@ -434,10 +460,97 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
public void setPoints(DataTileManager<LatLon> points) {
this.points = points;
prepareImage();
}
public void addZoomButtons(){
JButton zoomIn = new JButton("+");
JButton zoomOut = new JButton("-");
zoomIn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
setZoom(getZoom() + 1);
}
});
zoomOut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
setZoom(getZoom() - 1);
}
});
add(zoomIn);
add(zoomOut);
}
public class MapSelectionArea {
private double lat1;
private double lon1;
private double lat2;
private double lon2;
public double getLat1() {
return lat1;
}
public double getLat2() {
return lat2;
}
public double getLon1() {
return lon1;
}
public double getLon2() {
return lon2;
}
public Rectangle getSelectedArea(){
Rectangle r = new Rectangle();
r.x = getWidth() / 2 + MapUtils.getPixelShiftX(zoom, lon1, getLongitude(), getTileSize());
r.y = getHeight() / 2 + MapUtils.getPixelShiftY(zoom, lat1, getLatitude(), getTileSize());
r.width = getWidth() / 2 + MapUtils.getPixelShiftX(zoom, lon2, getLongitude(), getTileSize()) - r.x;
r.height = getHeight() / 2 + MapUtils.getPixelShiftY(zoom, lat2, getLatitude(), getTileSize()) - r.y;
return r;
}
public boolean isVisible(){
if(lat1 == lat2 || lon1 == lon2){
return false;
}
Rectangle area = getSelectedArea();
return area.width > 4 && area.height > 4;
}
public void setSelectedArea(int x1, int y1, int x2, int y2){
int rx1 = Math.min(x1, x2);
int rx2 = Math.max(x1, x2);
int ry1 = Math.min(y1, y2);
int ry2 = Math.max(y1, y2);
int zoom = getZoom();
double xTile = getXTile();
double yTile = getYTile();
int wid = getWidth();
int h = getHeight();
int tileSize = getTileSize();
double xTile1 = xTile - (wid / 2 - rx1) / ((double)tileSize);
double yTile1 = yTile - (h / 2 - ry1) / ((double)tileSize);
double xTile2 = xTile - (wid / 2 - rx2) / ((double)tileSize);
double yTile2 = yTile - (h / 2 - ry2) / ((double)tileSize);
lat1 = MapUtils.getLatitudeFromTile(zoom, yTile1);
lat2 = MapUtils.getLatitudeFromTile(zoom, yTile2);
lon1 = MapUtils.getLongitudeFromTile(zoom, xTile1);
lon2 = MapUtils.getLongitudeFromTile(zoom, xTile2);
}
}
public class MapMouseAdapter extends MouseAdapter {
private Point startDragging = null;
private Point startSelecting = null;
@Override
public void mouseClicked(MouseEvent e) {
@ -462,14 +575,29 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
startDragging = e.getPoint();
}
}
if(startSelecting != null){
selectionArea.setSelectedArea(startSelecting.x, startSelecting.y, e.getPoint().x, e.getPoint().y);
updateUI();
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if(e.getWheelRotation() < 0){
setZoom(getZoom() + 1);
} else if(e.getWheelRotation() > 0) {
setZoom(getZoom() - 1);
}
super.mouseWheelMoved(e);
}
@Override
public void mousePressed(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3){
if(startDragging == null){
startDragging = e.getPoint();
}
} else if(e.getButton() == MouseEvent.BUTTON1){
startSelecting = e.getPoint();
}
}
@Override
@ -481,6 +609,14 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
startDragging = null;
}
}
if(e.getButton() == MouseEvent.BUTTON1){
if(startSelecting != null){
selectionArea.setSelectedArea(startSelecting.x, startSelecting.y, e.getPoint().x, e.getPoint().y);
startSelecting = null;
updateUI();
}
}
super.mouseReleased(e);
}

View file

@ -43,6 +43,8 @@ import javax.swing.filechooser.FileFilter;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;
import com.osmand.DataExtraction;
@ -53,6 +55,7 @@ import com.osmand.data.City;
import com.osmand.data.DataTileManager;
import com.osmand.data.Region;
import com.osmand.data.Street;
import com.osmand.data.Amenity.AmenityType;
import com.osmand.data.City.CityType;
import com.osmand.osm.Entity;
import com.osmand.osm.LatLon;
@ -61,8 +64,15 @@ import com.osmand.osm.Node;
import com.osmand.osm.OSMSettings.OSMTagKey;
public class OsmExtractionUI implements IMapLocationListener {
protected City selectedCity;
private static final Log log = LogFactory.getLog(OsmExtractionUI.class);
public static void main(String[] args) {
OsmExtractionUI ui = new OsmExtractionUI(null);
ui.runUI();
}
protected City selectedCity;
private MapPanel mapPanel = new MapPanel(new File(DefaultLauncherConstants.pathToDirWithTiles));
private DefaultMutableTreeNode amenitiesTree;
@ -81,10 +91,6 @@ public class OsmExtractionUI implements IMapLocationListener {
workingDir = new File(DefaultLauncherConstants.pathToWorkingDir);
}
public static void main(String[] args) {
OsmExtractionUI ui = new OsmExtractionUI(null);
ui.runUI();
}
public void setRegion(Region region){
if (this.region == region) {
@ -94,6 +100,9 @@ public class OsmExtractionUI implements IMapLocationListener {
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
amenitiesTree = new DataExtractionTreeNode("Amenities", region);
amenitiesTree.add(new DataExtractionTreeNode("closest", region));
for(AmenityType type : AmenityType.values()){
amenitiesTree.add(new DataExtractionTreeNode(type.toString().toLowerCase(), type));
}
root.add(amenitiesTree);
if (region != null) {
@ -131,7 +140,7 @@ public class OsmExtractionUI implements IMapLocationListener {
public void createUI(){
frame = new JFrame("Tree of choose");
frame = new JFrame("OsmAnd Map Creator");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
@ -222,7 +231,6 @@ public class OsmExtractionUI implements IMapLocationListener {
}
});
treePlaces.addTreeSelectionListener(new TreeSelectionListener(){
@Override
public void valueChanged(TreeSelectionEvent e) {
@ -235,6 +243,11 @@ public class OsmExtractionUI implements IMapLocationListener {
mapPanel.setLatLon(c.getNode().getLatitude(), c.getNode().getLongitude());
mapPanel.requestFocus();
}
if (o instanceof Amenity) {
Amenity c = (Amenity) o;
mapPanel.setLatLon(c.getNode().getLatitude(), c.getNode().getLongitude());
mapPanel.requestFocus();
}
if (o instanceof Entity) {
Entity c = (Entity) o;
@ -348,13 +361,16 @@ public class OsmExtractionUI implements IMapLocationListener {
}
});
Region region = (Region) dlg.run();
if(region != null){
setRegion(region);
frame.setTitle("OsmAnd Map Creator - " + f.getName());
} else {
//frame.setTitle("OsmAnd Map Creator");
}
} catch (InterruptedException e1) {
// TODO
e1.printStackTrace();
log.error("Interrupted", e1);
} catch (InvocationTargetException e1) {
// TODO
e1.printStackTrace();
log.error("Exception during operation", e1.getCause());
}
}
@ -371,21 +387,29 @@ public class OsmExtractionUI implements IMapLocationListener {
}
});
Map<String, List<Amenity>> filter = new TreeMap<String, List<Amenity>>();
Map<AmenityType, List<Amenity>> filter = new TreeMap<AmenityType, List<Amenity>>();
for (Amenity n : closestAmenities) {
String type = n.getType().toString();
AmenityType type = n.getType();
if (!filter.containsKey(type)) {
filter.put(type, new ArrayList<Amenity>());
}
filter.get(type).add(n);
}
for (int i = 1; i < amenitiesTree.getChildCount();) {
if (!filter.containsKey(((DefaultMutableTreeNode) amenitiesTree.getChildAt(i)).getUserObject())) {
amenitiesTree.remove(i);
} else {
i++;
for (int i = 1; i < amenitiesTree.getChildCount(); i++) {
AmenityType type = (AmenityType) ((DefaultMutableTreeNode) amenitiesTree.getChildAt(i)).getUserObject();
((DefaultMutableTreeNode) amenitiesTree.getChildAt(i)).removeAllChildren();
if (filter.get(type) != null) {
for (Amenity n : filter.get(type)) {
int dist = (int) (MapUtils.getDistance(n.getNode(), newLatitude, newLongitude));
String str = n.getStringWithoutType() + " [" + dist + " m ]";
DataExtractionTreeNode node = new DataExtractionTreeNode(str, n);
((DefaultMutableTreeNode) amenitiesTree.getChildAt(i)).add(node);
}
}
((DefaultTreeModel)treePlaces.getModel()).nodeStructureChanged(amenitiesTree.getChildAt(i));
}
((DefaultMutableTreeNode) amenitiesTree.getChildAt(0)).removeAllChildren();
for (int i = 0; i < 15 && i < closestAmenities.size(); i++) {
@ -393,30 +417,10 @@ public class OsmExtractionUI implements IMapLocationListener {
int dist = (int) (MapUtils.getDistance(n.getNode(), newLatitude, newLongitude));
String str = n.getSimpleFormat() + " [" + dist + " m ]";
((DefaultMutableTreeNode) amenitiesTree.getChildAt(0)).add(new DataExtractionTreeNode(str, n));
((DefaultTreeModel)treePlaces.getModel()).nodeStructureChanged(amenitiesTree.getChildAt(0));
}
for (String s : filter.keySet()) {
DefaultMutableTreeNode p = null;
for (int i = 0; i < amenitiesTree.getChildCount(); i++) {
if (s.equals(((DefaultMutableTreeNode) amenitiesTree.getChildAt(i)).getUserObject())) {
p = ((DefaultMutableTreeNode) amenitiesTree.getChildAt(i));
break;
}
}
if (p == null) {
p = new DefaultMutableTreeNode(s);
}
p.removeAllChildren();
for (Amenity n : filter.get(s)) {
int dist = (int) (MapUtils.getDistance(n.getNode(), newLatitude, newLongitude));
String str = n.getSimpleFormat() + " [" + dist + " m ]";
DataExtractionTreeNode node = new DataExtractionTreeNode(str, n);
p.add(node);
}
amenitiesTree.add(p);
}
treePlaces.updateUI();
}
}

View file

@ -10,6 +10,7 @@ import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import com.osmand.IProgress;
@ -49,12 +50,11 @@ public class ProgressDialog extends JDialog implements IProgress {
finished = false;
result = null;
new WorkerThread().start();
synchronized (this) {
if(!finished){
setVisible(true);
if (!finished) {
}
}
if(exception != null){
if (exception != null) {
throw exception;
}
return result;
@ -64,19 +64,19 @@ public class ProgressDialog extends JDialog implements IProgress {
@Override
public void run() {
try {
if(run != null){
if (run != null) {
run.run();
}
} catch(RuntimeException e){
} catch (RuntimeException e) {
exception = new InvocationTargetException(e);
} finally {
synchronized (this) {
finished = true;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setVisible(false);
}
});
}
}
@ -119,7 +119,7 @@ public class ProgressDialog extends JDialog implements IProgress {
private void updateMessage() {
if(!progressBar.isIndeterminate()){
label.setText(taskName + String.format("\t%.1f %%", progressBar.getValue() * 100f / ((float) progressBar.getMaximum())));
label.setText(taskName + String.format("\t %.1f %%", progressBar.getValue() * 100f / ((float) progressBar.getMaximum())));
}
}
@ -170,14 +170,23 @@ public class ProgressDialog extends JDialog implements IProgress {
if(work == 0){
work = 1;
}
if(work != -1){
final int w = work;
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
if(w != -1){
progressBar.setMinimum(0);
progressBar.setMaximum(work);
progressBar.setMaximum(w);
progressBar.setValue(0);
progressBar.setIndeterminate(false);
} else {
progressBar.setIndeterminate(true);
}
}
});
deltaWork = 0;
}
}

View file

@ -1,43 +0,0 @@
package com.osmand.swing;
import com.osmand.IProgress;
public class SwingProgressImplementation implements IProgress {
@Override
public void finishTask() {
// TODO Auto-generated method stub
}
@Override
public boolean isIndeterminate() {
// TODO Auto-generated method stub
return false;
}
@Override
public void progress(int deltaWork) {
// TODO Auto-generated method stub
}
@Override
public void remaining(int remainingWork) {
// TODO Auto-generated method stub
}
@Override
public void startTask(String taskName, int work) {
// TODO Auto-generated method stub
}
@Override
public void startWork(int work) {
// TODO Auto-generated method stub
}
}