Skip to content

Package: NumberCellEditor$TargetToModelStrategy$1

NumberCellEditor$TargetToModelStrategy$1

nameinstructionbranchcomplexitylinemethod
handleResult(int)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 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: package org.eclipse.emf.ecp.edit.internal.swt.table;
15:
16: import java.text.DecimalFormat;
17: import java.text.ParseException;
18: import java.text.ParsePosition;
19: import java.util.Locale;
20:
21: import org.eclipse.core.databinding.DataBindingContext;
22: import org.eclipse.core.databinding.UpdateValueStrategy;
23: import org.eclipse.core.databinding.property.value.IValueProperty;
24: import org.eclipse.emf.databinding.EMFUpdateValueStrategy;
25: import org.eclipse.emf.ecore.EStructuralFeature;
26: import org.eclipse.emf.ecp.edit.internal.swt.Activator;
27: import org.eclipse.emf.ecp.edit.internal.swt.controls.NumericalHelper;
28: import org.eclipse.emf.ecp.edit.spi.ViewLocaleService;
29: import org.eclipse.emf.ecp.edit.spi.swt.table.StringBasedCellEditor;
30: import org.eclipse.emf.ecp.edit.spi.swt.util.ECPDialogExecutor;
31: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
32: import org.eclipse.emf.edit.command.SetCommand;
33: import org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleProvider;
34: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
35: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
36: import org.eclipse.jface.databinding.viewers.CellEditorProperties;
37: import org.eclipse.jface.dialogs.IDialogLabelKeys;
38: import org.eclipse.jface.dialogs.MessageDialog;
39: import org.eclipse.jface.resource.JFaceResources;
40: import org.eclipse.swt.SWT;
41: import org.eclipse.swt.graphics.Image;
42: import org.eclipse.swt.widgets.Composite;
43: import org.eclipse.swt.widgets.Text;
44:
45: /**
46: * A number cell Editor which displays numbers.
47: *
48: * @author Eugen Neufeld
49: *
50: */
51: public class NumberCellEditor extends StringBasedCellEditor {
52:
53:         /**
54:          * @author Jonas
55:          *
56:          */
57:         private final class TargetToModelStrategy extends EMFUpdateValueStrategy {
58:                 private final DataBindingContext databindingContext;
59:
60:                 /**
61:                  * @param databindingContext
62:                  */
63:                 private TargetToModelStrategy(DataBindingContext databindingContext) {
64:                         this.databindingContext = databindingContext;
65:                 }
66:
67:                 @Override
68:                 public Object convert(final Object value) {
69:                         final DecimalFormat format = NumericalHelper.setupFormat(getLocale(),
70:                                 getInstanceClass());
71:                         try {
72:                                 Number number = null;
73:                                 if (value == null) {
74:                                         number = NumericalHelper.getDefaultValue(getInstanceClass());
75:                                 } else {
76:                                         final ParsePosition pp = new ParsePosition(0);
77:                                         number = format.parse((String) value, pp);
78:                                         if (pp.getErrorIndex() != -1 || pp.getIndex() != ((String) value).length()) {
79:                                                 return revertToOldValue(value);
80:                                         }
81:                                         if (NumericalHelper.isInteger(getInstanceClass())) {
82:                                                 boolean maxValue = false;
83:                                                 final Class<?> instanceClass = getInstanceClass();
84:                                                 String formatedValue = ""; //$NON-NLS-1$
85:                                                 if (isOfClass(Integer.class, instanceClass) && Integer.MAX_VALUE == number.intValue()) {
86:                                                         maxValue = true;
87:                                                         formatedValue = format.format(Integer.MAX_VALUE);
88:                                                 } else if (isOfClass(Long.class, instanceClass) && Long.MAX_VALUE == number.longValue()) {
89:                                                         maxValue = true;
90:                                                         formatedValue = format.format(Long.MAX_VALUE);
91:                                                 }
92:
93:                                                 if (maxValue) {
94:                                                         getText().setText(formatedValue);
95:                                                         return NumericalHelper.numberToInstanceClass(number, getInstanceClass());
96:                                                 }
97:                                         }
98:                                 }
99:                                 String formatedNumber = ""; //$NON-NLS-1$
100:                                 if (number != null) {
101:                                         formatedNumber = format.format(number);
102:                                 }
103:                                 // if (number.toString().contains("E")
104:                                 // || ((String) value).matches("0*" + formatedNumber + "\\"
105:                                 // + format.getDecimalFormatSymbols().getDecimalSeparator() + "?0*")) {
106:                                 //
107:                                 // }
108:                                 // return revertToOldValue(value);
109:                                 getText().setText(formatedNumber);
110:                                 if (formatedNumber.length() == 0) {
111:                                         return null;
112:                                 }
113:                                 return NumericalHelper.numberToInstanceClass(format.parse(formatedNumber), getInstanceClass());
114:                         } catch (final ParseException ex) {
115:                                 return revertToOldValue(value);
116:                         }
117:                 }
118:
119:                 private <T extends Number> boolean isOfClass(Class<T> clazz, Class<?> toCheck) {
120:                         try {
121:                                 return clazz.isAssignableFrom(toCheck)
122:                                         || clazz.getField("TYPE").get(null).equals(toCheck); //$NON-NLS-1$
123:                         } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException ex) {
124:                                 Activator.logException(ex);
125:                                 return false;
126:                         }
127:                 }
128:
129:                 private Object revertToOldValue(final Object value) {
130:
131:                         if (eStructuralFeature.getDefaultValue() == null && (value == null || value.equals(""))) { //$NON-NLS-1$
132:                                 return null;
133:                         }
134:
135:                         final Object result = null;
136:
137:                         final MessageDialog messageDialog = new MessageDialog(text.getShell(),
138:                                 LocalizationServiceHelper.getString(getClass(), TableMessageKeys.NumberCellEditor_InvalidNumber),
139:                                 null, LocalizationServiceHelper.getString(getClass(),
140:                                         TableMessageKeys.NumberCellEditor_NumberYouEnteredIsInvalid),
141:                                 MessageDialog.ERROR,
142:                                 new String[] { JFaceResources.getString(IDialogLabelKeys.OK_LABEL_KEY) }, 0);
143:
144:                         new ECPDialogExecutor(messageDialog) {
145:                                 @Override
146:                                 public void handleResult(int codeResult) {
147:
148:                                 }
149:                         }.execute();
150:
151:                         databindingContext.updateTargets();
152:
153:                         if (eStructuralFeature.isUnsettable()) {
154:                                 return SetCommand.UNSET_VALUE;
155:                         }
156:                         return result;
157:                 }
158:         }
159:
160:         private EStructuralFeature eStructuralFeature;
161:         @Deprecated
162:         private ViewLocaleService localeService;
163:         private EMFFormsLocaleProvider localeProvider;
164:
165:         /**
166:          * The constructor which only takes a parent composite.
167:          *
168:          * @param parent the {@link Composite} to use as a parent.
169:          */
170:         public NumberCellEditor(Composite parent) {
171:                 super(parent, SWT.RIGHT);
172:         }
173:
174:         /**
175:          * A constructor which takes a parent and the style to use, the style is ignored by this cell editor.
176:          *
177:          * @param parent the {@link Composite} to use as a parent
178:          * @param style the SWT style to set
179:          */
180:         public NumberCellEditor(Composite parent, int style) {
181:                 super(parent, style | SWT.RIGHT);
182:         }
183:
184:         @Override
185:         public IValueProperty getValueProperty() {
186:                 return CellEditorProperties.control().value(WidgetProperties.text(SWT.FocusOut));
187:         }
188:
189:         @Override
190:         public void instantiate(EStructuralFeature eStructuralFeature, ViewModelContext viewModelContext) {
191:                 super.instantiate(eStructuralFeature, viewModelContext);
192:                 this.eStructuralFeature = eStructuralFeature;
193:                 getControl().setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_edit_cellEditor_numberical"); //$NON-NLS-1$
194:                 localeService = viewModelContext.getService(ViewLocaleService.class);
195:                 localeProvider = viewModelContext.getService(EMFFormsLocaleProvider.class);
196:         }
197:
198:         @Override
199:         public String getFormatedString(Object value) {
200:                 if (value == null) {
201:                         setErrorMessage(LocalizationServiceHelper.getString(NumberCellEditor.class,
202:                                 TableMessageKeys.NumberCellEditor_ValueIsNull));
203:                         return ""; //$NON-NLS-1$
204:                 }
205:
206:                 final DecimalFormat format = NumericalHelper.setupFormat(getLocale(), getInstanceClass());
207:                 return format.format(value);
208:
209:                 // if (BigDecimal.class.isInstance(value)) {
210:                 // BigDecimal bigDecimal = (BigDecimal) value;
211:                 // bigDecimal.toPlainString();
212:                 // }
213:                 // return ((Number) value).toString();
214:         }
215:
216:         private Locale getLocale() {
217:                 if (localeService != null) {
218:                         return localeService.getLocale();
219:                 }
220:                 if (localeProvider != null) {
221:                         return localeProvider.getLocale();
222:                 }
223:                 return Locale.getDefault();
224:         }
225:
226:         @Override
227:         public int getColumnWidthWeight() {
228:                 return 50;
229:         }
230:
231:         @Override
232:         public UpdateValueStrategy getTargetToModelStrategy(final DataBindingContext databindingContext) {
233:                 return withPreSetValidation(eStructuralFeature, new TargetToModelStrategy(databindingContext));
234:         }
235:
236:         @Override
237:         public UpdateValueStrategy getModelToTargetStrategy(DataBindingContext databindingContext) {
238:                 return new EMFUpdateValueStrategy() {
239:                         @Override
240:                         public Object convert(Object value) {
241:                                 if (value == null) {
242:                                         return ""; //$NON-NLS-1$
243:                                 }
244:                                 final DecimalFormat format = NumericalHelper.setupFormat(getLocale(),
245:                                         getInstanceClass());
246:                                 return format.format(value);
247:                         }
248:                 };
249:         }
250:
251:         private Class<?> getInstanceClass() {
252:                 return eStructuralFeature.getEType().getInstanceClass();
253:         }
254:
255:         private Text getText() {
256:                 return text;
257:         }
258:
259:         @Override
260:         public void setEditable(boolean editable) {
261:                 if (getText() != null) {
262:                         getText().setEditable(editable);
263:                 }
264:         }
265:
266:         @Override
267:         public Image getImage(Object value) {
268:                 return null;
269:         }
270:
271:         @Override
272:         public int getMinWidth() {
273:                 return 0;
274:         }
275: }