Changeset 12395 in josm
- Timestamp:
- 2017-06-11T15:27:18+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/layer/LayerVisibilityAction.java
r12393 r12395 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.BorderLayout; 7 import java.awt.Color; 6 8 import java.awt.Component; 9 import java.awt.Dimension; 7 10 import java.awt.GridBagLayout; 8 11 import java.awt.event.ActionEvent; 12 import java.awt.event.MouseAdapter; 13 import java.awt.event.MouseEvent; 9 14 import java.awt.event.MouseWheelEvent; 10 15 import java.util.ArrayList; 11 16 import java.util.Collection; 17 import java.util.HashMap; 12 18 import java.util.List; 19 import java.util.stream.Collectors; 13 20 14 21 import javax.swing.AbstractAction; … … 22 29 import javax.swing.JPopupMenu; 23 30 import javax.swing.JSlider; 31 import javax.swing.border.Border; 24 32 25 33 import org.openstreetmap.josm.Main; … … 27 35 import org.openstreetmap.josm.gui.dialogs.IEnabledStateUpdating; 28 36 import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel; 37 import org.openstreetmap.josm.gui.layer.GpxLayer; 29 38 import org.openstreetmap.josm.gui.layer.ImageryLayer; 30 39 import org.openstreetmap.josm.gui.layer.Layer; … … 82 91 addContentEntry(new GammaFilterSlider()); 83 92 addContentEntry(new SharpnessSlider()); 93 addContentEntry(new ColorSelector()); 84 94 } 85 95 … … 505 515 } 506 516 } 517 518 /** 519 * Allows to select the color for the GPX layer 520 * @author Michael Zangl 521 */ 522 private class ColorSelector extends JPanel implements LayerVisibilityMenuEntry { 523 524 private final Border NORMAL_BORDER = BorderFactory.createEmptyBorder(2, 2, 2, 2); 525 private final Border SELECTED_BORDER = BorderFactory.createLineBorder(Color.BLACK, 2); 526 527 // TODO: Nicer color palette 528 private final Color[] COLORS = new Color[] { 529 Color.RED, 530 Color.ORANGE, 531 Color.YELLOW, 532 Color.GREEN, 533 Color.BLUE, 534 Color.CYAN, 535 Color.GRAY, 536 }; 537 private final HashMap<Color, JPanel> panels = new HashMap<>(); 538 539 public ColorSelector() { 540 super(new GridBagLayout()); 541 for (Color color : COLORS) { 542 addPanelForColor(color); 543 } 544 } 545 546 private void addPanelForColor(Color color) { 547 JPanel innerPanel = new JPanel(); 548 innerPanel.setBackground(color); 549 550 JPanel colorPanel = new JPanel(new BorderLayout()); 551 colorPanel.setBorder(NORMAL_BORDER); 552 colorPanel.add(innerPanel); 553 colorPanel.setMinimumSize(new Dimension(20, 20)); 554 colorPanel.addMouseListener(new MouseAdapter() { 555 @Override 556 public void mouseClicked(MouseEvent e) { 557 List<Layer> layers = model.getSelectedLayers(); 558 for(Layer l : layers) { 559 if (l instanceof GpxLayer) { 560 l.getColorProperty().put(color); 561 } 562 } 563 highlightColor(color); 564 } 565 }); 566 add(colorPanel, GBC.std().weight(1, 1).fill().insets(5)); 567 panels.put(color, colorPanel); 568 } 569 570 @Override 571 public void updateLayers(List<Layer> layers, boolean allVisible, boolean allHidden) { 572 List<Color> colors = layers.stream().filter(l -> l instanceof GpxLayer) 573 .map(l -> ((GpxLayer)l).getColorProperty().get()) 574 .distinct() 575 .collect(Collectors.toList()); 576 if (colors.size() == 1) { 577 setVisible(true); 578 highlightColor(colors.get(0)); 579 } else if (colors.size() > 0) { 580 setVisible(true); 581 highlightColor(null); 582 } else { 583 // no GPX layer 584 setVisible(false); 585 } 586 } 587 588 private void highlightColor(Color color) { 589 panels.values().forEach(panel -> panel.setBorder(NORMAL_BORDER)); 590 if (color != null) { 591 JPanel selected = panels.get(color); 592 if (selected != null) { 593 selected.setBorder(SELECTED_BORDER); 594 } 595 } 596 repaint(); 597 } 598 599 @Override 600 public JComponent getPanel() { 601 return this; 602 } 603 } 507 604 }
Note:
See TracChangeset
for help on using the changeset viewer.