KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > dialog > DialogFactory


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.dialog;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.util.ClassUtil;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.jcr.RepositoryException;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.commons.lang.exception.NestableRuntimeException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30 /**
31  * Factory for dialogs. This class handles the registration of native dialogs mantaing a map of (control name | dialog
32  * class).
33  * @author Fabrizio Giustina
34  * @version $Revision: 6341 $ ($Author: philipp $)
35  */

36 public final class DialogFactory {
37
38     /**
39      * Registered controls.
40      */

41     private static Map JavaDoc controls = new HashMap JavaDoc();
42
43     /**
44      * Logger.
45      */

46     private static Logger log = LoggerFactory.getLogger(DialogFactory.class);
47
48     /**
49      * Utility class, don't instantiate.
50      */

51     private DialogFactory() {
52         // unused
53
}
54
55     /**
56      * Register a new dialog.
57      * @param name dialog name (e.g. "richEdit")
58      * @param dialogClass implementing class. Must implements <code>info.magnolia.cms.gui.dialog.DialogControl</code>
59      * @see info.magnolia.cms.gui.dialog.DialogControl
60      */

61     public static void registerDialog(String JavaDoc name, Class JavaDoc dialogClass) {
62         // @todo check if dialogClass is a valid dialog
63
// @todo synchronize
64

65         if (log.isDebugEnabled()) {
66             log.debug("Registering control [{}]", name); //$NON-NLS-1$
67
}
68
69         controls.put(name, dialogClass);
70     }
71
72     /**
73      * Load and initialize a dialog.
74      * @param storageNode current website node
75      * @param configNode configuration node for the dialog. The type of the dialog is read from the "controlType"
76      * nodeData
77      * @throws RepositoryException for errors during initialization of dialog with repository data
78      */

79     public static DialogControl loadDialog(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
80         Content storageNode, Content configNode) throws RepositoryException {
81         String JavaDoc controlType = configNode.getNodeData("controlType").getString(); //$NON-NLS-1$
82

83         return getDialogControlInstanceByName(request, response, storageNode, configNode, controlType);
84     }
85
86     public static Dialog getDialogInstance(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
87         Content storageNode, Content configNode) throws RepositoryException {
88         Dialog dialog = new Dialog();
89         dialog.init(request, response, storageNode, configNode);
90         return dialog;
91     }
92
93     public static DialogStatic getDialogStaticInstance(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
94         Content storageNode, Content configNode) throws RepositoryException {
95         DialogStatic dialog = new DialogStatic();
96         dialog.init(request, response, storageNode, configNode);
97         return dialog;
98     }
99
100     public static DialogHidden getDialogHiddenInstance(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
101         Content storageNode, Content configNode) throws RepositoryException {
102         DialogHidden dialog = new DialogHidden();
103         dialog.init(request, response, storageNode, configNode);
104         return dialog;
105     }
106
107     public static DialogEdit getDialogEditInstance(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
108         Content storageNode, Content configNode) throws RepositoryException {
109         DialogEdit dialog = new DialogEdit();
110         dialog.init(request, response, storageNode, configNode);
111         return dialog;
112     }
113
114     public static DialogButton getDialogButtonInstance(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
115         Content storageNode, Content configNode) throws RepositoryException {
116         DialogButton dialog = new DialogButton();
117         dialog.init(request, response, storageNode, configNode);
118         return dialog;
119     }
120
121     public static DialogPassword getDialogPasswordInstance(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
122         Content storageNode, Content configNode) throws RepositoryException {
123         DialogPassword dialog = new DialogPassword();
124         dialog.init(request, response, storageNode, configNode);
125         return dialog;
126     }
127
128     public static DialogButtonSet getDialogButtonSetInstance(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
129         Content storageNode, Content configNode) throws RepositoryException {
130         DialogButtonSet dialog = new DialogButtonSet();
131         dialog.init(request, response, storageNode, configNode);
132         return dialog;
133     }
134
135     public static DialogInclude getDialogIncludeInstance(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
136         Content storageNode, Content configNode) throws RepositoryException {
137         DialogInclude dialog = new DialogInclude();
138         dialog.init(request, response, storageNode, configNode);
139         return dialog;
140     }
141
142     public static DialogSelect getDialogSelectInstance(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
143         Content storageNode, Content configNode) throws RepositoryException {
144         DialogSelect dialog = new DialogSelect();
145         dialog.init(request, response, storageNode, configNode);
146         return dialog;
147     }
148
149     /**
150      * Get a instance by the control type name. Those name class mappings are configured in the admin interface
151      * configuration.
152      * @param request
153      * @param response
154      * @param storageNode the node holding the data (can be null)
155      * @param configNode the node holding the configuration (can be null)
156      * @param controlType the name of the control
157      * @return the conrol
158      * @throws RepositoryException
159      */

160     public static DialogControl getDialogControlInstanceByName(HttpServletRequest JavaDoc request,
161         HttpServletResponse JavaDoc response, Content storageNode, Content configNode, String JavaDoc controlType)
162         throws RepositoryException {
163
164         Class JavaDoc dialogClass = (Class JavaDoc) controls.get(controlType);
165
166         if (dialogClass == null) {
167             try {
168                 dialogClass = ClassUtil.classForName(controlType);
169             }
170             catch (ClassNotFoundException JavaDoc e) {
171                 throw new IllegalArgumentException JavaDoc("Unknown control type: \"" + controlType + "\""); //$NON-NLS-1$ //$NON-NLS-2$
172
}
173         }
174
175         DialogControl control = null;
176         try {
177             control = (DialogControl) dialogClass.newInstance();
178         }
179         catch (Exception JavaDoc e) {
180             // should never happen
181
throw new NestableRuntimeException("Unable to instantiate " //$NON-NLS-1$
182
+ dialogClass
183                 + " due to: InstantiationException - " //$NON-NLS-1$
184
+ e.getMessage());
185         }
186
187         control.init(request, response, storageNode, configNode);
188         return control;
189     }
190 }
191
Popular Tags