blob: e3fac2fca916c54cfe32334536c16d99323644e0 [file] [log] [blame]
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2007 IBM Corporation and others.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
// IBM Corporation - initial implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.library.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.eclipse.epf.library.LibraryResources;
import org.eclipse.epf.library.edit.PresentationContext;
import org.eclipse.epf.library.edit.util.Comparators;
import org.eclipse.epf.uma.MethodElement;
import org.eclipse.epf.uma.MethodElementProperty;
import org.eclipse.epf.uma.UmaFactory;
/**
* Helper class to handle sorting of Category elements
* @author Jeff Hardy
*
*/
public class CategorySortHelper {
public static final String KEY_CATEGORY_ELEMENTS__SORT_TYPE = "CategoryElementsSortType"; //$NON-NLS-1$
public static final String V_CATEGORY_ELEMENTS__SORT_TYPE_ALPHA = "Alphabetic"; //$NON-NLS-1$
public static final String V_CATEGORY_ELEMENTS__SORT_TYPE_REVERSE_ALPHA = "ReverseAlphabetic"; //$NON-NLS-1$
public static final String V_CATEGORY_ELEMENTS__SORT_TYPE_METHOD_TYPE = "MethodType"; //$NON-NLS-1$
public static final String V_CATEGORY_ELEMENTS__SORT_TYPE_MANUAL = "Manual"; //$NON-NLS-1$
/**
* Creates a new MethodElementProperty to store the Category sort type
* @param sortType
* @return
*/
public static MethodElementProperty createNewSortProperty(String sortType) {
MethodElementProperty prop = UmaFactory.eINSTANCE.createMethodElementProperty();
prop.setName(KEY_CATEGORY_ELEMENTS__SORT_TYPE);
if (sortType != null) {
prop.setValue(sortType);
}
return prop;
}
/**
* Return category sort property for method element
* @param element
* @return
* property if found, else null
*/
public static MethodElementProperty getCategorySortProperty(MethodElement element) {
List props = element.getMethodElementProperty();
for (Iterator it = props.iterator(); it.hasNext();) {
MethodElementProperty prop = (MethodElementProperty) it.next();
if (KEY_CATEGORY_ELEMENTS__SORT_TYPE.equals(prop.getName())) {
return prop;
}
}
return null;
}
/**
* return true iff the category sort-type is set to manual
* @param element
* @return
* property if found, else null
*/
public static boolean isManualCategorySort(MethodElement element) {
String sortType = getCategorySortValue(element);
if (V_CATEGORY_ELEMENTS__SORT_TYPE_MANUAL.equals(sortType))
return true;
return false;
}
/**
* Returns the sort-type for the given MethodElement.
* If none is set, will return Alphabetic sort (V_CATEGORY_ELEMENTS__SORT_TYPE_ALPHA) as a default
* @param element
* @return
*/
public static String getCategorySortValue(MethodElement element) {
MethodElementProperty prop = getCategorySortProperty(element);
if (prop == null)
return V_CATEGORY_ELEMENTS__SORT_TYPE_ALPHA;
return prop.getValue();
}
public static List<String> getCategorySortTypes() {
List<String> returnList = Arrays.<String>asList(
V_CATEGORY_ELEMENTS__SORT_TYPE_MANUAL,
V_CATEGORY_ELEMENTS__SORT_TYPE_ALPHA,
V_CATEGORY_ELEMENTS__SORT_TYPE_REVERSE_ALPHA,
V_CATEGORY_ELEMENTS__SORT_TYPE_METHOD_TYPE);
return returnList;
}
public static String getSortTypeDisplayName(String sortType) {
if (V_CATEGORY_ELEMENTS__SORT_TYPE_ALPHA.equals(sortType)) {
return LibraryResources.SortType_Alphabetic;
}
if (V_CATEGORY_ELEMENTS__SORT_TYPE_REVERSE_ALPHA.equals(sortType)) {
return LibraryResources.SortType_ReverseAlphabetic;
}
if (V_CATEGORY_ELEMENTS__SORT_TYPE_METHOD_TYPE.equals(sortType)) {
return LibraryResources.SortType_MethodType;
}
if (V_CATEGORY_ELEMENTS__SORT_TYPE_MANUAL.equals(sortType)) {
return LibraryResources.SortType_Manual;
}
return "";
}
/**
* Returns a sorted category element list based on the category's sort type
* @param element
* @param elements array to sort
* @return
*/
public static List<Object> sortCategoryElements(MethodElement element, Object[] elements) {
List<Object> returnList = new ArrayList<Object>(Arrays.<Object>asList(elements));
String sortType = getCategorySortValue(element);
if (V_CATEGORY_ELEMENTS__SORT_TYPE_MANUAL.equals(sortType)) {
return returnList;
} else if (V_CATEGORY_ELEMENTS__SORT_TYPE_ALPHA.equals(sortType)) {
Collections.<Object>sort(returnList, Comparators.DEFAULT_COMPARATOR);
} else if (V_CATEGORY_ELEMENTS__SORT_TYPE_REVERSE_ALPHA.equals(sortType)) {
Collections.<Object>sort(returnList, Comparators.REVERSE_NAME_COMPARATOR);
} else if (V_CATEGORY_ELEMENTS__SORT_TYPE_METHOD_TYPE.equals(sortType)) {
Collections.<Object>sort(returnList, PresentationContext.INSTANCE.getMethodElementTypeComparator());
}
return returnList;
}
}