Changeset 25670 in osm for applications


Ignore:
Timestamp:
2011-03-22T22:57:56+01:00 (13 years ago)
Author:
zverik
Message:

popup menu for chosen relation (relcontext plugin)

Location:
applications/editors/josm/plugins/relcontext/src/relcontext
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelationComponent.java

    r25669 r25670  
    1 /*
    2  * To change this template, choose Tools | Templates
    3  * and open the template in the editor.
    4  */
    5 
    61package relcontext;
    72
    8 import java.awt.event.MouseAdapter;
    9 import java.awt.event.MouseEvent;
    103import javax.swing.JLabel;
    11 import org.openstreetmap.josm.Main;
    124import org.openstreetmap.josm.data.osm.Relation;
    13 import org.openstreetmap.josm.gui.DefaultNameFormatter;
    145
    156/**
     
    2819        this.chRel = rel;
    2920        rel.addChosenRelationListener(this);
    30         addMouseListener(new MouseAdapter() {
    31             @Override
    32             public void mouseClicked( MouseEvent e ) {
    33                 if( chRel.get() != null && Main.map.mapView.getEditLayer() != null )
    34                     Main.map.mapView.getEditLayer().data.setSelected(chRel.get());
    35             }
    36         });
    3721    }
    3822
  • applications/editors/josm/plugins/relcontext/src/relcontext/RelContextDialog.java

    r25669 r25670  
    2626import java.util.*;
    2727import javax.swing.*;
    28 import org.openstreetmap.josm.data.osm.DataSet;
    2928import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
    3029import relcontext.actions.*;
     
    4039    private ChosenRelation chosenRelation;
    4140    private JPanel topLine;
     41    private ChosenRelationPopupMenu popupMenu;
    4242
    4343    public RelContextDialog() {
     
    8080        topLeftButtons.add(new JButton(new ClearChosenRelationAction(chosenRelation)));
    8181        topLine.add(topLeftButtons, BorderLayout.WEST);
    82         topLine.add(new ChosenRelationComponent(chosenRelation), BorderLayout.CENTER);
     82        final ChosenRelationComponent chosenRelationComponent = new ChosenRelationComponent(chosenRelation);
     83        chosenRelationComponent.addMouseListener(new ChosenRelationMouseAdapter());
     84        topLine.add(chosenRelationComponent, BorderLayout.CENTER);
    8385        JPanel topRightButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    8486        final Action downloadChosenRelationAction = new DownloadChosenRelationAction(chosenRelation);
     
    104106
    105107        add(rcPanel, BorderLayout.CENTER);
     108
     109        popupMenu = new ChosenRelationPopupMenu();
    106110    }
    107111
     
    160164        updateSelection();
    161165    }
     166
     167    private class ChosenRelationMouseAdapter extends MouseAdapter {
     168        @Override
     169        public void mouseClicked( MouseEvent e ) {
     170            if( SwingUtilities.isLeftMouseButton(e) && chosenRelation.get() != null && Main.map.mapView.getEditLayer() != null ) {
     171                Main.map.mapView.getEditLayer().data.setSelected(chosenRelation.get());
     172            }
     173        }
     174
     175        @Override
     176        public void mousePressed( MouseEvent e ) {
     177            checkPopup(e);
     178        }
     179
     180        @Override
     181        public void mouseReleased( MouseEvent e ) {
     182            checkPopup(e);
     183        }
     184
     185        private void checkPopup( MouseEvent e ) {
     186            if( e.isPopupTrigger() && chosenRelation.get() != null ) {
     187                popupMenu.show(e.getComponent(), e.getX(), e.getY());
     188            }
     189        }
     190    }
     191
     192    private class ChosenRelationPopupMenu extends JPopupMenu {
     193        public ChosenRelationPopupMenu() {
     194            add(new SelectMembersAction(chosenRelation));
     195            add(new DeleteChosenRelationAction(chosenRelation));
     196        }
     197    }
    162198}
  • applications/editors/josm/plugins/relcontext/src/relcontext/actions/CreateMultipolygonAction.java

    r25669 r25670  
    11package relcontext.actions;
    22
    3 import java.util.Collection;
    43import org.openstreetmap.josm.data.osm.OsmPrimitive;
    54import static org.openstreetmap.josm.tools.I18n.tr;
    65import java.awt.event.ActionEvent;
    7 import java.util.LinkedList;
     6import java.util.*;
     7import javax.swing.JOptionPane;
    88import org.openstreetmap.josm.Main;
    99import org.openstreetmap.josm.actions.JosmAction;
    1010import org.openstreetmap.josm.command.AddCommand;
     11import org.openstreetmap.josm.command.ChangePropertyCommand;
    1112import org.openstreetmap.josm.command.Command;
     13import org.openstreetmap.josm.command.SequenceCommand;
     14import org.openstreetmap.josm.data.osm.MultipolygonCreate;
    1215import org.openstreetmap.josm.data.osm.Relation;
    1316import org.openstreetmap.josm.data.osm.RelationMember;
     
    3639
    3740    public void actionPerformed( ActionEvent e ) {
    38         // todo!
    39        
     41        // for now, just copying standard action
     42        MultipolygonCreate mpc = new MultipolygonCreate();
     43        String error = mpc.makeFromWays(getCurrentDataSet().getSelectedWays());
     44        if( error != null ) {
     45            JOptionPane.showMessageDialog(Main.parent, error);
     46            return;
     47        }
    4048        Relation rel = new Relation();
    4149        rel.put("type", "multipolygon");
    42         for( OsmPrimitive selected : getCurrentDataSet().getSelected() ) {
    43             rel.addMember(new RelationMember("", selected));
    44         }
    45 
    46         Collection<Command> cmds = new LinkedList<Command>();
    47         Main.main.undoRedo.add(new AddCommand(rel));
    48 
    49         if( chRel != null ) {
     50        for( MultipolygonCreate.JoinedPolygon poly : mpc.outerWays )
     51            for( Way w : poly.ways )
     52                rel.addMember(new RelationMember("outer", w));
     53        for( MultipolygonCreate.JoinedPolygon poly : mpc.innerWays )
     54            for( Way w : poly.ways )
     55                rel.addMember(new RelationMember("inner", w));
     56        List<Command> list = removeTagsFromInnerWays(rel);
     57        list.add(new AddCommand(rel));
     58        Main.main.undoRedo.add(new SequenceCommand(tr("Create multipolygon"), list));
     59       
     60        if( chRel != null )
    5061            chRel.set(rel);
    51         }
    5262    }
    5363
     
    7585        setEnabled(enabled);
    7686    }
     87
     88    /**
     89     * This method removes tags/value pairs from inner ways that are present in relation or outer ways.
     90     * It was copypasted from the standard {@link org.openstreetmap.josm.actions.CreateMultipolygonAction}.
     91     * Todo: rewrite it.
     92     */
     93    private List<Command> removeTagsFromInnerWays(Relation relation) {
     94        Map<String, String> values = new HashMap<String, String>();
     95
     96        if (relation.hasKeys()){
     97            for(String key: relation.keySet()) {
     98                values.put(key, relation.get(key));
     99            }
     100        }
     101
     102        List<Way> innerWays = new ArrayList<Way>();
     103
     104        for (RelationMember m: relation.getMembers()) {
     105
     106            if (m.hasRole() && m.getRole() == "inner" && m.isWay() && m.getWay().hasKeys()) {
     107                innerWays.add(m.getWay());
     108            }
     109
     110            if (m.hasRole() && m.getRole() == "outer" && m.isWay() && m.getWay().hasKeys()) {
     111                Way way = m.getWay();
     112                for (String key: way.keySet()) {
     113                    if (!values.containsKey(key)) { //relation values take precedence
     114                        values.put(key, way.get(key));
     115                    }
     116                }
     117            }
     118        }
     119
     120        List<Command> commands = new ArrayList<Command>();
     121
     122        for(String key: values.keySet()) {
     123            List<OsmPrimitive> affectedWays = new ArrayList<OsmPrimitive>();
     124            String value = values.get(key);
     125
     126            for (Way way: innerWays) {
     127                if (value.equals(way.get(key))) {
     128                    affectedWays.add(way);
     129                }
     130            }
     131
     132            if (affectedWays.size() > 0) {
     133                commands.add(new ChangePropertyCommand(affectedWays, key, null));
     134            }
     135        }
     136
     137        return commands;
     138    }
    77139}
Note: See TracChangeset for help on using the changeset viewer.