KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > CmsWorkplaceMessages


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/CmsWorkplaceMessages.java,v $
3  * Date : $Date: 2006/07/20 13:46:39 $
4  * Version: $Revision: 1.41 $
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;
33
34 import org.opencms.i18n.A_CmsMessageBundle;
35 import org.opencms.i18n.CmsMessages;
36 import org.opencms.i18n.CmsMultiMessages;
37 import org.opencms.i18n.I_CmsMessageBundle;
38 import org.opencms.main.OpenCms;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Locale JavaDoc;
44 import java.util.Set JavaDoc;
45
46 /**
47  * Provides access to the localized messages for the OpenCms workplace.<p>
48  *
49  * The workplace messages are collected from the workplace resource bundles of all installed modules,
50  * plus all the OpenCms core packages.<p>
51  *
52  * To be recognized as a workplace module resource bundle,
53  * the workplace property file must follow the naming convention <code>${module_package_name}.workplace${locale}.properties</code>,
54  * or <code>${module_package_name}.messages${locale}.properties</code>
55  * for example like <code>com.mycompany.module.workplace_en.properties</code> or
56  * <code>com.mycompany.module.messages_en.properties</code>.<p>
57  *
58  * Workplace messages are cached for faster lookup. If a localized key is contained in more then one module,
59  * it will be used only from the module where it was first found in. The module order is undefined. It is therefore
60  * recommended to ensure the uniqueness of all module keys by placing a special prefix in front of all keys of a module.<p>
61  *
62  * @author Alexander Kandzior
63  *
64  * @version $Revision: 1.41 $
65  *
66  * @since 6.0.0
67  */

68 public class CmsWorkplaceMessages extends CmsMultiMessages {
69
70     /** The title key prefix used for the "new resource" dialog. */
71     public static final String JavaDoc GUI_NEW_RESOURCE_TITLE_PREFIX = "title.new";
72
73     /** The prefix to generate the resource type names with. */
74     public static final String JavaDoc GUI_RESOURCE_TYPE_PREFIX = "fileicon.";
75
76     /** Constant for the <code>".messages"</code> prefix. */
77     public static final String JavaDoc PREFIX_BUNDLE_MESSAGES = ".messages";
78
79     /** Constant for the <code>".workplace"</code> prefix. */
80     public static final String JavaDoc PREFIX_BUNDLE_WORKPLACE = ".workplace";
81
82     /** Constant for the multi bundle name. */
83     public static final String JavaDoc WORKPLACE_BUNDLE_NAME = CmsWorkplaceMessages.class.getName();
84
85     /**
86      * Constructor for creating a new messages object
87      * initialized with the provided locale.<p>
88      *
89      * @param locale the locale to initialize
90      */

91     public CmsWorkplaceMessages(Locale JavaDoc locale) {
92
93         super(locale);
94         setBundleName(WORKPLACE_BUNDLE_NAME);
95         addMessages(collectModuleMessages(locale));
96     }
97
98     /**
99      * Returns the title for the "new resource" dialog.<p>
100      *
101      * It will look up a key with the prefix {@link #GUI_NEW_RESOURCE_TITLE_PREFIX}
102      * and the given name appended (converted to lower case).<p>
103      *
104      * If this key is not found, the value of
105      * {@link org.opencms.workplace.explorer.Messages#GUI_TITLE_NEWFILEOTHER_0} will be returned.<p>
106      *
107      * @param wp an instance of a {@link CmsWorkplace} to resolve the key name with
108      * @param name the type to generate the title for
109      *
110      * @return the title for the "new resource" dialog
111      */

112     public static String JavaDoc getNewResourceTitle(CmsWorkplace wp, String JavaDoc name) {
113
114         // try to find the localized key
115
String JavaDoc title = wp.key(GUI_NEW_RESOURCE_TITLE_PREFIX + name.toLowerCase());
116         if (CmsMessages.isUnknownKey(title)) {
117             // still unknown - use default title
118
title = wp.key(org.opencms.workplace.explorer.Messages.GUI_TITLE_NEWFILEOTHER_0);
119         }
120         return title;
121     }
122
123     /**
124      * Returns the nice name of the given resource type name.<p>
125      *
126      * It will look up a key with the prefix {@link #GUI_RESOURCE_TYPE_PREFIX}
127      * and the given name appended.<p>
128      *
129      * If this key is not found, the value of
130      * the name input will be returned.<p>
131      *
132      * @param wp an instance of a {@link CmsWorkplace} to resolve the key name with
133      * @param name the resource type name to generate the nice name for
134      *
135      * @return the nice name of the given resource type name
136      */

137     public static String JavaDoc getResourceName(CmsWorkplace wp, String JavaDoc name) {
138
139         // try to find the localized key
140
return wp.keyDefault(GUI_RESOURCE_TYPE_PREFIX + name, name);
141     }
142
143     /**
144      * Gathers all localization files for the workplace from the different modules.<p>
145      *
146      * For a module named "my.module.name" the locale file must be named
147      * "my.module.name.workplace" and be located in the classpath so that the resource loader
148      * can find it.<p>
149      *
150      * @param locale the selected locale
151      *
152      * @return an initialized set of module messages
153      */

154     private static List JavaDoc collectModuleMessages(Locale JavaDoc locale) {
155
156         // create a new list and add the base bundle
157
ArrayList JavaDoc result = new ArrayList JavaDoc();
158
159         //////////// iterate over all registered modules ////////////////
160
Set JavaDoc names = OpenCms.getModuleManager().getModuleNames();
161         if (names != null) {
162             // iterate all module names
163
Iterator JavaDoc i = names.iterator();
164             while (i.hasNext()) {
165                 String JavaDoc modName = (String JavaDoc)i.next();
166                 //////////// collect the workplace.properties ////////////////
167
// this should result in a name like "my.module.name.workplace"
168
String JavaDoc bundleName = modName + PREFIX_BUNDLE_WORKPLACE;
169                 // try to load a bundle with the module names
170
CmsMessages msg = new CmsMessages(bundleName, locale);
171                 // bundle was loaded, add to list of bundles
172
if (msg.isInitialized()) {
173                     result.add(msg);
174                 }
175                 //////////// collect the messages.properties ////////////////
176
// this should result in a name like "my.module.name.messages"
177
bundleName = modName + PREFIX_BUNDLE_MESSAGES;
178                 // try to load a bundle with the module names
179
msg = new CmsMessages(bundleName, locale);
180                 // bundle was loaded, add to list of bundles
181
if (msg.isInitialized()) {
182                     result.add(msg);
183                 }
184             }
185         }
186
187         //////////// collect additional core packages ////////////////
188
I_CmsMessageBundle[] coreMsgs = A_CmsMessageBundle.getOpenCmsMessageBundles();
189         for (int i = 0; i < coreMsgs.length; i++) {
190             I_CmsMessageBundle bundle = coreMsgs[i];
191             result.add(bundle.getBundle(locale));
192         }
193
194         return result;
195     }
196
197     /**
198      * @see java.lang.Object#equals(java.lang.Object)
199      */

200     public boolean equals(Object JavaDoc obj) {
201
202         if (obj == this) {
203             return true;
204         }
205         if (obj instanceof CmsWorkplaceMessages) {
206             // workplace messages are equal if the locale is equal (since all bundles are the same)
207
CmsMessages other = (CmsMessages)obj;
208             return other.getLocale().equals(getLocale());
209         }
210         return false;
211     }
212
213     /**
214      * @see org.opencms.i18n.CmsMessages#hashCode()
215      */

216     public int hashCode() {
217
218         return getLocale().hashCode();
219     }
220 }
Popular Tags