1 /* -------------------------------------------------------------------
2 * Java source file for the class EnumEditor
3 *
4 * Copyright (c), 2002, Masahiro Takatsuka.
5 * All Rights Researved.
6 *
7 * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8 * $Author: takatsukam $
9 *
10 * $Date: 2003/07/25 04:51:45 $
11 *
12 * $Id: EnumEditor.java,v 1.1.1.1 2003/07/25 04:51:45 takatsukam Exp $
13 *
14 * Reference: Document no:
15 * ___ ___
16 *
17 * To Do:
18 * ___
19 *
20 ------------------------------------------------------------------- */
21
22 /* --------------------------- Package ---------------------------- */
23 package net.jbeans.bean.property.editor;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */
26 import java.awt.event.*;
27 import java.beans.*;
28 import javax.swing.*;
29
30 /*====================================================================
31 Implementation of class EnumEditor
32 ====================================================================*/
33 /***
34 * A property editor for a swing enumerated type. Handles the case in which the
35 * PropertyDescriptor has a value for "enumerationValues".
36 * Note: the init() method must be called before the set/get methods can be
37 * called.
38 *
39 * @version $Revision: 1.1.1.1 $
40 * @author Masahiro Takatsuka (masa@jbeans.net)
41 * @see EditorSupport
42 * @see ActionListener
43 */
44
45 public final class EnumEditor extends EditorSupport implements ActionListener {
46 public JComboBox combobox;
47
48 public final void setValue(Object value) {
49 super.setValue(value);
50
51 // Set combo box if it's a new value. We want to reduce number
52 // of extraneous events.
53 EnumeratedItem item = (EnumeratedItem)this.combobox.getSelectedItem();
54 if (value != null && !value.equals(item.getValue())) {
55 for (int i = 0; i < this.combobox.getItemCount(); ++i ) {
56 item = (EnumeratedItem)this.combobox.getItemAt(i);
57 if (item.getValue().equals(value)) {
58 // XXX - hack! Combo box shouldn't call action event
59 // for setSelectedItem!!
60 this.combobox.removeActionListener(this);
61 this.combobox.setSelectedItem(item);
62 this.combobox.addActionListener(this);
63 return;
64 }
65 }
66 }
67 }
68
69 public final String getJavaInitializationString(){
70 return "" + getValue();
71
72 }
73
74 /***
75 * Initializes this property editor with the enumerated items. Instances
76 * can be shared but there are issues.
77 * <p>
78 * This method does a lot of jiggery pokery since enumerated
79 * types are unlike any other homogenous types. Enumerated types may not
80 * represent the same set of values.
81 * <p>
82 * One method would be to empty the list of values which would have the
83 * side effect of firing notification events. Another method would be to
84 * recreate the combobox.
85 */
86 public final void init(PropertyDescriptor descriptor) {
87 Object[] enum = (Object[])descriptor.getValue( "enumerationValues" );
88 if (enum != null) {
89 if (this.combobox == null) {
90 this.combobox = new JComboBox();
91
92 JPanel panel = new JPanel();
93 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
94 panel.add(this.combobox);
95 setPanel(panel);
96 } else {
97 // Remove action listener to reduce extra events.
98 this.combobox.removeActionListener(this);
99 this.combobox.removeAllItems();
100 }
101
102 for ( int i = 0; i < enum.length; i += 3 ) {
103 this.combobox.addItem(new EnumeratedItem((Integer)enum[i+1], (String)enum[i] ) );
104 }
105
106 this.combobox.addActionListener(this);
107 }
108 }
109
110 /***
111 * Event is set when a combo selection changes.
112 */
113 public final void actionPerformed(ActionEvent evt) {
114 EnumeratedItem item = (EnumeratedItem)this.combobox.getSelectedItem();
115 if (item != null && !getValue().equals(item.getValue())) {
116 setValue(item.getValue());
117 }
118 }
119
120 /***
121 * Object which holds an enumerated item plus its label.
122 */
123 final class EnumeratedItem {
124 private Integer value;
125 private String name;
126
127 public EnumeratedItem(Integer value, String name) {
128 this.value = value;
129 this.name = name;
130 }
131
132 public final Integer getValue() {
133 return this.value;
134 }
135
136 public final String toString() {
137 return this.name;
138 }
139 }
140 }
This page was automatically generated by Maven