Ignore:
Timestamp:
2013-08-14T03:24:08+02:00 (11 years ago)
Author:
Don-vip
Message:

sonar - move duplicated class between history and conflict to util

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/conflict/pair/ListMerger.java

    r6146 r6147  
    55import static org.openstreetmap.josm.tools.I18n.trn;
    66
    7 import java.awt.Adjustable;
    87import java.awt.FlowLayout;
    98import java.awt.GridBagConstraints;
     
    1110import java.awt.Insets;
    1211import java.awt.event.ActionEvent;
    13 import java.awt.event.AdjustmentEvent;
    14 import java.awt.event.AdjustmentListener;
    1512import java.awt.event.ItemEvent;
    1613import java.awt.event.ItemListener;
    1714import java.beans.PropertyChangeEvent;
    1815import java.beans.PropertyChangeListener;
    19 import java.util.ArrayList;
    2016import java.util.Collection;
    21 import java.util.HashMap;
    2217import java.util.List;
    2318import java.util.Observable;
     
    4338import org.openstreetmap.josm.data.osm.Way;
    4439import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     40import org.openstreetmap.josm.gui.util.AdjustmentSynchronizer;
    4541import org.openstreetmap.josm.gui.widgets.JosmComboBox;
    4642import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTable;
    47 import org.openstreetmap.josm.tools.CheckParameterUtil;
    4843import org.openstreetmap.josm.tools.ImageProvider;
    4944
     
    894889        mergedEntriesTable.unlinkAsListener();
    895890        theirEntriesTable.unlinkAsListener();
    896     }
    897 
    898     /**
    899      * Synchronizes scrollbar adjustments between a set of
    900      * {@link Adjustable}s. Whenever the adjustment of one of
    901      * the registerd Adjustables is updated the adjustment of
    902      * the other registered Adjustables is adjusted too.
    903      *
    904      */
    905     class AdjustmentSynchronizer implements AdjustmentListener {
    906 
    907         private final  ArrayList<Adjustable> synchronizedAdjustables;
    908         private final  HashMap<Adjustable, Boolean> enabledMap;
    909 
    910         private final Observable observable;
    911 
    912         public AdjustmentSynchronizer() {
    913             synchronizedAdjustables = new ArrayList<Adjustable>();
    914             enabledMap = new HashMap<Adjustable, Boolean>();
    915             observable = new Observable();
    916         }
    917 
    918         /**
    919          * registers an {@link Adjustable} for participation in synchronized
    920          * scrolling.
    921          *
    922          * @param adjustable the adjustable
    923          */
    924         public void participateInSynchronizedScrolling(Adjustable adjustable) {
    925             if (adjustable == null)
    926                 return;
    927             if (synchronizedAdjustables.contains(adjustable))
    928                 return;
    929             synchronizedAdjustables.add(adjustable);
    930             setParticipatingInSynchronizedScrolling(adjustable, true);
    931             adjustable.addAdjustmentListener(this);
    932         }
    933 
    934         /**
    935          * event handler for {@link AdjustmentEvent}s
    936          *
    937          */
    938         @Override
    939         public void adjustmentValueChanged(AdjustmentEvent e) {
    940             if (! enabledMap.get(e.getAdjustable()))
    941                 return;
    942             for (Adjustable a : synchronizedAdjustables) {
    943                 if (a != e.getAdjustable() && isParticipatingInSynchronizedScrolling(a)) {
    944                     a.setValue(e.getValue());
    945                 }
    946             }
    947         }
    948 
    949         /**
    950          * sets whether adjustable participates in adjustment synchronization
    951          * or not
    952          *
    953          * @param adjustable the adjustable
    954          */
    955         protected void setParticipatingInSynchronizedScrolling(Adjustable adjustable, boolean isParticipating) {
    956             CheckParameterUtil.ensureParameterNotNull(adjustable, "adjustable");
    957             if (! synchronizedAdjustables.contains(adjustable))
    958                 throw new IllegalStateException(tr("Adjustable {0} not registered yet. Cannot set participation in synchronized adjustment.", adjustable));
    959 
    960             enabledMap.put(adjustable, isParticipating);
    961             observable.notifyObservers();
    962         }
    963 
    964         /**
    965          * returns true if an adjustable is participating in synchronized scrolling
    966          *
    967          * @param adjustable the adjustable
    968          * @return true, if the adjustable is participating in synchronized scrolling, false otherwise
    969          * @throws IllegalStateException thrown, if adjustable is not registered for synchronized scrolling
    970          */
    971         protected boolean isParticipatingInSynchronizedScrolling(Adjustable adjustable) throws IllegalStateException {
    972             if (! synchronizedAdjustables.contains(adjustable))
    973                 throw new IllegalStateException(tr("Adjustable {0} not registered yet.", adjustable));
    974 
    975             return enabledMap.get(adjustable);
    976         }
    977 
    978         /**
    979          * wires a {@link JCheckBox} to  the adjustment synchronizer, in such a way  that:
    980          * <li>
    981          *   <ol>state changes in the checkbox control whether the adjustable participates
    982          *      in synchronized adjustment</ol>
    983          *   <ol>state changes in this {@link AdjustmentSynchronizer} are reflected in the
    984          *      {@link JCheckBox}</ol>
    985          * </li>
    986          *
    987          *
    988          * @param view  the checkbox to control whether an adjustable participates in synchronized
    989          *      adjustment
    990          * @param adjustable the adjustable
    991          * @exception IllegalArgumentException thrown, if view is null
    992          * @exception IllegalArgumentException thrown, if adjustable is null
    993          */
    994         protected void adapt(final JCheckBox view, final Adjustable adjustable) throws IllegalStateException {
    995             CheckParameterUtil.ensureParameterNotNull(adjustable, "adjustable");
    996             CheckParameterUtil.ensureParameterNotNull(view, "view");
    997 
    998             if (! synchronizedAdjustables.contains(adjustable)) {
    999                 participateInSynchronizedScrolling(adjustable);
    1000             }
    1001 
    1002             // register an item lister with the check box
    1003             //
    1004             view.addItemListener(new ItemListener() {
    1005                 @Override
    1006                 public void itemStateChanged(ItemEvent e) {
    1007                     switch(e.getStateChange()) {
    1008                     case ItemEvent.SELECTED:
    1009                         if (!isParticipatingInSynchronizedScrolling(adjustable)) {
    1010                             setParticipatingInSynchronizedScrolling(adjustable, true);
    1011                         }
    1012                         break;
    1013                     case ItemEvent.DESELECTED:
    1014                         if (isParticipatingInSynchronizedScrolling(adjustable)) {
    1015                             setParticipatingInSynchronizedScrolling(adjustable, false);
    1016                         }
    1017                         break;
    1018                     }
    1019                 }
    1020             });
    1021 
    1022             observable.addObserver(
    1023                     new Observer() {
    1024                         @Override
    1025                         public void update(Observable o, Object arg) {
    1026                             boolean sync = isParticipatingInSynchronizedScrolling(adjustable);
    1027                             if (view.isSelected() != sync) {
    1028                                 view.setSelected(sync);
    1029                             }
    1030                         }
    1031                     }
    1032             );
    1033             setParticipatingInSynchronizedScrolling(adjustable, true);
    1034             view.setSelected(true);
    1035         }
    1036891    }
    1037892
Note: See TracChangeset for help on using the changeset viewer.