| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.drawing; |
| 16 | |
| 17 | import java.awt.Color; |
| 18 | import java.beans.PropertyChangeEvent; |
| 19 | import java.beans.PropertyChangeListener; |
| 20 | |
| 21 | import org.jdesktop.application.Resource; |
| 22 | import org.jdesktop.application.ResourceMap; |
| 23 | |
| 24 | import net.sourceforge.hivegui.component.AlphaColorChooser; |
| 25 | import net.sourceforge.hivegui.component.ColorIcon; |
| 26 | import net.sourceforge.hivegui.component.FixedSizeColorIcon; |
| 27 | |
| 28 | public class ColorPropertyTool extends AbstractPushButtonTool |
| 29 | { |
| 30 | static final public String TOOL_PROPERTY_COLOR = "Color"; |
| 31 | |
| 32 | static final private int COLOR_ICON_SIZE = 16; |
| 33 | static final private int COLOR_ICON_INSET = 2; |
| 34 | |
| 35 | public ColorPropertyTool(AlphaColorChooser chooser) |
| 36 | { |
| 37 | _chooser = chooser; |
| 38 | _icon = new FixedSizeColorIcon( COLOR_ICON_SIZE - 2 * COLOR_ICON_INSET, |
| 39 | COLOR_ICON_SIZE - 2 * COLOR_ICON_INSET, |
| 40 | COLOR_ICON_INSET, |
| 41 | COLOR_ICON_INSET); |
| 42 | _icon.setColor(_color); |
| 43 | } |
| 44 | |
| 45 | @Override protected void initProperty() |
| 46 | { |
| 47 | _properties.put(TOOL_PROPERTY_COLOR, _color); |
| 48 | } |
| 49 | |
| 50 | @Override protected void initResources(ResourceMap map) |
| 51 | { |
| 52 | super.initResources(map); |
| 53 | _button.setIcon(_icon); |
| 54 | _button.addPropertyChangeListener(new PropertyChangeListener() |
| 55 | { |
| 56 | public void propertyChange(PropertyChangeEvent e) |
| 57 | { |
| 58 | if ("enabled".equals(e.getPropertyName())) |
| 59 | { |
| 60 | boolean enabled = ((Boolean) e.getNewValue()).booleanValue(); |
| 61 | if (enabled) |
| 62 | { |
| 63 | _icon.setColor(_color); |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | _icon.setColor(Color.GRAY); |
| 68 | } |
| 69 | _button.repaint(); |
| 70 | } |
| 71 | } |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | @Override public void accept() |
| 76 | { |
| 77 | // Open Color chooser |
| 78 | _chooser.setTitle(_chooserTitle); |
| 79 | _chooser.setColor(_color); |
| 80 | _application.showDialog(_chooser); |
| 81 | _button.setSelected(false); |
| 82 | _color = _chooser.getColor(); |
| 83 | _icon.setColor(_color); |
| 84 | _button.repaint(); |
| 85 | initProperty(); |
| 86 | } |
| 87 | |
| 88 | private final AlphaColorChooser _chooser; |
| 89 | @Resource(key = "chooserTitle") |
| 90 | private String _chooserTitle = ""; |
| 91 | private Color _color = Color.BLACK; |
| 92 | private final ColorIcon _icon; |
| 93 | } |