- Timestamp:
- 2017-06-11T14:39:43+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/layer/LayerVisibilityAction.java
r11913 r12392 16 16 import javax.swing.ImageIcon; 17 17 import javax.swing.JCheckBox; 18 import javax.swing.JComponent; 18 19 import javax.swing.JLabel; 19 20 import javax.swing.JMenuItem; … … 51 52 private final JPopupMenu popup; 52 53 private SideButton sideButton; 53 private final JCheckBox visibilityCheckbox; 54 /** 55 * The real content, just to add a border 56 */ 57 private final JPanel content = new JPanel(); 54 58 final OpacitySlider opacitySlider = new OpacitySlider(); 55 private final ArrayList< FilterSlider<?>> sliders = new ArrayList<>();59 private final ArrayList<LayerVisibilityMenuEntry> sliders = new ArrayList<>(); 56 60 57 61 /** … … 65 69 popup.addMouseWheelListener(MouseWheelEvent::consume); 66 70 67 // just to add a border68 JPanel content = new JPanel();69 71 popup.add(content); 70 72 content.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); … … 74 76 putValue(SHORT_DESCRIPTION, tr("Change visibility of the selected layer.")); 75 77 76 visibilityCheckbox = new JCheckBox(tr("Show layer")); 77 visibilityCheckbox.addChangeListener(e -> setVisibleFlag(visibilityCheckbox.isSelected())); 78 content.add(visibilityCheckbox, GBC.eop()); 79 80 addSlider(content, opacitySlider); 81 addSlider(content, new ColorfulnessSlider()); 82 addSlider(content, new GammaFilterSlider()); 83 addSlider(content, new SharpnessSlider()); 84 } 85 86 private void addSlider(JPanel content, FilterSlider<?> slider) { 87 // wrap to a common content pane to allow for mouse wheel listener on label. 88 JPanel container = new JPanel(new GridBagLayout()); 89 container.add(new JLabel(slider.getIcon()), GBC.std().span(1, 2).insets(0, 0, 5, 0)); 90 container.add(new JLabel(slider.getLabel()), GBC.eol()); 91 container.add(slider, GBC.eol()); 92 content.add(container, GBC.eop()); 93 94 container.addMouseWheelListener(slider::mouseWheelMoved); 78 addContentEntry(new VisibilityCheckbox()); 79 80 addContentEntry(opacitySlider); 81 addContentEntry(new ColorfulnessSlider()); 82 addContentEntry(new GammaFilterSlider()); 83 addContentEntry(new SharpnessSlider()); 84 } 85 86 private void addContentEntry(LayerVisibilityMenuEntry slider) { 87 content.add(slider.getPanel(), GBC.eop()); 95 88 sliders.add(slider); 96 89 } … … 118 111 List<Layer> layers = model.getSelectedLayers(); 119 112 120 visibilityCheckbox.setEnabled(!layers.isEmpty());121 113 boolean allVisible = true; 122 114 boolean allHidden = true; … … 125 117 allHidden &= !l.isVisible(); 126 118 } 127 // TODO: Indicate tristate. 128 visibilityCheckbox.setSelected(allVisible && !allHidden); 129 130 for (FilterSlider<?> slider : sliders) { 131 slider.updateSlider(layers, allHidden); 119 120 for (LayerVisibilityMenuEntry slider : sliders) { 121 slider.updateLayers(layers, allVisible, allHidden); 132 122 } 133 123 } … … 154 144 public void setCorrespondingSideButton(SideButton sideButton) { 155 145 this.sideButton = sideButton; 146 } 147 148 /** 149 * An entry in the visibility settings dropdown. 150 * @author Michael Zangl 151 */ 152 private interface LayerVisibilityMenuEntry { 153 154 /** 155 * Update the displayed value depending on the current layers 156 * @param layers The layers 157 * @param allVisible <code>true</code> if all layers are visible 158 * @param allHidden <code>true</code> if all layers are hidden 159 */ 160 void updateLayers(List<Layer> layers, boolean allVisible, boolean allHidden); 161 162 /** 163 * Get the panel that should be added to the menu 164 * @return The panel 165 */ 166 JComponent getPanel(); 167 } 168 169 private class VisibilityCheckbox extends JCheckBox implements LayerVisibilityMenuEntry { 170 171 VisibilityCheckbox() { 172 super(tr("Show layer")); 173 addChangeListener(e -> setVisibleFlag(isSelected())); 174 } 175 176 @Override 177 public void updateLayers(List<Layer> layers, boolean allVisible, boolean allHidden) { 178 setEnabled(!layers.isEmpty()); 179 // TODO: Indicate tristate. 180 setSelected(allVisible && !allHidden); 181 } 182 183 @Override 184 public JComponent getPanel() { 185 return this; 186 } 156 187 } 157 188 … … 162 193 * @param <T> The layer type. 163 194 */ 164 private abstract class FilterSlider<T extends Layer> extends JSlider{195 private abstract class AbstractFilterSlider<T extends Layer> extends JPanel implements LayerVisibilityMenuEntry { 165 196 private final double minValue; 166 197 private final double maxValue; 167 198 private final Class<T> layerClassFilter; 199 200 protected final JSlider slider = new JSlider(JSlider.HORIZONTAL); 168 201 169 202 /** … … 173 206 * @param layerClassFilter The type of layer influenced by this filter. 174 207 */ 175 FilterSlider(double minValue, double maxValue, Class<T> layerClassFilter) {176 super( JSlider.HORIZONTAL);208 AbstractFilterSlider(double minValue, double maxValue, Class<T> layerClassFilter) { 209 super(new GridBagLayout()); 177 210 this.minValue = minValue; 178 211 this.maxValue = maxValue; 179 212 this.layerClassFilter = layerClassFilter; 180 setMaximum(SLIDER_STEPS); 213 214 add(new JLabel(getIcon()), GBC.std().span(1, 2).insets(0, 0, 5, 0)); 215 add(new JLabel(getLabel()), GBC.eol()); 216 add(slider, GBC.eol()); 217 addMouseWheelListener(this::mouseWheelMoved); 218 219 slider.setMaximum(SLIDER_STEPS); 181 220 int tick = convertFromRealValue(1); 182 s etMinorTickSpacing(tick);183 s etMajorTickSpacing(tick);184 s etPaintTicks(true);185 186 addChangeListener(e -> onStateChanged());221 slider.setMinorTickSpacing(tick); 222 slider.setMajorTickSpacing(tick); 223 slider.setPaintTicks(true); 224 225 slider.addChangeListener(e -> onStateChanged()); 187 226 } 188 227 189 228 /** 190 229 * Called whenever the state of the slider was changed. 191 * @see #getValueIsAdjusting()230 * @see JSlider#getValueIsAdjusting() 192 231 * @see #getRealValue() 193 232 */ … … 206 245 } 207 246 double rotation = -1 * e.getPreciseWheelRotation(); 208 double destinationValue = getValue() + rotation * SLIDER_WHEEL_INCREMENT;247 double destinationValue = slider.getValue() + rotation * SLIDER_WHEEL_INCREMENT; 209 248 if (rotation < 0) { 210 249 destinationValue = Math.floor(destinationValue); … … 212 251 destinationValue = Math.ceil(destinationValue); 213 252 } 214 s etValue(Utils.clamp((int) destinationValue, getMinimum(),getMaximum()));253 slider.setValue(Utils.clamp((int) destinationValue, slider.getMinimum(), slider.getMaximum())); 215 254 } 216 255 … … 218 257 219 258 protected double getRealValue() { 220 return convertToRealValue( getValue());259 return convertToRealValue(slider.getValue()); 221 260 } 222 261 … … 227 266 228 267 protected void setRealValue(double value) { 229 s etValue(convertFromRealValue(value));268 slider.setValue(convertFromRealValue(value)); 230 269 } 231 270 232 271 protected int convertFromRealValue(double value) { 233 272 int i = (int) ((value - minValue) / (maxValue - minValue) * SLIDER_STEPS + .5); 234 return Utils.clamp(i, getMinimum(),getMaximum());273 return Utils.clamp(i, slider.getMinimum(), slider.getMaximum()); 235 274 } 236 275 … … 239 278 public abstract String getLabel(); 240 279 241 public void updateSlider(List<Layer> layers, boolean allHidden) { 280 @Override 281 public void updateLayers(List<Layer> layers, boolean allVisible, boolean allHidden) { 242 282 Collection<? extends Layer> usedLayers = filterLayers(layers); 243 if ( usedLayers.isEmpty() || allHidden) {244 s etEnabled(false);283 if (!usedLayers.stream().anyMatch(Layer::isVisible)) { 284 slider.setEnabled(false); 245 285 } else { 246 s etEnabled(true);286 slider.setEnabled(true); 247 287 updateSliderWhileEnabled(usedLayers, allHidden); 248 288 } … … 254 294 255 295 protected abstract void updateSliderWhileEnabled(Collection<? extends Layer> usedLayers, boolean allHidden); 296 297 @Override 298 public JComponent getPanel() { 299 return this; 300 } 256 301 } 257 302 … … 262 307 * @see Layer#setOpacity(double) 263 308 */ 264 class OpacitySlider extends FilterSlider<Layer> {309 class OpacitySlider extends AbstractFilterSlider<Layer> { 265 310 /** 266 311 * Creaate a new {@link OpacitySlider}. … … 268 313 OpacitySlider() { 269 314 super(0, 1, Layer.class); 270 s etToolTipText(tr("Adjust opacity of the layer."));315 slider.setToolTipText(tr("Adjust opacity of the layer.")); 271 316 } 272 317 273 318 @Override 274 319 protected void onStateChanged() { 275 if (getRealValue() <= 0.001 && ! getValueIsAdjusting()) {320 if (getRealValue() <= 0.001 && !slider.getValueIsAdjusting()) { 276 321 setVisibleFlag(false); 277 322 } else { … … 333 378 * @see ImageryFilterSettings#setGamma(double) 334 379 */ 335 private class GammaFilterSlider extends FilterSlider<ImageryLayer> {380 private class GammaFilterSlider extends AbstractFilterSlider<ImageryLayer> { 336 381 337 382 /** … … 340 385 GammaFilterSlider() { 341 386 super(-1, 1, ImageryLayer.class); 342 s etToolTipText(tr("Adjust gamma value of the layer."));387 slider.setToolTipText(tr("Adjust gamma value of the layer.")); 343 388 } 344 389 … … 392 437 * @see ImageryFilterSettings#setSharpenLevel(double) 393 438 */ 394 private class SharpnessSlider extends FilterSlider<ImageryLayer> {439 private class SharpnessSlider extends AbstractFilterSlider<ImageryLayer> { 395 440 396 441 /** … … 399 444 SharpnessSlider() { 400 445 super(0, MAX_SHARPNESS_FACTOR, ImageryLayer.class); 401 s etToolTipText(tr("Adjust sharpness/blur value of the layer."));446 slider.setToolTipText(tr("Adjust sharpness/blur value of the layer.")); 402 447 } 403 448 … … 429 474 * @see ImageryFilterSettings#setColorfulness(double) 430 475 */ 431 private class ColorfulnessSlider extends FilterSlider<ImageryLayer> {476 private class ColorfulnessSlider extends AbstractFilterSlider<ImageryLayer> { 432 477 433 478 /** … … 436 481 ColorfulnessSlider() { 437 482 super(0, MAX_COLORFUL_FACTOR, ImageryLayer.class); 438 s etToolTipText(tr("Adjust colorfulness of the layer."));483 slider.setToolTipText(tr("Adjust colorfulness of the layer.")); 439 484 } 440 485
Note:
See TracChangeset
for help on using the changeset viewer.