KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > ed > VComboBox


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.grid.ed;
15
16 import java.math.*;
17 import javax.swing.*;
18
19 import org.compiere.model.*;
20 import org.compiere.util.*;
21 import org.compiere.plaf.*;
22 import org.compiere.swing.*;
23
24 /**
25  * Combobox with KeyNamePair/ValueNamePair or Locator.
26  * <p>
27  * It has the same hight as a TextField
28  *
29  * @author Jorg Janke
30  * @version $Id: VComboBox.java,v 1.9 2003/08/15 17:24:12 jjanke Exp $
31  */

32 public class VComboBox extends CComboBox
33 {
34     /**
35      * Constructor
36      */

37     public VComboBox()
38     {
39         super();
40 // common_init();
41
}
42     public VComboBox(Object JavaDoc[] items)
43     {
44         super(items);
45 // common_init();
46
}
47     public VComboBox(ComboBoxModel model)
48     {
49         super(model);
50 // common_init();
51
} // VComboBox
52

53     /**
54      * Common Setup
55      *
56     private void common_init()
57     {
58         LookAndFeel.installColorsAndFont(this, "TextField.background", "TextField.foreground", "TextField.font");
59         setForeground(CompierePLAF.getTextColor_Normal());
60         setBackground(CompierePLAF.getFieldBackground_Normal());
61         setPreferredSize(s_text.getPreferredSize());
62     // this.setKeySelectionManager(new ComboSelectionManager());
63     } // common_init
64
65     /** Reference Field *
66     private static JTextField s_text = new JTextField(VTextField.DISPLAY_SIZE);
67
68     /**
69      * Set Selected Item to key.
70      * Find key value in list
71      * @param key
72      */

73     public void setValue(Object JavaDoc key)
74     {
75         if (key == null)
76         {
77             this.setSelectedIndex(-1);
78             return;
79         }
80
81         ComboBoxModel model = getModel();
82         int size = model.getSize();
83         for (int i = 0; i < size; i++)
84         {
85             Object JavaDoc element = model.getElementAt(i);
86             String JavaDoc ID = null;
87             if (element instanceof NamePair)
88                 ID = ((NamePair)element).getID();
89             else if (element instanceof MLocator)
90                 ID = String.valueOf(((MLocator)element).getM_Locator_ID());
91             else
92                 Log.error("VComboBox.setValue - Element not NamePair - " + element.getClass().toString());
93
94             if (key == null || ID == null)
95             {
96                 if (key == null && ID == null)
97                 {
98                     setSelectedIndex(i);
99                     return;
100                 }
101             }
102             else if (ID.equals(key.toString()))
103             {
104                 setSelectedIndex(i);
105                 return;
106             }
107         }
108         setSelectedIndex(-1);
109         setSelectedItem(null);
110     } // setValue
111

112     /**
113      * Set Selected item to key if exists
114      * @param key
115      */

116     public void setValue (int key)
117     {
118         setValue(String.valueOf(key));
119     } // setValue
120

121     /**
122      * Get Value
123      * @return key as Integer or String value
124      */

125     public Object JavaDoc getValue()
126     {
127         if (getSelectedIndex() == -1)
128             return null;
129         //
130
NamePair p = (NamePair)getSelectedItem();
131         if (p == null)
132             return null;
133         //
134
if (p instanceof KeyNamePair)
135         {
136             if (p.getID() == null)
137                 return null;
138             return new Integer JavaDoc(p.getID());
139         }
140         return p.getID();
141     } // getValue
142

143     /**
144      * Get Display
145      * @returns displayed string
146      */

147     public String JavaDoc getDisplay()
148     {
149         if (getSelectedIndex() == -1)
150             return "";
151         //
152
NamePair p = (NamePair)getSelectedItem();
153         if (p == null)
154             return "";
155         return p.getName();
156     } // getDisplay
157

158 } // VComboBox
159
Popular Tags