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