2 * Copyright 2015 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;
19 import java.util.List;
22 import oic.simulator.serviceprovider.Activator;
23 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
24 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
25 import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
26 import oic.simulator.serviceprovider.manager.ResourceManager;
27 import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
28 import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
29 import oic.simulator.serviceprovider.resource.SimulatorResource;
30 import oic.simulator.serviceprovider.utils.Constants;
32 import org.eclipse.jface.viewers.ColumnLabelProvider;
33 import org.eclipse.jface.viewers.IStructuredContentProvider;
34 import org.eclipse.jface.viewers.StyledCellLabelProvider;
35 import org.eclipse.jface.viewers.TableViewer;
36 import org.eclipse.jface.viewers.TableViewerColumn;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.jface.viewers.ViewerCell;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.graphics.Color;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.Group;
47 import org.eclipse.swt.widgets.Table;
48 import org.eclipse.ui.part.ViewPart;
51 * This class manages and shows the attribute view in the perspective.
53 public class AttributeView extends ViewPart {
55 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.attribute";
57 private TableViewer attTblViewer;
59 private AttributeEditingSupport attributeEditor;
61 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
62 private IResourceModelChangedUIListener resourceModelChangedUIListener;
63 private IAutomationUIListener automationUIListener;
65 private final String[] attTblHeaders = { "Name",
66 "Value", "Automation" };
67 private final Integer[] attTblColWidth = { 150, 190,
70 private ResourceManager resourceManager;
72 public AttributeView() {
74 resourceManager = Activator.getDefault().getResourceManager();
76 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
79 public void onResourceSelectionChange() {
80 Display.getDefault().asyncExec(new Runnable() {
84 if (null != attTblViewer) {
85 updateViewer(getData());
86 SimulatorResource resource = resourceManager
87 .getCurrentResourceInSelection();
88 Table tbl = attTblViewer.getTable();
89 if (!tbl.isDisposed()) {
92 .isResourceAutomationInProgress()) {
93 tbl.setEnabled(false);
104 resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
107 public void onResourceModelChange(
108 final ModelChangeNotificationType notificationType,
109 final String resourceURI,
110 final Set<LocalResourceAttribute> valueChangeSet) {
111 Display.getDefault().asyncExec(new Runnable() {
114 // Handle the notification only if it is for the current
115 // resource in selection
116 SimulatorResource resource = resourceManager
117 .getCurrentResourceInSelection();
118 if (null == resource) {
121 if (!resourceURI.equals(resource.getResourceURI())) {
122 // This notification is for a different resource
123 // whose attributes are not
124 // currently not being shown in UI. So ignoring this
128 // Refresh the table viewers which will display
129 // the updated values
130 if (null != attTblViewer) {
131 if (notificationType == ModelChangeNotificationType.ATTRIBUTE_ADDED
132 || notificationType == ModelChangeNotificationType.ATTRIBUTE_REMOVED) {
133 updateViewer(getData());
134 } else if (notificationType == ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL) {
135 attTblViewer.setInput(null);
136 } else if (notificationType == ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED) {
137 if (null != valueChangeSet) {
139 valueChangeSet.toArray(), null);
148 automationUIListener = new IAutomationUIListener() {
151 public void onResourceAutomationStart(final String resourceURI) {
152 Display.getDefault().asyncExec(new Runnable() {
156 if (null == resourceURI) {
159 SimulatorResource resource = resourceManager
160 .getCurrentResourceInSelection();
161 if (null == resource) {
164 String uri = resource.getResourceURI();
165 // Checking whether attributes view is currently
166 // displaying the attributes of the
167 // resource whose automation has just started
168 if (null != uri && uri.equals(resourceURI)) {
170 tbl = attTblViewer.getTable();
171 if (!tbl.isDisposed()) {
172 attTblViewer.refresh();
174 // Disabling the table to prevent interactions
175 // during the automation
176 tbl.setEnabled(false);
185 public void onAutomationComplete(final String resourceURI,
186 final String attName) {
187 // This method notifies the completion of attribute level
189 Display.getDefault().asyncExec(new Runnable() {
193 if (null == resourceURI) {
196 // Check if the given resourceURI is the uri of the
197 // resource whose attributes are currently being
198 // displayed by this view.
199 SimulatorResource resource = resourceManager
200 .getCurrentResourceInSelection();
201 if (null == resource) {
204 String uri = resource.getResourceURI();
205 if (null == uri || !uri.equals(resourceURI)) {
209 tbl = attTblViewer.getTable();
210 if (!tbl.isDisposed()) {
211 if (null != attName) {
212 // Attribute level automation has stopped
213 LocalResourceAttribute att = resourceManager
214 .getAttributeByResourceURI(resourceURI,
219 attTblViewer.update(att, null);
222 // Resource level automation has stopped
223 // Enabling the table which was disabled at the
224 // beginning of automation
225 tbl.setEnabled(true);
226 attTblViewer.refresh();
236 public void createPartControl(Composite parent) {
237 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
239 parent.setLayout(new GridLayout());
240 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
241 parent.setLayoutData(gd);
243 Group attGroup = new Group(parent, SWT.NONE);
244 attGroup.setLayout(new GridLayout());
245 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
246 attGroup.setLayoutData(gd);
247 attGroup.setText("Attributes");
248 attGroup.setBackground(color);
250 attTblViewer = new TableViewer(attGroup, SWT.SINGLE | SWT.H_SCROLL
251 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
253 createAttributeColumns(attTblViewer);
255 // make lines and header visible
256 Table table = attTblViewer.getTable();
257 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
258 table.setHeaderVisible(true);
259 table.setLinesVisible(true);
261 attTblViewer.setContentProvider(new AttributeContentProvider());
263 addManagerListeners();
265 // Check whether there is any resource selected already
266 List<LocalResourceAttribute> propertyList = getData();
267 if (null != propertyList) {
268 updateViewer(propertyList);
272 public void createAttributeColumns(TableViewer tableViewer) {
274 attributeEditor = new AttributeEditingSupport();
276 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
277 attName.getColumn().setWidth(attTblColWidth[0]);
278 attName.getColumn().setText(attTblHeaders[0]);
279 attName.setLabelProvider(new StyledCellLabelProvider() {
281 public void update(ViewerCell cell) {
282 Object element = cell.getElement();
283 if (element instanceof LocalResourceAttribute) {
284 LocalResourceAttribute attribute = (LocalResourceAttribute) element;
285 if (null != attribute) {
286 cell.setText(attribute.getAttributeName());
292 TableViewerColumn attValue = new TableViewerColumn(tableViewer,
294 attValue.getColumn().setWidth(attTblColWidth[1]);
295 attValue.getColumn().setText(attTblHeaders[1]);
296 attValue.setLabelProvider(new ColumnLabelProvider() {
298 public String getText(Object element) {
299 if (element instanceof LocalResourceAttribute) {
300 LocalResourceAttribute attribute = (LocalResourceAttribute) element;
301 if (null != attribute) {
302 Object val = attribute.getAttributeValue();
304 return String.valueOf(val);
311 attValue.setEditingSupport(attributeEditor
312 .createAttributeValueEditor(attTblViewer));
314 TableViewerColumn automation = new TableViewerColumn(tableViewer,
316 automation.getColumn().setWidth(attTblColWidth[2]);
317 automation.getColumn().setText(attTblHeaders[2]);
318 automation.setLabelProvider(new ColumnLabelProvider() {
320 public String getText(Object element) {
321 LocalResourceAttribute att = (LocalResourceAttribute) element;
322 if (!resourceManager.isAttHasRangeOrAllowedValues(att)) {
323 System.out.println("No range or allowed values");
326 if (att.isAutomationInProgress()) {
327 return Constants.ENABLED;
329 return Constants.DISABLED;
333 public Image getImage(Object element) {
334 LocalResourceAttribute att = (LocalResourceAttribute) element;
335 if (!resourceManager.isAttHasRangeOrAllowedValues(att)) {
336 System.out.println("No range or allowed values");
339 if (att.isAutomationInProgress()) {
340 return Activator.getDefault().getImageRegistry()
341 .get(Constants.CHECKED);
343 return Activator.getDefault().getImageRegistry()
344 .get(Constants.UNCHECKED);
348 automation.setEditingSupport(attributeEditor
349 .createAutomationEditor(attTblViewer));
352 private void addManagerListeners() {
354 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
356 .addResourceModelChangedUIListener(resourceModelChangedUIListener);
357 resourceManager.addAutomationUIListener(automationUIListener);
360 private List<LocalResourceAttribute> getData() {
361 SimulatorResource resourceInSelection = resourceManager
362 .getCurrentResourceInSelection();
363 if (null != resourceInSelection) {
364 List<LocalResourceAttribute> attList = resourceManager
365 .getAttributes(resourceInSelection);
372 private void updateViewer(List<LocalResourceAttribute> attList) {
374 if (null != attList) {
375 tbl = attTblViewer.getTable();
376 if (null != tbl && !tbl.isDisposed()) {
377 tbl.setLinesVisible(true);
378 attTblViewer.setInput(attList.toArray());
381 // Clear the attributes table viewer
382 if (null != attTblViewer) {
383 tbl = attTblViewer.getTable();
384 if (null != tbl && !tbl.isDisposed()) {
385 // tbl.deselectAll();
387 tbl.setLinesVisible(false);
393 class AttributeContentProvider implements IStructuredContentProvider {
396 public void dispose() {
400 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
404 public Object[] getElements(Object element) {
405 return (Object[]) element;
411 public void dispose() {
412 // Unregister the selection listener
413 if (null != resourceSelectionChangedListener) {
415 .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
418 // Unregister the model change listener
419 if (null != resourceModelChangedUIListener) {
421 .removeResourceModelChangedUIListener(resourceModelChangedUIListener);
424 // Unregister the automation complete listener
425 if (null != automationUIListener) {
426 resourceManager.removeAutomationUIListener(automationUIListener);
433 public void setFocus() {