View Javadoc
1   /*
2    * Created on 2003-jun-12
3    */
4   package net.sourceforge.jsh3modtool.gui.imagetable.cellsorters;
5   
6   import java.util.Comparator;
7   
8   import net.sourceforge.jsh3modtool.gui.imagetable.FolderCell;
9   
10  /***
11   * The FoldersFirstCellSorter sorts so all folders are first and images comes
12   * afterwards. In order to be able to sort the same cell types another Comparator
13   * must be used when sorting objects of the same type.
14   * 
15   * @author erma
16   */
17  public class FoldersFirstCellSorter extends AbstractCellSorter
18  {
19      private static final String PARENT_DIR_STR = "..";
20  	private Comparator sameCellTypeSorter;
21      
22      /***
23       * Creates a FoldersFirstCellSorter.
24       * @param sorter the actual comparator to use for the same class types.
25       */
26      public FoldersFirstCellSorter(Comparator sorter )
27      {
28          sameCellTypeSorter = sorter;
29      }
30      
31  	/*** {@inheritDoc} */
32  	public int compare(Object o1, Object o2)
33  	{
34          int value;
35          
36          if ( o1.getClass().equals(o2.getClass()))
37          {
38              if ( isFolderCell( o1 ))
39              {
40                  value = compareFolderCells((FolderCell) o1, (FolderCell) o2);
41              } 
42              else
43              {
44                  value = sameCellTypeSorter.compare(o1, o2);
45              }
46          } 
47          else
48          {
49              if ( isFolderCell(o1) && isImageCell(o2) )
50              {
51                  value = -1;
52              }
53              else if ( isImageCell(o1) && isFolderCell(o2) )
54              {
55                  value = 1;
56              }
57              else
58              {
59                  throw new IllegalStateException("Can not sort the types " + o1.getClass() + " and " + o2.getClass());
60              }
61          }
62  		
63  		return value;
64  	}
65  
66      /***
67       * Compares the two objects (that is folder cells). This method make sure that the
68       * ".." folder always is first in the list.
69       * @param o1 first object.
70       * @param o2 second object.
71       * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. 
72       */
73  	private int compareFolderCells(FolderCell o1, FolderCell o2)
74  	{
75  		int value;
76  		if ( o1.getTargetPath().equals(PARENT_DIR_STR) )
77  		{
78  		    value = -1;
79  		}
80  		else if ( o2.getTargetPath().equals(PARENT_DIR_STR) )
81  		{
82  		    value = 1;
83  		}
84  		else
85  		{
86  		    value = sameCellTypeSorter.compare(o1, o2);
87  		}
88  		return value;
89  	}
90  
91  }
92  
93  /***
94   * Revision history
95   * 
96   * $Log:   P:/scm_db/archives/iWIP/iWIP Client Java/src/com/imbridge/iwip/client/browsegui/browsertable/cellsorters/FoldersFirstCellSorter.java-arc  $
97   * 
98   *    Rev 1.0   Jun 12 2003 10:10:54   Erik Mattsson
99   * Initial revision.
100  */