Skip to content

Package: XmlDateControlText$DateTargetToModelUpdateStrategy

XmlDateControlText$DateTargetToModelUpdateStrategy

nameinstructionbranchcomplexitylinemethod
XmlDateControlText.DateTargetToModelUpdateStrategy(XmlDateControlText)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
convertValue(Object)
M: 61 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
revertToOldValue(Object)
M: 87 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 23 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.text.DateFormat;
18: import java.text.ParseException;
19: import java.text.SimpleDateFormat;
20: import java.util.Calendar;
21: import java.util.Date;
22: import java.util.Locale;
23: import java.util.regex.Pattern;
24:
25: import javax.xml.datatype.XMLGregorianCalendar;
26:
27: import org.eclipse.core.databinding.Binding;
28: import org.eclipse.core.databinding.observable.value.IObservableValue;
29: import org.eclipse.emf.ecp.edit.internal.swt.Activator;
30: import org.eclipse.emf.ecp.edit.internal.swt.util.DateUtil;
31: import org.eclipse.emf.ecp.edit.spi.swt.util.ECPDialogExecutor;
32: import org.eclipse.emf.edit.command.SetCommand;
33: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
34: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
35: import org.eclipse.jface.dialogs.IDialogLabelKeys;
36: import org.eclipse.jface.dialogs.MessageDialog;
37: import org.eclipse.jface.layout.GridDataFactory;
38: import org.eclipse.jface.layout.GridLayoutFactory;
39: import org.eclipse.jface.resource.JFaceResources;
40: import org.eclipse.swt.SWT;
41: import org.eclipse.swt.events.SelectionAdapter;
42: import org.eclipse.swt.events.SelectionEvent;
43: import org.eclipse.swt.graphics.Point;
44: import org.eclipse.swt.graphics.Rectangle;
45: import org.eclipse.swt.layout.GridLayout;
46: import org.eclipse.swt.widgets.Button;
47: import org.eclipse.swt.widgets.Composite;
48: import org.eclipse.swt.widgets.DateTime;
49: import org.eclipse.swt.widgets.Shell;
50: import org.eclipse.swt.widgets.Text;
51:
52: /**
53: * This is a XMLDateControl. It is used to display values of type {@link XMLGregorianCalendar}. This control only
54: * displays a date widget.
55: *
56: * @author Eugen Neufeld
57: *
58: * private Button bDate;
59: */
60: @Deprecated
61: public class XmlDateControlText extends AbstractTextControl {
62:         private static final DateFormat CHECK_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); //$NON-NLS-1$
63:         private static final Pattern CHECK_PATTERN = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$"); //$NON-NLS-1$
64:         private Button bDate;
65:
66:         @Override
67:         protected String getTextVariantID() {
68:                 return "org_eclipse_emf_ecp_control_xmldate"; //$NON-NLS-1$
69:         }
70:
71:         @Override
72:         protected String getUnsetLabelText() {
73:                 return LocalizationServiceHelper.getString(getClass(),
74:                         DepricatedControlMessageKeys.XmlDateControlText_NoDateSetClickToSetDate);
75:         }
76:
77:         @Override
78:         protected String getUnsetButtonTooltip() {
79:                 return LocalizationServiceHelper.getString(getClass(),
80:                         DepricatedControlMessageKeys.XmlDateControlText_UnsetDate);
81:         }
82:
83:         @Override
84:         protected void customizeText(Text text) {
85:                 super.customizeText(text);
86:                 text.setMessage(((SimpleDateFormat) setupFormat()).toPattern());
87:         }
88:
89:         @Override
90:         public void setEditable(boolean editable) {
91:                 super.setEditable(editable);
92:                 bDate.setVisible(editable);
93:         }
94:
95:         @Override
96:         protected void fillControlComposite(Composite composite) {
97:                 final Composite main = new Composite(composite, SWT.NONE);
98:                 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(main);
99:                 GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.BEGINNING).applyTo(main);
100:                 super.fillControlComposite(main);
101:                 bDate = new Button(main, SWT.PUSH);
102:                 bDate.setImage(Activator.getImage("icons/date.png")); //$NON-NLS-1$
103:                 bDate.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_control_xmldate"); //$NON-NLS-1$
104:                 bDate.addSelectionListener(new SelectionAdapterExtension());
105:         }
106:
107:         @Override
108:         public Binding bindValue() {
109:                 final IObservableValue value = WidgetProperties.text(SWT.FocusOut).observe(getText());
110:                 final DateTargetToModelUpdateStrategy targetToModelUpdateStrategy = new DateTargetToModelUpdateStrategy();
111:                 final DateModelToTargetUpdateStrategy modelToTargetUpdateStrategy = new DateModelToTargetUpdateStrategy();
112:                 final Binding binding = getDataBindingContext().bindValue(value, getModelValue(),
113:                         targetToModelUpdateStrategy, modelToTargetUpdateStrategy);
114:                 createTooltipBinding(targetToModelUpdateStrategy, modelToTargetUpdateStrategy);
115:                 return binding;
116:         }
117:
118:         /**
119:          * @author Jonas
120:          *
121:          */
122:         private final class SelectionAdapterExtension extends SelectionAdapter {
123:                 @Override
124:                 public void widgetSelected(SelectionEvent e) {
125:                         final Shell dialog = new Shell(getText().getShell(), SWT.NONE);
126:                         dialog.setLayout(new GridLayout(1, false));
127:
128:                         final DateTime calendar = new DateTime(dialog, SWT.CALENDAR | SWT.BORDER);
129:                         final XMLGregorianCalendar gregorianCalendar = (XMLGregorianCalendar) getModelValue().getValue();
130:                         final Calendar cal = Calendar.getInstance(getLocale());
131:                         if (gregorianCalendar != null) {
132:                                 cal.setTime(gregorianCalendar.toGregorianCalendar().getTime());
133:                         }
134:                         calendar.setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
135:
136:                         final IObservableValue dateObserver = WidgetProperties.dateTimeSelection().observe(calendar);
137:                         final Binding binding = getDataBindingContext().bindValue(dateObserver, getModelValue(),
138:                                 new DateTargetToModelUpdateStrategy(), new DateModelToTargetUpdateStrategy());
139:                         binding.updateModelToTarget();
140:
141:                         final Button okButton = new Button(dialog, SWT.PUSH);
142:                         okButton.setText(JFaceResources.getString(IDialogLabelKeys.OK_LABEL_KEY));
143:                         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).grab(false, false).applyTo(okButton);
144:                         okButton.addSelectionListener(new SelectionAdapter() {
145:                                 /**
146:                                  * {@inheritDoc}
147:                                  *
148:                                  * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
149:                                  */
150:                                 @Override
151:                                 public void widgetSelected(SelectionEvent e) {
152:                                         binding.updateTargetToModel();
153:                                         binding.dispose();
154:                                         dialog.close();
155:                                 }
156:                         });
157:
158:                         dialog.pack();
159:                         dialog.layout();
160:                         final Point dialogSize = dialog.getSize();
161:                         final Rectangle displayBounds = dialog.getDisplay().getBounds();
162:                         final Point buttonLocation = okButton.toDisplay(okButton.getSize().x, okButton.getSize().y);
163:
164:                         // TODO what if dialogsize > displaybounds? + some other cases
165:
166:                         int dialogX = buttonLocation.x - dialogSize.x;
167:                         int dialogY = buttonLocation.y;
168:                         if (dialogY + dialogSize.y > displayBounds.height) {
169:                                 dialogY = dialogY - okButton.getSize().y - dialogSize.y;
170:                         }
171:                         if (dialogX + dialogSize.x > displayBounds.width) {
172:                                 dialogX = dialogX - dialogSize.x;
173:                         } else if (dialogX - dialogSize.x < displayBounds.x) {
174:                                 dialogX = buttonLocation.x - okButton.getSize().x;
175:                         }
176:                         dialog.setLocation(dialogX, dialogY);
177:                         dialog.open();
178:                 }
179:         }
180:
181:         private class DateModelToTargetUpdateStrategy extends ModelToTargetUpdateStrategy {
182:
183:                 @Override
184:                 public Object convertValue(Object value) {
185:                         // final DecimalFormat format = (DecimalFormat) DecimalFormat
186:                         // .getInstance(getModelElementContext().getLocale());
187:                         // format.setGroupingUsed(false);
188:                         // format.setParseIntegerOnly(isInteger());
189:                         final DateFormat format = setupFormat();
190:                         final XMLGregorianCalendar gregorianCalendar = (XMLGregorianCalendar) value;
191:                         if (gregorianCalendar == null) {
192:                                 return null;
193:                         }
194:                         final Date date = gregorianCalendar.toGregorianCalendar().getTime();
195:                         return format.format(date);
196:                 }
197:         }
198:
199:         private class DateTargetToModelUpdateStrategy extends TargetToModelUpdateStrategy {
200:
201:                 private final DateFormat format;
202:
203:                 DateTargetToModelUpdateStrategy() {
204:                         super();
205:                         format = setupFormat();
206:
207:                 }
208:
209:                 @Override
210:                 protected Object convertValue(final Object value) {
211:                         try {
212:                                 Date date = null;
213:•                                if (String.class.isInstance(value)) {
214:                                         date = format.parse((String) value);
215:•                                } else if (Date.class.isInstance(value)) {
216:                                         date = (Date) value;
217:•                                } else if (value == null) {
218:                                         return value;
219:                                 }
220:                                 final String xmlFormat = CHECK_FORMAT.format(date);
221:•                                if (!CHECK_PATTERN.matcher(xmlFormat).matches()) {
222:                                         return revertToOldValue(value);
223:                                 }
224:                                 final String formatedDate = format.format(date);
225:                                 getText().setText(formatedDate);
226:
227:                                 final Calendar targetCal = Calendar.getInstance();
228:                                 targetCal.setTime(date);
229:                                 return DateUtil.convertOnlyDateToXMLGregorianCalendar(targetCal);
230:                         } catch (final ParseException ex) {
231:                                 return revertToOldValue(value);
232:                         }
233:                         // return null;
234:                 }
235:
236:                 private Object revertToOldValue(final Object value) {
237:
238:•                        if (getFirstStructuralFeature().getDefaultValue() == null && (value == null || value.equals(""))) { //$NON-NLS-1$
239:                                 return null;
240:                         }
241:
242:                         final Object result = getModelValue().getValue();
243:
244:                         final MessageDialog messageDialog = new MessageDialog(getText().getShell(),
245:                                 LocalizationServiceHelper.getString(getClass(),
246:                                         DepricatedControlMessageKeys.XmlDateControlText_InvalidNumber),
247:                                 null,
248:                                 LocalizationServiceHelper.getString(getClass(),
249:                                         DepricatedControlMessageKeys.XmlDateControlText_NumberInvalidValueWillBeUnset),
250:                                 MessageDialog.ERROR,
251:                                 new String[] { JFaceResources.getString(IDialogLabelKeys.OK_LABEL_KEY) }, 0);
252:
253:                         new ECPDialogExecutor(messageDialog) {
254:                                 @Override
255:                                 public void handleResult(int codeResult) {
256:
257:                                 }
258:                         }.execute();
259:
260:•                        if (result == null) {
261:                                 getText().setText(""); //$NON-NLS-1$
262:                         } else {
263:                                 final XMLGregorianCalendar gregorianCalendar = (XMLGregorianCalendar) result;
264:                                 final Date date = gregorianCalendar.toGregorianCalendar().getTime();
265:                                 getText().setText(format.format(date));
266:                         }
267:
268:•                        if (getFirstStructuralFeature().isUnsettable() && result == null) {
269:                                 showUnsetLabel();
270:                                 return SetCommand.UNSET_VALUE;
271:                         }
272:                         return result;
273:                 }
274:         }
275:
276:         /**
277:          * Sets up a {@link DateFormat} for the current {@link java.util.Locale}.
278:          *
279:          * @return the date format
280:          */
281:         protected DateFormat setupFormat() {
282:                 return DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale());
283:         }
284: }