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
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.*;

import java.util.*;

public class ClickableLimitedListDataCell<E, L extends List<E>> extends AbstractCell<L> {

    // 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 = 6;
    private static final int POPUP_PADDING_TOP_PX = 4;
    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 static final int MAX_ELEMENTS_TO_DISPLAY = 2;

    private DataClickHandler<L> clickHandler;
    private DisplayStringHandler<E> displayStringHandler;
    private VerticalPanel fullTextPanel = new VerticalPanel();
    private L cellValue;
    private DecoratedPopupPanel popup = new DecoratedPopupPanel( true );

    public ClickableLimitedListDataCell( DataClickHandler<L> _clickHandler, DisplayStringHandler<E> _displayStringHandler ) {
        super( CLICK_EVENT, MOUSEOVER_EVENT );
        clickHandler = _clickHandler;
        displayStringHandler = _displayStringHandler;
        if ( displayStringHandler == null ) {
            displayStringHandler = new DisplayStringHandler<E>() {
                public String getDisplayString( E value ) {
                    return value.toString();
                }
            };
        }
        initializePopup();
    }

    private void initializePopup() {
        popup.setGlassEnabled( false );
        popup.setWidget( fullTextPanel );
        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 ) {
                    L value = getCellValue();
                    if ( value != null ) {
                        clickHandler.onClick( value );
                    }
                }
            }
        }, ClickEvent.getType() );
    }

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

    private L getCellValue() {
        return cellValue;
    }

    protected void showFullTextPopup( Element wrapper, L valueList ) {
        setCellValue( valueList );
        fullTextPanel.clear();
        for ( E value : valueList ) {
            Label fullTextLabel = new Label( displayStringHandler.getDisplayString( value ) );
            fullTextLabel.setStyleName( LINK_STYLE );
            fullTextPanel.add( fullTextLabel );
        }
        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();
    }

    @Override
    public void render( Context context, L valueList, SafeHtmlBuilder sb ) {
        if ( !CollectionsUtils.isEmpty( valueList ) ) {
            int displayedElements = 0;
            for ( E valueListElement : valueList ) {
                if ( displayedElements > 0 ) {
                    sb.appendHtmlConstant( "<br/>" );
                }
                if ( displayedElements >= MAX_ELEMENTS_TO_DISPLAY ) {
                    sb.appendHtmlConstant( "<span class='link-button'>...</span>" );
                    break;
                }
                if ( clickHandler != null ) {
                    sb.appendHtmlConstant( "<span class='link-button'>" );
                } else {
                    sb.appendHtmlConstant( "<span>" );
                }
                sb.appendEscaped( displayStringHandler.getDisplayString( valueListElement ) );
                sb.appendHtmlConstant( "</span>" );
                displayedElements++;
            }
        }
    }

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

        if ( !CollectionsUtils.isEmpty( valueList ) && 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, valueList );
                                break;
                            }
                        } else if ( eventType.equals( CLICK_EVENT ) ) {
                            // click
                            if ( clickHandler != null ) {
                                clickHandler.onClick( valueList );
                            }
                            break;
                        }
                    }
                    wrapper = wrapper.getNextSiblingElement();
                }
            }
        }
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/client/foundation/widget/table/cell/ClickableLimitedListDataCell.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