KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/CmsFrameset.java,v $
3  * Date : $Date: 2006/07/21 12:01:54 $
4  * Version: $Revision: 1.87 $
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.file.CmsGroup;
35 import org.opencms.file.CmsProject;
36 import org.opencms.file.CmsResourceFilter;
37 import org.opencms.i18n.CmsEncoder;
38 import org.opencms.jsp.CmsJspActionElement;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.CmsLog;
41 import org.opencms.main.OpenCms;
42 import org.opencms.site.CmsSite;
43 import org.opencms.site.CmsSiteManager;
44 import org.opencms.synchronize.CmsSynchronizeSettings;
45 import org.opencms.util.CmsRequestUtil;
46 import org.opencms.util.CmsStringUtil;
47
48 import java.util.ArrayList JavaDoc;
49 import java.util.Arrays JavaDoc;
50 import java.util.Collections JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Vector JavaDoc;
54
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56
57 import org.apache.commons.logging.Log;
58
59 /**
60  * Provides methods for building the main framesets of the OpenCms Workplace.<p>
61  *
62  * The following files use this class:
63  * <ul>
64  * <li>/views/top.html
65  * <li>/views/top_foot.html
66  * <li>/views/top_head.html
67  * </ul>
68  * <p>
69  *
70  * @author Alexander Kandzior
71  *
72  * @version $Revision: 1.87 $
73  *
74  * @since 6.0.0
75  */

