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
package org.litesoft.GWT.forms.client.components.factories.input;

import java.util.*;

import com.google.gwt.user.client.ui.*;
import org.litesoft.GWT.forms.client.components.factories.*;
import org.litesoft.GWT.forms.client.components.impls.input.*;
import org.litesoft.GWT.forms.client.nonpublic.*;
import org.litesoft.core.simpletypes.*;
import org.litesoft.rpcpassable.*;
import org.litesoft.ui.def.nonpublic.*;
import org.litesoft.ui.def.nonpublic.support.*;
import org.litesoft.uispecification.*;

public class UiComboBoxWidgetFactory implements InputWidgetFactory
{
    public Widget create( FormInstanceComponentHandler pComponentHandler, //
                          UiInputDef pUiDef, AttributeMetaData pMetaData,
                          boolean pHasHorizontalPeer )
    {
        ComboBoxMetaData zMD = (ComboBoxMetaData) pMetaData;

        String zLabelText = !pUiDef.isLabelLess() ? zMD.getExternalText() : null;
        UiFont zLabelFont = pUiDef.getLabelFont();
        String zTooltip = zMD.getTooltip();

        return new MyFormAdapter( pComponentHandler, zMD,
                                  createFormComboBox( zLabelText, zLabelFont, zTooltip ) ).init();
    }

    protected FormComboBox createFormComboBox( String pLabelText, UiFont pLabelFont, String pTooltip )
    {
        return new FormComboBox( pLabelText, pLabelFont, pTooltip );
    }

    private static final class MyFormAdapter extends AbstractFormRequireableAttributeAdapter
    {
        private FormComboBox mFC;
        private SimpleKeyValuePair mOptionZero;
        private SimpleKeyValuePair[] mValidOptions;
        private List<RPCpassable> mNewValidOptions = new ArrayList<RPCpassable>();
        private boolean mHasFocus = false;

        public MyFormAdapter( FormInstanceComponentHandler pComponentHandler, //
                              ComboBoxMetaData pMD, FormComboBox pFC )
        {
            super( pComponentHandler, pMD, pMD, pFC );
            mFC = pFC;
            mFC.addItem( mOptionZero = pMD.getNoSendOptionZero() );
            mFC.addItems( mValidOptions = pMD.getValidOptions() );
        }

        public void updateValidOptions( RPCpassable[] pValidOptions )
        {
            convertOptions( pValidOptions );
            if ( !mHasFocus || mFC.areOptionsAlwaysUpdatable() )
            {
                LLupdateValidOptions();
            }
        }

        private void convertOptions( RPCpassable[] pValidOptions )
        {
            mNewValidOptions.clear();
            if ( pValidOptions != null )
            {
                for ( RPCpassable zValidOption : pValidOptions )
                {
                    SimpleKeyValuePair option = convertOption( zValidOption );
                    if ( option != null )
                    {
                        mNewValidOptions.add( option );
                    }
                }
            }
        }

        private void LLupdateValidOptions()
        {
            if ( !mNewValidOptions.isEmpty() )
            {
                // save the currently select & null it if it is the default option zero
                SimpleKeyValuePair selected = convertOption( mFC.getSelectedKeyValuePair() );

                mFC.setSelectedIndex( -1 );
                
                // Remove all the old options
                for ( SimpleKeyValuePair zValuePair : mValidOptions )
                {
                    mFC.removeItem( zValuePair );
                }
                // Add all the new ones & note if old current selected was found
                mValidOptions = new SimpleKeyValuePair[mNewValidOptions.size()];
                boolean found = false;
                for ( int i = 0; i < mNewValidOptions.size(); i++ )
                {
                    SimpleKeyValuePair option = (SimpleKeyValuePair) mNewValidOptions.get( i );
                    found |= option.equals( selected );
                    mFC.addItem( mValidOptions[i] = option );
                }
                mNewValidOptions.clear();

                setComponentCurrentValue( found ? selected : null );
            }
        }

        protected Object getComponentCurrentValue()
        {
            Object rv = super.getComponentCurrentValue();
            return !areEqual( rv, mOptionZero ) ? rv : null;
        }

        protected void setComponentCurrentValue( Object pCurrentValue )
        {
            if ( pCurrentValue == null )
            {
                pCurrentValue = mOptionZero;
            }
            super.setComponentCurrentValue( pCurrentValue );
        }

        protected RPCpassable convertComponentValueToSendable( Object pValue )
        {
            return !areEqual( pValue, mOptionZero ) ? (RPCpassable) pValue : null;
        }

        protected Object convertSendableToComponentValue( RPCpassable pValue )
        {
            return convertOption( pValue );
        }

        protected String convertComponentValueToString( Object pValue )
        {
            if ( pValue instanceof SimpleKeyValuePair )
            {
                return "@" + ((SimpleKeyValuePair) pValue).getKey();
            }
            return (pValue == null) ? "null" : "?" + pValue;
        }

        protected Object convertStringToComponentValue( String pValue )
        {
            if ( (pValue == null) || pValue.equals( "null" ) )
            {
                return null;
            }
            return pValue.startsWith( "@" ) ? pValue.substring( 1 ) : pValue;
        }

        public void focusOccured()
        {
            super.focusOccured();
            mHasFocus = true;
        }

        public void blurOccurred()
        {
            mHasFocus = false;
            LLupdateValidOptions();
            super.blurOccurred();
        }

        private SimpleKeyValuePair convertOption( RPCpassable pValue )
        {
            if ( pValue == null )
            {
                return null;
            }
            SimpleKeyValuePair rv = (pValue instanceof SimpleKeyValuePair) ? //
                                    (SimpleKeyValuePair) pValue : //
                                    new StringKeyValuePair( pValue.toString() );

            return !areEqual( rv, mOptionZero ) ? rv : null;
        }
    }
}

Commits for litesoft/trunk/Java/GWT/OldClient/src/org/litesoft/GWT/forms/client/components/factories/input/UiComboBoxWidgetFactory.java

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