Updated copyright
[pithos-web-client] / src / gr / grnet / pithos / web / client / SortableHeader.java
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35 package gr.grnet.pithos.web.client;
36
37 import com.google.gwt.cell.client.Cell.Context;
38 import com.google.gwt.cell.client.ClickableTextCell;
39 import com.google.gwt.core.client.GWT;
40 import com.google.gwt.resources.client.ClientBundle;
41 import com.google.gwt.resources.client.ImageResource;
42 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
43 import com.google.gwt.safehtml.shared.SafeHtml;
44 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
45 import com.google.gwt.safehtml.shared.SafeHtmlUtils;
46 import com.google.gwt.user.cellview.client.Header;
47 import com.google.gwt.user.client.ui.AbstractImagePrototype;
48
49 /**
50  * A {@link Header} subclass that maintains sorting state and displays an icon
51  * to indicate the sort direction.
52  */
53 public class SortableHeader extends Header<String> {
54
55   interface Template extends SafeHtmlTemplates {
56     @Template("<div style=\"position:relative;cursor:hand;cursor:pointer;"
57         + "padding-right:{0}px;\">{1}<div>{2}</div></div>")
58     SafeHtml sorted(int imageWidth, SafeHtml arrow, String text);
59
60     @Template("<div style=\"position:relative;cursor:hand;cursor:pointer;"
61         + "padding-right:{0}px;\"><div style=\"position:absolute;display:none;"
62         + "\"></div><div>{1}</div></div>")
63     SafeHtml unsorted(int imageWidth, String text);
64   }
65
66   private static Template template;
67
68   /**
69    * Image resources.
70    */
71   public static interface Resources extends ClientBundle {
72
73     ImageResource downArrow();
74
75     ImageResource upArrow();
76   }
77
78   private static final Resources RESOURCES = GWT.create(Resources.class);
79   private static final int IMAGE_WIDTH = 16;
80   private static final SafeHtml DOWN_ARROW = makeImage(RESOURCES.downArrow());
81   private static final SafeHtml UP_ARROW = makeImage(RESOURCES.upArrow());
82
83   private static SafeHtml makeImage(ImageResource resource) {
84     AbstractImagePrototype proto = AbstractImagePrototype.create(resource);
85     String html = proto.getHTML().replace("style='",
86         "style='position:absolute;right:0px;top:0px;");
87     return SafeHtmlUtils.fromTrustedString(html);
88   }
89
90   private boolean reverseSort = false;
91   private boolean sorted = false;
92   private String text;
93
94   SortableHeader(String text) {
95     super(new ClickableTextCell());
96     if (template == null) {
97       template = GWT.create(Template.class);
98     }
99     this.text = text;
100   }
101
102   public boolean getReverseSort() {
103     return reverseSort;
104   }
105
106   @Override
107   public String getValue() {
108     return text;
109   }
110
111   @Override
112   public void render(Context context, SafeHtmlBuilder sb) {
113     if (sorted) {
114       sb.append(template.sorted(IMAGE_WIDTH, reverseSort ? DOWN_ARROW : UP_ARROW, text));
115     } else {
116       sb.append(template.unsorted(IMAGE_WIDTH, text));
117     }
118   }
119
120   public void setReverseSort(boolean reverseSort) {
121     this.reverseSort = reverseSort;
122   }
123
124   public void setSorted(boolean sorted) {
125     this.sorted = sorted;
126   }
127
128   public void toggleReverseSort() {
129     this.reverseSort = !this.reverseSort;
130   }
131 }