Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 106cfd0badc52acae2127c48669886dc2e464b05 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*******************************************************************************
 * Copyright (c) 2013 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:
 *     Cedric Dumoulin - cedric.dumoulin@lifl.fr
 ******************************************************************************/
package org.eclipse.papyrus.layers.stackmodel.layers.util;

import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EObjectResolvingEList;
import org.eclipse.papyrus.layers.stackmodel.BadStateException;
import org.eclipse.papyrus.layers.stackmodel.LayersException;
import org.eclipse.papyrus.layers.stackmodel.NotFoundException;
import org.eclipse.papyrus.layers.stackmodel.layers.Property;


/**
 * An EMF {@link EObjectResolvingEList} that automaticaly reflect a
 * Map of (String, T). The keys are the names of the Property.
 * The list also know the list of Properties, and use it to indexing
 * the types of the Map.
 * <br>
 * usage:
 * <br>
 * list = new PropertyIndexedList<type>(...);
 * list.setPropertyList( propertyRegistry.getProperties() );
 * 
 * @author cedric dumoulin
 *
 * @param <T> The type of the objects contained in the list. The type should be 
 * the same as the type of the values in the backuped Map.
 */
public class PropertyIndexedList<T> extends EObjectResolvingEList<T> {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The backuped Map. The implementation should be an Observable Map from EMF.
	 * 
	 */
	protected EMap<String, T> map;
	
	/**
	 * The ordered list of Property, used to indexing this List.
	 * The Property::index should be set.
	 */
	protected List<Property> propertyList;
	
	/**
	 * The classtype of the owner of the map
	 */
	protected Class<?> mapParentClasstype;
	
	/**
	 * The id of the map property in its parent.
	 */
	final protected int MAP_FEATURE_ID;
	
	/**
	 * The value that is used as a "null value".
	 * We can't use 'null' because the list don't support it (even
	 * if we set canSupportNull to true).
	 */
	final protected T NULL_VALUE;
	
	/**
	 * Constructor.
	 *
	 * @param dataClass Type of the element of the list
	 * @param layer The owner of this list and of the map
	 * @param featureID The feature Id of this list in the owner
	 * 
	 * @param mapFeatureID The map feature id in the owner
	 */
	public PropertyIndexedList(EMap<String, T> map, Class<T> dataClass, InternalEObject layer, int featureID, int mapFeatureID, T nullValue) {
		super(dataClass, layer, featureID);
		MAP_FEATURE_ID = mapFeatureID;
		NULL_VALUE = nullValue;
		mapParentClasstype = layer.getClass();
		this.map= map; 
		init();
	}

	/**
	 * Constructor.
	 *
	 * @param dataClass Type of the element of the list
	 * @param layer The owner of this list and of the map
	 * @param featureID The feature Id of this list in the owner
	 * 
	 * @param mapFeatureID The map feature id in the owner
	 */
	public PropertyIndexedList(Map<String, T> map, Class<T> dataClass, InternalEObject layer, int featureID, int mapFeatureID, T nullValue) {
		super(dataClass, layer, featureID);
		MAP_FEATURE_ID = mapFeatureID;
		NULL_VALUE = nullValue;
		mapParentClasstype = layer.getClass();
		this.map= (EMap<String, T>)map; 
		init();
	}

	/**
	 * Listen on the map for addition/deletion.
	 * 
	 */	
	protected void init() {
		getEObject().eAdapters().add(new PropertyValuesSynchronizer());
		
	}

	/**
	 * Allows double
	 * @see org.eclipse.emf.ecore.util.EObjectEList#isUnique()
	 *
	 * @return
	 */
	// 
	protected boolean isUnique() {
		return false;
	}

