1 package oic.simulator.serviceprovider.view;
6 import oic.simulator.serviceprovider.Activator;
7 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
8 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
9 import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
10 import oic.simulator.serviceprovider.manager.ResourceManager;
11 import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
12 import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
13 import oic.simulator.serviceprovider.resource.SimulatorResource;
14 import oic.simulator.serviceprovider.utils.Constants;
16 import org.eclipse.jface.viewers.ColumnLabelProvider;
17 import org.eclipse.jface.viewers.IStructuredContentProvider;
18 import org.eclipse.jface.viewers.StyledCellLabelProvider;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.TableViewerColumn;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.jface.viewers.ViewerCell;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Group;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.ui.part.ViewPart;
34 public class AttributeView extends ViewPart {
36 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.attribute";
38 private TableViewer attTblViewer;
40 private AttributeEditingSupport attributeEditor;
42 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
43 private IResourceModelChangedUIListener resourceModelChangedUIListener;
44 private IAutomationUIListener automationUIListener;
46 private final String[] attTblHeaders = { "Name",
47 "Value", "Automation" };
48 private final Integer[] attTblColWidth = { 150, 190,
51 private ResourceManager resourceManager;
53 private static final Image CHECKED = Activator
58 private static final Image UNCHECKED = Activator
61 "icons/unchecked.gif");
63 public AttributeView() {
65 resourceManager = Activator.getDefault().getResourceManager();
67 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
70 public void onResourceSelectionChange() {
71 Display.getDefault().asyncExec(new Runnable() {
75 if (null != attTblViewer) {
76 updateViewer(checkSelection());
77 SimulatorResource resource = resourceManager
78 .getCurrentResourceInSelection();
79 Table tbl = attTblViewer.getTable();
80 if (!tbl.isDisposed()) {
83 .isResourceAutomationInProgress()) {
84 tbl.setEnabled(false);
95 resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
98 public void onResourceModelChange(
99 final ModelChangeNotificationType notificationType,
100 final String resourceURI,
101 final Set<LocalResourceAttribute> changeSet) {
102 Display.getDefault().asyncExec(new Runnable() {
105 // Handle the notification only if it is for the current
106 // resource in selection
107 SimulatorResource resource = resourceManager
108 .getCurrentResourceInSelection();
109 if (null == resource) {
112 if (!resourceURI.equals(resource.getResourceURI())) {
115 // Refresh the table viewers which will display
116 // the updated values
117 if (null != attTblViewer) {
118 if (notificationType == ModelChangeNotificationType.ATTRIBUTE_ADDED
119 || notificationType == ModelChangeNotificationType.ATTRIBUTE_REMOVED) {
120 attTblViewer.refresh();
121 } else if (notificationType == ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED) {
122 if (null != changeSet) {
123 attTblViewer.update(changeSet.toArray(),
133 automationUIListener = new IAutomationUIListener() {
136 public void onResourceAutomationStart(final String resourceURI) {
137 Display.getDefault().asyncExec(new Runnable() {
141 if (null == resourceURI) {
144 SimulatorResource resource = resourceManager
145 .getCurrentResourceInSelection();
146 if (null == resource) {
149 String uri = resource.getResourceURI();
150 // Checking whether attributes view is currently
151 // displaying the attributes of the
152 // resource whose automation has just started
153 if (null != uri && uri.equals(resourceURI)) {
155 tbl = attTblViewer.getTable();
156 if (!tbl.isDisposed()) {
157 attTblViewer.refresh();
159 // Disabling the table to prevent interactions
160 // during the automation
161 tbl.setEnabled(false);
170 public void onAutomationComplete(final String resourceURI,
171 final String attName) {
172 // This method notifies the completion of attribute level
174 Display.getDefault().asyncExec(new Runnable() {
178 System.out.println("onAutomationcomplete Impl: uri:"
179 + resourceURI + ",attname:" + attName);
180 if (null == resourceURI) {
183 // Check if the given resourceURI is the uri of the
184 // resource whose attributes are currently being
185 // displayed by this view.
186 SimulatorResource resource = resourceManager
187 .getCurrentResourceInSelection();
188 if (null == resource) {
191 String uri = resource.getResourceURI();
192 if (null == uri || !uri.equals(resourceURI)) {
196 tbl = attTblViewer.getTable();
197 if (!tbl.isDisposed()) {
198 if (null != attName) {
199 // Attribute level automation has stopped
200 LocalResourceAttribute att = resourceManager
201 .getAttributeByResourceURI(resourceURI,
203 System.out.println(att == null);
207 attTblViewer.update(att, null);
210 // Resource level automation has stopped
211 // Enabling the table which was disabled at the
212 // beginning of automation
213 tbl.setEnabled(true);
214 attTblViewer.refresh();
224 public void createPartControl(Composite parent) {
225 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
227 parent.setLayout(new GridLayout());
228 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
229 parent.setLayoutData(gd);
231 Group attGroup = new Group(parent, SWT.NONE);
232 attGroup.setLayout(new GridLayout());
233 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
234 attGroup.setLayoutData(gd);
235 attGroup.setText("Attributes");
236 attGroup.setBackground(color);
238 attTblViewer = new TableViewer(attGroup, SWT.SINGLE | SWT.H_SCROLL
239 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
241 createAttributeColumns(attTblViewer);
243 // make lines and header visible
244 Table table = attTblViewer.getTable();
245 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
246 table.setHeaderVisible(true);
247 table.setLinesVisible(true);
249 attTblViewer.setContentProvider(new AttributeContentProvider());
251 addManagerListeners();
253 // Check whether there is any resource selected already
254 List<LocalResourceAttribute> propertyList = checkSelection();
255 if (null != propertyList) {
256 updateViewer(propertyList);
260 public void createAttributeColumns(TableViewer tableViewer) {
262 attributeEditor = new AttributeEditingSupport();
264 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
265 attName.getColumn().setWidth(attTblColWidth[0]);
266 attName.getColumn().setText(attTblHeaders[0]);
267 attName.setLabelProvider(new StyledCellLabelProvider() {
269 public void update(ViewerCell cell) {
270 Object element = cell.getElement();
271 if (element instanceof LocalResourceAttribute) {
272 LocalResourceAttribute attribute = (LocalResourceAttribute) element;
273 if (null != attribute) {
274 cell.setText(attribute.getAttributeName());
280 TableViewerColumn attValue = new TableViewerColumn(tableViewer,
282 attValue.getColumn().setWidth(attTblColWidth[1]);
283 attValue.getColumn().setText(attTblHeaders[1]);
284 attValue.setLabelProvider(new ColumnLabelProvider() {
286 public String getText(Object element) {
287 if (element instanceof LocalResourceAttribute) {
288 LocalResourceAttribute attribute = (LocalResourceAttribute) element;
289 if (null != attribute) {
290 Object val = attribute.getAttributeValue();
292 return String.valueOf(val);
299 attValue.setEditingSupport(attributeEditor
300 .createAttributeValueEditor(attTblViewer));
302 TableViewerColumn automation = new TableViewerColumn(tableViewer,
304 automation.getColumn().setWidth(attTblColWidth[2]);
305 automation.getColumn().setText(attTblHeaders[2]);
306 automation.setLabelProvider(new ColumnLabelProvider() {
308 public String getText(Object element) {
309 LocalResourceAttribute att = (LocalResourceAttribute) element;
310 if (att.isAutomationInProgress()) {
311 return Constants.ENABLED;
313 return Constants.DISABLED;
317 public Image getImage(Object element) {
318 LocalResourceAttribute att = (LocalResourceAttribute) element;
319 if (att.isAutomationInProgress()) {
326 automation.setEditingSupport(attributeEditor
327 .createAutomationEditor(attTblViewer));
330 private void addManagerListeners() {
332 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
334 .addResourceModelChangedUIListener(resourceModelChangedUIListener);
335 resourceManager.addAutomationUIListener(automationUIListener);
338 private List<LocalResourceAttribute> checkSelection() {
339 SimulatorResource resourceInSelection = resourceManager
340 .getCurrentResourceInSelection();
341 if (null != resourceInSelection) {
342 List<LocalResourceAttribute> attList = resourceManager
343 .getAttributes(resourceInSelection);
350 private void updateViewer(List<LocalResourceAttribute> attList) {
352 if (null != attList) {
353 tbl = attTblViewer.getTable();
354 if (null != tbl && !tbl.isDisposed()) {
355 tbl.setLinesVisible(true);
356 attTblViewer.setInput(attList.toArray());
359 // Clear the attributes table viewer
360 if (null != attTblViewer) {
361 tbl = attTblViewer.getTable();
362 if (null != tbl && !tbl.isDisposed()) {
363 // tbl.deselectAll();
365 tbl.setLinesVisible(false);
371 class AttributeContentProvider implements IStructuredContentProvider {
374 public void dispose() {
378 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
382 public Object[] getElements(Object element) {
383 return (Object[]) element;
389 public void dispose() {
390 // Unregister the selection listener
391 if (null != resourceSelectionChangedListener) {
393 .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
396 // Unregister the model change listener
397 if (null != resourceModelChangedUIListener) {
399 .removeResourceModelChangedUIListener(resourceModelChangedUIListener);
402 // Unregister the automation complete listener
403 if (null != automationUIListener) {
404 resourceManager.removeAutomationUIListener(automationUIListener);
411 public void setFocus() {