Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cd21f4911145699dca2f2cf94bdda102feac39f6 (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
377
378
379
380
381
382
/*******************************************************************************
 * Copyright (c) 2006 - 2007 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:
 *     CEA LIST - initial API and implementation
 *******************************************************************************/
package org.eclipse.papyrus.views.panels;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.papyrus.C_Cpp.Array;
import org.eclipse.papyrus.C_Cpp.Const;
import org.eclipse.papyrus.C_Cpp.Ptr;
import org.eclipse.papyrus.C_Cpp.Ref;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.papyrus.views.cpp.CommandSupport;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;


/**
 * Panel displayed when a Property is selected
 */
public class CppPropertyPanel extends CppAbstractPanel {

	private Button isStatic;

	private Button isConst;

	private Property selectedProperty;

	// document used by the viewer
	private IDocument docPtr;

	private IDocument docRef;

	private IDocument docArray;

	private IDocument docDefault;

	@SuppressWarnings("unused")
	private SourceViewer viewerPtr;

	@SuppressWarnings("unused")
	private SourceViewer viewerRef;

	@SuppressWarnings("unused")
	private SourceViewer viewerArray;

	@SuppressWarnings("unused")
	private SourceViewer viewerDefault;

	private Group groupPtr;

	private Group groupRef;

	private Group groupArray;

	private Group groupDefault;

	public CppPropertyPanel(Composite parent, int style) {
		super(parent, style);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cea.accordcpp.core.ui.panels.AccordCppAbstractPanel#getSelectedElement()
	 */
	@Override
	public Property getSelectedElement() {
		return selectedProperty;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cea.accordcpp.core.ui.panels.AccordCppAbstractPanel#setSelectedElement(java.lang.Element)
	 */
	@Override
	public void setSelectedElement(Element newElement) {
		super.setSelectedElement(newElement);
		if (newElement instanceof Property) {
			this.selectedProperty = (Property) newElement;
		}
		else {
			throw new RuntimeException("bad selection: " + newElement + " should be an uml2 Property");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cea.accorduml.ui.views.panels.AccordUMLAbstractPanel#createContent()
	 */
	@Override
	public Control createContent() {
		// /////////////////////////////////////////////////////////////////////
		// Create save reset buttons with superclass method
		// /////////////////////////////////////////////////////////////////////
		createSaveResetButtons();

		// /////////////////////////////////////////////////////////////////////
		// Create checkboxes
		// /////////////////////////////////////////////////////////////////////
		isStatic = createButton("isStatic", this, null);
		isConst = createButton("isConst", this, isStatic);

		// /////////////////////////////////////////////////////////////////////
		// Add checkboxes listeners
		// /////////////////////////////////////////////////////////////////////
		isStatic.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				updateModel();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		addStereotypeSelectionListener(isConst, "Const");

		// /////////////////////////////////////////////////////////////////////
		// Pointer declaration
		// /////////////////////////////////////////////////////////////////////
		docPtr = createDocument();
		groupPtr = createGroup(
				this
				, "Pointer declaration"
				, buttonSave
				, null
				, false
				, 0
				, 25
				, false);

		// Use CDT CEditor coloration
		viewerPtr = createViewer(docPtr, groupPtr);

		// /////////////////////////////////////////////////////////////////////
		// Pointer declaration
		// /////////////////////////////////////////////////////////////////////
		docRef = createDocument();
		groupRef = createGroup(
				this
				, "Reference declaration"
				, buttonSave
				, groupPtr
				, false
				, 0
				, 50
				, false);

		// Use CDT CEditor coloration
		viewerRef = createViewer(docRef, groupRef);

		// /////////////////////////////////////////////////////////////////////
		// Pointer declaration
		// /////////////////////////////////////////////////////////////////////
		docDefault = createDocument();
		groupDefault = createGroup(
				this
				, "Default value"
				, buttonSave
				, groupRef
				, false
				, 0
				, 75
				, false);

		// Use CDT CEditor coloration
		viewerDefault = createViewer(docDefault, groupDefault);

		// /////////////////////////////////////////////////////////////////////
		// Pointer declaration
		// /////////////////////////////////////////////////////////////////////
		docArray = createDocument();
		groupArray = createGroup(
				this
				, "Array value ([...])"
				, buttonSave
				, groupDefault
				, true
				, 0
				, 0
				, false);

		// Use CDT CEditor coloration
		viewerArray = createViewer(docArray, groupArray);

		// /////////////////////////////////////////////////////////////////////
		// Return control
		// /////////////////////////////////////////////////////////////////////

		return this;
	}

	/**
	 * Saves the body for an '<code>Property</code>'
	 */
	@Override
	public void save()
	{
		if (selectedProperty == null) {
			/* Log.debug("saveBody : selectedProperty is null"); */
		}
		else {
			CommandSupport.exec("C++ package save", new Runnable() {

				@Override
				public void run()
				{
					// Treat Pointer
					String newPtr = docPtr.get();
					if (newPtr.equals("")) {
						StereotypeUtil.unapply(selectedProperty, Ptr.class);
					}
					else {
						StereotypeUtil.apply(selectedProperty, Ptr.class);
					}

					// Treat Reference
					String newRef = docRef.get();
					if (newRef.equals("")) {
						StereotypeUtil.unapply(selectedProperty, Ref.class);
					}
					else {
						StereotypeUtil.apply(selectedProperty, Ref.class);
					}

					// Treat DefaultValue
					// need common class for facilitating this
					// selectedParameter.setDefaultValue(selectedParameter.createDefaultValue("default", selectedParameter.getType(), null);

					// Treat Array
					String newArray = docArray.get();
					if (newArray.equals("")) {
						StereotypeUtil.unapply(selectedProperty, Array.class);
					}
					else {
						StereotypeUtil.apply(selectedProperty, Array.class);
					}
				}
			});
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cea.accordcpp.core.ui.panels.AccordCppAbstractPanel#refreshPanel()
	 */
	@Override
	protected void refreshPanel() {
		if (selectedProperty == null) {
			/* Log.debug("resetBody : selectedProperty is null"); */
		}
		else {
			isStatic.setSelection(selectedProperty.isStatic());
			isConst.setSelection(StereotypeUtil.isApplied(selectedProperty, Const.class));
			docPtr.set(StereotypeUtil.isApplied(selectedProperty, Ptr.class) ? "*" : "");
			docRef.set(StereotypeUtil.isApplied(selectedProperty, Ref.class) ? "&" : "");
			docDefault.set(selectedProperty.getDefault());
			docArray.set(StereotypeUtil.isApplied(selectedProperty, Array.class) ? "[]" : "");
		}
	}

	/**
	 * Called when the static checkbox is modified
	 */
	protected void checkStatic() {

		boolean boxState = isStatic.getSelection();

		selectedProperty.setIsStatic(boxState);
	}

	/**
	 * Called when the const checkbox is modified
	 */
	protected void checkConst() {
		boolean boxState = isConst.getSelection();

		if (StereotypeUtil.isApplied(selectedProperty, Const.class) != boxState) {
			if (boxState) {
				StereotypeUtil.apply(selectedProperty, Const.class);
			}
			else {
				StereotypeUtil.unapply(selectedProperty, Const.class);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cea.accorduml.ui.views.panels.AccordUMLAbstractPanel#entryAction()
	 */
	@Override
	public void entryAction() {
		super.entryAction();
		reset();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cea.accordcpp.core.ui.panels.AccordCppAbstractPanel#exitAction()
	 */
	@Override
	public void exitAction() {
		super.exitAction();
	}

	/**
	 * Checks if the content of the panel is different to the model
	 * <p>
	 * This is a read only operation
	 *
	 * @return <code>true</code> if one of the element of the model is not up-to-date
	 *         with the content of the panel
	 */
	@Override
	public boolean checkModifications() {
		String ptrValue = StereotypeUtil.isApplied(selectedProperty, Ptr.class) ? "*" : "";
		if (!docPtr.get().equals(ptrValue)) {
			return true;
		}

		String refValue = StereotypeUtil.isApplied(selectedProperty, Ref.class) ? "&" : "";
		if (!docRef.get().equals(refValue)) {
			return true;
		}

		String defaultValue = selectedProperty.getDefault();
		if (defaultValue == null) {
			if (!docDefault.get().equals("")) {
				return true;
			}
		}
		else if (!docDefault.get().equals(defaultValue)) {
			return true;
		}

		String arrayValue = StereotypeUtil.isApplied(selectedProperty, Array.class) ? "[]" : "";
		if (!docArray.get().equals(arrayValue)) {
			return true;
		}
		return false;

	}


	@Override
	protected void updateModel()
	{
		CommandSupport.exec("C++ property save", new Runnable() {

			@Override
			public void run()
			{
				// Check button changes
				checkStatic();
				checkConst();

			}
		});
	}
}

Back to the top