2 * Copyright 2016 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.serviceprovider.view.dialogs;
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;
42 import org.oic.simulator.AttributeValue.ValueType;
43 import org.oic.simulator.SimulatorResourceAttribute;
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;
54 * This class manages and shows the automation settings dialog from the
57 public class ModelArrayAddItemDialog extends TitleAreaDialog {
59 private TreeViewer attViewer;
61 private AttributeEditingSupport attributeEditor;
63 private final String[] attTblHeaders = { "Attribute Name",
65 private final Integer[] attTblColWidth = { 200, 300 };
67 private ResourceManager resourceManager;
69 private ResourceRepresentation representation;
71 public ModelArrayAddItemDialog(Shell parentShell,
72 ResourceRepresentation representation) {
74 resourceManager = Activator.getDefault().getResourceManager();
75 this.representation = representation;
79 public void create() {
81 setTitle("Add Items To Complex Array");
82 setMessage("Add one or more items in the complex array type attribute");
86 protected Control createDialogArea(Composite parent) {
87 Composite compLayout = (Composite) super.createDialogArea(parent);
89 compLayout.setLayout(new GridLayout());
90 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
91 compLayout.setLayoutData(gd);
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");
99 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
100 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
101 addressTree.setHeaderVisible(true);
103 attViewer = new TreeViewer(addressTree);
105 createAttributeColumns(attViewer);
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);
113 attViewer.setContentProvider(new AttributeContentProvider());
114 attViewer.setLabelProvider(new AttributeLabelProvider());
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();
126 public void createAttributeColumns(TreeViewer viewer) {
127 Tree tree = viewer.getTree();
129 attributeEditor = new AttributeEditingSupport();
131 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
132 attName.setWidth(attTblColWidth[0]);
133 attName.setText(attTblHeaders[0]);
135 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
136 attValue.setWidth(attTblColWidth[1]);
137 attValue.setText(attTblHeaders[1]);
139 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
141 attValueVwrCol.setEditingSupport(attributeEditor
142 .createAttributeValueEditor(attViewer, false));
144 addColumnListeners();
147 private void addColumnListeners() {
148 TreeColumn[] columns = attViewer.getTree().getColumns();
149 for (TreeColumn column : columns) {
150 column.addSelectionListener(new SelectionAdapter() {
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
164 private static class AttributeContentProvider implements
165 ITreeContentProvider {
168 public void dispose() {
172 public void inputChanged(Viewer viewer, Object oldAttribute,
173 Object newAttribute) {
177 public Object[] getChildren(Object attribute) {
178 if (attribute instanceof AttributeElement) {
179 Map<String, AttributeElement> childs = ((AttributeElement) attribute)
181 if (null == childs) {
182 return new Object[0];
185 return childs.values().toArray();
188 return new Object[0];
192 public Object getParent(Object attribute) {
193 if (attribute instanceof AttributeElement)
194 return ((AttributeElement) attribute).getParent();
199 public boolean hasChildren(Object attribute) {
200 if (attribute instanceof AttributeElement)
201 return ((AttributeElement) attribute).hasChildren();
206 public Object[] getElements(Object resourceModel) {
207 if (resourceModel instanceof ResourceRepresentation) {
208 return ((ResourceRepresentation) resourceModel).getAttributes()
212 return new Object[0];
216 private static class AttributeLabelProvider implements ITableLabelProvider {
219 public void addListener(ILabelProviderListener arg0) {
223 public void dispose() {
227 public boolean isLabelProperty(Object arg0, String arg1) {
232 public void removeListener(ILabelProviderListener arg0) {
237 public Image getColumnImage(Object element, int col) {
242 public String getColumnText(Object element, int column) {
243 if (element instanceof AttributeElement) {
244 AttributeElement attrElement = (AttributeElement) element;
246 case 0: // Attribute name column
248 SimulatorResourceAttribute attribute = attrElement
249 .getSimulatorResourceAttribute();
250 return attribute.name();
253 case 1: // Attribute value column
255 SimulatorResourceAttribute attribute = attrElement
256 .getSimulatorResourceAttribute();
258 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
259 String value = Utility
260 .getAttributeValueAsString(attribute
278 protected Point getInitialSize() {
279 Point initialPoint = super.getInitialSize();
280 initialPoint.y += 100;
285 protected boolean isResizable() {
290 public boolean isHelpAvailable() {