Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bc73a83321ab43026d9e749c34f033b172368d84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *  Vincent Lorenzo (CEA-LIST) vincent.lorenzo@cea.fr
 *****************************************************************************/
package org.eclipse.papyrus.properties.runtime.modelhandler.emf;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.papyrus.properties.runtime.Activator;
import org.eclipse.papyrus.properties.runtime.propertyeditor.descriptor.IBoundedValuesPropertyEditorDescriptor;
import org.eclipse.papyrus.properties.runtime.propertyeditor.descriptor.IPropertyEditorDescriptor;

/**
 * Model Handler for enumeration
 */
public class BooleanEMFModelHandler extends EnumerationEMFModelHandler {

	/** id of this model handler */
	public static final String ID = "Boolean"; //$NON-NLS-1$

	/**
	 * Creates a new BooleanEMFModelHandler.
	 * 
	 * @param featureName
	 *        the name of the feature to edit
	 */
	public BooleanEMFModelHandler(String featureName) {
		super(featureName);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object getValueToEdit(EObject objectToEdit) {
		EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
		if(featureToEdit == null) {
			return null;
		}
		Object value = objectToEdit.eGet(featureToEdit);

		// should perhaps take into account default values in case the feature is not set...
		return (value instanceof Boolean) ? ((Boolean)value).toString() : value;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void setValueInModel(EObject objectToEdit, Object newValue) {
		EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
		if(featureToEdit == null) {
			return;
		}
		// remove value if result of the editor is empty
		if(newValue == null || newValue.equals("")) { //$NON-NLS-1$
			objectToEdit.eUnset(featureToEdit);
		} else if(newValue instanceof String) {
			objectToEdit.eSet(featureToEdit, Boolean.parseBoolean((String)newValue));
		} else if(newValue instanceof Boolean) {
			objectToEdit.eSet(featureToEdit, newValue);
		} else if(newValue instanceof List<?>) {
			List<Object> newValues = new ArrayList<Object>();
			for(Object value : (List<Object>)newValue) {
				if(value instanceof String) {
					newValues.add(Boolean.parseBoolean((String)value));
				} else if(value instanceof Boolean) {
					newValues.add(value);
				}
			}
			objectToEdit.eSet(featureToEdit, newValues);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void completeEditorDescriptor(IPropertyEditorDescriptor descriptor, List<? extends EObject> objectToEdit) {
		if(objectToEdit.size() < 1) {
			return;
		}
		EStructuralFeature featureToEdit = getFeatureByName(objectToEdit.get(0));
		if(featureToEdit == null) {
			return;
		}
		// test enumeration, reference, etc.
		List<String> values = new ArrayList<String>();

		// check if there is an empty string at the beginning. there is one if the lower bound of the feature to edit equal 0 
		if(featureToEdit.getLowerBound() == 0) {
			values.add(""); //$NON-NLS-1$
		}

		values.add("true"); //$NON-NLS-1$
		values.add("false"); //$NON-NLS-1$
		if(descriptor instanceof IBoundedValuesPropertyEditorDescriptor) {
			((IBoundedValuesPropertyEditorDescriptor)descriptor).setAvailableValues(values);
		} else {
			Activator.log.debug("Warning: " + descriptor + "could not be completed."); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getId() {
		return ID;
	}

	/**
	 * 
	 * @param objectToEdit
	 * @param newValue
	 * @return
	 */
	@Override
	public SetRequest[] getSetRequest(TransactionalEditingDomain domain, EObject objectToEdit, Object newValue) {
		EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
		if(featureToEdit == null) {
			return null;
		}
		// remove value if result of the editor is empty
		if(newValue == null || newValue.equals("")) { //$NON-NLS-1$
			return new SetRequest[]{ new SetRequest(domain, objectToEdit, featureToEdit, featureToEdit.getDefaultValue()) };
		} else if(newValue instanceof String) {
			return new SetRequest[]{ new SetRequest(domain, objectToEdit, featureToEdit, Boolean.parseBoolean((String)newValue)) };
		} else if(newValue instanceof Boolean) {
			return new SetRequest[]{ new SetRequest(domain, objectToEdit, featureToEdit, newValue) };
		} else if(newValue instanceof List<?>) {
			List<Object> newValues = new ArrayList<Object>();
			for(Object value : (List<?>)newValue) {
				if(value instanceof String) {
					newValues.add(Boolean.parseBoolean((String)value));
				} else if(value instanceof Boolean) {
					newValues.add(value);
				}
			}
			return new SetRequest[]{ new SetRequest(domain, objectToEdit, featureToEdit, newValues) };
		}
		return null;
	}
}

Back to the top