001/* 002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 */ 018package org.nuxeo.ecm.platform.rendition.action; 019 020import net.sf.json.JSONArray; 021import net.sf.json.JSONObject; 022import org.jboss.seam.annotations.Name; 023import org.jboss.seam.annotations.Scope; 024import org.nuxeo.ecm.platform.rendition.automation.SuggestRenditionDefinitionEntry; 025import org.nuxeo.ecm.platform.ui.select2.common.Select2Common; 026 027import java.io.Serializable; 028import java.util.Arrays; 029import java.util.List; 030import java.util.Map; 031import java.util.Map.Entry; 032 033import static org.jboss.seam.ScopeType.EVENT; 034 035/** 036 * Helper component for rendition name widget relying on select2. 037 * 038 * @since 8.3 039 */ 040@Name("renditionDefinitionSelect2Support") 041@Scope(EVENT) 042public class RenditionDefinitionSelect2Support { 043 044 protected String label; 045 046 public String resolveRenditionDefinitions(List<String> list) { 047 return Select2Common.resolveDefaultEntries(list); 048 } 049 050 public String resolveRenditionDefinitions(String[] array) { 051 if (array == null || array.length == 0) { 052 return Select2Common.resolveDefaultEntries(null); 053 } 054 return Select2Common.resolveDefaultEntries(Arrays.asList(array)); 055 } 056 057 protected void reset() { 058 label = null; 059 } 060 061 public String encodeParameters(Map<String, Serializable> widgetProperties) { 062 return encodeCommonParameters(widgetProperties).toString(); 063 } 064 065 protected JSONObject encodeCommonParameters(Map<String, Serializable> widgetProperties) { 066 return encodeCommonParameters(widgetProperties, null); 067 } 068 069 protected JSONObject encodeCommonParameters(Map<String, Serializable> widgetProperties, 070 Map<String, String> additionalParameters) { 071 JSONObject obj = new JSONObject(); 072 obj.put("multiple", "true"); 073 obj.put(Select2Common.MIN_CHARS, "1"); 074 obj.put(Select2Common.READ_ONLY_PARAM, "false"); 075 obj.put(Select2Common.OPERATION_ID, SuggestRenditionDefinitionEntry.ID); 076 obj.put(Select2Common.WIDTH, "300px"); 077 obj.put(Select2Common.SELECTION_FORMATTER, "formatSelectedRenditionDefinitions"); 078 obj.put(Select2Common.SUGGESTION_FORMATTER, "formatSuggestedRenditionDefinitions"); 079 JSONArray tokenSeparator = new JSONArray(); 080 tokenSeparator.add(","); 081 tokenSeparator.add(" "); 082 obj.put("tokenSeparators", tokenSeparator); 083 if (additionalParameters != null) { 084 for (Entry<String, String> entry : additionalParameters.entrySet()) { 085 obj.put(entry.getKey(), entry.getValue().toString()); 086 } 087 } 088 for (Entry<String, Serializable> entry : widgetProperties.entrySet()) { 089 obj.put(entry.getKey(), entry.getValue().toString()); 090 } 091 return obj; 092 } 093 094 public String getLabel() { 095 return label; 096 } 097 098 public void setLabel(String label) { 099 this.label = label; 100 } 101 102}