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;
21 import oic.simulator.serviceprovider.Activator;
22 import oic.simulator.serviceprovider.listener.ISelectionChangedListener;
23 import oic.simulator.serviceprovider.manager.ResourceManager;
24 import oic.simulator.serviceprovider.manager.UiListenerHandler;
25 import oic.simulator.serviceprovider.model.Device;
26 import oic.simulator.serviceprovider.model.MetaProperty;
27 import oic.simulator.serviceprovider.model.Resource;
28 import oic.simulator.serviceprovider.utils.Constants;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.viewers.CellEditor;
32 import org.eclipse.jface.viewers.ColumnLabelProvider;
33 import org.eclipse.jface.viewers.EditingSupport;
34 import org.eclipse.jface.viewers.IStructuredContentProvider;
35 import org.eclipse.jface.viewers.StyledCellLabelProvider;
36 import org.eclipse.jface.viewers.TableViewer;
37 import org.eclipse.jface.viewers.TableViewerColumn;
38 import org.eclipse.jface.viewers.TextCellEditor;
39 import org.eclipse.jface.viewers.Viewer;
40 import org.eclipse.jface.viewers.ViewerCell;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.events.SelectionAdapter;
43 import org.eclipse.swt.events.SelectionEvent;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.layout.GridLayout;
46 import org.eclipse.swt.widgets.Button;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Display;
49 import org.eclipse.swt.widgets.Table;
50 import org.eclipse.ui.part.ViewPart;
51 import org.oic.simulator.SimulatorException;
54 * This class manages and shows the meta properties view in the perspective.
56 public class MetaPropertiesView extends ViewPart {
58 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.metaproperties";
60 private TableViewer tableViewer;
62 private final String[] columnHeaders = { "Property", "Value" };
64 private final Integer[] columnWidth = { 150, 150 };
66 private ISelectionChangedListener resourceSelectionChangedListener;
68 private ResourceManager resourceManagerRef;
70 private List<MetaProperty> properties;
72 private boolean enable_edit;
73 private Button editBtn;
74 private Button cancelBtn;
76 public MetaPropertiesView() {
78 resourceManagerRef = Activator.getDefault().getResourceManager();
80 resourceSelectionChangedListener = new ISelectionChangedListener() {
83 public void onResourceSelectionChange(final Resource resource) {
84 Display.getDefault().asyncExec(new Runnable() {
88 if (null != tableViewer) {
89 properties = getData(resource);
90 updateViewer(properties);
92 updateEditControls(resource);
98 public void onDeviceSelectionChange(final Device dev) {
99 Display.getDefault().asyncExec(new Runnable() {
103 if (null != tableViewer) {
104 properties = getData(dev);
105 updateViewer(properties);
107 updateEditControls(dev);
115 public void createPartControl(final Composite parent) {
116 parent.setLayout(new GridLayout(2, false));
118 tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
119 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
121 createColumns(tableViewer);
123 // Make lines and header visible
124 final Table table = tableViewer.getTable();
125 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
126 gd.horizontalSpan = 2;
127 table.setLayoutData(gd);
128 table.setHeaderVisible(true);
129 table.setLinesVisible(true);
131 tableViewer.setContentProvider(new PropertycontentProvider());
133 editBtn = new Button(parent, SWT.PUSH);
134 editBtn.setText("Edit");
137 editBtn.setLayoutData(gd);
138 editBtn.addSelectionListener(new SelectionAdapter() {
140 public void widgetSelected(SelectionEvent e) {
141 if (editBtn.getText().equals("Edit")) {
142 cancelBtn.setEnabled(true);
143 editBtn.setText("Save");
146 boolean result = false;
147 Resource resourceInSelection = resourceManagerRef
148 .getCurrentResourceInSelection();
149 if (null != resourceInSelection) {
152 result = resourceManagerRef.isPropertyValueInvalid(
153 resourceInSelection, properties,
154 Constants.RESOURCE_URI);
156 MessageDialog.openError(parent.getShell(),
157 "Invalid Resource URI.",
158 Constants.INVALID_URI_MESSAGE);
162 result = resourceManagerRef.isPropertyValueInvalid(
163 resourceInSelection, properties,
164 Constants.RESOURCE_NAME);
166 MessageDialog.openError(parent.getShell(),
168 "Resource Name is invalid.");
172 boolean update = false;
173 boolean uriChange = false;
174 boolean nameChange = false;
175 if (resourceManagerRef.isPropValueChanged(
176 resourceInSelection, properties,
177 Constants.RESOURCE_NAME)) {
181 if (resourceManagerRef.isPropValueChanged(
182 resourceInSelection, properties,
183 Constants.RESOURCE_URI)) {
184 // Check whether the new URI is unique.
185 if (!resourceManagerRef.isUriUnique(properties)) {
186 MessageDialog.openError(parent.getShell(),
187 "Resource URI in use",
188 "Resource URI is in use. Please try a different URI.");
195 if (resourceManagerRef
196 .isResourceStarted(resourceInSelection)) {
197 update = MessageDialog.openQuestion(
198 parent.getShell(), "Save Details",
199 "Resource will be restarted to take the changes."
200 + " Do you want to continue?");
210 .getResourceManager()
211 .updateResourceProperties(
213 .getCurrentResourceInSelection(),
214 properties, uriChange,
216 } catch (SimulatorException ex) {
220 MessageDialog.openInformation(
221 parent.getShell(), "Operation status",
222 "Resource properties updated.");
224 MessageDialog.openInformation(
225 parent.getShell(), "Operation status",
226 "Failed to update the resource properties.");
228 // Reset the old property values.
229 properties = getData(resourceManagerRef
230 .getCurrentResourceInSelection());
231 updateViewer(properties);
235 Device dev = resourceManagerRef
236 .getCurrentDeviceInSelection();
239 result = resourceManagerRef.isPropertyValueInvalid(dev,
240 properties, Constants.DEVICE_NAME);
242 MessageDialog.openError(parent.getShell(),
243 "Invalid Input", "Device Name is invalid.");
247 if (resourceManagerRef.isPropValueChanged(dev,
248 properties, Constants.DEVICE_NAME)) {
249 resourceManagerRef.updateDeviceProperties(dev,
254 cancelBtn.setEnabled(false);
255 editBtn.setText("Edit");
261 cancelBtn = new Button(parent, SWT.PUSH);
262 cancelBtn.setText("Cancel");
263 cancelBtn.setEnabled(false);
266 cancelBtn.setLayoutData(gd);
267 cancelBtn.addSelectionListener(new SelectionAdapter() {
269 public void widgetSelected(SelectionEvent e) {
270 Resource res = resourceManagerRef
271 .getCurrentResourceInSelection();
273 properties = getData(res);
275 Device dev = resourceManagerRef
276 .getCurrentDeviceInSelection();
278 properties = getData(dev);
281 updateViewer(properties);
283 cancelBtn.setEnabled(false);
284 editBtn.setText("Edit");
289 addManagerListeners();
291 // Check whether there is any resource selected already
292 Resource resource = resourceManagerRef.getCurrentResourceInSelection();
293 properties = getData(resource);
294 if (null != properties) {
295 updateViewer(properties);
297 updateEditControls(resource);
300 private void updateEditControls(Object obj) {
301 if (!editBtn.isDisposed() && !cancelBtn.isDisposed()) {
303 if (editBtn.getText().equals("Save")) {
304 editBtn.setText("Edit");
309 editBtn.setEnabled(false);
311 editBtn.setEnabled(true);
313 cancelBtn.setEnabled(false);
317 private List<MetaProperty> getData(Resource resource) {
318 if (null != resource) {
319 List<MetaProperty> metaPropertyList = resourceManagerRef
320 .getMetaProperties(resource);
321 return metaPropertyList;
327 private List<MetaProperty> getData(Device dev) {
329 List<MetaProperty> metaPropertyList = resourceManagerRef
330 .getMetaProperties(dev);
331 return metaPropertyList;
337 private void updateViewer(List<MetaProperty> metaPropertyList) {
338 if (null != tableViewer) {
339 Table tbl = tableViewer.getTable();
340 if (null != metaPropertyList) {
341 tableViewer.setInput(metaPropertyList.toArray());
342 if (!tbl.isDisposed()) {
343 tbl.setLinesVisible(true);
346 if (!tbl.isDisposed()) {
348 tbl.setLinesVisible(false);
354 public void createColumns(TableViewer tableViewer) {
355 TableViewerColumn propName = new TableViewerColumn(tableViewer,
357 propName.getColumn().setWidth(columnWidth[0]);
358 propName.getColumn().setText(columnHeaders[0]);
359 propName.setLabelProvider(new StyledCellLabelProvider() {
361 public void update(ViewerCell cell) {
362 MetaProperty prop = (MetaProperty) cell.getElement();
363 cell.setText(prop.getPropName());
368 TableViewerColumn propValue = new TableViewerColumn(tableViewer,
370 propValue.getColumn().setWidth(columnWidth[1]);
371 propValue.getColumn().setText(columnHeaders[1]);
372 propValue.setLabelProvider(new ColumnLabelProvider() {
374 public String getText(Object element) {
375 MetaProperty prop = (MetaProperty) element;
377 return prop.getPropValue();
383 propValue.setEditingSupport(new PropValueEditor(tableViewer));
386 private void addManagerListeners() {
387 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
388 resourceSelectionChangedListener);
391 class PropertycontentProvider implements IStructuredContentProvider {
394 public void dispose() {
398 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
402 public Object[] getElements(Object element) {
403 return (Object[]) element;
408 public void dispose() {
409 // Unregister the listener
410 if (null != resourceSelectionChangedListener) {
411 UiListenerHandler.getInstance()
412 .removeResourceSelectionChangedUIListener(
413 resourceSelectionChangedListener);
418 class PropValueEditor extends EditingSupport {
420 private final TableViewer viewer;
422 public PropValueEditor(TableViewer viewer) {
424 this.viewer = viewer;
428 protected boolean canEdit(Object element) {
433 protected CellEditor getCellEditor(Object element) {
437 // Disabling edit for resource type
438 String propName = ((MetaProperty) element).getPropName();
439 if (null != propName && propName.equals(Constants.RESOURCE_TYPE)) {
442 CellEditor editor = new TextCellEditor(viewer.getTable());
447 protected Object getValue(Object element) {
448 return ((MetaProperty) element).getPropValue();
452 protected void setValue(Object element, Object value) {
453 MetaProperty prop = (MetaProperty) element;
454 prop.setPropValue(String.valueOf(value));
455 viewer.update(element, null);
461 public void setFocus() {