001: // StringChoice.java
002: // $Id: StringChoice.java,v 1.7 2000/08/16 21:37:31 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.jigadmin.widgets;
007:
008: import java.awt.Component;
009: import java.awt.BorderLayout;
010: import java.awt.GridLayout;
011: import java.awt.event.ItemListener;
012: import java.awt.event.ItemEvent;
013: import java.awt.event.ActionListener;
014:
015: import javax.swing.JPanel;
016: import javax.swing.JTextField;
017: import javax.swing.JComboBox;
018: import javax.swing.BorderFactory;
019: import javax.swing.event.DocumentListener;
020:
021: /**
022: * String choice widget.
023: * @version $Revision: 1.7 $
024: * @author Benoît Mahé (bmahe@w3.org)
025: */
026: public class StringChoice extends JPanel {
027:
028: private JComboBox combo = null;
029: private JTextField text = null;
030:
031: ItemListener il = new ItemListener() {
032: public void itemStateChanged(ItemEvent e) {
033: if (e.getStateChange() == ItemEvent.SELECTED) {
034: setTextInternal((String) e.getItem());
035: }
036: }
037: };
038:
039: public synchronized void addActionListener(ActionListener l) {
040: text.addActionListener(l);
041: }
042:
043: public synchronized void removeActionListener(ActionListener l) {
044: text.removeActionListener(l);
045: }
046:
047: public synchronized void addDocumentListener(DocumentListener l) {
048: text.getDocument().addDocumentListener(l);
049: }
050:
051: public void removeDocumentListener(DocumentListener l) {
052: text.getDocument().removeDocumentListener(l);
053: }
054:
055: public synchronized void addItemListener(ItemListener l) {
056: combo.addItemListener(l);
057: }
058:
059: public synchronized void removeItemListener(ItemListener l) {
060: combo.removeItemListener(l);
061: }
062:
063: public void addItem(String item) {
064: combo.addItem(item);
065: }
066:
067: public void addItems(String items[]) {
068: for (int i = 0; i < items.length; i++) {
069: if (items[i] != null)
070: addItem(items[i]);
071: }
072: }
073:
074: public synchronized void select(String str) {
075: combo.setSelectedItem(str);
076: }
077:
078: public synchronized void remove(String item) {
079: combo.removeItem(item);
080: }
081:
082: public void removeAll() {
083: combo.removeAll();
084: }
085:
086: public void setText(String stext) {
087: text.setText(stext);
088: }
089:
090: public String getText() {
091: return text.getText();
092: }
093:
094: protected void setTextInternal(String stext) {
095: text.setText(stext);
096: }
097:
098: public void initialize(String items[]) {
099: text = new JTextField(10);
100: text.setBorder(BorderFactory.createLoweredBevelBorder());
101:
102: combo = new JComboBox(items);
103: combo.addItemListener(il);
104:
105: setLayout(new BorderLayout(5, 5));
106:
107: add(text, BorderLayout.WEST);
108: add(combo, BorderLayout.CENTER);
109: }
110:
111: /**
112: * Get a "one line" String choice with no border.
113: */
114: public StringChoice() {
115: super();
116: }
117:
118: }
|