root / src / gr / grnet / pithos / web / client / ResizableHeader.java @ e6b93be1
History | View | Annotate | Download (3.3 kB)
1 |
package gr.grnet.pithos.web.client; |
---|---|
2 |
|
3 |
|
4 |
import com.google.gwt.cell.client.AbstractCell; |
5 |
import com.google.gwt.cell.client.Cell.Context; |
6 |
import com.google.gwt.dom.client.Element; |
7 |
import com.google.gwt.dom.client.NativeEvent; |
8 |
import com.google.gwt.dom.client.Style.Cursor; |
9 |
import com.google.gwt.event.shared.HandlerRegistration; |
10 |
import com.google.gwt.safehtml.shared.SafeHtmlBuilder; |
11 |
import com.google.gwt.safehtml.shared.SafeHtmlUtils; |
12 |
import com.google.gwt.user.cellview.client.AbstractCellTable; |
13 |
import com.google.gwt.user.cellview.client.Column; |
14 |
import com.google.gwt.user.client.Event; |
15 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
16 |
import com.google.gwt.user.client.Event.NativePreviewHandler; |
17 |
|
18 |
public class ResizableHeader<T> extends SortableHeader { |
19 |
|
20 |
private Column<T, ?> column = null; |
21 |
private AbstractCellTable<T> cellTable;
|
22 |
private String title = ""; |
23 |
static private final int width = 10; |
24 |
|
25 |
public ResizableHeader(String title, AbstractCellTable<T> cellTable, Column<T, ?> column) { |
26 |
super(title);
|
27 |
this.title = title;
|
28 |
this.cellTable = cellTable;
|
29 |
this.column = column;
|
30 |
} |
31 |
|
32 |
@Override
|
33 |
public String getValue() |
34 |
{ |
35 |
return title;
|
36 |
} |
37 |
|
38 |
@Override
|
39 |
public void onBrowserEvent(Context context, Element target, NativeEvent event) { |
40 |
String eventType = event.getType();
|
41 |
int clientX = event.getClientX();
|
42 |
int absoluteLeft = target.getAbsoluteLeft();
|
43 |
int offsetWidth = target.getOffsetWidth();
|
44 |
if (clientX > absoluteLeft + offsetWidth - width) {
|
45 |
setCursor(target, Cursor.COL_RESIZE);
|
46 |
} else {
|
47 |
setCursor(target, Cursor.DEFAULT);
|
48 |
} |
49 |
if(eventType.equals("mousedown")) { |
50 |
if (clientX > absoluteLeft + offsetWidth - width) {
|
51 |
new ColumnResizeHelper<T>(cellTable, column, target);
|
52 |
} |
53 |
event.preventDefault(); |
54 |
event.stopPropagation(); |
55 |
} else {
|
56 |
return;
|
57 |
} |
58 |
} |
59 |
|
60 |
private void setCursor(Element element, Cursor cursor) { |
61 |
element.getStyle().setCursor(cursor); |
62 |
} |
63 |
|
64 |
class ColumnResizeHelper<T> implements NativePreviewHandler { |
65 |
|
66 |
private HandlerRegistration handler;
|
67 |
private AbstractCellTable<T> table;
|
68 |
private Column<T, ?> col;
|
69 |
private Element el; |
70 |
|
71 |
public ColumnResizeHelper(AbstractCellTable<T> table, Column<T, ?> col, Element el) { |
72 |
this.el = el;
|
73 |
this.table = table;
|
74 |
this.col = col;
|
75 |
handler = Event.addNativePreviewHandler(this); |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public void onPreviewNativeEvent(NativePreviewEvent event) { |
80 |
NativeEvent nativeEvent = event.getNativeEvent(); |
81 |
nativeEvent.preventDefault(); |
82 |
nativeEvent.stopPropagation(); |
83 |
|
84 |
if (nativeEvent.getType().equals("mousemove")) { |
85 |
int absoluteLeft = el.getAbsoluteLeft();
|
86 |
int clientX = nativeEvent.getClientX();
|
87 |
int newWidth = clientX - absoluteLeft;
|
88 |
newWidth = newWidth < 10 ? 10 : newWidth; |
89 |
table.setColumnWidth(col, newWidth + "px");
|
90 |
} else if (nativeEvent.getType().equals("mouseup")) { |
91 |
handler.removeHandler(); |
92 |
} |
93 |
} |
94 |
} |
95 |
|
96 |
static class HeaderCell extends AbstractCell<String> { |
97 |
|
98 |
public HeaderCell() {
|
99 |
super("click", "mousedown", "mousemove"); |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public void render(com.google.gwt.cell.client.Cell.Context context, String value, SafeHtmlBuilder sb) { |
104 |
sb.append( |
105 |
new SafeHtmlBuilder()
|
106 |
.append(SafeHtmlUtils.fromString(value)) |
107 |
.toSafeHtml()); |
108 |
} |
109 |
|
110 |
} |
111 |
}; |
112 |
|
113 |
|