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.MetaProperty;
26 import oic.simulator.serviceprovider.model.Resource;
27 import oic.simulator.serviceprovider.utils.Constants;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.viewers.CellEditor;
31 import org.eclipse.jface.viewers.EditingSupport;
32 import org.eclipse.jface.viewers.IStructuredContentProvider;
33 import org.eclipse.jface.viewers.StyledCellLabelProvider;
34 import org.eclipse.jface.viewers.TableViewer;
35 import org.eclipse.jface.viewers.TableViewerColumn;
36 import org.eclipse.jface.viewers.TextCellEditor;
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.events.SelectionAdapter;
41 import org.eclipse.swt.events.SelectionEvent;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Button;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Display;
47 import org.eclipse.swt.widgets.Table;
48 import org.eclipse.ui.part.ViewPart;
49 import org.oic.simulator.SimulatorException;
52 * This class manages and shows the meta properties view in the perspective.
54 public class MetaPropertiesView extends ViewPart {
56 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.metaproperties";
58 private TableViewer tableViewer;
60 private final String[] columnHeaders = { "Property", "Value" };
62 private final Integer[] columnWidth = { 150, 150 };
64 private ISelectionChangedListener resourceSelectionChangedListener;
66 private ResourceManager resourceManagerRef;
68 private List<MetaProperty> properties;
70 private boolean enable_edit;
71 private Button editBtn;
72 private Button cancelBtn;
74 public MetaPropertiesView() {
76 resourceManagerRef = Activator.getDefault().getResourceManager();
78 resourceSelectionChangedListener = new ISelectionChangedListener() {
81 public void onResourceSelectionChange(final Resource resource) {
82 Display.getDefault().asyncExec(new Runnable() {
86 if (null != tableViewer) {
87 properties = getData(resource);
88 updateViewer(properties);
90 updateEditControls(resource);
98 public void createPartControl(final Composite parent) {
99 parent.setLayout(new GridLayout(2, false));
101 tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
102 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
104 createColumns(tableViewer);
106 // Make lines and header visible
107 final Table table = tableViewer.getTable();
108 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
109 gd.horizontalSpan = 2;
110 table.setLayoutData(gd);
111 table.setHeaderVisible(true);
112 table.setLinesVisible(true);
114 tableViewer.setContentProvider(new PropertycontentProvider());
116 editBtn = new Button(parent, SWT.PUSH);
117 editBtn.setText("Edit");
120 editBtn.setLayoutData(gd);
121 editBtn.addSelectionListener(new SelectionAdapter() {
123 public void widgetSelected(SelectionEvent e) {
124 if (editBtn.getText().equals("Edit")) {
125 cancelBtn.setEnabled(true);
126 editBtn.setText("Save");
129 boolean result = false;
130 Resource resourceInSelection = resourceManagerRef
131 .getCurrentResourceInSelection();
132 if (null != resourceInSelection) {
135 result = resourceManagerRef.isPropertyValueInvalid(
136 resourceInSelection, properties,
137 Constants.RESOURCE_URI);
139 MessageDialog.openError(parent.getShell(),
140 "Invalid Resource URI.",
141 Constants.INVALID_URI_MESSAGE);
145 result = resourceManagerRef.isPropertyValueInvalid(
146 resourceInSelection, properties,
147 Constants.RESOURCE_NAME);
149 MessageDialog.openError(parent.getShell(),
151 "Resource Name is invalid.");
155 boolean update = false;
156 boolean uriChange = false;
157 boolean nameChange = false;
159 if (resourceManagerRef.isPropValueChanged(
160 resourceInSelection, properties,
161 Constants.RESOURCE_NAME)) {
165 if (resourceManagerRef.isPropValueChanged(
166 resourceInSelection, properties,
167 Constants.RESOURCE_URI)) {
168 // Check whether the new URI is unique.
169 if (!resourceManagerRef.isUriUnique(properties)) {
170 MessageDialog.openError(parent.getShell(),
171 "Resource URI in use",
172 "Resource URI is in use. Please try a different URI.");
176 if (resourceManagerRef
177 .isResourceStarted(resourceInSelection)) {
178 update = MessageDialog.openQuestion(
179 parent.getShell(), "Save Details",
180 "Resource will be restarted to take the changes."
181 + " Do you want to continue?");
192 if (uriChange || nameChange)
195 .getResourceManager()
196 .updateResourceProperties(
198 .getCurrentResourceInSelection(),
199 properties, uriChange,
201 } catch (SimulatorException ex) {
205 MessageDialog.openInformation(
206 parent.getShell(), "Operation status",
207 "Resource properties updated.");
209 MessageDialog.openInformation(
210 parent.getShell(), "Operation status",
211 "Failed to update the resource properties.");
213 // Reset the old property values.
214 properties = getData(resourceManagerRef
215 .getCurrentResourceInSelection());
216 updateViewer(properties);
220 cancelBtn.setEnabled(false);
221 editBtn.setText("Edit");
222 setEnableEdit(false);
227 cancelBtn = new Button(parent, SWT.PUSH);
228 cancelBtn.setText("Cancel");
229 cancelBtn.setEnabled(false);
232 cancelBtn.setLayoutData(gd);
233 cancelBtn.addSelectionListener(new SelectionAdapter() {
235 public void widgetSelected(SelectionEvent e) {
236 Resource res = resourceManagerRef
237 .getCurrentResourceInSelection();
239 properties = getData(res);
241 updateViewer(properties);
243 cancelBtn.setEnabled(false);
244 editBtn.setText("Edit");
245 setEnableEdit(false);
249 addManagerListeners();
251 // Check whether there is any resource selected already
252 Resource resource = resourceManagerRef.getCurrentResourceInSelection();
253 properties = getData(resource);
254 if (null != properties) {
255 updateViewer(properties);
257 updateEditControls(resource);
260 private void updateEditControls(Object obj) {
261 if (!editBtn.isDisposed() && !cancelBtn.isDisposed()) {
263 if (editBtn.getText().equals("Save")) {
264 editBtn.setText("Edit");
265 setEnableEdit(false);
269 editBtn.setEnabled(false);
271 editBtn.setEnabled(true);
273 cancelBtn.setEnabled(false);
277 private List<MetaProperty> getData(Resource resource) {
278 if (null != resource) {
279 List<MetaProperty> metaPropertyList = resourceManagerRef
280 .getMetaProperties(resource);
281 return metaPropertyList;
287 private void updateViewer(List<MetaProperty> metaPropertyList) {
288 if (null != tableViewer) {
289 Table tbl = tableViewer.getTable();
290 if (tbl.isDisposed()) {
293 if (null != metaPropertyList) {
294 tableViewer.setInput(metaPropertyList.toArray());
295 tbl.setLinesVisible(true);
298 tbl.setLinesVisible(false);
303 public void createColumns(final TableViewer tableViewer) {
304 TableViewerColumn propName = new TableViewerColumn(tableViewer,
306 propName.getColumn().setWidth(columnWidth[0]);
307 propName.getColumn().setText(columnHeaders[0]);
308 propName.setLabelProvider(new StyledCellLabelProvider() {
310 public void update(ViewerCell cell) {
311 MetaProperty prop = (MetaProperty) cell.getElement();
312 cell.setText(prop.getPropName());
317 TableViewerColumn propValue = new TableViewerColumn(tableViewer,
319 propValue.getColumn().setWidth(columnWidth[1]);
320 propValue.getColumn().setText(columnHeaders[1]);
321 propValue.setLabelProvider(new StyledCellLabelProvider() {
323 public void update(ViewerCell cell) {
324 MetaProperty prop = (MetaProperty) cell.getElement();
325 cell.setText(prop.getPropValue());
329 propValue.setEditingSupport(new PropValueEditor(tableViewer));
332 private void addManagerListeners() {
333 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
334 resourceSelectionChangedListener);
337 class PropertycontentProvider implements IStructuredContentProvider {
340 public void dispose() {
344 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
348 public Object[] getElements(Object element) {
349 return (Object[]) element;
354 public void dispose() {
355 // Unregister the listener
356 if (null != resourceSelectionChangedListener) {
357 UiListenerHandler.getInstance()
358 .removeResourceSelectionChangedUIListener(
359 resourceSelectionChangedListener);
364 class PropValueEditor extends EditingSupport {
366 private final TableViewer viewer;
368 public PropValueEditor(TableViewer viewer) {
370 this.viewer = viewer;
374 protected boolean canEdit(Object element) {
379 protected CellEditor getCellEditor(final Object element) {
380 if (!getEnableEdit()) {
383 // Disabling edit for resource type
384 String propName = ((MetaProperty) element).getPropName();
385 if (null != propName && propName.equals(Constants.RESOURCE_TYPE)) {
389 CellEditor editor = new TextCellEditor(viewer.getTable());
394 protected Object getValue(Object element) {
395 return ((MetaProperty) element).getPropValue();
399 protected void setValue(Object element, Object value) {
400 MetaProperty prop = (MetaProperty) element;
401 if (prop.getPropName().equals(Constants.INTERFACE_TYPES)) {
404 prop.setPropValue(String.valueOf(value));
405 viewer.update(element, null);
409 private synchronized Boolean getEnableEdit() {
413 private synchronized void setEnableEdit(boolean value) {
418 public void setFocus() {