source: josm/src/org/openstreetmap/josm/gui/layer/markerlayer/MarkerLayer.java@ 304

Last change on this file since 304 was 304, checked in by imi, 17 years ago
  • fixed a bug that nodes were not created when no data layer was loaded
  • deprecated Main.map.mapView.addLayerChangeListener.
File size: 5.4 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.layer.markerlayer;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.Color;
8import java.awt.Component;
9import java.awt.Graphics;
10import java.awt.Point;
11import java.awt.event.ActionEvent;
12import java.awt.event.ActionListener;
13import java.awt.event.MouseAdapter;
14import java.awt.event.MouseEvent;
15import java.io.File;
16import java.util.Collection;
17
18import javax.swing.Icon;
19import javax.swing.JColorChooser;
20import javax.swing.JMenuItem;
21import javax.swing.JOptionPane;
22import javax.swing.JSeparator;
23import javax.swing.SwingUtilities;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.actions.RenameLayerAction;
27import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
28import org.openstreetmap.josm.gui.MapView;
29import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
30import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
31import org.openstreetmap.josm.gui.layer.Layer;
32import org.openstreetmap.josm.tools.ColorHelper;
33import org.openstreetmap.josm.tools.ImageProvider;
34
35/**
36 * A layer holding markers.
37 *
38 * Markers are GPS points with a name and, optionally, a symbol code attached;
39 * marker layers can be created from waypoints when importing raw GPS data,
40 * but they may also come from other sources.
41 *
42 * The symbol code is for future use.
43 *
44 * The data is read only.
45 */
46public class MarkerLayer extends Layer {
47
48 /**
49 * A list of markers.
50 */
51 public final Collection<Marker> data;
52 private boolean mousePressed = false;
53
54 public MarkerLayer(Collection<Marker> indata, String name, File associatedFile) {
55 super(name);
56 this.associatedFile = associatedFile;
57 this.data = indata;
58
59 SwingUtilities.invokeLater(new Runnable(){
60 public void run() {
61 Main.map.mapView.addMouseListener(new MouseAdapter() {
62 @Override public void mousePressed(MouseEvent e) {
63 if (e.getButton() != MouseEvent.BUTTON1)
64 return;
65 mousePressed = true;
66 if (visible)
67 Main.map.mapView.repaint();
68 }
69 @Override public void mouseReleased(MouseEvent ev) {
70 if (ev.getButton() != MouseEvent.BUTTON1)
71 return;
72 mousePressed = false;
73 if (!visible)
74 return;
75 if (ev.getPoint() != null) {
76 for (Marker mkr : data) {
77 if (mkr.containsPoint(ev.getPoint()))
78 mkr.actionPerformed(new ActionEvent(this, 0, null));
79 }
80 }
81 Main.map.mapView.repaint();
82 }
83 });
84 }
85 });
86 }
87
88 /**
89 * Return a static icon.
90 */
91 @Override public Icon getIcon() {
92 return ImageProvider.get("layer", "marker_small");
93 }
94
95 @Override public void paint(Graphics g, MapView mv) {
96 boolean mousePressedTmp = mousePressed;
97 Point mousePos = mv.getMousePosition();
98 String mkrCol = Main.pref.get("color.gps marker");
99 String mkrColSpecial = Main.pref.get("color.layer "+name);
100 String mkrTextShow = Main.pref.get("marker.show "+name, "show");
101
102 if (!mkrColSpecial.equals(""))
103 g.setColor(ColorHelper.html2color(mkrColSpecial));
104 else if (!mkrCol.equals(""))
105 g.setColor(ColorHelper.html2color(mkrCol));
106 else
107 g.setColor(Color.GRAY);
108
109 for (Marker mkr : data) {
110 if (mousePos != null && mkr.containsPoint(mousePos)) {
111 mkr.paint(g, mv, mousePressedTmp, mkrTextShow);
112 mousePressedTmp = false;
113 } else {
114 mkr.paint(g, mv, false, mkrTextShow);
115 }
116 }
117 }
118
119 @Override public String getToolTipText() {
120 return data.size()+" "+trn("marker", "markers", data.size());
121 }
122
123 @Override public void mergeFrom(Layer from) {
124 MarkerLayer layer = (MarkerLayer)from;
125 data.addAll(layer.data);
126 }
127
128 @Override public boolean isMergable(Layer other) {
129 return other instanceof MarkerLayer;
130 }
131
132 @Override public void visitBoundingBox(BoundingXYVisitor v) {
133 for (Marker mkr : data)
134 v.visit(mkr.eastNorth);
135 }
136
137 @Override public Object getInfoComponent() {
138 return "<html>"+trn("{0} consists of {1} marker", "{0} consists of {1} markers", data.size(), name, data.size()) + "</html>";
139 }
140
141 @Override public Component[] getMenuEntries() {
142 JMenuItem color = new JMenuItem(tr("Customize Color"), ImageProvider.get("colorchooser"));
143 color.addActionListener(new ActionListener(){
144 public void actionPerformed(ActionEvent e) {
145 String col = Main.pref.get("color.layer "+name, Main.pref.get("color.gps marker", ColorHelper.color2html(Color.gray)));
146 JColorChooser c = new JColorChooser(ColorHelper.html2color(col));
147 Object[] options = new Object[]{tr("OK"), tr("Cancel"), tr("Default")};
148 int answer = JOptionPane.showOptionDialog(Main.parent, c, tr("Choose a color"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
149 switch (answer) {
150 case 0:
151 Main.pref.put("color.layer "+name, ColorHelper.color2html(c.getColor()));
152 break;
153 case 1:
154 return;
155 case 2:
156 Main.pref.put("color.layer "+name, null);
157 break;
158 }
159 Main.map.repaint();
160 }
161 });
162
163 return new Component[] {
164 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)),
165 new JMenuItem(new LayerListDialog.ShowHideMarkerText(this)),
166 new JMenuItem(new LayerListDialog.DeleteLayerAction(this)),
167 new JSeparator(),
168 color,
169 new JMenuItem(new RenameLayerAction(associatedFile, this)),
170 new JSeparator(),
171 new JMenuItem(new LayerListPopup.InfoAction(this))
172 };
173 }
174}
Note: See TracBrowser for help on using the repository browser.