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.ISelectionChangedUIListener;
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 ISelectionChangedUIListener 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 ISelectionChangedUIListener() {
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 Input", "Resource URI is invalid.");
161 result = resourceManagerRef.isPropertyValueInvalid(
162 resourceInSelection, properties,
163 Constants.RESOURCE_NAME);
165 MessageDialog.openError(parent.getShell(),
167 "Resource Name is invalid.");
171 boolean update = false;
172 boolean uriChange = false;
173 boolean nameChange = false;
174 if (resourceManagerRef.isPropValueChanged(
175 resourceInSelection, properties,
176 Constants.RESOURCE_NAME)) {
180 if (resourceManagerRef.isPropValueChanged(
181 resourceInSelection, properties,
182 Constants.RESOURCE_URI)) {
183 // Check whether the new URI is unique.
184 if (!resourceManagerRef.isUriUnique(properties)) {
185 MessageDialog.openError(parent.getShell(),
186 "Resource URI in use",
187 "Resource URI is in use. Please try a different URI.");
194 if (resourceManagerRef
195 .isResourceStarted(resourceInSelection)) {
196 update = MessageDialog.openQuestion(
197 parent.getShell(), "Save Details",
198 "Resource will be restarted to take the changes."
199 + " Do you want to continue?");
209 .getResourceManager()
210 .updateResourceProperties(
212 .getCurrentResourceInSelection(),
213 properties, uriChange,
215 } catch (SimulatorException ex) {
219 MessageDialog.openInformation(
220 parent.getShell(), "Operation status",
221 "Resource properties updated.");
223 MessageDialog.openInformation(
224 parent.getShell(), "Operation status",
225 "Failed to update the resource properties.");
227 // Reset the old property values.
228 properties = getData(resourceManagerRef
229 .getCurrentResourceInSelection());
230 updateViewer(properties);
234 Device dev = resourceManagerRef
235 .getCurrentDeviceInSelection();
238 result = resourceManagerRef.isPropertyValueInvalid(dev,
239 properties, Constants.DEVICE_NAME);
241 MessageDialog.openError(parent.getShell(),
242 "Invalid Input", "Device Name is invalid.");
246 if (resourceManagerRef.isPropValueChanged(dev,
247 properties, Constants.DEVICE_NAME)) {
248 resourceManagerRef.updateDeviceProperties(dev,
253 cancelBtn.setEnabled(false);
254 editBtn.setText("Edit");
260 cancelBtn = new Button(parent, SWT.PUSH);
261 cancelBtn.setText("Cancel");
262 cancelBtn.setEnabled(false);
265 cancelBtn.setLayoutData(gd);
266 cancelBtn.addSelectionListener(new SelectionAdapter() {
268 public void widgetSelected(SelectionEvent e) {
269 Resource res = resourceManagerRef
270 .getCurrentResourceInSelection();
272 properties = getData(res);
274 Device dev = resourceManagerRef
275 .getCurrentDeviceInSelection();
277 properties = getData(dev);
280 updateViewer(properties);
282 cancelBtn.setEnabled(false);
283 editBtn.setText("Edit");
288 addManagerListeners();
290 // Check whether there is any resource selected already
291 Resource resource = resourceManagerRef.getCurrentResourceInSelection();
292 properties = getData(resource);
293 if (null != properties) {
294 updateViewer(properties);
296 updateEditControls(resource);
299 private void updateEditControls(Object obj) {
300 if (!editBtn.isDisposed() && !cancelBtn.isDisposed()) {
302 if (editBtn.getText().equals("Save")) {
303 editBtn.setText("Edit");
308 editBtn.setEnabled(false);
310 editBtn.setEnabled(true);
312 cancelBtn.setEnabled(false);
316 private List<MetaProperty> getData(Resource resource) {
317 if (null != resource) {
318 List<MetaProperty> metaPropertyList = resourceManagerRef
319 .getMetaProperties(resource);
320 return metaPropertyList;
326 private List<MetaProperty> getData(Device dev) {
328 List<MetaProperty> metaPropertyList = resourceManagerRef
329 .getMetaProperties(dev);
330 return metaPropertyList;
336 private void updateViewer(List<MetaProperty> metaPropertyList) {
337 if (null != tableViewer) {
338 Table tbl = tableViewer.getTable();
339 if (null != metaPropertyList) {
340 tableViewer.setInput(metaPropertyList.toArray());
341 if (!tbl.isDisposed()) {
342 tbl.setLinesVisible(true);
345 if (!tbl.isDisposed()) {
347 tbl.setLinesVisible(false);
353 public void createColumns(TableViewer tableViewer) {
354 TableViewerColumn propName = new TableViewerColumn(tableViewer,
356 propName.getColumn().setWidth(columnWidth[0]);
357 propName.getColumn().setText(columnHeaders[0]);
358 propName.setLabelProvider(new StyledCellLabelProvider() {
360 public void update(ViewerCell cell) {
361 MetaProperty prop = (MetaProperty) cell.getElement();
362 cell.setText(prop.getPropName());
367 TableViewerColumn propValue = new TableViewerColumn(tableViewer,
369 propValue.getColumn().setWidth(columnWidth[1]);
370 propValue.getColumn().setText(columnHeaders[1]);
371 propValue.setLabelProvider(new ColumnLabelProvider() {
373 public String getText(Object element) {
374 MetaProperty prop = (MetaProperty) element;
376 return prop.getPropValue();
382 propValue.setEditingSupport(new PropValueEditor(tableViewer));
385 private void addManagerListeners() {
386 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
387 resourceSelectionChangedListener);
390 class PropertycontentProvider implements IStructuredContentProvider {
393 public void dispose() {
397 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
401 public Object[] getElements(Object element) {
402 return (Object[]) element;
407 public void dispose() {
408 // Unregister the listener
409 if (null != resourceSelectionChangedListener) {
410 UiListenerHandler.getInstance()
411 .removeResourceSelectionChangedUIListener(
412 resourceSelectionChangedListener);
417 class PropValueEditor extends EditingSupport {
419 private final TableViewer viewer;
421 public PropValueEditor(TableViewer viewer) {
423 this.viewer = viewer;
427 protected boolean canEdit(Object element) {
432 protected CellEditor getCellEditor(Object element) {
436 // Disabling edit for resource type
437 String propName = ((MetaProperty) element).getPropName();
438 if (null != propName && propName.equals(Constants.RESOURCE_TYPE)) {
441 CellEditor editor = new TextCellEditor(viewer.getTable());
446 protected Object getValue(Object element) {
447 return ((MetaProperty) element).getPropValue();
451 protected void setValue(Object element, Object value) {
452 MetaProperty prop = (MetaProperty) element;
453 prop.setPropValue(String.valueOf(value));
454 viewer.update(element, null);
460 public void setFocus() {