001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ui; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsProject; 032import org.opencms.main.CmsUIServlet; 033import org.opencms.main.OpenCms; 034import org.opencms.ui.components.CmsBasicDialog; 035import org.opencms.ui.components.CmsBasicDialog.DialogWidth; 036import org.opencms.ui.components.extensions.CmsWindowExtension; 037import org.opencms.ui.login.CmsLoginHelper; 038import org.opencms.ui.util.CmsDisplayType; 039import org.opencms.util.CmsRequestUtil; 040import org.opencms.workplace.CmsWorkplaceManager; 041import org.opencms.workplace.CmsWorkplaceSettings; 042 043import java.io.Serializable; 044import java.util.Map; 045import java.util.concurrent.ConcurrentHashMap; 046 047import javax.servlet.http.HttpSession; 048 049import com.google.common.collect.Multimap; 050import com.vaadin.server.VaadinRequest; 051import com.vaadin.server.VaadinService; 052import com.vaadin.server.VaadinServlet; 053import com.vaadin.server.WrappedHttpSession; 054import com.vaadin.ui.Button; 055import com.vaadin.ui.Component; 056import com.vaadin.v7.ui.Label; 057import com.vaadin.ui.Notification; 058import com.vaadin.ui.Notification.Type; 059import com.vaadin.ui.UI; 060import com.vaadin.v7.ui.VerticalLayout; 061import com.vaadin.ui.Window; 062 063/** 064 * Abstract UI class providing access to the OpenCms context.<p> 065 */ 066public abstract class A_CmsUI extends UI { 067 068 /** The last offline project attribute key. */ 069 public static final String LAST_OFFLINE_PROJECT = "lastOfflineProject"; 070 071 /** Serial version id. */ 072 private static final long serialVersionUID = 989182479322461838L; 073 074 /** UI attribute storage. */ 075 private Map<String, Serializable> m_attributes; 076 077 /** The display type at the time the UI was initialized. */ 078 private CmsDisplayType m_displayType; 079 080 /** Extension used for opening new browser windows. */ 081 private CmsWindowExtension m_windowExtension; 082 083 /** 084 * Constructor.<p> 085 */ 086 public A_CmsUI() { 087 m_windowExtension = new CmsWindowExtension(this); 088 m_attributes = new ConcurrentHashMap<String, Serializable>(); 089 getLoadingIndicatorConfiguration().setFirstDelay(600); 090 } 091 092 /** 093 * Returns the current UI.<p> 094 * 095 * @return the current UI 096 */ 097 public static A_CmsUI get() { 098 099 return (A_CmsUI)(UI.getCurrent()); 100 } 101 102 /** 103 * Returns the current cms context.<p> 104 * 105 * @return the current cms context 106 */ 107 public static CmsObject getCmsObject() { 108 109 return ((CmsUIServlet)VaadinServlet.getCurrent()).getCmsObject(); 110 } 111 112 /** 113 * Changes to the given project. Will update session and workplace settings.<p> 114 * 115 * @param project the project to change to 116 */ 117 public void changeProject(CmsProject project) { 118 119 CmsObject cms = getCmsObject(); 120 if (!cms.getRequestContext().getCurrentProject().equals(project)) { 121 cms.getRequestContext().setCurrentProject(project); 122 getWorkplaceSettings().setProject(project.getUuid()); 123 OpenCms.getSessionManager().updateSessionInfo(cms, getHttpSession()); 124 if (!project.isOnlineProject()) { 125 setAttribute(LAST_OFFLINE_PROJECT, project); 126 } 127 } 128 } 129 130 /** 131 * Changes to the given site. Will update session and workplace settings.<p> 132 * 133 * @param siteRoot the site to change to 134 */ 135 public void changeSite(String siteRoot) { 136 137 if (!getCmsObject().getRequestContext().getSiteRoot().equals(siteRoot)) { 138 getCmsObject().getRequestContext().setSiteRoot(siteRoot); 139 getWorkplaceSettings().setSite(siteRoot); 140 OpenCms.getSessionManager().updateSessionInfo(getCmsObject(), getHttpSession()); 141 } 142 } 143 144 /** 145 * Closes all opened dialog windows.<p> 146 */ 147 public void closeWindows() { 148 149 for (Window window : getWindows()) { 150 window.close(); 151 } 152 } 153 154 /** 155 * Returns the requested UI attribute.<p> 156 * 157 * @param key the attribute key 158 * 159 * @return the attribute 160 */ 161 public Serializable getAttribute(String key) { 162 163 return m_attributes.get(key); 164 } 165 166 /** 167 * Gets the display type from the time when the UI was initialized.<p> 168 * 169 * @return the display type 170 */ 171 public CmsDisplayType getDisplayType() { 172 173 return m_displayType; 174 } 175 176 /** 177 * Returns the HTTP session.<p> 178 * 179 * @return the HTTP session 180 */ 181 public HttpSession getHttpSession() { 182 183 return ((WrappedHttpSession)getSession().getSession()).getHttpSession(); 184 } 185 186 /** 187 * Returns the last used offline project.<p> 188 * 189 * @return the last used offline project 190 */ 191 public CmsProject getLastOfflineProject() { 192 193 return (CmsProject)getAttribute(LAST_OFFLINE_PROJECT); 194 } 195 196 /** 197 * Gets the request parameters with which the application was loaded.<p> 198 * 199 * @return the request parameters 200 */ 201 public Multimap<String, String> getParameters() { 202 203 return CmsRequestUtil.getParameters(getPage().getLocation()); 204 } 205 206 /** 207 * Returns the workplace settings.<p> 208 * 209 * @return the workplace settings 210 */ 211 public CmsWorkplaceSettings getWorkplaceSettings() { 212 213 CmsWorkplaceSettings settings = (CmsWorkplaceSettings)getSession().getSession().getAttribute( 214 CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS); 215 if (settings == null) { 216 settings = CmsLoginHelper.initSiteAndProject(getCmsObject()); 217 VaadinService.getCurrentRequest().getWrappedSession().setAttribute( 218 CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS, 219 settings); 220 } 221 return settings; 222 } 223 224 /** 225 * Tries to open a new browser window, and shows a warning if opening the window fails (usually because of popup blockers).<p> 226 * 227 * @param link the URL to open in the new window 228 * @param target the target window name 229 */ 230 public void openPageOrWarn(String link, String target) { 231 232 openPageOrWarn(link, target, CmsVaadinUtils.getMessageText(org.opencms.ui.Messages.GUI_POPUP_BLOCKED_0)); 233 } 234 235 /** 236 * Tries to open a new browser window, and shows a warning if opening the window fails (usually because of popup blockers).<p> 237 * 238 * @param link the URL to open in the new window 239 * @param target the target window name 240 * @param warning the warning to show if opening the window fails 241 */ 242 public void openPageOrWarn(String link, String target, final String warning) { 243 244 m_windowExtension.open(link, target, new Runnable() { 245 246 public void run() { 247 248 Notification.show(warning, Type.ERROR_MESSAGE); 249 } 250 }); 251 } 252 253 /** 254 * Reloads the current UI.<p> 255 */ 256 public void reload() { 257 258 getPage().reload(); 259 } 260 261 /** 262 * Sets an UI attribute.<p> 263 * 264 * @param key the attribute key 265 * @param value the attribute value 266 */ 267 public void setAttribute(String key, Serializable value) { 268 269 m_attributes.put(key, value); 270 } 271 272 /** 273 * Replaces the ui content with a single dialog.<p> 274 * 275 * @param caption the caption 276 * @param dialog the dialog content 277 */ 278 public void setContentToDialog(String caption, CmsBasicDialog dialog) { 279 280 setContent(new Label()); 281 Window window = CmsBasicDialog.prepareWindow(DialogWidth.narrow); 282 window.setContent(dialog); 283 window.setCaption(caption); 284 window.setClosable(false); 285 addWindow(window); 286 window.center(); 287 } 288 289 /** 290 * Replaces the ui content with a single dialog.<p> 291 * 292 * TODO: In the future this should only handle window creation, refactor dialog contents to CmsBasicDialog 293 * 294 * @param caption the caption 295 * @param component the dialog content 296 */ 297 public void setContentToDialog(String caption, Component component) { 298 299 setContent(new Label()); 300 Window window = CmsBasicDialog.prepareWindow(DialogWidth.narrow); 301 CmsBasicDialog dialog = new CmsBasicDialog(); 302 VerticalLayout result = new VerticalLayout(); 303 dialog.setContent(result); 304 window.setContent(dialog); 305 window.setCaption(caption); 306 window.setClosable(false); 307 addWindow(window); 308 window.center(); 309 if (component instanceof I_CmsHasButtons) { 310 I_CmsHasButtons hasButtons = (I_CmsHasButtons)component; 311 for (Button button : hasButtons.getButtons()) { 312 dialog.addButton(button); 313 } 314 315 } 316 result.addComponent(component); 317 318 } 319 320 /** 321 * Displays an error message in a centered box.<p> 322 * 323 * @param error the error message to display 324 */ 325 public void setError(String error) { 326 327 setContentToDialog("Error", new Label(error)); 328 } 329 330 /** 331 * @see com.vaadin.ui.UI#init(com.vaadin.server.VaadinRequest) 332 */ 333 @Override 334 protected void init(VaadinRequest request) { 335 336 m_displayType = CmsDisplayType.getDisplayType(getPage().getBrowserWindowWidth()); 337 } 338 339}