| 1 | package cadastre_fr;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 | import java.awt.Cursor;
|
|---|
| 6 | import java.awt.GridBagLayout;
|
|---|
| 7 | import java.awt.Point;
|
|---|
| 8 | import java.awt.event.ActionEvent;
|
|---|
| 9 | import java.awt.event.ActionListener;
|
|---|
| 10 | import java.awt.event.KeyEvent;
|
|---|
| 11 | import java.awt.event.MouseEvent;
|
|---|
| 12 | import java.awt.event.MouseListener;
|
|---|
| 13 | import java.awt.event.MouseMotionListener;
|
|---|
| 14 | import java.util.ArrayList;
|
|---|
| 15 | import java.util.List;
|
|---|
| 16 |
|
|---|
| 17 | import javax.swing.ButtonGroup;
|
|---|
| 18 | import javax.swing.JButton;
|
|---|
| 19 | import javax.swing.JDialog;
|
|---|
| 20 | import javax.swing.JLabel;
|
|---|
| 21 | import javax.swing.JOptionPane;
|
|---|
| 22 | import javax.swing.JPanel;
|
|---|
| 23 | import javax.swing.JRadioButton;
|
|---|
| 24 | import javax.swing.JTextField;
|
|---|
| 25 |
|
|---|
| 26 | import org.openstreetmap.josm.Main;
|
|---|
| 27 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
|---|
| 28 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 29 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 30 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 31 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 32 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 33 | import org.openstreetmap.josm.data.osm.WaySegment;
|
|---|
| 34 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 35 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 36 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 37 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 38 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 39 |
|
|---|
| 40 | public class Address extends MapMode implements MouseListener, MouseMotionListener, ActionListener {
|
|---|
| 41 | private static final long serialVersionUID = 1L;
|
|---|
| 42 |
|
|---|
| 43 | static final String tagHighway = "highway";
|
|---|
| 44 | static final String tagHighwayName = "name";
|
|---|
| 45 | static final String tagHouseNumber = "addr:housenumber";
|
|---|
| 46 | static final String tagHouseStreet = "addr:street";
|
|---|
| 47 | static final String relationAddrType = "associatedStreet";
|
|---|
| 48 | static final String relationStreetNameAttr = "name";
|
|---|
| 49 | static final String relationAddrStreetRole = "street";
|
|---|
| 50 |
|
|---|
| 51 | private JRadioButton plus_one = new JRadioButton("+1", false);
|
|---|
| 52 | private JRadioButton plus_two = new JRadioButton("+2", true);
|
|---|
| 53 | private JRadioButton minus_one = new JRadioButton("-1", false);
|
|---|
| 54 | private JRadioButton minus_two = new JRadioButton("-2", false);
|
|---|
| 55 |
|
|---|
| 56 | JDialog dialog = null;
|
|---|
| 57 | JButton clearButton = null;
|
|---|
| 58 | final JTextField inputNumber = new JTextField();
|
|---|
| 59 | final JTextField inputStreet = new JTextField();
|
|---|
| 60 |
|
|---|
| 61 | public Address(MapFrame mapFrame) {
|
|---|
| 62 | super(tr("Add address"), "buildings",
|
|---|
| 63 | tr("Create house number and street name relation"),
|
|---|
| 64 | Shortcut.registerShortcut("mapmode:buildings", tr("Mode: {0}", tr("Buildings")), KeyEvent.VK_E, Shortcut.GROUP_EDIT),
|
|---|
| 65 | mapFrame, getCursor());
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | @Override public void enterMode() {
|
|---|
| 69 | super.enterMode();
|
|---|
| 70 | if (dialog == null) {
|
|---|
| 71 | JPanel p = new JPanel(new GridBagLayout());
|
|---|
| 72 | JLabel number = new JLabel(tr("Number"));
|
|---|
| 73 | JLabel street = new JLabel(tr("Street"));
|
|---|
| 74 | p.add(number, GBC.std().insets(0, 0, 5, 0));
|
|---|
| 75 | p.add(inputNumber, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 5, 0, 5));
|
|---|
| 76 | p.add(street, GBC.std().insets(0, 0, 5, 0));
|
|---|
| 77 | inputStreet.setEditable(false);
|
|---|
| 78 | p.add(inputStreet, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 5, 0, 5));
|
|---|
| 79 | clearButton = new JButton("Clear");
|
|---|
| 80 | clearButton.addActionListener(new ActionListener() {
|
|---|
| 81 | public void actionPerformed(ActionEvent e) {
|
|---|
| 82 | inputNumber.setText("");
|
|---|
| 83 | inputStreet.setText("");
|
|---|
| 84 | }
|
|---|
| 85 | });
|
|---|
| 86 | ButtonGroup bgIncremental = new ButtonGroup();
|
|---|
| 87 | bgIncremental.add(plus_one);
|
|---|
| 88 | bgIncremental.add(plus_two);
|
|---|
| 89 | bgIncremental.add(minus_one);
|
|---|
| 90 | bgIncremental.add(minus_two);
|
|---|
| 91 | p.add(minus_one, GBC.std().insets(10, 0, 10, 0));
|
|---|
| 92 | p.add(plus_one, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
|
|---|
| 93 | p.add(minus_two, GBC.std().insets(10, 0, 10, 0));
|
|---|
| 94 | p.add(plus_two, GBC.std().insets(10, 0, 10, 0));
|
|---|
| 95 | p.add(clearButton, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
|
|---|
| 96 |
|
|---|
| 97 | final Object[] options = {};
|
|---|
| 98 | final JOptionPane pane = new JOptionPane(p,
|
|---|
| 99 | JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION,
|
|---|
| 100 | null, options, null);
|
|---|
| 101 | dialog = pane.createDialog(Main.parent, tr("Enter addresses"));
|
|---|
| 102 | dialog.setModal(false);
|
|---|
| 103 | dialog.setAlwaysOnTop(true);
|
|---|
| 104 | }
|
|---|
| 105 | dialog.setVisible(true);
|
|---|
| 106 | Main.map.mapView.addMouseListener(this);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | @Override public void exitMode() {
|
|---|
| 110 | super.exitMode();
|
|---|
| 111 | Main.map.mapView.removeMouseListener(this);
|
|---|
| 112 | dialog.setVisible(false);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | @Override
|
|---|
| 116 | public void mousePressed(MouseEvent e) {
|
|---|
| 117 | if (e.getButton() != MouseEvent.BUTTON1)
|
|---|
| 118 | return;
|
|---|
| 119 | MapView mv = Main.map.mapView;
|
|---|
| 120 | Point mousePos = e.getPoint();
|
|---|
| 121 | List<Way> mouseOnExistingWays = new ArrayList<Way>();
|
|---|
| 122 | mouseOnExistingWays = new ArrayList<Way>();
|
|---|
| 123 | Node currentMouseNode = mv.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
|
|---|
| 124 | if(currentMouseNode != null) {
|
|---|
| 125 | String num = currentMouseNode.get(tagHouseNumber);
|
|---|
| 126 | if (num != null) {
|
|---|
| 127 | try {
|
|---|
| 128 | Integer.parseInt(num);
|
|---|
| 129 | inputNumber.setText(num);
|
|---|
| 130 | } catch (NumberFormatException en) {
|
|---|
| 131 | System.out.println("Unable to parse house number \"" + num + "\"");
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 | if (currentMouseNode.get(tagHouseStreet) != null)
|
|---|
| 135 | inputStreet.setText(currentMouseNode.get(tagHouseNumber));
|
|---|
| 136 | else {
|
|---|
| 137 | // check if the node belongs to an associatedStreet relation
|
|---|
| 138 | List<OsmPrimitive> l = currentMouseNode.getReferrers();
|
|---|
| 139 | for (OsmPrimitive osm : l) {
|
|---|
| 140 | if (osm instanceof Relation && osm.hasKey("type") && osm.get("type").equals(relationAddrType)) {
|
|---|
| 141 | if (osm.hasKey(relationStreetNameAttr)) {
|
|---|
| 142 | inputStreet.setText(osm.get(relationStreetNameAttr));
|
|---|
| 143 | break;
|
|---|
| 144 | } else {
|
|---|
| 145 | for (RelationMember rm : ((Relation)osm).getMembers())
|
|---|
| 146 | if (rm.getRole().equals(relationAddrStreetRole)) {
|
|---|
| 147 | OsmPrimitive osp = rm.getMember();
|
|---|
| 148 | if (osp.hasKey(tagHighwayName)) {
|
|---|
| 149 | inputStreet.setText(osp.get(tagHighwayName));
|
|---|
| 150 | break;
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | } else {
|
|---|
| 158 | List<WaySegment> wss = mv.getNearestWaySegments(mousePos, OsmPrimitive.isSelectablePredicate);
|
|---|
| 159 | for(WaySegment ws : wss) {
|
|---|
| 160 | if (ws.way.get(tagHighway) != null && ws.way.get(tagHighwayName) != null)
|
|---|
| 161 | mouseOnExistingWays.add(ws.way);
|
|---|
| 162 | }
|
|---|
| 163 | if (mouseOnExistingWays.size() == 1) {
|
|---|
| 164 | // clicked on existing highway => set new street name
|
|---|
| 165 | inputStreet.setText(mouseOnExistingWays.get(0).get(tagHighwayName));
|
|---|
| 166 | } else if (mouseOnExistingWays.size() == 0) {
|
|---|
| 167 | // clicked a non highway and not a node => add the new address
|
|---|
| 168 | Node n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
|
|---|
| 169 | if (n != null)
|
|---|
| 170 | if (!n.hasKey(tagHouseNumber))
|
|---|
| 171 | addAddrToNode(n);
|
|---|
| 172 | else
|
|---|
| 173 | createNewNode(mousePos);
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | private void addAddrToNode(Node n) {
|
|---|
| 180 | // node exists : just add the tag addr:housenumber and member in relation
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | private void createNewNode(Point mousePos) {
|
|---|
| 184 |
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | private static Cursor getCursor() {
|
|---|
| 188 | try {
|
|---|
| 189 | return ImageProvider.getCursor("crosshair", null);
|
|---|
| 190 | } catch (Exception e) {
|
|---|
| 191 | }
|
|---|
| 192 | return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|