Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a9cc934ce410b299f0f2ea8746dc3cceb5ae58c6 (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
/*****************************************************************************
 * 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.properties.runtime.view;

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

import org.eclipse.papyrus.properties.runtime.state.AbstractState;
import org.eclipse.papyrus.properties.runtime.state.IFragmentDescriptorState;
import org.eclipse.papyrus.properties.runtime.view.constraints.ConstraintDescriptorState;
import org.eclipse.papyrus.properties.runtime.view.constraints.IConstraintDescriptor;
import org.eclipse.papyrus.properties.runtime.view.content.ContainerDescriptor;
import org.eclipse.papyrus.properties.runtime.view.content.ContainerDescriptorState;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


/**
 * state for Fragment descriptor
 */
public class FragmentDescriptorState extends AbstractState implements IFragmentDescriptorState {

	/** descriptor managed by this state */
	private FragmentDescriptor descriptor;

	/** list of container descriptors state children of this state */
	private final List<ContainerDescriptorState> containerDescriptorStates = new ArrayList<ContainerDescriptorState>();

	/** list of constraints descriptors state children of this state */
	private final List<ConstraintDescriptorState> constraintDescriptorStates = new ArrayList<ConstraintDescriptorState>();

	/** selection size state */
	private int selectionSize;

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

	/** id of the fragment managed by this state */
	private String id;

	/**
	 * Creates a new FragmentDescriptorState.
	 * 
	 * @param descriptor
	 *        the fragment descriptor managed by this state
	 * @param readOnly
	 *        <code>true</code> if the state should not be modifiable
	 */
	public FragmentDescriptorState(FragmentDescriptor descriptor, boolean readOnly) {
		super(readOnly);
		this.descriptor = descriptor;

		id = descriptor.getId();

		// retrieve and build the states for the container children
		List<ContainerDescriptor> containerDescriptors = descriptor.getContainerDescriptors();
		for(ContainerDescriptor containerDescriptor : containerDescriptors) {
			containerDescriptorStates.add(containerDescriptor.createState(readOnly));
		}

		for(IConstraintDescriptor constraintDescriptor : descriptor.getConstraintDescriptors()) {
			getConstraintDescriptorStates().add(constraintDescriptor.createState(readOnly));
		}

		selectionSize = descriptor.getSelectionSize();

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

	public FragmentDescriptor getDescriptor() {
		return descriptor;
	}


	/**
	 * Returns the state for the selection size
	 * 
	 * @return the state for the selection size
	 */
	public int getSelectionSize() {
		return selectionSize;
	}

	/**
	 * Sets the state for the selection size
	 * 
	 * @param selectionSize
	 *        the selectionSize to set
	 */
	public void setSelectionSizeState(int selectionSize) {
		int oldSize = this.selectionSize;
		this.selectionSize = selectionSize;

		changeSupport.firePropertyChange("selectionSize", oldSize, this.selectionSize);
	}

	/**
	 * Returns the containerDescriptor States for this fragment
	 * 
	 * @return the containerDescriptor States for this fragment
	 */
	public List<ContainerDescriptorState> getContainerDescriptorStates() {
		return containerDescriptorStates;
	}

	/**
	 * Returns the constraintDescriptor States for this fragment
	 * 
	 * @return the constraintDescriptor States for this fragment
	 */
	public List<ConstraintDescriptorState> getConstraintDescriptorStates() {
		return constraintDescriptorStates;
	}

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

	/**
	 * {@inheritDoc}
	 */
	public String getText() {
		return "Fragment: " + getId() + " (size: " + getSelectionSize() + ")";
	}

	/**
	 * Adds a property change listener to this class
	 * 
	 * @param listener
	 *        the listener to add
	 */
	public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
		changeSupport.addPropertyChangeListener(listener);
	}

	/**
	 * Removes a property change listener from this class
	 * 
	 * @param listener
	 *        the listener to remove
	 */
	public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
		changeSupport.removePropertyChangeListener(listener);
	}

	/**
	 * Adds the {@link ContainerDescriptorState} to the list of descriptor states belonging to this fragment
	 * 
	 * @param containerDescriptorState
	 *        the state to add
	 */
	public void addContainerDescriptorState(ContainerDescriptorState containerDescriptorState) {
		containerDescriptorStates.add(containerDescriptorState);

		changeSupport.firePropertyChange(PROPERTY_ADD_CHILD, null, containerDescriptorStates);
	}

	/**
	 * Removes the {@link ContainerDescriptorState} from the list of descriptor states belonging to this fragment
	 * 
	 * @param containerDescriptorState
	 *        the state to remove
	 */
	public void removeContainerDescriptorState(ContainerDescriptorState containerDescriptorState) {
		containerDescriptorStates.remove(containerDescriptorState);

		changeSupport.firePropertyChange(PROPERTY_REMOVE_CHILD, null, containerDescriptorStates);
	}

	/**
	 * Adds the {@link ContainerDescriptorState} to the list of descriptor states belonging to this fragment
	 * 
	 * @param constraintDescriptorState
	 *        the state to add
	 */
	public void addConstraintDescriptorState(ConstraintDescriptorState constraintDescriptorState) {
		constraintDescriptorStates.add(constraintDescriptorState);

		changeSupport.firePropertyChange(PROPERTY_ADD_CHILD, null, constraintDescriptorState);
	}

	/**
	 * Removes the {@link ContainerDescriptorState} from the list of descriptor states belonging to this fragment
	 * 
	 * @param constraintDescriptorState
	 *        the state to remove
	 */
	public void removeContainerDescriptorState(ConstraintDescriptorState constraintDescriptorState) {
		constraintDescriptorStates.remove(constraintDescriptorState);

		changeSupport.firePropertyChange(PROPERTY_REMOVE_CHILD, null, constraintDescriptorState);
	}

	/**
	 * {@inheritDoc}
	 */
	public List<ContainerDescriptorState> getChildren() {
		return getContainerDescriptorStates();
	}

	/**
	 * {@inheritDoc}
	 */
	public Node generateNode(Document document) {
		Element node = document.createElement("fragment");

		generateAttributes(node, document);

		// generate for children(containers and constraints)
		generateConstraints(node, document);
		generateContainers(node, document);

		return node;
	}

	/**
	 * Generates attributes for this fragment
	 * 
	 * @param node
	 *        the node to complete
	 * @param document
	 *        the document used to create nodes
	 */
	protected void generateAttributes(Element node, Document document) {
		node.setAttribute("id", getId());

	}

	/**
	 * Generates the constraints for the fragment descriptor
	 * 
	 * @param node
	 *        the parent node for generated nodes
	 * @param document
	 *        the document used to create elements
	 */
	protected void generateConstraints(Element node, Document document) {
		Element contentElement = document.createElement("context");
		// add the enables for attribute
		contentElement.setAttribute("enablesFor", "" + getSelectionSize());

		// generate for each constraint
		for(ConstraintDescriptorState state : getConstraintDescriptorStates()) {
			contentElement.appendChild(state.generateNode(document));
		}
		node.appendChild(contentElement);

	}

	/**
	 * Generate the children nodes for all containers
	 * 
	 * @param node
	 *        the parent node for generated nodes
	 * @param document
	 *        the document used to create elements
	 */
	protected void generateContainers(Element node, Document document) {
		Element contentElement = document.createElement("content");
		for(ContainerDescriptorState state : getContainerDescriptorStates()) {
			contentElement.appendChild(state.generateNode(document));
		}
		node.appendChild(contentElement);
	}

	/**
	 * Sets the id
	 * 
	 * @param id
	 *        the id to set
	 */
	public void setId(String id) {
		changeSupport.firePropertyChange("id", this.id, this.id = id);
	}

	/**
	 * Returns the id
	 * 
	 * @return the id
	 */
	public String getId() {
		return id;
	}
}

Back to the top