KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > DatabaseTypePropertyEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.explorer;
21
22 import java.awt.*;
23 import java.beans.*;
24 import java.sql.*;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27
28 import org.openide.util.NbBundle;
29
30 /** A property editor for Color class.
31 */

32 public class DatabaseTypePropertyEditor implements PropertyEditor {
33
34     private int[] constants;
35     private String JavaDoc[] names;
36     private int index;
37     private String JavaDoc name;
38     private PropertyChangeSupport support;
39
40     public DatabaseTypePropertyEditor() {
41         ResourceBundle JavaDoc bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle");
42         support = new PropertyChangeSupport(this);
43         constants = new int[] {
44             java.sql.Types.ARRAY,
45             java.sql.Types.BIGINT,
46             java.sql.Types.BINARY,
47             java.sql.Types.BIT,
48             java.sql.Types.BLOB,
49             java.sql.Types.CHAR,
50             java.sql.Types.CLOB,
51             java.sql.Types.DATE,
52             java.sql.Types.DECIMAL,
53             java.sql.Types.DISTINCT,
54             java.sql.Types.DOUBLE,
55             java.sql.Types.FLOAT,
56             java.sql.Types.INTEGER,
57             java.sql.Types.JAVA_OBJECT,
58             java.sql.Types.LONGVARBINARY,
59             java.sql.Types.LONGVARCHAR,
60             java.sql.Types.NUMERIC,
61             java.sql.Types.REAL,
62             java.sql.Types.REF,
63             java.sql.Types.SMALLINT,
64             java.sql.Types.TIME,
65             java.sql.Types.TIMESTAMP,
66             java.sql.Types.TINYINT,
67             java.sql.Types.VARBINARY,
68             java.sql.Types.VARCHAR,
69             java.sql.Types.OTHER};
70         names = new String JavaDoc[] {
71             bundle.getString("SQL_ARRAY"), //NOI18N
72
bundle.getString("SQL_BIGINT"), //NOI18N
73
bundle.getString("SQL_BINARY"), //NOI18N
74
bundle.getString("SQL_BIT"), //NOI18N
75
bundle.getString("SQL_BLOB"), //NOI18N
76
bundle.getString("SQL_CHAR"), //NOI18N
77
bundle.getString("SQL_CLOB"), //NOI18N
78
bundle.getString("SQL_DATE"), //NOI18N
79
bundle.getString("SQL_DECIMAL"), //NOI18N
80
bundle.getString("SQL_DISTINCT"), //NOI18N
81
bundle.getString("SQL_DOUBLE"), //NOI18N
82
bundle.getString("SQL_FLOAT"), //NOI18N
83
bundle.getString("SQL_INTEGER"), //NOI18N
84
bundle.getString("SQL_JAVA_OBJECT"), //NOI18N
85
bundle.getString("SQL_LONGVARBINARY"), //NOI18N
86
bundle.getString("SQL_LONGVARCHAR"), //NOI18N
87
bundle.getString("SQL_NUMERIC"), //NOI18N
88
bundle.getString("SQL_REAL"), //NOI18N
89
bundle.getString("SQL_REF"), //NOI18N
90
bundle.getString("SQL_SMALLINT"), //NOI18N
91
bundle.getString("SQL_TIME"), //NOI18N
92
bundle.getString("SQL_TIMESTAMP"), //NOI18N
93
bundle.getString("SQL_TINYINT"), //NOI18N
94
bundle.getString("SQL_VARBINARY"), //NOI18N
95
bundle.getString("SQL_VARCHAR"), //NOI18N
96
bundle.getString("SQL_OTHER") //NOI18N
97
}; //NOI18N
98
}
99
100     public DatabaseTypePropertyEditor(int[] types, String JavaDoc[] titles) {
101         support = new PropertyChangeSupport(this);
102         constants = types;
103         names = titles;
104     }
105
106     public Object JavaDoc getValue () {
107         return new Integer JavaDoc(constants[index]);
108     }
109
110     public void setValue (Object JavaDoc object) {
111         ResourceBundle JavaDoc bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle");
112 // if (!(object instanceof Number)) {
113
// String message = MessageFormat.format(bundle.getString("EXC_CannotOperateWith"), new String[] {object.toString()}); // NOI18N
114
// throw new IllegalArgumentException(message);
115
// }
116
// int ii = ((Number)object).intValue ();
117

118 //cannot use previous code because of MSSQL ODBC problems - see DriverSpecification.getRow() for more info
119
Integer JavaDoc type;
120         try {
121             type = new Integer JavaDoc(object.toString());
122         } catch (NumberFormatException JavaDoc exc) {
123             String JavaDoc message = MessageFormat.format(bundle.getString("EXC_CannotOperateWith"), new String JavaDoc[] {object.toString()}); // NOI18N
124
throw new IllegalArgumentException JavaDoc(message);
125         }
126         
127         int ii = type.intValue();
128 //end of MSSQL hack
129

130         int i;
131         int k = constants.length;
132         
133         for (i = 0; i < k; i++)
134             if (constants [i] == ii)
135                 break;
136         
137         if (i == k) {
138             switch (ii) { //cannot find 'ii' type, try to find it in java.sql.Types
139
case -7: name = bundle.getString("SQL_BIT"); break; //NOI18N
140
case -6: name = bundle.getString("SQL_TINYINT"); break; //NOI18N
141
case 5: name = bundle.getString("SQL_SMALLINT"); break; //NOI18N
142
case 4: name = bundle.getString("SQL_INTEGER"); break; //NOI18N
143
case -5: name = bundle.getString("SQL_BIGINT"); break; //NOI18N
144
case 6: name = bundle.getString("SQL_FLOAT"); break; //NOI18N
145
case 7: name = bundle.getString("SQL_REAL"); break; //NOI18N
146
case 8: name = bundle.getString("SQL_DOUBLE"); break; //NOI18N
147
case 2: name = bundle.getString("SQL_NUMERIC"); break; //NOI18N
148
case 3: name = bundle.getString("SQL_DECIMAL"); break; //NOI18N
149
case 1: name = bundle.getString("SQL_CHAR"); break; //NOI18N
150
case 12: name = bundle.getString("SQL_VARCHAR"); break; //NOI18N
151
case -1: name = bundle.getString("SQL_LONGVARCHAR"); break; //NOI18N
152
case 91: name = bundle.getString("SQL_DATE"); break; //NOI18N
153
case 92: name = bundle.getString("SQL_TIME"); break; //NOI18N
154
case 93: name = bundle.getString("SQL_TIMESTAMP"); break; //NOI18N
155
case -2: name = bundle.getString("SQL_BINARY"); break; //NOI18N
156
case -3: name = bundle.getString("SQL_VARBINARY"); break; //NOI18N
157
case -4: name = bundle.getString("SQL_LONGVARBINARY"); break; //NOI18N
158
case 0: name = bundle.getString("SQL_NULL"); break; //NOI18N
159
case 1111: name = bundle.getString("SQL_OTHER"); break; //NOI18N
160
case 2000: name = bundle.getString("SQL_JAVA_OBJECT"); break; //NOI18N
161
case 2001: name = bundle.getString("SQL_DISTINCT"); break; //NOI18N
162
case 2002: name = bundle.getString("SQL_STRUCT"); break; //NOI18N
163
case 2003: name = bundle.getString("SQL_ARRAY"); break; //NOI18N
164
case 2004: name = bundle.getString("SQL_BLOB"); break; //NOI18N
165
case 2005: name = bundle.getString("SQL_CLOB"); break; //NOI18N
166
case 2006: name = bundle.getString("SQL_REF"); break; //NOI18N
167
default: name = bundle.getString("SQL_UNKNOWN"); //NOI18N
168
}
169             index = 0;
170         } else {
171             index = i;
172             name = names [i];
173         }
174
175         support.firePropertyChange (null, null, null);
176     }
177
178     public String JavaDoc getAsText () {
179         return name;
180     }
181
182     public void setAsText (String JavaDoc string) throws IllegalArgumentException JavaDoc {
183         int i, k = names.length;
184         for (i = 0; i < k; i++) if (names [i].equals (string)) break;
185         if (i == k) {
186             String JavaDoc message = MessageFormat.format(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("EXC_CannotFindAsText"), new String JavaDoc[] {string}); // NOI18N
187
throw new IllegalArgumentException JavaDoc(message);
188         }
189         index = i;
190         name = names [i];
191         return;
192     }
193
194     public String JavaDoc getJavaInitializationString () {
195         return "" + index; //NOI18N
196
}
197
198     public String JavaDoc[] getTags () {
199         return names;
200     }
201
202     public boolean isPaintable () {
203         return false;
204     }
205
206     public void paintValue (Graphics g, Rectangle rectangle) {
207     }
208
209     public boolean supportsCustomEditor () {
210         return false;
211     }
212
213     public Component getCustomEditor () {
214         return null;
215     }
216
217     public void addPropertyChangeListener (PropertyChangeListener propertyChangeListener) {
218         support.addPropertyChangeListener (propertyChangeListener);
219     }
220
221     public void removePropertyChangeListener (PropertyChangeListener propertyChangeListener) {
222         support.removePropertyChangeListener (propertyChangeListener);
223     }
224 }
225
Popular Tags