2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
35 package gr.grnet.pithos.web.client;
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;
50 * A {@link Header} subclass that maintains sorting state and displays an icon
51 * to indicate the sort direction.
53 public class SortableHeader extends Header<String> {
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);
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);
66 private static Template template;
71 public static interface Resources extends ClientBundle {
73 ImageResource downArrow();
75 ImageResource upArrow();
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());
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);
90 private boolean reverseSort = false;
91 private boolean sorted = false;
93 private String property;
95 SortableHeader(String text, String property) {
96 super(new ClickableTextCell());
97 if (template == null) {
98 template = GWT.create(Template.class);
101 this.property = property;
104 public boolean getReverseSort() {
109 public String getValue() {
114 public void render(Context context, SafeHtmlBuilder sb) {
116 sb.append(template.sorted(IMAGE_WIDTH, reverseSort ? DOWN_ARROW : UP_ARROW, text));
118 sb.append(template.unsorted(IMAGE_WIDTH, text));
122 public void setReverseSort(boolean reverseSort) {
123 this.reverseSort = reverseSort;
126 public boolean isSorted() {
130 public void setSorted(boolean sorted) {
131 this.sorted = sorted;
134 public void toggleReverseSort() {
135 this.reverseSort = !this.reverseSort;
138 public String getProperty() {