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.clientcontroller.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;
40 import org.oic.simulator.AttributeValue.ValueType;
41 import org.oic.simulator.SimulatorResourceAttribute;
43 import oic.simulator.clientcontroller.Activator;
44 import oic.simulator.clientcontroller.manager.ResourceManager;
45 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
46 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
47 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
48 import oic.simulator.clientcontroller.utils.Utility;
49 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
52 * This class manages and shows the automation settings dialog from the
55 public class ModelArrayAddItemDialog extends TitleAreaDialog {
57 private TreeViewer attViewer;
59 private AttributeEditingSupport attributeEditor;
61 private final String[] attTblHeaders = { "Attribute Name",
63 private final Integer[] attTblColWidth = { 200, 300 };
65 private ResourceManager resourceManager;
67 private ResourceRepresentation representation;
69 private TitleAreaDialog dialog;
71 public ModelArrayAddItemDialog(Shell parentShell, TitleAreaDialog dialog,
72 ResourceRepresentation representation) {
74 resourceManager = Activator.getDefault().getResourceManager();
76 this.representation = representation;
80 public void create() {
82 setTitle("Add Items To Complex Array");
83 setMessage("Add one or more items in the complex array type attribute");
87 protected Control createDialogArea(Composite parent) {
88 Composite compLayout = (Composite) super.createDialogArea(parent);
90 compLayout.setLayout(new GridLayout());
91 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
92 compLayout.setLayoutData(gd);
94 Group attGroup = new Group(compLayout, SWT.NONE);
95 attGroup.setLayout(new GridLayout());
96 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
97 attGroup.setLayoutData(gd);
98 attGroup.setText("Attributes");
100 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
101 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
102 addressTree.setHeaderVisible(true);
104 attViewer = new TreeViewer(addressTree);
106 createAttributeColumns(attViewer);
108 // make lines and header visible
109 Tree tree = attViewer.getTree();
110 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
111 tree.setHeaderVisible(true);
112 tree.setLinesVisible(true);
114 attViewer.setContentProvider(new AttributeContentProvider());
115 attViewer.setLabelProvider(new AttributeLabelProvider());
117 // Check whether there is any resource selected already
118 RemoteResource resource = resourceManager
119 .getCurrentResourceInSelection();
120 if (resource != null) {
121 attViewer.setInput(representation);
122 attViewer.expandAll();
128 public void createAttributeColumns(TreeViewer viewer) {
129 Tree tree = viewer.getTree();
131 attributeEditor = new AttributeEditingSupport();
133 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
134 attName.setWidth(attTblColWidth[0]);
135 attName.setText(attTblHeaders[0]);
137 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
138 attValue.setWidth(attTblColWidth[1]);
139 attValue.setText(attTblHeaders[1]);
141 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
143 attValueVwrCol.setEditingSupport(attributeEditor
144 .createAttributeValueEditor(attViewer, dialog));
146 addColumnListeners();
149 private void addColumnListeners() {
150 TreeColumn[] columns = attViewer.getTree().getColumns();
151 for (TreeColumn column : columns) {
152 column.addSelectionListener(new SelectionAdapter() {
154 public void widgetSelected(SelectionEvent e) {
155 // Refreshing the viewer. If combo list is open,
156 // then click events on other parts of the view or outside
157 // the combo should hide the editor.
158 // Refreshing the viewer hides the combo and other editors
166 class AttributeContentProvider implements ITreeContentProvider {
169 public void dispose() {
173 public void inputChanged(Viewer viewer, Object oldAttribute,
174 Object newAttribute) {
178 public Object[] getChildren(Object attribute) {
179 if (attribute instanceof AttributeElement) {
180 return ((AttributeElement) attribute).getChildren().values()
184 return new Object[0];
188 public Object getParent(Object attribute) {
189 if (attribute instanceof AttributeElement)
190 return ((AttributeElement) attribute).getParent();
195 public boolean hasChildren(Object attribute) {
196 if (attribute instanceof AttributeElement)
197 return ((AttributeElement) attribute).hasChildren();
202 public Object[] getElements(Object resourceModel) {
203 if (resourceModel instanceof ResourceRepresentation) {
204 return ((ResourceRepresentation) resourceModel).getAttributes()
208 return new Object[0];
212 class AttributeLabelProvider implements ITableLabelProvider {
215 public void addListener(ILabelProviderListener arg0) {
219 public void dispose() {
223 public boolean isLabelProperty(Object arg0, String arg1) {
228 public void removeListener(ILabelProviderListener arg0) {
233 public Image getColumnImage(Object element, int col) {
238 public String getColumnText(Object element, int column) {
239 if (element instanceof AttributeElement) {
240 AttributeElement attrElement = (AttributeElement) element;
242 case 0: // Attribute name column
244 SimulatorResourceAttribute attribute = attrElement
245 .getSimulatorResourceAttribute();
246 return attribute.name();
249 case 1: // Attribute value column
251 SimulatorResourceAttribute attribute = attrElement
252 .getSimulatorResourceAttribute();
254 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
255 String value = Utility
256 .getAttributeValueAsString(attribute
274 protected Point getInitialSize() {
275 Point initialPoint = super.getInitialSize();
276 initialPoint.y += 100;
281 protected boolean isResizable() {
286 public boolean isHelpAvailable() {