2015-06-24 15:16:13 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.portlets.widgets.wsexplorer.server;
|
|
|
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
|
|
|
import org.gcube.portlets.widgets.wsexplorer.client.Util;
|
2015-06-24 15:18:42 +02:00
|
|
|
import org.gcube.portlets.widgets.wsexplorer.client.WorkspaceExplorerConstants;
|
2018-05-18 18:31:46 +02:00
|
|
|
import org.gcube.portlets.widgets.wsexplorer.shared.ItemDTO;
|
2015-06-24 15:16:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A comparator that sorts the items first by type, folder up others down, the each group alphabetically on the item name.
|
|
|
|
* @author "Federico De Faveri defaveri@isti.cnr.it"
|
|
|
|
*/
|
2018-05-18 18:31:46 +02:00
|
|
|
public class ItemComparator implements Comparator<ItemDTO> {
|
2015-06-24 15:16:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
2018-05-18 18:31:46 +02:00
|
|
|
public int compare(ItemDTO item1, ItemDTO item2) {
|
2015-06-24 15:16:13 +02:00
|
|
|
|
|
|
|
//if one of the item is folder and the other one not, we move up the folder
|
|
|
|
boolean isItem1Folder = Util.isFolder(item1.getType());
|
|
|
|
boolean isItem2Folder = Util.isFolder(item2.getType());
|
|
|
|
|
2015-07-06 12:27:48 +02:00
|
|
|
//XOR
|
|
|
|
if (isSpecialFolder(item1) ^ isSpecialFolder(item2)) return isSpecialFolder(item1) ? -1 : 1;
|
2015-06-24 15:16:13 +02:00
|
|
|
|
2015-07-06 12:27:48 +02:00
|
|
|
//XOR
|
2015-06-24 15:16:13 +02:00
|
|
|
if (isItem1Folder ^ isItem2Folder) return isItem1Folder ? -1 : 1;
|
|
|
|
|
|
|
|
//otherwise we compare the names
|
|
|
|
return String.CASE_INSENSITIVE_ORDER.compare(item1.getName(), item2.getName());
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:31:46 +02:00
|
|
|
private boolean isSpecialFolder(ItemDTO item) {
|
2015-07-13 17:15:21 +02:00
|
|
|
// return item.getName().equals(WorkspaceExplorerConstants.VRE_FOLDERS_LABEL) && item.getParent()!=null && item.getParent().isRoot();
|
|
|
|
return item.getName().equals(WorkspaceExplorerConstants.VRE_FOLDERS_LABEL) && item.isSpecialFolder();
|
2015-06-24 15:16:13 +02:00
|
|
|
}
|
|
|
|
}
|