Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0f6d4ef8fd7b34932b607584c17622a7c4a22ba6 (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
/*****************************************************************************
 * Copyright (c) 2014 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.papyrusrt.umlrt.tooling.ui.widgets;

import java.util.Collections;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.papyrus.uml.properties.creation.UMLPropertyEditorFactory;
import org.eclipse.papyrus.views.properties.contexts.Context;
import org.eclipse.papyrus.views.properties.contexts.View;
import org.eclipse.papyrus.views.properties.creation.CreationContext;
import org.eclipse.papyrus.views.properties.runtime.ConfigurationManager;
import org.eclipse.papyrusrt.umlrt.profile.UMLRealTime.RTMessageKind;
import org.eclipse.papyrusrt.umlrt.tooling.ui.Messages;
import org.eclipse.swt.widgets.Control;
import org.eclipse.uml2.uml.UMLFactory;

/**
 * ValueFactory for the MessageSet ownedOperation (protocol message for message sets)
 */
public class MessageSetOwnedProtocolMessageValueFactory extends UMLPropertyEditorFactory {

	public static final String SINGLE_PROTOCOL_MESSAGE = "Single ProtocolMessage"; //$NON-NLS-1$

	public static final String UML_RT = "uml-rt"; //$NON-NLS-1$

	private RTMessageKind direction = null;

	public MessageSetOwnedProtocolMessageValueFactory(EReference eReference) {
		super(eReference);
	}

	/**
	 * Set the direction of the protocol messages handled by this factory, e.g. to make
	 * it clear in create and edit dialogues which direction the protocol message has
	 *
	 * @param direction The direction of the protocol messages handled by this factory
	 * @return The factory itself
	 */
	public MessageSetOwnedProtocolMessageValueFactory setDirection(RTMessageKind direction) {
		this.direction = direction;
		return this;
	}

	@Override
	protected EObject simpleCreateObject(Control widget) {
		return UMLFactory.eINSTANCE.createOperation();
	}

	@Override
	public Object edit(Control widget, Object source) {
		Set<View> views = getProtocolMessageViews();
		if (!views.isEmpty()) {
			return doEdit(widget, source, views, getEditionDialogTitle(source));
		}

		return source;
	}

	@Override
	protected Object createObject(Control widget, Object context, Object source) {
		if (source == null) {
			return null;
		}

		Set<View> views = getProtocolMessageViews();
		if (!views.isEmpty()) {
			CreationContext creationContext = getCreationContext(context);
			creationContext.pushCreatedElement(source);
			try {
				return doEdit(widget, source, views, getCreationDialogTitle());
			} finally {
				creationContext.popCreatedElement(source);
			}
		}

		return source;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getCreationDialogTitle() {
		switch (direction) {
		case OUT:
			return Messages.MessageSetOwnedProtocolMessageValueFactory_CreateOutProtocolMessageDialogTitle;

		case IN:
			return Messages.MessageSetOwnedProtocolMessageValueFactory_CreateInProtocolMessageDialogTitle;

		case IN_OUT:
			return Messages.MessageSetOwnedProtocolMessageValueFactory_CreateInOutProtocolMessageDialogTitle;

		default:
			return Messages.MessageSetOwnedProtocolMessageValueFactory_CreateDialogTitle;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getEditionDialogTitle(Object objectToEdit) {
		switch (direction) {
		case OUT:
			return Messages.MessageSetOwnedProtocolMessageValueFactory_EditOutProtocolMessageDialogTitle;

		case IN:
			return Messages.MessageSetOwnedProtocolMessageValueFactory_EditInProtocolMessageDialogTitle;

		case IN_OUT:
			return Messages.MessageSetOwnedProtocolMessageValueFactory_EditInOutProtocolMessageDialogTitle;

		default:
			return Messages.MessageSetOwnedProtocolMessageValueFactory_EditDialogTitle;
		}
	}

	protected Set<View> getProtocolMessageViews() {
		String contextName = UML_RT;
		String viewName = SINGLE_PROTOCOL_MESSAGE;
		Context context = ConfigurationManager.getInstance().getContext(contextName);
		for (View view : context.getViews()) {
			if (viewName.equals(view.getName())) {
				return Collections.singleton(view);
			}
		}
		return Collections.emptySet();
	}
}

Back to the top