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.foundation.client.widget.input.fieldsupport;

import org.litesoft.externalization.shared.*;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.ui.*;
import com.temp.foundation.client.widget.*;
import com.temp.foundation.client.widget.input.support.*;
import com.temp.common.shared.utils.*;

@SuppressWarnings("deprecation")
public class CompleteInputFieldAccessor<T> extends InputFieldAccessor<T> {
    private final E13nResolver e13nResolver;
    private final Widget labelLabel;
    private final Widget extendedLabel;
    private final Widget exampleLabel;
    private final Widget errorLabel;
    private final HelpWidget helpWidget;
    private final Boolean[] focusableFocused;
    private final Focusable focusable;
    private final boolean anyFocusTracking;
    private FocusChangedListener focusChangedListener;

    public CompleteInputFieldAccessor( E13nResolver e13nResolver, TextLabel labelLabel, TextLabel extendedLabel, InputFieldAccessor<T> inputFieldAccessor,
                                       HelpWidget helpWidget, TextLabel exampleLabel, TextLabel errorLabel, Focusable... additionalFocusables ) {
        super( inputFieldAccessor.getInput(), inputFieldAccessor.getValidator(), inputFieldAccessor.getValueAdapter(), inputFieldAccessor.getInputWidget(),
               append( inputFieldAccessor.getFocusables(), additionalFocusables ) );
        this.e13nResolver = e13nResolver;
        this.labelLabel = labelLabel;
        this.extendedLabel = extendedLabel;
        this.exampleLabel = exampleLabel;
        this.errorLabel = errorLabel;
        this.helpWidget = helpWidget;
        focusableFocused = new Boolean[focusables.length];
        Focusable firstFocusable = null;
        boolean anyFocusTracking = false;
        for ( int i = 0; i < focusables.length; i++ ) {
            Focusable focusable = focusables[i];
            if ( focusable != null ) {
                if ( firstFocusable == null ) {
                    firstFocusable = focusable;
                }
                if ( registerFocusTracking( i, focusable ) ) {
                    focusableFocused[i] = false; // Not currently Focused!
                    anyFocusTracking = true;
                }
            }
        }
        this.focusable = firstFocusable;
        this.anyFocusTracking = anyFocusTracking;
    }

    private boolean registerFocusTracking( int index, Focusable focusable ) {
        if ( focusable instanceof HasAllFocusHandlers ) {
            createFocusHandler( index, (HasAllFocusHandlers) focusable );
            return true;
        }
        if ( focusable instanceof SourcesFocusEvents ) {
            createFocusListener( index, (SourcesFocusEvents) focusable );
            return true;
        }
        return false;
    }

    public E13nResolver getE13nResolver() {
        return e13nResolver;
    }

    public Widget getLabelLabel() {
        return labelLabel;
    }

    public Widget getExtendedLabel() {
        return extendedLabel;
    }

    public Widget getExampleLabel() {
        return exampleLabel;
    }

    public Widget getErrorLabel() {
        return errorLabel;
    }

    public HelpWidget getHelpWidget() {
        return helpWidget;
    }

    public void init( boolean enabled, FocusChangedListener focusChangedListener ) {
        setEnabled( enabled );
        this.focusChangedListener = focusChangedListener;
    }

    /**
     * Return if the Field has Focus (or null indicating that the Focus state is not tracked)
     */
    public Boolean isFocused() {
        if ( !anyFocusTracking ) {
            return null;
        }
        if ( isEnabled() ) {
            for ( Boolean focused : focusableFocused ) {
                if ( Boolean.TRUE.equals( focused ) ) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Pull the Focus to this Field (return false if the field is "thought" to be currently un-focusable, e.g. has no Focusable components or disabled)
     */
    public boolean setFocused() {
        if ( isEnabled() && (focusable != null) ) {
            focusable.setFocus( true );
            // We "should" get the appropriate events...
            return true;
        }
        return false;
    }

    private void focusChange( int index, boolean focused ) {
        if ( focusableFocused[index] != focused ) {
            focusableFocused[index] = focused;
            if ( focusChangedListener != null ) {
                focusChangedListener.focusChanged();
            }
        }
    }

    private void createFocusListener( final int index, SourcesFocusEvents focusable ) {
        focusable.addFocusListener( new FocusListener() {
            @Override
            public void onFocus( Widget sender ) {
                focusChange( index, true );
            }

            @Override
            public void onLostFocus( Widget sender ) {
                focusChange( index, false );
            }
        } );
    }

    private void createFocusHandler( int index, HasAllFocusHandlers focusable ) {
        FocusBlurHandler handler = new FocusBlurHandler( index );
        focusable.addFocusHandler( handler );
        focusable.addBlurHandler( handler );
    }

    private class FocusBlurHandler implements FocusHandler,
                                              BlurHandler {
        private final int index;

        public FocusBlurHandler( int index ) {
            this.index = index;
        }

        @Override
        public void onFocus( FocusEvent event ) {
            focusChange( index, true );
        }

        @Override
        public void onBlur( BlurEvent event ) {
            focusChange( index, false );
        }
    }

    private static Focusable[] append( Focusable[] focusables1, Focusable[] focusables2 ) {
        if ( CollectionsUtils.isEmpty( focusables2 ) ) {
            return focusables1;
        }
        if ( CollectionsUtils.isEmpty( focusables1 ) ) {
            return focusables2;
        }
        Focusable[] merged = new Focusable[focusables1.length + focusables2.length];
        System.arraycopy( focusables1, 0, merged, 0, focusables1.length );
        System.arraycopy( focusables2, 0, merged, focusables1.length, focusables2.length );
        return merged;
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/foundation/client/widget/input/fieldsupport/CompleteInputFieldAccessor.java

Diff revisions: vs.
Revision Author Commited Message
965 GeorgeS picture GeorgeS Fri 01 Aug, 2014 03:20:35 +0000

!