| 1 | package de.uka.ipd.sdq.pcm.dialogs.parameters; |
| 2 | |
| 3 | import org.eclipse.core.runtime.Assert; |
| 4 | import org.eclipse.emf.common.util.EList; |
| 5 | |
| 6 | import de.uka.ipd.sdq.pcm.dialogs.Messages; |
| 7 | import de.uka.ipd.sdq.pcm.dialogs.datatype.PalladioDataTypeDialog; |
| 8 | import de.uka.ipd.sdq.pcm.repository.CompositeDataType; |
| 9 | import de.uka.ipd.sdq.pcm.repository.InnerDeclaration; |
| 10 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
| 11 | import de.uka.ipd.sdq.pcm.repository.Signature; |
| 12 | |
| 13 | /** |
| 14 | * The Class place the validate methods for CompositeDataType and |
| 15 | * Parameters(Signature) editor area. The methods are used for validating of |
| 16 | * InnerDeclaration of CompositeDataType and signature parameters. Validating |
| 17 | * passes each Action (Delete, Up, Down) with call. |
| 18 | * |
| 19 | * @author Roman Andrej |
| 20 | */ |
| 21 | public class UpDownButtonsValidator { |
| 22 | |
| 23 | private static UpDownButtonsValidator singelton = null; |
| 24 | private CreateEditorContents contents; |
| 25 | |
| 26 | private UpDownButtonsValidator() { |
| 27 | } |
| 28 | |
| 29 | public void validate(int elementIndex, int maxIndex) { |
| 30 | Assert.isNotNull(contents); |
| 31 | |
| 32 | contents.setDownItemsEnabled(true); |
| 33 | contents.setUpItemsEnabled(true); |
| 34 | |
| 35 | if (elementIndex == 0) |
| 36 | contents.setUpItemsEnabled(false); |
| 37 | if (elementIndex == maxIndex - 1) |
| 38 | contents.setDownItemsEnabled(false); |
| 39 | } |
| 40 | |
| 41 | /** Validate selection from table viewer */ |
| 42 | public void validateSelection(Object selection) { |
| 43 | if (selection == null) { |
| 44 | contents.setDeleteItemsEnabled(false); |
| 45 | contents.setDownItemsEnabled(false); |
| 46 | contents.setUpItemsEnabled(false); |
| 47 | } else if (selection instanceof Parameter) { |
| 48 | Parameter parameter = (Parameter) selection; |
| 49 | UpDownButtonsValidator.getSingelton().validateParameter(parameter); |
| 50 | contents.setDeleteItemsEnabled(true); |
| 51 | } else if (selection instanceof InnerDeclaration) { |
| 52 | InnerDeclaration declaration = (InnerDeclaration) selection; |
| 53 | UpDownButtonsValidator.getSingelton().validateInnerDeclaration( |
| 54 | declaration); |
| 55 | contents.setDeleteItemsEnabled(true); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Validate (Enabled/Unenabled) up-, down-button in the ParameterDialog. |
| 61 | * Call if selection instanceof Parameter. |
| 62 | */ |
| 63 | public void validateParameter(Parameter parameter) { |
| 64 | EList<Parameter> parameters = ParametersUtil.getParametersOfSignature((Signature)parameter.eContainer()); |
| 65 | validate(parameters.indexOf(parameter), parameters.size()); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Validate (Enabled/Unenabled) up-, down-button in the DataTypeDialog. Call |
| 70 | * if selection instanceof InnerDeclaration. |
| 71 | */ |
| 72 | public void validateInnerDeclaration(InnerDeclaration declaration) { |
| 73 | if (declaration.eContainer() instanceof CompositeDataType) { |
| 74 | CompositeDataType dataType = (CompositeDataType) declaration |
| 75 | .eContainer(); |
| 76 | EList<InnerDeclaration> declarations = dataType |
| 77 | .getInnerDeclaration_CompositeDataType(); |
| 78 | validate(declarations.indexOf(declaration), declarations.size()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public boolean validdateDeclarationInnerDataType(InnerDeclaration declaration, PalladioDataTypeDialog dialog){ |
| 83 | if (declaration.getDatatype_InnerDeclaration() == null) { |
| 84 | dialog.setErrorMessage(Messages.DataTypeDialog_ErrorMsgInnerName); |
| 85 | return false; |
| 86 | } |
| 87 | if (declaration.getEntityName().equals("")) { |
| 88 | dialog.setErrorMessage(Messages.DataTypeDialog_ErrorMsgInnerName); |
| 89 | return false; |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | public static UpDownButtonsValidator getSingelton() { |
| 95 | if (singelton == null) |
| 96 | singelton = new UpDownButtonsValidator(); |
| 97 | return singelton; |
| 98 | } |
| 99 | |
| 100 | public void setContents(CreateEditorContents contents) { |
| 101 | this.contents = contents; |
| 102 | } |
| 103 | } |