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