Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / SortableHeader.java @ e6e9f6e6

History | View | Annotate | Download (4.6 kB)

1
/*
2
 * Copyright 2011-2013 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
  private String property;
94

    
95
  SortableHeader(String text, String property) {
96
    super(new ClickableTextCell());
97
    if (template == null) {
98
      template = GWT.create(Template.class);
99
    }
100
    this.text = text;
101
    this.property = property;
102
  }
103

    
104
  public boolean getReverseSort() {
105
    return reverseSort;
106
  }
107

    
108
  @Override
109
  public String getValue() {
110
    return text;
111
  }
112

    
113
  @Override
114
  public void render(Context context, SafeHtmlBuilder sb) {
115
    if (sorted) {
116
      sb.append(template.sorted(IMAGE_WIDTH, reverseSort ? DOWN_ARROW : UP_ARROW, text));
117
    } else {
118
      sb.append(template.unsorted(IMAGE_WIDTH, text));
119
    }
120
  }
121

    
122
  public void setReverseSort(boolean reverseSort) {
123
    this.reverseSort = reverseSort;
124
  }
125

    
126
  public boolean isSorted() {
127
          return sorted;
128
  }
129
  
130
  public void setSorted(boolean sorted) {
131
    this.sorted = sorted;
132
  }
133

    
134
  public void toggleReverseSort() {
135
    this.reverseSort = !this.reverseSort;
136
  }
137

    
138
public String getProperty() {
139
        return property;
140
}
141
}