Ignore:
Timestamp:
2015-09-04T10:32:05+02:00 (9 years ago)
Author:
simon04
Message:

fix #10417 - Selectable Gamma value for background imagery

File:
1 edited

Legend:

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

    r8728 r8729  
    5656import org.openstreetmap.josm.gui.SideButton;
    5757import org.openstreetmap.josm.gui.help.HelpUtil;
     58import org.openstreetmap.josm.gui.layer.ImageryLayer;
    5859import org.openstreetmap.josm.gui.layer.JumpToMarkerActions;
    5960import org.openstreetmap.josm.gui.layer.Layer;
     
    7172import org.openstreetmap.josm.tools.MultikeyShortcutAction.MultikeyInfo;
    7273import org.openstreetmap.josm.tools.Shortcut;
     74import org.openstreetmap.josm.tools.Utils;
    7375
    7476/**
     
    113115
    114116    private SideButton opacityButton;
     117    private SideButton gammaButton;
    115118
    116119    private ActivateLayerAction activateLayerAction;
     
    261264        opacityButton = new SideButton(layerOpacityAction, false);
    262265
     266        // -- layer gamma action
     267        LayerGammaAction layerGammaAction = new LayerGammaAction();
     268        adaptTo(layerGammaAction, selectionModel);
     269        gammaButton = new SideButton(layerGammaAction, false);
     270
    263271        // -- delete layer action
    264272        DeleteLayerAction deleteLayerAction = new DeleteLayerAction();
     
    288296                new SideButton(showHideLayerAction, false),
    289297                opacityButton,
     298                gammaButton,
    290299                new SideButton(deleteLayerAction, false)
    291300        ));
     
    513522
    514523    /**
     524     * Abstract action which allows to adjust a double value using a slider
     525     */
     526    public static abstract class AbstractLayerPropertySliderAction extends AbstractAction implements IEnabledStateUpdating, LayerAction {
     527        protected final JPopupMenu popup;
     528        protected final JSlider slider;
     529        private final double factor;
     530
     531        public AbstractLayerPropertySliderAction(String name, final double factor) {
     532            super(name);
     533            this.factor = factor;
     534            updateEnabledState();
     535
     536            popup = new JPopupMenu();
     537            slider = new JSlider(JSlider.VERTICAL);
     538            slider.addChangeListener(new ChangeListener() {
     539                @Override
     540                public void stateChanged(ChangeEvent e) {
     541                    setValue((double) slider.getValue() / factor);
     542                }
     543            });
     544            popup.add(slider);
     545
     546        }
     547
     548        protected abstract void setValue(double value);
     549
     550        protected abstract double getValue();
     551
     552        protected abstract SideButton getCorrespondingSideButton();
     553
     554        @Override
     555        public void actionPerformed(ActionEvent e) {
     556            final SideButton sideButton = getCorrespondingSideButton();
     557            slider.setValue((int) (getValue() * factor));
     558            if (e.getSource() == sideButton) {
     559                popup.show(sideButton, 0, sideButton.getHeight());
     560            } else {
     561                // Action can be trigger either by opacity button or by popup menu (in case toggle buttons are hidden).
     562                // In that case, show it in the middle of screen (because opacityButton is not visible)
     563                popup.show(Main.parent, Main.parent.getWidth() / 2, (Main.parent.getHeight() - popup.getHeight()) / 2);
     564            }
     565        }
     566
     567        @Override
     568        public Component createMenuComponent() {
     569            return new JMenuItem(this);
     570        }
     571
     572    }
     573
     574    /**
    515575     * Action which allows to change the opacity of one or more layers.
    516576     */
    517     public final class LayerOpacityAction extends AbstractAction implements IEnabledStateUpdating, LayerAction {
     577    public final class LayerOpacityAction extends AbstractLayerPropertySliderAction {
    518578        private transient Layer layer;
    519         private JPopupMenu popup;
    520         private JSlider slider = new JSlider(JSlider.VERTICAL);
    521579
    522580        /**
     
    529587        public LayerOpacityAction(Layer layer) {
    530588            this();
    531             putValue(NAME, tr("Opacity"));
    532589            CheckParameterUtil.ensureParameterNotNull(layer, "layer");
    533590            this.layer = layer;
     
    541598         */
    542599        public LayerOpacityAction() {
    543             putValue(NAME, tr("Opacity"));
     600            super(tr("Opacity"), 100);
    544601            putValue(SHORT_DESCRIPTION, tr("Adjust opacity of the layer."));
    545602            putValue(SMALL_ICON, ImageProvider.get("dialogs/layerlist", "transparency"));
    546             updateEnabledState();
    547 
    548             popup = new JPopupMenu();
    549             slider.addChangeListener(new ChangeListener() {
    550                 @Override
    551                 public void stateChanged(ChangeEvent e) {
    552                     setOpacity((double) slider.getValue()/100);
    553                 }
    554             });
    555             popup.add(slider);
    556         }
    557 
    558         private void setOpacity(double value) {
     603        }
     604
     605        @Override
     606        protected void setValue(double value) {
    559607            if (!isEnabled()) return;
    560608            if (layer != null) {
     
    567615        }
    568616
    569         private double getOpacity() {
     617        @Override
     618        protected double getValue() {
    570619            if (layer != null)
    571620                return layer.getOpacity();
     
    581630
    582631        @Override
    583         public void actionPerformed(ActionEvent e) {
    584             slider.setValue((int) Math.round(getOpacity()*100));
    585             if (e.getSource() == opacityButton) {
    586                 popup.show(opacityButton, 0, opacityButton.getHeight());
    587             } else {
    588                 // Action can be trigger either by opacity button or by popup menu (in case toggle buttons are hidden).
    589                 // In that case, show it in the middle of screen (because opacityButton is not visible)
    590                 popup.show(Main.parent, Main.parent.getWidth() / 2, (Main.parent.getHeight() - popup.getHeight()) / 2);
    591             }
     632        protected SideButton getCorrespondingSideButton() {
     633            return opacityButton;
    592634        }
    593635
     
    602644
    603645        @Override
    604         public Component createMenuComponent() {
    605             return new JMenuItem(this);
    606         }
    607 
    608         @Override
    609646        public boolean supportLayers(List<Layer> layers) {
    610647            return true;
    611648        }
    612 
    613         @Override
    614         public boolean equals(Object obj) {
    615             return obj instanceof LayerOpacityAction;
    616         }
    617 
    618         @Override
    619         public int hashCode() {
    620             return getClass().hashCode();
     649    }
     650
     651    /**
     652     * Action which allows to change the gamma of one imagery layer.
     653     */
     654    public final class LayerGammaAction extends AbstractLayerPropertySliderAction {
     655
     656        public LayerGammaAction() {
     657            super(tr("Gamma"), 50);
     658            putValue(SHORT_DESCRIPTION, tr("Adjust gamma value of the layer."));
     659            putValue(SMALL_ICON, ImageProvider.get("dialogs/layerlist", "gamma"));
     660        }
     661
     662        @Override
     663        protected void setValue(double value) {
     664            for (ImageryLayer imageryLayer : Utils.filteredCollection(model.getSelectedLayers(), ImageryLayer.class)) {
     665                imageryLayer.setGamma(value);
     666            }
     667        }
     668
     669        @Override
     670        protected double getValue() {
     671            return Utils.filteredCollection(model.getSelectedLayers(), ImageryLayer.class).iterator().next().getGamma();
     672        }
     673
     674        @Override
     675        protected SideButton getCorrespondingSideButton() {
     676            return gammaButton;
     677        }
     678
     679        @Override
     680        public void updateEnabledState() {
     681            setEnabled(!Utils.filteredCollection(model.getSelectedLayers(), ImageryLayer.class).isEmpty());
     682        }
     683
     684        @Override
     685        public boolean supportLayers(List<Layer> layers) {
     686            return !Utils.filteredCollection(layers, ImageryLayer.class).isEmpty();
    621687        }
    622688    }
Note: See TracChangeset for help on using the changeset viewer.