KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > editors > CmsEditorDisplayOptions


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsEditorDisplayOptions.java,v $
3  * Date : $Date: 2006/03/27 14:52:49 $
4  * Version: $Revision: 1.12 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.editors;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResourceFilter;
37 import org.opencms.jsp.CmsJspActionElement;
38 import org.opencms.jsp.CmsJspNavBuilder;
39 import org.opencms.jsp.CmsJspNavElement;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsLog;
42
43 import java.io.ByteArrayInputStream JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.Map JavaDoc;
48 import java.util.Properties JavaDoc;
49
50 import org.apache.commons.collections.map.LRUMap;
51 import org.apache.commons.logging.Log;
52
53 /**
54  * Provides methods to determine the display options of a workplace editor for the current user.<p>
55  *
56  * On the editor JSP, do the following:
57  * <ul>
58  * <li>get the object instance with <code>OpenCms.getWorkplaceManager().getEditorDisplayOptions()</code>.</li>
59  * <li>get the Properties for the current user with <code>getDisplayOptions(CmsJspActionElement)</code>.</li>
60  * <li>use <code>showElement(key, Properties)</code> to determine if an element is shown.</li>
61  * </ul>
62  *
63  * Define your editor display options in property files located in the VFS folder
64  * /system/workplace/editors/configuration/.<br> *
65  * Set navigation position property values on the configuration files
66  * and use the permission system to determine which groups and users
67  * should use which configuration file.<br>
68  * The configuration with the most enabled options should be the first in navigation,
69  * followed by configurations with less enabled options, because
70  * the first file readable for the current user will be used for configuration.<p>
71  *
72  * If no configuration file can be found for the current user,
73  * all display options will be disabled by default.<p>
74  *
75  * @author Andreas Zahner
76  *
77  * @version $Revision: 1.12 $
78  *
79  * @since 6.0.0
80  */

