Skip to content

Package: XmlDateCellEditor$3

XmlDateCellEditor$3

nameinstructionbranchcomplexitylinemethod
widgetDefaultSelected(SelectionEvent)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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.DateFormat;
17: import java.text.MessageFormat;
18: import java.text.SimpleDateFormat;
19: import java.util.Calendar;
20: import java.util.GregorianCalendar;
21:
22: import javax.xml.datatype.XMLGregorianCalendar;
23:
24: import org.eclipse.core.databinding.DataBindingContext;
25: import org.eclipse.core.databinding.UpdateValueStrategy;
26: import org.eclipse.core.databinding.observable.value.IObservableValue;
27: import org.eclipse.core.databinding.property.INativePropertyListener;
28: import org.eclipse.core.databinding.property.ISimplePropertyListener;
29: import org.eclipse.core.databinding.property.value.IValueProperty;
30: import org.eclipse.core.databinding.property.value.SimpleValueProperty;
31: import org.eclipse.emf.ecore.EStructuralFeature;
32: import org.eclipse.emf.ecp.edit.internal.swt.util.DateUtil;
33: import org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor;
34: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
35: import org.eclipse.jface.viewers.CellEditor;
36: import org.eclipse.swt.SWT;
37: import org.eclipse.swt.events.FocusAdapter;
38: import org.eclipse.swt.events.FocusEvent;
39: import org.eclipse.swt.events.KeyAdapter;
40: import org.eclipse.swt.events.KeyEvent;
41: import org.eclipse.swt.events.SelectionAdapter;
42: import org.eclipse.swt.events.SelectionEvent;
43: import org.eclipse.swt.events.TraverseEvent;
44: import org.eclipse.swt.events.TraverseListener;
45: import org.eclipse.swt.graphics.Image;
46: import org.eclipse.swt.widgets.Composite;
47: import org.eclipse.swt.widgets.Control;
48: import org.eclipse.swt.widgets.DateTime;
49:
50: /**
51: * A cell editor for editing an {@link XMLGregorianCalendar}.
52: *
53: * @author Eugen Neufeld
54: *
55: */
56: public class XmlDateCellEditor extends CellEditor implements ECPCellEditor {
57:
58:         private DateTime dateWidget;
59:         private final DateFormat dateFormat = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM);
60:
61:         /**
62:          * Default constructor.
63:          */
64:         public XmlDateCellEditor() {
65:         }
66:
67:         /**
68:          * A constructor which takes only a parent.
69:          *
70:          * @param parent the {@link Composite} to use as a parent.
71:          */
72:         public XmlDateCellEditor(Composite parent) {
73:                 super(parent);
74:         }
75:
76:         /**
77:          * A constructor which takes the parent and the style.
78:          *
79:          * @param parent the {@link Composite} to use as a parent
80:          * @param style the Style to set
81:          */
82:         public XmlDateCellEditor(Composite parent, int style) {
83:                 super(parent, style);
84:         }
85:
86:         /**
87:          * {@inheritDoc}
88:          *
89:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getValueProperty()
90:          */
91:         @Override
92:         public IValueProperty getValueProperty() {
93:                 return new SimpleValueProperty() {
94:
95:                         @Override
96:                         public Object getValueType() {
97:                                 return XMLGregorianCalendar.class;
98:                         }
99:
100:                         @Override
101:                         protected Object doGetValue(Object source) {
102:                                 return XmlDateCellEditor.this.doGetValue();
103:                         }
104:
105:                         @Override
106:                         protected void doSetValue(Object source, Object value) {
107:                                 XmlDateCellEditor.this.doSetValue(value);
108:                         }
109:
110:                         @Override
111:                         public IObservableValue observe(Object source) {
112:                                 if (source instanceof XmlDateCellEditor) {
113:                                         return observe(dateWidget);
114:                                 }
115:                                 return super.observe(source);
116:                         }
117:
118:                         @Override
119:                         public INativePropertyListener adaptListener(ISimplePropertyListener listener) {
120:                                 return null;
121:                         }
122:                 };
123:         }
124:
125:         /**
126:          * {@inheritDoc}
127:          *
128:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#instantiate(org.eclipse.emf.ecore.EStructuralFeature,
129:          * org.eclipse.emf.ecp.view.spi.context.ViewModelContext)
130:          */
131:         @Override
132:         public void instantiate(EStructuralFeature feature, ViewModelContext viewModelContext) {
133:
134:         }
135:
136:         @Override
137:         protected Control createControl(Composite parent) {
138:                 dateWidget = new DateTime(parent, SWT.DATE | SWT.DROP_DOWN);
139:                 dateWidget.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_edit_cellEditor_xmlDate"); //$NON-NLS-1$
140:                 dateWidget.addKeyListener(new KeyAdapter() {
141:                         private static final long serialVersionUID = 1L;
142:
143:                         // hook key pressed - see PR 14201
144:                         @Override
145:                         public void keyPressed(KeyEvent e) {
146:                                 keyReleaseOccured(e);
147:                         }
148:                 });
149:                 dateWidget.addSelectionListener(new SelectionAdapter() {
150:                         private static final long serialVersionUID = 1L;
151:
152:                         @Override
153:                         public void widgetDefaultSelected(SelectionEvent event) {
154:                                 applyEditorValueAndDeactivate();
155:                         }
156:
157:                 });
158:
159:                 dateWidget.addTraverseListener(new TraverseListener() {
160:                         private static final long serialVersionUID = 1L;
161:
162:                         @Override
163:                         public void keyTraversed(TraverseEvent e) {
164:                                 if (e.detail == SWT.TRAVERSE_ESCAPE || e.detail == SWT.TRAVERSE_RETURN) {
165:                                         e.doit = false;
166:                                 }
167:                         }
168:                 });
169:
170:                 dateWidget.addFocusListener(new FocusAdapter() {
171:                         private static final long serialVersionUID = 1L;
172:
173:                         @Override
174:                         public void focusLost(FocusEvent e) {
175:                                 XmlDateCellEditor.this.focusLost();
176:                         }
177:                 });
178:                 return dateWidget;
179:         }
180:
181:         @Override
182:         protected Object doGetValue() {
183:                 final Calendar selectedCalendarDate = Calendar.getInstance();
184:                 selectedCalendarDate.set(Calendar.YEAR, dateWidget.getYear());
185:                 selectedCalendarDate.set(Calendar.MONTH, dateWidget.getMonth());
186:                 selectedCalendarDate.set(Calendar.DAY_OF_MONTH, dateWidget.getDay());
187:
188:                 return DateUtil.convertOnlyDateToXMLGregorianCalendar(selectedCalendarDate);
189:
190:         }
191:
192:         @Override
193:         protected void doSetFocus() {
194:                 dateWidget.setFocus();
195:         }
196:
197:         @Override
198:         protected void doSetValue(Object value) {
199:                 if (value == null) {
200:                         return;
201:                 }
202:                 final XMLGregorianCalendar cal = (XMLGregorianCalendar) value;
203:                 final GregorianCalendar gregCal = cal.toGregorianCalendar();
204:                 dateWidget.setDate(gregCal.get(Calendar.YEAR), gregCal.get(Calendar.MONTH), gregCal.get(Calendar.DAY_OF_MONTH));
205:         }
206:
207:         /*
208:          * (non-Javadoc)
209:          * @see org.eclipse.jface.viewers.CellEditor#focusLost()
210:          */
211:         @Override
212:         protected void focusLost() {
213:                 if (isActivated()) {
214:                         applyEditorValueAndDeactivate();
215:                 }
216:         }
217:
218:         /*
219:          * (non-Javadoc)
220:          * @see org.eclipse.jface.viewers.CellEditor#keyReleaseOccured(org.eclipse.swt.events.KeyEvent)
221:          */
222:         @Override
223:         protected void keyReleaseOccured(KeyEvent keyEvent) {
224:                 super.keyReleaseOccured(keyEvent);
225:                 if (keyEvent.character == '\u001b') { // Escape character
226:                         fireCancelEditor();
227:                 } else if (keyEvent.character == '\t') { // tab key
228:                         applyEditorValueAndDeactivate();
229:                 }
230:         }
231:
232:         /**
233:          * Applies the currently selected value and deactiavates the cell editor.
234:          */
235:         void applyEditorValueAndDeactivate() {
236:                 // must set the selection before getting value
237:
238:                 final Object newValue = doGetValue();
239:                 markDirty();
240:                 final boolean isValid = isCorrect(newValue);
241:                 setValueValid(isValid);
242:
243:                 if (!isValid) {
244:                         MessageFormat.format(getErrorMessage(), new Object[] { newValue });
245:                 }
246:
247:                 fireApplyEditorValue();
248:                 deactivate();
249:         }
250:
251:         /**
252:          *
253:          * {@inheritDoc}
254:          *
255:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getFormatedString(java.lang.Object)
256:          */
257:         @Override
258:         public String getFormatedString(Object value) {
259:                 final XMLGregorianCalendar cal = (XMLGregorianCalendar) value;
260:                 if (value == null) {
261:                         return ""; //$NON-NLS-1$
262:                 }
263:                 return dateFormat.format(cal.toGregorianCalendar().getTime());
264:         }
265:
266:         /**
267:          *
268:          * {@inheritDoc}
269:          *
270:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getColumnWidthWeight()
271:          */
272:         @Override
273:         public int getColumnWidthWeight() {
274:                 return 75;
275:         }
276:
277:         /**
278:          *
279:          * {@inheritDoc}
280:          *
281:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getTargetToModelStrategy(org.eclipse.core.databinding.DataBindingContext)
282:          */
283:         @Override
284:         public UpdateValueStrategy getTargetToModelStrategy(DataBindingContext databindingContext) {
285:                 return null;
286:         }
287:
288:         /**
289:          *
290:          * {@inheritDoc}
291:          *
292:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getModelToTargetStrategy(org.eclipse.core.databinding.DataBindingContext)
293:          */
294:         @Override
295:         public UpdateValueStrategy getModelToTargetStrategy(DataBindingContext databindingContext) {
296:                 return null;
297:         }
298:
299:         /**
300:          * {@inheritDoc}
301:          *
302:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#setEditable(boolean)
303:          */
304:         @Override
305:         public void setEditable(boolean editable) {
306:                 if (dateWidget != null) {
307:                         dateWidget.setEnabled(editable);
308:                 }
309:         }
310:
311:         /**
312:          * {@inheritDoc}
313:          *
314:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getImage(java.lang.Object)
315:          */
316:         @Override
317:         public Image getImage(Object value) {
318:                 return null;
319:         }
320:
321:         /**
322:          * {@inheritDoc}
323:          *
324:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getMinWidth()
325:          */
326:         @Override
327:         public int getMinWidth() {
328:                 return 0;
329:         }
330: }