~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/attredit/AttributeEditorFactory.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
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.
 
9
 * 
 
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.
 
14
 * 
 
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 **/
 
19
 
 
20
package com.netscape.admin.dirserv.attredit;
 
21
 
 
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;
 
27
 
 
28
/**
 
29
  * A factory for producing an attribute editor for a particular attribute
 
30
  *
 
31
  * @author  rweltman
 
32
  * @version 1.1, 02/18/00
 
33
  * @date        5/24/98
 
34
  */
 
35
public class AttributeEditorFactory {
 
36
 
 
37
        /**
 
38
         * Set up the built-in associations
 
39
         */
 
40
        static private boolean init() {
 
41
                _nameTable = new Hashtable();
 
42
                _typeTable = new Hashtable();
 
43
                _editorTable = new Hashtable();
 
44
                _allTable = new Hashtable();
 
45
 
 
46
                /* Set up the built-in preferences */
 
47
                addName( "objectclass",
 
48
                                 _package + ".ObjectClassAttributeEditor" );
 
49
                addName( "jpegphoto", 
 
50
                                 _package + ".ImageAttributeEditor" );
 
51
                addName( "userpassword",
 
52
                                 _package + ".PasswordAttributeEditor" );
 
53
                addName( "usercertificate", 
 
54
                                 _package + ".BinaryAttributeEditor" );
 
55
 
 
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" );
 
73
 
 
74
                setDefaultEditor( _defaultEditorName );
 
75
                return true;
 
76
        }
 
77
 
 
78
        /**
 
79
         * Register a class to invoke for editing a particular attribute.
 
80
         * Any previous registration for this name is overwritten.
 
81
         *
 
82
         * @param name Name of attribute
 
83
         * @param className Name of class to invoke
 
84
         */
 
85
    static public void addName( String name, String className ) {
 
86
                _nameTable.put( name, className );
 
87
                _allTable.put( className, name.trim().toLowerCase() );
 
88
        }
 
89
 
 
90
        /**
 
91
         * Register a class to invoke for editing a particular attribute syntax
 
92
         * Any previous registration for this syntax is overwritten.
 
93
         *
 
94
         * @param syntax Syntax, as defined in LDAPAttributeSchema
 
95
         * @param className Name of class to invoke
 
96
         */
 
97
    static public void addSyntax( int syntax, String className ) {
 
98
                _typeTable.put( new Integer(syntax), className );
 
99
                _allTable.put( className, new Integer(syntax) );
 
100
        }
 
101
 
 
102
        /**
 
103
         * Register a class which will be asked to register any editors
 
104
         * it contains.
 
105
         *
 
106
         * @param className Name of class to invoke
 
107
         */
 
108
    static public void addClass( String className ) {
 
109
                /* Instantiate the class to let it register its editors */
 
110
                Class editor;
 
111
                try {
 
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 );
 
118
                }
 
119
        }
 
120
 
 
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 ) ) {
 
127
                                h.remove( key );
 
128
                        }
 
129
                }
 
130
        }
 
131
 
 
132
        /**
 
133
         * Unregister a class which was previously registered as an editor
 
134
         *
 
135
         * @param className Name of class to unregister
 
136
         */
 
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 );
 
143
        }
 
144
 
 
145
        /**
 
146
         * Register a class to be used as the default editor, if there is no
 
147
         * editor registered which matches a particular attribute.
 
148
         *
 
149
         * @param className Name of class to invoke
 
150
         */
 
151
    static public void setDefaultEditor( String className ) {
 
152
                /* Try to obtain a class reference */
 
153
                try {
 
154
                        _defaultEditor = Class.forName( className );
 
155
                } catch ( ClassNotFoundException e ) {
 
156
                        Debug.println( "AttributeEditorFactory.setDefaultEditor: no " +
 
157
                                                                   "class for " + className );
 
158
                }
 
159
        }
 
160
 
 
161
        /**
 
162
         * Get the class to be used as the default editor, if there is no
 
163
         * editor registered which matches a particular attribute.
 
164
         *
 
165
         * @return Name of the class, or null if none
 
166
         */
 
167
    static public String getDefaultEditor() {
 
168
                if ( _defaultEditor != null ) {
 
169
                        return _defaultEditor.getClass().getName();
 
170
                } else {
 
171
                        return null;
 
172
                }
 
173
        }
 
