001/* 002 * (C) Copyright 2010 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 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.platform.forms.layout.api.impl; 020 021import java.util.Comparator; 022 023import org.nuxeo.ecm.platform.forms.layout.api.WidgetTypeConfiguration; 024import org.nuxeo.ecm.platform.forms.layout.api.WidgetTypeDefinition; 025 026/** 027 * Compares widget types on id or label. 028 * 029 * @author Anahide Tchertchian 030 * @since 5.4.2 031 */ 032public class WidgetTypeDefinitionComparator implements Comparator<WidgetTypeDefinition> { 033 034 protected boolean compareLabels = false; 035 036 public WidgetTypeDefinitionComparator(boolean compareLabels) { 037 super(); 038 this.compareLabels = compareLabels; 039 } 040 041 @Override 042 public int compare(WidgetTypeDefinition o1, WidgetTypeDefinition o2) { 043 if (o1 == null && o2 == null) { 044 return 0; 045 } 046 if (o1 == null) { 047 return -1; 048 } 049 if (o2 == null) { 050 return 1; 051 } 052 return getComparisonString(o1).compareTo(getComparisonString(o2)); 053 } 054 055 protected String getComparisonString(WidgetTypeDefinition def) { 056 String res = def.getName(); 057 if (compareLabels) { 058 WidgetTypeConfiguration conf = def.getConfiguration(); 059 if (conf != null && conf.getTitle() != null) { 060 res = conf.getTitle(); 061 } 062 } 063 return res; 064 } 065 066}