KickJava   Java API By Example, From Geeks To Geeks.

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


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

12 package info.magnolia.cms.gui.dialog;
13
14 import info.magnolia.cms.beans.config.ContentRepository;
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ContentHandler;
17 import info.magnolia.cms.core.HierarchyManager;
18 import info.magnolia.cms.core.ItemType;
19
20 import java.util.Iterator JavaDoc;
21
22 import javax.jcr.RepositoryException;
23 import javax.jcr.ValueFactory;
24 import javax.jcr.observation.Event;
25 import javax.jcr.observation.EventIterator;
26 import javax.jcr.observation.EventListener;
27 import javax.jcr.observation.ObservationManager;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.log4j.Logger;
31
32
33 /**
34  * @author Fabrizio Giustina
35  * @version $Revision: $ ($Author: $)
36  */

37 public final class DialogManager {
38
39     /**
40      * Logger.
41      */

42     protected static Logger log = Logger.getLogger(DialogManager.class);
43
44     /**
45      * Config node name: "controls".
46      */

47     private static final String JavaDoc DIALOGCONTROLS_CONFIG_NAME = "controls"; //$NON-NLS-1$
48

49     /**
50      * "/modules/adminInterface/Config".
51      */

52     private static final String JavaDoc ADMIN_CONFIG_NODE_NAME = "/modules/adminInterface/Config"; //$NON-NLS-1$
53

54     /**
55      * Node data name for control class.
56      */

57     private static final String JavaDoc DATA_CONTROL_CLASS = "class"; //$NON-NLS-1$
58

59     /**
60      * Node data name for control name.
61      */

62     private static final String JavaDoc DATA_CONTROL_NAME = "name"; //$NON-NLS-1$
63

64     /**
65      * don't instantiate.
66      */

67     private DialogManager() {
68         // unused
69
}
70
71     /**
72      * Called through the initialization process
73      */

74     public static void init() {
75         load();
76         registerEventListener();
77     }
78
79     /**
80      * Register an event listener: reload configuration when something changes.
81      */

82     private static void registerEventListener() {
83
84         log.info("Registering event listener for Controls"); //$NON-NLS-1$
85

86         try {
87             ObservationManager observationManager = ContentRepository
88                 .getHierarchyManager(ContentRepository.CONFIG)
89                 .getWorkspace()
90                 .getObservationManager();
91
92             observationManager.addEventListener(
93                 new EventListener() {
94
95                     public void onEvent(EventIterator iterator) {
96                         // reload everything
97
reload();
98                     }
99                 },
100                 Event.NODE_ADDED
101                     | Event.NODE_REMOVED
102                     | Event.PROPERTY_ADDED
103                     | Event.PROPERTY_CHANGED
104                     | Event.PROPERTY_REMOVED,
105                 ADMIN_CONFIG_NODE_NAME + "/" + DIALOGCONTROLS_CONFIG_NAME, true, null, null, false); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
106
}
107         catch (RepositoryException e) {
108             log.error("Unable to add event listeners for Controls", e); //$NON-NLS-1$
109
}
110     }
111
112     /**
113      * Called through the initialization process
114      */

115     public static void reload() {
116         load();
117     }
118
119     /**
120      * Loads and caches dialog controls.
121      */

122     public static void load() {
123         log.info("Config : loading dialog controls configuration"); //$NON-NLS-1$
124

125         // reading the configuration from the repository
126
HierarchyManager configHierarchyManager = ContentRepository.getHierarchyManager(ContentRepository.CONFIG);
127
128         try {
129             Content serverNode = configHierarchyManager.getContent(ADMIN_CONFIG_NODE_NAME);
130
131             Content configNode;
132             try {
133                 configNode = serverNode.getContent(DIALOGCONTROLS_CONFIG_NAME);
134             }
135             catch (javax.jcr.PathNotFoundException e) {
136
137                 log.info("Initialize default configuration for dialog controls"); //$NON-NLS-1$
138

139                 configNode = serverNode.createContent(DIALOGCONTROLS_CONFIG_NAME, ItemType.CONTENT);
140
141                 ValueFactory valueFactory = configHierarchyManager.getWorkspace().getSession().getValueFactory();
142
143                 // sample fckedit dialog
144
Content fckedit = configNode.createContent("fckEdit", ItemType.CONTENTNODE); //$NON-NLS-1$
145
fckedit.createNodeData(DATA_CONTROL_NAME, valueFactory.createValue("fckEdit")); //$NON-NLS-1$
146
fckedit.createNodeData(DATA_CONTROL_CLASS, valueFactory.createValue(DialogFckEdit.class.getName()));
147                 configHierarchyManager.save();
148             }
149
150             if (configNode == null) {
151                 return;
152             }
153
154             Iterator JavaDoc iterator = configNode
155                 .getChildren(ItemType.CONTENTNODE, ContentHandler.SORT_BY_SEQUENCE)
156                 .iterator();
157
158             while (iterator.hasNext()) {
159                 Content controlNode = (Content) iterator.next();
160
161                 if (log.isDebugEnabled()) {
162                     log.debug("Initializing control [" + controlNode + "]"); //$NON-NLS-1$ //$NON-NLS-2$
163
}
164
165                 String JavaDoc classNodeData = controlNode.getNodeData(DATA_CONTROL_CLASS).getString();
166                 String JavaDoc nameNodeData = controlNode.getNodeData(DATA_CONTROL_NAME).getString();
167
168                 if (StringUtils.isEmpty(classNodeData) || StringUtils.isEmpty(nameNodeData)) {
169                     log.warn("Config : Can't add custom control with name [" //$NON-NLS-1$
170
+ nameNodeData + "] and class [" //$NON-NLS-1$
171
+ classNodeData + "] specified in node [" //$NON-NLS-1$
172
+ controlNode.getName() + "]"); //$NON-NLS-1$
173

174                     continue;
175                 }
176                 Class JavaDoc controlClass = null;
177
178                 try {
179                     controlClass = Class.forName(classNodeData);
180                 }
181                 catch (ClassNotFoundException JavaDoc e) {
182                     log.error("Config : Failed to load dialog control with class [" + classNodeData, e); //$NON-NLS-1$
183
continue;
184                 }
185
186                 if (!DialogInterface.class.isAssignableFrom(controlClass)) {
187                     log.error("Config : Invalid class specified for control [" //$NON-NLS-1$
188
+ nameNodeData + "]: does not implement DialogInterface"); //$NON-NLS-1$
189
continue;
190                 }
191
192                 DialogFactory.registerDialog(nameNodeData, controlClass);
193
194             }
195
196         }
197         catch (RepositoryException e) {
198             log.error("Config : Failed to load dialog controls configuration - " //$NON-NLS-1$
199
+ ADMIN_CONFIG_NODE_NAME + "/" //$NON-NLS-1$
200
+ DIALOGCONTROLS_CONFIG_NAME, e);
201         }
202
203     }
204
205 }
206
Popular Tags