Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 25bfd8992eca483874ff022660845889851e7c56 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*******************************************************************************
 * 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.ISourceViewer;
import org.eclipse.papyrus.C_Cpp.Const;
import org.eclipse.papyrus.C_Cpp.ConstInit;
import org.eclipse.papyrus.C_Cpp.Friend;
import org.eclipse.papyrus.C_Cpp.Inline;
import org.eclipse.papyrus.C_Cpp.Virtual;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.papyrus.views.cpp.CommandSupport;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.OpaqueBehavior;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.profile.standard.Create;
import org.eclipse.uml2.uml.profile.standard.Destroy;
import org.eclipse.uml2.uml.util.UMLUtil;


/**
 * Panel displayed when an Operation is selected
 *
 * TODO: instead of keeping isAbstract and pureVirtual Stereotype synchronized, pure virtual
 * could be removed from the panel
 * @deprecated This view will be replaced by a XWT view for the C++ profile
 */
@Deprecated
public class CppOperationPanel extends CppAbstractPanel {

	@SuppressWarnings("unused")
	private ISourceViewer viewerBody;

	@SuppressWarnings("unused")
	private ISourceViewer viewerConstInit;

	private Group groupBody;

	private Group groupCInit;

	// document used by the viewer
	private IDocument docBody;

	private IDocument docConstInit;

	private Button isStatic;

	private Button isInline;

	private Button isConst;

	private Button isFriend;

	private Button isCreate;

	private Button isDestroy;

	private Combo vPropCombo;

	private Operation selectedOperation;

	Element selectedEOwner;

	protected String origBody;
	
	public CppOperationPanel(Composite parent, int style) {
		super(parent, style);
	}

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

