Fix for klocwork issues.
[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     class AttributeContentProvider implements ITreeContentProvider {
169
170         @Override
171         public void dispose() {
172         }
173
174         @Override
175         public void inputChanged(Viewer viewer, Object oldAttribute,
176                 Object newAttribute) {
177         }
178
179         @Override
180         public Object[] getChildren(Object attribute) {
181             if (attribute instanceof AttributeElement) {
182                 Map<String, AttributeElement> children = ((AttributeElement) attribute)
183                         .getChildren();
184                 if (null != children) {
185                     return children.values().toArray();
186                 }
187             }
188
189             return new Object[0];
190         }
191
192         @Override
193         public Object getParent(Object attribute) {
194             if (attribute instanceof AttributeElement)
195                 return ((AttributeElement) attribute).getParent();
196             return null;
197         }
198
199         @Override
200         public boolean hasChildren(Object attribute) {
201             if (attribute instanceof AttributeElement)
202                 return ((AttributeElement) attribute).hasChildren();
203             return false;
204         }
205
206         @Override
207         public Object[] getElements(Object resourceModel) {
208             if (resourceModel instanceof ResourceRepresentation) {
209                 return ((ResourceRepresentation) resourceModel).getAttributes()
210                         .values().toArray();
211             }
212
213             return new Object[0];
214         }
215     }
216
217     class AttributeLabelProvider implements ITableLabelProvider {
218
219         @Override
220         public void addListener(ILabelProviderListener arg0) {
221         }
222
223         @Override
224         public void dispose() {
225         }
226
227         @Override
228         public boolean isLabelProperty(Object arg0, String arg1) {
229             return false;
230         }
231
232         @Override
233         public void removeListener(ILabelProviderListener arg0) {
234
235         }
236
237         @Override
238         public Image getColumnImage(Object element, int col) {
239             return null;
240         }
241
242         @Override
243         public String getColumnText(Object element, int column) {
244             if (element instanceof AttributeElement) {
245                 AttributeElement attrElement = (AttributeElement) element;
246                 switch (column) {
247                     case 0: // Attribute name column
248                     {
249                         SimulatorResourceAttribute attribute = attrElement
250                                 .getSimulatorResourceAttribute();
251                         return attribute.name();
252                     }
253
254                     case 1: // Attribute value column
255                     {
256                         SimulatorResourceAttribute attribute = attrElement
257                                 .getSimulatorResourceAttribute();
258
259                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
260                             String value = Utility
261                                     .getAttributeValueAsString(attribute
262                                             .value());
263                             if (null == value) {
264                                 value = "";
265                             }
266                             return value;
267                         }
268                         return null;
269                     }
270                 }
271             }
272
273             return null;
274         }
275
276     }
277
278     @Override
279     protected Point getInitialSize() {
280         Point initialPoint = super.getInitialSize();
281         initialPoint.y += 100;
282         return initialPoint;
283     }
284
285     @Override
286     protected boolean isResizable() {
287         return true;
288     }
289
290     @Override
291     public boolean isHelpAvailable() {
292         return false;
293     }
294 }