View Javadoc
1   package org.argeo.eclipse.ui.jcr.lists;
2   
3   import javax.jcr.Node;
4   import javax.jcr.RepositoryException;
5   import javax.jcr.query.Row;
6   
7   import org.argeo.eclipse.ui.EclipseUiException;
8   import org.eclipse.jface.viewers.Viewer;
9   
10  /**
11   * Base comparator to enable ordering on Table or Tree viewer that display Jcr
12   * rows
13   */
14  public class RowViewerComparator extends NodeViewerComparator {
15  	private static final long serialVersionUID = 7020939505172625113L;
16  	protected String selectorName;
17  
18  	public RowViewerComparator() {
19  	}
20  
21  	/**
22  	 * e1 and e2 must both be Jcr rows.
23  	 * 
24  	 * @param viewer
25  	 * @param e1
26  	 * @param e2
27  	 * @return
28  	 */
29  	@Override
30  	public int compare(Viewer viewer, Object e1, Object e2) {
31  		try {
32  			Node n1 = ((Row) e1).getNode(selectorName);
33  			Node n2 = ((Row) e2).getNode(selectorName);
34  			return super.compare(viewer, n1, n2);
35  		} catch (RepositoryException re) {
36  			throw new EclipseUiException("Unexpected error "
37  					+ "while comparing nodes", re);
38  		}
39  	}
40  
41  	/**
42  	 * @param propertyType
43  	 *            Corresponding JCR type
44  	 * @param propertyName
45  	 *            name of the property to use.
46  	 */
47  	public void setColumn(int propertyType, String selectorName,
48  			String propertyName) {
49  		if (this.selectorName != null && getPropertyName() != null
50  				&& this.selectorName.equals(selectorName)
51  				&& this.getPropertyName().equals(propertyName)) {
52  			// Same column as last sort; toggle the direction
53  			setDirection(1 - getDirection());
54  		} else {
55  			// New column; do a descending sort
56  			setPropertyType(propertyType);
57  			setPropertyName(propertyName);
58  			this.selectorName = selectorName;
59  			setDirection(NodeViewerComparator.ASCENDING);
60  		}
61  	}
62  }