| 1 | package de.uka.ipd.sdq.pcm.dialogs.variablenames; |
| 2 | |
| 3 | import org.eclipse.jface.dialogs.TitleAreaDialog; |
| 4 | import org.eclipse.swt.SWT; |
| 5 | import org.eclipse.swt.events.ModifyEvent; |
| 6 | import org.eclipse.swt.events.ModifyListener; |
| 7 | import org.eclipse.swt.events.SelectionAdapter; |
| 8 | import org.eclipse.swt.layout.GridData; |
| 9 | import org.eclipse.swt.layout.GridLayout; |
| 10 | import org.eclipse.swt.widgets.Composite; |
| 11 | import org.eclipse.swt.widgets.Control; |
| 12 | import org.eclipse.swt.widgets.Label; |
| 13 | import org.eclipse.swt.widgets.Shell; |
| 14 | import org.eclipse.swt.widgets.Text; |
| 15 | |
| 16 | import de.uka.ipd.sdq.stoex.AbstractNamedReference; |
| 17 | import de.uka.ipd.sdq.stoex.NamespaceReference; |
| 18 | import de.uka.ipd.sdq.stoex.StoexFactory; |
| 19 | |
| 20 | public class SetOutputVariableNameDialog extends TitleAreaDialog { |
| 21 | |
| 22 | private String DIALOG_TITLE = "Enter Local Variable Name for Storing Output Variable"; |
| 23 | private Text setNameText; |
| 24 | private String result; |
| 25 | |
| 26 | private class SetNameValidationListener extends SelectionAdapter implements ModifyListener { |
| 27 | public void modifyText(ModifyEvent e) { |
| 28 | |
| 29 | result = setNameText.getText(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | private SetNameValidationListener listener = new SetNameValidationListener(); |
| 34 | |
| 35 | public SetOutputVariableNameDialog(Shell parentShell) { |
| 36 | super(parentShell); |
| 37 | // TODO Auto-generated constructor stub |
| 38 | |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | protected Control createDialogArea(Composite parent) { |
| 43 | Composite area = (Composite) super.createDialogArea(parent); |
| 44 | Composite container = new Composite(area, SWT.NONE); |
| 45 | //container.setLayout(new FillLayout()); |
| 46 | container.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 47 | |
| 48 | this.setTitle(DIALOG_TITLE); |
| 49 | |
| 50 | |
| 51 | final GridLayout myGL = new GridLayout(); |
| 52 | myGL.numColumns = 2; |
| 53 | container.setLayout(myGL); |
| 54 | |
| 55 | |
| 56 | Label setNameLabel = new Label(container, SWT.NONE); |
| 57 | setNameLabel.setText("Enter Local Variable Name:"); |
| 58 | setNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 59 | |
| 60 | setNameText = new Text(container, SWT.SINGLE | SWT.BORDER); |
| 61 | setNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 62 | setNameText.addModifyListener(listener); |
| 63 | |
| 64 | return super.createDialogArea(parent); |
| 65 | } |
| 66 | |
| 67 | public String getResult(){ |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected void cancelPressed() { |
| 73 | // TODO Auto-generated method stub |
| 74 | super.cancelPressed(); |
| 75 | result = null; |
| 76 | } |
| 77 | |
| 78 | public AbstractNamedReference getOutputVariableReference() { |
| 79 | String[] enteredNameSplitted = result.split("\\."); |
| 80 | AbstractNamedReference namedReference = referenceFactory( |
| 81 | enteredNameSplitted[enteredNameSplitted.length - 1], true); |
| 82 | |
| 83 | for (int i=enteredNameSplitted.length-2; i>=0; i--){ |
| 84 | NamespaceReference nr = (NamespaceReference)referenceFactory(enteredNameSplitted[i], false); |
| 85 | nr.setInnerReference_NamespaceReference(namedReference); |
| 86 | namedReference = nr; |
| 87 | } |
| 88 | return namedReference; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Create the AbstractNamedReference and set a string parameter |
| 93 | */ |
| 94 | private AbstractNamedReference referenceFactory(String string, |
| 95 | boolean shouldGenerateVariableReference) { |
| 96 | AbstractNamedReference parameterReference = null; |
| 97 | if (shouldGenerateVariableReference) { |
| 98 | parameterReference = StoexFactory.eINSTANCE |
| 99 | .createVariableReference(); |
| 100 | } else { |
| 101 | parameterReference = StoexFactory.eINSTANCE |
| 102 | .createNamespaceReference(); |
| 103 | } |
| 104 | parameterReference.setReferenceName(string); |
| 105 | return parameterReference; |
| 106 | } |
| 107 | |
| 108 | } |