| 1 | // License: GPL. See LICENSE file for details. |
|---|
| 2 | package org.openstreetmap.josm.gui.layer; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 6 | |
|---|
| 7 | import java.awt.Color; |
|---|
| 8 | import java.awt.Component; |
|---|
| 9 | import java.awt.event.ActionEvent; |
|---|
| 10 | |
|---|
| 11 | import javax.swing.AbstractAction; |
|---|
| 12 | import javax.swing.Action; |
|---|
| 13 | import javax.swing.JColorChooser; |
|---|
| 14 | import javax.swing.JMenuItem; |
|---|
| 15 | import javax.swing.JOptionPane; |
|---|
| 16 | import java.util.LinkedList; |
|---|
| 17 | import java.util.List; |
|---|
| 18 | |
|---|
| 19 | import org.openstreetmap.josm.Main; |
|---|
| 20 | import org.openstreetmap.josm.gui.layer.Layer; |
|---|
| 21 | import org.openstreetmap.josm.gui.layer.Layer.LayerAction; |
|---|
| 22 | import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction; |
|---|
| 23 | import org.openstreetmap.josm.tools.ImageProvider; |
|---|
| 24 | |
|---|
| 25 | public class CustomizeColor extends AbstractAction implements LayerAction, MultiLayerAction { |
|---|
| 26 | List<Layer> layers; |
|---|
| 27 | |
|---|
| 28 | public CustomizeColor(List<Layer> l) { |
|---|
| 29 | this(); |
|---|
| 30 | layers = l; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | public CustomizeColor(Layer l) { |
|---|
| 34 | this(); |
|---|
| 35 | layers = new LinkedList<Layer>(); |
|---|
| 36 | layers.add(l); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | private CustomizeColor() { |
|---|
| 40 | super(tr("Customize Color"), ImageProvider.get("colorchooser")); |
|---|
| 41 | putValue("help", ht("/Action/LayerCustomizeColor")); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | @Override |
|---|
| 45 | public boolean supportLayers(List<Layer> layers) { |
|---|
| 46 | for(Layer layer: layers) { |
|---|
| 47 | if(layer.getColor(false) == null) |
|---|
| 48 | return false; |
|---|
| 49 | } |
|---|
| 50 | return true; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | @Override |
|---|
| 54 | public Component createMenuComponent() { |
|---|
| 55 | return new JMenuItem(this); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | @Override |
|---|
| 59 | public Action getMultiLayerAction(List<Layer> layers) { |
|---|
| 60 | return new CustomizeColor(layers); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | @Override |
|---|
| 64 | public void actionPerformed(ActionEvent e) { |
|---|
| 65 | Color cl=layers.get(0).getColor(false); if (cl==null) cl=Color.gray; |
|---|
| 66 | JColorChooser c = new JColorChooser(cl); |
|---|
| 67 | Object[] options = new Object[]{tr("OK"), tr("Cancel"), tr("Default")}; |
|---|
| 68 | int answer = JOptionPane.showOptionDialog( |
|---|
| 69 | Main.parent, |
|---|
| 70 | c, |
|---|
| 71 | tr("Choose a color"), |
|---|
| 72 | JOptionPane.OK_CANCEL_OPTION, |
|---|
| 73 | JOptionPane.PLAIN_MESSAGE, |
|---|
| 74 | null, |
|---|
| 75 | options, |
|---|
| 76 | options[0] |
|---|
| 77 | ); |
|---|
| 78 | switch (answer) { |
|---|
| 79 | case 0: |
|---|
| 80 | for(Layer layer : layers) |
|---|
| 81 | Main.pref.putColor("layer "+layer.getName(), c.getColor()); |
|---|
| 82 | break; |
|---|
| 83 | case 1: |
|---|
| 84 | return; |
|---|
| 85 | case 2: |
|---|
| 86 | for(Layer layer : layers) |
|---|
| 87 | Main.pref.putColor("layer "+layer.getName(), null); |
|---|
| 88 | break; |
|---|
| 89 | } |
|---|
| 90 | Main.map.repaint(); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|