Skip to content

Package: MultiStringCellEditor$TargetToModelStrategy

MultiStringCellEditor$TargetToModelStrategy

nameinstructionbranchcomplexitylinemethod
MultiStringCellEditor.TargetToModelStrategy(MultiStringCellEditor)
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%
convert(Object)
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: * Jonas Helming - initial API and implementation
13: *******************************************************************************/
14: package org.eclipse.emf.ecp.edit.spi.swt.table;
15:
16: import java.util.ArrayList;
17: import java.util.List;
18:
19: import org.eclipse.core.databinding.DataBindingContext;
20: import org.eclipse.core.databinding.UpdateValueStrategy;
21: import org.eclipse.core.databinding.property.value.IValueProperty;
22: import org.eclipse.emf.databinding.EMFUpdateValueStrategy;
23: import org.eclipse.emf.ecore.EStructuralFeature;
24: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
25: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
26: import org.eclipse.jface.databinding.viewers.CellEditorProperties;
27: import org.eclipse.swt.SWT;
28: import org.eclipse.swt.graphics.Image;
29: import org.eclipse.swt.widgets.Composite;
30: import org.eclipse.swt.widgets.Text;
31:
32: /**
33: * A cell Editor to display List of Strings in a cell editor. it allows to enter a list of string using a
34: * separator (by default ";").
35: *
36: * @author Jonas Helming
37: * @since 1.14
38: *
39: */
40: public class MultiStringCellEditor extends StringBasedCellEditor {
41:
42:         /**
43:          * @author Jonas Helming
44:          *
45:          */
46:         protected final class TargetToModelStrategy extends EMFUpdateValueStrategy {
47:
48:                 @Override
49:                 public Object convert(final Object value) {
50:                         return convertStringToList((String) value);
51:                 }
52:
53:         }
54:
55:         /**
56:          * Convertes a String into a list of Strings using the separator.
57:          *
58:          * @param value the String to split
59:          * @return the list of sub string
60:          */
61:         protected List<String> convertStringToList(final String value) {
62:                 final boolean emptyStringAtEnd = value.endsWith(getSeparator());
63:                 final String[] split = value.split(getSeparator());
64:                 final List<String> list = new ArrayList<String>();
65:                 for (int i = 0; i < split.length; i++) {
66:                         list.add(split[i]);
67:                 }
68:                 if (emptyStringAtEnd) {
69:                         list.add(""); //$NON-NLS-1$
70:                 }
71:                 return list;
72:         }
73:
74:         /**
75:          * Returns the separator used to split the input string into entries of the String list.
76:          *
77:          * @return the separator as a String
78:          */
79:         protected String getSeparator() {
80:                 return ";"; //$NON-NLS-1$
81:         }
82:
83:         private EStructuralFeature eStructuralFeature;
84:
85:         /**
86:          * The constructor which only takes a parent composite.
87:          *
88:          * @param parent the {@link Composite} to use as a parent.
89:          */
90:         public MultiStringCellEditor(Composite parent) {
91:                 super(parent, SWT.NONE);
92:         }
93:
94:         /**
95:          * A constructor which takes a parent and the style to use, the style is ignored by this cell editor.
96:          *
97:          * @param parent the {@link Composite} to use as a parent
98:          * @param style the SWT style to set
99:          */
100:         public MultiStringCellEditor(Composite parent, int style) {
101:                 super(parent, style);
102:         }
103:
104:         @SuppressWarnings({ "rawtypes" })
105:         @Override
106:         public IValueProperty getValueProperty() {
107:                 return CellEditorProperties.control().value(WidgetProperties.text(SWT.FocusOut));
108:         }
109:
110:         @Override
111:         public void instantiate(EStructuralFeature eStructuralFeature, ViewModelContext viewModelContext) {
112:                 super.instantiate(eStructuralFeature, viewModelContext);
113:                 this.eStructuralFeature = eStructuralFeature;
114:         }
115:
116:         @Override
117:         public String getFormatedString(Object value) {
118:                 if (value == null) {
119:                         return ""; //$NON-NLS-1$
120:                 }
121:                 if (!(value instanceof List)) {
122:                         return ""; //$NON-NLS-1$
123:                 }
124:
125:                 @SuppressWarnings("unchecked")
126:                 final List<String> list = (List<String>) value;
127:                 String string = ""; //$NON-NLS-1$
128:                 int i = 0;
129:                 for (final String subString : list) {
130:                         if (i != 0) {
131:                                 string = string + getSeparator();
132:                         }
133:                         string = string + subString;
134:                         i++;
135:                 }
136:                 return string;
137:         }
138:
139:         @Override
140:         public int getColumnWidthWeight() {
141:                 return 50;
142:         }
143:
144:         @Override
145:         public UpdateValueStrategy getTargetToModelStrategy(final DataBindingContext databindingContext) {
146:                 return withPreSetValidation(eStructuralFeature, new TargetToModelStrategy());
147:         }
148:
149:         @Override
150:         public UpdateValueStrategy getModelToTargetStrategy(DataBindingContext databindingContext) {
151:                 return new EMFUpdateValueStrategy() {
152:                         @Override
153:                         public Object convert(Object value) {
154:                                 return getFormatedString(value);
155:                         }
156:                 };
157:         }
158:
159:         /**
160:          * returns the {@link Text} of the cell editor.
161:          *
162:          * @return a {@link Text}
163:          */
164:         protected Text getText() {
165:                 return text;
166:         }
167:
168:         @Override
169:         public void setEditable(boolean editable) {
170:                 if (getText() != null) {
171:                         getText().setEditable(editable);
172:                 }
173:         }
174:
175:         @Override
176:         public Image getImage(Object value) {
177:                 return null;
178:         }
179:
180:         @Override
181:         public int getMinWidth() {
182:                 return 0;
183:         }
184: }