001: // StringChoice.java
002: // $Id: StringChoice.java,v 1.2 2000/08/16 21:37:57 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.widgets;
007:
008: import java.awt.BorderLayout;
009: import java.awt.Choice;
010: import java.awt.Container;
011: import java.awt.GridLayout;
012: import java.awt.TextComponent;
013: import java.awt.TextField;
014:
015: import java.awt.event.ActionListener;
016: import java.awt.event.ItemEvent;
017: import java.awt.event.ItemListener;
018: import java.awt.event.TextListener;
019:
020: /**
021: * @version $Revision: 1.2 $
022: * @author Benoît Mahé (bmahe@w3.org)
023: */
024:
025: public class StringChoice extends BorderPanel {
026:
027: class IListener implements ItemListener {
028:
029: public void itemStateChanged(ItemEvent e) {
030: if (e.getStateChange() == ItemEvent.SELECTED) {
031: text.setText((String) e.getItem());
032: }
033: }
034:
035: }
036:
037: public static final int ONE_LINE = 1;
038: public static final int TWO_LINES = 2;
039:
040: private Choice choice = null;
041: private TextField text = null;
042:
043: public synchronized void addActionListener(ActionListener l) {
044: text.addActionListener(l);
045: }
046:
047: public synchronized void removeActionListener(ActionListener l) {
048: text.removeActionListener(l);
049: }
050:
051: public synchronized void addTextListener(TextListener l) {
052: text.addTextListener(l);
053: }
054:
055: public void removeTextListener(TextListener l) {
056: text.removeTextListener(l);
057: }
058:
059: public synchronized void addItemListener(ItemListener l) {
060: choice.addItemListener(l);
061: }
062:
063: public synchronized void removeItemListener(ItemListener l) {
064: choice.removeItemListener(l);
065: }
066:
067: public void addItem(String item) {
068: choice.addItem(item);
069: }
070:
071: public void addItems(String items[]) {
072: for (int i = 0; i < items.length; i++) {
073: if (items[i] != null)
074: addItem(items[i]);
075: }
076: }
077:
078: public synchronized void select(String str) {
079: choice.select(str);
080: }
081:
082: public synchronized void remove(String item) {
083: choice.remove(item);
084: }
085:
086: public void removeAll() {
087: choice.removeAll();
088: }
089:
090: public void setText(String stext) {
091: text.setText(stext);
092: }
093:
094: public String getText() {
095: return text.getText();
096: }
097:
098: private void build(int type) {
099: text = new TextField(20);
100: choice = new Choice();
101: choice.addItemListener(new IListener());
102: switch (type) {
103: case ONE_LINE:
104: setLayout(new BorderLayout(5, 5));
105: add(text, "West");
106: add(choice, "Center");
107: break;
108: case TWO_LINES:
109: default:
110: setLayout(new GridLayout(2, 1, 5, 5));
111: add(choice);
112: add(text);
113: break;
114: }
115: }
116:
117: /**
118: * Get a "one line" String choice with no border.
119: */
120: public StringChoice() {
121: super (IN, 0);
122: build(ONE_LINE);
123: }
124:
125: /**
126: * Get a StringChoice widget.
127: * @param type The posisionning (ONE_LINE, TWO_LINES)
128: */
129: public StringChoice(int type) {
130: super (IN, 0);
131: build(type);
132: }
133:
134: /**
135: * Get a StringChoice widget.
136: * @param type The posisionning (ONE_LINE, TWO_LINES)
137: * @param border The border type (SOLID, RAISED, LOWERED, IN, OUT)
138: */
139: public StringChoice(int type, int border) {
140: super (border);
141: build(type);
142: }
143:
144: /**
145: * Get a StringChoice widget.
146: * @param type The posisionning (ONE_LINE, TWO_LINES)
147: * @param border The border type (SOLID, RAISED, LOWERED, IN, OUT)
148: * @param thickness The border's thickness.
149: */
150: public StringChoice(int type, int border, int thickness) {
151: super(border, thickness);
152: build(type);
153: }
154: }
|