KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > PersistenceObjectRegistry


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
21 package org.netbeans.modules.form;
22
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import org.openide.util.Utilities;
26 import org.openide.filesystems.FileObject;
27
28 /**
29  *
30  * @author Tran Duc Trung
31  */

32
33 public class PersistenceObjectRegistry
34 {
35
36     private static Map JavaDoc _nameToClassname = new HashMap JavaDoc();
37     private static Map JavaDoc _classToPrimaryName = new HashMap JavaDoc();
38
39     private PersistenceObjectRegistry() {
40     }
41
42     public static void registerPrimaryName(String JavaDoc classname, String JavaDoc name) {
43         _classToPrimaryName.put(classname, name);
44         _nameToClassname.put(name, classname);
45     }
46
47     public static void registerPrimaryName(Class JavaDoc clazz, String JavaDoc name) {
48         _classToPrimaryName.put(clazz.getName(), name);
49         _nameToClassname.put(name, clazz.getName());
50     }
51
52     public static void registerAlias(String JavaDoc classname, String JavaDoc alias) {
53         _nameToClassname.put(alias, classname);
54     }
55
56     public static void registerAlias(Class JavaDoc clazz, String JavaDoc alias) {
57         _nameToClassname.put(alias, clazz.getName());
58     }
59
60     public static Object JavaDoc createInstance(String JavaDoc classname, FileObject form)
61         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, ClassNotFoundException JavaDoc
62     {
63         return loadClass(classname, form).newInstance();
64     }
65
66     public static Class JavaDoc loadClass(String JavaDoc name, FileObject form)
67         throws ClassNotFoundException JavaDoc
68     {
69         name = Utilities.translate(name);
70         String JavaDoc classname =(String JavaDoc) _nameToClassname.get(name);
71         if (classname == null)
72             classname = name;
73         return FormUtils.loadClass(classname, form);
74     }
75
76     public static String JavaDoc getPrimaryName(Object JavaDoc instance) {
77         return getPrimaryName(instance.getClass());
78     }
79
80     public static String JavaDoc getPrimaryName(Class JavaDoc clazz) {
81         return getPrimaryName(clazz.getName());
82     }
83
84     static String JavaDoc getPrimaryName(String JavaDoc className) {
85         String JavaDoc name = (String JavaDoc) _classToPrimaryName.get(className);
86         return name != null ? name : className;
87     }
88
89     static String JavaDoc getClassName(String JavaDoc primaryName) {
90         primaryName = Utilities.translate(primaryName);
91         String JavaDoc classname = (String JavaDoc) _nameToClassname.get(primaryName);
92         return classname != null ? classname : primaryName;
93     }
94 }
95
Popular Tags