Skip to main content
summaryrefslogtreecommitdiffstats
blob: ade5eb232b86380b299b8ecfa5eb8977a0e2ff94 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2013, 2015 EclipseSource and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    EclipseSource - initial API and implementation
 ******************************************************************************/
package org.eclipse.rap.addons.dropdown.demo;

import java.util.Arrays;

import org.eclipse.rap.addons.autosuggest.AutoSuggest;
import org.eclipse.rap.addons.autosuggest.DataProvider;
import org.eclipse.rap.addons.autosuggest.DataSource;
import org.eclipse.rap.addons.autosuggest.SuggestionSelectedListener;
import org.eclipse.rap.addons.dropdown.demo.data.KFZ;
import org.eclipse.rap.addons.dropdown.demo.scripts.CustomAutoSuggestClientListener;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.application.AbstractEntryPoint;
import org.eclipse.rap.rwt.scripting.ClientListener;
import org.eclipse.rap.rwt.widgets.WidgetUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;


public class AutoSuggestDemo extends AbstractEntryPoint {

  private Text text;
  private AutoSuggest autoSuggest;
  private DataSource de;
  private DataSource at;
  private boolean useCustomScripts = false;
  private boolean useAutoComplete = false;
  private DataSource currentDataSource;
  private Table table;

  @Override
  protected void createContents( Composite parent ) {
    createDataSources();
    setupWidgetDataWhiteList();
    Composite composite = new Composite( parent, SWT.NONE );
    composite.setLayout( new GridLayout( 3, false ) );
    createTextArea( composite );
    createAutoSuggest();
    createLocationArea( composite );
    createConfigArea( composite );
    createLogArea( composite );
    createLogger();
    createDisposeButton( composite );
  }

  private void createDataSources() {
    de = new DataSource();
    de.setDataProvider( new DataProvider<String[]>() {
      @Override
      public Iterable<String[]> getSuggestions() {
        return Arrays.asList( KFZ.DE );
      }
      @Override
      public String getValue( String[] suggestion ) {
        return suggestion[ 2 ] + " (" + suggestion[ 0 ] + ")";
      }
    } );
    at = new DataSource();
    at.setDataProvider( new DataProvider<String[]>() {
      @Override
      public Iterable<String[]> getSuggestions() {
        return Arrays.asList( KFZ.AT );
      }
      @Override
      public String getValue( String[] suggestion ) {
        return suggestion[ 1 ] + " (" + suggestion[ 0 ] + ")";
      }
    } );
    currentDataSource = de;
  }

  private void createTextArea( Composite parent ) {
    text = new Text( parent, SWT.BORDER );
    text.setData( RWT.CUSTOM_VARIANT, "dropdown" );
    text.setCursor( text.getDisplay().getSystemCursor( SWT.CURSOR_ARROW ) );
    GridData gridData = new GridData( 200, 23 );
    gridData.verticalAlignment = SWT.TOP;
    text.setLayoutData( gridData );
    text.setMessage( "City" );
    text.setFocus();
  }

  public void createAutoSuggest() {
    if( useCustomScripts ) {
      autoSuggest = new CustomAutoSuggest( text );
    } else {
      autoSuggest = new AutoSuggest( text );
    }
    autoSuggest.setDataSource( currentDataSource );
    autoSuggest.setAutoComplete( useAutoComplete );
  }

  private void createLocationArea( Composite parent ) {
    Group location = new Group( parent, SWT.NONE );
    location.setText( "Location" );
    GridData layoutData = new GridData( SWT.CENTER, SWT.FILL, false, true );
    location.setLayoutData( layoutData );
    layoutData.verticalSpan = 2;
    location.setLayout( new GridLayout( 1, true ) );
    final Button germany = new Button( location, SWT.RADIO );
    germany.setText( "Germany" );
    germany.setSelection( true );
    germany.addListener( SWT.Selection, new Listener() {
      @Override
      public void handleEvent( Event event ) {
        if( germany.getSelection() ) {
          currentDataSource = de;
          autoSuggest.setDataSource( de );
        }
      }
    } );
    final Button austria = new Button( location, SWT.RADIO );
    austria.setText( "Austria" );
    austria.addListener( SWT.Selection, new Listener() {
      @Override
      public void handleEvent( Event event ) {
        if( austria.getSelection() ) {
          currentDataSource = at;
          autoSuggest.setDataSource( at );
        }
      }
    } );
  }

  private void createConfigArea( Composite parent ) {
    Group location = new Group( parent, SWT.NONE );
    location.setText( "Config" );
    GridData layoutData = new GridData( SWT.CENTER, SWT.FILL, false, true );
    location.setLayoutData( layoutData );
    layoutData.verticalSpan = 2;
    location.setLayout( new GridLayout( 1, true ) );
    final Button autoComplete = new Button( location, SWT.CHECK );
    autoComplete.setText( "AutoComplete" );
    autoComplete.addListener( SWT.Selection, new Listener() {
      @Override
      public void handleEvent( Event event ) {
        useAutoComplete = autoComplete.getSelection();
        autoSuggest.setAutoComplete( useAutoComplete );
      }
    } );
    final Button customScripts = new Button( location, SWT.CHECK );
    customScripts.setText( "Custom Scripts" );
    customScripts.addListener( SWT.Selection, new Listener() {
      @Override
      public void handleEvent( Event event ) {
        autoSuggest.dispose();
        useCustomScripts = customScripts.getSelection();
        createAutoSuggest();
        createLogger();
      }
    } );
    customScripts.setEnabled( false ); // Broken in Demo due to Bug 415485
  }

  private void createLogArea( Composite parent ) {
    table = new Table( parent, SWT.BORDER | SWT.HIDE_SELECTION );
    new TableColumn( table, SWT.NONE ).setText( "KFZ" );
    new TableColumn( table, SWT.NONE ).setText( "Area" );
    table.getColumn( 0 ).setWidth( 200 );
    table.getColumn( 1 ).setWidth( 40 );
    GridData gridData = new GridData( 250, 60 );
    table.setLayoutData( gridData );
  }

  private void createLogger() {
    autoSuggest.addSelectionListener( new SuggestionSelectedListener() {
      @Override
      public void suggestionSelected() {
        TableItem item = new TableItem( table, SWT.NONE );
        item.setText( text.getText() );
        table.setTopIndex( table.indexOf( item ) );
      }
    } );
  }

  private void createDisposeButton( Composite parent ) {
    Button button = new Button( parent, SWT.PUSH );
    button.setText( "Dispose!" );
    button.addListener( SWT.Selection, new Listener() {
      @Override
      public void handleEvent( Event event ) {
        autoSuggest.setDataSource( at );
        text.dispose();
      }
    } );
  }

  private static void setupWidgetDataWhiteList() {
    WidgetUtil.registerDataKeys( "dropdown", "text", "data" );
  }

  private class CustomAutoSuggest extends AutoSuggest {

    public CustomAutoSuggest( Text text ) {
      super( text );
    }

    @Override
    protected ClientListener getAutoSuggestListener() {
      return CustomAutoSuggestClientListener.getInstance();
    }

  }

}

Back to the top