Changeset 23970 in osm for applications/editors/josm


Ignore:
Timestamp:
2010-10-31T19:36:05+01:00 (14 years ago)
Author:
oliverw
Message:
  • Added action RemoveAddressTagsAction
  • Collect all commands within a single transaction per action
Location:
applications/editors/josm/plugins/FixAddresses
Files:
5 added
7 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/AddressNode.java

    r23967 r23970  
    171171                }
    172172                return TagUtils.getAddrCountryValue(osmObject);
     173        }
     174       
     175        /**
     176         * Removes all addresss related tags from the node or way.
     177         */
     178        public void removeAllAddressTags() {
     179                removeOSMTag(TagUtils.ADDR_CITY_TAG);
     180                removeOSMTag(TagUtils.ADDR_COUNTRY_TAG);
     181                removeOSMTag(TagUtils.ADDR_POSTCODE_TAG);
     182                removeOSMTag(TagUtils.ADDR_HOUSENUMBER_TAG);
     183                removeOSMTag(TagUtils.ADDR_STATE_TAG);
     184                removeOSMTag(TagUtils.ADDR_STREET_TAG);
    173185        }
    174186       
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/INodeEntity.java

    r23961 r23970  
    5858         */
    5959        public LatLon getCoor();
     60       
     61        /**
     62         * Adds a command listener.
     63         * @param listener
     64         */
     65        public void addCommandListener(ICommandListener listener);
     66       
     67        /**
     68         * Removes a command listener.
     69         * @param listener
     70         */
     71        public void removeCommandListener(ICommandListener listener);
    6072}
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/NodeEntityBase.java

    r23967 r23970  
    1919import java.util.List;
    2020
    21 import org.openstreetmap.josm.Main;
    22 import org.openstreetmap.josm.command.ChangeCommand;
     21import org.openstreetmap.josm.command.ChangePropertyCommand;
     22import org.openstreetmap.josm.command.Command;
    2323import org.openstreetmap.josm.data.coor.LatLon;
    2424import org.openstreetmap.josm.data.osm.BBox;
     
    2929public class NodeEntityBase implements INodeEntity, Comparable<INodeEntity> {
    3030        public static final String ANONYMOUS = tr("No name");
    31         private static List<IAddressEditContainerListener> listeners = new ArrayList<IAddressEditContainerListener>();
     31        private static List<IAddressEditContainerListener> containerListeners = new ArrayList<IAddressEditContainerListener>();
     32        private List<ICommandListener> cmdListeners = new ArrayList<ICommandListener>();
    3233       
    3334        protected OsmPrimitive osmObject;
     
    5354         */
    5455        public static void addChangedListener(IAddressEditContainerListener listener) {
    55                 listeners.add(listener);
     56                containerListeners.add(listener);
    5657        }
    5758       
     
    6162         */
    6263        public static void removeChangedListener(IAddressEditContainerListener listener) {
    63                 listeners.remove(listener);
     64                containerListeners.remove(listener);
    6465        }
    6566       
     
    6869         */
    6970        protected static void fireEntityChanged(INodeEntity entity) {
    70                 for (IAddressEditContainerListener listener : listeners) {
     71                for (IAddressEditContainerListener listener : containerListeners) {
    7172                        listener.entityChanged(entity);
    7273                }
    7374        }
     75       
     76        /**
     77         * Adds a command listener.
     78         * @param listener
     79         */
     80        public void addCommandListener(ICommandListener listener) {
     81                cmdListeners.add(listener);
     82        }
     83       
     84        /**
     85         * Removes a command listener.
     86         * @param listener
     87         */
     88        public void removeCommandListener(ICommandListener listener) {
     89                cmdListeners.remove(listener);
     90        }
     91       
     92        /**
     93         * Notifies clients that an entity has issued a command.
     94         *
     95         * @param source the entity that issued the command.
     96         * @param cmd the command to execute.
     97         */
     98        protected void fireCommandIssued(Command cmd) {
     99                if (cmdListeners.size() == 0) {
     100                        throw new RuntimeException("Object has no TX context: " + this);
     101                }
     102               
     103                for (ICommandListener l : cmdListeners) {
     104                        l.commandIssued(this, cmd);
     105                }
     106        }       
    74107
    75108        public OsmPrimitive getOsmObject() {
     
    79112        @Override
    80113        public List<INodeEntity> getChildren() {
    81                 // TODO Auto-generated method stub
    82114                return null;
    83115        }
     
    109141         * @param tag The tag to change.
    110142         * @param newValue The new value for the tag.
     143         * @param cmd The surrounding command sequence
    111144         */
    112145        protected void setOSMTag(String tag, String newValue) {
    113                 OsmPrimitive oldObject = osmObject;
    114                 OsmPrimitive newObject = null;
    115                        
    116                 // I would appreciate a clone method...
    117                 if (oldObject instanceof Node) {
    118                         newObject = new Node();
    119                 } else if (oldObject instanceof Way) {
    120                         newObject = new Way();
    121                 }
     146                if (StringUtils.isNullOrEmpty(tag)) return;
    122147               
    123                 if (newObject != null) {
    124                         newObject.cloneFrom(oldObject);
    125                         newObject.put(tag, newValue);
    126                         Main.main.undoRedo.add( new ChangeCommand(oldObject, newObject));
     148                if (osmObject != null && osmObject.hasKey(tag)) {
     149                        fireCommandIssued(new ChangePropertyCommand(osmObject, tag, newValue));
    127150                        fireEntityChanged(this);
    128                 } else {
    129                         throw new RuntimeException("Cannot modify tag for " + osmObject);
    130                 }
     151                }
     152        }
     153       
     154        /**
     155         * Removes the given tag from the OSM object.
     156         *
     157         * @param tag the tag
     158         */
     159        protected void removeOSMTag(String tag) {
     160                setOSMTag(tag, null); // a value of null removes the tag
    131161        }
    132162
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/AbstractAddressEditAction.java

    r23943 r23970  
    1515
    1616import java.awt.event.ActionEvent;
    17 
     17import java.util.ArrayList;
     18import java.util.List;
     19
     20import org.openstreetmap.josm.Main;
    1821import org.openstreetmap.josm.actions.JosmAction;
     22import org.openstreetmap.josm.command.Command;
     23import org.openstreetmap.josm.command.SequenceCommand;
    1924import org.openstreetmap.josm.plugins.fixAddresses.AddressEditContainer;
    2025import org.openstreetmap.josm.plugins.fixAddresses.IAddressEditContainerListener;
     26import org.openstreetmap.josm.plugins.fixAddresses.ICommandListener;
    2127import org.openstreetmap.josm.plugins.fixAddresses.INodeEntity;
     28import org.openstreetmap.josm.plugins.fixAddresses.StringUtils;
    2229
    2330/**
     
    3441
    3542@SuppressWarnings("serial")
    36 public abstract class AbstractAddressEditAction extends JosmAction implements IAddressEditContainerListener {   
     43public abstract class AbstractAddressEditAction extends JosmAction implements IAddressEditContainerListener, ICommandListener {
    3744        private AddressEditSelectionEvent event;
    3845        protected AddressEditContainer container;
     46        private List<Command> commands;
     47        private String txName;
    3948
    4049        /**
     
    167176                updateEnabledState();           
    168177        }
     178       
     179        /**
     180         * Begins the transaction (command sequence). Must be called by every subclass before
     181         * any modification on OSM objects starts.
     182         *
     183         * @param txName the name of the transaction (e. g. "change address tags").
     184         */
     185        public void beginTransaction(String txName) {
     186                if (commands != null && commands.size() > 0) {
     187                        throw new RuntimeException("TX has not been closed (missing finishTransaction?)");
     188                }
     189               
     190                commands = new ArrayList<Command>();
     191                if (StringUtils.isNullOrEmpty(txName)) {
     192                        throw new RuntimeException("Transaction must have a name");
     193                }
     194                this.txName = txName;
     195        }
     196       
     197        /**
     198         * Finishes the transaction and passes the command sequence to the framework.
     199         */
     200        public void finishTransaction() {
     201                if (commands == null) {
     202                        throw new RuntimeException("No command list available. Did you forget to call beginTransaction?");
     203                }
     204                // execute the command
     205                Main.main.undoRedo.add(new SequenceCommand(txName, commands));
     206                commands.clear();
     207                container.invalidate();
     208        }
     209       
     210        /**
     211         * Begins the transaction for a single object.
     212         *
     213         * @param entity the entity
     214         */
     215        public void beginObjectTransaction(INodeEntity entity) {
     216                if (entity != null) {
     217                        entity.addCommandListener(this);
     218                } else {
     219                        throw new RuntimeException("Entity must not be null");
     220                }
     221        }
     222       
     223        /**
     224         * Finishes the transaction for a single object.
     225         *
     226         * @param entity the entity
     227         */
     228        public void finishObjectTransaction(INodeEntity entity) {
     229                if (entity != null) {
     230                        entity.removeCommandListener(this);
     231                } else {
     232                        throw new RuntimeException("Entity must not be null");
     233                }
     234        }
     235       
     236        /* (non-Javadoc)
     237         * @see org.openstreetmap.josm.plugins.fixAddresses.ICommandListener#commandIssued(org.openstreetmap.josm.plugins.fixAddresses.INodeEntity, org.openstreetmap.josm.command.Command)
     238         */
     239        @Override
     240        public void commandIssued(INodeEntity entity, Command command) {
     241                if (commands == null) {
     242                        throw new RuntimeException("No command list available. Did you forget to call beginTransaction?");
     243                }
     244                commands.add(command);
     245        }
    169246}
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/AddressEditDialog.java

    r23969 r23970  
    7575        private GuessAddressDataAction guessAddressAction = new GuessAddressDataAction();
    7676        private SelectAddressesInMapAction selectAddressesInMapAction = new SelectAddressesInMapAction();
     77        private RemoveAddressTagsAction removeAddressTagsAction = new RemoveAddressTagsAction();
    7778       
    7879        private AbstractAddressEditAction[] actions = new AbstractAddressEditAction[] {
     
    8081                guessAddressAction,
    8182                applyAllGuessesAction,
    82                 selectAddressesInMapAction
     83                selectAddressesInMapAction,
     84                removeAddressTagsAction
    8385        };
    8486        private JLabel streetLabel;
     
    145147                               
    146148                                unresolvedButtons.add(new JSeparator());
     149                               
     150                                SideButton removeAddressTags = new SideButton(removeAddressTagsAction);                                                                                                                   
     151                                unresolvedButtons.add(removeAddressTags);
     152                               
     153                                unresolvedButtons.add(new JSeparator());
     154                               
    147155                                SideButton selectInMap = new SideButton(selectAddressesInMapAction);                                                                                                                       
    148156                                unresolvedButtons.add(selectInMap);
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/ApplyAllGuessesAction.java

    r23944 r23970  
    5050
    5151        private void applyGuesses(List<AddressNode> addrToFix) {
     52                beginTransaction(tr("Applied guessed values"));
    5253                List<AddressNode> addrToFixShadow = new ArrayList<AddressNode>(addrToFix);
    5354                for (AddressNode aNode : addrToFixShadow) {
     55                        beginObjectTransaction(aNode);
    5456                        aNode.applyAllGuesses();
     57                        finishObjectTransaction(aNode);
    5558                }
     59                finishTransaction();
    5660        }
    5761
  • applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/AssignAddressToStreetAction.java

    r23944 r23970  
    2626 *
    2727 */
    28 public class AssignAddressToStreetAction extends AbstractAddressEditAction {
     28public class AssignAddressToStreetAction extends AbstractAddressEditAction  {
    2929
    3030        public AssignAddressToStreetAction() {
     
    4343        public void addressEditActionPerformed(AddressEditSelectionEvent ev) {         
    4444                StreetNode streetNode = ev.getSelectedStreet();
    45                                
     45                                               
    4646                if (streetNode != null && ev.getSelectedUnresolvedAddresses() != null) {
     47                        beginTransaction(tr("Set street name") + " '" + streetNode.getName() + "'");
    4748                        for (AddressNode addrNode : ev.getSelectedUnresolvedAddresses()) {
    48                                 addrNode.assignStreet(streetNode);                             
     49                                beginObjectTransaction(addrNode);
     50                                addrNode.assignStreet(streetNode);
     51                                finishObjectTransaction(addrNode);
    4952                        }
     53                        finishTransaction();
    5054                }
     55               
    5156        }
    5257
Note: See TracChangeset for help on using the changeset viewer.