Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.temp.client.foundation.widget.table.cell;

import com.google.gwt.cell.client.*;
import com.google.gwt.dom.client.*;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.safehtml.shared.*;
import com.google.gwt.user.client.ui.*;
import com.temp.client.foundation.handler.*;
import com.temp.shared.utils.*;

public class TextDataCell<E> extends AbstractCell<E> {

    // full text popup variables
    private static final String LINK_STYLE = "link-button";
    private static final String POPUP_STYLE = "HoverFullTextPopup";
    private static final int POPUP_PADDING_LEFT_PX = 8;
    private static final int POPUP_PADDING_TOP_PX = 6;
    private static final int POPUP_BORDER_WIDTH_PX = 1;
    private static final int CELL_PADDING_PX = 4;

    private static final String MOUSEOVER_EVENT = "mouseover";
    private static final String CLICK_EVENT = "click";

    private DataIsClickableHandler<E> isClickableHandler;
    private DisplayStringHandler<E> displayStringHandler;
    private DataClickHandler<E> clickHandler;
    private Label fullTextLabel = new Label();
    private E cellValue;
    private DecoratedPopupPanel popup = new DecoratedPopupPanel( true );

    public TextDataCell() {
        this( null, null, null );
    }

    public TextDataCell( DisplayStringHandler<E> displayStringHandler ) {
        this( displayStringHandler, null, null );
    }

    public TextDataCell(
            DisplayStringHandler<E> displayStringHandler,
            DataClickHandler<E> clickHandler ) {
        this( displayStringHandler, clickHandler, null );
    }

    public TextDataCell(
            DisplayStringHandler<E> displayStringHandler,
            DataClickHandler<E> clickHandler,
            DataIsClickableHandler<E> isClickableHandler
    ) {

        super( MOUSEOVER_EVENT, CLICK_EVENT );
        setDataDisplayStringHandler( displayStringHandler );
        setDataClickHandler( clickHandler );
        setDataIsClickableHandler( isClickableHandler );
        initializePopup();
    }

    private void setCellValue( E value ) {
        this.cellValue = value;
    }

    private E getCellValue() {
        return cellValue;
    }

    private void setDataDisplayStringHandler( DisplayStringHandler<E> displayStringHandler ) {
        if ( displayStringHandler == null ) {
            displayStringHandler = new DisplayStringHandler<E>() {
                public String getDisplayString( E value ) {
                    if ( value != null ) {
                        return value.toString();
                    }
                    return "";
                }
            };
        }
        this.displayStringHandler = displayStringHandler;
    }

    private void setDataClickHandler( DataClickHandler<E> clickHandler ) {
        this.clickHandler = clickHandler;
    }

    private void setDataIsClickableHandler( DataIsClickableHandler<E> isClickableHandler ) {
        if ( isClickableHandler == null ) {
            isClickableHandler = new DataIsClickableHandler<E>() {
                public boolean isClickable( E elem ) {
                    return true;
                }
            };
        }
        this.isClickableHandler = isClickableHandler;
    }

    private void initializePopup() {
        popup.setGlassEnabled( false );
        popup.setWidget( fullTextLabel );
        popup.setStyleName( POPUP_STYLE );
        popup.hide();
        popup.addDomHandler( new MouseOutHandler() {
            public void onMouseOut( MouseOutEvent event ) {
                popup.hide();
            }
        }, MouseOutEvent.getType() );
        popup.addDomHandler( new ClickHandler() {
            public void onClick( ClickEvent event ) {
                if ( clickHandler != null ) {
                    E value = getCellValue();
                    if ( value != null ) {
                        clickHandler.onClick( value );
                    }
                }
            }
        }, ClickEvent.getType() );
    }

    protected void showFullTextPopup( Element wrapper, E value ) {
        // check if some of the text is being hidden.
        if ( isClickable( value ) ) {
            setCellValue( value );
            fullTextLabel.setStyleName( LINK_STYLE );
        } else {
            fullTextLabel.removeStyleName( LINK_STYLE );
            setCellValue( null );
        }
        fullTextLabel.setText( displayStringHandler.getDisplayString( value ) );
        int left = wrapper.getAbsoluteLeft() - (POPUP_PADDING_LEFT_PX + POPUP_BORDER_WIDTH_PX);
        int top = wrapper.getAbsoluteTop() - (POPUP_PADDING_TOP_PX + POPUP_BORDER_WIDTH_PX);
        popup.setPopupPosition( left, top );
        popup.show();
    }

    private boolean isClickable( E value ) {
        return clickHandler != null && isClickableHandler.isClickable( value );
    }

    @Override
    public void render( com.google.gwt.cell.client.Cell.Context context, E value, SafeHtmlBuilder sb ) {
        if ( isClickable( value ) ) {
            sb.appendHtmlConstant( "<span class='" + LINK_STYLE + "'>" );
        } else {
            sb.appendHtmlConstant( "<span>" );
        }
        sb.appendEscaped( displayStringHandler.getDisplayString( value ) );
        sb.appendHtmlConstant( "</span>" );
    }

    @Override
    public void onBrowserEvent( Context context, Element parent, E value, NativeEvent event, ValueUpdater<E> valueUpdater ) {
        String eventType = event.getType();

        if ( value != null && getConsumedEvents().contains( eventType ) ) {

            EventTarget eventTarget = event.getEventTarget();
            if ( Element.is( eventTarget ) ) {
                Element target = eventTarget.cast();
                Element container = parent;
                Element wrapper = container.getFirstChildElement();

                while ( wrapper != null ) {
                    if ( wrapper.isOrHasChild( target ) ) {
                        if ( eventType.equals( MOUSEOVER_EVENT ) ) {
                            // on hover full text
                            int containerWidth = container.getAbsoluteRight() - container.getAbsoluteLeft() + CELL_PADDING_PX;
                            int textWidth = wrapper.getAbsoluteRight() - wrapper.getAbsoluteLeft();
                            if ( containerWidth < textWidth ) {
                                showFullTextPopup( wrapper, value );
                            }
                            break;
                        } else if ( eventType.equals( CLICK_EVENT ) ) {
                            // click
                            if ( isClickable( value ) ) {
                                clickHandler.onClick( value );
                            }
                            break;
                        }
                    }
                    wrapper = wrapper.getNextSiblingElement();
                }
            }
        }
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/client/foundation/widget/table/cell/TextDataCell.java

Diff revisions: vs.
Revision Author Commited Message
948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

626 GeorgeS picture GeorgeS Wed 11 Apr, 2012 19:39:41 +0000