81 public class CmsEditorDisplayOptions {
82
83     /** The name of the configuration folder.<p> */
84     public static final String JavaDoc FOLDER_EDITORCONFIGURATION = CmsEditor.PATH_EDITORS + "configuration/";
85
86     /** Mapping entry name that is used if no mapping is available for the user.<p> */
87     public static final String JavaDoc NO_MAPPING_FOR_USER = "na";
88
89     /** Maximum size of the stored editor configurations.<p> */
90     public static final int SIZE_CONFIGURATIONFILES = 12;
91
92     /** Maximum size of the user editor configuration mappings.<p> */
93     public static final int SIZE_USERENTRIES = 100;
94
95     /** The log object for this class. */
96     private static final Log LOG = CmsLog.getLog(CmsEditorDisplayOptions.class);
97
98     /** Stores all loaded editor configuration options.<p> */
99     private Map JavaDoc m_loadedConfigurations;
100
101     /** Stores the mappings of users to their configuration options to use.<p> */
102     private Map JavaDoc m_userMappings;
103
104     /**
105      * Constructor that initializes the editor display options for the workplace.<p>
106      */

107     public CmsEditorDisplayOptions() {
108
109         // initialize members
110
m_userMappings = new LRUMap(SIZE_USERENTRIES);
111         m_loadedConfigurations = new LRUMap(SIZE_CONFIGURATIONFILES);
112     }
113
114     /**
115      * Reads the editor configuration file valid for the current user and caches the result in a Map.<p>
116      *
117      * The configuration settings of the found file are stored in a Map holding the loaded configuration
118      * with the configuration file name as key.<p>
119      *
120      * The configuration file name to use for the current user is stored in another Map with the user name
121      * as key.<p>
122      *
123      * @param jsp the JSP action element to access the VFS and current user information
124      * @return the display options to use for the current user or null if no display options were found
125      */

126     public Properties JavaDoc getDisplayOptions(CmsJspActionElement jsp) {
127
128         return getDisplayOptions(jsp.getCmsObject());
129     }
130
131     /**
132      * Reads the editor configuration file valid for the current user and caches the result in a Map.<p>
133      *
134      * The configuration settings of the found file are stored in a Map holding the loaded configuration
135      * with the configuration file name as key.<p>
136      *
137      * The configuration file name to use for the current user is stored in another Map with the user name
138      * as key.<p>
139      *
140      * @param cms the CmsObject to access the VFS and current user information
141      * @return the display options to use for the current user or null if no display options were found
142      */

143     public Properties JavaDoc getDisplayOptions(CmsObject cms) {
144
145         // get the configuration file name for the current user
146
String JavaDoc mappedConfigFile = (String JavaDoc)m_userMappings.get(cms.getRequestContext().currentUser().getName());
147         Properties JavaDoc displayOptions;
148         if (mappedConfigFile == null) {
149             // no configuration file name stored for user, get the navigation items of the configuration folder
150
List JavaDoc items = CmsJspNavBuilder.getNavigationForFolder(cms, FOLDER_EDITORCONFIGURATION);
151             if (items.size() > 0) {
152                 // get first found configuration file
153
CmsJspNavElement nav = (CmsJspNavElement)items.get(0);
154                 mappedConfigFile = nav.getFileName();
155                 synchronized (m_loadedConfigurations) {
156                     // must sync read/write access to shared map
157
displayOptions = (Properties JavaDoc)m_loadedConfigurations.get(nav.getFileName());
158                     if (displayOptions == null) {
159                         // configuration file has not yet been loaded, load it
160
try {
161                             // read configuration file
162
CmsFile optionFile = cms.readFile(
163                                 nav.getResourceName(),
164                                 CmsResourceFilter.IGNORE_EXPIRATION);
165                             InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(optionFile.getContents());
166                             displayOptions = new Properties JavaDoc();
167                             displayOptions.load(in);
168                             // store loaded options
169
m_loadedConfigurations.put(nav.getFileName(), displayOptions);
170                         } catch (CmsException e) {
171                             // set configuration to not available
172
if (LOG.isInfoEnabled()) {
173                                 LOG.info(e);
174                             }
175                             mappedConfigFile = NO_MAPPING_FOR_USER;
176                             displayOptions = null;
177                         } catch (IOException JavaDoc e) {
178                             // set configuration to not available
179
if (LOG.isInfoEnabled()) {
180                                 LOG.info(e);
181                             }
182                             mappedConfigFile = NO_MAPPING_FOR_USER;
183                             displayOptions = null;
184                         }
185                     }
186                 }
187             } else {
188                 // no configuration available for current user, store this in mapping
189
mappedConfigFile = NO_MAPPING_FOR_USER;
190                 displayOptions = null;
191             }
192             if (LOG.isDebugEnabled()) {
193                 // check which mapping has been stored
194
LOG.debug(Messages.get().getBundle().key(
195                     Messages.LOG_MAP_CONFIG_FILE_TO_USER_2,
196                     mappedConfigFile,
197                     cms.getRequestContext().currentUser().getName()));
198             }
199             // store the file name of the configuration file for the current user
200
m_userMappings.put(cms.getRequestContext().currentUser().getName(), mappedConfigFile);
201         } else {
202             // configuration file for current user is known, get options from loaded configurations
203
displayOptions = (Properties JavaDoc)m_loadedConfigurations.get(mappedConfigFile);
204         }
205         // return the editor display options for this user
206
return displayOptions;
207     }
208
209     /**
210      * Determines if the given element should be shown in the editor.<p>
211      *
212      * @param key the element key name which should be displayed
213      * @param displayOptions the display options for the current user
214      * @return true if the element should be shown, otherwise false
215      */

216     public boolean showElement(String JavaDoc key, Properties JavaDoc displayOptions) {
217
218         return (displayOptions != null && Boolean.valueOf(displayOptions.getProperty(key)).booleanValue());
219     }
220
221 }
222
Popular Tags