	/*
	 * (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 Operation) {
			this.selectedOperation = (Operation) newElement;
			// Retrieve the owner
			this.selectedEOwner = selectedOperation.getOwner();
		}
		else {
			throw new RuntimeException("bad selection: " + newElement + " should be an uml2 Operation");
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.cea.accorduml.ui.views.panels.AccordUMLAbstractPanel#createContent()
	 */
	@Override
	public Control createContent()
	{
		// /////////////////////////////////////////////////////////////////////
		// Create checkboxes
		// /////////////////////////////////////////////////////////////////////
		isStatic = createButton("isStatic", this, null);
		isInline = createButton("isInline", this, isStatic);
		isConst = createButton("isConst", this, isInline);
		isFriend = createButton("isFriend", this, isConst);
		isCreate = createButton("isCreate", this, isFriend);
		isDestroy = createButton("isDestroy", this, isCreate);

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

			@Override
			public void widgetSelected(SelectionEvent e) {
				CommandSupport.exec("C++ operation save", new Runnable() {

					@Override
					public void run() {
						updateModel();
					}
				});
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		addStereotypeSelectionListener(isInline, "Inline");
		addStereotypeSelectionListener(isConst, "Const");
		addStereotypeSelectionListener(isFriend, "Friend");
		addStereotypeSelectionListener(isCreate, "Create");
		addStereotypeSelectionListener(isDestroy, "Destroy");

		// /////////////////////////////////////////////////////////////////////
		// Virtual properties combo box
		// /////////////////////////////////////////////////////////////////////
		vPropCombo = new Combo(this, SWT.DROP_DOWN | SWT.READ_ONLY);
		String items[] = { "not virtual", "virtual", "pure virtual" };
		vPropCombo.setItems(items);
		vPropCombo.setText("Virtual operation qualifiers");
		vPropCombo.addSelectionListener(
				new SelectionAdapter() {

					@Override
					public void widgetSelected(SelectionEvent event) {
						checkVirtual();
						reset();
					}
				});

		FormData data = new FormData();
		data.left = new FormAttachment(isDestroy, H_SPACE);
		data.top = new FormAttachment(0, H_SPACE);
		vPropCombo.setLayoutData(data);

		// /////////////////////////////////////////////////////////////////////
		// Create save reset buttons with superclass method
		// /////////////////////////////////////////////////////////////////////
		createSaveResetButtons();

		// /////////////////////////////////////////////////////////////////////
		// Constructor init list
		// /////////////////////////////////////////////////////////////////////
		docConstInit = createDocument();
		groupCInit = createGroup(
				this
				, "Constructor initialisation list"
				, isStatic
				, null
				, true
				, 0
				, 0
				, false);

		// Use CDT CEditor coloration
		viewerConstInit = createViewer(docConstInit, groupCInit);

		// /////////////////////////////////////////////////////////////////////
		// Body
		// /////////////////////////////////////////////////////////////////////

		docBody = createDocumentC();
		groupBody = createGroup(
				this
				, "Method body"
				, groupCInit
				, null
				, true
				, 0
				, 0
				, true);
		// Use CDT CEditor coloration
		viewerBody = createViewerC(docBody, groupBody);

		// /////////////////////////////////////////////////////////////////////

		return this;
	}



	/*
	 * (non-Javadoc)
	 *
	 * @see com.cea.accordcpp.core.ui.panels.AccordCppAbstractPanel#save()
	 */
	@Override
	public void save()
	{
		if (selectedOperation == null) {
			/* Log.debug("saveBody : selectedOperation is null"); */
		} else {
			CommandSupport.exec("C++ operation save", new Runnable() {

				@Override
				public void run() {
					String newConstInit = docConstInit.get();
					if (newConstInit.equals("")) { //$NON-NLS-1$
						StereotypeUtil.unapply(selectedOperation, ConstInit.class);
					} else {
						ConstInit cppConstInit =
								StereotypeUtil.applyApp(selectedOperation, ConstInit.class);
						cppConstInit.setInitialisation(newConstInit);
					}

					// Body
					setCppBody(selectedOperation, docBody.get());
					origBody = docBody.get();
				}
			});
		}

	}

	public static String getCppBody(Operation operation)
	{
		// get the body of the operation
		for (Behavior behavior : operation.getMethods()) {
			if (behavior instanceof OpaqueBehavior) {
				OpaqueBehavior ob = (OpaqueBehavior) behavior;
				int i = 0;
				for (String language : ob.getLanguages()) {
					if (language.equals(C_CPP_ID)) {
						return ob.getBodies().get(i);
					}
					else if (language.equals(CPP_ID)) {
						return ob.getBodies().get(i);
					}
					else if (language.equals(C_ID)) {
						return ob.getBodies().get(i);
					}
					i++;
				}
			}
		}
		return ""; //$NON-NLS-1$
	}

	public static void setCppBody(Operation operation, String body)
	{
		boolean done = false;
		for (Behavior behavior : operation.getMethods()) {
			if (behavior instanceof OpaqueBehavior) {
				OpaqueBehavior ob = (OpaqueBehavior) behavior;
				int i = 0;
				for (String language : ob.getLanguages()) {
					if (language.equals(C_CPP_ID) || language.equals(CPP_ID) || language.equals(C_ID)) {
						done = true;
						ob.getBodies().set(i, body);
						break;
					}
					i++;
				}
			}
			if (done) {
				break;
			}
		}
		if (!done) {
			OpaqueBehavior ob = (OpaqueBehavior)
					operation.getClass_().createOwnedBehavior(operation.getName(), UMLPackage.eINSTANCE.getOpaqueBehavior());
			ob.getLanguages().add(C_CPP_ID);
			ob.getBodies().add(body);
			ob.setSpecification(operation);
		}

	}

	/**
	 * Called when the static checkbox is modified
	 */
	protected void checkStatic()
	{
		boolean boxState = isStatic.getSelection();

		selectedOperation.setIsStatic(boxState);
	}

	/**
	 * Called when the inline checkbox is modified
	 */
	protected void checkInline() {
		// get the text in the activity
		boolean boxState = isInline.getSelection();

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

	/**
	 * Called when the const checkbox is modified
	 */
	protected void checkConst() {

		boolean boxState = isConst.getSelection();

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

	/**
	 * Called when the friend checkbox is modified
	 */
	protected void checkFriend() {

		boolean boxState = isFriend.getSelection();

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

	/**
	 * Called when the create checkbox is modified
	 */
	protected void checkCreate() {
		// get the text in the activity
		boolean boxState = isCreate.getSelection();

		if (boxState) {
			StereotypeUtil.apply(selectedOperation, Create.class);
		}
		else {
			StereotypeUtil.unapply(selectedOperation, ConstInit.class);
			StereotypeUtil.unapply(selectedOperation, Create.class);
		}

		groupCInit.setEnabled(boxState);
	}

	/**
	 * Called when the destroy checkbox is modified
	 */
	protected void checkDestroy() {
		// get the text in the activity
		boolean boxState = isDestroy.getSelection();

		if (boxState) {
			StereotypeUtil.apply(selectedOperation, Destroy.class);
		}
		else {
			StereotypeUtil.unapply(selectedOperation, Destroy.class);
		}
	}

	/**
	 * Called when the virtual qualifier is modified
	 */
	protected void checkVirtual() {

		// Treat the operation
		int comboSelected = 2;

		// If owner is a class
		if (selectedEOwner instanceof org.eclipse.uml2.uml.Class) {
			comboSelected = vPropCombo.getSelectionIndex();
		} else {
			// Assume this is an interface
			comboSelected = 2;
		}

		if (comboSelected == 0 /* not virtual */) {

			selectedOperation.setIsAbstract(false);
			StereotypeUtil.unapply(selectedOperation, Virtual.class);
		}
		else if (comboSelected == 1 /* virtual */) {

			selectedOperation.setIsAbstract(false);
			StereotypeUtil.apply(selectedOperation, Virtual.class);
		}
		else if (comboSelected == 2 /* pure virtual */) {

			selectedOperation.setIsAbstract(true);
			StereotypeUtil.apply(selectedOperation, Virtual.class);
		}

		// Treat the owner class
		if (selectedEOwner instanceof Class) {
			Class ownerCl = (Class) selectedEOwner;

			ownerCl.setIsAbstract(ownerCl.isAbstract());
		} // else nothing
	}

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

	/*
	 * (non-Javadoc)
	 *
	 * @see com.cea.accorduml.ui.views.panels.AccordUMLAbstractPanel#exitAction()
	 */
	@Override
	public void exitAction() {
		// do exit action, only if the owner is a class
		if (selectedEOwner instanceof Class) {
			super.exitAction();
		}
	}

	@Override
	public boolean checkModifications() {
		// check if ConstInit has changed
		String valueConstInit = ""; //$NON-NLS-1$
		ConstInit constInit = UMLUtil.getStereotypeApplication(selectedOperation, ConstInit.class);
		if (constInit != null) {
			valueConstInit = constInit.getInitialisation();
		}
		if (!docConstInit.get().equals(valueConstInit)) {
			return true;
		}

		// String methodBody = getCppBody(selectedOperation);
		if (!(docBody.get().equals(origBody))) {
			return true;
		}

		return false;
	}

	@Override
	protected void refreshPanel() {
		if (selectedOperation == null) {
			/* Log.debug("resetBody : selectedOperation is null"); */
		} else {

			// get the text in the activity if parent is a class
			if (selectedEOwner instanceof Class) {

				// This part is done for a class only
				ConstInit constInit = UMLUtil.getStereotypeApplication(selectedOperation, ConstInit.class);
				if (constInit != null) {
					docConstInit.set(constInit.getInitialisation());
				}

				// Don't show this unless the operation is a constructor
				if (isCreate.getSelection()) {
					groupCInit.setEnabled(true);
				} else {
					docConstInit.set(""); //$NON-NLS-1$
					groupCInit.setEnabled(false);
				}

				String body = getCppBody(selectedOperation);
				origBody = body;
				docBody.set(body);

				// Combo Box
				if (selectedOperation.isAbstract()) {
					vPropCombo.select(2);
					// Nothing in body and body unavailable
					docBody.set(""); //$NON-NLS-1$
					groupBody.setEnabled(false);
				}
				else {
					if (StereotypeUtil.isApplied(selectedOperation, Virtual.class)) {
						vPropCombo.select(1);
						groupBody.setEnabled(true);
					}
					else {
						vPropCombo.select(0);
						groupBody.setEnabled(true);
					}
				}

				// Inline
				if (StereotypeUtil.isApplied(selectedOperation, Inline.class)) {
					isInline.setSelection(true);
				}

			}
			else {
				// Don't show text areas nor buttons
				groupBody.setVisible(false);
				buttonSave.setVisible(false);
				buttonCancel.setVisible(false);

				// Methods are virtual pure
				vPropCombo.select(2);
				vPropCombo.setEnabled(false);

				// Inline not visible
				isInline.setVisible(false);
			}

			isStatic.setSelection(selectedOperation.isStatic());
			isConst.setSelection(StereotypeUtil.isApplied(selectedOperation, Const.class));
			isFriend.setSelection(StereotypeUtil.isApplied(selectedOperation, Friend.class));
			isCreate.setSelection(StereotypeUtil.isApplied(selectedOperation, Create.class));
			isDestroy.setSelection(StereotypeUtil.isApplied(selectedOperation, Destroy.class));
		}
	}

	@Override
	protected boolean isModelValid() {
		if (selectedOperation == null) {
			return true;
		}
		if (selectedEOwner instanceof Class) {

			if (selectedOperation.isAbstract()) {
				if (!StereotypeUtil.isApplied(selectedOperation, Virtual.class)) {
					return false;
				}
			}
		} else {
			// owner is interface
			if (!selectedOperation.isAbstract()) {
				return false;
			}
			if (StereotypeUtil.isApplied(selectedOperation, Virtual.class)) {
				return false;
			}
		}
		return true;

	}

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

			@Override
			public void run()
			{
				// Check button changes
				checkStatic();
				checkConst();
				checkCreate();
				checkDestroy();
				checkInline();
				checkFriend();

				if (selectedOperation == null) {
					return;
				}
				if (selectedEOwner instanceof Class) {
					// toggle Stereotypes pure virtual if element is abstract
					if (selectedOperation.isAbstract()) {
						if (!StereotypeUtil.isApplied(selectedOperation, Virtual.class)) {
							// selectedOperation.toggleStereotype("VirtualPure", true);
							// selectedOperation.toggleStereotype("Virtual", false);
						} else {
							// selectedOperation.toggleStereotype("Virtual", false);
						}
					}

					// if element is abstract and has VirtualPure, set to Virtual only
					if (!selectedOperation.isAbstract()) {
						if (StereotypeUtil.isApplied(selectedOperation, Virtual.class)) {
							// selectedOperation.toggleStereotype("VirtualPure", false);
							// selectedOperation.toggleStereotype("Virtual", true);
						} else {
							// selectedOperation.toggleStereotype("VirtualPure", false);
							// selectedOperation.toggleStereotype("Virtual", false);
						}
					}
				}
				else {
					selectedOperation.setIsAbstract(true);
					// selectedOperation.toggleStereotype("Virtual", false);
					// selectedOperation.toggleStereotype("VirtualPure", true);
				}
			}
		});
	}
}

Back to the top