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.dialogs; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsProject; 032import org.opencms.gwt.CmsCoreService; 033import org.opencms.main.CmsException; 034import org.opencms.main.CmsLog; 035import org.opencms.main.OpenCms; 036import org.opencms.ui.A_CmsUI; 037import org.opencms.ui.CmsVaadinUtils; 038import org.opencms.ui.I_CmsDialogContext; 039import org.opencms.ui.apps.Messages; 040import org.opencms.ui.components.CmsBasicDialog; 041import org.opencms.ui.components.CmsErrorDialog; 042import org.opencms.ui.components.CmsExtendedSiteSelector; 043import org.opencms.ui.components.CmsOkCancelActionHandler; 044import org.opencms.util.CmsStringUtil; 045import org.opencms.util.CmsUUID; 046 047import java.util.Arrays; 048import java.util.Collections; 049import java.util.Map; 050 051import org.apache.commons.logging.Log; 052 053import com.vaadin.data.provider.ListDataProvider; 054import com.vaadin.server.Page; 055import com.vaadin.ui.Button; 056import com.vaadin.ui.Button.ClickEvent; 057import com.vaadin.ui.Button.ClickListener; 058import com.vaadin.ui.ComboBox; 059import com.vaadin.ui.FormLayout; 060import com.vaadin.ui.UI; 061 062/** 063 * The site select dialog.<p> 064 */ 065public class CmsSiteSelectDialog extends CmsBasicDialog { 066 067 /** Logger instance for this class. */ 068 static final Log LOG = CmsLog.getLog(CmsSiteSelectDialog.class); 069 070 /** The serial version id. */ 071 private static final long serialVersionUID = 4455901453008760434L; 072 073 /** The cancel button. */ 074 private Button m_cancelButton; 075 076 /** The dialog context. */ 077 private I_CmsDialogContext m_context; 078 079 /** The site select. */ 080 private CmsExtendedSiteSelector m_siteComboBox; 081 082 /** Select box for changing the current project. */ 083 private ComboBox<CmsUUID> m_projectSelector; 084 085 /** 086 * Constructor.<p> 087 * 088 * @param context the dialog context 089 */ 090 public CmsSiteSelectDialog(CmsEmbeddedDialogContext context) { 091 092 m_context = context; 093 setContent(initForm()); 094 m_cancelButton = createButtonCancel(); 095 m_cancelButton.addClickListener(new ClickListener() { 096 097 private static final long serialVersionUID = 1L; 098 099 public void buttonClick(ClickEvent event) { 100 101 cancel(); 102 } 103 }); 104 addButton(m_cancelButton); 105 106 setActionHandler(new CmsOkCancelActionHandler() { 107 108 private static final long serialVersionUID = 1L; 109 110 @Override 111 protected void cancel() { 112 113 CmsSiteSelectDialog.this.cancel(); 114 } 115 116 @Override 117 protected void ok() { 118 119 changeSite(); 120 } 121 }); 122 } 123 124 /** 125 * Actually changes the site. 126 * 127 * @param context the dialog context 128 * @param option the site selector option 129 */ 130 public static void changeSite(CmsEmbeddedDialogContext context, CmsExtendedSiteSelector.SiteSelectorOption option) { 131 132 if ((option.getPath() != null) 133 || !CmsStringUtil.comparePaths(context.getCms().getRequestContext().getSiteRoot(), option.getSite())) { 134 String siteRoot = option.getSite(); 135 A_CmsUI.get().changeSite(option.getSite()); 136 if (CmsStringUtil.comparePaths(siteRoot, "/") || OpenCms.getSiteManager().isSharedFolder(siteRoot)) { 137 // switch to explorer view when selecting shared or root site 138 139 Page.getCurrent().open(CmsCoreService.getFileExplorerLink(A_CmsUI.getCmsObject(), siteRoot), "_top"); 140 return; 141 } 142 context.finish(option); 143 } else { 144 context.finish(null, null); 145 } 146 } 147 148 /** 149 * Cancels the dialog action.<p> 150 */ 151 void cancel() { 152 153 m_context.finish(Collections.<CmsUUID> emptyList()); 154 } 155 156 /** 157 * Submits the dialog action.<p> 158 */ 159 void changeSite() { 160 161 CmsExtendedSiteSelector.SiteSelectorOption option = m_siteComboBox.getValue(); 162 I_CmsDialogContext context = m_context; 163 changeSite((CmsEmbeddedDialogContext)context, option); 164 } 165 166 /** 167 * Initializes the form component.<p> 168 * 169 * @return the form component 170 */ 171 private FormLayout initForm() { 172 173 FormLayout form = new FormLayout(); 174 form.setWidth("100%"); 175 CmsObject cms = A_CmsUI.getCmsObject(); 176 m_siteComboBox = prepareSiteSelector(org.opencms.workplace.Messages.GUI_LABEL_SITE_0); 177 CmsExtendedSiteSelector.SiteSelectorOption optionForCurrentSite = m_siteComboBox.getOptionForSiteRoot( 178 cms.getRequestContext().getSiteRoot()); 179 if (optionForCurrentSite != null) { 180 m_siteComboBox.setValue(optionForCurrentSite); 181 } 182 form.addComponent(m_siteComboBox); 183 m_siteComboBox.addValueChangeListener(evt -> changeSite()); 184 185 Map<CmsUUID, String> projects = CmsVaadinUtils.getProjectsMap(A_CmsUI.getCmsObject()); 186 187 // switching to the Online project gives you no way to switch back unless you have access to the workplace 188 projects.remove(CmsProject.ONLINE_PROJECT_ID); 189 190 ListDataProvider<CmsUUID> projectsProvider = new ListDataProvider<>(projects.keySet()); 191 m_projectSelector = new ComboBox<CmsUUID>(); 192 m_projectSelector.setDataProvider(projectsProvider); 193 m_projectSelector.setItemCaptionGenerator(item -> projects.get(item)); 194 m_projectSelector.setWidth("100%"); 195 m_projectSelector.setEmptySelectionAllowed(false); 196 m_projectSelector.setCaption(CmsVaadinUtils.getMessageText(org.opencms.workplace.Messages.GUI_LABEL_PROJECT_0)); 197 CmsUUID currentProjectId = m_context.getCms().getRequestContext().getCurrentProject().getUuid(); 198 if (projects.containsKey(currentProjectId)) { 199 m_projectSelector.setValue(currentProjectId); 200 } else { 201 try { 202 CmsUUID ouProject = OpenCms.getOrgUnitManager().readOrganizationalUnit( 203 m_context.getCms(), 204 m_context.getCms().getRequestContext().getOuFqn()).getProjectId(); 205 if (projects.containsKey(ouProject)) { 206 m_projectSelector.setValue(ouProject); 207 } 208 } catch (CmsException e) { 209 LOG.error("Error while reading current OU.", e); 210 } 211 } 212 form.addComponent(m_projectSelector); 213 m_projectSelector.addValueChangeListener(event -> { 214 try { 215 A_CmsUI.get().changeProject(cms.readProject(event.getValue())); 216 217 // Reload, to make sure that we don't have any project ids stored in client side code that differ 218 // from the new project 219 m_context.finish(Arrays.asList(CmsUUID.getNullUUID())); 220 } catch (Exception e) { 221 CmsErrorDialog.showErrorDialog(e, () -> m_context.finish(null)); 222 } 223 }); 224 return form; 225 } 226 227 /** 228 * Prepares a combo box.<p> 229 * 230 * @param captionKey the caption message key 231 * 232 * @return the combo box 233 */ 234 private CmsExtendedSiteSelector prepareSiteSelector(String captionKey) { 235 236 CmsExtendedSiteSelector result = new CmsExtendedSiteSelector(); 237 String caption = CmsVaadinUtils.getWpMessagesForCurrentLocale().key(captionKey); 238 result.setCaption(caption); 239 result.initOptions(m_context.getCms(), true); 240 result.setPageLength(CmsExtendedSiteSelector.LONG_PAGE_LENGTH); 241 result.setWidth("100%"); 242 result.setPlaceholder( 243 Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_CLICK_TO_EDIT_0)); 244 return result; 245 } 246}