Skip to content

Package: BooleanCellEditor

BooleanCellEditor

nameinstructionbranchcomplexitylinemethod
BooleanCellEditor(Composite)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
activate()
M: 0 C: 14
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 4
100%
M: 0 C: 1
100%
activate(ColumnViewerEditorActivationEvent)
M: 0 C: 8
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
createControl(Composite)
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
doGetValue()
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
doSetFocus()
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%
doSetValue(Object)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getColumnWidthWeight()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getCopyMap(Widget)
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getFormatedString(Object)
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%
getImage(Object)
M: 0 C: 13
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getMinWidth()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getModelToTargetStrategy(DataBindingContext)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getTargetToModelStrategy(DataBindingContext)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getValueProperty()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
instantiate(EStructuralFeature, ViewModelContext)
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%
setCopyTextMarker(ViewerCell, Object)
M: 0 C: 18
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
setEditable(boolean)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
updateCell(ViewerCell, Object)
M: 3 C: 21
88%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 6
86%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 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: * jfaltermeier - initial API and implementation
13: * Christian W. Damus - bug 547271
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.internal.table.celleditor.rcp;
16:
17: import java.util.HashMap;
18: import java.util.Map;
19:
20: import org.eclipse.core.databinding.DataBindingContext;
21: import org.eclipse.core.databinding.UpdateValueStrategy;
22: import org.eclipse.core.databinding.observable.Diffs;
23: import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
24: import org.eclipse.core.databinding.observable.value.IObservableValue;
25: import org.eclipse.core.databinding.property.INativePropertyListener;
26: import org.eclipse.core.databinding.property.ISimplePropertyListener;
27: import org.eclipse.core.databinding.property.value.IValueProperty;
28: import org.eclipse.core.databinding.property.value.SimpleValueProperty;
29: import org.eclipse.core.runtime.Assert;
30: import org.eclipse.emf.databinding.EMFUpdateValueStrategy;
31: import org.eclipse.emf.ecore.EStructuralFeature;
32: import org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor;
33: import org.eclipse.emf.ecp.edit.spi.swt.table.ECPCustomUpdateCellEditor;
34: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
35: import org.eclipse.emf.ecp.view.spi.table.celleditor.rcp.NativeWidgetHelper;
36: import org.eclipse.emf.ecp.view.spi.table.celleditor.rcp.NativeWidgetHelper.CheckBoxState;
37: import org.eclipse.jface.viewers.CellEditor;
38: import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
39: import org.eclipse.jface.viewers.ICellEditorListener;
40: import org.eclipse.jface.viewers.ViewerCell;
41: import org.eclipse.swt.graphics.Image;
42: import org.eclipse.swt.widgets.Composite;
43: import org.eclipse.swt.widgets.Control;
44: import org.eclipse.swt.widgets.Widget;
45:
46: /**
47: * Cell editor for boolean values.
48: *
49: * @author jfaltermeier
50: *
51: */
52: public class BooleanCellEditor extends CellEditor implements ECPCellEditor, ECPCustomUpdateCellEditor {
53:
54:         private boolean editable;
55:         private final Composite parent;
56:         private boolean value;
57:
58:         /**
59:          * A constructor which takes only a parent.
60:          *
61:          * @param parent the {@link Composite} to use as a parent.
62:          */
63:         public BooleanCellEditor(Composite parent) {
64:                 super(parent);
65:                 this.parent = parent;
66:         }
67:
68:         /**
69:          * {@inheritDoc}
70:          *
71:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getValueProperty()
72:          */
73:         @Override
74:         public IValueProperty getValueProperty() {
75:                 return new SimpleValueProperty() {
76:
77:                         @Override
78:                         public Object getValueType() {
79:                                 return Boolean.class;
80:                         }
81:
82:                         @Override
83:                         protected Object doGetValue(Object source) {
84:                                 return BooleanCellEditor.this.doGetValue();
85:                         }
86:
87:                         @Override
88:                         protected void doSetValue(Object source, Object value) {
89:                                 BooleanCellEditor.this.doSetValue(value);
90:                         }
91:
92:                         @Override
93:                         public IObservableValue observe(Object source) {
94:                                 return new BooleanCellEditorObservableValue();
95:                         }
96:
97:                         @Override
98:                         public INativePropertyListener adaptListener(ISimplePropertyListener listener) {
99:                                 return null;
100:                         }
101:                 };
102:         }
103:
104:         /**
105:          * {@inheritDoc}
106:          *
107:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#instantiate(org.eclipse.emf.ecore.EStructuralFeature,
108:          * org.eclipse.emf.ecp.view.spi.context.ViewModelContext)
109:          */
110:         @Override
111:         public void instantiate(EStructuralFeature feature, ViewModelContext viewModelContext) {
112:                 // no op
113:         }
114:
115:         /**
116:          * {@inheritDoc}
117:          *
118:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getFormatedString(java.lang.Object)
119:          */
120:         @Override
121:         public String getFormatedString(Object value) {
122:                 return null;
123:         }
124:
125:         /**
126:          * {@inheritDoc}
127:          *
128:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getColumnWidthWeight()
129:          */
130:         @Override
131:         public int getColumnWidthWeight() {
132:                 return 25;
133:         }
134:
135:         /**
136:          * {@inheritDoc}
137:          *
138:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getTargetToModelStrategy(org.eclipse.core.databinding.DataBindingContext)
139:          */
140:         @Override
141:         public UpdateValueStrategy getTargetToModelStrategy(DataBindingContext databindingContext) {
142:                 return new EMFUpdateValueStrategy();
143:         }
144:
145:         /**
146:          * {@inheritDoc}
147:          *
148:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getModelToTargetStrategy(org.eclipse.core.databinding.DataBindingContext)
149:          */
150:         @Override
151:         public UpdateValueStrategy getModelToTargetStrategy(DataBindingContext databindingContext) {
152:                 return new EMFUpdateValueStrategy();
153:         }
154:
155:         /**
156:          * {@inheritDoc}
157:          *
158:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#setEditable(boolean)
159:          */
160:         @Override
161:         public void setEditable(boolean editable) {
162:                 this.editable = editable;
163:         }
164:
165:         /**
166:          * {@inheritDoc}
167:          *
168:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getImage(java.lang.Object)
169:          */
170:         @Override
171:         public Image getImage(Object value) {
172:•                final CheckBoxState state = (Boolean) value ? CheckBoxState.checked : CheckBoxState.unchecked;
173:                 return NativeWidgetHelper.getCheckBoxImage(parent, state);
174:         }
175:
176:         /**
177:          * {@inheritDoc}
178:          *
179:          * @see org.eclipse.jface.viewers.CheckboxCellEditor#activate()
180:          */
181:         @Override
182:         public void activate() {
183:•                if (editable) {
184:•                        value = !value;
185:                         fireApplyEditorValue();
186:                 }
187:         }
188:
189:         /**
190:          * {@inheritDoc}
191:          *
192:          * @see org.eclipse.jface.viewers.CellEditor#createControl(org.eclipse.swt.widgets.Composite)
193:          */
194:         @Override
195:         protected Control createControl(Composite parent) {
196:                 // intended null since there is no actual cell editor. Activate is used as trigger.
197:                 return null;
198:         }
199:
200:         /**
201:          * {@inheritDoc}
202:          *
203:          * @see org.eclipse.jface.viewers.CellEditor#doGetValue()
204:          */
205:         @Override
206:         protected Object doGetValue() {
207:•                return value ? Boolean.TRUE : Boolean.FALSE;
208:         }
209:
210:         /**
211:          * {@inheritDoc}
212:          *
213:          * @see org.eclipse.jface.viewers.CellEditor#doSetFocus()
214:          */
215:         @Override
216:         protected void doSetFocus() {
217:                 // ignored
218:         }
219:
220:         /**
221:          * {@inheritDoc}
222:          *
223:          * @see org.eclipse.jface.viewers.CellEditor#doSetValue(java.lang.Object)
224:          */
225:         @Override
226:         protected void doSetValue(Object value) {
227:                 Assert.isTrue(value instanceof Boolean);
228:                 this.value = ((Boolean) value).booleanValue();
229:         }
230:
231:         @Override
232:         public void activate(ColumnViewerEditorActivationEvent activationEvent) {
233:•                if (activationEvent.eventType != ColumnViewerEditorActivationEvent.TRAVERSAL) {
234:                         super.activate(activationEvent);
235:                 }
236:         }
237:
238:         /**
239:          * {@inheritDoc}
240:          *
241:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCustomUpdateCellEditor#updateCell(org.eclipse.jface.viewers.ViewerCell,
242:          * java.lang.Object)
243:          */
244:         @Override
245:         public void updateCell(ViewerCell cell, Object value) {
246:•                if (!"".equals(cell.getText())) { //$NON-NLS-1$
247:                         cell.setText(""); //$NON-NLS-1$
248:                 }
249:                 final Image image = getImage(value);
250:•                if (cell.getImage() != image) {
251:                         cell.setImage(image);
252:                 }
253:                 setCopyTextMarker(cell, value);
254:         }
255:
256:         /**
257:          * Sets the copy text marker for the given {@code cell} and {@code value}.
258:          *
259:          * @param cell the {@link ViewerCell}.
260:          * @param value the {@link Object} value.
261:          */
262:         protected void setCopyTextMarker(ViewerCell cell, Object value) {
263:•                if (value != null) {
264:                         final Widget item = cell.getItem();
265:                         final Map<Integer, String> copyMap = getCopyMap(item);
266:                         copyMap.put(cell.getColumnIndex(), value.toString());
267:                 }
268:         }
269:
270:         /**
271:          * Get the copy map of the given {@code widget}. If it does not exist it will be created.
272:          *
273:          * @param widget the {@link Widget} to check.
274:          * @return the copy map.
275:          */
276:         protected Map<Integer, String> getCopyMap(Widget widget) {
277:                 @SuppressWarnings("unchecked")
278:                 Map<Integer, String> copyMap = (Map<Integer, String>) widget.getData(ECPCellEditor.COPY_STRING_ALTERNATIVE);
279:•                if (copyMap == null) {
280:                         copyMap = new HashMap<Integer, String>();
281:                         widget.setData(ECPCellEditor.COPY_STRING_ALTERNATIVE, copyMap);
282:                 }
283:                 return copyMap;
284:         }
285:
286:         /**
287:          * {@link IObservableValue} for the boolean cell editor.
288:          *
289:          * @author jfaltermeier
290:          *
291:          */
292:         private class BooleanCellEditorObservableValue extends AbstractObservableValue {
293:
294:                 private final ICellEditorListener listener;
295:
296:                 BooleanCellEditorObservableValue() {
297:                         listener = new ICellEditorListener() {
298:
299:                                 @Override
300:                                 public void editorValueChanged(boolean oldValidState, boolean newValidState) {
301:                                         // no op
302:                                 }
303:
304:                                 @Override
305:                                 public void cancelEditor() {
306:                                         // no op
307:                                 }
308:
309:                                 @Override
310:                                 public void applyEditorValue() {
311:                                         fireValueChange(Diffs.createValueDiff(!Boolean.class.cast(BooleanCellEditor.this.getValue()),
312:                                                 BooleanCellEditor.this.getValue()));
313:                                 }
314:                         };
315:                         BooleanCellEditor.this.addListener(listener);
316:                 }
317:
318:                 /**
319:                  * {@inheritDoc}
320:                  *
321:                  * @see org.eclipse.core.databinding.observable.value.IObservableValue#getValueType()
322:                  */
323:                 @Override
324:                 public Object getValueType() {
325:                         return Boolean.class;
326:                 }
327:
328:                 /**
329:                  * {@inheritDoc}
330:                  *
331:                  * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#doGetValue()
332:                  */
333:                 @Override
334:                 protected Object doGetValue() {
335:                         return BooleanCellEditor.this.getValue();
336:                 }
337:
338:                 /**
339:                  * {@inheritDoc}
340:                  *
341:                  * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#doSetValue(java.lang.Object)
342:                  */
343:                 @Override
344:                 protected void doSetValue(Object value) {
345:                         BooleanCellEditor.this.setValue(value);
346:                 }
347:
348:                 /**
349:                  * {@inheritDoc}
350:                  *
351:                  * @see org.eclipse.core.databinding.observable.AbstractObservable#dispose()
352:                  */
353:                 @Override
354:                 public synchronized void dispose() {
355:                         BooleanCellEditor.this.removeListener(listener);
356:                         super.dispose();
357:                 }
358:
359:         }
360:
361:         /**
362:          * {@inheritDoc}
363:          *
364:          * @see org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor#getMinWidth()
365:          */
366:         @Override
367:         public int getMinWidth() {
368:                 return 0;
369:         }
370: }