	/**
	 * Allow null values in the list.
	 * @see org.eclipse.emf.ecore.util.EObjectEList#canContainNull()
	 *
	 * @return
	 */
//	@Override
//	protected boolean canContainNull() {
//		return true;
//	}
	
	
	/**
	 * @param propertyList the propertyList to set
	 */
	public void setPropertyList(List<Property> propertyList) {
		this.propertyList = propertyList;
		try {
			resetListElements();
		} catch (BadStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Reset the elements of the list, according to the map and the 
	 * propertyList.
	 * @throws BadStateException 
	 * 
	 */
	private void resetListElements() throws BadStateException {
		// Reset list
		this.clear();

		// Check application
		if(propertyList== null) {
			// do not synchronize the list
			return;
//			throw new BadStateException("Property 'application' must be set for resetAllPropertyValuesFromRegistry() to work.");
		}
		
		List<Property> availableProperties;
		int size;
		try {
			availableProperties = propertyList;
			size = availableProperties.size();
		} catch (NullPointerException e) {
			throw new BadStateException("Property 'application.propertyRegistry' must be set for resetAllPropertyValuesFromRegistry() to work.");
		}
		
		// initialize the list
		for(int i=0;i<size;i++) {
			String propertyName = availableProperties.get(i).getName();
			// Add the value, or null if not found. This ensure
			// That the list will have the correct size.
			T value = map.get(propertyName);
			if( value == null ) {
				value = NULL_VALUE;
			} 
			this.add( value);
		}
	}

	/**
	 * Get an element by its property.
	 * @param property
	 * @return
	 * @throws NotFoundException If the index of the property is not found.
	 */
	public T get(Property property) throws NotFoundException {
		try {
			return get( property.getIndex());
		} catch (IndexOutOfBoundsException e) {
			throw new NotFoundException("Can't get element for Property '" + property.getName()
					+ "'. ", e);
		}
	}
	
	/**
	 * Set the element for the specified Property.
	 * @param property
	 * @param ele
	 * @throws BadStateException When the associated map is not set.
	 */
	public void set( Property property, T value ) throws BadStateException {
		if( map == null) {
			throw new BadStateException("Can't set element for Property '" + property.getName()
					+ "'. The associated map should be set first");
			
		}
		
		// Set the element in the map
		map.put(property.getName(), value);
	}
	
	/**
	 * Synchronize the specified propertyName with the value in the {@link #propertyValues} list.
	 * @param propertyName
	 * @param value
	 * @throws NotFoundException 
	 */
	protected void synchronizePropertyValue(String propertyName, T value) throws BadStateException, NotFoundException {
		
		if( propertyList == null) {
			//silently fail
			return;
		}
		
		try {
			int propertyIndex = getPropertyIndex(propertyName);
			set(propertyIndex, value);
		} catch (NullPointerException e) {
			throw new BadStateException("propertyList should be set first.");
		}
	}
	

	/**
	 * Get a Property by its name.
	 * Lookup in the {@link #propertyList} property.
	 * 
	 * @param propertyName
	 * @return
	 * @throws NotFoundException
	 */
	protected int getPropertyIndex( String propertyName) throws NotFoundException {
		
		if(propertyName == null) {
			throw new NotFoundException("Null name not Allowed");
		}
		for( int i=0; i<propertyList.size(); i++) {
			if( propertyName.equals(propertyList.get(i).getName() ) ) {
				return i;
			}
		}
		
		// Not found
		throw new NotFoundException("No property found under name '" + propertyName + "'");

		
	}
	/**
	 * This class listen to #propertyValueMap, and synchronize propertyValues accordingly.
	 * 
	 * This adapter listen on the map's parent to know if a Property key is
	 * added or removed.
	 * 
	 */
	public class PropertyValuesSynchronizer extends AdapterImpl {
		
		
		@Override
		public void notifyChanged(Notification msg) {
			System.err.println("event " + msg.getEventType());
			
		
			// Check if the notification comes from the map
			if( msg.getFeatureID(mapParentClasstype) ==  MAP_FEATURE_ID) {
				notifyLayerPropertyValueMapChanged(msg);
			}
		}
		
		/**
		 * The {@link PropertyIndexedList#map} has changed. Synchronize this list.
		 * @param msg
		 */
		protected void notifyLayerPropertyValueMapChanged(Notification msg) {
			System.err.println("map changed " + msg.getEventType());
			switch(msg.getEventType()) {
			case Notification.SET:	
			{
				// A key is modified
				// Add the value to other list
				@SuppressWarnings("unchecked")
				Map.Entry<String, T> entry = (Map.Entry<String, T>)msg.getNewValue();
				System.out.println("SET - newValue=" + entry.getValue()
						 + ", key=" + entry.getKey());
				
//				String newKey = (String)msg.getNewValue();
				T value = entry.getValue();
				if( value != null) {
					try {
						synchronizePropertyValue(entry.getKey(), value);
//						synchronizePropertyValue(newKey, value);
					} catch (BadStateException e) {
						// Show error for debug
						e.printStackTrace();
					} catch (NotFoundException e) {
						// Show error for debug
						e.printStackTrace();
					}
				}
				
	
				break;
			}
			case Notification.UNSET:	
				// A key is added
				break;
			case Notification.ADD:	
			{
				// An entry is added
				@SuppressWarnings("unchecked")
				Map.Entry<String, T> entry = (Map.Entry<String, T>)msg.getNewValue();
				System.out.println("ADD - newValue=" + entry.getValue()
						 + ", key=" + entry.getKey());
				// Add the corresponding instance to propertyValues
				try {
					synchronizePropertyValue(entry.getKey(), entry.getValue());
				} catch (LayersException e) {
					// should not happen
					e.printStackTrace();
				}
				break;
			}
			case Notification.REMOVE:	
			{
				// An entry is removed
				@SuppressWarnings("unchecked")
				Map.Entry<String, T> entry = (Map.Entry<String, T>)msg.getNewValue();
				System.out.println("REMOVE" + entry.getValue()
						 + ", key=" + entry.getKey());
				// Add the corresponding instance to propertyValues
				try {
					synchronizePropertyValue(entry.getKey(), NULL_VALUE);
				} catch (LayersException e) {
					// should not happen
					e.printStackTrace();
				}
				break;
			}
			default:
				break;
			}
	
		}
	
	}

}

Back to the top