Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: be30c71f83ea65c4c053fa90b5b1feb546f13f38 (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
/*******************************************************************************
 * 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 v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * 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 org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.conversion.Converter;
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.CommunicationType;
import org.eclipse.etrice.core.room.CompoundProtocolClass;
import org.eclipse.etrice.core.room.ExternalPort;
import org.eclipse.etrice.core.room.GeneralProtocolClass;
import org.eclipse.etrice.core.room.Port;
import org.eclipse.etrice.core.room.ProtocolClass;
import org.eclipse.etrice.core.room.RoomFactory;
import org.eclipse.etrice.core.room.RoomPackage;
import org.eclipse.etrice.core.room.SubSystemClass;
import org.eclipse.etrice.core.validation.ValidationUtil.Result;
import org.eclipse.etrice.ui.common.dialogs.AbstractPropertyDialog;
import org.eclipse.etrice.ui.structure.Activator;
import org.eclipse.etrice.ui.structure.support.SupportUtil;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;

public class PortPropertyDialog extends AbstractPropertyDialog {

	class NameValidator implements IValidator {

		@Override
		public IStatus validate(Object value) {
			if (value instanceof String) {
				String name = (String) value;
				
				Result result = SupportUtil.getInstance().getValidationUtil().isUniqueName(port, name);
				if (!result.isOk())
					return ValidationStatus.error(result.getMsg());
			}
			return Status.OK_STATUS;
		}
	}
	
	class ProtocolValidator implements IValidator {

		@Override
		public IStatus validate(Object value) {
			if (value==null)
				return ValidationStatus.error("select a protocol");
			
			if (value instanceof CompoundProtocolClass)
				if (!relay)
					return ValidationStatus.error("compound protocol only possible for relay port");
			
			return Status.OK_STATUS;
		}
	}
	
	class MultiplicityValidator implements IValidator {

		private boolean mayChange;
		private int old;
		private boolean multAnyAllowed;

		public MultiplicityValidator(boolean mayChange, int old, boolean multAnyAllowed) {
			this.mayChange = mayChange;
			this.old = old;
			this.multAnyAllowed = multAnyAllowed;
		}

		@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");
				if (!mayChange) {
					if (old==1 && (m>1 || m==-1))
						return ValidationStatus.error("cannot change connected port to replicated");
					if ((old>1 || old==-1) && m==1)
						return ValidationStatus.error("cannot change connected port to not replicated");
				}
				if (m==-1 && !multAnyAllowed)
					return ValidationStatus.error("multiplicity * not allowed (actor used replicated)");
				
				if (port.getProtocol()!=null)
					if (port.getProtocol() instanceof ProtocolClass && ((ProtocolClass)port.getProtocol()).getCommType()==CommunicationType.DATA_DRIVEN) {
					if (m!=1)
						return ValidationStatus.error("data driven ports can not be replicated");
				}
			}
			return Status.OK_STATUS;
		}
	}
	
	class RelayValidator implements IValidator {

		/* (non-Javadoc)
		 * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
		 */
		@Override
		public IStatus validate(Object value) {
			if (!((Boolean)value).booleanValue())
				if (port.getProtocol() instanceof CompoundProtocolClass)
					return ValidationStatus.error("external end port must not have compound protocol");
			
			return Status.OK_STATUS;
		}
		
	}

	static class Multiplicity2StringConverter extends Converter {
		
		public Multiplicity2StringConverter() {
			super(Integer.class, String.class);
		}

		@Override
		public Object convert(Object fromObject) {
			if (fromObject instanceof Integer) {
				int val = (Integer) fromObject;
				if (val==-1)
					return "*";
				else
					return fromObject.toString();
			}
			return fromObject;
		}
		
	}
	
	static class String2MultiplicityConverter extends Converter {
		String2MultiplicityConverter() {
			super(String.class, Integer.class);
		}

		@Override
		public Object convert(Object fromObject) {
			if (fromObject.equals("*"))
				return -1;
			else
				return Integer.parseInt((String) fromObject);
		}
	}
	
	private Port port;
	private IScope scope;
	private ActorContainerClass acc;
	private boolean newPort;
	private boolean refitem;
	private boolean internal;
	private Button relayCheck = null;
	private boolean relay;
	private boolean oldRelay;

	public PortPropertyDialog(Shell shell, Port port, IScope scope, ActorContainerClass acc, boolean newPort, boolean refitem, boolean internal) {
		super(shell, "Edit Port");
		this.port = port;
		this.scope = scope;
		this.acc = acc;
		this.newPort = newPort;
		this.refitem = refitem;
		this.internal = internal;
		
		relay = isPortRelay();
		oldRelay = relay;
	}

	private boolean isPortRelay() {
		if (internal)
			return false;
		
		if (acc instanceof SubSystemClass)
			return true;
		else if (acc instanceof ActorClass) {
			for (ExternalPort xp : ((ActorClass) acc).getExternalPorts()) {
				if (xp.getInterfacePort()==port)
					return false;
			}
			return true;
		}
		
		return false;
	}

	@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) {
		Result notReferenced = SupportUtil.getInstance().getValidationUtil().isFreeOfReferences(port);
		boolean multiplicityAnyAllowed = true;
		ActorContainerClass parent = (ActorContainerClass) port.eContainer();
		if (parent instanceof ActorClass) {
			 if (SupportUtil.getInstance().getValidationUtil().isReferencedAsReplicatedInModel((ActorClass) parent))
				 multiplicityAnyAllowed = false;
		}
		NameValidator nv = new NameValidator();
		ProtocolValidator pv = new ProtocolValidator();
		MultiplicityValidator mv = new MultiplicityValidator(newPort || notReferenced.isOk(), port.getMultiplicity(), multiplicityAnyAllowed);

		ArrayList<IEObjectDescription> protocols = new ArrayList<IEObjectDescription>();
        Iterator<IEObjectDescription> it = scope.getAllElements().iterator();
        while (it.hasNext()) {
        	IEObjectDescription desc = it.next();
        	EObject obj = desc.getEObjectOrProxy();
        	if (obj instanceof GeneralProtocolClass)
        		protocols.add(desc);
		}
		
		Text name = createText(body, "&Name:", port, RoomPackage.eINSTANCE.getInterfaceItem_Name(), nv);
		Combo protocol = createComboUsingDesc(body, "&Protocol:", port, GeneralProtocolClass.class, RoomPackage.eINSTANCE.getPort_Protocol(), protocols, RoomPackage.eINSTANCE.getRoomClass_Name(), pv);
		Button conj = createCheck(body, "&Conjugated:", port, RoomPackage.eINSTANCE.getPort_Conjugated());
		if (!internal && !refitem && (acc instanceof ActorClass))
			createRelayCheck(body, notReferenced, mform.getToolkit());
		
		Multiplicity2StringConverter m2s = new Multiplicity2StringConverter();
		String2MultiplicityConverter s2m = new String2MultiplicityConverter();
		Text multi = createText(body, "&Multiplicity:", port, RoomPackage.eINSTANCE.getPort_Multiplicity(), mv, s2m, m2s, false);
		
		if (!newPort) {
			if (!notReferenced.isOk()) {
				protocol.setEnabled(false);
				createInfoDecorator(protocol, notReferenced.getMsg());
				conj.setEnabled(false);
				createInfoDecorator(conj, notReferenced.getMsg());
				if (port.getMultiplicity()==1) {
					multi.setEnabled(false);
					createInfoDecorator(multi, notReferenced.getMsg());
				}
			}
			
			if (refitem) {
				name.setEnabled(false);
				createInfoDecorator(name, "inherited");
				protocol.setEnabled(false);
				createInfoDecorator(protocol, "inherited");
				conj.setEnabled(false);
				createInfoDecorator(conj, "inherited");
				multi.setEnabled(false);
				createInfoDecorator(multi, "inherited");
			}
		}
		
		createDecorator(name, "invalid name");
		createDecorator(protocol, "no protocol selected");
		createDecorator(multi, "multiplicity must be greater 1");
		
		name.selectAll();
		name.setFocus();
	}

	private void createRelayCheck(Composite parent, Result notReferenced, FormToolkit toolkit) {
		Label l = toolkit.createLabel(parent, "Is Relay Port:", SWT.NONE);
		l.setLayoutData(new GridData(SWT.NONE));
		
		relayCheck = toolkit.createButton(parent, "", SWT.CHECK);
		relayCheck.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		relayCheck.setSelection(relay);
		relayCheck.setEnabled(notReferenced.isOk());

		RelayValidator validator = new RelayValidator();
		UpdateValueStrategy t2m = new UpdateValueStrategy();
		t2m.setAfterConvertValidator(validator);
		t2m.setBeforeSetValidator(validator);
		UpdateValueStrategy m2t = new UpdateValueStrategy();
		m2t.setAfterConvertValidator(validator);
		m2t.setBeforeSetValidator(validator);

		getBindingContext().bindValue(SWTObservables.observeSelection(relayCheck), PojoObservables.observeValue(
				this, "relay"), t2m, m2t);
		
		if (notReferenced.isOk())
			createDecorator(relayCheck, "");
		else
			createInfoDecorator(relayCheck, notReferenced.getMsg());
	}
	
	@Override
	protected void okPressed() {
		if (relay!=oldRelay) {
			// we know it's an ActorClass (else the flag couldn't have changed)
			ActorClass ac = (ActorClass) acc;
			
			if (relay) {
				for (ExternalPort xp : ac.getExternalPorts()) {
					if (xp.getInterfacePort()==port) {
						ac.getExternalPorts().remove(xp);
						break;
					}
				}
			}
			else {
				ExternalPort xp = RoomFactory.eINSTANCE.createExternalPort();
				xp.setInterfacePort(port);
				ac.getExternalPorts().add(xp);
			}
		}
		
		super.okPressed();
	}

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

	public boolean isRelay() {
		return relay;
	}

	public void setRelay(boolean relay) {
		this.relay = relay;
	}
}

Back to the top