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