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
package com.temp.client.foundation.widget.input.support;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.*;
import com.google.gwt.user.client.Timer;
import com.temp.shared.utils.*;

import java.util.*;

public class TextBoxBaseChangeListenableHelper {
    public interface Participant {
        public HandlerRegistration addFocusHandler(FocusHandler handler);

        public HandlerRegistration addBlurHandler(BlurHandler handler);

        String getText();

        void fireChangeListeners();
    }

    public static final TextBoxBaseChangeListenableHelper INSTANCE = new TextBoxBaseChangeListenableHelper();

    private TextBoxBaseChangeListenableHelper() {
    }

    private class Control implements FocusHandler, BlurHandler {
        private final Participant participant;
        private final HandlerRegistration focus, blur;
        private boolean participantIsFocused = false;
        private String currentText = "";

        public Control(Participant participant) {
            this.participant = participant;
            focus = participant.addFocusHandler(this);
            blur = participant.addBlurHandler(this);
        }

        private void detach() {
            remove(focus);
            remove(blur);
        }

        private void remove(HandlerRegistration handler) {
            if (handler != null) {
                handler.removeHandler();
            }
        }

        @Override
        public void onFocus(FocusEvent event) {
            participantIsFocused = true;
            currentText = getCurrentText();
            setCurrentControl(this); // Enable polling
        }

        @Override
        public void onBlur(BlurEvent event) {
            clearCurrentControl(this); // Disable polling
            if (participantIsFocused) {
                checkForChange();
                participantIsFocused = false;
                currentText = "";
            }
        }

        private boolean participantTextChanged() {
            String value = getCurrentText();
            if (!currentText.equals(value)) {
                currentText = value;
                return true;
            }
            return false;
        }

        private String getCurrentText() {
            return StringUtils.deNull(participant.getText());
        }

        private void checkForChange() {
            if (participantIsFocused && participantTextChanged()) {
                participant.fireChangeListeners();
            }
        }
    }

    private Map<Participant, Control> participantControl = new IdentityHashMap<Participant, Control>();
    private Timer timer = null;
    private Control currentControl;

    public void attach(Participant participant) {
        boolean wasEmpty = participantControl.isEmpty();
        participantControl.put(participant, new Control(participant));
        if (wasEmpty) {
            startTimer();
        }
    }

    public void detach(Participant participant) {
        Control control = participantControl.remove(participant);
        if (control != null) {
            control.detach();
            if (participantControl.isEmpty()) {
                stopTimer();
            }
        }
    }

    private void setCurrentControl(Control control) {
        currentControl = control;
    }

    private void clearCurrentControl(Control control) {
        if (control == currentControl) {
            setCurrentControl(null);
        }
    }

    private void startTimer() {
        stopTimer();
        timer = new Timer() {
            @Override
            public void run() {
                Control control = currentControl;
                if (control != null) {
                    control.checkForChange();
                }
            }
        };
        timer.scheduleRepeating(250);
    }

    private void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

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