Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3e223fc50e963183a30c8cbc347aa3f065e4df73 (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
/*******************************************************************************
 * Copyright (c) 2010 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * CONTRIBUTORS:
 * 		Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.ui.structure.dialogs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.ActorContainerClass;
import org.eclipse.etrice.core.room.ActorContainerRef;
import org.eclipse.etrice.core.room.ActorRef;
import org.eclipse.etrice.core.room.LogicalSystem;
import org.eclipse.etrice.core.room.Port;
import org.eclipse.etrice.core.room.ReferenceType;
import org.eclipse.etrice.core.room.RoomPackage;
import org.eclipse.etrice.core.room.StructureClass;
import org.eclipse.etrice.core.room.SubSystemClass;
import org.eclipse.etrice.core.room.SubSystemRef;
import org.eclipse.etrice.ui.common.base.dialogs.AbstractPropertyDialog;
import org.eclipse.etrice.ui.common.base.dialogs.MultiValidator2;
import org.eclipse.etrice.ui.structure.Activator;
import org.eclipse.etrice.ui.structure.dialogs.PortPropertyDialog.Multiplicity2StringConverter;
import org.eclipse.etrice.ui.structure.dialogs.PortPropertyDialog.String2MultiplicityConverter;
import org.eclipse.etrice.ui.structure.support.SupportUtil;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;

@SuppressWarnings("rawtypes")
public class ActorContainerRefPropertyDialog extends AbstractPropertyDialog {
	
	class NameValidator implements IValidator {

		@Override
		public IStatus validate(Object value) {
			if (value instanceof String) {
				String name = (String) value;
				
				if (name.isEmpty())
					return ValidationStatus.error("name must not be empty");
				
				// TODOHRR: check valid identifier
				// TODOHRR: use ValidationUtil
				
				if (sc instanceof ActorClass) {
					if (nameExists((ActorClass) sc, name))
						return ValidationStatus.error("name already exists");
				}
				else if (sc instanceof SubSystemClass) {
					SubSystemClass ssc = (SubSystemClass) sc;
					for (ActorRef ar : ssc.getActorRefs()) {
						if (ar!=ref && ar.getName().equals(name))
							return ValidationStatus.error("name already exists");
					}
				}
				else if (sc instanceof LogicalSystem) {
					LogicalSystem ssc = (LogicalSystem) sc;
					for (SubSystemRef ssr : ssc.getSubSystems()) {
						if (ssr!=ref && ssr.getName().equals(name))
							return ValidationStatus.error("name already exists");
					}
				}
				else {
					assert(false): "unexpected type";
				}
				return Status.OK_STATUS;
			}
			return Status.OK_STATUS;
		}

		private boolean nameExists(ActorClass ac, String name) {
			for (ActorRef ar : ac.getActorRefs()) {
				if (ar!=ref && ar.getName().equals(name))
					return true;
			}
			if (ac.getActorBase()!=null)
				return nameExists(ac.getActorBase(), name);
			
			return false;
		}
	}
	
	class ProtocolValidator implements IValidator {

		@Override
		public IStatus validate(Object value) {
			if (value==null)
				return ValidationStatus.error("select a class");
			
			return Status.OK_STATUS;
		}
	}
	
	class SizeAndRefTypeValidator extends MultiValidator2{

		public SizeAndRefTypeValidator(DataBindingContext bindingContext) {
			super(bindingContext, 2);
		}

		@Override
		public IStatus validate(List<Object> values) {
			int m = (Integer) values.get(0);
			ReferenceType rt = (ReferenceType) values.get(1);
			if (m==0)
				return ValidationStatus.error("multiplicity must not be 0");
			if (m<-1)
				return ValidationStatus.error("multiplicity must be -1 or positive");
			if (rt==ReferenceType.OPTIONAL) {
				if (m>1)
					return ValidationStatus.error("multiplicity >1 not allowed (only fixed actors)");
			}
			else {
				if (m==-1)
					return ValidationStatus.error("multiplicity * not allowed (only optional actors)");
			}
			
			return ValidationStatus.ok();
		}
		
	}
	
	class SizeValidator implements IValidator {

		@Override
		public IStatus validate(Object value) {
			if(value instanceof Integer){
				int m = (Integer) value;
				if (m==0)
					return ValidationStatus.error("multiplicity must not be 0");
				if (m<-1)
					return ValidationStatus.error("multiplicity must be -1 or positive");
			}
			
			return ValidationStatus.ok();
		}
	}

	private ActorContainerRef ref;
	private IScope scope;
	private StructureClass sc;
	private boolean newRef;

	public ActorContainerRefPropertyDialog(Shell shell, ActorContainerRef ref, IScope scope, StructureClass sc, boolean newRef) {
		super(shell, "Edit Reference");
		this.ref = ref;
		this.scope = scope;
		this.sc = sc;
		this.newRef = newRef;
	}

	@Override
	protected void initializeBounds() {
		super.initializeBounds();
		Point size = getShell().getSize();
		getShell().setSize((int)(size.x*1.2), size.y);
	}

	@Override
	protected void createContent(IManagedForm mform, Composite body,
			DataBindingContext bindingContext) {

		NameValidator nv = new NameValidator();
		ProtocolValidator pv = new ProtocolValidator();

		boolean refIsActor = sc instanceof ActorContainerClass;
		boolean containerIsActor = sc instanceof ActorClass;

		ArrayList<IEObjectDescription> actors = new ArrayList<IEObjectDescription>();
        Iterator<IEObjectDescription> it = scope.getAllElements().iterator();
        while (it.hasNext()) {
        	IEObjectDescription desc = it.next();
        	EObject obj = desc.getEObjectOrProxy();
        	if (refIsActor && obj instanceof ActorClass) {
        		if (containerIsActor) {
        			if (!SupportUtil.getInstance().getRoomHelpers().isReferencing((ActorClass)obj, (ActorClass)sc))
        				actors.add(desc);
        		}
        		else
        			actors.add(desc);
        	}
        	else if (!refIsActor && obj instanceof SubSystemClass) {
        		if (obj!=sc)
        			actors.add(desc);
        	}
		}
		
		Text name = createText(body, "&Name:", ref, RoomPackage.eINSTANCE.getActorContainerRef_Name(), nv);
		Combo refClass = refIsActor?
				createComboUsingDesc(body, "Actor &Class:", ref, ActorClass.class, RoomPackage.eINSTANCE.getActorRef_Type(), actors, pv)
			:	createComboUsingDesc(body, "SubSystem &Class:", ref, SubSystemClass.class, RoomPackage.eINSTANCE.getSubSystemRef_Type(), actors, pv);

		createDecorator(name, "invalid name");
		createDecorator(refClass, "no class selected");

		if (!newRef) {
			refClass.setEnabled(false);
			createInfoDecorator(refClass, "class fixed for exisiting ref");
		}

		if (ref instanceof ActorRef) {
			Multiplicity2StringConverter m2s = new Multiplicity2StringConverter();
			String2MultiplicityConverter s2m = new String2MultiplicityConverter();
			MultiValidator2 multiValidator = new SizeAndRefTypeValidator(bindingContext);
			
			IValidator sizeValidator = new SizeValidator();
			Text size = createText(body, "&Multiplicity", ref, RoomPackage.eINSTANCE.getActorRef_Multiplicity(), sizeValidator, multiValidator, s2m, m2s, false);
			if (hasInterfacePortWithMultiplicityAny(((ActorRef) ref).getType())) {
				size.setEnabled(false);
				createInfoDecorator(size, "size fixed since actor has interface ports with multiplicity *");
			}
			else {
				createDecorator(size, "multiplicity");
			}
			
			Combo refType = createCombo(body, "Reference &Type:", ref, ReferenceType.class, RoomPackage.eINSTANCE.getActorRef_RefType(), ReferenceType.VALUES, null, multiValidator);
			createDecorator(refType, "invalid ref type");
		}
		
		name.selectAll();
		name.setFocus();
	}

	/**
	 * @param ac
	 * @return
	 */
	private boolean hasInterfacePortWithMultiplicityAny(ActorClass ac) {
		if (ac==null)
			return false;
		
		for (Port p : ac.getInterfacePorts()) {
			if (p.getMultiplicity()<0)
				return true;
		}
		return false;
	}

	@Override
	protected Image getImage() {
		return Activator.getImage("icons/Structure.gif");
	}

	@Override
	protected String getFeatureContextHelpId() {
		return "ActorRefPropertyDialog";
	}

}

Back to the top