001: // FakeComboBox.java
002: // $Id: FakeComboBox.java,v 1.9 2000/08/17 13:11:26 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.widgets;
007:
008: import java.awt.AWTEventMulticaster;
009: import java.awt.BorderLayout;
010: import java.awt.Component;
011: import java.awt.Container;
012: import java.awt.Dimension;
013: import java.awt.Image;
014: import java.awt.List;
015: import java.awt.Panel;
016: import java.awt.TextComponent;
017: import java.awt.TextField;
018: import java.awt.Toolkit;
019:
020: import java.awt.List;
021:
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.event.ItemEvent;
025: import java.awt.event.ItemListener;
026:
027: /**
028: * FakeComboBox :
029: * @author Benoit Mahe <bmahe@sophia.inria.fr>
030: */
031:
032: public class FakeComboBox extends Panel implements ActionListener,
033: ItemListener {
034:
035: class GoodList extends List {
036:
037: FakeComboBox parent = null;
038:
039: int max(int a, int b) {
040: return ((a < b) ? b : a);
041: }
042:
043: public Dimension getMinimumSize() {
044: return new Dimension(max(parent.text.getSize().width, super
045: .getMinimumSize().width),
046: super .getMinimumSize().height);
047: }
048:
049: public Dimension getPreferredSize() {
050: return getMinimumSize();
051: }
052:
053: public void add(String item) { //FIXME
054: super .addItem(item);
055: }
056:
057: GoodList(FakeComboBox parent, int nb) {
058: super (nb);
059: this .parent = parent;
060: }
061:
062: }
063:
064: // The FakeComboBox itself
065:
066: protected TextField text = null;
067: protected ImageButton button = null;
068: protected int listSize = 0;
069: protected GoodList list = null;
070: protected Panel plist = null;
071: transient ActionListener actionListener;
072: private String command = "";
073:
074: // ItemListener
075:
076: public void itemStateChanged(ItemEvent e) {
077: if (e.getStateChange() == ItemEvent.SELECTED) {
078: Integer idx = (Integer) e.getItem();
079: String key = list.getItem(idx.intValue());
080: if (key != null) {
081: setText(key);
082: fireActionEvent();
083: }
084: hidePopup();
085: }
086: }
087:
088: /**
089: * Sets the action command String used when an ActionEvent is fired
090: * @param command The command String
091: */
092:
093: public void setActionCommand(String command) {
094: this .command = command;
095: }
096:
097: /**
098: * Returns the action command String
099: */
100:
101: public String getActionCommand() {
102: return command;
103: }
104:
105: /**
106: * Adds an action listener to this FakeComboBox
107: * @param al The ActionListener
108: */
109:
110: public synchronized void addActionListener(ActionListener al) {
111: actionListener = AWTEventMulticaster.add(actionListener, al);
112: }
113:
114: /**
115: * Removes an action listener to this FakeComboBox
116: * @param al The ActionListener
117: */
118:
119: public synchronized void removeActionListener(ActionListener al) {
120: actionListener = AWTEventMulticaster.remove(actionListener, al);
121: }
122:
123: /**
124: * fire a new ActionEvent and process it, if some listeners are listening
125: */
126:
127: protected void fireActionEvent() {
128: if (actionListener != null) {
129: ActionEvent ae = new ActionEvent(this ,
130: ActionEvent.ACTION_PERFORMED, command);
131: actionListener.actionPerformed(ae);
132: }
133: }
134:
135: protected void hidePopup() {
136: if (plist.isShowing()) {
137: plist.setVisible(false);
138: button.switchImage();
139: }
140: }
141:
142: protected void showPopup() {
143: if (!plist.isShowing()) {
144: plist.setVisible(true);
145: button.switchImage();
146: updateParent();
147: }
148: }
149:
150: void updateParent() {
151: Component parent = getParent();
152: if (parent != null)
153: parent.validate();
154: }
155:
156: /**
157: * ActionListener
158: */
159:
160: public void actionPerformed(ActionEvent evt) {
161: String command = evt.getActionCommand();
162: if (command.equals("popup")) {
163: if (plist.isShowing())
164: hidePopup();
165: else
166: showPopup();
167: } else if (evt.getSource() == text) {
168: fireActionEvent();
169: }
170: }
171:
172: public void setText(String text) {
173: this .text.setText(text);
174: }
175:
176: public String getText() {
177: return this .text.getText();
178: }
179:
180: public void add(String item) {
181: list.add(item);
182: }
183:
184: public FakeComboBox(int size, int listSize, boolean editable,
185: String imagePath1, String imagePath2) {
186: super ();
187: list = new GoodList(this , listSize);
188: list.addItemListener(this );
189: Image down = Toolkit.getDefaultToolkit().getImage(imagePath1);
190: Image left = Toolkit.getDefaultToolkit().getImage(imagePath2);
191:
192: setLayout(new BorderLayout());
193: text = new TextField(size);
194: text.setEditable(editable);
195: text.addActionListener(this );
196: button = new ImageButton(down, left);
197: button.addActionListener(this );
198: button.setActionCommand("popup");
199: add(text, "Center");
200: add(button, "East");
201: plist = new Panel();
202: plist.add(list);
203: add(plist, "South");
204: plist.setVisible(true);
205: }
206:
207: }
|