KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > salsa > OpenList


1 /*
2 ** Salsa - Swing Add-On Suite
3 ** Copyright (c) 2001, 2002 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package salsa;
24
25 import java.awt.*;
26 import java.awt.event.*;
27 import javax.swing.*;
28 import javax.swing.event.*;
29
30 /**
31  * OpenList combines a label, text field and a list.
32  */

33
34 public class OpenList extends JPanel
35        implements ListSelectionListener, ActionListener
36 {
37    JList _list;
38    JScrollPane _scroll;
39    JTextField _text;
40    JLabel _title;
41
42    public OpenList( String JavaDoc data[], String JavaDoc title )
43    {
44       setLayout( null );
45
46       _title = new JLabel( title, JLabel.LEFT );
47       add( _title );
48
49       _text = new JTextField();
50       _text.addActionListener( this );
51       add( _text );
52
53       _list = new JList( data );
54       _list.setVisibleRowCount( 4 );
55       _list.addListSelectionListener( this );
56
57       _scroll = new JScrollPane( _list );
58       add( _scroll );
59    }
60
61    public void setSelected( String JavaDoc value )
62    {
63       _list.setSelectedValue( value, true );
64       _text.setText( value );
65    }
66
67    public void setSelectedInt( int value )
68    {
69       setSelected( "" + value );
70    }
71
72    public Dimension getMaximumSize()
73    {
74       Insets ins = getInsets();
75       Dimension d1 = _title.getMaximumSize();
76       Dimension d2 = _text.getMaximumSize();
77       Dimension d3 = _scroll.getMaximumSize();
78
79       int w = Math.max( Math.max( d1.width, d2.width ), d3.width );
80       int h = d1.height + d2.height + d3.height;
81
82       return new Dimension( w + ins.left + ins.right,
83             h + ins.top + ins.bottom );
84    }
85
86    public Dimension getMinimumSize()
87    {
88       Insets ins = getInsets();
89       Dimension d1 = _title.getMinimumSize();
90       Dimension d2 = _text.getMinimumSize();
91       Dimension d3 = _scroll.getMinimumSize();
92
93       int w = Math.max( Math.max( d1.width, d2.width ), d3.width );
94       int h = d1.height + d2.height + d3.height;
95
96       return new Dimension( w + ins.left + ins.right,
97             h + ins.top + ins.bottom );
98    }
99
100    public Dimension getPreferredSize()
101    {
102       Insets ins = getInsets();
103       Dimension d1 = _title.getPreferredSize();
104       Dimension d2 = _text.getPreferredSize();
105       Dimension d3 = _scroll.getPreferredSize();
106
107       int w = Math.max( Math.max( d1.width, d2.width ), d3.width );
108       int h = d1.height + d2.height + d3.height;
109
110       return new Dimension( w + ins.left + ins.right,
111             h + ins.top + ins.bottom );
112    }
113
114    public String JavaDoc getSelected()
115    {
116       return _text.getText();
117    }
118
119    public int getSelectedInt()
120    {
121       try
122       {
123          return Integer.parseInt( getSelected() );
124       }
125       catch( NumberFormatException JavaDoc nex )
126       {
127          return -1;
128       }
129    }
130
131    public void actionPerformed( ActionEvent ev )
132    {
133       // sync listbox with textbox
134

135       ListModel model = _list.getModel();
136       String JavaDoc key = _text.getText().toLowerCase();
137       for( int i = 0; i < model.getSize(); i++ )
138       {
139          String JavaDoc data = ( String JavaDoc ) model.getElementAt( i );
140          if( data.toLowerCase().startsWith( key ) )
141          {
142             _list.setSelectedValue( data, true );
143             break;
144          }
145       }
146    }
147
148    public void addListSelectionListener( ListSelectionListener l )
149    {
150       _list.addListSelectionListener( l );
151    }
152
153    public void doLayout()
154    {
155       Insets ins = getInsets();
156       Dimension d = getSize();
157       int x = ins.left;
158       int y = ins.top;
159       int w = d.width - ins.left - ins.right;
160       int h = d.height - ins.top - ins.bottom;
161
162       Dimension d1 = _title.getPreferredSize();
163       _title.setBounds( x, y, w, d1.height );
164       y += d1.height;
165
166       Dimension d2 = _text.getPreferredSize();
167       _text.setBounds( x, y, w, d2.height );
168       y += d2.height;
169
170       _scroll.setBounds( x, y, w, h - y );
171    }
172
173    public void valueChanged( ListSelectionEvent ev )
174    {
175       // sync textbox with listbox
176
Object JavaDoc obj = _list.getSelectedValue();
177       if( obj != null )
178          _text.setText( obj.toString() );
179    }
180 }
181
Popular Tags