View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package coras.client.ui;
22  
23  import java.awt.CardLayout;
24  import java.awt.event.ItemEvent;
25  import java.awt.event.ItemListener;
26  
27  import javax.swing.ComboBoxModel;
28  import javax.swing.JComboBox;
29  import javax.swing.JPanel;
30  import javax.swing.JTextField;
31  /***
32   * @author fvr
33   *
34   * TODO To change the template for this generated type comment go to
35   * Window - Preferences - Java - Code Style - Code Templates
36   */
37  public class CorasComboBox extends JPanel {
38  
39  	public static final String SELECTED_ITEM = "selectedItem";
40  	
41  	private JComboBox comboBox = null;
42  	private JTextField textField = null;
43      private boolean editable = true;
44  
45      private ItemListener itemListener = new ItemListener() {
46  		public void itemStateChanged(ItemEvent e) {
47  			if (e.getStateChange() == ItemEvent.SELECTED) {
48  				firePropertyChange(SELECTED_ITEM, null, getComboBox().getSelectedItem());
49  			}
50  		}
51      };
52      
53  	/***
54  	 * This is the default constructor
55  	 */
56  	public CorasComboBox() {
57  		super();
58  		initialize();
59  		getComboBox().addItemListener(itemListener);
60  	}
61  	
62  	public synchronized  void setModel(ComboBoxModel newModel) {
63  	    getComboBox().setModel(newModel);
64          Object item = getComboBox().getSelectedItem();
65          textField.setText(item != null ? item.toString() : "");
66  	}
67  
68      /***
69       * @return
70       */
71      public Object getSelectedItem() {
72          return getComboBox().getSelectedItem();
73      }
74  	
75  	public synchronized void setSelectedItem(Object item) {
76  	    getComboBox().setSelectedItem(item);
77          item = getComboBox().getSelectedItem();
78          textField.setText(item != null ? item.toString() : "");
79  	}
80  	
81      public Object[] getSelectedObjects() {
82  		return getComboBox().getSelectedObjects();
83  	}
84  
85  	public boolean isEditable() {
86          return editable;
87      }
88      
89      public synchronized void setEditable(boolean editable) {
90          if (this.editable == editable) {
91              return;
92          }
93          this.editable = editable;
94          CardLayout layout = (CardLayout) getLayout();
95          if (editable) {
96              layout.show(this, getComboBox().getName());
97          } else {
98              Object item = getComboBox().getSelectedItem();
99              textField.setText(item != null ? item.toString() : "");
100             layout.show(this, getTextField().getName());
101         }
102         repaint();
103     }
104     
105     public boolean isComboBoxEditable() {
106         return getComboBox().isEditable();
107     }
108     
109     public void setComboBoxEditable(boolean editable) {
110         getComboBox().setEditable(editable);
111     }
112     
113 	/***
114 	 * This method initializes this
115 	 * 
116 	 * @return void
117 	 */
118 	private void initialize() {
119 		this.setLayout(new CardLayout());
120 		this.setSize(300,200);
121 		this.add(getComboBox(), getComboBox().getName());
122 		this.add(getTextField(), getTextField().getName());
123 	}
124 	/***
125 	 * This method initializes jComboBox	
126 	 * 	
127 	 * @return javax.swing.JComboBox	
128 	 */    
129 	private JComboBox getComboBox() {
130 		if (comboBox == null) {
131 			comboBox = new JComboBox();
132 			comboBox.setName("comboBox");
133 		}
134 		return comboBox;
135 	}
136 	/***
137 	 * This method initializes jTextField	
138 	 * 	
139 	 * @return javax.swing.JTextField	
140 	 */    
141 	private JTextField getTextField() {
142 		if (textField == null) {
143 			textField = new JTextField();
144 			textField.setName("textField");
145 			textField.setEditable(false);
146 			// textField.setBackground(CorasClient.DISABLED_COLOR);
147 		}
148 		return textField;
149 	}
150 
151 }