sorting support for celltable
[pithos] / src / gr / ebs / gss / client / SortableHeader.java
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package gr.ebs.gss.client;
17
18 import com.google.gwt.cell.client.Cell.Context;
19 import com.google.gwt.cell.client.ClickableTextCell;
20 import com.google.gwt.core.client.GWT;
21 import com.google.gwt.resources.client.ClientBundle;
22 import com.google.gwt.resources.client.ImageResource;
23 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
24 import com.google.gwt.safehtml.shared.SafeHtml;
25 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
26 import com.google.gwt.safehtml.shared.SafeHtmlUtils;
27 import com.google.gwt.user.cellview.client.Header;
28 import com.google.gwt.user.client.ui.AbstractImagePrototype;
29
30 /**
31  * A {@link Header} subclass that maintains sorting state and displays an icon
32  * to indicate the sort direction.
33  */
34 public class SortableHeader extends Header<String> {
35
36   interface Template extends SafeHtmlTemplates {
37     @Template("<div style=\"position:relative;cursor:hand;cursor:pointer;"
38         + "padding-right:{0}px;\">{1}<div>{2}</div></div>")
39     SafeHtml sorted(int imageWidth, SafeHtml arrow, String text);
40
41     @Template("<div style=\"position:relative;cursor:hand;cursor:pointer;"
42         + "padding-right:{0}px;\"><div style=\"position:absolute;display:none;"
43         + "\"></div><div>{1}</div></div>")
44     SafeHtml unsorted(int imageWidth, String text);
45   }
46
47   private static Template template;
48
49   /**
50    * Image resources.
51    */
52   public static interface Resources extends ClientBundle {
53
54     ImageResource downArrow();
55
56     ImageResource upArrow();
57   }
58
59   private static final Resources RESOURCES = GWT.create(Resources.class);
60   private static final int IMAGE_WIDTH = 16;
61   private static final SafeHtml DOWN_ARROW = makeImage(RESOURCES.downArrow());
62   private static final SafeHtml UP_ARROW = makeImage(RESOURCES.upArrow());
63
64   private static SafeHtml makeImage(ImageResource resource) {
65     AbstractImagePrototype proto = AbstractImagePrototype.create(resource);
66     String html = proto.getHTML().replace("style='",
67         "style='position:absolute;right:0px;top:0px;");
68     return SafeHtmlUtils.fromTrustedString(html);
69   }
70
71   private boolean reverseSort = false;
72   private boolean sorted = false;
73   private String text;
74
75   SortableHeader(String text) {
76     super(new ClickableTextCell());
77     if (template == null) {
78       template = GWT.create(Template.class);
79     }
80     this.text = text;
81   }
82
83   public boolean getReverseSort() {
84     return reverseSort;
85   }
86
87   @Override
88   public String getValue() {
89     return text;
90   }
91
92   @Override
93   public void render(Context context, SafeHtmlBuilder sb) {
94     if (sorted) {
95       sb.append(template.sorted(IMAGE_WIDTH, reverseSort ? DOWN_ARROW : UP_ARROW, text));
96     } else {
97       sb.append(template.unsorted(IMAGE_WIDTH, text));
98     }
99   }
100
101   public void setReverseSort(boolean reverseSort) {
102     this.reverseSort = reverseSort;
103   }
104
105   public void setSorted(boolean sorted) {
106     this.sorted = sorted;
107   }
108
109   public void toggleReverseSort() {
110     this.reverseSort = !this.reverseSort;
111   }
112 }