174
 
 
175
        /**
 
176
         * Get a class reference for an editor class name
 
177
         *
 
178
         * @param className Name of the class
 
179
         *
 
180
         * @return Editor class, or null if none can be instantiated
 
181
         */
 
182
    static public Class getClassForEditor( String className ) {
 
183
                if ( _editorTable == null ) {
 
184
                        _editorTable = new Hashtable();
 
185
                }
 
186
 
 
187
                Class editor = null;
 
188
                /* Try to obtain a class reference */
 
189
                try {
 
190
                        editor = Class.forName( className );
 
191
                        /* Save the class reference for the future */
 
192
                        _editorTable.put( className, editor );
 
193
                } catch ( ClassNotFoundException e ) {
 
194
                        Debug.println(
 
195
                                "AttributeEditorFactory.getClassForEditor: no " +
 
196
                                "class for " + className );
 
197
                }
 
198
                return editor;
 
199
        }
 
200
 
 
201
        /**
 
202
         * Get the class to be used as the editor for a particular attribute.
 
203
         *
 
204
         * @param name Name of the attribute
 
205
         *
 
206
         * @return Editor class for the attribute, or null if none
 
207
         */
 
208
    static public Class getEditorForAttribute( String name ) {
 
209
                if ( _nameTable == null ) {
 
210
                        return null;
 
211
                }
 
212
                Class editor = 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 );
 
219
                }
 
220
                return editor;
 
221
        }
 
222
 
 
223
        /**
 
224
         * Get the class to be used as the editor for a particular syntax.
 
225
         *
 
226
         * @param syntax Identifier of the syntax
 
227
         *
 
228
         * @return Editor class for the syntax, or null if none
 
229
         */
 
230
    static public Class getEditorForSyntax( int syntax ) {
 
231
                if ( _typeTable == null ) {
 
232
                        return null;
 
233
                }
 
234
                Class editor = null;
 
235
                String editorName =
 
236
                        (String)_typeTable.get( new Integer( syntax ) );
 
237
                if ( editorName != null ) {
 
238
                        /* Try to obtain a class reference */
 
239
                        editor = getClassForEditor( editorName );
 
240
                }
 
241
                return editor;
 
242
        }
 
243
 
 
244
        /**
 
245
         * Get all known editor classes
 
246
         *
 
247
         * @return Enumeration of all known attribute editor classes
 
248
         */
 
249
    static public Enumeration getAttributeEditors() {
 
250
                if ( _allTable == null ) {
 
251
                        return null;
 
252
                }
 
253
                return _allTable.keys();
 
254
        }
 
255
 
 
256
        /**
 
257
         * Report if this is a known editor
 
258
         *
 
259
         * @param name Name of a class
 
260
         * @return true if the name corresponds to a known Editor class
 
261
         */
 
262
    static public boolean isEditor( String name ) {
 
263
                if ( _allTable == null ) {
 
264
                        Debug.println( "AttributeEditorFactory.isEditor: null _allTable" );
 
265
                        return false;
 
266
                }
 
267
                return (_allTable.get(name) != null);
 
268
        }
 
269
 
 
270
        /**
 
271
         * Instantiate an editor appropriate for this attribute type
 
272
         *
 
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
 
278
         *
 
279
         * @return An instance of an approprite editor, or null
 
280
         */
 
281
    static public IAttributeEditor makeEditor( String name,
 
282
                                                                                           String labelText,
 
283
                                                                                           Rectangle rect,
 
284
                                                                                           boolean singleValued,
 
285
                                                                                           int syntax ) {
 
286
                Class editor = getEditorForAttribute( name );
 
287
                if ( editor == null ) {
 
288
                        editor = getEditorForSyntax( syntax );
 
289
                }
 
290
                if ( editor == null ) {
 
291
                        editor = _defaultEditor;
 
292
                }
 
293
                if ( editor != null ) {
 
294
                        try {
 
295
                                IAttributeEditor ed = (IAttributeEditor)editor.newInstance();
 
296
                                ed.init( name, labelText, rect, singleValued );
 
297
                                return ed;
 
298
                        } catch ( Exception e ) {
 
299
                                Debug.println( "AttributeEditorFactory.makeEditor: " +
 
300
                                                           "cannot instantiate " + editor.getName() );
 
301
                        }
 
302
                }
 
303
 
 
304
                return null;
 
305
        }
 
306
 
 
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();
 
317
}