Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / SortableHeader.java @ 9d8eee31

History | View | Annotate | Download (4.5 kB)

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