Resolved issues and concerns found during overall functionality testing.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / AttributeView.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.serviceprovider.view;
18
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.ILabelProviderListener;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.ITableLabelProvider;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.jface.viewers.TreeViewer;
26 import org.eclipse.jface.viewers.TreeViewerColumn;
27 import org.eclipse.jface.viewers.Viewer;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.MenuAdapter;
31 import org.eclipse.swt.events.MenuEvent;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.graphics.Color;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Group;
41 import org.eclipse.swt.widgets.Menu;
42 import org.eclipse.swt.widgets.MenuItem;
43 import org.eclipse.swt.widgets.MessageBox;
44 import org.eclipse.swt.widgets.Tree;
45 import org.eclipse.swt.widgets.TreeColumn;
46 import org.eclipse.swt.widgets.TreeItem;
47 import org.eclipse.ui.part.ViewPart;
48
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.HashMap;
52 import java.util.List;
53 import java.util.Map;
54
55 import org.oic.simulator.ArrayProperty;
56 import org.oic.simulator.AttributeProperty;
57 import org.oic.simulator.AttributeValue;
58 import org.oic.simulator.AttributeValue.TypeInfo;
59 import org.oic.simulator.AttributeValue.ValueType;
60 import org.oic.simulator.ModelProperty;
61 import org.oic.simulator.SimulatorResourceAttribute;
62 import org.oic.simulator.SimulatorResourceModel;
63
64 import oic.simulator.serviceprovider.Activator;
65 import oic.simulator.serviceprovider.listener.IAutomationListener;
66 import oic.simulator.serviceprovider.listener.IDataChangeListener;
67 import oic.simulator.serviceprovider.listener.ISelectionChangedListener;
68 import oic.simulator.serviceprovider.manager.ResourceManager;
69 import oic.simulator.serviceprovider.manager.UiListenerHandler;
70 import oic.simulator.serviceprovider.model.AttributeElement;
71 import oic.simulator.serviceprovider.model.Resource;
72 import oic.simulator.serviceprovider.model.ResourceRepresentation;
73 import oic.simulator.serviceprovider.model.SingleResource;
74 import oic.simulator.serviceprovider.utils.Constants;
75 import oic.simulator.serviceprovider.utils.Utility;
76 import oic.simulator.serviceprovider.view.dialogs.ModelArrayAddItemDialog;
77
78 /**
79  * This class manages and shows the attribute view in the perspective.
80  */
81 public class AttributeView extends ViewPart {
82
83     public static final String        VIEW_ID        = "oic.simulator.serviceprovider.view.attribute";
84
85     private TreeViewer                attViewer;
86
87     private AttributeEditingSupport   attributeEditor;
88
89     private ISelectionChangedListener resourceSelectionChangedListener;
90     private IAutomationListener       automationUIListener;
91     private IDataChangeListener       dataChangeListener;
92
93     private final String[]            attTblHeaders  = { "Name", "Value",
94             "Automation"                            };
95     private final Integer[]           attTblColWidth = { 150, 190, 150 };
96
97     private ResourceManager           resourceManager;
98
99     public AttributeView() {
100
101         resourceManager = Activator.getDefault().getResourceManager();
102
103         resourceSelectionChangedListener = new ISelectionChangedListener() {
104
105             @Override
106             public void onResourceSelectionChange(final Resource resource) {
107                 Display.getDefault().asyncExec(new Runnable() {
108                     @Override
109                     public void run() {
110                         if (null != attViewer) {
111                             Tree tree = attViewer.getTree();
112                             if (null == tree || tree.isDisposed()) {
113                                 return;
114                             }
115
116                             // Enabling/disabling the tree based on the resource
117                             // and automation status.
118                             if (resource instanceof SingleResource)
119                                 if (((SingleResource) resource)
120                                         .isResourceAutomationInProgress())
121                                     tree.setEnabled(false);
122                                 else
123                                     tree.setEnabled(true);
124
125                             if (null != resource
126                                     && null != resource
127                                             .getResourceRepresentation()) {
128                                 attViewer.setInput(resource
129                                         .getResourceRepresentation());
130                                 attViewer.expandAll();
131                                 tree.setLinesVisible(true);
132
133                             } else {
134                                 attViewer.setInput(null);
135                                 tree.setLinesVisible(false);
136                             }
137                         }
138                     }
139                 });
140             }
141         };
142
143         dataChangeListener = new IDataChangeListener() {
144
145             @Override
146             public void add(final AttributeElement attribute) {
147                 Display.getDefault().asyncExec(new Runnable() {
148                     @Override
149                     public void run() {
150                         attViewer.refresh(attribute.getParent());
151                         attViewer.expandAll();
152                     }
153                 });
154             }
155
156             @Override
157             public void remove(final AttributeElement attribute) {
158                 Display.getDefault().asyncExec(new Runnable() {
159                     @Override
160                     public void run() {
161                         attViewer.refresh(attribute.getParent());
162                         attViewer.expandAll();
163                     }
164                 });
165             }
166
167             @Override
168             public void update(final AttributeElement attribute) {
169                 Display.getDefault().asyncExec(new Runnable() {
170                     @Override
171                     public void run() {
172                         attViewer.update(attribute, null);
173                         attViewer.expandAll();
174                     }
175                 });
176             }
177         };
178
179         automationUIListener = new IAutomationListener() {
180
181             @Override
182             public void onResourceAutomationStart(final SingleResource resource) {
183                 Display.getDefault().asyncExec(new Runnable() {
184
185                     @Override
186                     public void run() {
187                         if (null == resource || null == attViewer) {
188                             return;
189                         }
190                         Resource resourceInSelection = resourceManager
191                                 .getCurrentResourceInSelection();
192                         if (null == resourceInSelection) {
193                             return;
194                         }
195                         // Checking whether attributes view is currently
196                         // displaying the attributes of the
197                         // resource whose automation has just started
198                         if (resource == resourceInSelection) {
199                             Tree tree;
200                             tree = attViewer.getTree();
201                             if (!tree.isDisposed()) {
202                                 // Disabling the table to prevent interactions
203                                 // during the automation
204                                 tree.setEnabled(false);
205                                 tree.deselectAll();
206                             }
207                         }
208                     }
209                 });
210             }
211
212             @Override
213             public void onAutomationComplete(final SingleResource resource,
214                     final String attName) {
215                 // This method notifies the completion of attribute level
216                 // automation.
217                 Display.getDefault().asyncExec(new Runnable() {
218
219                     @Override
220                     public void run() {
221                         if (null == resource) {
222                             return;
223                         }
224                         // Check if the given resourceURI is the uri of the
225                         // resource whose attributes are currently being
226                         // displayed by this view.
227                         Resource resourceInSelection = resourceManager
228                                 .getCurrentResourceInSelection();
229                         if (null == resourceInSelection) {
230                             return;
231                         }
232                         if (resource != resourceInSelection) {
233                             return;
234                         }
235                         Tree tree;
236                         tree = attViewer.getTree();
237                         if (!tree.isDisposed()) {
238                             tree.setEnabled(true);
239                         }
240                     }
241                 });
242             }
243         };
244     }
245
246     @Override
247     public void createPartControl(Composite parent) {
248         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
249
250         parent.setLayout(new GridLayout());
251         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
252         parent.setLayoutData(gd);
253
254         Group attGroup = new Group(parent, SWT.NONE);
255         attGroup.setLayout(new GridLayout());
256         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
257         attGroup.setLayoutData(gd);
258         attGroup.setText("Attributes");
259         attGroup.setBackground(color);
260
261         Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
262                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
263         addressTree.setHeaderVisible(true);
264
265         attViewer = new TreeViewer(addressTree);
266
267         createAttributeColumns(attViewer);
268
269         // make lines and header visible
270         Tree tree = attViewer.getTree();
271         tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
272         tree.setHeaderVisible(true);
273         tree.setLinesVisible(true);
274
275         attViewer.setContentProvider(new AttributeContentProvider());
276         attViewer.setLabelProvider(new AttributeLabelProvider());
277
278         addManagerListeners();
279
280         // Check whether there is any resource selected already
281         Resource resource = resourceManager.getCurrentResourceInSelection();
282         if (resource != null) {
283             attViewer.setInput(resource.getResourceRepresentation());
284         }
285     }
286
287     public void createAttributeColumns(TreeViewer viewer) {
288         Tree tree = viewer.getTree();
289
290         attributeEditor = new AttributeEditingSupport();
291
292         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
293         attName.setWidth(attTblColWidth[0]);
294         attName.setText(attTblHeaders[0]);
295
296         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
297         attValue.setWidth(attTblColWidth[1]);
298         attValue.setText(attTblHeaders[1]);
299
300         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
301                 attValue);
302         attValueVwrCol.setEditingSupport(attributeEditor
303                 .createAttributeValueEditor(attViewer, true));
304
305         TreeColumn automation = new TreeColumn(tree, SWT.NONE);
306         automation.setWidth(attTblColWidth[2]);
307         automation.setText(attTblHeaders[2]);
308         TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
309                 automation);
310         automationVwrCol.setEditingSupport(attributeEditor
311                 .createAutomationEditor(attViewer));
312
313         addColumnListeners();
314
315         addMenuItems();
316     }
317
318     private void addColumnListeners() {
319         TreeColumn[] columns = attViewer.getTree().getColumns();
320         for (TreeColumn column : columns) {
321             column.addSelectionListener(new SelectionAdapter() {
322                 @Override
323                 public void widgetSelected(SelectionEvent e) {
324                     // Refreshing the viewer. If combo list is open,
325                     // then click events on other parts of the view or outside
326                     // the combo should hide the editor.
327                     // Refreshing the viewer hides the combo and other editors
328                     // which are active.
329                     attViewer.refresh();
330                 }
331             });
332         }
333     }
334
335     private void addMenuItems() {
336         if (null != attViewer) {
337             final Tree resourceTreeHead = attViewer.getTree();
338             if (null != resourceTreeHead) {
339                 // Below code creates menu entries and shows them on right
340                 // clicking a resource
341                 final Menu menu = new Menu(resourceTreeHead);
342                 resourceTreeHead.setMenu(menu);
343                 menu.addMenuListener(new MenuAdapter() {
344                     @Override
345                     public void menuShown(MenuEvent e) {
346                         // Clear existing menu items
347                         MenuItem[] items = menu.getItems();
348                         for (int index = 0; index < items.length; index++) {
349                             items[index].dispose();
350                         }
351
352                         IStructuredSelection selection = ((IStructuredSelection) attViewer
353                                 .getSelection());
354                         final AttributeElement attElement = (AttributeElement) selection
355                                 .getFirstElement();
356                         if (null == attElement) {
357                             return;
358                         }
359
360                         // Check the type of attribute.
361                         SimulatorResourceAttribute attribute = attElement
362                                 .getSimulatorResourceAttribute();
363                         if (null == attribute) {
364                             return;
365                         }
366
367                         AttributeValue value = attribute.value();
368                         if (null == value || null == value.get()) {
369                             return;
370                         }
371
372                         TypeInfo type = value.typeInfo();
373
374                         final Object parent = attElement.getParent();
375
376                         if ((type.mType == ValueType.ARRAY
377                                 && type.mBaseType == ValueType.RESOURCEMODEL && type.mDepth == 1)
378                                 && (null == parent || parent instanceof ResourceRepresentation)) {
379                             addMenuToOneDimensionalTopLevelModelAttributes(menu);
380                             return;
381                         }
382
383                         if (null != parent
384                                 && !(parent instanceof ResourceRepresentation)) {
385                             Object grandParent = ((AttributeElement) parent)
386                                     .getParent();
387                             if (null == grandParent
388                                     || grandParent instanceof ResourceRepresentation) {
389                                 AttributeElement parentElement = (AttributeElement) parent;
390
391                                 // Check the type of attribute.
392                                 SimulatorResourceAttribute parentAttribute = parentElement
393                                         .getSimulatorResourceAttribute();
394                                 if (null != parentAttribute
395                                         && null != parentAttribute.value()
396                                         && null != parentAttribute.value()
397                                                 .get()) {
398                                     AttributeValue parentValue = parentAttribute
399                                             .value();
400
401                                     TypeInfo parentType = parentValue
402                                             .typeInfo();
403                                     if (parentType.mType == ValueType.ARRAY
404                                             && parentType.mBaseType == ValueType.RESOURCEMODEL
405                                             && parentType.mDepth == 1) {
406                                         addDeleteMenuToArrayItemsOfOneDimensionalModelAttribute(
407                                                 menu, attElement, parentElement);
408                                         return;
409                                     }
410                                 }
411                             }
412                         }
413
414                     }
415                 });
416             }
417         }
418     }
419
420     private void addMenuToOneDimensionalTopLevelModelAttributes(Menu menu) {
421         // Menu to add items to the array.
422         MenuItem addItems = new MenuItem(menu, SWT.NONE);
423         addItems.setText("Add Items");
424         addItems.addSelectionListener(new SelectionAdapter() {
425             @Override
426             public void widgetSelected(SelectionEvent e) {
427                 // Get the attributes.
428                 ResourceRepresentation representation;
429                 representation = getRepresentationForOneDimensionTopLevelAttribute();
430                 if (null == representation) {
431                     MessageDialog
432                             .openError(Display.getDefault().getActiveShell(),
433                                     "Unable to perform the operation.",
434                                     "Failed to obtain the required data. Operation cannot be performed.");
435                 } else {
436                     // Check whether a new item can be added to the array by
437                     // checking
438                     // the array property of the current attribute in
439                     // selection(Model Array type attribute).
440                     AttributeElement attElement = getSelectedElement();
441                     SimulatorResourceAttribute attribute = attElement
442                             .getSimulatorResourceAttribute();
443
444                     AttributeValue attValue = attribute.value();
445                     AttributeProperty attProperty = attribute.property();
446                     if (null != attProperty
447                             && attProperty instanceof ArrayProperty) {
448                         ArrayProperty prop = attProperty.asArray();
449                         if (null != prop && !prop.isVariable()) {
450                             SimulatorResourceModel[] model = (SimulatorResourceModel[]) attValue
451                                     .get();
452                             if (null != model
453                                     && model.length >= prop.maxItems()) {
454                                 MessageDialog
455                                         .openError(
456                                                 Display.getDefault()
457                                                         .getActiveShell(),
458                                                 "Unable to perform the operation.",
459                                                 "Exceeding the maximum number of array elements allowed for this attribute.\n"
460                                                         + "Maximum number of allowed array element(s): "
461                                                         + prop.maxItems());
462                                 return;
463                             }
464                         }
465                     }
466
467                     ModelArrayAddItemDialog dialog = new ModelArrayAddItemDialog(
468                             Display.getDefault().getActiveShell(),
469                             representation);
470                     if (Window.OK == dialog.open()) {
471                         // Add the new item to the local resource
472                         // representation.
473                         AttributeElement newElement = (AttributeElement) representation
474                                 .getAttributes().values().toArray()[0];
475                         SimulatorResourceAttribute newAttribute = newElement
476                                 .getSimulatorResourceAttribute();
477                         SimulatorResourceModel newModel = (SimulatorResourceModel) newAttribute
478                                 .value().get();
479
480                         SimulatorResourceModel[] modelArray = (SimulatorResourceModel[]) attribute
481                                 .value().get();
482                         SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[modelArray.length + 1];
483
484                         int i;
485                         for (i = 0; i < modelArray.length; i++) {
486                             newModelArray[i] = modelArray[i];
487                         }
488                         newModelArray[i] = newModel;
489
490                         AttributeValue newValue = new AttributeValue(
491                                 newModelArray);
492
493                         newAttribute.setValue(newValue);
494
495                         newAttribute.setProperty(attribute.property());
496
497                         attElement.update(newAttribute);
498
499                         boolean updated = resourceManager.attributeValueUpdated(
500                                 (SingleResource) resourceManager
501                                         .getCurrentResourceInSelection(),
502                                 attribute.name(), newValue);
503
504                         if (!updated) {
505                             attribute.setValue(new AttributeValue(modelArray));
506                             attElement.update(newAttribute);
507
508                             MessageDialog
509                                     .openInformation(Display.getDefault()
510                                             .getActiveShell(),
511                                             "Operation failed",
512                                             "Failed to insert a new item in the array.");
513                         } else {
514                             // Highlight the newly added item.
515                             AttributeElement addedElement = attElement
516                                     .getChildren().get("[" + i + "]");
517                             attViewer.setSelection(new StructuredSelection(
518                                     addedElement), true);
519                         }
520                     }
521                 }
522             }
523         });
524     }
525
526     private void addDeleteMenuToArrayItemsOfOneDimensionalModelAttribute(
527             final Menu menu, final AttributeElement elementToDelete,
528             final AttributeElement parentElement) {
529         // Menu to add items to the array.
530         MenuItem addItems = new MenuItem(menu, SWT.NONE);
531         addItems.setText("Delete Item");
532         addItems.addSelectionListener(new SelectionAdapter() {
533             @Override
534             public void widgetSelected(SelectionEvent e) {
535                 // Check whether any existing item can be removed from the array
536                 // by checking
537                 // the array property of the current attribute in
538                 // selection(Model Array type attribute).
539                 SimulatorResourceAttribute parentSRA = parentElement
540                         .getSimulatorResourceAttribute();
541                 AttributeValue value = parentSRA.value();
542                 AttributeProperty attProperty = parentSRA.property();
543                 if (null != attProperty && attProperty instanceof ArrayProperty) {
544                     ArrayProperty prop = attProperty.asArray();
545                     if (null != prop) {
546                         SimulatorResourceModel[] model = (SimulatorResourceModel[]) value
547                                 .get();
548                         if (null != model && model.length <= prop.minItems()) {
549                             MessageDialog
550                                     .openError(
551                                             Display.getDefault()
552                                                     .getActiveShell(),
553                                             "Unable to perform the operation.",
554                                             "Violating the minimum number of array elements allowed for this attribute.\n"
555                                                     + "Minimum number of allowed array element(s): "
556                                                     + prop.minItems());
557                             return;
558                         }
559                     }
560                 }
561
562                 MessageBox dialog = new MessageBox(menu.getShell(),
563                         SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
564                 dialog.setText("Confirm action");
565                 dialog.setMessage("Do you want to delete this item from the array?");
566                 int retval = dialog.open();
567                 if (retval != SWT.OK) {
568                     return;
569                 }
570
571                 // Removing the element from the attribute value.
572                 SimulatorResourceModel[] modelArray = (SimulatorResourceModel[]) value
573                         .get();
574
575                 String elementIndexName = elementToDelete
576                         .getSimulatorResourceAttribute().name();
577                 int elementIndex = Integer.parseInt(elementIndexName.substring(
578                         elementIndexName.indexOf('[') + 1,
579                         elementIndexName.indexOf(']')));
580
581                 SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[modelArray.length - 1];
582                 int sIndex = 0, dIndex = 0;
583                 for (SimulatorResourceModel model : modelArray) {
584                     if (sIndex != elementIndex)
585                         newModelArray[dIndex++] = model;
586                     sIndex++;
587                 }
588
589                 // Setting the new model array in the attribute.
590                 AttributeValue newValue = new AttributeValue(newModelArray);
591                 parentSRA.setValue(newValue);
592
593                 // Removing the element from the child map.
594                 Map<String, AttributeElement> elements = parentElement
595                         .getChildren();
596                 List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
597                 attElementList.addAll(elements.values());
598                 Collections.sort(attElementList, Utility.attributeComparator);
599
600                 // Renaming the index of the elements.
601                 AttributeElement[] attElementArray = attElementList
602                         .toArray(new AttributeElement[0]);
603                 boolean deleted = false;
604                 int index, newIndex;
605                 for (index = 0, newIndex = 0; index < attElementArray.length; index++) {
606                     if (index == elementIndex) {
607                         elements.remove(elementIndexName);
608                         deleted = true;
609                     } else {
610                         if (deleted) {
611                             AttributeElement element = attElementArray[index];
612                             String curIndexStr = "[" + index + "]";
613                             String newIndexStr = "[" + newIndex + "]";
614
615                             element.getSimulatorResourceAttribute().setName(
616                                     newIndexStr);
617
618                             elements.remove(curIndexStr);
619                             elements.put(newIndexStr, element);
620                         }
621                         newIndex++;
622                     }
623                 }
624
625                 resourceManager.attributeValueUpdated(
626                         (SingleResource) resourceManager
627                                 .getCurrentResourceInSelection(), parentSRA
628                                 .name(), newValue);
629
630                 attViewer.refresh(parentElement);
631             }
632         });
633     }
634
635     private ResourceRepresentation getRepresentationForOneDimensionTopLevelAttribute() {
636         ResourceRepresentation representation = null;
637
638         AttributeValue value = null;
639         ModelProperty property = null;
640         SimulatorResourceAttribute attribute;
641
642         AttributeElement element = getSelectedElement();
643         if (null == element)
644             return null;
645
646         SimulatorResourceAttribute modelArrayAtt = element
647                 .getSimulatorResourceAttribute();
648         if (null == modelArrayAtt) {
649             return null;
650         }
651
652         AttributeValue attValue = modelArrayAtt.value();
653         if (null == attValue) {
654             return null;
655         }
656
657         TypeInfo type = attValue.typeInfo();
658
659         if (!(type.mType == ValueType.ARRAY
660                 && type.mBaseType == ValueType.RESOURCEMODEL && type.mDepth == 1)) {
661             return null;
662         }
663
664         SimulatorResourceModel[] modelValue = (SimulatorResourceModel[]) attValue
665                 .get();
666         if (null == modelValue || modelValue.length < 0) {
667             return null;
668         }
669
670         // Clone an instance of model value.
671         try {
672             value = Utility.cloneAttributeValue(new AttributeValue(
673                     modelValue[0]));
674         } catch (Exception e) {
675             return null;
676         }
677
678         if (null == value) {
679             return null;
680         }
681
682         // Get the model property of the model value instance.
683         AttributeProperty attProperty = modelArrayAtt.property();
684         if (null != attProperty && attProperty instanceof ArrayProperty) {
685             ArrayProperty prop = attProperty.asArray();
686             if (null != prop) {
687                 AttributeProperty elementProperty = prop.getElementProperty();
688                 if (null != elementProperty && elementProperty.isModel()) {
689                     property = elementProperty.asModel();
690                 }
691             }
692         }
693
694         attribute = new SimulatorResourceAttribute(modelArrayAtt.name(), value,
695                 property);
696
697         Map<String, SimulatorResourceAttribute> attributes = new HashMap<String, SimulatorResourceAttribute>();
698         attributes.put(attribute.name(), attribute);
699
700         representation = new ResourceRepresentation(attributes);
701
702         return representation;
703     }
704
705     private AttributeElement getSelectedElement() {
706         IStructuredSelection selection = (IStructuredSelection) attViewer
707                 .getSelection();
708         if (null == selection) {
709             return null;
710         }
711
712         Object obj = selection.getFirstElement();
713         if (null == obj) {
714             return null;
715         }
716
717         Tree t = attViewer.getTree();
718         TreeItem item = t.getSelection()[0];
719         if (null == item) {
720             return null;
721         }
722
723         if (!(item.getData() instanceof AttributeElement)) {
724             return null;
725         }
726
727         return (AttributeElement) item.getData();
728     }
729
730     private void addManagerListeners() {
731         UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
732                 resourceSelectionChangedListener);
733         UiListenerHandler.getInstance().addDataChangeListener(
734                 dataChangeListener);
735         UiListenerHandler.getInstance().addAutomationUIListener(
736                 automationUIListener);
737     }
738
739     class AttributeContentProvider implements ITreeContentProvider {
740
741         @Override
742         public void dispose() {
743         }
744
745         @Override
746         public void inputChanged(Viewer viewer, Object oldAttribute,
747                 Object newAttribute) {
748         }
749
750         @Override
751         public Object[] getChildren(Object attribute) {
752             if (attribute instanceof AttributeElement) {
753                 List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
754                 Map<String, AttributeElement> children = ((AttributeElement) attribute)
755                         .getChildren();
756                 if (null != children) {
757                     attElementList.addAll(children.values());
758                     Collections.sort(attElementList,
759                             Utility.attributeComparator);
760                     return attElementList.toArray();
761                 }
762             }
763
764             return new Object[0];
765         }
766
767         @Override
768         public Object getParent(Object attribute) {
769             if (attribute instanceof AttributeElement)
770                 return ((AttributeElement) attribute).getParent();
771             return null;
772         }
773
774         @Override
775         public boolean hasChildren(Object attribute) {
776             if (attribute instanceof AttributeElement)
777                 return ((AttributeElement) attribute).hasChildren();
778             return false;
779         }
780
781         @Override
782         public Object[] getElements(Object resourceModel) {
783             if (resourceModel instanceof ResourceRepresentation) {
784                 return ((ResourceRepresentation) resourceModel).getAttributes()
785                         .values().toArray();
786             }
787
788             return new Object[0];
789         }
790     }
791
792     class AttributeLabelProvider implements ITableLabelProvider {
793
794         @Override
795         public void addListener(ILabelProviderListener arg0) {
796         }
797
798         @Override
799         public void dispose() {
800         }
801
802         @Override
803         public boolean isLabelProperty(Object arg0, String arg1) {
804             return false;
805         }
806
807         @Override
808         public void removeListener(ILabelProviderListener arg0) {
809
810         }
811
812         @Override
813         public Image getColumnImage(Object element, int col) {
814             if (col == 2) {
815                 if (element instanceof AttributeElement) {
816                     // Ignore for non-single resource
817                     Resource res = resourceManager
818                             .getCurrentResourceInSelection();
819                     if (res instanceof SingleResource) {
820                         AttributeElement attrElement = (AttributeElement) element;
821                         if (attrElement.isAutoUpdateSupport()
822                                 && !attrElement.isReadOnly()) {
823                             if (attrElement.isAutoUpdateInProgress()) {
824                                 return Activator.getDefault()
825                                         .getImageRegistry()
826                                         .get(Constants.CHECKED);
827                             } else {
828                                 return Activator.getDefault()
829                                         .getImageRegistry()
830                                         .get(Constants.UNCHECKED);
831                             }
832                         }
833                     }
834                 }
835             }
836             return null;
837         }
838
839         @Override
840         public String getColumnText(Object element, int column) {
841             if (element instanceof AttributeElement) {
842                 AttributeElement attrElement = (AttributeElement) element;
843                 switch (column) {
844                     case 0: // Attribute name column
845                     {
846                         SimulatorResourceAttribute attribute = attrElement
847                                 .getSimulatorResourceAttribute();
848                         return attribute.name();
849                     }
850
851                     case 1: // Attribute value column
852                     {
853                         SimulatorResourceAttribute attribute = attrElement
854                                 .getSimulatorResourceAttribute();
855
856                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
857                             String value = Utility
858                                     .getAttributeValueAsString(attribute
859                                             .value());
860                             if (null == value) {
861                                 value = "";
862                             }
863                             return value;
864                         }
865                         return null;
866                     }
867
868                     case 2: {
869                         // Ignore for non-single resource
870                         Resource res = resourceManager
871                                 .getCurrentResourceInSelection();
872                         if (res instanceof SingleResource) {
873                             SimulatorResourceAttribute attribute = attrElement
874                                     .getSimulatorResourceAttribute();
875                             TypeInfo type = attribute.value().typeInfo();
876                             if (type.mType == AttributeValue.ValueType.ARRAY) {
877                                 if (type.mBaseType != AttributeValue.ValueType.RESOURCEMODEL) {
878                                     return "NA";
879                                 }
880                             } else if (type.mType != AttributeValue.ValueType.RESOURCEMODEL) {
881                                 Object parent = attrElement.getParent();
882                                 if (null != parent
883                                         && !(parent instanceof ResourceRepresentation)) {
884                                     return "NA";
885                                 } else if (attrElement.isReadOnly()) {
886                                     return "NA";
887                                 } else if (attrElement.isAutoUpdateSupport()) {
888                                     if (attrElement.isAutoUpdateInProgress())
889                                         return Constants.ENABLED;
890                                     else
891                                         return Constants.DISABLED;
892                                 }
893                             }
894                         }
895
896                         return "";
897                     }
898                 }
899             }
900
901             return null;
902         }
903
904     }
905
906     @Override
907     public void dispose() {
908         // Unregister the selection listener
909         if (null != resourceSelectionChangedListener) {
910             UiListenerHandler.getInstance()
911                     .removeResourceSelectionChangedUIListener(
912                             resourceSelectionChangedListener);
913         }
914
915         // Unregister the data model change listener
916         if (null != dataChangeListener) {
917             UiListenerHandler.getInstance().removeDataChangeListener(
918                     dataChangeListener);
919         }
920
921         // Unregister the automation complete listener
922         if (null != automationUIListener) {
923             UiListenerHandler.getInstance().removeAutomationUIListener(
924                     automationUIListener);
925         }
926
927         super.dispose();
928     }
929
930     @Override
931     public void setFocus() {
932
933     }
934 }