Changeset 9926 in josm for trunk


Ignore:
Timestamp:
2016-03-05T14:37:53+01:00 (9 years ago)
Author:
Don-vip
Message:

LayerListDialog: add first unit tests

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java

    r9840 r9926  
    115115    private final LayerList layerList;
    116116
    117     private final SideButton opacityButton;
    118     private final SideButton gammaButton;
    119 
    120117    private final ActivateLayerAction activateLayerAction;
    121118    private final ShowHideLayerAction showHideLayerAction;
     
    268265
    269266        // -- layer opacity action
    270         LayerOpacityAction layerOpacityAction = new LayerOpacityAction();
     267        LayerOpacityAction layerOpacityAction = new LayerOpacityAction(model);
    271268        adaptTo(layerOpacityAction, selectionModel);
    272         opacityButton = new SideButton(layerOpacityAction, false);
     269        SideButton opacityButton = new SideButton(layerOpacityAction, false);
     270        layerOpacityAction.setCorrespondingSideButton(opacityButton);
    273271
    274272        // -- layer gamma action
    275         LayerGammaAction layerGammaAction = new LayerGammaAction();
     273        LayerGammaAction layerGammaAction = new LayerGammaAction(model);
    276274        adaptTo(layerGammaAction, selectionModel);
    277         gammaButton = new SideButton(layerGammaAction, false);
     275        SideButton gammaButton = new SideButton(layerGammaAction, false);
     276        layerGammaAction.setCorrespondingSideButton(gammaButton);
    278277
    279278        // -- delete layer action
     
    533532     */
    534533    public abstract static class AbstractLayerPropertySliderAction extends AbstractAction implements IEnabledStateUpdating, LayerAction {
     534        protected final LayerListModel model;
    535535        protected final JPopupMenu popup;
    536536        protected final JSlider slider;
    537537        private final double factor;
    538 
    539         public AbstractLayerPropertySliderAction(String name, final double factor) {
     538        private SideButton sideButton;
     539
     540        protected AbstractLayerPropertySliderAction(LayerListModel model, String name, final double factor) {
    540541            super(name);
     542            this.model = model;
    541543            this.factor = factor;
    542544            updateEnabledState();
     
    551553            });
    552554            popup.add(slider);
    553 
    554555        }
    555556
     
    558559        protected abstract double getValue();
    559560
    560         protected abstract SideButton getCorrespondingSideButton();
     561        /**
     562         * Sets the corresponding side button.
     563         * @param sideButton the corresponding side button
     564         */
     565        final void setCorrespondingSideButton(SideButton sideButton) {
     566            this.sideButton = sideButton;
     567        }
    561568
    562569        @Override
    563570        public void actionPerformed(ActionEvent e) {
    564             final SideButton sideButton = getCorrespondingSideButton();
    565571            slider.setValue((int) (getValue() * factor));
    566572            if (e.getSource() == sideButton) {
     
    577583            return new JMenuItem(this);
    578584        }
    579 
    580585    }
    581586
     
    583588     * Action which allows to change the opacity of one or more layers.
    584589     */
    585     public final class LayerOpacityAction extends AbstractLayerPropertySliderAction {
     590    public static final class LayerOpacityAction extends AbstractLayerPropertySliderAction {
    586591        private transient Layer layer;
    587592
    588593        /**
    589          * Creates a {@link LayerOpacityAction} which allows to change the
    590          * opacity of one or more layers.
    591          *
     594         * Creates a {@link LayerOpacityAction} which allows to change the opacity of one or more layers.
     595         *
     596         * @param model layer list model
    592597         * @param layer  the layer. Must not be null.
    593598         * @throws IllegalArgumentException if layer is null
    594599         */
    595         public LayerOpacityAction(Layer layer) {
    596             this();
     600        public LayerOpacityAction(LayerListModel model, Layer layer) {
     601            this(model);
    597602            CheckParameterUtil.ensureParameterNotNull(layer, "layer");
    598603            this.layer = layer;
     
    601606
    602607        /**
    603          * Creates a {@link ShowHideLayerAction} which will toggle the visibility of
    604          * the currently selected layers
    605          *
    606          */
    607         public LayerOpacityAction() {
    608             super(tr("Opacity"), 100);
     608         * Creates a {@link ShowHideLayerAction} which will toggle the visibility of the currently selected layers
     609         * @param model layer list model
     610         */
     611        public LayerOpacityAction(LayerListModel model) {
     612            super(model, tr("Opacity"), 100);
    609613            putValue(SHORT_DESCRIPTION, tr("Adjust opacity of the layer."));
    610614            putValue(SMALL_ICON, ImageProvider.get("dialogs/layerlist", "transparency"));
     
    617621                layer.setOpacity(value);
    618622            } else {
    619                 for (Layer layer: model.getSelectedLayers()) {
    620                     layer.setOpacity(value);
     623                for (Layer l : model.getSelectedLayers()) {
     624                    l.setOpacity(value);
    621625                }
    622626            }
     
    630634                double opacity = 0;
    631635                List<Layer> layers = model.getSelectedLayers();
    632                 for (Layer layer: layers) {
    633                     opacity += layer.getOpacity();
     636                for (Layer l : layers) {
     637                    opacity += l.getOpacity();
    634638                }
    635639                return opacity / layers.size();
    636640            }
    637         }
    638 
    639         @Override
    640         protected SideButton getCorrespondingSideButton() {
    641             return opacityButton;
    642641        }
    643642
     
    645644        public void updateEnabledState() {
    646645            if (layer == null) {
    647                 setEnabled(!getModel().getSelectedLayers().isEmpty());
     646                setEnabled(!model.getSelectedLayers().isEmpty());
    648647            } else {
    649648                setEnabled(true);
     
    660659     * Action which allows to change the gamma of one imagery layer.
    661660     */
    662     public final class LayerGammaAction extends AbstractLayerPropertySliderAction {
    663 
    664         public LayerGammaAction() {
    665             super(tr("Gamma"), 50);
     661    public static final class LayerGammaAction extends AbstractLayerPropertySliderAction {
     662
     663        /**
     664         * Constructs a new {@code LayerGammaAction}.
     665         * @param model layer list model
     666         */
     667        public LayerGammaAction(LayerListModel model) {
     668            super(model, tr("Gamma"), 50);
    666669            putValue(SHORT_DESCRIPTION, tr("Adjust gamma value of the layer."));
    667670            putValue(SMALL_ICON, ImageProvider.get("dialogs/layerlist", "gamma"));
     
    678681        protected double getValue() {
    679682            return Utils.filteredCollection(model.getSelectedLayers(), ImageryLayer.class).iterator().next().getGamma();
    680         }
    681 
    682         @Override
    683         protected SideButton getCorrespondingSideButton() {
    684             return gammaButton;
    685683        }
    686684
     
    11121110            Layer layer = (Layer) value;
    11131111            if (layer instanceof NativeScaleLayer) {
    1114                 boolean active = layer != null && layer == Main.map.mapView.getNativeScaleLayer();
     1112                boolean active = ((NativeScaleLayer) layer) == Main.map.mapView.getNativeScaleLayer();
    11151113                cb.setSelected(active);
    11161114                cb.setToolTipText(active
     
    12711269     * the properties {@link Layer#VISIBLE_PROP} and {@link Layer#NAME_PROP}.
    12721270     */
    1273     public final class LayerListModel extends AbstractTableModel implements MapView.LayerChangeListener, PropertyChangeListener {
     1271    public static final class LayerListModel extends AbstractTableModel implements MapView.LayerChangeListener, PropertyChangeListener {
    12741272        /** manages list selection state*/
    12751273        private final DefaultListSelectionModel selectionModel;
    12761274        private final CopyOnWriteArrayList<LayerListModelListener> listeners;
     1275        private LayerList layerList;
    12771276
    12781277        /**
     
    12811280         * @param selectionModel the list selection model
    12821281         */
    1283         private LayerListModel(DefaultListSelectionModel selectionModel) {
     1282        LayerListModel(DefaultListSelectionModel selectionModel) {
    12841283            this.selectionModel = selectionModel;
    12851284            listeners = new CopyOnWriteArrayList<>();
     1285        }
     1286
     1287        void setlayerList(LayerList layerList) {
     1288            this.layerList = layerList;
    12861289        }
    12871290
     
    17321735
    17331736    static class LayerList extends JTable {
    1734         LayerList(TableModel dataModel) {
     1737        LayerList(LayerListModel dataModel) {
    17351738            super(dataModel);
     1739            dataModel.setlayerList(this);
    17361740        }
    17371741
Note: See TracChangeset for help on using the changeset viewer.