Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6fe2c882594f9cbf6760d3fc1f0a4d3bf27eeca2 (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
package org.eclipse.etrice.ui.behavior.dialogs;

import java.util.EnumSet;

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.EStructuralFeature;
import org.eclipse.etrice.core.fsm.fSM.ComponentCommunicationType;
import org.eclipse.etrice.core.fsm.fSM.DetailCode;
import org.eclipse.etrice.core.fsm.fSM.FSMPackage;
import org.eclipse.etrice.core.fsm.fSM.RefinedState;
import org.eclipse.etrice.core.fsm.fSM.SimpleState;
import org.eclipse.etrice.core.fsm.fSM.State;
import org.eclipse.etrice.core.fsm.validation.FSMValidationUtilXtend.Result;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.util.RoomHelpers;
import org.eclipse.etrice.ui.behavior.Activator;
import org.eclipse.etrice.ui.behavior.detailcode.RuntimeDetailExpressionProvider;
import org.eclipse.etrice.ui.behavior.fsm.actioneditor.IActionCodeEditor;
import org.eclipse.etrice.ui.behavior.fsm.dialogs.AbstractMemberAwarePropertyDialog;
import org.eclipse.etrice.ui.behavior.fsm.dialogs.DetailCodeToString;
import org.eclipse.etrice.ui.behavior.fsm.dialogs.IStatePropertyDialog;
import org.eclipse.etrice.ui.behavior.fsm.dialogs.QuickFixDialog;
import org.eclipse.etrice.ui.behavior.fsm.dialogs.StringToDetailCode;
import org.eclipse.etrice.ui.behavior.support.SupportUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;

public class StatePropertyDialog extends AbstractMemberAwarePropertyDialog implements IStatePropertyDialog {

	class NameValidator implements IValidator {

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

	private ActorClass ac;
	private State state;
	private boolean inherited;

	/**
	 * Enum for quickfix. Used when {@link StatePropertyDialog} is invoked from
	 * {@link QuickFixDialog}.
	 * 
	 * @author jayant
	 * 
	 */
	public enum Where {
		ENTRY, EXIT, DO 
	}

	private boolean addCode = false;
	private String codeSelectionString = "";
	private EnumSet<Where> where = EnumSet.noneOf(Where.class);
	private String messageToDisplay = "";
	private String messageTitle = "";

	public StatePropertyDialog(Shell shell, ActorClass ac, State s, boolean edit) {
		super(shell, edit?"Edit State":"View State", ac);
		this.ac = ac;
		this.state = s;

		Activator.getDefault().getInjector().injectMembers(this);
		
		inherited = SupportUtil.getInstance().getRoomHelpers().getActorClass(s)!=ac;
	}

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

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

		if (state instanceof SimpleState && !inherited) {
			NameValidator nv = new NameValidator();
			
			Text name = createText(body, "&Name:", state, FSMPackage.eINSTANCE.getSimpleState_Name(), nv);
			configureMemberAwareness(name);
			
			createDecorator(name, "invalid name");
			
			name.setFocus();
			name.selectAll();

			if (!where.isEmpty())
				name.setEnabled(false);
		}
		else {
			createFixedText(body, "Name:", state.getName(), false);
		}

		DetailCodeToString m2s = new DetailCodeToString();
		StringToDetailCode s2m = new StringToDetailCode();
		
		RoomHelpers roomHelpers = SupportUtil.getInstance().getRoomHelpers();
		
		if (inherited) {
			String code = roomHelpers.getDetailCode(state.getEntryCode());
			if (state instanceof RefinedState)
				code += roomHelpers.getBaseEntryCode((RefinedState)state);
			Text entry = createFixedText(body, "Entry Code:", code, true);
			GridData gd = new GridData(GridData.FILL_BOTH);
			gd.heightHint = 100;
			entry.setLayoutData(gd);
		}
		else {
			if (state instanceof RefinedState)
			{
				String code = roomHelpers.getBaseEntryCode((RefinedState)state);
				Text entry = createFixedText(body, "Base Entry Code:", code, true);
				GridData gd = new GridData(GridData.FILL_BOTH);
				gd.heightHint = 100;
				entry.setLayoutData(gd);

				if (where.contains(Where.ENTRY)) {
					if (addCode)
						entry.append(codeSelectionString + "();\n");
					setTextSelectionAndFocus(entry, codeSelectionString);
				}
			}

			createActionCodeEditor(body, "&Entry Code:", state.getEntryCode(),
					FSMPackage.eINSTANCE.getState_EntryCode(), s2m, m2s);
		}

		if (inherited) {
			String code = roomHelpers.getDetailCode(state.getExitCode());
			if (state instanceof RefinedState)
				code = roomHelpers.getBaseExitCode((RefinedState)state) + code;
			Text entry = createFixedText(body, "Exit Code:", code, true);
			GridData gd = new GridData(GridData.FILL_BOTH);
			gd.heightHint = 100;
			entry.setLayoutData(gd);
		}
		else {
			createActionCodeEditor(body, "E&xit Code:", state.getExitCode(),
					FSMPackage.eINSTANCE.getState_ExitCode(), s2m, m2s);

			if (state instanceof RefinedState)
			{
				String code = roomHelpers.getBaseExitCode((RefinedState)state);
				Text entry = createFixedText(body, "Base Exit Code:", code, true);
				GridData gd = new GridData(GridData.FILL_BOTH);
				gd.heightHint = 100;
				entry.setLayoutData(gd);
			}
		}

		ActorClass ac = roomHelpers.getActorClass(state);
		if (ac.getCommType()!=ComponentCommunicationType.EVENT_DRIVEN)
			createActionCodeEditor(body, "&Do Code:", state.getDoCode(),
					FSMPackage.eINSTANCE.getState_DoCode(), s2m, m2s);

		createMembersAndMessagesButtons(body);

		if (!messageToDisplay.isEmpty()) {
			getShell().getParent().getDisplay().asyncExec(new Runnable() {
				public void run() {
					MessageDialog.openInformation(getShell(), messageTitle,
							messageToDisplay);
				}
			});
		}
	}

	/**
	 * Creates Action Code Editor with the given parameters for the
	 * {@link #state} and binds it with the model.
	 * 
	 * @author jayant
	 * 
	 * @param parent
	 *            the {@link Composite} which will hold the editor
	 * @param label
	 *            the label for the editor
	 * @param detailCode
	 *            the {@link DetailCode} object to be represented
	 * @param feat
	 *            the {@link EStructuralFeature} associated with the code
	 * @param s2m
	 *            a String to Model converter
	 * @param m2s
	 *            a Model to string converter
	 * 
	 * @return the constructed instance of {@link IActionCodeEditor}
	 */
	private void createActionCodeEditor(Composite parent, String label,
			DetailCode detailCode, EStructuralFeature feat,
			StringToDetailCode s2m, DetailCodeToString m2s) {

		IActionCodeEditor entry = super.createActionCodeEditor(parent, label,
				detailCode, state, feat, s2m, m2s, new RuntimeDetailExpressionProvider(ac));

		Control control;
		if (entry != null)
			control = entry.getControl();
		else {
			// if action editor cannot be created, create a simple SWT Text
			// widget.
			Text textEntry = createText(parent, label, state, feat, null, s2m,
					m2s, true);
			configureMemberAwareness(textEntry, true, true);
			control = textEntry;
		}

		//set layout for the created control
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.heightHint = 100;
		control.setLayoutData(gd);

		// TODO Change IActionCodeEditor API to allow append or change
		// the quick fix method
		/*if (where.contains(Where.ENTRY)) {
			if (addCode)
				entry.append(codeSelectionString + "();\n");
			setTextSelectionAndFocus(entry, codeSelectionString);
		}*/
	}

	public void setAddCode(boolean add) {
		addCode = add;
	}

	public void setCodeSelectionString(String selectionString, EnumSet<Where> where) {
		this.where = where;
		codeSelectionString = selectionString;
	}

	public void setMessageDialogContents(String message, String title) {
		messageToDisplay = message;
		messageTitle = title;
	}

	@Override
	protected String getFeatureContextHelpId() {
		// TODO add context help
		return null;
	}
}

Back to the top