forked from kiegroup/guvnor
/
CEPWindowOperatorsDropdown.java
238 lines (206 loc) · 9.17 KB
/
CEPWindowOperatorsDropdown.java
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
/*
* Copyright 2011 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.guvnor.client.asseteditor.drools.modeldriven.ui;
import java.util.List;
import org.drools.guvnor.client.asseteditor.drools.modeldriven.HumanReadable;
import org.drools.guvnor.client.messages.Constants;
import org.drools.guvnor.client.resources.OperatorsCss;
import org.drools.guvnor.client.resources.OperatorsResource;
import org.drools.ide.common.client.modeldriven.SuggestionCompletionEngine;
import org.drools.ide.common.client.modeldriven.brl.HasCEPWindow;
import org.drools.ide.common.shared.SharedConstants;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
/**
* Drop-down Widget for Operators including supplementary controls for CEP
* operator parameters
*/
public class CEPWindowOperatorsDropdown extends Composite
implements
HasValueChangeHandlers<OperatorSelection> {
private static final Constants constants = ((Constants) GWT.create( Constants.class ));
private static final OperatorsResource resources = GWT.create( OperatorsResource.class );
private static final OperatorsCss css = resources.operatorsCss();
private List<String> operators = SuggestionCompletionEngine.getCEPWindowOperators();
private ListBox box;
private HorizontalPanel parametersContainer = new HorizontalPanel();
private HorizontalPanel windowContainer = new HorizontalPanel();
private boolean isReadOnly = false;
protected HasCEPWindow hcw;
//Parameter value defining the server-side class used to generate DRL for CEP operator parameters (key is in droolsjbpm-ide-common)
private static final String CEP_OPERATOR_PARAMETER_GENERATOR = "org.drools.ide.common.server.util.CEPWindowOperatorParameterDRLBuilder";
public CEPWindowOperatorsDropdown() {
windowContainer.setStylePrimaryName( css.container() );
initWidget( windowContainer );
}
public CEPWindowOperatorsDropdown(HasCEPWindow hcw,
boolean isReadOnly) {
this();
this.isReadOnly = isReadOnly;
setCEPWindow( hcw );
}
public void setCEPWindow(HasCEPWindow hcw) {
this.hcw = hcw;
windowContainer.clear();
windowContainer.add( getDropDown() );
windowContainer.add( getOperatorExtension() );
}
/**
* Gets the index of the currently-selected item.
*
* @return
*/
public int getSelectedIndex() {
return box.getSelectedIndex();
}
/**
* Gets the value associated with the item at a given index.
*
* @param index
* @return
*/
public String getValue(int index) {
return box.getValue( index );
}
//Additional widget for CEP Window operator parameter
private Widget getOperatorExtension() {
parametersContainer.setStylePrimaryName( css.container() );
return parametersContainer;
}
//Hide\display the additional CEP widget is appropriate
private void operatorChanged(OperatorSelection selection) {
parametersContainer.clear();
String operator = selection.getValue();
if ( SuggestionCompletionEngine.isCEPWindowOperatorTime( operator ) ) {
AbstractRestrictedEntryTextBox txt = new CEPTimeParameterTextBox( hcw.getWindow(),
1 );
initialiseTextBox( txt );
} else if ( SuggestionCompletionEngine.isCEPWindowOperatorLength( operator ) ) {
AbstractRestrictedEntryTextBox txt = new CEPLengthParameterTextBox( hcw.getWindow(),
1 );
initialiseTextBox( txt );
} else {
parametersContainer.setVisible( false );
hcw.getWindow().clearParameters();
}
}
private void initialiseTextBox(AbstractRestrictedEntryTextBox txt) {
String key = String.valueOf( 1 );
String value = hcw.getWindow().getParameter( key );
if ( value == null ) {
value = "";
hcw.getWindow().setParameter( key,
value );
}
if ( !txt.isValidValue( value ) ) {
value = "";
hcw.getWindow().setParameter( key,
value );
}
txt.setText( value );
txt.setEnabled( !isReadOnly );
parametersContainer.add( txt );
parametersContainer.setVisible( true );
hcw.getWindow().setParameter( SharedConstants.OPERATOR_PARAMETER_GENERATOR,
CEP_OPERATOR_PARAMETER_GENERATOR );
}
//Actual drop-down
private Widget getDropDown() {
String selected = "";
String selectedText = "";
box = new ListBox();
box.setEnabled( !isReadOnly );
box.addItem( constants.noCEPWindow(),
"" );
for ( int i = 0; i < operators.size(); i++ ) {
String op = operators.get( i );
box.addItem( HumanReadable.getOperatorDisplayName( op ),
op );
if ( op.equals( hcw.getWindow().getOperator() ) ) {
selected = op;
selectedText = HumanReadable.getOperatorDisplayName( op );
box.setSelectedIndex( i + 1 );
}
}
selectItem( hcw.getWindow().getOperator() );
//Fire event to ensure parent Widgets correct their state depending on selection
final HasValueChangeHandlers<OperatorSelection> source = this;
final OperatorSelection selection = new OperatorSelection( selected,
selectedText );
Scheduler.get().scheduleFinally( new Command() {
public void execute() {
operatorChanged( selection );
ValueChangeEvent.fire( source,
selection );
}
} );
//Signal parent Widget whenever a change happens
box.addChangeHandler( new ChangeHandler() {
public void onChange(ChangeEvent event) {
String selected = box.getValue( box.getSelectedIndex() );
String selectedText = box.getItemText( box.getSelectedIndex() );
OperatorSelection selection = new OperatorSelection( selected,
selectedText );
operatorChanged( selection );
ValueChangeEvent.fire( source,
selection );
}
} );
return box;
}
/**
* Allow parent Widgets to register for events when the operator changes
*/
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<OperatorSelection> handler) {
return addHandler( handler,
ValueChangeEvent.getType() );
}
/**
* Select a given item in the drop-down
*
* @param operator
* The DRL operator, not the HumanReadable form
*/
public void selectItem(String operator) {
String currentOperator = box.getValue( box.getSelectedIndex() );
if ( currentOperator.equals( operator ) ) {
return;
}
for ( int i = 0; i < box.getItemCount(); i++ ) {
String op = box.getValue( i );
if ( op.equals( operator ) ) {
box.setSelectedIndex( i );
break;
}
}
String selected = box.getValue( box.getSelectedIndex() );
String selectedText = box.getItemText( box.getSelectedIndex() );
OperatorSelection selection = new OperatorSelection( selected,
selectedText );
operatorChanged( selection );
}
}