001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.sitemap; 029 030import org.opencms.file.CmsResource; 031import org.opencms.file.CmsResourceFilter; 032import org.opencms.i18n.CmsLocaleGroup; 033import org.opencms.i18n.CmsLocaleGroupService; 034import org.opencms.main.CmsException; 035import org.opencms.main.CmsLog; 036import org.opencms.main.OpenCms; 037import org.opencms.site.CmsSite; 038import org.opencms.ui.A_CmsUI; 039import org.opencms.ui.CmsVaadinUtils; 040import org.opencms.ui.I_CmsDialogContext; 041import org.opencms.ui.Messages; 042import org.opencms.ui.components.fileselect.CmsResourceSelectDialog; 043import org.opencms.ui.components.fileselect.CmsResourceTreeContainer; 044import org.opencms.ui.components.fileselect.I_CmsSelectionHandler; 045import org.opencms.util.CmsUUID; 046 047import java.util.Arrays; 048import java.util.Collections; 049import java.util.Locale; 050import java.util.Map; 051 052import org.apache.commons.logging.Log; 053 054import com.google.common.base.Predicate; 055import com.vaadin.v7.data.Item; 056import com.vaadin.v7.data.util.IndexedContainer; 057 058/** 059 * Dialog used to select a resource which should be linked to a locale group.<p> 060 */ 061public class CmsLocaleLinkTargetSelectionDialog extends CmsResourceSelectDialog { 062 063 /** Logger instance for this class. */ 064 private static final Log LOG = CmsLog.getLog(CmsLocaleLinkTargetSelectionDialog.class); 065 066 /** Serial version id. */ 067 private static final long serialVersionUID = 1L; 068 069 /** The dialog context. */ 070 I_CmsDialogContext m_context; 071 072 /** The locale compare context. */ 073 private I_CmsLocaleCompareContext m_localeContext; 074 075 /** 076 * Creates a new instance.<p> 077 * 078 * @param context the dialog context 079 * @param localeContext the locale compare context 080 * 081 * @throws CmsException if something goes wrong 082 */ 083 public CmsLocaleLinkTargetSelectionDialog(I_CmsDialogContext context, I_CmsLocaleCompareContext localeContext) 084 throws CmsException { 085 086 super(CmsResourceFilter.ONLY_VISIBLE_NO_DELETED.addRequireFolder()); 087 088 m_localeContext = localeContext; 089 CmsResource contextResource = context.getResources().get(0); 090 CmsResource realFile = contextResource; 091 if (realFile.isFolder()) { 092 CmsResource defaultFile = context.getCms().readDefaultFile(realFile, CmsResourceFilter.IGNORE_EXPIRATION); 093 if (defaultFile != null) { 094 realFile = defaultFile; 095 } 096 } 097 getContents().displayResourceInfo(Collections.singletonList(realFile)); 098 099 IndexedContainer siteData = (IndexedContainer)getContents().getSiteSelector().getContainerDataSource(); 100 101 m_context = context; 102 103 CmsLocaleGroup localeGroup = localeContext.getLocaleGroup(); 104 Map<Locale, CmsResource> resourcesByLocale = localeGroup.getResourcesByLocale(); 105 int index = 0; 106 for (Map.Entry<Locale, CmsResource> entry : resourcesByLocale.entrySet()) { 107 Locale localeKey = entry.getKey(); 108 CmsResource resourceValue = entry.getValue(); 109 String folderPath = null; 110 if (resourceValue.isFile()) { 111 folderPath = CmsResource.getParentFolder(resourceValue.getRootPath()); 112 } else { 113 folderPath = resourceValue.getRootPath(); 114 } 115 116 Item item = siteData.addItemAt(index, folderPath); 117 index++; 118 item.getItemProperty(getContents().getSiteSelector().getItemCaptionPropertyId()).setValue( 119 CmsVaadinUtils.getMessageText( 120 Messages.GUI_LOCALECOMPARE_LOCALE_LABEL_1, 121 localeKey.getDisplayLanguage())); 122 } 123 124 addSelectionHandler(new I_CmsSelectionHandler<CmsResource>() { 125 126 public void onSelection(CmsResource selected) { 127 128 onClickOk(selected); 129 } 130 }); 131 getFileTree().setSelectionFilter(new Predicate<Item>() { 132 133 public boolean apply(Item item) { 134 135 CmsResource resource = (CmsResource)(item.getItemProperty( 136 CmsResourceTreeContainer.PROPERTY_RESOURCE).getValue()); 137 CmsResource srcResource = m_context.getResources().get(0); 138 switch (A_CmsUI.getCmsObject().getLocaleGroupService().checkLinkable(srcResource, resource)) { 139 case linkable: 140 return true; 141 default: 142 return false; 143 144 } 145 } 146 }); 147 148 Locale secondaryLocale = m_localeContext.getComparisonLocale(); 149 CmsLocaleGroup group = m_localeContext.getLocaleGroup(); 150 CmsSite site = OpenCms.getSiteManager().getSiteForRootPath(m_localeContext.getRoot().getRootPath()); 151 if (group.hasLocale(secondaryLocale)) { 152 153 CmsResource res = group.getResourcesByLocale().get(secondaryLocale); 154 String folder = res.getRootPath(); 155 if (res.isFile()) { 156 folder = CmsResource.getParentFolder(folder); 157 } 158 getContents().getSiteSelector().setValue(folder); 159 } else if (site != null) { 160 getContents().getSiteSelector().setValue(site.getSiteRoot()); 161 } 162 163 } 164 165 /** 166 * Executed when the 'Cancel' button is clicked.<p> 167 */ 168 public void onClickCancel() { 169 170 m_context.finish(Arrays.<CmsUUID> asList()); 171 } 172 173 /** 174 * Executed when the 'OK' button is clicked.<p> 175 * 176 * @param selected the selected resource 177 */ 178 public void onClickOk(CmsResource selected) { 179 180 try { 181 CmsResource target = selected; 182 CmsResource source = m_context.getResources().get(0); 183 CmsLocaleGroupService service = A_CmsUI.getCmsObject().getLocaleGroupService(); 184 service.attachLocaleGroupIndirect(source, target); 185 m_context.finish(Arrays.asList(source.getStructureId())); 186 } catch (CmsException e) { 187 LOG.error(e.getLocalizedMessage(), e); 188 m_context.error(e); 189 } 190 } 191}