+ private void addMenuItems() {
+ if (null != attViewer) {
+ final Tree resourceTreeHead = attViewer.getTree();
+ if (null != resourceTreeHead) {
+ // Below code creates menu entries and shows them on right
+ // clicking a resource
+ final Menu menu = new Menu(resourceTreeHead);
+ resourceTreeHead.setMenu(menu);
+ menu.addMenuListener(new MenuAdapter() {
+ @Override
+ public void menuShown(MenuEvent e) {
+ // Clear existing menu items
+ MenuItem[] items = menu.getItems();
+ for (int index = 0; index < items.length; index++) {
+ items[index].dispose();
+ }
+
+ IStructuredSelection selection = ((IStructuredSelection) attViewer
+ .getSelection());
+ final AttributeElement attElement = (AttributeElement) selection
+ .getFirstElement();
+ if (null == attElement) {
+ return;
+ }
+
+ // Check the type of attribute.
+ SimulatorResourceAttribute attribute = attElement
+ .getSimulatorResourceAttribute();
+ if (null == attribute) {
+ return;
+ }
+
+ AttributeValue value = attribute.value();
+ if (null == value || null == value.get()) {
+ return;
+ }
+
+ TypeInfo type = value.typeInfo();
+
+ final Object parent = attElement.getParent();
+
+ if ((type.mType == ValueType.ARRAY
+ && type.mBaseType == ValueType.RESOURCEMODEL && type.mDepth == 1)
+ && (null == parent || parent instanceof ResourceRepresentation)) {
+ addMenuToOneDimensionalTopLevelModelAttributes(menu);
+ return;
+ }
+
+ if (null != parent
+ && !(parent instanceof ResourceRepresentation)) {
+ Object grandParent = ((AttributeElement) parent)
+ .getParent();
+ if (null == grandParent
+ || grandParent instanceof ResourceRepresentation) {
+ AttributeElement parentElement = (AttributeElement) parent;
+
+ // Check the type of attribute.
+ SimulatorResourceAttribute parentAttribute = parentElement
+ .getSimulatorResourceAttribute();
+ if (null != parentAttribute
+ && null != parentAttribute.value()
+ && null != parentAttribute.value()
+ .get()) {
+ AttributeValue parentValue = parentAttribute
+ .value();
+
+ TypeInfo parentType = parentValue
+ .typeInfo();
+ if (parentType.mType == ValueType.ARRAY
+ && parentType.mBaseType == ValueType.RESOURCEMODEL
+ && parentType.mDepth == 1) {
+ addDeleteMenuToArrayItemsOfOneDimensionalModelAttribute(
+ menu, attElement, parentElement);
+ return;
+ }
+ }
+ }
+ }
+
+ }
+ });
+ }
+ }
+ }
+
+ private void addMenuToOneDimensionalTopLevelModelAttributes(Menu menu) {
+ // Menu to add items to the array.
+ MenuItem addItems = new MenuItem(menu, SWT.NONE);
+ addItems.setText("Add Items");
+ addItems.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ // Get the attributes.
+ ResourceRepresentation representation;
+ representation = getRepresentationForOneDimensionTopLevelAttribute();
+ if (null == representation) {
+ MessageDialog
+ .openError(Display.getDefault().getActiveShell(),
+ "Unable to perform the operation.",
+ "Failed to obtain the required data. Operation cannot be performed.");
+ } else {
+ // Check whether a new item can be added to the array by
+ // checking
+ // the array property of the current attribute in
+ // selection(Model Array type attribute).
+ AttributeElement attElement = getSelectedElement();
+ SimulatorResourceAttribute attribute = attElement
+ .getSimulatorResourceAttribute();
+
+ AttributeValue attValue = attribute.value();
+ AttributeProperty attProperty = attribute.property();
+ if (null != attProperty
+ && attProperty instanceof ArrayProperty) {
+ ArrayProperty prop = attProperty.asArray();
+ if (null != prop && !prop.isVariable()) {
+ SimulatorResourceModel[] model = (SimulatorResourceModel[]) attValue
+ .get();
+ if (null != model
+ && model.length >= prop.maxItems()) {
+ MessageDialog
+ .openError(
+ Display.getDefault()
+ .getActiveShell(),
+ "Unable to perform the operation.",
+ "Exceeding the maximum number of array elements allowed for this attribute.\n"
+ + "Maximum number of allowed array element(s): "
+ + prop.maxItems());
+ return;
+ }
+ }
+ }
+
+ ModelArrayAddItemDialog dialog = new ModelArrayAddItemDialog(
+ Display.getDefault().getActiveShell(),
+ representation);
+ if (Window.OK == dialog.open()) {
+ // Add the new item to the local resource
+ // representation.
+ AttributeElement newElement = (AttributeElement) representation
+ .getAttributes().values().toArray()[0];
+ SimulatorResourceAttribute newAttribute = newElement
+ .getSimulatorResourceAttribute();
+ SimulatorResourceModel newModel = (SimulatorResourceModel) newAttribute
+ .value().get();
+
+ SimulatorResourceModel[] modelArray = (SimulatorResourceModel[]) attribute
+ .value().get();
+ SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[modelArray.length + 1];
+
+ int i;
+ for (i = 0; i < modelArray.length; i++) {
+ newModelArray[i] = modelArray[i];
+ }
+ newModelArray[i] = newModel;
+
+ AttributeValue newValue = new AttributeValue(
+ newModelArray);
+
+ newAttribute.setValue(newValue);
+
+ newAttribute.setProperty(attribute.property());
+
+ attElement.update(newAttribute);
+
+ boolean updated = resourceManager.attributeValueUpdated(
+ (SingleResource) resourceManager
+ .getCurrentResourceInSelection(),
+ attribute.name(), newValue);
+
+ if (!updated) {
+ attribute.setValue(new AttributeValue(modelArray));
+ attElement.update(newAttribute);
+
+ MessageDialog
+ .openInformation(Display.getDefault()
+ .getActiveShell(),
+ "Operation failed",
+ "Failed to insert a new item in the array.");
+ } else {
+ // Highlight the newly added item.
+ AttributeElement addedElement = attElement
+ .getChildren().get("[" + i + "]");
+ attViewer.setSelection(new StructuredSelection(
+ addedElement), true);
+ }
+ }
+ }
+ }
+ });
+ }
+
+ private void addDeleteMenuToArrayItemsOfOneDimensionalModelAttribute(
+ final Menu menu, final AttributeElement elementToDelete,
+ final AttributeElement parentElement) {
+ // Menu to add items to the array.
+ MenuItem addItems = new MenuItem(menu, SWT.NONE);
+ addItems.setText("Delete Item");
+ addItems.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ // Check whether any existing item can be removed from the array
+ // by checking
+ // the array property of the current attribute in
+ // selection(Model Array type attribute).
+ SimulatorResourceAttribute parentSRA = parentElement
+ .getSimulatorResourceAttribute();
+ AttributeValue value = parentSRA.value();
+ AttributeProperty attProperty = parentSRA.property();
+ if (null != attProperty && attProperty instanceof ArrayProperty) {
+ ArrayProperty prop = attProperty.asArray();
+ if (null != prop) {
+ SimulatorResourceModel[] model = (SimulatorResourceModel[]) value
+ .get();
+ if (null != model && model.length <= prop.minItems()) {
+ MessageDialog
+ .openError(
+ Display.getDefault()
+ .getActiveShell(),
+ "Unable to perform the operation.",
+ "Violating the minimum number of array elements allowed for this attribute.\n"
+ + "Minimum number of allowed array element(s): "
+ + prop.minItems());
+ return;
+ }
+ }
+ }
+
+ MessageBox dialog = new MessageBox(menu.getShell(),
+ SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
+ dialog.setText("Confirm action");
+ dialog.setMessage("Do you want to delete this item from the array?");
+ int retval = dialog.open();
+ if (retval != SWT.OK) {
+ return;
+ }
+
+ // Removing the element from the attribute value.
+ SimulatorResourceModel[] modelArray = (SimulatorResourceModel[]) value
+ .get();
+
+ String elementIndexName = elementToDelete
+ .getSimulatorResourceAttribute().name();
+ int elementIndex = Integer.parseInt(elementIndexName.substring(
+ elementIndexName.indexOf('[') + 1,
+ elementIndexName.indexOf(']')));
+
+ SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[modelArray.length - 1];
+ int sIndex = 0, dIndex = 0;
+ for (SimulatorResourceModel model : modelArray) {
+ if (sIndex != elementIndex)
+ newModelArray[dIndex++] = model;
+ sIndex++;
+ }
+
+ // Setting the new model array in the attribute.
+ AttributeValue newValue = new AttributeValue(newModelArray);
+ parentSRA.setValue(newValue);
+
+ // Removing the element from the child map.
+ Map<String, AttributeElement> elements = parentElement
+ .getChildren();
+ List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
+ attElementList.addAll(elements.values());
+ Collections.sort(attElementList, Utility.attributeComparator);
+
+ // Renaming the index of the elements.
+ AttributeElement[] attElementArray = attElementList
+ .toArray(new AttributeElement[0]);
+ boolean deleted = false;
+ int index, newIndex;
+ for (index = 0, newIndex = 0; index < attElementArray.length; index++) {
+ if (index == elementIndex) {
+ elements.remove(elementIndexName);
+ deleted = true;
+ } else {
+ if (deleted) {
+ AttributeElement element = attElementArray[index];
+ String curIndexStr = "[" + index + "]";
+ String newIndexStr = "[" + newIndex + "]";
+
+ element.getSimulatorResourceAttribute().setName(
+ newIndexStr);
+
+ elements.remove(curIndexStr);
+ elements.put(newIndexStr, element);
+ }
+ newIndex++;
+ }
+ }
+
+ resourceManager.attributeValueUpdated(
+ (SingleResource) resourceManager
+ .getCurrentResourceInSelection(), parentSRA
+ .name(), newValue);
+
+ attViewer.refresh(parentElement);
+ }
+ });
+ }
+
+ private ResourceRepresentation getRepresentationForOneDimensionTopLevelAttribute() {
+ ResourceRepresentation representation = null;
+
+ AttributeValue value = null;
+ ModelProperty property = null;
+ SimulatorResourceAttribute attribute;
+
+ AttributeElement element = getSelectedElement();
+ if (null == element)
+ return null;
+
+ SimulatorResourceAttribute modelArrayAtt = element
+ .getSimulatorResourceAttribute();
+ if (null == modelArrayAtt) {
+ return null;
+ }
+
+ AttributeValue attValue = modelArrayAtt.value();
+ if (null == attValue) {
+ return null;
+ }
+
+ TypeInfo type = attValue.typeInfo();
+
+ if (!(type.mType == ValueType.ARRAY
+ && type.mBaseType == ValueType.RESOURCEMODEL && type.mDepth == 1)) {
+ return null;
+ }
+
+ SimulatorResourceModel[] modelValue = (SimulatorResourceModel[]) attValue
+ .get();
+ if (null == modelValue || modelValue.length < 0) {
+ return null;
+ }
+
+ // Clone an instance of model value.
+ try {
+ value = Utility.cloneAttributeValue(new AttributeValue(
+ modelValue[0]));
+ } catch (Exception e) {
+ return null;
+ }
+
+ if (null == value) {
+ return null;
+ }
+
+ // Get the model property of the model value instance.
+ AttributeProperty attProperty = modelArrayAtt.property();
+ if (null != attProperty && attProperty instanceof ArrayProperty) {
+ ArrayProperty prop = attProperty.asArray();
+ if (null != prop) {
+ AttributeProperty elementProperty = prop.getElementProperty();
+ if (null != elementProperty && elementProperty.isModel()) {
+ property = elementProperty.asModel();
+ }
+ }
+ }
+
+ attribute = new SimulatorResourceAttribute(modelArrayAtt.name(), value,
+ property);
+
+ Map<String, SimulatorResourceAttribute> attributes = new HashMap<String, SimulatorResourceAttribute>();
+ attributes.put(attribute.name(), attribute);
+
+ representation = new ResourceRepresentation(attributes);
+
+ return representation;
+ }
+
+ private AttributeElement getSelectedElement() {
+ IStructuredSelection selection = (IStructuredSelection) attViewer
+ .getSelection();
+ if (null == selection) {
+ return null;
+ }
+
+ Object obj = selection.getFirstElement();
+ if (null == obj) {
+ return null;
+ }
+
+ Tree t = attViewer.getTree();
+ TreeItem item = t.getSelection()[0];
+ if (null == item) {
+ return null;
+ }
+
+ if (!(item.getData() instanceof AttributeElement)) {
+ return null;
+ }
+
+ return (AttributeElement) item.getData();
+ }
+