Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d67d56b649730fbcb3398fe0b52fe5c752b1ceff (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*****************************************************************************
 * 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
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.tabbed.core.view.subfeatures;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.papyrus.views.properties.runtime.Activator;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


/**
 * Simple sub feature descriptor for emf objects. It only looks in a given feature of the list of given EObjects
 */
public class EMFSimpleSubFeatureDescriptor extends SubFeatureDescriptor {

	/** name of the feature to edit */
	protected final String featureNameToEdit;

	/**
	 * Creates a new EMFSimpleSubFeatureDescriptor.
	 * 
	 * @param featureName
	 *        the name of the feature to edit
	 */
	public EMFSimpleSubFeatureDescriptor(String featureName) {
		this.featureNameToEdit = featureName;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getSubElementsToEdit(List<Object> editedObjects) {
		List<Object> results = new ArrayList<Object>();
		for(Object object : editedObjects) {
			if(object instanceof EObject) {
				EStructuralFeature feature = ((EObject)object).eClass().getEStructuralFeature(featureNameToEdit);
				if(feature != null) {
					Object values = ((EObject)object).eGet(feature);
					if(values instanceof List<?>) {
						results.addAll((List<Object>)values);
					} else if(values != null) {
						results.add(values);
					}
				} else {
					Activator.log.error("Impossible to find the feature [" + featureNameToEdit + "] for object: " + object, null);
				}
			}
		}
		return results;
	}

	/**
	 * Returns the name of the feature to edit
	 * 
	 * @return the name of the feature to edit
	 */
	public String getFeatureNameToEdit() {
		return featureNameToEdit;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getText() {
		return "EMF Feature: " + featureNameToEdit;
	}

	/**
	 * {@inheritDoc}
	 */
	public Image getImage() {
		return org.eclipse.papyrus.views.properties.tabbed.core.Activator.getImage("/icons/EMFFeatureDescriptor.gif");
	}

	/**
	 * {@inheritDoc}
	 */
	public EMFSimpleSubFeatureDescriptorState createState(boolean readOnly) {
		return new EMFSimpleSubFeatureDescriptorState(this, readOnly);
	}

	/**
	 * State for {@link EMFSimpleSubFeatureDescriptor}
	 */
	public class EMFSimpleSubFeatureDescriptorState extends SubFeatureDescriptorState {

		/** change support for this bean */
		private PropertyChangeSupport changeSupport;

		/** name of the feature to edit */
		private String featureNameState;

		/**
		 * Creates a new EMFSimpleSubFeatureDescriptorState.
		 * 
		 * @param emfSimpleSubFeatureDescriptor
		 *        the descriptor to customize
		 * @param readOnly
		 *        read only mode of this state
		 */
		public EMFSimpleSubFeatureDescriptorState(EMFSimpleSubFeatureDescriptor emfSimpleSubFeatureDescriptor, boolean readOnly) {
			super(emfSimpleSubFeatureDescriptor, readOnly);

			featureNameState = emfSimpleSubFeatureDescriptor.getFeatureNameToEdit();

			// register change support
			changeSupport = new PropertyChangeSupport(this);
		}

		/**
		 * {@inheritDoc}
		 */
		public EMFSimpleSubFeatureDescriptor getDescriptor() {
			return (EMFSimpleSubFeatureDescriptor)super.getDescriptor();
		}

		/**
		 * {@inheritDoc}
		 */
		public String getText() {
			return "EMF Feature: " + featureNameState;
		}

		/**
		 * {@inheritDoc}
		 */
		public String getEditionDialogId() {
			return "EMFSimpleSubFeatureDescriptorStateDialog";
		}

		/**
		 * {@inheritDoc}
		 */
		public void addPropertyChangeListener(PropertyChangeListener listener) {
			changeSupport.addPropertyChangeListener(listener);
		}

		/**
		 * {@inheritDoc}
		 */
		public void removePropertyChangeListener(PropertyChangeListener listener) {
			changeSupport.removePropertyChangeListener(listener);
		}

		/**
		 * customizes the featureNameState
		 * 
		 * @param featureNameState
		 *        the featureNameState to set
		 */
		public void setFeatureNameState(String featureNameState) {
			String oldFeatureName = this.featureNameState;
			this.featureNameState = featureNameState;

			changeSupport.firePropertyChange("featureNameState", oldFeatureName, this.featureNameState);
		}

		/**
		 * Returns the customized feature Name
		 * 
		 * @return the customized feature Name
		 */
		public String getFeatureNameState() {
			return featureNameState;
		}

		/**
		 * {@inheritDoc}
		 */
		public Node generateNode(Document document) {
			//<subFeatureDescriptor featureName="memberEnd">
			//</subFeatureDescriptor>
			Element node = document.createElement("subFeatureDescriptor");
			node.setAttribute("featureName", getFeatureNameState());
			return node;
		}
	}
}

Back to the top