2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.clientcontroller.view.dialogs;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.dialogs.TitleAreaDialog;
22 import org.eclipse.jface.viewers.ILabelProviderListener;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.ITableLabelProvider;
25 import org.eclipse.jface.viewers.ITreeContentProvider;
26 import org.eclipse.jface.viewers.TreeViewer;
27 import org.eclipse.jface.viewers.TreeViewerColumn;
28 import org.eclipse.jface.viewers.Viewer;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.MenuAdapter;
32 import org.eclipse.swt.events.MenuEvent;
33 import org.eclipse.swt.events.ModifyEvent;
34 import org.eclipse.swt.events.ModifyListener;
35 import org.eclipse.swt.events.SelectionAdapter;
36 import org.eclipse.swt.events.SelectionEvent;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Button;
41 import org.eclipse.swt.widgets.Combo;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Display;
45 import org.eclipse.swt.widgets.Group;
46 import org.eclipse.swt.widgets.Label;
47 import org.eclipse.swt.widgets.Menu;
48 import org.eclipse.swt.widgets.MenuItem;
49 import org.eclipse.swt.widgets.MessageBox;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.Tree;
52 import org.eclipse.swt.widgets.TreeColumn;
53 import org.eclipse.swt.widgets.TreeItem;
55 import java.util.ArrayList;
56 import java.util.Collections;
57 import java.util.HashMap;
58 import java.util.Iterator;
59 import java.util.List;
61 import java.util.Vector;
63 import org.oic.simulator.ArrayProperty;
64 import org.oic.simulator.AttributeProperty;
65 import org.oic.simulator.AttributeValue;
66 import org.oic.simulator.AttributeValue.TypeInfo;
67 import org.oic.simulator.AttributeValue.ValueType;
68 import org.oic.simulator.ModelProperty;
69 import org.oic.simulator.SimulatorResourceAttribute;
70 import org.oic.simulator.SimulatorResourceModel;
71 import org.oic.simulator.client.SimulatorRemoteResource.RequestType;
73 import oic.simulator.clientcontroller.Activator;
74 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
75 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
76 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
77 import oic.simulator.clientcontroller.utils.Constants;
78 import oic.simulator.clientcontroller.utils.Utility;
79 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
82 * This dialog is used for generating a PUT request.
84 public class PutRequestDialog extends TitleAreaDialog {
86 private TreeViewer attViewer;
87 private Combo ifTypesCmb;
89 private String ifType;
91 private Map<String, String> ifTypes;
93 private AttributeEditingSupport attributeEditor;
95 private ResourceRepresentation updatedRepresentation;
97 private final String[] attTblHeaders = { "Name", "Value" };
98 private final Integer[] attTblColWidth = { 200, 200 };
100 public PutRequestDialog(Shell parentShell) {
105 public void create() {
107 setTitle("Generate PUT Request");
108 setMessage("Dialog which takes input and generates a put request.");
112 protected Control createDialogArea(Composite parent) {
113 Composite compLayout = (Composite) super.createDialogArea(parent);
115 Composite rootContainer = new Composite(compLayout, SWT.NONE);
116 GridLayout layout = new GridLayout();
117 rootContainer.setLayout(layout);
118 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
119 rootContainer.setLayoutData(gd);
121 Group paramsGrp = new Group(rootContainer, SWT.NONE);
123 gd.horizontalAlignment = SWT.FILL;
124 gd.grabExcessHorizontalSpace = true;
125 gd.minimumHeight = 50;
126 paramsGrp.setLayoutData(gd);
127 layout = new GridLayout(2, false);
128 layout.verticalSpacing = 10;
129 layout.marginTop = 10;
130 paramsGrp.setLayout(layout);
132 Label ifTypeLbl = new Label(paramsGrp, SWT.NONE);
133 ifTypeLbl.setText("Interface Type");
135 ifTypesCmb = new Combo(paramsGrp, SWT.NULL);
137 gd.grabExcessHorizontalSpace = true;
138 gd.horizontalAlignment = SWT.FILL;
139 ifTypesCmb.setLayoutData(gd);
140 ifTypesCmb.addModifyListener(new ModifyListener() {
142 public void modifyText(ModifyEvent e) {
143 ifType = ifTypesCmb.getText();
147 RemoteResource resource = Activator.getDefault().getResourceManager()
148 .getCurrentResourceInSelection();
150 // Set the interface types in combo box.
151 Map<String, String> ifTypes = Utility.getResourceInterfaces();
152 this.ifTypes = new HashMap<String, String>();
154 for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
155 key = entry.getValue() + " (" + entry.getKey() + ")";
156 this.ifTypes.put(key, entry.getKey());
160 // Select the default value to be shown in the interface types combo.
161 Vector<String> ifTypesSupportedByResource = resource
162 .getRemoteResourceRef().getResourceInterfaces();
163 if (null != ifTypesSupportedByResource) {
165 if (ifTypesSupportedByResource
166 .contains(Constants.BASELINE_INTERFACE)
167 && ifTypes.containsKey(Constants.BASELINE_INTERFACE)) {
168 // Baseline interface is given preference to be shown in the if
170 String value = ifTypes.get(Constants.BASELINE_INTERFACE);
171 index = ifTypesCmb.indexOf(value + " ("
172 + Constants.BASELINE_INTERFACE + ")");
174 ifTypesCmb.select(index);
177 // Baseline interface is not selected so selecting some other
178 // interface supported by the resource.
179 Iterator<String> itr = ifTypesSupportedByResource.iterator();
180 while (itr.hasNext() && index == -1) {
182 if (ifTypes.containsKey(key)) {
183 String value = ifTypes.get(key);
184 index = ifTypesCmb.indexOf(value + " (" + key + ")");
186 ifTypesCmb.select(index);
191 if (index == -1 && !ifTypesSupportedByResource.isEmpty()) {
192 // Resource has custom interfaces.
193 ifTypesCmb.setText(ifTypesSupportedByResource.get(0));
198 Composite container = new Composite(rootContainer, SWT.NONE);
199 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
200 container.setLayoutData(gd);
201 layout = new GridLayout(1, false);
202 layout.verticalSpacing = 10;
203 layout.marginTop = 10;
204 container.setLayout(layout);
206 createTreeViewer(container);
208 // Clone the resource model for maintaining a local copy of attributes
210 SimulatorResourceModel resourceModel = null;
212 resourceModel = (SimulatorResourceModel) Utility
213 .cloneAttributeValue(
214 new AttributeValue(resource.getResourceModelRef()))
216 } catch (Exception e2) {
219 if (null == resourceModel) {
220 // Failed to clone. So taking the base copy and continuing the
222 resourceModel = resource.getResourceModelRef();
225 updatedRepresentation = new ResourceRepresentation(resourceModel);
227 if (resource.isConfigUploaded()) {
229 // Request Type needs to be changed to PUT.
230 updatedRepresentation.updateAttributeProperties(resource
231 .getRequestModels().get(RequestType.POST),
233 } catch (Exception e1) {
237 attViewer.setInput(updatedRepresentation);
239 attViewer.expandAll();
244 private void createTreeViewer(Composite parent) {
245 Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
246 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
247 addressTree.setHeaderVisible(true);
249 attViewer = new TreeViewer(addressTree);
251 createAttributeColumns(attViewer);
253 // make lines and header visible
254 Tree tree = attViewer.getTree();
255 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
256 tree.setLayoutData(gd);
257 tree.setHeaderVisible(true);
258 tree.setLinesVisible(true);
260 attViewer.setContentProvider(new AttributeContentProvider());
261 attViewer.setLabelProvider(new AttributeLabelProvider());
264 public void createAttributeColumns(TreeViewer viewer) {
265 Tree tree = viewer.getTree();
267 attributeEditor = new AttributeEditingSupport();
269 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
270 attName.setWidth(attTblColWidth[0]);
271 attName.setText(attTblHeaders[0]);
273 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
274 attValue.setWidth(attTblColWidth[1]);
275 attValue.setText(attTblHeaders[1]);
276 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
278 attValueVwrCol.setEditingSupport(attributeEditor
279 .createAttributeValueEditor(attViewer, this));
284 private void addMenuItems() {
285 if (null != attViewer) {
286 final Tree resourceTreeHead = attViewer.getTree();
287 if (null != resourceTreeHead) {
288 // Below code creates menu entries and shows them on right
289 // clicking a resource
290 final Menu menu = new Menu(resourceTreeHead);
291 resourceTreeHead.setMenu(menu);
292 menu.addMenuListener(new MenuAdapter() {
294 public void menuShown(MenuEvent e) {
295 // Clear existing menu items
296 MenuItem[] items = menu.getItems();
297 for (int index = 0; index < items.length; index++) {
298 items[index].dispose();
301 IStructuredSelection selection = ((IStructuredSelection) attViewer
303 final AttributeElement attElement = (AttributeElement) selection
305 if (null == attElement) {
309 // Check the type of attribute.
310 SimulatorResourceAttribute attribute = attElement
311 .getSimulatorResourceAttribute();
312 if (null == attribute) {
316 AttributeValue value = attribute.value();
317 if (null == value || null == value.get()) {
321 TypeInfo type = value.typeInfo();
323 final Object parent = attElement.getParent();
325 if ((type.mType == ValueType.ARRAY
326 && type.mBaseType == ValueType.RESOURCEMODEL && type.mDepth == 1)
327 && (null == parent || parent instanceof ResourceRepresentation)) {
328 addMenuToOneDimensionalTopLevelModelAttributes(menu);
333 && !(parent instanceof ResourceRepresentation)) {
334 Object grandParent = ((AttributeElement) parent)
336 if (null == grandParent
337 || grandParent instanceof ResourceRepresentation) {
338 AttributeElement parentElement = (AttributeElement) parent;
340 // Check the type of attribute.
341 SimulatorResourceAttribute parentAttribute = parentElement
342 .getSimulatorResourceAttribute();
343 if (null != parentAttribute
344 && null != parentAttribute.value()
345 && null != parentAttribute.value()
347 AttributeValue parentValue = parentAttribute
350 TypeInfo parentType = parentValue
352 if (parentType.mType == ValueType.ARRAY
353 && parentType.mBaseType == ValueType.RESOURCEMODEL
354 && parentType.mDepth == 1) {
355 addDeleteMenuToArrayItemsOfOneDimensionalModelAttribute(
356 menu, attElement, parentElement);
368 private void addMenuToOneDimensionalTopLevelModelAttributes(Menu menu) {
369 // Menu to add items to the array.
370 MenuItem addItems = new MenuItem(menu, SWT.NONE);
371 addItems.setText("Add Items");
372 addItems.addSelectionListener(new SelectionAdapter() {
374 public void widgetSelected(SelectionEvent e) {
375 // Get the attributes.
376 ResourceRepresentation representation;
377 representation = getRepresentationForOneDimensionTopLevelAttribute();
378 if (null == representation) {
380 .openError(Display.getDefault().getActiveShell(),
381 "Unable to perform the operation.",
382 "Failed to obtain the required data. Operation cannot be performed.");
384 ModelArrayAddItemDialog dialog = new ModelArrayAddItemDialog(
385 Display.getDefault().getActiveShell(),
386 PutRequestDialog.this, representation);
387 if (Window.OK == dialog.open()) {
388 // Add the new item to the local resource
390 AttributeElement newElement = (AttributeElement) representation
391 .getAttributes().values().toArray()[0];
392 SimulatorResourceAttribute newAttribute = newElement
393 .getSimulatorResourceAttribute();
394 SimulatorResourceModel newModel = (SimulatorResourceModel) newAttribute
397 AttributeElement attElement = getSelectedElement();
398 SimulatorResourceAttribute attribute = attElement
399 .getSimulatorResourceAttribute();
400 SimulatorResourceModel[] modelArray = (SimulatorResourceModel[]) attribute
402 SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[modelArray.length + 1];
405 for (i = 0; i < modelArray.length; i++) {
406 newModelArray[i] = modelArray[i];
408 newModelArray[i] = newModel;
410 AttributeValue newValue = new AttributeValue(
413 newAttribute.setValue(newValue);
415 newAttribute.setProperty(attribute.property());
417 attribute.setValue(newValue);
419 attElement.update(newAttribute);
421 attViewer.refresh(attElement);
423 attViewer.expandAll();
430 private void addDeleteMenuToArrayItemsOfOneDimensionalModelAttribute(
431 final Menu menu, final AttributeElement elementToDelete,
432 final AttributeElement parentElement) {
433 // Menu to add items to the array.
434 MenuItem addItems = new MenuItem(menu, SWT.NONE);
435 addItems.setText("Delete Item");
436 addItems.addSelectionListener(new SelectionAdapter() {
438 public void widgetSelected(SelectionEvent e) {
439 MessageBox dialog = new MessageBox(menu.getShell(),
440 SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
441 dialog.setText("Confirm action");
442 dialog.setMessage("Do you want to delete this item from the array?");
443 int retval = dialog.open();
444 if (retval != SWT.OK) {
448 // Removing the element from the attribute value.
449 SimulatorResourceAttribute parentSRA = parentElement
450 .getSimulatorResourceAttribute();
451 AttributeValue value = parentSRA.value();
452 SimulatorResourceModel[] modelArray = (SimulatorResourceModel[]) value
455 String elementIndexName = elementToDelete
456 .getSimulatorResourceAttribute().name();
457 int elementIndex = Integer.parseInt(elementIndexName.substring(
458 elementIndexName.indexOf('[') + 1,
459 elementIndexName.indexOf(']')));
461 SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[modelArray.length - 1];
462 int sIndex = 0, dIndex = 0;
463 for (SimulatorResourceModel model : modelArray) {
464 if (sIndex != elementIndex)
465 newModelArray[dIndex++] = model;
469 // Setting the new model array in the attribute.
470 AttributeValue newValue = new AttributeValue(newModelArray);
471 parentSRA.setValue(newValue);
473 // Removing the element from the child map.
474 Map<String, AttributeElement> elements = parentElement
476 List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
477 attElementList.addAll(elements.values());
478 Collections.sort(attElementList, Utility.attributeComparator);
480 // Renaming the index of the elements.
481 AttributeElement[] attElementArray = attElementList
482 .toArray(new AttributeElement[0]);
483 boolean deleted = false;
485 for (index = 0, newIndex = 0; index < attElementArray.length; index++) {
486 if (index == elementIndex) {
487 elements.remove(elementIndexName);
491 AttributeElement element = attElementArray[index];
492 String curIndexStr = "[" + index + "]";
493 String newIndexStr = "[" + newIndex + "]";
495 element.getSimulatorResourceAttribute().setName(
498 elements.remove(curIndexStr);
499 elements.put(newIndexStr, element);
505 attViewer.refresh(parentElement);
510 private ResourceRepresentation getRepresentationForOneDimensionTopLevelAttribute() {
511 ResourceRepresentation representation = null;
513 AttributeValue value = null;
514 ModelProperty property = null;
516 AttributeElement element = getSelectedElement();
520 SimulatorResourceAttribute modelArrayAtt = element
521 .getSimulatorResourceAttribute();
522 if (null == modelArrayAtt) {
526 AttributeValue attValue = modelArrayAtt.value();
527 if (null == attValue) {
531 TypeInfo type = attValue.typeInfo();
533 if (!(type.mType == ValueType.ARRAY
534 && type.mBaseType == ValueType.RESOURCEMODEL && type.mDepth == 1)) {
538 SimulatorResourceModel[] modelValue = (SimulatorResourceModel[]) attValue
540 if (null == modelValue || modelValue.length < 0) {
544 // Clone an instance of model value.
546 value = Utility.cloneAttributeValue(new AttributeValue(
548 } catch (Exception e) {
556 // Get the model property of the model value instance.
557 AttributeProperty attProperty = modelArrayAtt.property();
558 if (null != attProperty && attProperty instanceof ArrayProperty) {
559 ArrayProperty prop = attProperty.asArray();
561 AttributeProperty elementProperty = prop.getElementProperty();
562 if (null != elementProperty && elementProperty.isModel()) {
563 property = elementProperty.asModel();
568 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
569 modelArrayAtt.name(), value, property);
570 Map<String, SimulatorResourceAttribute> attributes = new HashMap<String, SimulatorResourceAttribute>();
571 attributes.put(modelArrayAtt.name(), attribute);
572 representation = new ResourceRepresentation(attributes, false);
574 return representation;
577 private AttributeElement getSelectedElement() {
578 IStructuredSelection selection = (IStructuredSelection) attViewer
580 if (null == selection) {
584 Object obj = selection.getFirstElement();
589 Tree t = attViewer.getTree();
590 TreeItem item = t.getSelection()[0];
595 if (!(item.getData() instanceof AttributeElement)) {
599 return (AttributeElement) item.getData();
602 class AttributeContentProvider implements ITreeContentProvider {
605 public void dispose() {
609 public void inputChanged(Viewer viewer, Object oldAttribute,
610 Object newAttribute) {
614 public Object[] getChildren(Object attribute) {
615 if (attribute instanceof AttributeElement) {
616 List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
617 attElementList.addAll(((AttributeElement) attribute)
618 .getChildren().values());
619 Collections.sort(attElementList, Utility.attributeComparator);
620 return attElementList.toArray();
623 return new Object[0];
627 public Object getParent(Object attribute) {
628 if (attribute instanceof AttributeElement)
629 return ((AttributeElement) attribute).getParent();
634 public boolean hasChildren(Object attribute) {
635 if (attribute instanceof AttributeElement)
636 return ((AttributeElement) attribute).hasChildren();
641 public Object[] getElements(Object resourceModel) {
642 if (resourceModel instanceof ResourceRepresentation) {
643 return ((ResourceRepresentation) resourceModel).getAttributes()
647 return new Object[0];
651 class AttributeLabelProvider implements ITableLabelProvider {
654 public void addListener(ILabelProviderListener arg0) {
658 public void dispose() {
662 public boolean isLabelProperty(Object arg0, String arg1) {
667 public void removeListener(ILabelProviderListener arg0) {
672 public Image getColumnImage(Object element, int col) {
677 public String getColumnText(Object element, int column) {
678 if (element instanceof AttributeElement) {
679 AttributeElement attrElement = (AttributeElement) element;
681 case 0: // Attribute name column
683 SimulatorResourceAttribute attribute = attrElement
684 .getSimulatorResourceAttribute();
685 return attribute.name();
688 case 1: // Attribute value column
690 SimulatorResourceAttribute attribute = attrElement
691 .getSimulatorResourceAttribute();
693 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
694 String value = Utility
695 .getAttributeValueAsString(attribute
711 protected boolean isResizable() {
716 public boolean isHelpAvailable() {
721 protected Button createButton(Composite parent, int id, String label,
722 boolean defaultButton) {
723 if (id == IDialogConstants.OK_ID) {
726 return super.createButton(parent, id, label, defaultButton);
729 public ResourceRepresentation getUpdatedRepresentation() {
730 return updatedRepresentation;
733 public String getIfType() {
734 if (ifTypes.containsKey(ifType)) {
735 return ifTypes.get(ifType);