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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package org.litesoft.GWT.forms.client.components.nonpublic;

import org.litesoft.GWT.client.widgets.*;
import org.litesoft.GWT.forms.client.components.*;
import org.litesoft.uispecification.*;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;

public abstract class AbstractTextFieldWithPicker extends AbstractTextField
{
    protected DialogBox mPicker;
    private boolean mBlurring = false;
    private boolean mPicking = false;
    private PickerImageButton mPickerButton = null;
    private String mPickerButtonName;

    public AbstractTextFieldWithPicker( String pTooltip, boolean pMixedSizeChars, int pWidth, //
                                        String pPickerButtonName, DialogBox pPicker )
    {
        super( pTooltip, pMixedSizeChars, pWidth, pWidth + 1 );
        mPickerButtonName = pPickerButtonName;
        (mPicker = pPicker).addCloseHandler( new CloseHandler<PopupPanel>()
        {
            public void onClose( CloseEvent<PopupPanel> event )
            {
                pickerClosed( event.isAutoClosed() );
            }
        } );
    }

    protected int buildComponentPanel( String pLabel, UiFont pLabelFont )
    {
        int zUniqueID = super.buildComponentPanel( pLabel, pLabelFont );
        mInnerHTMLpanel.add( (mPickerButton = createPickerButton()), "cal_" + zUniqueID );
        return zUniqueID;
    }

    protected String buildInputRow( String pTrId, String pInputId, int pUniqueID )
    {
        return buildInputRowWithOptionalCell( pTrId, pInputId, "<td><div class='litesoft-PickerButtonContainer' id='cal_" + pUniqueID + "'></div></td>" );
    }

    protected PickerImageButton createPickerButton()
    {
        final PickerImageButton button = new PickerImageButton( mPickerButtonName, null );

        button.addClickHandler( new ClickHandler()
        {
            public void onClick( ClickEvent event )
            {
                int absoluteLeft = button.getAbsoluteLeft();
                int absoluteTop = button.getAbsoluteTop();

                Object currentValue = getCurrentValue();
                showPicker( absoluteLeft, absoluteTop, currentValue, mLastValidInternalFormValue );
            }
        } );

        button.setTabIndex( -1 );

        button.addFocusHandler( this );
        button.addBlurHandler( this );

        return button;
    }

    public void setEnabled( boolean pEnabled )
    {
        if ( mPickerButton != null )
        {
            mPickerButton.setEnabled( pEnabled );
        }
        super.setEnabled( pEnabled );
    }

    protected void setCursor()
    {
        TextBox textfield = getTextBox();
        textfield.setFocus( true );
        int length = textfield.getText().length();
        textfield.setCursorPos( length );
    }

    protected void setTextValueFromPicker( String pFromPicker )
    {
        if ( !getCurrentText().equals( pFromPicker ) )
        {
            setCurrentText( pFromPicker );
            fireChangeListeners();
        }
    }

    protected void fireChangeListeners()
    {
        mInternalFormValid = false;
        super.fireChangeListeners();
    }

    private boolean mInternalFormValid = false;
    private Object mInternalFormValue = null;
    private Object mLastValidInternalFormValue = null;

    abstract protected Object parseNotEmptyTextFieldToInternalForm( String pText, Object pLastValidInternalFormValue )
            throws IllegalArgumentException;

    public Object getCurrentValue()
    {
        if ( !mInternalFormValid )
        {
            Object zInternalFormValue = null;
            String currentText = getCurrentText();
            if ( currentText.length() != 0 )
            {
                try
                {
                    zInternalFormValue = parseNotEmptyTextFieldToInternalForm( currentText, mLastValidInternalFormValue );
                }
                catch ( IllegalArgumentException e )
                {
                    return new FormTextInputErrorValue( currentText, e.getMessage() );
                }
            }
            mLastValidInternalFormValue = mInternalFormValue = zInternalFormValue;
            mInternalFormValid = true;
        }
        return mInternalFormValue;
    }

    public void setCurrentValue( Object pNewValue )
    {
        if ( pNewValue instanceof String )
        {
            mInternalFormValid = false;
            setCurrentText( pNewValue.toString() ); // this line and
            getCurrentValue(); // .................... this one are are doing reformatting/validation!
            return;
        }
        mLastValidInternalFormValue = mInternalFormValue = pNewValue;
        mInternalFormValid = true;
        setCurrentText( (pNewValue == null) ? "" : pNewValue.toString() );
    }

    private void show( String pMsg, Object pSender )
    {
        if ( LOGGER.trace.isEnabled() )
        {
            LLshow( pMsg + "(" + simpleClassname( pSender ) + ")" );
        }
    }

    protected void show( String pMsg, boolean pParam1 )
    {
        if ( LOGGER.trace.isEnabled() )
        {
            LLshow( pMsg + "(" + pParam1 + ")" );
        }
    }

    protected void show( String pMsg )
    {
        if ( LOGGER.trace.isEnabled() )
        {
            LLshow( pMsg );
        }
    }

    private void LLshow( String pMsg )
    {
        LOGGER.trace.log( simpleClassname( this ) + " - " + pMsg + "  [" + mBlurring + ":Blurring-Picking:" + mPicking + "]" );
    }

    public void publishAnyPendingChanges()
    {
        show( "publishAnyPendingChanges" );
        if ( hidePicker() || mBlurring )
        {
            mPicking = false;
            processDefferredBlur();
        }
        super.publishAnyPendingChanges();
    }

    @Override public void onFocus( FocusEvent event ) // TextBox / Button
    {
        show( "onFocus", event.getSource() );
        if ( mBlurring )
        {
            mBlurring = false;
        }
        else
        {
            super.onFocus( event );
        }
    }

    @Override public void onBlur( BlurEvent event ) // TextBox / Button
    {
        show( "onLostFocus", event.getSource() );
        mBlurring = true;
        defferBlur();
    }

    protected void processDefferredBlur()
    {
        show( "defferredBlur" );
        if ( mBlurring && !mPicking )
        {
            mBlurring = false;
            // Attempt to reformat
            Object value = getCurrentValue();
            if ( !(value instanceof String) && !(value instanceof FormTextInputErrorValue) )
            {
                setCurrentValue( value );
            }
            super.onBlur( null );
        }
    }

    protected void defferBlur()
    {
        DeferredCommand.addCommand( new Command()
        {
            public void execute()
            {
                processDefferredBlur();
            }
        } );
    }

    protected void showPicker( int pAbsoluteLeft, int pAbsoluteTop, Object pCurrentValue, Object pLastValidInternalFormValue )
    {
        show( "showPicker" );
        mBlurring = mPicking = true;
        setFocus();
        mPicker.setPopupPositionAndShow( new PopupPanelVisiblePositionCallBack( mPicker, //
                                                                                pAbsoluteLeft, pAbsoluteTop ) );
    }

    protected boolean hidePicker()
    {
        show( "hidePicker" );
        boolean rv = mPicking;
        if ( mPicking )
        {
            mPicker.hide();
        }
        return rv;
    }

    protected void pickerClosed( boolean autoClosed )
    {
        show( "pickerClosed", autoClosed );
        mPicking = false;
        defferBlur();
        if ( !autoClosed )
        {
            setCursor();
        }
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/forms/client/components/nonpublic/AbstractTextFieldWithPicker.java

Diff revisions: vs.
Revision Author Commited Message
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000