2 * Copyright 2010 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package gr.ebs.gss.client;
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;
31 * A {@link Header} subclass that maintains sorting state and displays an icon
32 * to indicate the sort direction.
34 public class SortableHeader extends Header<String> {
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);
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);
47 private static Template template;
52 public static interface Resources extends ClientBundle {
54 ImageResource downArrow();
56 ImageResource upArrow();
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());
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);
71 private boolean reverseSort = false;
72 private boolean sorted = false;
75 SortableHeader(String text) {
76 super(new ClickableTextCell());
77 if (template == null) {
78 template = GWT.create(Template.class);
83 public boolean getReverseSort() {
88 public String getValue() {
93 public void render(Context context, SafeHtmlBuilder sb) {
95 sb.append(template.sorted(IMAGE_WIDTH, reverseSort ? DOWN_ARROW : UP_ARROW, text));
97 sb.append(template.unsorted(IMAGE_WIDTH, text));
101 public void setReverseSort(boolean reverseSort) {
102 this.reverseSort = reverseSort;
105 public void setSorted(boolean sorted) {
106 this.sorted = sorted;
109 public void toggleReverseSort() {
110 this.reverseSort = !this.reverseSort;