Changeset 2622 in josm for trunk


Ignore:
Timestamp:
2009-12-12T20:21:36+01:00 (12 years ago)
Author:
jttt
Message:

Dataset changed events refactored - DatasetEventManager now allows to register listeners to current dataset (no need to reregister when activeLayer is changed, events from current dataset are always returned), possibility to automatically receive events in EDT

Location:
trunk/src/org/openstreetmap/josm
Files:
11 added
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r2613 r2622  
    2020
    2121import org.openstreetmap.josm.data.SelectionChangedListener;
     22import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
     23import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
     24import org.openstreetmap.josm.data.osm.event.DataSetListener;
     25import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
     26import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
     27import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
     28import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
     29import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
     30import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
    2231
    2332
     
    816825    }
    817826
    818     private void fireDataChanged() {
     827    private void fireEvent(AbstractDatasetChangedEvent event) {
    819828        if (updateCount == 0) {
    820829            for (DataSetListener dsl : listeners) {
    821                 dsl.dataChanged();
    822             }
    823         }
     830                event.fire(dsl);
     831            }
     832        }
     833    }
     834
     835    private void fireDataChanged() {
     836        fireEvent(new DataChangedEvent(this));
    824837    }
    825838
    826839    void firePrimitivesAdded(Collection<? extends OsmPrimitive> added) {
    827         if (updateCount == 0) {
    828             for (DataSetListener dsl : listeners) {
    829                 dsl.primtivesAdded(added);
    830             }
    831         }
     840        fireEvent(new PrimitivesAddedEvent(this, added));
    832841    }
    833842
    834843    void firePrimitivesRemoved(Collection<? extends OsmPrimitive> removed) {
    835         if (updateCount == 0) {
    836             for (DataSetListener dsl : listeners) {
    837                 dsl.primtivesRemoved(removed);
    838             }
    839         }
     844        fireEvent(new PrimitivesRemovedEvent(this, removed));
    840845    }
    841846
    842847    void fireTagsChanged(OsmPrimitive prim) {
    843         if (updateCount == 0) {
    844             for (DataSetListener dsl : listeners) {
    845                 dsl.tagsChanged(prim);
    846             }
    847         }
     848        fireEvent(new TagsChangedEvent(this, prim));
    848849    }
    849850
    850851    void fireRelationMembersChanged(Relation r) {
    851         if (updateCount == 0) {
    852             for (DataSetListener dsl : listeners) {
    853                 dsl.relationMembersChanged(r);
    854             }
    855         }
     852        fireEvent(new RelationMembersChangedEvent(this, r));
    856853    }
    857854
    858855    void fireNodeMoved(Node node) {
    859856        reindexNode(node);
    860         if (updateCount == 0) {
    861             for (DataSetListener dsl : listeners) {
    862                 dsl.nodeMoved(node);
    863             }
    864         }
     857        fireEvent(new NodeMovedEvent(this, node));
    865858    }
    866859
    867860    void fireWayNodesChanged(Way way) {
    868861        reindexWay(way);
    869         if (updateCount == 0) {
    870             for (DataSetListener dsl : listeners) {
    871                 dsl.wayNodesChanged(way);
    872             }
    873         }
     862        fireEvent(new WayNodesChangedEvent(this, way));
    874863    }
    875864
  • trunk/src/org/openstreetmap/josm/data/osm/event/DataSetListener.java

    r2620 r2622  
    2020 */
    2121
    22 package org.openstreetmap.josm.data.osm;
     22package org.openstreetmap.josm.data.osm.event;
    2323
    24 import java.util.Collection;
    2524
    2625/**
     
    3736     * @param added A collection of newly-visible primitives
    3837     */
    39     void primtivesAdded(Collection<? extends OsmPrimitive> added);
     38    void primtivesAdded(PrimitivesAddedEvent event);
    4039
    4140    /**
     
    4544     * @param removed A collection of newly-invisible primitives
    4645     */
    47     void primtivesRemoved(Collection<? extends OsmPrimitive> removed);
     46    void primtivesRemoved(PrimitivesRemovedEvent event);
    4847
    4948    /**
     
    5352     * @param prim the primitive, whose tags were affected.
    5453     */
    55     void tagsChanged(OsmPrimitive prim);
     54    void tagsChanged(TagsChangedEvent event);
    5655
    5756    /**
     
    5958     * @param node The node that was moved.
    6059     */
    61     void nodeMoved(Node node);
     60    void nodeMoved(NodeMovedEvent event);
    6261
    6362    /**
     
    6564     * @param way The way that was modified.
    6665     */
    67     void wayNodesChanged(Way way);
     66    void wayNodesChanged(WayNodesChangedEvent event);
    6867
    6968    /**
     
    7170     * @param relation The relation that was modified.
    7271     */
    73     void relationMembersChanged(Relation r);
     72    void relationMembersChanged(RelationMembersChangedEvent event);
    7473
    7574    /**
     
    7776     * after operation is completed (Dataset.endUpdate()), {@link #dataChanged()} is called.
    7877     */
    79     void dataChanged();
     78    void dataChanged(DataChangedEvent event);
    8079
    8180}
  • trunk/src/org/openstreetmap/josm/gui/MapView.java

    r2621 r2622  
    246246        if (layer instanceof OsmDataLayer || activeLayer == null) {
    247247            // autoselect the new layer
    248             Layer old = activeLayer;
    249248            setActiveLayer(layer);
    250             fireActiveLayerChanged(old, layer);
    251249        }
    252250        layer.addPropertyChangeListener(this);
  • trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java

    r2621 r2622  
    3333
    3434import org.openstreetmap.josm.Main;
    35 import org.openstreetmap.josm.data.osm.DataSetListener;
    3635import org.openstreetmap.josm.data.osm.NameFormatter;
    37 import org.openstreetmap.josm.data.osm.Node;
    3836import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3937import org.openstreetmap.josm.data.osm.Relation;
    4038import org.openstreetmap.josm.data.osm.RelationMember;
    41 import org.openstreetmap.josm.data.osm.Way;
     39import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
     40import org.openstreetmap.josm.data.osm.event.DataSetListener;
     41import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
     42import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
     43import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
     44import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
     45import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
     46import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
     47import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
    4248import org.openstreetmap.josm.gui.DefaultNameFormatter;
    4349import org.openstreetmap.josm.gui.MapView;
    4450import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
    4551import org.openstreetmap.josm.gui.SideButton;
    46 import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    4752import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
    4853import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
     
    140145        MapView.addLayerChangeListener(this);
    141146        MapView.addLayerChangeListener(newAction);
    142         // Register as a data set listener for the current edit layer only.
    143         // See also activeLayerChanged
    144         if (Main.main.getEditLayer() != null) {
    145             Main.main.getEditLayer().data.addDataSetListener(this);
    146         }
    147         dataChanged();
     147        DatasetEventManager.getInstance().addDatasetListener(this, true);
     148        dataChanged(Main.main.getEditLayer());
    148149    }
    149150
     
    151152        MapView.removeLayerChangeListener(this);
    152153        MapView.removeLayerChangeListener(newAction);
    153         // unregistering from *all* data layer is somewhat overkill but it
    154         // doesn't harm either.
    155         for (OsmDataLayer layer:Main.map.mapView.getLayersOfType(OsmDataLayer.class)) {
    156             layer.data.removeDataSetListener(this);
    157         }
     154        DatasetEventManager.getInstance().removeDatasetListener(this);
    158155    }
    159156
     
    701698    /* ---------------------------------------------------------------------------------- */
    702699    public void activeLayerChange(Layer a, Layer b) {
    703         initFromLayer(b);
    704700        if (a != null && a instanceof OsmDataLayer) {
    705             ((OsmDataLayer)a).data.removeDataSetListener(this);
    706701            ((OsmDataLayer)a).listenerDataChanged.remove(this);
    707702        }
    708703        if (b != null && b instanceof OsmDataLayer) {
    709             ((OsmDataLayer)b).data.addDataSetListener(this);
    710704            ((OsmDataLayer)b).listenerDataChanged.add(this);
    711705        }
     
    719713    /* ---------------------------------------------------------------------------------- */
    720714
    721     public void nodeMoved(Node node) {/* irrelevant in this context */}
    722 
    723     public void wayNodesChanged(Way way) {/* irrelevant in this context */}
    724 
    725     public void primtivesAdded(final Collection<? extends OsmPrimitive> added) {
    726         Runnable task = new Runnable() {
    727             public void run() {
    728                 model.addRelations(added);
    729             }
    730         };
    731         if (SwingUtilities.isEventDispatchThread()) {
    732             task.run();
    733         } else {
    734             SwingUtilities.invokeLater(task);
    735         }
    736     }
    737 
    738     public void primtivesRemoved(final Collection<? extends OsmPrimitive> removed) {
    739         Runnable task = new Runnable() {
    740             public void run() {
    741                 model.removeRelations(removed);
    742             }
    743         };
    744         if (SwingUtilities.isEventDispatchThread()) {
    745             task.run();
    746         } else {
    747             SwingUtilities.invokeLater(task);
    748         }
    749     }
    750 
    751     public void relationMembersChanged(final Relation r) {
    752         Runnable task = new Runnable() {
    753             public void run() {
    754                 List<Relation> sel = model.getSelectedRelations();
    755                 model.sort();
    756                 model.setSelectedRelations(sel);
    757                 displaylist.repaint();
    758             }
    759         };
    760         if (SwingUtilities.isEventDispatchThread()) {
    761             task.run();
    762         } else {
    763             SwingUtilities.invokeLater(task);
    764         }
    765     }
    766 
    767     public void tagsChanged(OsmPrimitive prim) {
     715    public void nodeMoved(NodeMovedEvent event) {/* irrelevant in this context */}
     716
     717    public void wayNodesChanged(WayNodesChangedEvent event) {/* irrelevant in this context */}
     718
     719    public void primtivesAdded(final PrimitivesAddedEvent event) {
     720        model.addRelations(event.getPrimitives());
     721    }
     722
     723    public void primtivesRemoved(final PrimitivesRemovedEvent event) {
     724        model.removeRelations(event.getPrimitives());
     725    }
     726
     727    public void relationMembersChanged(final RelationMembersChangedEvent event) {
     728        List<Relation> sel = model.getSelectedRelations();
     729        model.sort();
     730        model.setSelectedRelations(sel);
     731        displaylist.repaint();
     732    }
     733
     734    public void tagsChanged(TagsChangedEvent event) {
     735        OsmPrimitive prim = event.getPrimitive();
    768736        if (prim == null || ! (prim instanceof Relation))
    769737            return;
    770         Runnable task = new Runnable() {
    771             public void run() {
    772                 // trigger a sort of the relation list because the display name may
    773                 // have changed
    774                 //
    775                 List<Relation> sel = model.getSelectedRelations();
    776                 model.sort();
    777                 model.setSelectedRelations(sel);
    778                 displaylist.repaint();
    779             }
    780         };
    781         if (SwingUtilities.isEventDispatchThread()) {
    782             task.run();
    783         } else {
    784             SwingUtilities.invokeLater(task);
    785         }
    786     }
    787 
    788     public void dataChanged() {
     738        // trigger a sort of the relation list because the display name may
     739        // have changed
     740        //
     741        List<Relation> sel = model.getSelectedRelations();
     742        model.sort();
     743        model.setSelectedRelations(sel);
     744        displaylist.repaint();
     745    }
     746
     747    public void dataChanged(DataChangedEvent event) {
    789748        Layer l = Main.main.getEditLayer();
    790749        if (l != null) {
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java

    r2578 r2622  
    2828import org.openstreetmap.josm.data.SelectionChangedListener;
    2929import org.openstreetmap.josm.data.coor.EastNorth;
    30 import org.openstreetmap.josm.data.osm.DataSetListener;
    3130import org.openstreetmap.josm.data.osm.Node;
    3231import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    3433import org.openstreetmap.josm.data.osm.RelationMember;
    3534import org.openstreetmap.josm.data.osm.Way;
     35import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
     36import org.openstreetmap.josm.data.osm.event.DataSetListener;
     37import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
     38import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
     39import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
     40import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
     41import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
     42import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
    3643import org.openstreetmap.josm.gui.dialogs.relation.WayConnectionType.Direction;
    3744import org.openstreetmap.josm.gui.layer.DataChangeListener;
     
    8895    /* Interface DataSetListener                                                   */
    8996    /* --------------------------------------------------------------------------- */
    90     public void dataChanged() {
     97    public void dataChanged(DataChangedEvent event) {
    9198        // just trigger a repaint - the display name of the relation members may
    9299        // have changed
     
    96103    }
    97104
    98     public void nodeMoved(Node node) {/* ignore */}
    99     public void primtivesAdded(Collection<? extends OsmPrimitive> added) {/* ignore */}
    100 
    101     public void primtivesRemoved(Collection<? extends OsmPrimitive> removed) {
     105    public void nodeMoved(NodeMovedEvent event) {/* ignore */}
     106    public void primtivesAdded(PrimitivesAddedEvent event) {/* ignore */}
     107
     108    public void primtivesRemoved(PrimitivesRemovedEvent event) {
    102109        // ignore - the relation in the editor might become out of sync with the relation
    103110        // in the dataset. We will deal with it when the relation editor is closed or
     
    105112    }
    106113
    107     public void relationMembersChanged(Relation r) {
     114    public void relationMembersChanged(RelationMembersChangedEvent event) {
    108115        // ignore - the relation in the editor might become out of sync with the relation
    109116        // in the dataset. We will deal with it when the relation editor is closed or
     
    111118    }
    112119
    113     public void tagsChanged(OsmPrimitive prim) {
     120    public void tagsChanged(TagsChangedEvent event) {
    114121        // just refresh the respective table cells
    115122        //
    116123        Collection<RelationMember> sel = getSelectedMembers();
    117124        for (int i=0; i < members.size();i++) {
    118             if (members.get(i).getMember() == prim) {
     125            if (members.get(i).getMember() == event.getPrimitive()) {
    119126                fireTableCellUpdated(i, 1 /* the column with the primitive name */);
    120127            }
     
    123130    }
    124131
    125     public void wayNodesChanged(Way way) {/* ignore */}
     132    public void wayNodesChanged(WayNodesChangedEvent event) {/* ignore */}
    126133    /* --------------------------------------------------------------------------- */
    127134
  • trunk/src/org/openstreetmap/josm/gui/history/HistoryBrowserModel.java

    r2621 r2622  
    55
    66import java.util.ArrayList;
    7 import java.util.Collection;
    87import java.util.Collections;
    98import java.util.HashSet;
    109import java.util.Observable;
    11 import java.util.logging.Logger;
    1210
    1311import javax.swing.table.DefaultTableModel;
    1412
    1513import org.openstreetmap.josm.Main;
    16 import org.openstreetmap.josm.data.osm.DataSetListener;
    1714import org.openstreetmap.josm.data.osm.Node;
    1815import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    2320import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
    2421import org.openstreetmap.josm.data.osm.Way;
     22import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
     23import org.openstreetmap.josm.data.osm.event.DataSetListener;
     24import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
     25import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
     26import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
     27import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
     28import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
     29import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
    2530import org.openstreetmap.josm.data.osm.history.History;
    2631import org.openstreetmap.josm.data.osm.history.HistoryNode;
     
    3035import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
    3136import org.openstreetmap.josm.gui.MapView;
     37import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    3238import org.openstreetmap.josm.gui.layer.DataChangeListener;
    3339import org.openstreetmap.josm.gui.layer.Layer;
     
    5965 * @see HistoryBrowser
    6066 */
    61 public class HistoryBrowserModel extends Observable implements MapView.LayerChangeListener, DataSetListener, DataChangeListener {
    62 
    63     private static Logger logger = Logger.getLogger(HistoryBrowserModel.class.getName());
     67public class HistoryBrowserModel extends Observable implements LayerChangeListener, DataSetListener, DataChangeListener {
     68    //private static Logger logger = Logger.getLogger(HistoryBrowserModel.class.getName());
    6469
    6570    /** the history of an OsmPrimitive */
     
    777782    /* DataSetListener                                                        */
    778783    /* ---------------------------------------------------------------------- */
    779     public void nodeMoved(Node node) {
     784    public void nodeMoved(NodeMovedEvent event) {
     785        Node node = event.getNode();
    780786        if (!node.isNew() && node.getId() == history.getId()) {
    781787            setLatest(new HistoryPrimitiveBuilder().build(node));
     
    783789    }
    784790
    785     public void primtivesAdded(Collection<? extends OsmPrimitive> added) {
    786         if (added == null || added.isEmpty()) return;
    787         for (OsmPrimitive p: added) {
     791    public void primtivesAdded(PrimitivesAddedEvent event) {
     792        for (OsmPrimitive p: event.getPrimitives()) {
    788793            if (canShowAsLatest(p)) {
    789794                setLatest(new HistoryPrimitiveBuilder().build(p));
     
    792797    }
    793798
    794     public void primtivesRemoved(Collection<? extends OsmPrimitive> removed) {
    795         if (removed == null || removed.isEmpty()) return;
    796         for (OsmPrimitive p: removed) {
     799    public void primtivesRemoved(PrimitivesRemovedEvent event) {
     800        for (OsmPrimitive p: event.getPrimitives()) {
    797801            if (!p.isNew() && p.getId() == history.getId()) {
    798802                setLatest(null);
     
    801805    }
    802806
    803     public void relationMembersChanged(Relation r) {
     807    public void relationMembersChanged(RelationMembersChangedEvent event) {
     808        Relation r = event.getRelation();
    804809        if (!r.isNew() && r.getId() == history.getId()) {
    805810            setLatest(new HistoryPrimitiveBuilder().build(r));
     
    807812    }
    808813
    809     public void tagsChanged(OsmPrimitive prim) {
     814    public void tagsChanged(TagsChangedEvent event) {
     815        OsmPrimitive prim = event.getPrimitive();
    810816        if (!prim.isNew() && prim.getId() == history.getId()) {
    811817            setLatest(new HistoryPrimitiveBuilder().build(prim));
     
    813819    }
    814820
    815     public void wayNodesChanged(Way way) {
     821    public void wayNodesChanged(WayNodesChangedEvent event) {
     822        Way way = event.getChangedWay();
    816823        if (!way.isNew() && way.getId() == history.getId()) {
    817824            setLatest(new HistoryPrimitiveBuilder().build(way));
     
    819826    }
    820827
    821     public void dataChanged() {
     828    public void dataChanged(DataChangedEvent event) {
    822829        dataChanged(getEditLayer());
    823830    }
    824831
    825832    /* ---------------------------------------------------------------------- */
    826     /* DataChangeListener                                                    */
     833    /* DataChangeListener                                                     */
    827834    /* ---------------------------------------------------------------------- */
    828835    public void dataChanged(OsmDataLayer l) {
Note: See TracChangeset for help on using the changeset viewer.