[IoTivity Simulator] Handling resource interfaces.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / SimpleResourceBasicDetailsPage.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.serviceprovider.view.dialogs;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Set;
24
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;
29
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;
48
49 public class SimpleResourceBasicDetailsPage extends WizardPage {
50
51     private Text                resNameTxt;
52     private Text                resUriTxt;
53     private Text                resTypeTxt;
54
55     private Button              observeBtn;
56     private Button              addIfTypeBtn;
57     private Button              removeIfTypeBtn;
58
59     private List                ifTypesList;
60
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;
67
68     protected SimpleResourceBasicDetailsPage() {
69         super("Basic Details");
70         selectedIfTypes = new HashSet<String>();
71     }
72
73     @Override
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);
78
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);
84
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);
91
92         Label resNameLbl = new Label(resDetGrp, SWT.NULL);
93         resNameLbl.setText(Constants.RESOURCE_NAME);
94         gd = new GridData();
95         resNameLbl.setLayoutData(gd);
96
97         resNameTxt = new Text(resDetGrp, SWT.BORDER);
98         gd = new GridData();
99         gd.horizontalAlignment = SWT.FILL;
100         gd.grabExcessHorizontalSpace = true;
101         gd.horizontalSpan = 3;
102         resNameTxt.setLayoutData(gd);
103         resNameTxt.setFocus();
104
105         Label resUriLbl = new Label(resDetGrp, SWT.NULL);
106         resUriLbl.setText(Constants.RESOURCE_URI);
107
108         resUriTxt = new Text(resDetGrp, SWT.BORDER);
109         gd = new GridData();
110         gd.horizontalSpan = 3;
111         gd.horizontalAlignment = SWT.FILL;
112         gd.grabExcessHorizontalSpace = true;
113         resUriTxt.setLayoutData(gd);
114
115         Label resTypeLbl = new Label(resDetGrp, SWT.NULL);
116         resTypeLbl.setText(Constants.RESOURCE_TYPE);
117
118         resTypeTxt = new Text(resDetGrp, SWT.BORDER);
119         gd = new GridData();
120         gd.horizontalSpan = 3;
121         gd.horizontalAlignment = SWT.FILL;
122         gd.grabExcessHorizontalSpace = true;
123         resTypeTxt.setLayoutData(gd);
124
125         Label ifTypesLbl = new Label(resDetGrp, SWT.NULL);
126         ifTypesLbl.setText(Constants.INTERFACE_TYPES);
127         gd = new GridData();
128         ifTypesLbl.setLayoutData(gd);
129
130         ifTypesList = new List(resDetGrp, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL
131                 | SWT.H_SCROLL);
132         gd = new GridData();
133         gd.horizontalAlignment = SWT.FILL;
134         gd.grabExcessHorizontalSpace = true;
135         gd.horizontalSpan = 2;
136         ifTypesList.setLayoutData(gd);
137         ifTypesList.setBackground(Display.getDefault().getSystemColor(
138                 SWT.COLOR_WHITE));
139
140         Composite ifTypesOpComp = new Composite(resDetGrp, SWT.NONE);
141         gridLayout = new GridLayout();
142         ifTypesOpComp.setLayout(gridLayout);
143         gd = new GridData();
144         gd.verticalAlignment = SWT.TOP;
145         ifTypesOpComp.setLayoutData(gd);
146
147         addIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
148         addIfTypeBtn.setText("Add");
149         gd = new GridData();
150         gd.widthHint = 70;
151         addIfTypeBtn.setLayoutData(gd);
152
153         removeIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
154         removeIfTypeBtn.setText("Remove");
155         gd = new GridData();
156         gd.widthHint = 70;
157         removeIfTypeBtn.setLayoutData(gd);
158         removeIfTypeBtn.setEnabled(false);
159
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);
166
167         observeBtn = new Button(otherOptionsGrp, SWT.CHECK);
168         observeBtn.setText("Observable");
169         observeBtn.setSelection(true);
170         observable = true;
171
172         addUiListeners();
173
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>();
179             String key;
180             for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
181                 key = entry.getValue() + " (" + entry.getKey() + ")";
182                 this.ifTypes.put(key, entry.getKey());
183             }
184         }
185
186         setControl(container);
187     }
188
189     private void addUiListeners() {
190
191         resUriTxt.addModifyListener(new ModifyListener() {
192             @Override
193             public void modifyText(ModifyEvent arg0) {
194                 resURI = resUriTxt.getText();
195                 if (null == resURI) {
196                     return;
197                 }
198
199                 getWizard().getContainer().updateButtons();
200             }
201         });
202
203         resNameTxt.addModifyListener(new ModifyListener() {
204             @Override
205             public void modifyText(ModifyEvent arg0) {
206                 resName = resNameTxt.getText();
207                 getWizard().getContainer().updateButtons();
208             }
209         });
210
211         resTypeTxt.addModifyListener(new ModifyListener() {
212             @Override
213             public void modifyText(ModifyEvent arg0) {
214                 resType = resTypeTxt.getText();
215                 getWizard().getContainer().updateButtons();
216             }
217         });
218
219         observeBtn.addSelectionListener(new SelectionAdapter() {
220             @Override
221             public void widgetSelected(SelectionEvent e) {
222                 observable = observeBtn.getSelection();
223             }
224         });
225
226         addIfTypeBtn.addSelectionListener(new SelectionAdapter() {
227             @Override
228             public void widgetSelected(SelectionEvent e) {
229                 boolean addAll = false;
230                 int itemsAddedCount = ifTypesList.getItemCount();
231                 if (itemsAddedCount < ifTypes.size()) {
232                     String key;
233                     ArrayList<String> pendingItems = new ArrayList<String>();
234                     if (0 == itemsAddedCount) {
235                         addAll = true;
236                     }
237                     for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
238                         key = entry.getKey();
239                         if (addAll || -1 == ifTypesList.indexOf(key)) {
240                             pendingItems.add(key);
241                         }
242                     }
243
244                     AddInterfaceTypeDialog addIfTypeDlg = new AddInterfaceTypeDialog(
245                             getShell(), pendingItems);
246                     if (Window.CANCEL == addIfTypeDlg.open()) {
247                         return;
248                     }
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);
255
256                     if (itemsAddedCount + 1 == ifTypes.size()) {
257                         addIfTypeBtn.setEnabled(false);
258                     }
259                 }
260             }
261         });
262
263         removeIfTypeBtn.addSelectionListener(new SelectionAdapter() {
264             @Override
265             public void widgetSelected(SelectionEvent e) {
266                 int[] selection = ifTypesList.getSelectionIndices();
267                 if (null != selection && selection.length > 0) {
268                     ifTypesList.remove(selection);
269                 }
270
271                 removeIfTypeBtn.setEnabled(false);
272                 addIfTypeBtn.setEnabled(true);
273             }
274         });
275
276         ifTypesList.addSelectionListener(new SelectionAdapter() {
277             @Override
278             public void widgetSelected(SelectionEvent e) {
279                 int[] selection = ifTypesList.getSelectionIndices();
280                 if (null != selection && selection.length > 0) {
281                     removeIfTypeBtn.setEnabled(true);
282                 }
283             }
284         });
285     }
286
287     @Override
288     public boolean canFlipToNextPage() {
289         if (null == resName || null == resURI || null == resType) {
290             return false;
291         }
292         resName = resName.trim();
293         resURI = resURI.trim();
294         resType = resType.trim();
295         if (resName.length() < 1 || resURI.length() < 1 || resType.length() < 1) {
296             return false;
297         }
298         return true;
299     }
300
301     @Override
302     public IWizardPage getNextPage() {
303         if (!Utility.isUriValid(resURI)) {
304             MessageDialog.openError(Display.getDefault().getActiveShell(),
305                     "Invalid Resource URI.", Constants.INVALID_URI_MESSAGE);
306             return null;
307         }
308
309         if (!Utility.isResourceTypeValid(resType)) {
310             MessageDialog.openError(Display.getDefault().getActiveShell(),
311                     "Invalid Resource Type.",
312                     Constants.INVALID_RESOURCE_TYPE_MESSAGE);
313             return null;
314         }
315
316         // Checking whether the uri is used by any other resource.
317         if (Activator.getDefault().getResourceManager().isResourceExist(resURI)) {
318             MessageDialog
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
322             // itself.
323             return null;
324         }
325
326         String[] items = ifTypesList.getItems();
327         if (null == items || items.length == 0) {
328             MessageDialog
329                     .openInformation(
330                             getShell(),
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 + ")");
336         }
337         selectedIfTypes.clear();
338         for (String item : ifTypesList.getItems()) {
339             selectedIfTypes.add(ifTypes.get(item));
340         }
341
342         return ((CreateResourceWizard) getWizard())
343                 .getSimpleResourceAddAttributePage();
344     }
345
346     public String getResName() {
347         return resName;
348     }
349
350     public String getResURI() {
351         return resURI;
352     }
353
354     public String getResType() {
355         return resType;
356     }
357
358     public Set<String> getInterfaceTypes() {
359         return selectedIfTypes;
360     }
361
362     public boolean isObservable() {
363         return observable;
364     }
365 }