001 /** =====================================================================
002 *
003 * File Name : $Id: LabelListBox.java,v 1.4 2008/01/15 11:08:16 cb Exp $
004 *
005 * Description
006 * -----------
007 *
008 * See javadoc comment
009 *
010 * =====================================================================
011 *
012 * @Author : Craige Bevil
013 * Control Software Group
014 * Isaac Newton Group of Telescopes
015 *
016 * =====================================================================
017 *
018 * Modification Log
019 *
020 * Vers Date Author Reason
021 * ---- ---- ------ ------
022 * 1 C.Bevil First Release
023 *
024 * Commissioning Notes
025 * -------------------
026 *
027 * None
028 *
029 * =====================================================================
030 *
031 * @Version : $Id: LabelListBox.java,v 1.4 2008/01/15 11:08:16 cb Exp $
032 *
033 * @Author : $Author: cb $
034 *
035 * Header : $Header: /opt/INGsrc/src/CVS/softproj/FaultDatabase/src/FaultDatabase/FaultDatabase/src/GWTApplication/client/LabelListBox.java,v 1.4 2008/01/15 11:08:16 cb Exp $
036 *
037 * Log : $Log: LabelListBox.java,v $
038 * Log : Revision 1.4 2008/01/15 11:08:16 cb
039 * Log : Ran through PMD and sorted out the javadoc so that we could export the
040 * Log : javadoc to the javadoc repository.
041 * Log :
042 * Log : Revision 1.3 2007/08/17 14:26:42 cb
043 * Log : Updated for lastest prototype incorporating a lot of Nikos comments
044 * Log :
045 * Log : Revision 1.2 2007/08/01 13:00:06 cb
046 * Log : First prototype after import
047 * Log :
048 * Log : Revision 1.1.1.1 2007/06/01 08:33:26 cb
049 * Log : Imported using TkCVS
050 * Log :
051 *
052 * =====================================================================*/
053
054 package GWTApplication.client;
055
056 import com.google.gwt.user.client.*;
057 import com.google.gwt.user.client.rpc.*;
058 import com.google.gwt.user.client.ui.*;
059 import com.google.gwt.user.client.ui.FlexTable.*;
060
061 /**
062 * This is a composite widget which comprises of a list box and a
063 * label. It's a convenience class which binds together two types of
064 * widgets which are commonly used together. I have purposefully not
065 * reimplemented all of the methods associated with the sub widgets
066 * simply to make the task of coding the class simpler.
067 * @author Craige Bevil
068 * @version $Id: LabelListBox.java,v 1.4 2008/01/15 11:08:16 cb Exp $
069 */
070
071 class LabelListBox extends Composite {
072
073 /**
074 * Whether the two widgets should be stacked vertically
075 */
076
077 final public static int VERTICAL_ALIGNMENT = 1;
078
079 /**
080 * Whether the two widgets should be stacked horizontally
081 */
082
083 final public static int HORIZONTAL_ALIGNMENT = 2;
084
085 /**
086 * This is a list box which will contain all of the options which can may be selected by the user
087 */
088
089 public ListBox listBox = new ListBox();
090
091 /**
092 * This is the label which is associated with the list box.
093 */
094
095 public Label label = new Label();
096
097 /**
098 * This is the constructor which will generate a panel which
099 * contains both the label and the list box
100 * @param Alignment Whether the widgets should be arranged horizontally or
101 * vertically. Should be one of VERTICAL_ALIGNMENT or
102 * HORIZONTAL_ALIGNMENT.
103 */
104
105 LabelListBox (final int Alignment) {
106
107 if (Alignment != VERTICAL_ALIGNMENT && Alignment != HORIZONTAL_ALIGNMENT) {
108 return;
109 }
110
111 if (Alignment == HORIZONTAL_ALIGNMENT) {
112
113 final HorizontalPanel panel = new HorizontalPanel();
114
115 label.setWidth("100%");
116 listBox.setWidth("100%");
117
118 panel.add(label);
119 panel.add(listBox);
120
121 initWidget(panel);
122
123 } else {
124
125 final VerticalPanel panel = new VerticalPanel();
126
127 label.setWidth("100%");
128 listBox.setWidth("100%");
129
130 panel.add(label);
131 panel.add(listBox);
132
133 initWidget(panel);
134
135 }
136 }
137
138 /**
139 * This will return the value of the item which has been selected
140 * in the list
141 * @return The text of the item which has been selected in the
142 * list. If nothing has been selected by the user then it returns null
143 */
144
145 public String getSelectedItemText () {
146
147 // If the user has not selected anything then we can
148
149 if (listBox.getSelectedIndex() == -1) {
150 return null;
151 }
152
153 return listBox.getItemText(listBox.getSelectedIndex());
154 }
155
156 /**
157 * This will be used to select the item which is specified in the
158 * list
159 * @param ItemToSelect This is the item in the list which is to be
160 * selected by default.
161 * @return boolean True if we managed to set the item in the list
162 * to the default value which was specified.
163 */
164
165 public boolean setSelectedItem(final String ItemToSelect) {
166
167 for (int i=0; i < listBox.getItemCount();i++) {
168
169 if (listBox.getItemText(i).equals(ItemToSelect)) {
170 listBox.setSelectedIndex(i);
171 return true;
172 }
173 }
174
175 return false;
176 }
177
178 /**
179 * This method permits the caller to set the width of both the
180 * label and the list box at the same time. The units which must
181 * be passed to this method must be expressed in valid CSS units.
182 *
183 * @param labelWidth this is the length of the label which must be expressed in valid CSS units
184 * @param listBoxWidth this is the length of the list box which must be expressed in valid CSS units
185 */
186
187 public void setWidth(final String labelWidth,final String listBoxWidth) {
188 label.setWidth(labelWidth);
189 listBox.setWidth(listBoxWidth);
190 }
191 }