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 org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.CellEditor;
21 import org.eclipse.jface.viewers.EditingSupport;
22 import org.eclipse.jface.viewers.IStructuredContentProvider;
23 import org.eclipse.jface.viewers.StyledCellLabelProvider;
24 import org.eclipse.jface.viewers.TableViewer;
25 import org.eclipse.jface.viewers.TableViewerColumn;
26 import org.eclipse.jface.viewers.TextCellEditor;
27 import org.eclipse.jface.viewers.Viewer;
28 import org.eclipse.jface.viewers.ViewerCell;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.ModifyEvent;
32 import org.eclipse.swt.events.ModifyListener;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Table;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.part.ViewPart;
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.List;
50 import org.oic.simulator.SimulatorException;
52 import oic.simulator.serviceprovider.Activator;
53 import oic.simulator.serviceprovider.listener.ISelectionChangedListener;
54 import oic.simulator.serviceprovider.manager.ResourceManager;
55 import oic.simulator.serviceprovider.manager.UiListenerHandler;
56 import oic.simulator.serviceprovider.model.MetaProperty;
57 import oic.simulator.serviceprovider.model.Resource;
58 import oic.simulator.serviceprovider.model.SingleResource;
59 import oic.simulator.serviceprovider.utils.Constants;
60 import oic.simulator.serviceprovider.utils.Utility;
61 import oic.simulator.serviceprovider.view.dialogs.UpdateResourceInterfaceDialog;
64 * This class manages and shows the meta properties view in the perspective.
66 public class MetaPropertiesView extends ViewPart {
68 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.metaproperties";
70 private TableViewer tableViewer;
72 private final String[] columnHeaders = { "Property", "Value" };
74 private final Integer[] columnWidth = { 150, 150 };
76 private ISelectionChangedListener resourceSelectionChangedListener;
78 private ResourceManager resourceManagerRef;
80 private Set<String> updatedIfSet;
82 private List<MetaProperty> properties;
84 private boolean enable_edit;
85 private Button editBtn;
86 private Button cancelBtn;
88 private Map<String, String> ifTypes;
90 public MetaPropertiesView() {
92 resourceManagerRef = Activator.getDefault().getResourceManager();
94 resourceSelectionChangedListener = new ISelectionChangedListener() {
97 public void onResourceSelectionChange(final Resource resource) {
98 Display.getDefault().asyncExec(new Runnable() {
102 if (null != tableViewer) {
103 properties = getData(resource);
104 updateViewer(properties);
106 updateEditControls(resource);
114 public void createPartControl(final Composite parent) {
115 parent.setLayout(new GridLayout(2, false));
117 tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
118 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
120 createColumns(tableViewer);
122 // Make lines and header visible
123 final Table table = tableViewer.getTable();
124 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
125 gd.horizontalSpan = 2;
126 table.setLayoutData(gd);
127 table.setHeaderVisible(true);
128 table.setLinesVisible(true);
130 tableViewer.setContentProvider(new PropertycontentProvider());
132 editBtn = new Button(parent, SWT.PUSH);
133 editBtn.setText("Edit");
136 editBtn.setLayoutData(gd);
137 editBtn.addSelectionListener(new SelectionAdapter() {
139 public void widgetSelected(SelectionEvent e) {
140 if (editBtn.getText().equals("Edit")) {
141 cancelBtn.setEnabled(true);
142 editBtn.setText("Save");
145 Resource resource = resourceManagerRef
146 .getCurrentResourceInSelection();
147 if (null != resource) {
148 updatedIfSet = resource.getResourceInterfaces();
151 Resource resourceInSelection = resourceManagerRef
152 .getCurrentResourceInSelection();
153 if (null != resourceInSelection) {
156 boolean result = resourceManagerRef
157 .isPropertyValueInvalid(resourceInSelection,
158 properties, Constants.RESOURCE_URI);
160 MessageDialog.openError(parent.getShell(),
161 "Invalid Resource URI.",
162 Constants.INVALID_URI_MESSAGE);
166 result = resourceManagerRef.isPropertyValueInvalid(
167 resourceInSelection, properties,
168 Constants.RESOURCE_NAME);
170 MessageDialog.openError(parent.getShell(),
172 "Resource Name is invalid.");
176 result = resourceManagerRef.isPropertyValueInvalid(
177 resourceInSelection, properties,
178 Constants.RESOURCE_TYPE);
180 MessageDialog.openError(parent.getShell(),
181 "Invalid Resource Type.",
182 Constants.INVALID_RESOURCE_TYPE_MESSAGE);
186 boolean update = false;
187 boolean uriChange = false;
188 boolean typeChange = false;
189 boolean nameChange = false;
190 boolean interfaceChange = false;
192 if (resourceManagerRef.isPropValueChanged(
193 resourceInSelection, properties,
194 Constants.RESOURCE_NAME)) {
199 if (resourceManagerRef.isPropValueChanged(
200 resourceInSelection, properties,
201 Constants.RESOURCE_URI)) {
202 // Check whether the new URI is unique.
203 if (!resourceManagerRef.isUriUnique(properties)) {
204 MessageDialog.openError(parent.getShell(),
205 "Resource URI in use",
206 "Resource URI is in use. Please try a different URI.");
214 if (resourceManagerRef.isPropValueChanged(
215 resourceInSelection, properties,
216 Constants.RESOURCE_TYPE)) {
220 // Checking whether any changes made in resource
222 // comparing the current interface set and updated
224 Set<String> curIfSet = resourceInSelection
225 .getResourceInterfaces();
226 // Adding default interface to local set if removed.
227 updatedIfSet.add(Constants.BASELINE_INTERFACE);
228 if (null != curIfSet && null != updatedIfSet) {
229 if (curIfSet.size() != updatedIfSet.size()) {
231 interfaceChange = true;
233 Iterator<String> itr = updatedIfSet.iterator();
234 while (itr.hasNext()) {
235 if (!curIfSet.contains(itr.next())) {
237 interfaceChange = true;
244 if (uriChange || typeChange || interfaceChange) {
245 if (resourceManagerRef
246 .isResourceStarted(resourceInSelection)) {
247 update = MessageDialog.openQuestion(
250 "Resource will be restarted to take the changes."
251 + " Do you want to continue?");
258 if (uriChange || nameChange || typeChange)
261 .getResourceManager()
262 .updateResourceProperties(
264 .getCurrentResourceInSelection(),
265 properties, uriChange,
266 nameChange, typeChange);
270 .getResourceManager()
271 .updateResourceInterfaces(
275 } catch (SimulatorException ex) {
279 MessageDialog.openInformation(
280 parent.getShell(), "Operation status",
281 "Failed to update the resource properties.");
283 // Reset the old property values.
284 properties = getData(resourceManagerRef
285 .getCurrentResourceInSelection());
286 updateViewer(properties);
290 cancelBtn.setEnabled(false);
291 editBtn.setText("Edit");
292 setEnableEdit(false);
297 cancelBtn = new Button(parent, SWT.PUSH);
298 cancelBtn.setText("Cancel");
299 cancelBtn.setEnabled(false);
302 cancelBtn.setLayoutData(gd);
303 cancelBtn.addSelectionListener(new SelectionAdapter() {
305 public void widgetSelected(SelectionEvent e) {
306 Resource res = resourceManagerRef
307 .getCurrentResourceInSelection();
309 properties = getData(res);
311 updateViewer(properties);
313 cancelBtn.setEnabled(false);
314 editBtn.setText("Edit");
315 setEnableEdit(false);
316 if (null != updatedIfSet)
317 updatedIfSet.clear();
321 // Get the supported interfaces.
322 Map<String, String> ifTypesSupported = Utility
323 .getResourceInterfaces(SingleResource.class);
324 if (null != ifTypesSupported && !ifTypesSupported.isEmpty()) {
325 ifTypes = new HashMap<String, String>();
327 for (Map.Entry<String, String> entry : ifTypesSupported.entrySet()) {
328 key = entry.getValue() + " (" + entry.getKey() + ")";
329 ifTypes.put(key, entry.getKey());
333 addManagerListeners();
335 // Check whether there is any resource selected already
336 Resource resource = resourceManagerRef.getCurrentResourceInSelection();
337 properties = getData(resource);
338 if (null != properties) {
339 updateViewer(properties);
341 updateEditControls(resource);
344 private void updateEditControls(Object obj) {
345 if (!editBtn.isDisposed() && !cancelBtn.isDisposed()) {
347 if (editBtn.getText().equals("Save")) {
348 editBtn.setText("Edit");
349 setEnableEdit(false);
353 editBtn.setEnabled(false);
355 editBtn.setEnabled(true);
357 cancelBtn.setEnabled(false);
361 private List<MetaProperty> getData(Resource resource) {
362 if (null != resource) {
363 List<MetaProperty> metaPropertyList = resourceManagerRef
364 .getMetaProperties(resource);
365 return metaPropertyList;
371 private void updateViewer(List<MetaProperty> metaPropertyList) {
372 if (null != tableViewer) {
373 Table tbl = tableViewer.getTable();
374 if (tbl.isDisposed()) {
377 if (null != metaPropertyList) {
378 tableViewer.setInput(metaPropertyList.toArray());
379 tbl.setLinesVisible(true);
382 tbl.setLinesVisible(false);
387 public void createColumns(final TableViewer tableViewer) {
388 TableViewerColumn propName = new TableViewerColumn(tableViewer,
390 propName.getColumn().setWidth(columnWidth[0]);
391 propName.getColumn().setText(columnHeaders[0]);
392 propName.setLabelProvider(new StyledCellLabelProvider() {
394 public void update(ViewerCell cell) {
395 MetaProperty prop = (MetaProperty) cell.getElement();
396 cell.setText(prop.getPropName());
401 TableViewerColumn propValue = new TableViewerColumn(tableViewer,
403 propValue.getColumn().setWidth(columnWidth[1]);
404 propValue.getColumn().setText(columnHeaders[1]);
405 propValue.setLabelProvider(new StyledCellLabelProvider() {
407 public void update(ViewerCell cell) {
408 MetaProperty prop = (MetaProperty) cell.getElement();
409 cell.setText(prop.getPropValue());
413 propValue.setEditingSupport(new PropValueEditor(tableViewer));
416 private void addManagerListeners() {
417 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
418 resourceSelectionChangedListener);
421 private static class PropertycontentProvider implements
422 IStructuredContentProvider {
425 public void dispose() {
429 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
433 public Object[] getElements(Object element) {
434 return (Object[]) element;
439 public void dispose() {
440 // Unregister the listener
441 if (null != resourceSelectionChangedListener) {
442 UiListenerHandler.getInstance()
443 .removeResourceSelectionChangedUIListener(
444 resourceSelectionChangedListener);
449 class PropValueEditor extends EditingSupport {
451 private final TableViewer viewer;
453 public PropValueEditor(TableViewer viewer) {
455 this.viewer = viewer;
459 protected boolean canEdit(Object element) {
464 protected CellEditor getCellEditor(final Object element) {
465 if (!getEnableEdit()) {
468 String propName = ((MetaProperty) element).getPropName();
469 CellEditor editor = new TextCellEditor(viewer.getTable());
470 if (null != propName && propName.equals(Constants.INTERFACE_TYPES)) {
471 editor.setStyle(SWT.READ_ONLY);
472 final Text txt = (Text) editor.getControl();
473 txt.addModifyListener(new ModifyListener() {
475 public void modifyText(ModifyEvent e) {
476 if (null == updatedIfSet) {
479 // Form the result set of interfaces with check-box that
480 // will be shown in the dialog for editing.
481 Map<String, String> curResInterfaces = new HashMap<String, String>();
482 for (Map.Entry<String, String> entry : ifTypes
484 if (updatedIfSet.contains(entry.getValue())) {
485 curResInterfaces.put(entry.getKey(),
490 // Show the dialog for editing the resource interfaces.
491 UpdateResourceInterfaceDialog ad = new UpdateResourceInterfaceDialog(
492 Display.getDefault().getActiveShell(),
493 curResInterfaces, ifTypes);
494 if (ad.open() == Window.OK) {
495 // Update the local copy of the current resource
496 // interfaces to keep the state for save operation.
497 updatedIfSet.clear();
498 StringBuilder newPropValue = new StringBuilder();
499 for (Map.Entry<String, String> entry : curResInterfaces
501 if (!newPropValue.toString().isEmpty()) {
502 newPropValue.append(", ");
504 String value = ifTypes.get(entry.getKey());
505 newPropValue.append(value);
507 updatedIfSet.add(value);
510 MetaProperty prop = (MetaProperty) element;
511 StringBuilder value = new StringBuilder();
512 value.append(Constants.BASELINE_INTERFACE);
513 if (newPropValue.length() > 0) {
514 value.append(", " + newPropValue.toString());
516 prop.setPropValue(value.toString());
518 // Update the viewer in a separate UI thread.
519 Display.getDefault().asyncExec(new Runnable() {
522 viewer.refresh(element, true);
533 protected Object getValue(Object element) {
534 return ((MetaProperty) element).getPropValue();
538 protected void setValue(Object element, Object value) {
539 MetaProperty prop = (MetaProperty) element;
540 if (prop.getPropName().equals(Constants.INTERFACE_TYPES)) {
543 prop.setPropValue(String.valueOf(value));
544 viewer.update(element, null);
548 private synchronized Boolean getEnableEdit() {
552 private synchronized void setEnableEdit(boolean value) {
557 public void setFocus() {