Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a58d040f9633589a3611ac7c3cdcc9ab6317bf42 (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
/*******************************************************************************
 * 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 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.resource.Resource;
import org.eclipse.etrice.core.validation.ValidationUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.ActorContainerClass;
import org.eclipse.etrice.core.room.ExternalPort;
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.RoomModel;
import org.eclipse.etrice.core.room.RoomPackage;
import org.eclipse.etrice.core.room.SubSystemClass;
import org.eclipse.etrice.ui.common.dialogs.AbstractPropertyDialog;
import org.eclipse.etrice.ui.structure.Activator;

public class PortPropertyDialog 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 (acc instanceof ActorClass) {
					if (nameExists((ActorClass) acc, name))
						return ValidationStatus.error("name already exists");
				}
				else if (acc instanceof SubSystemClass) {
					SubSystemClass ssc = (SubSystemClass) acc;
					for (Port p : ssc.getRelayPorts()) {
						if (p!=port && p.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 (Port p : ac.getIfPorts()) {
				if (p!=port && p.getName().equals(name))
					return true;
			}
			for (Port p : ac.getIntPorts()) {
				if (p!=port && p.getName().equals(name))
					return true;
			}
			if (ac.getBase()!=null)
				return nameExists(ac.getBase(), name);
			
			return false;
		}
	}
	
	class ProtocolValidator implements IValidator {

		@Override
		public IStatus validate(Object value) {
			if (value==null)
				return ValidationStatus.error("select a protocol");
			
			return Status.OK_STATUS;
		}
	}
	
	class MultiplicityValidator implements IValidator {

		private boolean mayChange;
		private int old;

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

		@Override
		public IStatus validate(Object value) {
			if (value instanceof Integer) {
				int m = (Integer) value;
				if (m<=0)
					return ValidationStatus.error("multiplicity must be positive");
				if (!mayChange) {
					if (old==1 && m>1)
						return ValidationStatus.error("cannot make connected port replicated");
					if (old>1 && m==1)
						return ValidationStatus.error("cannot make connected port not replicated");
				}
			}
			return Status.OK_STATUS;
		}
	}
	
	private Port port;
	private ActorContainerClass acc;
	private boolean newPort;
	private boolean refitem;
	private boolean internal;
	private Button relayCheck = null;
	private boolean relay;

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

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

	@Override
	protected void initializeBounds() {
		getShell().setSize(300, 300);
	}
	
	@Override
	protected void createContent(IManagedForm mform, Composite body, DataBindingContext bindingContext) {
		boolean connected = ValidationUtil.isConnected(port, null, acc);
		NameValidator nv = new NameValidator();
		ProtocolValidator pv = new ProtocolValidator();
		MultiplicityValidator mv = new MultiplicityValidator(newPort || !connected, port.getMultiplicity());

		ArrayList<ProtocolClass> protocols = new ArrayList<ProtocolClass>();
		if (acc.eResource()!=null) {
			for (Resource r: acc.eResource().getResourceSet().getResources()) {
				if (!r.getContents().isEmpty()) {
					if (r.getContents().get(0) instanceof RoomModel) {
						protocols.addAll(((RoomModel)r.getContents().get(0)).getProtocolClasses());
					}
				}
			}
		}
		
		Text name = createText(body, "Name:", port, RoomPackage.eINSTANCE.getInterfaceItem_Name(), nv);
		Combo protocol = createCombo(body, "Protocol:", port, ProtocolClass.class, RoomPackage.eINSTANCE.getInterfaceItem_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, mform.getToolkit());
		Text multi = createText(body, "Multiplicity:", port, RoomPackage.eINSTANCE.getPort_Multiplicity(), mv);
		
		if (!newPort) {
			// TODOHRR: check whether port is used externally?
			if (connected) {
				protocol.setEnabled(false);
				conj.setEnabled(false);
				if (port.getMultiplicity()==1)
					multi.setEnabled(false);
			}
			
			if (refitem) {
				name.setEnabled(false);
				protocol.setEnabled(false);
				conj.setEnabled(false);
				multi.setEnabled(false);
			}
		}
		
		createDecorator(name, "invalid name");
		createDecorator(protocol, "no protocol selected");
		createDecorator(multi, "multiplicity must be greater 1");
		
		name.setFocus();
	}

	private void createRelayCheck(Composite parent, 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);
		
		if (ValidationUtil.isConnected(port, null, acc))
			relayCheck.setEnabled(false);
	}
	
	@Override
	protected void okPressed() {
		if (relayCheck!=null) {
			if (relay!=relayCheck.getSelection()) {
				relay = relayCheck.getSelection();
				
				// we know it's an ActorClass if we created the relayCheck in the first place
				ActorClass ac = (ActorClass) acc;
				
				if (relay) {
					for (ExternalPort xp : ac.getExtPorts()) {
						if (xp.getIfport()==port) {
							ac.getExtPorts().remove(xp);
							break;
						}
					}
				}
				else {
					ExternalPort xp = RoomFactory.eINSTANCE.createExternalPort();
					xp.setIfport(port);
					ac.getExtPorts().add(xp);
				}
			}
		}
		
		super.okPressed();
	}

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

Back to the top