Ignore:
Timestamp:
2010-07-04T21:32:57+02:00 (14 years ago)
Author:
pieren
Message:

First working version for help tool addr.

Location:
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/Address.java

    r22153 r22186  
    1616import java.awt.event.MouseListener;
    1717import java.awt.event.MouseMotionListener;
     18import java.awt.event.WindowEvent;
     19import java.awt.event.WindowListener;
    1820import java.util.ArrayList;
    1921import java.util.Collection;
     
    3032import javax.swing.ImageIcon;
    3133import javax.swing.JButton;
     34import javax.swing.JCheckBox;
    3235import javax.swing.JDialog;
    3336import javax.swing.JLabel;
     
    3639import javax.swing.JRadioButton;
    3740import javax.swing.JTextField;
     41import javax.swing.event.ChangeEvent;
     42import javax.swing.event.ChangeListener;
    3843
    3944import org.openstreetmap.josm.Main;
     
    6166    private static final long serialVersionUID = 1L;
    6267   
    63     static final String tagHighway = "highway";
    64     static final String tagHighwayName = "name";
    65     static final String tagHouseNumber = "addr:housenumber";
    66     static final String tagHouseStreet = "addr:street";
    67     static final String relationAddrType = "associatedStreet";
    68     static final String relationStreetNameAttr = "name";
    69     static final String relationAddrStreetRole = "street";
     68    // perhaps make all these tags configurable in the future
     69    private String tagHighway = "highway";
     70    private String tagHighwayName = "name";
     71    private String tagHouseNumber = "addr:housenumber";
     72    private String tagHouseStreet = "addr:street";
     73    private String tagBuilding = "building";
     74    private String relationAddrType = "associatedStreet";
     75    private String relationAddrName = "name";
     76    private String relationAddrStreetRole = "street";
     77    private String relationMemberHouse = "house";
    7078   
    7179    private JRadioButton plus_one = new JRadioButton("+1", false);
    72     private JRadioButton plus_two = new JRadioButton("+2", true);
     80    private JRadioButton plus_two = new JRadioButton("+2", true); // enable this by default
    7381    private JRadioButton minus_one = new JRadioButton("-1", false);
    7482    private JRadioButton minus_two = new JRadioButton("-2", false);
     83    final JCheckBox tagPolygon = new JCheckBox(tr("on polygon"));
    7584
    7685    JDialog dialog = null;
     
    8089    JLabel link = new JLabel();
    8190    private Way selectedWay;
     91    private Relation selectedRelation;
    8292   
    8393    MapFrame mapFrame;
     
    8595    public Address(MapFrame mapFrame) {
    8696        super(tr("Add address"), "buildings",
    87                 tr("Create house number and street name relation"),
     97                tr("Helping tool for tag address"),
    8898                Shortcut.registerShortcut("mapmode:buildings", tr("Mode: {0}", tr("Buildings")), KeyEvent.VK_E, Shortcut.GROUP_EDIT),
    8999                mapFrame, getCursor());
     
    114124        Point mousePos = e.getPoint();
    115125        List<Way> mouseOnExistingWays = new ArrayList<Way>();
     126        List<Way> mouseOnExistingBuildingWays = new ArrayList<Way>();
    116127        mouseOnExistingWays = new ArrayList<Way>();
    117128        Node currentMouseNode = mv.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
    118129        if (currentMouseNode != null) {
     130            // click on existing node
     131            setNewSelection(currentMouseNode);
    119132            String num = currentMouseNode.get(tagHouseNumber);
    120133            if (num != null) {
    121134                try {
     135                    // add new address
    122136                    Integer.parseInt(num);
    123137                    inputNumber.setText(num);
     
    129143            if (currentMouseNode.get(tagHouseStreet) != null) {
    130144                inputStreet.setText(currentMouseNode.get(tagHouseNumber));
    131                 setSelectedWay(null);
     145                setSelectedWay((Way)null);
    132146            } else {
    133147                // check if the node belongs to an associatedStreet relation
    134148                List<OsmPrimitive> l = currentMouseNode.getReferrers();
     149                boolean nodeBelongsToRelation = false;
    135150                for (OsmPrimitive osm : l) {
    136151                    if (osm instanceof Relation && osm.hasKey("type") && osm.get("type").equals(relationAddrType)) {
    137                         if (osm.hasKey(relationStreetNameAttr)) {
    138                             inputStreet.setText(osm.get(relationStreetNameAttr));
    139                             setSelectedWay(null);
    140                             break;
    141                         } else {
    142                             for (RelationMember rm : ((Relation)osm).getMembers())
    143                                 if (rm.getRole().equals(relationAddrStreetRole)) {
    144                                     OsmPrimitive osp = rm.getMember();
    145                                     if (osp instanceof Way && osp.hasKey(tagHighwayName)) {
    146                                         inputStreet.setText(osp.get(tagHighwayName));
    147                                         setSelectedWay((Way)osp);
    148                                         break;
    149                                     }
     152                        for (RelationMember rm : ((Relation)osm).getMembers()) {
     153                            if (rm.getRole().equals(relationAddrStreetRole)) {
     154                                OsmPrimitive osp = rm.getMember();
     155                                if (osp instanceof Way && osp.hasKey(tagHighwayName)) {
     156                                    inputStreet.setText(osp.get(tagHighwayName));
     157                                    setSelectedWay((Way)osp, (Relation)osm);
     158                                    nodeBelongsToRelation = true;
     159                                    break;
    150160                                }
     161                            }
    151162                        }
    152163                    }
     164                }
     165                if (!nodeBelongsToRelation) {
     166                    // node exists but doesn't carry address information : add tags like a new node
     167                    Collection<Command> cmds = new LinkedList<Command>();
     168                    addAddrToPrimitive(currentMouseNode, cmds);
    153169                }
    154170            }
     
    158174                if (ws.way.get(tagHighway) != null && ws.way.get(tagHighwayName) != null)
    159175                    mouseOnExistingWays.add(ws.way);
     176                else if (ws.way.get(tagBuilding) != null && ws.way.get(tagHouseNumber) == null)
     177                    mouseOnExistingBuildingWays.add(ws.way);
    160178            }
    161179            if (mouseOnExistingWays.size() == 1) {
     
    163181                inputStreet.setText(mouseOnExistingWays.get(0).get(tagHighwayName));
    164182                setSelectedWay(mouseOnExistingWays.get(0));
     183                inputNumber.setText("");
     184                setNewSelection(mouseOnExistingWays.get(0));
    165185            } else if (mouseOnExistingWays.size() == 0) {
    166186                // clicked a non highway and not a node => add the new address
     
    168188                    Toolkit.getDefaultToolkit().beep();
    169189                } else {
    170                     Node n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
    171190                    Collection<Command> cmds = new LinkedList<Command>();
    172                     if (n == null)
    173                         n = createNewNode(e, cmds);
    174                     addAddrToNode(n, cmds);
    175                     if (cmds.size() > 0) {
    176                         Command c = new SequenceCommand("Add node address", cmds);
    177                         Main.main.undoRedo.add(c);
     191                    if (tagPolygon.isSelected()) {
     192                        addAddrToPolygon(mouseOnExistingBuildingWays, cmds);
     193                    } else {
     194                        Node n = createNewNode(e, cmds);
     195                        addAddrToPrimitive(n, cmds);
    178196                    }
    179197                }
     
    183201    }
    184202   
    185     private void addAddrToNode(Node n, Collection<Command> cmds) {
     203    private void addAddrToPolygon(List<Way> mouseOnExistingBuildingWays, Collection<Command> cmds) {
     204        for (Way w:mouseOnExistingBuildingWays) {
     205            cmds.add(new ChangePropertyCommand(w, tagHouseNumber, inputNumber.getText()));
     206            addAddrToPrimitive(w, cmds);
     207        }
     208    }
     209   
     210    private void addAddrToPrimitive(OsmPrimitive osm, Collection<Command> cmds) {
     211        // add the current tag addr:housenumber in node and member in relation
     212        cmds.add(new ChangePropertyCommand(osm, tagHouseNumber, inputNumber.getText()));           
     213        if (Main.pref.getBoolean("cadastrewms.addr.dontUseRelation", false)) {
     214            cmds.add(new ChangePropertyCommand(osm, tagHouseStreet, inputStreet.getText()));
     215        } else if (selectedWay != null) {
     216           // add the node to its relation
     217           if (selectedRelation != null) {
     218               RelationMember rm = new RelationMember(relationMemberHouse, osm);
     219               Relation newRel = new Relation(selectedRelation);
     220               newRel.addMember(rm);
     221               cmds.add(new ChangeCommand(selectedRelation, newRel));
     222           } else {
     223               // create new relation
     224               Relation newRel = new Relation();
     225               newRel.put("type", relationAddrType);
     226               newRel.put(relationAddrName, selectedWay.get(tagHighwayName));
     227               newRel.addMember(new RelationMember(relationAddrStreetRole, selectedWay));
     228               newRel.addMember(new RelationMember(relationMemberHouse, osm));
     229               cmds.add(new AddCommand(newRel));
     230           }
     231        }
    186232        try {
    187             // add the tag addr:housenumber in node and member in relation
    188             cmds.add(new ChangePropertyCommand(n, tagHouseNumber, inputNumber.getText()));           
    189             cmds.add(new ChangePropertyCommand(n, tagHouseStreet, inputStreet.getText()));           
    190233            applyInputNumberChange();
     234            Command c = new SequenceCommand("Add node address", cmds);
     235            Main.main.undoRedo.add(c);
     236            setNewSelection(osm);
    191237        } catch (NumberFormatException en) {
    192238            System.out.println("Unable to parse house number \"" + inputNumber.getText() + "\"");
     
    335381        link.setEnabled(false);
    336382        JPanel p = new JPanel(new GridBagLayout());
    337         JLabel number = new JLabel(tr("Number"));
     383        JLabel number = new JLabel(tr("Next no"));
    338384        JLabel street = new JLabel(tr("Street"));
    339385        p.add(number, GBC.std().insets(0, 0, 0, 0));
     
    350396                inputNumber.setText("");
    351397                inputStreet.setText("");
    352                 setSelectedWay(null);
     398                setSelectedWay((Way)null);
    353399            }
    354400        });
     
    359405        bgIncremental.add(minus_two);
    360406        p.add(minus_one, GBC.std().insets(10, 0, 10, 0));
    361         p.add(plus_one, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
     407//        p.add(plus_one, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
     408        p.add(plus_one, GBC.std().insets(0, 0, 10, 0));
     409        tagPolygon.setSelected(Main.pref.getBoolean("cadastrewms.addr.onBuilding", false));
     410        tagPolygon.addChangeListener(new ChangeListener() {
     411            @Override
     412            public void stateChanged(ChangeEvent arg0) {
     413                Main.pref.put("cadastrewms.addr.onBuilding", tagPolygon.isSelected());
     414            }
     415        });
     416        p.add(tagPolygon, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 0));
    362417        p.add(minus_two, GBC.std().insets(10, 0, 10, 0));
    363         p.add(plus_two, GBC.std().insets(10, 0, 10, 0));
    364         p.add(clearButton, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
     418        p.add(plus_two, GBC.std().insets(0, 0, 10, 0));
     419        p.add(clearButton, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 0));
    365420   
    366421        final Object[] options = {};
     
    373428        dialog.addComponentListener(new ComponentAdapter() {
    374429            protected void rememberGeometry() {
    375                 Main.pref.put("cadastrewms.addr_bounds", dialog.getX()+","+dialog.getY()+","+dialog.getWidth()+","+dialog.getHeight());
     430                Main.pref.put("cadastrewms.addr.bounds", dialog.getX()+","+dialog.getY()+","+dialog.getWidth()+","+dialog.getHeight());
    376431            }
    377432            @Override public void componentMoved(ComponentEvent e) {
     
    382437            }
    383438        });
    384         String bounds = Main.pref.get("cadastrewms.addr_bounds",null);
     439        dialog.addWindowListener(new WindowListener() {
     440            @Override
     441            public void windowClosing(WindowEvent arg0) {
     442                exitMode();
     443                Main.map.selectMapMode((MapMode)Main.map.getDefaultButtonAction());
     444            }
     445            public void windowClosed(WindowEvent e) {}
     446            public void windowActivated(WindowEvent arg0) {}
     447            public void windowDeactivated(WindowEvent arg0) {}
     448            public void windowDeiconified(WindowEvent arg0) {}
     449            public void windowIconified(WindowEvent arg0) {}
     450            public void windowOpened(WindowEvent arg0) {}
     451        });
     452        String bounds = Main.pref.get("cadastrewms.addr.bounds",null);
    385453        if (bounds != null) {
    386454            String[] b = bounds.split(",");
     
    388456                    Integer.parseInt(b[0]),Integer.parseInt(b[1]),Integer.parseInt(b[2]),Integer.parseInt(b[3])));
    389457        }
    390 
    391 
    392    
    393    
    394 //        exitMode();
    395 //        Main.map.selectMapMode((MapMode)Main.map.getDefaultButtonAction());
    396458}
    397459   
    398460    private void setSelectedWay(Way w) {
    399461        this.selectedWay = w;
    400         if (w == null)
     462        this.selectedRelation = null;
     463        if (w == null) {
    401464            link.setEnabled(false);
    402         else
     465        } else
    403466            link.setEnabled(true);
    404467    }
     468   
     469    private void setSelectedWay(Way w, Relation r) {
     470        setSelectedWay(w);
     471        this.selectedRelation = r;
     472    }
     473   
     474    private void setNewSelection(OsmPrimitive osm) {
     475        Collection<OsmPrimitive> newSelection = new LinkedList<OsmPrimitive>(Main.main.getCurrentDataSet().getSelected());
     476        newSelection.clear();
     477        newSelection.add(osm);
     478        getCurrentDataSet().setSelected(osm);
     479    }
    405480
    406481}
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastrePreferenceSetting.java

    r21191 r22186  
    4141    private JCheckBox disableImageCropping = new JCheckBox(tr("Disable image cropping during georeferencing."));
    4242   
     43    private JCheckBox autoFirstLayer = new JCheckBox(tr("Select first WMS layer in list."));
     44   
     45    private JCheckBox dontUseRelation = new JCheckBox(tr("Don't use relation for addresses (but \"addr:street\" on nodes)."));
     46   
    4347    private JRadioButton grabMultiplier1 = new JRadioButton("", true);
    4448
     
    5761    private JRadioButton crosspiece4 = new JRadioButton("100m");
    5862
    59     private JCheckBox autoFirstLayer = new JCheckBox(tr("Select first WMS layer in list."));
    60    
    6163    private JRadioButton grabRes1 = new JRadioButton("high");
    6264
     
    324326        cadastrewms.add(jLabelCacheSize, GBC.std().insets(20, 0, 0, 0));
    325327        cadastrewms.add(cacheSize, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 200, 5));
     328
    326329        // separator
    327330        cadastrewms.add(new JSeparator(SwingConstants.HORIZONTAL), GBC.eol().fill(GBC.HORIZONTAL));
     331       
     332        // option to select the first WMS layer
    328333        autoFirstLayer.setSelected(Main.pref.getBoolean("cadastrewms.autoFirstLayer", false));
    329334        autoFirstLayer.setToolTipText(tr("Automatically selects the first WMS layer if multiple layers exist when grabbing."));
    330335        cadastrewms.add(autoFirstLayer, GBC.eop().insets(0, 0, 0, 0));
     336
     337        // separator
     338        cadastrewms.add(new JSeparator(SwingConstants.HORIZONTAL), GBC.eol().fill(GBC.HORIZONTAL));
     339
     340        // option to use or not relations in addresses
     341        dontUseRelation.setSelected(Main.pref.getBoolean("cadastrewms.addr.dontUseRelation", false));
     342        dontUseRelation.setToolTipText(tr("Enable this to use the tag \"add:street\" on nodes."));
     343        cadastrewms.add(dontUseRelation, GBC.eop().insets(0, 0, 0, 0));
     344       
     345        // end of dialog, scroll bar
    331346        cadastrewms.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
    332 //        JTabbedPane cadastrecontent = new JTabbedPane();
    333 //        cadastrecontent.add(cadastrewms);
    334347        JScrollPane scrollpane = new JScrollPane(cadastrewms);
    335348        scrollpane.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
    336349        cadastrewmsMast.add(scrollpane, GBC.eol().fill(GBC.BOTH));
    337 
    338350    }
    339351
     
    403415        Main.pref.put("cadastrewms.autoFirstLayer", autoFirstLayer.isSelected());
    404416        CacheControl.cacheEnabled = enableCache.isSelected();
     417        Main.pref.put("cadastrewms.addr.dontUseRelation", dontUseRelation.isSelected());
    405418        CadastrePlugin.refreshConfiguration();
    406419        CadastrePlugin.refreshMenu();
Note: See TracChangeset for help on using the changeset viewer.