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.dialogs;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.window.Window;
21 import org.eclipse.jface.wizard.IWizardPage;
22 import org.eclipse.jface.wizard.WizardPage;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.ModifyEvent;
25 import org.eclipse.swt.events.ModifyListener;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Group;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.List;
36 import org.eclipse.swt.widgets.Text;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.HashSet;
44 import oic.simulator.serviceprovider.Activator;
45 import oic.simulator.serviceprovider.model.SingleResource;
46 import oic.simulator.serviceprovider.utils.Constants;
47 import oic.simulator.serviceprovider.utils.Utility;
49 public class SimpleResourceBasicDetailsPage extends WizardPage {
51 private Text resNameTxt;
52 private Text resUriTxt;
53 private Text resTypeTxt;
55 private Button observeBtn;
56 private Button discoverBtn;
57 private Button addIfTypeBtn;
58 private Button removeIfTypeBtn;
60 private List ifTypesList;
62 private String resName;
63 private String resURI;
64 private String resType;
65 private boolean observable;
66 private boolean discoverable;
67 private Map<String, String> ifTypes;
68 private Set<String> selectedIfTypes;
70 protected SimpleResourceBasicDetailsPage() {
71 super("Basic Details");
72 selectedIfTypes = new HashSet<String>();
76 public void createControl(Composite parent) {
77 setPageComplete(false);
78 setTitle(Constants.SIMPLE_RESOURCE_BASIC_DETAILS_PAGE_TITLE);
79 setMessage(Constants.SIMPLE_RESOURCE_BASIC_DETAILS_PAGE_MESSAGE);
81 Composite container = new Composite(parent, SWT.NONE);
82 GridLayout gridLayout = new GridLayout();
83 container.setLayout(gridLayout);
84 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
85 container.setLayoutData(gd);
87 Group resDetGrp = new Group(container, SWT.NONE);
88 resDetGrp.setText("Resource Details");
89 gridLayout = new GridLayout(4, false);
90 resDetGrp.setLayout(gridLayout);
91 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
92 resDetGrp.setLayoutData(gd);
94 Label resNameLbl = new Label(resDetGrp, SWT.NULL);
95 resNameLbl.setText(Constants.RESOURCE_NAME);
97 resNameLbl.setLayoutData(gd);
99 resNameTxt = new Text(resDetGrp, SWT.BORDER);
101 gd.horizontalAlignment = SWT.FILL;
102 gd.grabExcessHorizontalSpace = true;
103 gd.horizontalSpan = 3;
104 resNameTxt.setLayoutData(gd);
105 resNameTxt.setFocus();
107 Label resUriLbl = new Label(resDetGrp, SWT.NULL);
108 resUriLbl.setText(Constants.RESOURCE_URI);
110 resUriTxt = new Text(resDetGrp, SWT.BORDER);
112 gd.horizontalSpan = 3;
113 gd.horizontalAlignment = SWT.FILL;
114 gd.grabExcessHorizontalSpace = true;
115 resUriTxt.setLayoutData(gd);
117 Label resTypeLbl = new Label(resDetGrp, SWT.NULL);
118 resTypeLbl.setText(Constants.RESOURCE_TYPE);
120 resTypeTxt = new Text(resDetGrp, SWT.BORDER);
122 gd.horizontalSpan = 3;
123 gd.horizontalAlignment = SWT.FILL;
124 gd.grabExcessHorizontalSpace = true;
125 resTypeTxt.setLayoutData(gd);
127 Label ifTypesLbl = new Label(resDetGrp, SWT.NULL);
128 ifTypesLbl.setText(Constants.INTERFACE_TYPES);
130 ifTypesLbl.setLayoutData(gd);
132 ifTypesList = new List(resDetGrp, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL
135 gd.horizontalAlignment = SWT.FILL;
136 gd.grabExcessHorizontalSpace = true;
137 gd.horizontalSpan = 2;
138 ifTypesList.setLayoutData(gd);
139 ifTypesList.setBackground(Display.getDefault().getSystemColor(
142 Composite ifTypesOpComp = new Composite(resDetGrp, SWT.NONE);
143 gridLayout = new GridLayout();
144 ifTypesOpComp.setLayout(gridLayout);
146 gd.verticalAlignment = SWT.TOP;
147 ifTypesOpComp.setLayoutData(gd);
149 addIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
150 addIfTypeBtn.setText("Add");
153 addIfTypeBtn.setLayoutData(gd);
155 removeIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
156 removeIfTypeBtn.setText("Remove");
159 removeIfTypeBtn.setLayoutData(gd);
160 removeIfTypeBtn.setEnabled(false);
162 Group otherOptionsGrp = new Group(container, SWT.NONE);
163 otherOptionsGrp.setText("Other options");
164 gridLayout = new GridLayout();
165 otherOptionsGrp.setLayout(gridLayout);
166 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
167 otherOptionsGrp.setLayoutData(gd);
169 discoverBtn = new Button(otherOptionsGrp, SWT.CHECK);
170 discoverBtn.setText("Discoverable");
171 discoverBtn.setSelection(true);
174 observeBtn = new Button(otherOptionsGrp, SWT.CHECK);
175 observeBtn.setText("Observable");
176 observeBtn.setSelection(true);
181 // Set the interface types.
182 Map<String, String> ifTypes = Utility
183 .getResourceInterfaces(SingleResource.class);
184 if (null != ifTypes && !ifTypes.isEmpty()) {
185 this.ifTypes = new HashMap<String, String>();
187 for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
188 key = entry.getValue() + " (" + entry.getKey() + ")";
189 this.ifTypes.put(key, entry.getKey());
193 setControl(container);
196 private void addUiListeners() {
198 resUriTxt.addModifyListener(new ModifyListener() {
200 public void modifyText(ModifyEvent arg0) {
201 resURI = resUriTxt.getText();
202 if (null == resURI) {
206 getWizard().getContainer().updateButtons();
210 resNameTxt.addModifyListener(new ModifyListener() {
212 public void modifyText(ModifyEvent arg0) {
213 resName = resNameTxt.getText();
214 getWizard().getContainer().updateButtons();
218 resTypeTxt.addModifyListener(new ModifyListener() {
220 public void modifyText(ModifyEvent arg0) {
221 resType = resTypeTxt.getText();
222 getWizard().getContainer().updateButtons();
226 discoverBtn.addSelectionListener(new SelectionAdapter() {
228 public void widgetSelected(SelectionEvent e) {
229 discoverable = discoverBtn.getSelection();
233 observeBtn.addSelectionListener(new SelectionAdapter() {
235 public void widgetSelected(SelectionEvent e) {
236 observable = observeBtn.getSelection();
240 addIfTypeBtn.addSelectionListener(new SelectionAdapter() {
242 public void widgetSelected(SelectionEvent e) {
243 boolean addAll = false;
244 int itemsAddedCount = ifTypesList.getItemCount();
245 if (itemsAddedCount < ifTypes.size()) {
247 ArrayList<String> pendingItems = new ArrayList<String>();
248 if (0 == itemsAddedCount) {
251 for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
252 key = entry.getKey();
253 if (addAll || -1 == ifTypesList.indexOf(key)) {
254 pendingItems.add(key);
258 AddInterfaceTypeDialog addIfTypeDlg = new AddInterfaceTypeDialog(
259 getShell(), pendingItems);
260 if (Window.CANCEL == addIfTypeDlg.open()) {
263 String ifType = addIfTypeDlg.getValue();
264 ifTypesList.add(ifType);
265 ifTypesList.deselectAll();
266 ifTypesList.select(ifTypesList.getItemCount() - 1);
267 ifTypesList.showSelection();
268 removeIfTypeBtn.setEnabled(true);
270 if (itemsAddedCount + 1 == ifTypes.size()) {
271 addIfTypeBtn.setEnabled(false);
277 removeIfTypeBtn.addSelectionListener(new SelectionAdapter() {
279 public void widgetSelected(SelectionEvent e) {
280 int[] selection = ifTypesList.getSelectionIndices();
281 if (null != selection && selection.length > 0) {
282 ifTypesList.remove(selection);
285 removeIfTypeBtn.setEnabled(false);
286 addIfTypeBtn.setEnabled(true);
290 ifTypesList.addSelectionListener(new SelectionAdapter() {
292 public void widgetSelected(SelectionEvent e) {
293 int[] selection = ifTypesList.getSelectionIndices();
294 if (null != selection && selection.length > 0) {
295 removeIfTypeBtn.setEnabled(true);
302 public boolean canFlipToNextPage() {
303 if (null == resName || null == resURI || null == resType) {
306 resName = resName.trim();
307 resURI = resURI.trim();
308 resType = resType.trim();
309 if (resName.length() < 1 || resURI.length() < 1 || resType.length() < 1) {
316 public IWizardPage getNextPage() {
317 if (!Utility.isUriValid(resURI)) {
318 MessageDialog.openError(Display.getDefault().getActiveShell(),
319 "Invalid Resource URI.", Constants.INVALID_URI_MESSAGE);
323 if (!Utility.isResourceTypeValid(resType)) {
324 MessageDialog.openError(Display.getDefault().getActiveShell(),
325 "Invalid Resource Type.",
326 Constants.INVALID_RESOURCE_TYPE_MESSAGE);
330 // Checking whether the uri is used by any other resource.
331 if (Activator.getDefault().getResourceManager().isResourceExist(resURI)) {
333 .openError(getShell(), "Resource URI in use",
334 "Entered resource URI is in use. Please try a different one.");
338 String[] items = ifTypesList.getItems();
339 if (null == items || items.length == 0) {
343 "Default Interface Type Selection",
344 "As no interface types are added, the resource will be "
345 + "configured with the default interface type(oic.if.baseline).");
346 ifTypesList.add("Baseline" + " ("
347 + Constants.DEFAULT_SINGLE_RESOURCE_INTERFACE + ")");
349 selectedIfTypes.clear();
350 for (String item : ifTypesList.getItems()) {
351 selectedIfTypes.add(ifTypes.get(item));
354 return ((CreateResourceWizard) getWizard())
355 .getSimpleResourceAddAttributePage();
358 public String getResName() {
362 public String getResURI() {
366 public String getResType() {
370 public Set<String> getInterfaceTypes() {
371 return selectedIfTypes;
374 public boolean isObservable() {
378 public boolean isDiscoverable() {