Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / view / dialogs / PostRequestDialog.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.clientcontroller.view.dialogs;
18
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;
54
55 import java.util.ArrayList;
56 import java.util.Collections;
57 import java.util.Date;
58 import java.util.HashMap;
59 import java.util.Iterator;
60 import java.util.List;
61 import java.util.Map;
62 import java.util.Vector;
63
64 import org.oic.simulator.ArrayProperty;
65 import org.oic.simulator.AttributeProperty;
66 import org.oic.simulator.AttributeValue;
67 import org.oic.simulator.AttributeValue.TypeInfo;
68 import org.oic.simulator.AttributeValue.ValueType;
69 import org.oic.simulator.ILogger.Level;
70 import org.oic.simulator.ModelProperty;
71 import org.oic.simulator.SimulatorResourceAttribute;
72 import org.oic.simulator.SimulatorResourceModel;
73 import org.oic.simulator.client.SimulatorRemoteResource.RequestType;
74
75 import oic.simulator.clientcontroller.Activator;
76 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
77 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
78 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
79 import oic.simulator.clientcontroller.utils.Constants;
80 import oic.simulator.clientcontroller.utils.Utility;
81 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
82
83 /**
84  * This dialog is used for generating a POST request.
85  */
86 public class PostRequestDialog extends TitleAreaDialog {
87
88     private TreeViewer              attViewer;
89     private Combo                   ifTypesCmb;
90
91     private String                  ifType;
92
93     private Map<String, String>     ifTypes;
94
95     private AttributeEditingSupport attributeEditor;
96
97     private ResourceRepresentation  updatedRepresentation;
98
99     private final String[]          attTblHeaders  = { "Name", "Value",
100             "Select"                              };
101     private final Integer[]         attTblColWidth = { 200, 200, 50 };
102
103     public PostRequestDialog(Shell parentShell) {
104         super(parentShell);
105     }
106
107     @Override
108     public void create() {
109         super.create();
110         setTitle("Generate POST Request");
111         setMessage("Dialog which takes input and generates a post request.");
112     }
113
114     @Override
115     protected Control createDialogArea(Composite parent) {
116         Composite compLayout = (Composite) super.createDialogArea(parent);
117
118         Composite rootContainer = new Composite(compLayout, SWT.NONE);
119         GridLayout layout = new GridLayout();
120         rootContainer.setLayout(layout);
121         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
122         rootContainer.setLayoutData(gd);
123
124         Group paramsGrp = new Group(rootContainer, SWT.NONE);
125         gd = new GridData();
126         gd.horizontalAlignment = SWT.FILL;
127         gd.grabExcessHorizontalSpace = true;
128         gd.minimumHeight = 50;
129         paramsGrp.setLayoutData(gd);
130         layout = new GridLayout(2, false);
131         layout.verticalSpacing = 10;
132         layout.marginTop = 10;
133         paramsGrp.setLayout(layout);
134
135         Label ifTypeLbl = new Label(paramsGrp, SWT.NONE);
136         ifTypeLbl.setText("Interface Type");
137
138         ifTypesCmb = new Combo(paramsGrp, SWT.NULL);
139         gd = new GridData();
140         gd.grabExcessHorizontalSpace = true;
141         gd.horizontalAlignment = SWT.FILL;
142         ifTypesCmb.setLayoutData(gd);
143         ifTypesCmb.addModifyListener(new ModifyListener() {
144             @Override
145             public void modifyText(ModifyEvent e) {
146                 ifType = ifTypesCmb.getText();
147             }
148         });
149
150         RemoteResource resource = Activator.getDefault().getResourceManager()
151                 .getCurrentResourceInSelection();
152
153         // Set the interface types in combo box.
154         Map<String, String> ifTypes = Utility.getResourceInterfaces();
155         this.ifTypes = new HashMap<String, String>();
156         String key;
157         for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
158             key = entry.getValue() + " (" + entry.getKey() + ")";
159             this.ifTypes.put(key, entry.getKey());
160             ifTypesCmb.add(key);
161         }
162
163         // Select the default value to be shown in the interface types combo.
164         Vector<String> ifTypesSupportedByResource = resource
165                 .getRemoteResourceRef().getResourceInterfaces();
166         if (null != ifTypesSupportedByResource) {
167             int index = -1;
168             if (ifTypesSupportedByResource
169                     .contains(Constants.BASELINE_INTERFACE)
170                     && ifTypes.containsKey(Constants.BASELINE_INTERFACE)) {
171                 // Baseline interface is given preference to be shown in the if
172                 // types combo.
173                 String value = ifTypes.get(Constants.BASELINE_INTERFACE);
174                 index = ifTypesCmb.indexOf(value + " ("
175                         + Constants.BASELINE_INTERFACE + ")");
176                 if (index != -1)
177                     ifTypesCmb.select(index);
178             }
179             if (index == -1) {
180                 // Baseline interface is not selected so selecting some other
181                 // interface supported by the resource.
182                 Iterator<String> itr = ifTypesSupportedByResource.iterator();
183                 while (itr.hasNext() && index == -1) {
184                     key = itr.next();
185                     if (ifTypes.containsKey(key)) {
186                         String value = ifTypes.get(key);
187                         index = ifTypesCmb.indexOf(value + " (" + key + ")");
188                         if (index != -1) {
189                             ifTypesCmb.select(index);
190                             break;
191                         }
192                     }
193                 }
194                 if (index == -1 && !ifTypesSupportedByResource.isEmpty()) {
195                     // Resource has custom interfaces.
196                     ifTypesCmb.setText(ifTypesSupportedByResource.get(0));
197                 }
198             }
199         }
200
201         Composite container = new Composite(rootContainer, SWT.NONE);
202         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
203         layout = new GridLayout(1, false);
204         layout.verticalSpacing = 10;
205         layout.marginTop = 10;
206         container.setLayout(layout);
207
208         createTreeViewer(container);
209
210         // Clone the resource model for maintaining a local copy of attributes
211         // for POST requests.
212         SimulatorResourceModel resourceModel = null;
213         try {
214             AttributeValue attValue = Utility
215                     .cloneAttributeValue(new AttributeValue(resource
216                             .getResourceModelRef()));
217             if (null != attValue)
218                 resourceModel = (SimulatorResourceModel) attValue.get();
219         } catch (Exception e) {
220             Activator
221                     .getDefault()
222                     .getLogManager()
223                     .log(Level.ERROR.ordinal(),
224                             new Date(),
225                             "There is an error while forming an instance of the attribute value.\n"
226                                     + Utility.getSimulatorErrorString(e, null));
227         }
228
229         if (null == resourceModel) {
230             // Failed to clone. So taking the base copy and continuing the
231             // operation.
232             resourceModel = resource.getResourceModelRef();
233         }
234
235         updatedRepresentation = new ResourceRepresentation(resourceModel);
236
237         if (resource.isConfigUploaded()) {
238             try {
239                 // Request Type needs to be changed to PUT.
240                 updatedRepresentation.updateAttributeProperties(resource
241                         .getRequestModels().get(RequestType.POST),
242                         resourceModel);
243             } catch (Exception e) {
244                 Activator
245                         .getDefault()
246                         .getLogManager()
247                         .log(Level.ERROR.ordinal(),
248                                 new Date(),
249                                 "There is an error while forming an instance of the attribute value.\n"
250                                         + Utility.getSimulatorErrorString(e,
251                                                 null));
252             }
253         }
254
255         attViewer.setInput(updatedRepresentation);
256
257         attViewer.expandAll();
258
259         return compLayout;
260     }
261
262     private void createTreeViewer(Composite parent) {
263         Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
264                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
265         addressTree.setHeaderVisible(true);
266
267         attViewer = new TreeViewer(addressTree);
268
269         createAttributeColumns(attViewer);
270
271         // make lines and header visible
272         Tree tree = attViewer.getTree();
273         tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
274         tree.setHeaderVisible(true);
275         tree.setLinesVisible(true);
276
277         attViewer.setContentProvider(new AttributeContentProvider());
278         attViewer.setLabelProvider(new AttributeLabelProvider());
279     }
280
281     public void createAttributeColumns(TreeViewer viewer) {
282         Tree tree = viewer.getTree();
283
284         attributeEditor = new AttributeEditingSupport();
285
286         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
287         attName.setWidth(attTblColWidth[0]);
288         attName.setText(attTblHeaders[0]);
289
290         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
291         attValue.setWidth(attTblColWidth[1]);
292         attValue.setText(attTblHeaders[1]);
293         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
294                 attValue);
295         attValueVwrCol.setEditingSupport(attributeEditor
296                 .createAttributeValueEditor(attViewer, this));
297
298         TreeColumn updateColumn = new TreeColumn(tree, SWT.NONE);
299         updateColumn.setWidth(attTblColWidth[2]);
300         updateColumn.setText(attTblHeaders[2]);
301         TreeViewerColumn updateVwrCol = new TreeViewerColumn(attViewer,
302                 updateColumn);
303         updateVwrCol.setEditingSupport(attributeEditor
304                 .createPostSelectionEditor(attViewer));
305
306         addMenuItems();
307     }
308
309     private void addMenuItems() {
310         if (null != attViewer) {
311             final Tree resourceTreeHead = attViewer.getTree();
312             if (null != resourceTreeHead) {
313                 // Below code creates menu entries and shows them on right
314                 // clicking a resource
315                 final Menu menu = new Menu(resourceTreeHead);
316                 resourceTreeHead.setMenu(menu);
317                 menu.addMenuListener(new MenuAdapter() {
318                     @Override
319                     public void menuShown(MenuEvent e) {
320                         // Clear existing menu items
321                         MenuItem[] items = menu.getItems();
322                         for (int index = 0; index < items.length; index++) {
323                             items[index].dispose();
324                         }
325
326                         IStructuredSelection selection = ((IStructuredSelection) attViewer
327                                 .getSelection());
328                         final AttributeElement attElement = (AttributeElement) selection
329                                 .getFirstElement();
330                         if (null == attElement) {
331                             return;
332                         }
333
334                         // Check the type of attribute.
335                         SimulatorResourceAttribute attribute = attElement
336                                 .getSimulatorResourceAttribute();
337                         if (null == attribute) {
338                             return;
339                         }
340
341                         AttributeValue value = attribute.value();
342                         if (null == value || null == value.get()) {
343                             return;
344                         }
345
346                         TypeInfo type = value.typeInfo();
347
348                         final Object parent = attElement.getParent();
349
350                         if ((type.mType == ValueType.ARRAY
351                                 && type.mBaseType == ValueType.RESOURCEMODEL && type.mDepth == 1)
352                                 && (null == parent || parent instanceof ResourceRepresentation)) {
353                             addMenuToOneDimensionalTopLevelModelAttributes(menu);
354                             return;
355                         }
356
357                         if (null != parent
358                                 && parent instanceof AttributeElement) {
359                             Object grandParent = ((AttributeElement) parent)
360                                     .getParent();
361                             if (null == grandParent
362                                     || grandParent instanceof ResourceRepresentation) {
363                                 AttributeElement parentElement = (AttributeElement) parent;
364
365                                 // Check the type of attribute.
366                                 SimulatorResourceAttribute parentAttribute = parentElement
367                                         .getSimulatorResourceAttribute();
368                                 if (null != parentAttribute
369                                         && null != parentAttribute.value()
370                                         && null != parentAttribute.value()
371                                                 .get()) {
372                                     AttributeValue parentValue = parentAttribute
373                                             .value();
374
375                                     TypeInfo parentType = parentValue
376                                             .typeInfo();
377                                     if (parentType.mType == ValueType.ARRAY
378                                             && parentType.mBaseType == ValueType.RESOURCEMODEL
379                                             && parentType.mDepth == 1) {
380                                         addDeleteMenuToArrayItemsOfOneDimensionalModelAttribute(
381                                                 menu, attElement, parentElement);
382                                         return;
383                                     }
384                                 }
385                             }
386                         }
387                     }
388                 });
389             }
390         }
391     }
392
393     private void addMenuToOneDimensionalTopLevelModelAttributes(Menu menu) {
394         // Menu to add items to the array.
395         MenuItem addItems = new MenuItem(menu, SWT.NONE);
396         addItems.setText("Add Items");
397         addItems.addSelectionListener(new SelectionAdapter() {
398             @Override
399             public void widgetSelected(SelectionEvent e) {
400                 // Get the attributes.
401                 ResourceRepresentation representation;
402                 representation = getRepresentationForOneDimensionTopLevelAttribute();
403                 if (null == representation) {
404                     MessageDialog
405                             .openError(Display.getDefault().getActiveShell(),
406                                     "Unable to perform the operation.",
407                                     "Failed to obtain the required data. Operation cannot be performed.");
408                 } else {
409                     ModelArrayAddItemDialog dialog = new ModelArrayAddItemDialog(
410                             Display.getDefault().getActiveShell(),
411                             PostRequestDialog.this, representation);
412                     if (Window.OK == dialog.open()) {
413                         // Add the new item to the local resource
414                         // representation.
415                         AttributeElement newElement = (AttributeElement) representation
416                                 .getAttributes().values().toArray()[0];
417                         SimulatorResourceAttribute newAttribute = newElement
418                                 .getSimulatorResourceAttribute();
419                         SimulatorResourceModel newModel = (SimulatorResourceModel) newAttribute
420                                 .value().get();
421
422                         AttributeElement attElement = getSelectedElement();
423                         if (null == attElement) {
424                             MessageDialog
425                                     .openError(Display.getDefault()
426                                             .getActiveShell(),
427                                             "Unable to perform the operation.",
428                                             "Failed to obtain the required data. Operation cannot be performed.");
429                             return;
430                         }
431
432                         SimulatorResourceAttribute attribute = attElement
433                                 .getSimulatorResourceAttribute();
434                         SimulatorResourceModel[] modelArray = (SimulatorResourceModel[]) attribute
435                                 .value().get();
436                         SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[modelArray.length + 1];
437
438                         int i;
439                         for (i = 0; i < modelArray.length; i++) {
440                             newModelArray[i] = modelArray[i];
441                         }
442                         newModelArray[i] = newModel;
443
444                         AttributeValue newValue = new AttributeValue(
445                                 newModelArray);
446
447                         newAttribute.setValue(newValue);
448
449                         newAttribute.setProperty(attribute.property());
450
451                         attribute.setValue(newValue);
452
453                         attElement.update(newAttribute);
454
455                         attElement.setPostState(true);
456
457                         attViewer.refresh(attElement);
458
459                         attViewer.expandAll();
460                     }
461                 }
462             }
463         });
464     }
465
466     private void addDeleteMenuToArrayItemsOfOneDimensionalModelAttribute(
467             final Menu menu, final AttributeElement elementToDelete,
468             final AttributeElement parentElement) {
469         // Menu to add items to the array.
470         MenuItem addItems = new MenuItem(menu, SWT.NONE);
471         addItems.setText("Delete Item");
472         addItems.addSelectionListener(new SelectionAdapter() {
473             @Override
474             public void widgetSelected(SelectionEvent e) {
475                 MessageBox dialog = new MessageBox(menu.getShell(),
476                         SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
477                 dialog.setText("Confirm action");
478                 dialog.setMessage("Do you want to delete this item from the array?");
479                 int retval = dialog.open();
480                 if (retval != SWT.OK) {
481                     return;
482                 }
483
484                 // Removing the element from the attribute value.
485                 SimulatorResourceAttribute parentSRA = parentElement
486                         .getSimulatorResourceAttribute();
487                 AttributeValue value = parentSRA.value();
488                 SimulatorResourceModel[] modelArray = (SimulatorResourceModel[]) value
489                         .get();
490
491                 String elementIndexName = elementToDelete
492                         .getSimulatorResourceAttribute().name();
493                 int elementIndex = Integer.parseInt(elementIndexName.substring(
494                         elementIndexName.indexOf('[') + 1,
495                         elementIndexName.indexOf(']')));
496
497                 SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[modelArray.length - 1];
498                 int sIndex = 0, dIndex = 0;
499                 for (SimulatorResourceModel model : modelArray) {
500                     if (sIndex != elementIndex)
501                         newModelArray[dIndex++] = model;
502                     sIndex++;
503                 }
504
505                 // Setting the new model array in the attribute.
506                 AttributeValue newValue = new AttributeValue(newModelArray);
507                 parentSRA.setValue(newValue);
508
509                 // Removing the element from the child map.
510                 Map<String, AttributeElement> elements = parentElement
511                         .getChildren();
512                 if (null == elements) {
513                     MessageDialog
514                             .openError(Display.getDefault().getActiveShell(),
515                                     "Operation failed.",
516                                     "There is an error while removing the array items.");
517                     return;
518                 }
519
520                 List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
521                 attElementList.addAll(elements.values());
522                 Collections.sort(attElementList, Utility.attributeComparator);
523
524                 // Renaming the index of the elements.
525                 AttributeElement[] attElementArray = attElementList
526                         .toArray(new AttributeElement[0]);
527                 boolean deleted = false;
528                 int index, newIndex;
529                 for (index = 0, newIndex = 0; index < attElementArray.length; index++) {
530                     if (index == elementIndex) {
531                         elements.remove(elementIndexName);
532                         deleted = true;
533                     } else {
534                         if (deleted) {
535                             AttributeElement element = attElementArray[index];
536                             String curIndexStr = "[" + index + "]";
537                             String newIndexStr = "[" + newIndex + "]";
538
539                             element.getSimulatorResourceAttribute().setName(
540                                     newIndexStr);
541
542                             elements.remove(curIndexStr);
543                             elements.put(newIndexStr, element);
544                         }
545                         newIndex++;
546                     }
547                 }
548
549                 parentElement.setPostState(true);
550
551                 attViewer.refresh(parentElement);
552             }
553         });
554     }
555
556     private ResourceRepresentation getRepresentationForOneDimensionTopLevelAttribute() {
557         ResourceRepresentation representation;
558
559         AttributeValue value;
560         ModelProperty property = null;
561
562         AttributeElement element = getSelectedElement();
563         if (null == element)
564             return null;
565
566         SimulatorResourceAttribute modelArrayAtt = element
567                 .getSimulatorResourceAttribute();
568         if (null == modelArrayAtt) {
569             return null;
570         }
571
572         AttributeValue attValue = modelArrayAtt.value();
573         if (null == attValue) {
574             return null;
575         }
576
577         TypeInfo type = attValue.typeInfo();
578
579         if (!(type.mType == ValueType.ARRAY
580                 && type.mBaseType == ValueType.RESOURCEMODEL && type.mDepth == 1)) {
581             return null;
582         }
583
584         SimulatorResourceModel[] modelValue = (SimulatorResourceModel[]) attValue
585                 .get();
586         if (null == modelValue || modelValue.length < 0) {
587             return null;
588         }
589
590         // Clone an instance of model value.
591         try {
592             value = Utility.cloneAttributeValue(new AttributeValue(
593                     modelValue[0]));
594         } catch (Exception e) {
595             return null;
596         }
597
598         if (null == value) {
599             return null;
600         }
601
602         // Get the model property of the model value instance.
603         AttributeProperty attProperty = modelArrayAtt.property();
604         if (null != attProperty && attProperty instanceof ArrayProperty) {
605             ArrayProperty prop = attProperty.asArray();
606             if (null != prop) {
607                 AttributeProperty elementProperty = prop.getElementProperty();
608                 if (null != elementProperty && elementProperty.isModel()) {
609                     property = elementProperty.asModel();
610                 }
611             }
612         }
613
614         SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
615                 modelArrayAtt.name(), value, property);
616         Map<String, SimulatorResourceAttribute> attributes = new HashMap<String, SimulatorResourceAttribute>();
617         attributes.put(modelArrayAtt.name(), attribute);
618         representation = new ResourceRepresentation(attributes, false);
619
620         return representation;
621     }
622
623     private AttributeElement getSelectedElement() {
624         IStructuredSelection selection = (IStructuredSelection) attViewer
625                 .getSelection();
626         if (null == selection) {
627             return null;
628         }
629
630         Object obj = selection.getFirstElement();
631         if (null == obj) {
632             return null;
633         }
634
635         Tree t = attViewer.getTree();
636         TreeItem item = t.getSelection()[0];
637         if (null == item) {
638             return null;
639         }
640
641         if (!(item.getData() instanceof AttributeElement)) {
642             return null;
643         }
644
645         return (AttributeElement) item.getData();
646     }
647
648     private static class AttributeContentProvider implements
649             ITreeContentProvider {
650
651         @Override
652         public void dispose() {
653         }
654
655         @Override
656         public void inputChanged(Viewer viewer, Object oldAttribute,
657                 Object newAttribute) {
658         }
659
660         @Override
661         public Object[] getChildren(Object attribute) {
662             if (attribute instanceof AttributeElement) {
663                 List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
664                 Map<String, AttributeElement> children = ((AttributeElement) attribute)
665                         .getChildren();
666                 if (null != children) {
667                     attElementList.addAll(children.values());
668                     Collections.sort(attElementList,
669                             Utility.attributeComparator);
670                     return attElementList.toArray();
671                 }
672             }
673
674             return new Object[0];
675         }
676
677         @Override
678         public Object getParent(Object attribute) {
679             if (attribute instanceof AttributeElement)
680                 return ((AttributeElement) attribute).getParent();
681             return null;
682         }
683
684         @Override
685         public boolean hasChildren(Object attribute) {
686             if (attribute instanceof AttributeElement)
687                 return ((AttributeElement) attribute).hasChildren();
688             return false;
689         }
690
691         @Override
692         public Object[] getElements(Object resourceModel) {
693             if (resourceModel instanceof ResourceRepresentation) {
694                 return ((ResourceRepresentation) resourceModel).getAttributes()
695                         .values().toArray();
696             }
697
698             return new Object[0];
699         }
700     }
701
702     private static class AttributeLabelProvider implements ITableLabelProvider {
703
704         @Override
705         public void addListener(ILabelProviderListener arg0) {
706         }
707
708         @Override
709         public void dispose() {
710         }
711
712         @Override
713         public boolean isLabelProperty(Object arg0, String arg1) {
714             return false;
715         }
716
717         @Override
718         public void removeListener(ILabelProviderListener arg0) {
719
720         }
721
722         @Override
723         public Image getColumnImage(Object element, int col) {
724             if (col == 2) {
725                 if (element instanceof AttributeElement) {
726
727                     AttributeElement attrElement = (AttributeElement) element;
728                     Object parent = attrElement.getParent();
729                     if (null == parent
730                             || parent instanceof ResourceRepresentation) {
731                         if (attrElement.getPostState()) {
732                             return Activator.getDefault().getImageRegistry()
733                                     .get(Constants.CHECKED);
734                         } else {
735                             return Activator.getDefault().getImageRegistry()
736                                     .get(Constants.UNCHECKED);
737                         }
738                     }
739                 }
740             }
741
742             return null;
743         }
744
745         @Override
746         public String getColumnText(Object element, int column) {
747             if (element instanceof AttributeElement) {
748                 AttributeElement attrElement = (AttributeElement) element;
749                 switch (column) {
750                     case 0: // Attribute name column
751                     {
752                         SimulatorResourceAttribute attribute = attrElement
753                                 .getSimulatorResourceAttribute();
754                         return attribute.name();
755                     }
756
757                     case 1: // Attribute value column
758                     {
759                         SimulatorResourceAttribute attribute = attrElement
760                                 .getSimulatorResourceAttribute();
761
762                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
763                             String value = Utility
764                                     .getAttributeValueAsString(attribute
765                                             .value());
766                             if (null == value) {
767                                 value = "";
768                             }
769                             return value;
770                         }
771                         return null;
772                     }
773
774                     case 2: {
775                         return "";
776                     }
777                 }
778             }
779             return null;
780         }
781     }
782
783     @Override
784     protected boolean isResizable() {
785         return true;
786     }
787
788     @Override
789     public boolean isHelpAvailable() {
790         return false;
791     }
792
793     @Override
794     protected Button createButton(Composite parent, int id, String label,
795             boolean defaultButton) {
796         if (id == IDialogConstants.OK_ID) {
797             label = "POST";
798         }
799         return super.createButton(parent, id, label, defaultButton);
800     }
801
802     public ResourceRepresentation getUpdatedRepresentation() {
803         return updatedRepresentation;
804     }
805
806     public String getIfType() {
807         if (ifTypes.containsKey(ifType)) {
808             return ifTypes.get(ifType);
809         }
810         return ifType;
811     }
812 }