Merge "Merge branch 'simulator'."
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / view / dialogs / ModelArrayAddItemDialog.java
1 /*
2  * Copyright 2016 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.TitleAreaDialog;
20 import org.eclipse.jface.viewers.ILabelProviderListener;
21 import org.eclipse.jface.viewers.ITableLabelProvider;
22 import org.eclipse.jface.viewers.ITreeContentProvider;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.jface.viewers.TreeViewerColumn;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Group;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Tree;
38 import org.eclipse.swt.widgets.TreeColumn;
39
40 import org.oic.simulator.AttributeValue.ValueType;
41 import org.oic.simulator.SimulatorResourceAttribute;
42
43 import oic.simulator.clientcontroller.Activator;
44 import oic.simulator.clientcontroller.manager.ResourceManager;
45 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
46 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
47 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
48 import oic.simulator.clientcontroller.utils.Utility;
49 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
50
51 /**
52  * This class manages and shows the automation settings dialog from the
53  * attribute view.
54  */
55 public class ModelArrayAddItemDialog extends TitleAreaDialog {
56
57     private TreeViewer              attViewer;
58
59     private AttributeEditingSupport attributeEditor;
60
61     private final String[]          attTblHeaders  = { "Attribute Name",
62             "Attribute Value"                     };
63     private final Integer[]         attTblColWidth = { 200, 300 };
64
65     private ResourceManager         resourceManager;
66
67     private ResourceRepresentation  representation;
68
69     private TitleAreaDialog         dialog;
70
71     public ModelArrayAddItemDialog(Shell parentShell, TitleAreaDialog dialog,
72             ResourceRepresentation representation) {
73         super(parentShell);
74         resourceManager = Activator.getDefault().getResourceManager();
75         this.dialog = dialog;
76         this.representation = representation;
77     }
78
79     @Override
80     public void create() {
81         super.create();
82         setTitle("Add Items To Complex Array");
83         setMessage("Add one or more items in the complex array type attribute");
84     }
85
86     @Override
87     protected Control createDialogArea(Composite parent) {
88         Composite compLayout = (Composite) super.createDialogArea(parent);
89
90         compLayout.setLayout(new GridLayout());
91         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
92         compLayout.setLayoutData(gd);
93
94         Group attGroup = new Group(compLayout, SWT.NONE);
95         attGroup.setLayout(new GridLayout());
96         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
97         attGroup.setLayoutData(gd);
98         attGroup.setText("Attributes");
99
100         Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
101                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
102         addressTree.setHeaderVisible(true);
103
104         attViewer = new TreeViewer(addressTree);
105
106         createAttributeColumns(attViewer);
107
108         // make lines and header visible
109         Tree tree = attViewer.getTree();
110         tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
111         tree.setHeaderVisible(true);
112         tree.setLinesVisible(true);
113
114         attViewer.setContentProvider(new AttributeContentProvider());
115         attViewer.setLabelProvider(new AttributeLabelProvider());
116
117         // Check whether there is any resource selected already
118         RemoteResource resource = resourceManager
119                 .getCurrentResourceInSelection();
120         if (resource != null) {
121             attViewer.setInput(representation);
122             attViewer.expandAll();
123         }
124
125         return compLayout;
126     }
127
128     public void createAttributeColumns(TreeViewer viewer) {
129         Tree tree = viewer.getTree();
130
131         attributeEditor = new AttributeEditingSupport();
132
133         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
134         attName.setWidth(attTblColWidth[0]);
135         attName.setText(attTblHeaders[0]);
136
137         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
138         attValue.setWidth(attTblColWidth[1]);
139         attValue.setText(attTblHeaders[1]);
140
141         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
142                 attValue);
143         attValueVwrCol.setEditingSupport(attributeEditor
144                 .createAttributeValueEditor(attViewer, dialog));
145
146         addColumnListeners();
147     }
148
149     private void addColumnListeners() {
150         TreeColumn[] columns = attViewer.getTree().getColumns();
151         for (TreeColumn column : columns) {
152             column.addSelectionListener(new SelectionAdapter() {
153                 @Override
154                 public void widgetSelected(SelectionEvent e) {
155                     // Refreshing the viewer. If combo list is open,
156                     // then click events on other parts of the view or outside
157                     // the combo should hide the editor.
158                     // Refreshing the viewer hides the combo and other editors
159                     // which are active.
160                     attViewer.refresh();
161                 }
162             });
163         }
164     }
165
166     class AttributeContentProvider implements ITreeContentProvider {
167
168         @Override
169         public void dispose() {
170         }
171
172         @Override
173         public void inputChanged(Viewer viewer, Object oldAttribute,
174                 Object newAttribute) {
175         }
176
177         @Override
178         public Object[] getChildren(Object attribute) {
179             if (attribute instanceof AttributeElement) {
180                 return ((AttributeElement) attribute).getChildren().values()
181                         .toArray();
182             }
183
184             return new Object[0];
185         }
186
187         @Override
188         public Object getParent(Object attribute) {
189             if (attribute instanceof AttributeElement)
190                 return ((AttributeElement) attribute).getParent();
191             return null;
192         }
193
194         @Override
195         public boolean hasChildren(Object attribute) {
196             if (attribute instanceof AttributeElement)
197                 return ((AttributeElement) attribute).hasChildren();
198             return false;
199         }
200
201         @Override
202         public Object[] getElements(Object resourceModel) {
203             if (resourceModel instanceof ResourceRepresentation) {
204                 return ((ResourceRepresentation) resourceModel).getAttributes()
205                         .values().toArray();
206             }
207
208             return new Object[0];
209         }
210     }
211
212     class AttributeLabelProvider implements ITableLabelProvider {
213
214         @Override
215         public void addListener(ILabelProviderListener arg0) {
216         }
217
218         @Override
219         public void dispose() {
220         }
221
222         @Override
223         public boolean isLabelProperty(Object arg0, String arg1) {
224             return false;
225         }
226
227         @Override
228         public void removeListener(ILabelProviderListener arg0) {
229
230         }
231
232         @Override
233         public Image getColumnImage(Object element, int col) {
234             return null;
235         }
236
237         @Override
238         public String getColumnText(Object element, int column) {
239             if (element instanceof AttributeElement) {
240                 AttributeElement attrElement = (AttributeElement) element;
241                 switch (column) {
242                     case 0: // Attribute name column
243                     {
244                         SimulatorResourceAttribute attribute = attrElement
245                                 .getSimulatorResourceAttribute();
246                         return attribute.name();
247                     }
248
249                     case 1: // Attribute value column
250                     {
251                         SimulatorResourceAttribute attribute = attrElement
252                                 .getSimulatorResourceAttribute();
253
254                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
255                             String value = Utility
256                                     .getAttributeValueAsString(attribute
257                                             .value());
258                             if (null == value) {
259                                 value = "";
260                             }
261                             return value;
262                         }
263                         return null;
264                     }
265                 }
266             }
267
268             return null;
269         }
270
271     }
272
273     @Override
274     protected Point getInitialSize() {
275         Point initialPoint = super.getInitialSize();
276         initialPoint.y += 100;
277         return initialPoint;
278     }
279
280     @Override
281     protected boolean isResizable() {
282         return true;
283     }
284
285     @Override
286     public boolean isHelpAvailable() {
287         return false;
288     }
289 }