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 java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
25 import oic.simulator.serviceprovider.Activator;
26 import oic.simulator.serviceprovider.model.SingleResource;
27 import oic.simulator.serviceprovider.utils.Constants;
28 import oic.simulator.serviceprovider.utils.Utility;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.jface.wizard.IWizardPage;
33 import org.eclipse.jface.wizard.WizardPage;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.events.ModifyEvent;
36 import org.eclipse.swt.events.ModifyListener;
37 import org.eclipse.swt.events.SelectionAdapter;
38 import org.eclipse.swt.events.SelectionEvent;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Display;
44 import org.eclipse.swt.widgets.Group;
45 import org.eclipse.swt.widgets.Label;
46 import org.eclipse.swt.widgets.List;
47 import org.eclipse.swt.widgets.Text;
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 addIfTypeBtn;
57 private Button removeIfTypeBtn;
59 private List ifTypesList;
61 private String resName;
62 private String resURI;
63 private String resType;
64 private boolean observable;
65 private Map<String, String> ifTypes;
66 private Set<String> selectedIfTypes;
68 protected SimpleResourceBasicDetailsPage() {
69 super("Basic Details");
70 selectedIfTypes = new HashSet<String>();
74 public void createControl(Composite parent) {
75 setPageComplete(false);
76 setTitle(Constants.SIMPLE_RESOURCE_BASIC_DETAILS_PAGE_TITLE);
77 setMessage(Constants.SIMPLE_RESOURCE_BASIC_DETAILS_PAGE_MESSAGE);
79 Composite container = new Composite(parent, SWT.NONE);
80 GridLayout gridLayout = new GridLayout();
81 container.setLayout(gridLayout);
82 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
83 container.setLayoutData(gd);
85 Group resDetGrp = new Group(container, SWT.NONE);
86 resDetGrp.setText("Resource Details");
87 gridLayout = new GridLayout(4, false);
88 resDetGrp.setLayout(gridLayout);
89 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
90 resDetGrp.setLayoutData(gd);
92 Label resNameLbl = new Label(resDetGrp, SWT.NULL);
93 resNameLbl.setText(Constants.RESOURCE_NAME);
95 resNameLbl.setLayoutData(gd);
97 resNameTxt = new Text(resDetGrp, SWT.BORDER);
99 gd.horizontalAlignment = SWT.FILL;
100 gd.grabExcessHorizontalSpace = true;
101 gd.horizontalSpan = 3;
102 resNameTxt.setLayoutData(gd);
103 resNameTxt.setFocus();
105 Label resUriLbl = new Label(resDetGrp, SWT.NULL);
106 resUriLbl.setText(Constants.RESOURCE_URI);
108 resUriTxt = new Text(resDetGrp, SWT.BORDER);
110 gd.horizontalSpan = 3;
111 gd.horizontalAlignment = SWT.FILL;
112 gd.grabExcessHorizontalSpace = true;
113 resUriTxt.setLayoutData(gd);
115 Label resTypeLbl = new Label(resDetGrp, SWT.NULL);
116 resTypeLbl.setText(Constants.RESOURCE_TYPE);
118 resTypeTxt = new Text(resDetGrp, SWT.BORDER);
120 gd.horizontalSpan = 3;
121 gd.horizontalAlignment = SWT.FILL;
122 gd.grabExcessHorizontalSpace = true;
123 resTypeTxt.setLayoutData(gd);
125 Label ifTypesLbl = new Label(resDetGrp, SWT.NULL);
126 ifTypesLbl.setText(Constants.INTERFACE_TYPES);
128 ifTypesLbl.setLayoutData(gd);
130 ifTypesList = new List(resDetGrp, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL
133 gd.horizontalAlignment = SWT.FILL;
134 gd.grabExcessHorizontalSpace = true;
135 gd.horizontalSpan = 2;
136 ifTypesList.setLayoutData(gd);
137 ifTypesList.setBackground(Display.getDefault().getSystemColor(
140 Composite ifTypesOpComp = new Composite(resDetGrp, SWT.NONE);
141 gridLayout = new GridLayout();
142 ifTypesOpComp.setLayout(gridLayout);
144 gd.verticalAlignment = SWT.TOP;
145 ifTypesOpComp.setLayoutData(gd);
147 addIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
148 addIfTypeBtn.setText("Add");
151 addIfTypeBtn.setLayoutData(gd);
153 removeIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
154 removeIfTypeBtn.setText("Remove");
157 removeIfTypeBtn.setLayoutData(gd);
158 removeIfTypeBtn.setEnabled(false);
160 Group otherOptionsGrp = new Group(container, SWT.NONE);
161 otherOptionsGrp.setText("Other options");
162 gridLayout = new GridLayout();
163 otherOptionsGrp.setLayout(gridLayout);
164 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
165 otherOptionsGrp.setLayoutData(gd);
167 observeBtn = new Button(otherOptionsGrp, SWT.CHECK);
168 observeBtn.setText("Observable");
169 observeBtn.setSelection(true);
174 // Set the interface types.
175 Map<String, String> ifTypes = Utility
176 .getResourceInterfaces(SingleResource.class);
177 if (null != ifTypes && !ifTypes.isEmpty()) {
178 this.ifTypes = new HashMap<String, String>();
180 for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
181 key = entry.getValue() + " (" + entry.getKey() + ")";
182 this.ifTypes.put(key, entry.getKey());
186 setControl(container);
189 private void addUiListeners() {
191 resUriTxt.addModifyListener(new ModifyListener() {
193 public void modifyText(ModifyEvent arg0) {
194 resURI = resUriTxt.getText();
195 if (null == resURI) {
199 getWizard().getContainer().updateButtons();
203 resNameTxt.addModifyListener(new ModifyListener() {
205 public void modifyText(ModifyEvent arg0) {
206 resName = resNameTxt.getText();
207 getWizard().getContainer().updateButtons();
211 resTypeTxt.addModifyListener(new ModifyListener() {
213 public void modifyText(ModifyEvent arg0) {
214 resType = resTypeTxt.getText();
215 getWizard().getContainer().updateButtons();
219 observeBtn.addSelectionListener(new SelectionAdapter() {
221 public void widgetSelected(SelectionEvent e) {
222 observable = observeBtn.getSelection();
226 addIfTypeBtn.addSelectionListener(new SelectionAdapter() {
228 public void widgetSelected(SelectionEvent e) {
229 boolean addAll = false;
230 int itemsAddedCount = ifTypesList.getItemCount();
231 if (itemsAddedCount < ifTypes.size()) {
233 ArrayList<String> pendingItems = new ArrayList<String>();
234 if (0 == itemsAddedCount) {
237 for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
238 key = entry.getKey();
239 if (addAll || -1 == ifTypesList.indexOf(key)) {
240 pendingItems.add(key);
244 AddInterfaceTypeDialog addIfTypeDlg = new AddInterfaceTypeDialog(
245 getShell(), pendingItems);
246 if (Window.CANCEL == addIfTypeDlg.open()) {
249 String ifType = addIfTypeDlg.getValue();
250 ifTypesList.add(ifType);
251 ifTypesList.deselectAll();
252 ifTypesList.select(ifTypesList.getItemCount() - 1);
253 ifTypesList.showSelection();
254 removeIfTypeBtn.setEnabled(true);
256 if (itemsAddedCount + 1 == ifTypes.size()) {
257 addIfTypeBtn.setEnabled(false);
263 removeIfTypeBtn.addSelectionListener(new SelectionAdapter() {
265 public void widgetSelected(SelectionEvent e) {
266 int[] selection = ifTypesList.getSelectionIndices();
267 if (null != selection && selection.length > 0) {
268 ifTypesList.remove(selection);
271 removeIfTypeBtn.setEnabled(false);
272 addIfTypeBtn.setEnabled(true);
276 ifTypesList.addSelectionListener(new SelectionAdapter() {
278 public void widgetSelected(SelectionEvent e) {
279 int[] selection = ifTypesList.getSelectionIndices();
280 if (null != selection && selection.length > 0) {
281 removeIfTypeBtn.setEnabled(true);
288 public boolean canFlipToNextPage() {
289 if (null == resName || null == resURI || null == resType) {
292 resName = resName.trim();
293 resURI = resURI.trim();
294 resType = resType.trim();
295 if (resName.length() < 1 || resURI.length() < 1 || resType.length() < 1) {
302 public IWizardPage getNextPage() {
303 if (!Utility.isUriValid(resURI)) {
304 MessageDialog.openError(Display.getDefault().getActiveShell(),
305 "Invalid Resource URI.", Constants.INVALID_URI_MESSAGE);
309 if (!Utility.isResourceTypeValid(resType)) {
310 MessageDialog.openError(Display.getDefault().getActiveShell(),
311 "Invalid Resource Type.",
312 Constants.INVALID_RESOURCE_TYPE_MESSAGE);
316 // Checking whether the uri is used by any other resource.
317 if (Activator.getDefault().getResourceManager().isResourceExist(resURI)) {
319 .openError(getShell(), "Resource URI in use",
320 "Entered resource URI is in use. Please try a different one.");
321 // TODO: Instead of MessageDialog, errors may be shown on wizard
326 String[] items = ifTypesList.getItems();
327 if (null == items || items.length == 0) {
331 "Default Interface Type Selection",
332 "As no interface types are added, the resource will be "
333 + "configured with the default interface type(oic.if.baseline).");
334 ifTypesList.add("Baseline" + " ("
335 + Constants.DEFAULT_SINGLE_RESOURCE_INTERFACE + ")");
337 selectedIfTypes.clear();
338 for (String item : ifTypesList.getItems()) {
339 selectedIfTypes.add(ifTypes.get(item));
342 return ((CreateResourceWizard) getWizard())
343 .getSimpleResourceAddAttributePage();
346 public String getResName() {
350 public String getResURI() {
354 public String getResType() {
358 public Set<String> getInterfaceTypes() {
359 return selectedIfTypes;
362 public boolean isObservable() {