Skip to content

Package: AbstractTextControl

AbstractTextControl

nameinstructionbranchcomplexitylinemethod
AbstractTextControl()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
bindValue()
M: 0 C: 32
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
createTextWidget(Composite)
M: 7 C: 28
80%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 6
75%
M: 0 C: 1
100%
createToolTip(int, String, String)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
createTooltipBinding(UpdateValueStrategy, UpdateValueStrategy)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
customizeText(Text)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
dispose()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
fillControlComposite(Composite)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getControlsForTooltip()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getText()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getTextWidgetLayoutData()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getTextWidgetStyle()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isDisableRenderedAsEditable()
M: 16 C: 24
60%
M: 4 C: 2
33%
M: 3 C: 1
25%
M: 3 C: 7
70%
M: 0 C: 1
100%
setEditable(boolean)
M: 4 C: 8
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
setUnvalidatedString(String)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
updateValidationColor(Color)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eugen Neufeld - initial API and implementation
13: *
14: *******************************************************************************/
15: package org.eclipse.emf.ecp.edit.internal.swt.controls;
16:
17: import java.util.Set;
18:
19: import org.eclipse.core.databinding.Binding;
20: import org.eclipse.core.databinding.UpdateValueStrategy;
21: import org.eclipse.core.databinding.observable.value.IObservableValue;
22: import org.eclipse.core.runtime.IStatus;
23: import org.eclipse.emf.databinding.EMFUpdateValueStrategy;
24: import org.eclipse.emf.ecp.view.template.model.VTStyleProperty;
25: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
26: import org.eclipse.emf.ecp.view.template.style.textControlEnablement.model.VTTextControlEnablementStyleProperty;
27: import org.eclipse.emf.edit.command.SetCommand;
28: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
29: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
30: import org.eclipse.swt.SWT;
31: import org.eclipse.swt.graphics.Color;
32: import org.eclipse.swt.layout.GridData;
33: import org.eclipse.swt.widgets.Composite;
34: import org.eclipse.swt.widgets.Control;
35: import org.eclipse.swt.widgets.Text;
36: import org.eclipse.swt.widgets.ToolTip;
37:
38: /**
39: * This abstract class is used as a common superclass for all widgets that use a {@link Text} widget.
40: *
41: * @author Eugen Neufeld
42: */
43: @Deprecated
44: public abstract class AbstractTextControl extends SingleControl {
45:
46:         /**
47:          * The {@link Text} holding the value.
48:          */
49:         private Text text;
50:         private boolean doVerify;
51:
52:         @Override
53:         protected void updateValidationColor(Color color) {
54:                 text.setBackground(color);
55:         }
56:
57:         @Override
58:         protected void fillControlComposite(Composite composite) {
59:                 doVerify = false;
60:                 createTextWidget(composite);
61:                 addControlDecoration(text);
62:         }
63:
64:         private void createTextWidget(Composite composite) {
65:                 text = new Text(composite, getTextWidgetStyle());
66:                 text.setLayoutData(getTextWidgetLayoutData());
67:
68:•                if (getFirstStructuralFeature().isUnsettable()) {
69:                         text.setMessage(LocalizationServiceHelper.getString(getClass(),
70:                                 DepricatedControlMessageKeys.AbstractTextControl_Unset));
71:                 }
72:                 text.setData(CUSTOM_VARIANT, getTextVariantID());
73:                 customizeText(text);
74:
75:         }
76:
77:         /**
78:          * Creates a {@link ToolTip}.
79:          *
80:          * @param style the SWT style
81:          * @param text the text
82:          * @param message the message
83:          * @return a {@link ToolTip}
84:          */
85:         protected ToolTip createToolTip(int style, String text, String message) {
86:                 final ToolTip toolTip = new ToolTip(this.text.getShell(), style);
87:                 toolTip.setText(text);
88:                 toolTip.setMessage(message);
89:                 return toolTip;
90:
91:         }
92:
93:         /**
94:          * This method allows to set custom values to the text field, e.g. a tooltip or a validation.
95:          *
96:          * @param text the text widget to customize
97:          */
98:         protected void customizeText(Text text) {
99:
100:         }
101:
102:         /**
103:          * The VariantId to use e.g. for RAP
104:          *
105:          * @return the String identifying this control
106:          */
107:         protected abstract String getTextVariantID();
108:
109:         /**
110:          * The LayoutData for the created {@link Text} widget. Can be changed by the concrete classes.
111:          *
112:          * @return the {@link GridData} to apply
113:          */
114:         protected GridData getTextWidgetLayoutData() {
115:                 return new GridData(SWT.FILL, SWT.FILL, true, true);
116:         }
117:
118:         /**
119:          * The style to apply to the text widget. This can be changed by the concrete classes.
120:          *
121:          * @return the style to apply
122:          */
123:         protected int getTextWidgetStyle() {
124:                 return SWT.SINGLE | SWT.BORDER;
125:         }
126:
127:         @Override
128:         protected Control[] getControlsForTooltip() {
129:                 // return new Control[] { text };
130:                 return new Control[0];
131:         }
132:
133:         /**
134:          * {@inheritDoc}
135:          */
136:         @Override
137:         public void setEditable(boolean isEditable) {
138:•                if (isDisableRenderedAsEditable()) {
139:                         text.setEditable(isEditable);
140:                 }
141:                 text.setEnabled(isEditable);
142:         }
143:
144:         private boolean isDisableRenderedAsEditable() {
145:
146:                 final VTViewTemplateProvider vtViewTemplateProvider = getViewModelContext()
147:                         .getService(VTViewTemplateProvider.class);
148:•                if (vtViewTemplateProvider == null) {
149:                         return false;
150:                 }
151:                 final Set<VTStyleProperty> styleProperties = vtViewTemplateProvider
152:                         .getStyleProperties(getControl(), getViewModelContext());
153:•                for (final VTStyleProperty styleProperty : styleProperties) {
154:•                        if (VTTextControlEnablementStyleProperty.class.isInstance(styleProperty)) {
155:                                 return VTTextControlEnablementStyleProperty.class.cast(styleProperty).isRenderDisableAsEditable();
156:                         }
157:                 }
158:                 return false;
159:         }
160:
161:         @Override
162:         public Binding bindValue() {
163:                 final IObservableValue value = WidgetProperties.text(SWT.FocusOut).observe(text);
164:                 final TargetToModelUpdateStrategy targetToModelUpdateStrategy = new TargetToModelUpdateStrategy();
165:                 final ModelToTargetUpdateStrategy modelToTargetUpdateStrategy = new ModelToTargetUpdateStrategy();
166:                 final Binding binding = getDataBindingContext().bindValue(value, getModelValue(),
167:                         targetToModelUpdateStrategy,
168:                         modelToTargetUpdateStrategy);
169:
170:                 createTooltipBinding(targetToModelUpdateStrategy, modelToTargetUpdateStrategy);
171:
172:                 return binding;
173:         }
174:
175:         /**
176:          * Creates a tooltip binding for this control.
177:          *
178:          * @param targetToModel the {@link UpdateValueStrategy} from target to Model
179:          * @param modelToTarget the {@link UpdateValueStrategy} from model to target
180:          * @return the created {@link Binding}
181:          */
182:         protected Binding createTooltipBinding(UpdateValueStrategy targetToModel, UpdateValueStrategy modelToTarget) {
183:                 final IObservableValue toolTip = WidgetProperties.tooltipText().observe(text);
184:                 return getDataBindingContext().bindValue(toolTip, getModelValue(),
185:                         targetToModel,
186:                         modelToTarget);
187:         }
188:
189:         /**
190:          * Sets the content of the SWT text control to the given string without calling {@link #validateString(String)}.
191:          *
192:          * @param string
193:          * the content of the SWT Text control
194:          */
195:         protected void setUnvalidatedString(String string) {
196:                 final boolean oldDoVerify = doVerify;
197:                 doVerify = false;
198:                 text.setText(string);
199:                 doVerify = oldDoVerify;
200:         }
201:
202:         @Override
203:         public void dispose() {
204:                 text.dispose();
205:                 super.dispose();
206:         }
207:
208:         /**
209:          * @return the text
210:          */
211:         public Text getText() {
212:                 return text;
213:         }
214:
215:         /**
216:          * An {@link EMFUpdateConvertValueStrategy} that encapsulates the converting
217:          * of the actual value. Use this class to provide a specific context
218:          * for the conversion of the value, but likewise enable it clients to modify
219:          * the conversion behavior.
220:          *
221:          * @author emueller
222:          *
223:          */
224:         class EMFUpdateConvertValueStrategy extends EMFUpdateValueStrategy {
225:
226:                 /**
227:                  * Constructor.
228:                  */
229:                 EMFUpdateConvertValueStrategy() {
230:                         super();
231:                 }
232:
233:                 /*
234:                  * (non-Javadoc)
235:                  * @see org.eclipse.core.databinding.UpdateValueStrategy#convert(java.lang.Object)
236:                  */
237:                 @Override
238:                 public Object convert(Object value) {
239:                         return convertValue(value);
240:                 }
241:
242:                 protected Object convertValue(Object value) {
243:                         return super.convert(value);
244:                 }
245:         }
246:
247:         protected class ModelToTargetUpdateStrategy extends EMFUpdateConvertValueStrategy {
248:
249:                 @Override
250:                 public Object convert(Object value) {
251:                         // controlDecoration.hide();
252:                         // updateValidationColor(null);
253:                         return convertValue(value);
254:                 }
255:
256:         }
257:
258:         protected class TargetToModelUpdateStrategy extends EMFUpdateConvertValueStrategy {
259:
260:                 @Override
261:                 public IStatus validateAfterGet(Object value) {
262:                         final IStatus status = super.validateAfterGet(value);
263:                         // if (status.getSeverity() == IStatus.ERROR) {
264:                         // controlDecoration.show();
265:                         // controlDecoration.setDescriptionText(status.getMessage());
266:                         // } else {
267:                         // controlDecoration.hide();
268:                         // controlDecoration.setDescriptionText(null);
269:                         // }
270:                         return status;
271:                 }
272:
273:                 /**
274:                  * {@inheritDoc}
275:                  */
276:                 @Override
277:                 public Object convert(Object value) {
278:                         try {
279:
280:                                 // controlDecoration.hide();
281:                                 // updateValidationColor(null);
282:                                 if ("".equals(value)) { //$NON-NLS-1$
283:                                         value = null;
284:                                 }
285:                                 if (value == null && getFirstStructuralFeature().isUnsettable()) {
286:                                         return SetCommand.UNSET_VALUE;
287:                                 }
288:
289:                                 final Object convertedValue = convertValue(value);
290:
291:                                 return convertedValue;
292:
293:                         } catch (final IllegalArgumentException e) {
294:                                 // controlDecoration.show();
295:                                 // updateValidationColor(getText().getShell().getDisplay().getSystemColor(SWT.COLOR_RED));
296:                                 // controlDecoration.setDescriptionText(ControlMessages.AbstractTextControl_InvalidInputSpace
297:                                 // + e.getLocalizedMessage());
298:                                 throw e;
299:                         }
300:                 }
301:         }
302: }