Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / ResizableHeader.java @ e6b93be1

History | View | Annotate | Download (3.3 kB)

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