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 POST request.
84 public class PostRequestDialog 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",
99 private final Integer[] attTblColWidth = { 200, 200, 50 };
101 public PostRequestDialog(Shell parentShell) {
106 public void create() {
108 setTitle("Generate POST Request");
109 setMessage("Dialog which takes input and generates a post request.");
113 protected Control createDialogArea(Composite parent) {
114 Composite compLayout = (Composite) super.createDialogArea(parent);
116 Composite rootContainer = new Composite(compLayout, SWT.NONE);
117 GridLayout layout = new GridLayout();
118 rootContainer.setLayout(layout);
119 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
120 rootContainer.setLayoutData(gd);
122 Group paramsGrp = new Group(rootContainer, SWT.NONE);
124 gd.horizontalAlignment = SWT.FILL;
125 gd.grabExcessHorizontalSpace = true;
126 gd.minimumHeight = 50;
127 paramsGrp.setLayoutData(gd);
128 layout = new GridLayout(2, false);
129 layout.verticalSpacing = 10;
130 layout.marginTop = 10;
131 paramsGrp.setLayout(layout);
133 Label ifTypeLbl = new Label(paramsGrp, SWT.NONE);
134 ifTypeLbl.setText("Interface Type");
136 ifTypesCmb = new Combo(paramsGrp, SWT.NULL);
138 gd.grabExcessHorizontalSpace = true;
139 gd.horizontalAlignment = SWT.FILL;
140 ifTypesCmb.setLayoutData(gd);
141 ifTypesCmb.addModifyListener(new ModifyListener() {
143 public void modifyText(ModifyEvent e) {
144 ifType = ifTypesCmb.getText();
148 RemoteResource resource = Activator.getDefault().getResourceManager()
149 .getCurrentResourceInSelection();
151 // Set the interface types in combo box.
152 Map<String, String> ifTypes = Utility.getResourceInterfaces();
153 this.ifTypes = new HashMap<String, String>();
155 for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
156 key = entry.getValue() + " (" + entry.getKey() + ")";
157 this.ifTypes.put(key, entry.getKey());
161 // Select the default value to be shown in the interface types combo.
162 Vector<String> ifTypesSupportedByResource = resource
163 .getRemoteResourceRef().getResourceInterfaces();
164 if (null != ifTypesSupportedByResource) {
166 if (ifTypesSupportedByResource
167 .contains(Constants.BASELINE_INTERFACE)
168 && ifTypes.containsKey(Constants.BASELINE_INTERFACE)) {
169 // Baseline interface is given preference to be shown in the if
171 String value = ifTypes.get(Constants.BASELINE_INTERFACE);
172 index = ifTypesCmb.indexOf(value + " ("
173 + Constants.BASELINE_INTERFACE + ")");
175 ifTypesCmb.select(index);
178 // Baseline interface is not selected so selecting some other
179 // interface supported by the resource.
180 Iterator<String> itr = ifTypesSupportedByResource.iterator();
181 while (itr.hasNext() && index == -1) {
183 if (ifTypes.containsKey(key)) {
184 String value = ifTypes.get(key);
185 index = ifTypesCmb.indexOf(value + " (" + key + ")");
187 ifTypesCmb.select(index);
192 if (index == -1 && !ifTypesSupportedByResource.isEmpty()) {
193 // Resource has custom interfaces.
194 ifTypesCmb.setText(ifTypesSupportedByResource.get(0));
199 Composite container = new Composite(rootContainer, SWT.NONE);
200 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
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 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
256 tree.setHeaderVisible(true);
257 tree.setLinesVisible(true);
259 attViewer.setContentProvider(new AttributeContentProvider());
260 attViewer.setLabelProvider(new AttributeLabelProvider());
263 public void createAttributeColumns(TreeViewer viewer) {
264 Tree tree = viewer.getTree();
266 attributeEditor = new AttributeEditingSupport();
268 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
269 attName.setWidth(attTblColWidth[0]);
270 attName.setText(attTblHeaders[0]);
272 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
273 attValue.setWidth(attTblColWidth[1]);
274 attValue.setText(attTblHeaders[1]);
275 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
277 attValueVwrCol.setEditingSupport(attributeEditor
278 .createAttributeValueEditor(attViewer, this));
280 TreeColumn updateColumn = new TreeColumn(tree, SWT.NONE);
281 updateColumn.setWidth(attTblColWidth[2]);
282 updateColumn.setText(attTblHeaders[2]);
283 TreeViewerColumn updateVwrCol = new TreeViewerColumn(attViewer,
285 updateVwrCol.setEditingSupport(attributeEditor
286 .createPostSelectionEditor(attViewer));
291 private void addMenuItems() {
292 if (null != attViewer) {
293 final Tree resourceTreeHead = attViewer.getTree();
294 if (null != resourceTreeHead) {
295 // Below code creates menu entries and shows them on right
296 // clicking a resource
297 final Menu menu = new Menu(resourceTreeHead);
298 resourceTreeHead.setMenu(menu);
299 menu.addMenuListener(new MenuAdapter() {
301 public void menuShown(MenuEvent e) {
302 // Clear existing menu items
303 MenuItem[] items = menu.getItems();
304 for (int index = 0; index < items.length; index++) {
305 items[index].dispose();
308 IStructuredSelection selection = ((IStructuredSelection) attViewer
310 final AttributeElement attElement = (AttributeElement) selection
312 if (null == attElement) {
316 // Check the type of attribute.
317 SimulatorResourceAttribute attribute = attElement
318 .getSimulatorResourceAttribute();
319 if (null == attribute) {
323 AttributeValue value = attribute.value();
324 if (null == value || null == value.get()) {
328 TypeInfo type = value.typeInfo();
330 final Object parent = attElement.getParent();
332 if ((type.mType == ValueType.ARRAY
333 && type.mBaseType == ValueType.RESOURCEMODEL && type.mDepth == 1)
334 && (null == parent || parent instanceof ResourceRepresentation)) {
335 addMenuToOneDimensionalTopLevelModelAttributes(menu);
340 && !(parent instanceof ResourceRepresentation)) {
341 Object grandParent = ((AttributeElement) parent)
343 if (null == grandParent
344 || grandParent instanceof ResourceRepresentation) {
345 AttributeElement parentElement = (AttributeElement) parent;
347 // Check the type of attribute.
348 SimulatorResourceAttribute parentAttribute = parentElement
349 .getSimulatorResourceAttribute();
350 if (null != parentAttribute
351 && null != parentAttribute.value()
352 && null != parentAttribute.value()
354 AttributeValue parentValue = parentAttribute
357 TypeInfo parentType = parentValue
359 if (parentType.mType == ValueType.ARRAY
360 && parentType.mBaseType == ValueType.RESOURCEMODEL
361 && parentType.mDepth == 1) {
362 addDeleteMenuToArrayItemsOfOneDimensionalModelAttribute(
363 menu, attElement, parentElement);
375 private void addMenuToOneDimensionalTopLevelModelAttributes(Menu menu) {
376 // Menu to add items to the array.
377 MenuItem addItems = new MenuItem(menu, SWT.NONE);
378 addItems.setText("Add Items");
379 addItems.addSelectionListener(new SelectionAdapter() {
381 public void widgetSelected(SelectionEvent e) {
382 // Get the attributes.
383 ResourceRepresentation representation;
384 representation = getRepresentationForOneDimensionTopLevelAttribute();
385 if (null == representation) {
387 .openError(Display.getDefault().getActiveShell(),
388 "Unable to perform the operation.",
389 "Failed to obtain the required data. Operation cannot be performed.");
391 ModelArrayAddItemDialog dialog = new ModelArrayAddItemDialog(
392 Display.getDefault().getActiveShell(),
393 PostRequestDialog.this, representation);
394 if (Window.OK == dialog.open()) {
395 // Add the new item to the local resource
397 AttributeElement newElement = (AttributeElement) representation
398 .getAttributes().values().toArray()[0];
399 SimulatorResourceAttribute newAttribute = newElement
400 .getSimulatorResourceAttribute();
401 SimulatorResourceModel newModel = (SimulatorResourceModel) newAttribute
404 AttributeElement attElement = getSelectedElement();
405 SimulatorResourceAttribute attribute = attElement
406 .getSimulatorResourceAttribute();
407 SimulatorResourceModel[] modelArray = (SimulatorResourceModel[]) attribute
409 SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[modelArray.length + 1];
412 for (i = 0; i < modelArray.length; i++) {
413 newModelArray[i] = modelArray[i];
415 newModelArray[i] = newModel;
417 AttributeValue newValue = new AttributeValue(
420 newAttribute.setValue(newValue);
422 newAttribute.setProperty(attribute.property());
424 attribute.setValue(newValue);
426 attElement.update(newAttribute);
428 attElement.setPostState(true);
430 attViewer.refresh(attElement);
432 attViewer.expandAll();
439 private void addDeleteMenuToArrayItemsOfOneDimensionalModelAttribute(
440 final Menu menu, final AttributeElement elementToDelete,
441 final AttributeElement parentElement) {
442 // Menu to add items to the array.
443 MenuItem addItems = new MenuItem(menu, SWT.NONE);
444 addItems.setText("Delete Item");
445 addItems.addSelectionListener(new SelectionAdapter() {
447 public void widgetSelected(SelectionEvent e) {
448 MessageBox dialog = new MessageBox(menu.getShell(),
449 SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
450 dialog.setText("Confirm action");
451 dialog.setMessage("Do you want to delete this item from the array?");
452 int retval = dialog.open();
453 if (retval != SWT.OK) {
457 // Removing the element from the attribute value.
458 SimulatorResourceAttribute parentSRA = parentElement
459 .getSimulatorResourceAttribute();
460 AttributeValue value = parentSRA.value();
461 SimulatorResourceModel[] modelArray = (SimulatorResourceModel[]) value
464 String elementIndexName = elementToDelete
465 .getSimulatorResourceAttribute().name();
466 int elementIndex = Integer.parseInt(elementIndexName.substring(
467 elementIndexName.indexOf('[') + 1,
468 elementIndexName.indexOf(']')));
470 SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[modelArray.length - 1];
471 int sIndex = 0, dIndex = 0;
472 for (SimulatorResourceModel model : modelArray) {
473 if (sIndex != elementIndex)
474 newModelArray[dIndex++] = model;
478 // Setting the new model array in the attribute.
479 AttributeValue newValue = new AttributeValue(newModelArray);
480 parentSRA.setValue(newValue);
482 // Removing the element from the child map.
483 Map<String, AttributeElement> elements = parentElement
485 List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
486 attElementList.addAll(elements.values());
487 Collections.sort(attElementList, Utility.attributeComparator);
489 // Renaming the index of the elements.
490 AttributeElement[] attElementArray = attElementList
491 .toArray(new AttributeElement[0]);
492 boolean deleted = false;
494 for (index = 0, newIndex = 0; index < attElementArray.length; index++) {
495 if (index == elementIndex) {
496 elements.remove(elementIndexName);
500 AttributeElement element = attElementArray[index];
501 String curIndexStr = "[" + index + "]";
502 String newIndexStr = "[" + newIndex + "]";
504 element.getSimulatorResourceAttribute().setName(
507 elements.remove(curIndexStr);
508 elements.put(newIndexStr, element);
514 parentElement.setPostState(true);
516 attViewer.refresh(parentElement);
521 private ResourceRepresentation getRepresentationForOneDimensionTopLevelAttribute() {
522 ResourceRepresentation representation = null;
524 AttributeValue value = null;
525 ModelProperty property = null;
527 AttributeElement element = getSelectedElement();
531 SimulatorResourceAttribute modelArrayAtt = element
532 .getSimulatorResourceAttribute();
533 if (null == modelArrayAtt) {
537 AttributeValue attValue = modelArrayAtt.value();
538 if (null == attValue) {
542 TypeInfo type = attValue.typeInfo();
544 if (!(type.mType == ValueType.ARRAY
545 && type.mBaseType == ValueType.RESOURCEMODEL && type.mDepth == 1)) {
549 SimulatorResourceModel[] modelValue = (SimulatorResourceModel[]) attValue
551 if (null == modelValue || modelValue.length < 0) {
555 // Clone an instance of model value.
557 value = Utility.cloneAttributeValue(new AttributeValue(
559 } catch (Exception e) {
567 // Get the model property of the model value instance.
568 AttributeProperty attProperty = modelArrayAtt.property();
569 if (null != attProperty && attProperty instanceof ArrayProperty) {
570 ArrayProperty prop = attProperty.asArray();
572 AttributeProperty elementProperty = prop.getElementProperty();
573 if (null != elementProperty && elementProperty.isModel()) {
574 property = elementProperty.asModel();
579 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
580 modelArrayAtt.name(), value, property);
581 Map<String, SimulatorResourceAttribute> attributes = new HashMap<String, SimulatorResourceAttribute>();
582 attributes.put(modelArrayAtt.name(), attribute);
583 representation = new ResourceRepresentation(attributes, false);
585 return representation;
588 private AttributeElement getSelectedElement() {
589 IStructuredSelection selection = (IStructuredSelection) attViewer
591 if (null == selection) {
595 Object obj = selection.getFirstElement();
600 Tree t = attViewer.getTree();
601 TreeItem item = t.getSelection()[0];
606 if (!(item.getData() instanceof AttributeElement)) {
610 return (AttributeElement) item.getData();
613 class AttributeContentProvider implements ITreeContentProvider {
616 public void dispose() {
620 public void inputChanged(Viewer viewer, Object oldAttribute,
621 Object newAttribute) {
625 public Object[] getChildren(Object attribute) {
626 if (attribute instanceof AttributeElement) {
627 List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
628 attElementList.addAll(((AttributeElement) attribute)
629 .getChildren().values());
630 Collections.sort(attElementList, Utility.attributeComparator);
631 return attElementList.toArray();
634 return new Object[0];
638 public Object getParent(Object attribute) {
639 if (attribute instanceof AttributeElement)
640 return ((AttributeElement) attribute).getParent();
645 public boolean hasChildren(Object attribute) {
646 if (attribute instanceof AttributeElement)
647 return ((AttributeElement) attribute).hasChildren();
652 public Object[] getElements(Object resourceModel) {
653 if (resourceModel instanceof ResourceRepresentation) {
654 return ((ResourceRepresentation) resourceModel).getAttributes()
658 return new Object[0];
662 class AttributeLabelProvider implements ITableLabelProvider {
665 public void addListener(ILabelProviderListener arg0) {
669 public void dispose() {
673 public boolean isLabelProperty(Object arg0, String arg1) {
678 public void removeListener(ILabelProviderListener arg0) {
683 public Image getColumnImage(Object element, int col) {
685 if (element instanceof AttributeElement) {
687 AttributeElement attrElement = (AttributeElement) element;
688 Object parent = attrElement.getParent();
690 || parent instanceof ResourceRepresentation) {
691 if (attrElement.getPostState()) {
692 return Activator.getDefault().getImageRegistry()
693 .get(Constants.CHECKED);
695 return Activator.getDefault().getImageRegistry()
696 .get(Constants.UNCHECKED);
706 public String getColumnText(Object element, int column) {
707 if (element instanceof AttributeElement) {
708 AttributeElement attrElement = (AttributeElement) element;
710 case 0: // Attribute name column
712 SimulatorResourceAttribute attribute = attrElement
713 .getSimulatorResourceAttribute();
714 return attribute.name();
717 case 1: // Attribute value column
719 SimulatorResourceAttribute attribute = attrElement
720 .getSimulatorResourceAttribute();
722 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
723 String value = Utility
724 .getAttributeValueAsString(attribute
744 protected boolean isResizable() {
749 public boolean isHelpAvailable() {
754 protected Button createButton(Composite parent, int id, String label,
755 boolean defaultButton) {
756 if (id == IDialogConstants.OK_ID) {
759 return super.createButton(parent, id, label, defaultButton);
762 public ResourceRepresentation getUpdatedRepresentation() {
763 return updatedRepresentation;
766 public String getIfType() {
767 if (ifTypes.containsKey(ifType)) {
768 return ifTypes.get(ifType);