001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.beans.editor;
018:
019: import com.l2fprod.common.swing.ComponentFactory;
020: import com.l2fprod.common.swing.PercentLayout;
021: import com.l2fprod.common.swing.renderer.ColorCellRenderer;
022: import com.l2fprod.common.util.ResourceManager;
023:
024: import java.awt.Color;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027:
028: import javax.swing.JButton;
029: import javax.swing.JColorChooser;
030: import javax.swing.JPanel;
031:
032: /**
033: * ColorPropertyEditor. <br>
034: *
035: */
036: public class ColorPropertyEditor extends AbstractPropertyEditor {
037:
038: private ColorCellRenderer label;
039: private JButton button;
040: private Color color;
041:
042: public ColorPropertyEditor() {
043: editor = new JPanel(new PercentLayout(PercentLayout.HORIZONTAL,
044: 0));
045: ((JPanel) editor).add("*", label = new ColorCellRenderer());
046: label.setOpaque(false);
047: ((JPanel) editor).add(button = ComponentFactory.Helper
048: .getFactory().createMiniButton());
049: button.addActionListener(new ActionListener() {
050: public void actionPerformed(ActionEvent e) {
051: selectColor();
052: }
053: });
054: ((JPanel) editor).add(button = ComponentFactory.Helper
055: .getFactory().createMiniButton());
056: button.setText("X");
057: button.addActionListener(new ActionListener() {
058: public void actionPerformed(ActionEvent e) {
059: selectNull();
060: }
061: });
062: ((JPanel) editor).setOpaque(false);
063: }
064:
065: public Object getValue() {
066: return color;
067: }
068:
069: public void setValue(Object value) {
070: color = (Color) value;
071: label.setValue(color);
072: }
073:
074: protected void selectColor() {
075: ResourceManager rm = ResourceManager
076: .all(FilePropertyEditor.class);
077: String title = rm.getString("ColorPropertyEditor.title");
078: Color selectedColor = JColorChooser.showDialog(editor, title,
079: color);
080:
081: if (selectedColor != null) {
082: Color oldColor = color;
083: Color newColor = selectedColor;
084: label.setValue(newColor);
085: color = newColor;
086: firePropertyChange(oldColor, newColor);
087: }
088: }
089:
090: protected void selectNull() {
091: Color oldColor = color;
092: label.setValue(null);
093: color = null;
094: firePropertyChange(oldColor, null);
095: }
096:
097: public static class AsInt extends ColorPropertyEditor {
098: public void setValue(Object arg0) {
099: if (arg0 instanceof Integer) {
100: super .setValue(new Color(((Integer) arg0).intValue()));
101: } else {
102: super .setValue(arg0);
103: }
104: }
105:
106: public Object getValue() {
107: Object value = super .getValue();
108: if (value == null) {
109: return null;
110: } else {
111: return new Integer(((Color) value).getRGB());
112: }
113: }
114:
115: protected void firePropertyChange(Object oldValue,
116: Object newValue) {
117: if (oldValue instanceof Color) {
118: oldValue = new Integer(((Color) oldValue).getRGB());
119: }
120: if (newValue instanceof Color) {
121: newValue = new Integer(((Color) newValue).getRGB());
122: }
123: super.firePropertyChange(oldValue, newValue);
124: }
125: }
126: }
|