1
/** BEGIN COPYRIGHT BLOCK
2
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
3
* Copyright (C) 2005 Red Hat, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* END COPYRIGHT BLOCK **/
20
package com.netscape.admin.dirserv.attredit;
22
import java.awt.Rectangle;
23
import java.util.Hashtable;
24
import java.util.Enumeration;
25
import com.netscape.management.client.util.Debug;
26
import netscape.ldap.LDAPAttributeSchema;
29
* A factory for producing an attribute editor for a particular attribute
32
* @version 1.1, 02/18/00
35
public class AttributeEditorFactory {
38
* Set up the built-in associations
40
static private boolean init() {
41
_nameTable = new Hashtable();
42
_typeTable = new Hashtable();
43
_editorTable = new Hashtable();
44
_allTable = new Hashtable();
46
/* Set up the built-in preferences */
47
addName( "objectclass",
48
_package + ".ObjectClassAttributeEditor" );
50
_package + ".ImageAttributeEditor" );
51
addName( "userpassword",
52
_package + ".PasswordAttributeEditor" );
53
addName( "usercertificate",
54
_package + ".BinaryAttributeEditor" );
56
addSyntax( LDAPAttributeSchema.integer,
57
_package + ".StringAttributeEditor" );
58
addSyntax( LDAPAttributeSchema.cis,
59
_package + ".StringAttributeEditor" );
60
addSyntax( LDAPAttributeSchema.ces,
61
_package + ".StringAttributeEditor" );
62
addSyntax( LDAPAttributeSchema.dn,
63
_package + ".StringAttributeEditor" );
64
addSyntax( LDAPAttributeSchema.telephone,
65
_package + ".StringAttributeEditor" );
66
addSyntax( LDAPAttributeSchema.binary,
67
_package + ".BinaryAttributeEditor" );
68
addSyntax( LDAPAttributeSchema.telephone,
69
_package + ".StringAttributeEditor" );
70
/* For the unknown syntaxes we use the string attribute editor (the default one) */
71
addSyntax( LDAPAttributeSchema.unknown,
72
_package + ".StringAttributeEditor" );
74
setDefaultEditor( _defaultEditorName );
79
* Register a class to invoke for editing a particular attribute.
80
* Any previous registration for this name is overwritten.
82
* @param name Name of attribute
83
* @param className Name of class to invoke
85
static public void addName( String name, String className ) {
86
_nameTable.put( name, className );
87
_allTable.put( className, name.trim().toLowerCase() );
91
* Register a class to invoke for editing a particular attribute syntax
92
* Any previous registration for this syntax is overwritten.
94
* @param syntax Syntax, as defined in LDAPAttributeSchema
95
* @param className Name of class to invoke
97
static public void addSyntax( int syntax, String className ) {
98
_typeTable.put( new Integer(syntax), className );
99
_allTable.put( className, new Integer(syntax) );
103
* Register a class which will be asked to register any editors
106
* @param className Name of class to invoke
108
static public void addClass( String className ) {
109
/* Instantiate the class to let it register its editors */
112
editor = Class.forName( className );
113
IAttributeEditor ed = (IAttributeEditor)editor.newInstance();
114
ed.registerEditors();
115
} catch ( Exception e ) {
116
Debug.println( "AttributeEditorFactory.addClass: no " +
117
"class for " + className + ", " + e );
121
static private void removeClassFromTable( String className, Hashtable h ) {
122
Enumeration en = h.keys();
123
while( en.hasMoreElements() ) {
124
String key = (String)en.nextElement();
125
Class c = (Class)h.get( key );
126
if ( c.getName().equals( className ) ) {
133
* Unregister a class which was previously registered as an editor
135
* @param className Name of class to unregister
137
static public void removeClass( String className ) {
138
/* Remove it from the editor table */
139
_editorTable.remove( className );
140
/* Remove it from the name and syntax tables */
141
removeClassFromTable( className, _nameTable );
142
removeClassFromTable( className, _typeTable );
146
* Register a class to be used as the default editor, if there is no
147
* editor registered which matches a particular attribute.
149
* @param className Name of class to invoke
151
static public void setDefaultEditor( String className ) {
152
/* Try to obtain a class reference */
154
_defaultEditor = Class.forName( className );
155
} catch ( ClassNotFoundException e ) {
156
Debug.println( "AttributeEditorFactory.setDefaultEditor: no " +
157
"class for " + className );
162
* Get the class to be used as the default editor, if there is no
163
* editor registered which matches a particular attribute.
165
* @return Name of the class, or null if none
167
static public String getDefaultEditor() {
168
if ( _defaultEditor != null ) {
169
return _defaultEditor.getClass().getName();
176
* Get a class reference for an editor class name
178
* @param className Name of the class
180
* @return Editor class, or null if none can be instantiated
182
static public Class getClassForEditor( String className ) {
183
if ( _editorTable == null ) {
184
_editorTable = new Hashtable();
188
/* Try to obtain a class reference */
190
editor = Class.forName( className );
191
/* Save the class reference for the future */
192
_editorTable.put( className, editor );
193
} catch ( ClassNotFoundException e ) {
195
"AttributeEditorFactory.getClassForEditor: no " +
196
"class for " + className );
202
* Get the class to be used as the editor for a particular attribute.
204
* @param name Name of the attribute
206
* @return Editor class for the attribute, or null if none
208
static public Class getEditorForAttribute( String name ) {
209
if ( _nameTable == null ) {
213
name = name.trim().toLowerCase();
214
/* See if there is a registered class name */
215
String editorName = (String)_nameTable.get( name );
216
if ( editorName != null ) {
217
/* Try to obtain a class reference */
218
editor = getClassForEditor( editorName );
224
* Get the class to be used as the editor for a particular syntax.
226
* @param syntax Identifier of the syntax
228
* @return Editor class for the syntax, or null if none
230
static public Class getEditorForSyntax( int syntax ) {
231
if ( _typeTable == null ) {
236
(String)_typeTable.get( new Integer( syntax ) );
237
if ( editorName != null ) {
238
/* Try to obtain a class reference */
239
editor = getClassForEditor( editorName );
245
* Get all known editor classes
247
* @return Enumeration of all known attribute editor classes
249
static public Enumeration getAttributeEditors() {
250
if ( _allTable == null ) {
253
return _allTable.keys();
257
* Report if this is a known editor
259
* @param name Name of a class
260
* @return true if the name corresponds to a known Editor class
262
static public boolean isEditor( String name ) {
263
if ( _allTable == null ) {
264
Debug.println( "AttributeEditorFactory.isEditor: null _allTable" );
267
return (_allTable.get(name) != null);
271
* Instantiate an editor appropriate for this attribute type
273
* @param name Name of the attribute
274
* @param labelText Text for label.
275
* @param rect Bounding rectangle for label.
276
* @param singleValued <code>true</code> if at most one value is allowed.
277
* @param syntax Syntax, as defined in LDAPAttributeSchema
279
* @return An instance of an approprite editor, or null
281
static public IAttributeEditor makeEditor( String name,
284
boolean singleValued,
286
Class editor = getEditorForAttribute( name );
287
if ( editor == null ) {
288
editor = getEditorForSyntax( syntax );
290
if ( editor == null ) {
291
editor = _defaultEditor;
293
if ( editor != null ) {
295
IAttributeEditor ed = (IAttributeEditor)editor.newInstance();
296
ed.init( name, labelText, rect, singleValued );
298
} catch ( Exception e ) {
299
Debug.println( "AttributeEditorFactory.makeEditor: " +
300
"cannot instantiate " + editor.getName() );
307
static private Hashtable _nameTable = null;
308
static private Hashtable _typeTable = null;
309
static private Hashtable _editorTable = null;
310
static private Hashtable _allTable = null;
311
static private final String _package =
312
"com.netscape.admin.dirserv.attredit";
313
static private final String _defaultEditorName =
314
_package + ".StringAttributeEditor";
315
static private Class _defaultEditor = null;
316
static private final boolean _initialized = init();