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.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 super(CmsResourceFilter.ONLY_VISIBLE_NO_DELETED.addRequireFolder()); 086 087 m_localeContext = localeContext; 088 CmsResource contextResource = context.getResources().get(0); 089 CmsResource realFile = contextResource; 090 if (realFile.isFolder()) { 091 CmsResource defaultFile = context.getCms().readDefaultFile(realFile, CmsResourceFilter.IGNORE_EXPIRATION); 092 if (defaultFile != null) { 093 realFile = defaultFile; 094 } 095 } 096 getContents().displayResourceInfo(Collections.singletonList(realFile)); 097 098 IndexedContainer siteData = (IndexedContainer)getContents().getSiteSelector().getContainerDataSource(); 099 100 m_context = context; 101 102 CmsLocaleGroup localeGroup = localeContext.getLocaleGroup(); 103 Map<Locale, CmsResource> resourcesByLocale = localeGroup.getResourcesByLocale(); 104 int index = 0; 105 for (Map.Entry<Locale, CmsResource> entry : resourcesByLocale.entrySet()) { 106 Locale localeKey = entry.getKey(); 107 CmsResource resourceValue = entry.getValue(); 108 String folderPath = null; 109 if (resourceValue.isFile()) { 110 folderPath = CmsResource.getParentFolder(resourceValue.getRootPath()); 111 } else { 112 folderPath = resourceValue.getRootPath(); 113 } 114 115 Item item = siteData.addItemAt(index, folderPath); 116 index++; 117 item.getItemProperty(getContents().getSiteSelector().getItemCaptionPropertyId()).setValue( 118 CmsVaadinUtils.getMessageText( 119 Messages.GUI_LOCALECOMPARE_LOCALE_LABEL_1, 120 localeKey.getDisplayLanguage())); 121 } 122 123 addSelectionHandler(new I_CmsSelectionHandler<CmsResource>() { 124 125 public void onSelection(CmsResource selected) { 126 127 onClickOk(selected); 128 } 129 }); 130 getFileTree().setSelectionFilter(new Predicate<Item>() { 131 132 public boolean apply(Item item) { 133 134 CmsResource resource = (CmsResource)(item.getItemProperty( 135 CmsResourceTreeContainer.PROPERTY_RESOURCE).getValue()); 136 CmsResource srcResource = m_context.getResources().get(0); 137 switch (A_CmsUI.getCmsObject().getLocaleGroupService().checkLinkable(srcResource, resource)) { 138 case linkable: 139 return true; 140 default: 141 return false; 142 143 } 144 } 145 }); 146 147 Locale secondaryLocale = m_localeContext.getComparisonLocale(); 148 CmsLocaleGroup group = m_localeContext.getLocaleGroup(); 149 CmsSite site = OpenCms.getSiteManager().getSiteForRootPath(m_localeContext.getRoot().getRootPath()); 150 if (group.hasLocale(secondaryLocale)) { 151 152 CmsResource res = group.getResourcesByLocale().get(secondaryLocale); 153 String folder = res.getRootPath(); 154 if (res.isFile()) { 155 folder = CmsResource.getParentFolder(folder); 156 } 157 getContents().getSiteSelector().setValue(folder); 158 } else if (site != null) { 159 getContents().getSiteSelector().setValue(site.getSiteRoot()); 160 } 161 162 } 163 164 /** 165 * Executed when the 'Cancel' button is clicked.<p> 166 */ 167 public void onClickCancel() { 168 169 m_context.finish(Arrays.<CmsUUID> asList()); 170 } 171 172 /** 173 * Executed when the 'OK' button is clicked.<p> 174 * 175 * @param selected the selected resource 176 */ 177 public void onClickOk(CmsResource selected) { 178 179 try { 180 CmsResource target = selected; 181 CmsResource source = m_context.getResources().get(0); 182 CmsLocaleGroupService service = A_CmsUI.getCmsObject().getLocaleGroupService(); 183 service.attachLocaleGroupIndirect(source, target); 184 m_context.finish(Arrays.asList(source.getStructureId())); 185 } catch (CmsException e) { 186 LOG.error(e.getLocalizedMessage(), e); 187 m_context.error(e); 188 } 189 } 190}