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 java.awt.Component;
020: import java.awt.event.KeyAdapter;
021: import java.awt.event.KeyEvent;
022:
023: import javax.swing.DefaultComboBoxModel;
024: import javax.swing.DefaultListCellRenderer;
025: import javax.swing.Icon;
026: import javax.swing.JComboBox;
027: import javax.swing.JLabel;
028: import javax.swing.JList;
029: import javax.swing.event.PopupMenuEvent;
030: import javax.swing.event.PopupMenuListener;
031:
032: /**
033: * ComboBoxPropertyEditor. <br>
034: *
035: */
036: public class ComboBoxPropertyEditor extends AbstractPropertyEditor {
037:
038: private Object oldValue;
039: private Icon[] icons;
040:
041: public ComboBoxPropertyEditor() {
042: editor = new JComboBox() {
043: public void setSelectedItem(Object anObject) {
044: oldValue = getSelectedItem();
045: super .setSelectedItem(anObject);
046: }
047: };
048:
049: final JComboBox combo = (JComboBox) editor;
050:
051: combo.setRenderer(new Renderer());
052: combo.addPopupMenuListener(new PopupMenuListener() {
053: public void popupMenuCanceled(PopupMenuEvent e) {
054: }
055:
056: public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
057: ComboBoxPropertyEditor.this .firePropertyChange(
058: oldValue, combo.getSelectedItem());
059: }
060:
061: public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
062: }
063: });
064: combo.addKeyListener(new KeyAdapter() {
065: public void keyPressed(KeyEvent e) {
066: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
067: ComboBoxPropertyEditor.this .firePropertyChange(
068: oldValue, combo.getSelectedItem());
069: }
070: }
071: });
072: combo.setSelectedIndex(-1);
073: }
074:
075: public Object getValue() {
076: Object selected = ((JComboBox) editor).getSelectedItem();
077: if (selected instanceof Value) {
078: return ((Value) selected).value;
079: } else {
080: return selected;
081: }
082: }
083:
084: public void setValue(Object value) {
085: JComboBox combo = (JComboBox) editor;
086: Object current = null;
087: int index = -1;
088: for (int i = 0, c = combo.getModel().getSize(); i < c; i++) {
089: current = combo.getModel().getElementAt(i);
090: if (value == current
091: || (current != null && current.equals(value))) {
092: index = i;
093: break;
094: }
095: }
096: ((JComboBox) editor).setSelectedIndex(index);
097: }
098:
099: public void setAvailableValues(Object[] values) {
100: ((JComboBox) editor).setModel(new DefaultComboBoxModel(values));
101: }
102:
103: public void setAvailableIcons(Icon[] icons) {
104: this .icons = icons;
105: }
106:
107: public class Renderer extends DefaultListCellRenderer {
108: public Component getListCellRendererComponent(JList list,
109: Object value, int index, boolean isSelected,
110: boolean cellHasFocus) {
111: Component component = super
112: .getListCellRendererComponent(
113: list,
114: (value instanceof Value) ? ((Value) value).visualValue
115: : value, index, isSelected,
116: cellHasFocus);
117: if (icons != null && index >= 0
118: && component instanceof JLabel)
119: ((JLabel) component).setIcon(icons[index]);
120: return component;
121: }
122: }
123:
124: public static final class Value {
125: private Object value;
126: private Object visualValue;
127:
128: public Value(Object value, Object visualValue) {
129: this .value = value;
130: this .visualValue = visualValue;
131: }
132:
133: public boolean equals(Object o) {
134: if (o == this )
135: return true;
136: if (value == o || (value != null && value.equals(o)))
137: return true;
138: return false;
139: }
140:
141: public int hashCode() {
142: return value == null ? 0 : value.hashCode();
143: }
144: }
145: }
|