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