76 public class CmsFrameset extends CmsWorkplace {
77
78     /** The names of the supported frames. */
79     public static final String JavaDoc[] FRAMES = {"top", "head", "body", "foot"};
80
81     /** The names of the supported frames in a list. */
82     public static final List JavaDoc FRAMES_LIST = Arrays.asList(FRAMES);
83
84     /** Path to the JSP workplace frame loader file. */
85     public static final String JavaDoc JSP_WORKPLACE_URI = CmsWorkplace.VFS_PATH_WORKPLACE + "views/workplace.jsp";
86
87     /** The request parameter for the workplace start selection. */
88     public static final String JavaDoc PARAM_WP_START = "wpStart";
89
90     /** The request parameter for the workplace view selection. */
91     public static final String JavaDoc PARAM_WP_VIEW = "wpView";
92     
93     /** Publish button appearance: show always. */
94     public static final String JavaDoc PUBLISHBUTTON_SHOW_ALWAYS = "always";
95     
96     /** Publish button appearance: show auto (only if user has publish permissions). */
97     public static final String JavaDoc PUBLISHBUTTON_SHOW_AUTO = "auto";
98     
99     /** Publish button appearance: show never. */
100     public static final String JavaDoc PUBLISHBUTTON_SHOW_NEVER = "never";
101
102     /** The log object for this class. */
103     private static final Log LOG = CmsLog.getLog(CmsFrameset.class);
104
105     /** Indicates if a reload of the main body frame is required. */
106     private boolean m_reloadRequired;
107
108     /** The request parameter for the selection of the frame. */
109     public static final String JavaDoc PARAM_WP_FRAME = "wpFrame";
110
111     /**
112      * Public constructor.<p>
113      *
114      * @param jsp an initialized JSP action element
115      */

116     public CmsFrameset(CmsJspActionElement jsp) {
117
118         super(jsp);
119     }
120
121     /**
122      * Returns the javascript code for the broadcast message alert in the foot of the workplace.<p>
123      *
124      * @return javascript code showing an alert box when the foot load
125      */

126     public String JavaDoc getBroadcastMessage() {
127
128         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
129         String JavaDoc message = getBroadcastMessageString();
130
131         if (CmsStringUtil.isNotEmpty(message)) {
132             // create a javascript alert for the message
133
result.append("\n<script type=\"text/javascript\">\n<!--\n");
134             // the timeout gives the frameset enough time to load before the alert is shown
135
result.append("function showMessage() {\n");
136             result.append("\talert(decodeURIComponent(\"");
137             // the user has pending messages, display them all
138
result.append(CmsEncoder.escapeWBlanks(message, CmsEncoder.ENCODING_UTF_8));
139             result.append("\"));\n}\n");
140             result.append("setTimeout('showMessage();', 2000);");
141             result.append("\n//-->\n</script>");
142         }
143         return result.toString();
144     }
145
146     /**
147      * Returns a html select box filled with groups of the current user.<p>
148      *
149      * @param htmlAttributes attributes that will be inserted into the generated html
150      * @return a html select box filled with groups of the current user
151      */

152     public String JavaDoc getGroupSelect(String JavaDoc htmlAttributes) {
153
154         // get the users groups from the request context
155
List JavaDoc allGroups = new Vector JavaDoc();
156         try {
157             allGroups = getCms().getGroupsOfUser(getSettings().getUser().getName());
158         } catch (CmsException e) {
159             // should usually never happen
160
if (LOG.isInfoEnabled()) {
161                 LOG.info(e.getLocalizedMessage());
162             }
163         }
164
165         List JavaDoc options = new ArrayList JavaDoc();
166         List JavaDoc values = new ArrayList JavaDoc();
167
168         // loop through all groups and build the result vectors
169
int numGroups = allGroups.size();
170         for (int i = 0; i < numGroups; i++) {
171             CmsGroup loopGroup = (CmsGroup)allGroups.get(i);
172             String JavaDoc loopGroupName = loopGroup.getName();
173             values.add(loopGroupName);
174             options.add(loopGroupName);
175         }
176
177         return buildSelect(htmlAttributes, options, values, 0);
178     }
179
180     /**
181      * Returns the remote ip address of the current user.<p>
182      *
183      * @return the remote ip address of the current user
184      */

185     public String JavaDoc getLoginAddress() {
186
187         return getCms().getRequestContext().getRemoteAddress();
188     }
189
190     /**
191      * Returns the last login time of the current user in localized format.<p>
192      *
193      * @return the last login time of the current user in localized format
194      */

195     public String JavaDoc getLoginTime() {
196
197         return getMessages().getDateTime(getSettings().getUser().getLastlogin());
198     }
199
200     /**
201      * Returns a html select box filled with the current users accessible projects.<p>
202      *
203      * @param htmlAttributes attributes that will be inserted into the generated html
204      * @param htmlWidth additional style attributes containing width information
205      * @return a html select box filled with the current users accessible projects
206      */

207     public String JavaDoc getProjectSelect(String JavaDoc htmlAttributes, String JavaDoc htmlWidth) {
208
209         // get all project information
210
List JavaDoc allProjects;
211         try {
212             allProjects = getCms().getAllAccessibleProjects();
213         } catch (CmsException e) {
214             // should usually never happen
215
if (LOG.isInfoEnabled()) {
216                 LOG.info(e.getLocalizedMessage());
217             }
218             allProjects = Collections.EMPTY_LIST;
219         }
220
221         List JavaDoc options = new ArrayList JavaDoc();
222         List JavaDoc values = new ArrayList JavaDoc();
223         int selectedIndex = 0;
224
225         // now loop through all projects and fill the result vectors
226
for (int i = 0, n = allProjects.size(); i < n; i++) {
227             CmsProject loopProject = (CmsProject)allProjects.get(i);
228             String JavaDoc loopProjectName = loopProject.getName();
229             String JavaDoc loopProjectId = Integer.toString(loopProject.getId());
230
231             values.add(loopProjectId);
232             options.add(loopProjectName);
233
234             if (loopProject.getId() == getSettings().getProject()) {
235                 // this is the user's current project
236
selectedIndex = i;
237             }
238             // check the length of the project name, to optionallly adjust the size of the selector
239
}
240         if (CmsStringUtil.isNotEmpty(htmlWidth)) {
241             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(htmlAttributes.length() + htmlWidth.length() + 2);
242             buf.append(htmlAttributes);
243             buf.append(" ");
244             buf.append(htmlWidth);
245             htmlAttributes = buf.toString();
246         }
247
248         return buildSelect(htmlAttributes, options, values, selectedIndex);
249     }
250
251     /**
252      * Returns the html for the "publish project" button depending on the current users permissions and the default
253      * workplace settings.<p>
254      *
255      * @return the html for the "publish project" button
256      */

257     public String JavaDoc getPublishButton() {
258
259         String JavaDoc publishButton = OpenCms.getWorkplaceManager().getDefaultUserSettings().getPublishButtonAppearance();
260         if (PUBLISHBUTTON_SHOW_NEVER.equals(publishButton)) {
261             return "";
262         }
263
264         int buttonStyle = getSettings().getUserSettings().getWorkplaceButtonStyle();
265
266         if (PUBLISHBUTTON_SHOW_AUTO.equals(publishButton)) {
267             if (getCms().isManagerOfProject()) {
268                 return button("../commons/publishproject.jsp", "body", "publish.png", Messages.GUI_BUTTON_PUBLISH_0 , buttonStyle);
269             } else {
270                 return "";
271             }
272         }
273
274         if (getCms().isManagerOfProject()) {
275             return (button("../commons/publishproject.jsp", "body", "publish.png", Messages.GUI_BUTTON_PUBLISH_0, buttonStyle));
276         } else {
277             return (button(null, null, "publish_in.png", Messages.GUI_BUTTON_PUBLISH_0, buttonStyle));
278         }
279     }
280
281     /**
282      * Returns a html select box filled with the current users accessible sites.<p>
283      *
284      * @param htmlAttributes attributes that will be inserted into the generated html
285      * @return a html select box filled with the current users accessible sites
286      */

287     public String JavaDoc getSiteSelect(String JavaDoc htmlAttributes) {
288
289         List JavaDoc options = new ArrayList JavaDoc();
290         List JavaDoc values = new ArrayList JavaDoc();
291         int selectedIndex = 0;
292
293         List JavaDoc sites = CmsSiteManager.getAvailableSites(getCms(), true);
294
295         Iterator JavaDoc i = sites.iterator();
296         int pos = 0;
297         while (i.hasNext()) {
298             CmsSite site = (CmsSite)i.next();
299             values.add(site.getSiteRoot());
300             options.add(site.getTitle());
301             if (site.getSiteRoot().equals(getSettings().getSite())) {
302                 // this is the user's current site
303
selectedIndex = pos;
304             }
305             pos++;
306         }
307
308         return buildSelect(htmlAttributes, options, values, selectedIndex);
309     }
310
311     /**
312      * Returns the startup URI for display in the main body frame, this can
313      * either be the user default view, or (if set) a sepcific startup resource.<p>
314      *
315      * @return the startup URI for display in the main body frame
316      */

317     public String JavaDoc getStartupUri() {
318
319         String JavaDoc result = getSettings().getViewStartup();
320         if (result == null) {
321             // no specific startup URI is set, use view from user settings
322
result = getSettings().getViewUri();
323         } else {
324             // reset the startup URI, so that it is not displayed again on reload of the frameset
325
getSettings().setViewStartup(null);
326         }
327         return CmsRequestUtil.appendParameter(result, CmsFrameset.PARAM_WP_FRAME, FRAMES[2]);
328     }
329
330     /**
331      * Returns a html select box filled with the views accessible by the current user.<p>
332      *
333      * @param htmlAttributes attributes that will be inserted into the generated html
334      * @return a html select box filled with the views accessible by the current user
335      */

336     public String JavaDoc getViewSelect(String JavaDoc htmlAttributes) {
337
338         List JavaDoc options = new ArrayList JavaDoc();
339         List JavaDoc values = new ArrayList JavaDoc();
340         int selectedIndex = 0;
341
342         // loop through the vectors and fill the result vectors
343
Iterator JavaDoc i = OpenCms.getWorkplaceManager().getViews().iterator();
344         int count = -1;
345         String JavaDoc currentView = getSettings().getViewUri();
346         if (CmsStringUtil.isNotEmpty(currentView)) {
347             // remove possible parameters from current view
348
int pos = currentView.indexOf('?');
349             if (pos >= 0) {
350                 currentView = currentView.substring(0, pos);
351             }
352         }
353         while (i.hasNext()) {
354             CmsWorkplaceView view = (CmsWorkplaceView)i.next();
355             if (getCms().existsResource(view.getUri(), CmsResourceFilter.ONLY_VISIBLE_NO_DELETED)) {
356                 count++;
357                 // ensure the current user has +v+r permissions on the view
358
String JavaDoc loopLink = getJsp().link(view.getUri());
359                 String JavaDoc localizedKey = resolveMacros(view.getKey());
360                 options.add(localizedKey);
361                 values.add(loopLink);
362
363                 if (loopLink.equals(currentView)) {
364                     selectedIndex = count;
365                 }
366             }
367         }
368
369         return buildSelect(htmlAttributes, options, values, selectedIndex);
370     }
371
372     /**
373      * Returns the reload URI for the OpenCms workplace.<p>
374      *
375      * @return the reload URI for the OpenCms workplace
376      */

377     public String JavaDoc getWorkplaceReloadUri() {
378
379         return getJsp().link(CmsFrameset.JSP_WORKPLACE_URI);
380     }
381
382     /**
383      * Returns <code>true</code> if a reload of the main body frame is required.<p>
384      *
385      * This value is modified with the select options (project, site or view) in the head frame of
386      * the Workplace. If a user changes one of these select values, the head frame is posted
387      * "against itself". The posted values will be processed by this class, causing
388      * the internal Workplace settings to change. After these settings have been changed,
389      * a reload of the main body frame is required in order to update it with the new values.
390      * A JavaScript in the Workplace head frame will be executed in this case.<p>
391      *
392      * @return <code>true</code> if a reload of the main body frame is required
393      */

394     public boolean isReloadRequired() {
395
396         return m_reloadRequired;
397     }
398
399     /**
400      * Returns true if the user has enabled synchronization.<p>
401      *
402      * @return true if the user has enabled synchronization
403      */

404     public boolean isSyncEnabled() {
405
406         CmsSynchronizeSettings syncSettings = getSettings().getUserSettings().getSynchronizeSettings();
407         return (syncSettings != null) && syncSettings.isSyncEnabled();
408     }
409
410     /**
411      * Indicates if the site selector should be shown in the top frame depending on the count of accessible sites.<p>
412      *
413      * @return true if site selector should be shown, otherwise false
414      */

415     public boolean showSiteSelector() {
416
417         if (getSettings().getUserSettings().getRestrictExplorerView()) {
418             // restricted explorer view to site and folder, do not show site selector
419
return false;
420         }
421         // count available sites
422
int siteCount = CmsSiteManager.getAvailableSites(getCms(), true).size();
423         return (siteCount > 1);
424     }
425
426     
427     /**
428      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
429      */

430     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
431
432         // check if a startup page has been set
433
String JavaDoc frame = CmsRequestUtil.getNotEmptyDecodedParameter(request, CmsFrameset.PARAM_WP_FRAME);
434         if ((frame == null) || (FRAMES_LIST.indexOf(frame) < 0)) {
435             // illegal or no frame selected, assume the "top" frame
436
frame = FRAMES[0];
437         }
438
439         if (FRAMES[0].equals(frame)) {
440             // top frame requested - execute special reload actions
441
topFrameReload(settings);
442         }
443         
444         // check if a startup page has been set
445
String JavaDoc startup = CmsRequestUtil.getNotEmptyDecodedParameter(request, CmsFrameset.PARAM_WP_START);
446         if (startup != null) {
447             m_reloadRequired = true;
448             settings.setViewStartup(startup);
449         }
450         
451         // check if the user requested a view change
452
String JavaDoc view = request.getParameter(CmsFrameset.PARAM_WP_VIEW);
453         if (view != null) {
454             m_reloadRequired = true;
455             settings.setViewUri(view);
456             // TODO: This is a workaround to make dialogs work in the legacy XMLTemplate views
457
settings.getFrameUris().put("body", view);
458             settings.getFrameUris().put("admin_content", "/system/workplace/action/administration_content_top.html");
459         }
460         
461         m_reloadRequired = initSettings(settings, request) || m_reloadRequired;
462     }
463
464     /**
465      * Performs certain clear cache actions if the top frame is reloaded.<p>
466      *
467      * @param settings the current users workplace settings
468      */

469     protected void topFrameReload(CmsWorkplaceSettings settings) {
470
471         // ensure to read the settings from the database
472
initUserSettings(getCms(), settings, true);
473
474         // reset the HTML list in order to force a full reload
475
settings.setListObject(null);
476     }
477 }
478
Popular Tags