Skip to content

Package: ValueSelectionHelper

ValueSelectionHelper

nameinstructionbranchcomplexitylinemethod
ValueSelectionHelper()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getPrimitiveClass(Class)
M: 43 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
openEnumDialog(Shell, Class)
M: 47 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
openValueSelectionDialog(Shell, EStructuralFeature)
M: 107 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 34 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 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 - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.template.internal.tooling.util;
15:
16: import java.lang.reflect.Constructor;
17: import java.lang.reflect.InvocationTargetException;
18:
19: import org.eclipse.emf.ecore.EAttribute;
20: import org.eclipse.emf.ecore.EReference;
21: import org.eclipse.emf.ecore.EStructuralFeature;
22: import org.eclipse.jface.dialogs.InputDialog;
23: import org.eclipse.jface.dialogs.MessageDialog;
24: import org.eclipse.jface.viewers.ArrayContentProvider;
25: import org.eclipse.jface.viewers.LabelProvider;
26: import org.eclipse.jface.window.Window;
27: import org.eclipse.swt.widgets.Shell;
28: import org.eclipse.ui.dialogs.ListDialog;
29:
30: /**
31: *
32: * @author Eugen Neufeld
33: *
34: */
35: public abstract class ValueSelectionHelper {
36:
37:         /**
38:          * Opens a dialog to select a possible value for a given {@link EStructuralFeature}.
39:          * If the feature is a enum or boolean, it will show suggestions, otherwise, it will accept Strings.
40:          *
41:          * @param shell The {@link Shell} to open the dialog on
42:          * @param structuralFeature The {@link EStructuralFeature} to select possible values for
43:          * @return The selected value as an {@link Object}
44:          */
45:         public static Object openValueSelectionDialog(Shell shell, EStructuralFeature structuralFeature) {
46:•                if (structuralFeature == null) {
47:                         return null;
48:                 }
49:•                if (EReference.class.isInstance(structuralFeature)) {
50:                         // TODO show all references
51:                         return null;
52:                 }
53:                 final EAttribute attribute = (EAttribute) structuralFeature;
54:                 Class<?> attribuetClazz = attribute.getEAttributeType().getInstanceClass();
55:•                if (attribuetClazz.isPrimitive()) {
56:                         attribuetClazz = getPrimitiveClass(attribuetClazz);
57:                 }
58:                 Object object = null;
59:•                if (Enum.class.isAssignableFrom(attribuetClazz)) {
60:                         object = openEnumDialog(shell, attribuetClazz);
61:•                } else if (String.class.isAssignableFrom(attribuetClazz)
62:•                        || Number.class.isAssignableFrom(attribuetClazz) || Boolean.class.isAssignableFrom(attribuetClazz)) {
63:                         try {
64:                                 final Constructor<?> constructor = attribuetClazz.getConstructor(String.class);
65:                                 final InputDialog id = new InputDialog(
66:                                         shell,
67:                                         "Insert the value", //$NON-NLS-1$
68:                                         "The value must be parseable by the " //$NON-NLS-1$
69:                                                 + attribuetClazz.getSimpleName()
70:                                                 + " class. For a double value please use the #.# format. For boolean values 'true' or 'false'.", //$NON-NLS-1$
71:                                         null, null);
72:                                 final int inputResult = id.open();
73:•                                if (Window.OK == inputResult) {
74:                                         object = constructor.newInstance(id.getValue());
75:                                 }
76:                         } catch (final IllegalArgumentException ex) {
77:
78:                         } catch (final SecurityException ex) {
79:
80:                         } catch (final NoSuchMethodException ex) {
81:                         } catch (final InstantiationException ex) {
82:                         } catch (final IllegalAccessException ex) {
83:                         } catch (final InvocationTargetException ex) {
84:                         }
85:                 } else {
86:                         MessageDialog.openError(shell, "Not primitive Attribute selected", //$NON-NLS-1$
87:                                 "The selected attribute has a not primitive type. We can't provide you support for it!"); //$NON-NLS-1$
88:                 }
89:                 return object;
90:         }
91:
92:         private static Object openEnumDialog(Shell shell, Class<?> attribuetClazz) {
93:                 final Object[] enumValues = attribuetClazz.getEnumConstants();
94:                 final ListDialog ld = new ListDialog(shell);
95:                 ld.setLabelProvider(new LabelProvider());
96:                 ld.setContentProvider(ArrayContentProvider.getInstance());
97:                 ld.setInput(enumValues);
98:                 ld.setInitialSelections(new Object[] { enumValues[0] });
99:                 ld.setMessage("Please select the enum value to set."); //$NON-NLS-1$
100:                 ld.setTitle("Select a value"); //$NON-NLS-1$
101:                 final int enumSelectionResult = ld.open();
102:•                if (Window.OK == enumSelectionResult) {
103:                         return ld.getResult()[0];
104:                 }
105:                 return null;
106:         }
107:
108:         private static Class<?> getPrimitiveClass(Class<?> attribuetClazz) {
109:•                if (int.class.isAssignableFrom(attribuetClazz)) {
110:                         attribuetClazz = Integer.class;
111:•                } else if (long.class.isAssignableFrom(attribuetClazz)) {
112:                         attribuetClazz = Long.class;
113:•                } else if (float.class.isAssignableFrom(attribuetClazz)) {
114:                         attribuetClazz = Float.class;
115:•                } else if (double.class.isAssignableFrom(attribuetClazz)) {
116:                         attribuetClazz = Double.class;
117:•                } else if (boolean.class.isAssignableFrom(attribuetClazz)) {
118:                         attribuetClazz = Boolean.class;
119:•                } else if (char.class.isAssignableFrom(attribuetClazz)) {
120:                         attribuetClazz = Character.class;
121:                 }
122:                 return attribuetClazz;
123:         }
124: }