Updated the order of imports in the source files of simulator plugins.
[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 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;
37
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.Map;
42 import java.util.Set;
43
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;
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              discoverBtn;
57     private Button              addIfTypeBtn;
58     private Button              removeIfTypeBtn;
59
60     private List                ifTypesList;
61
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;
69
70     protected SimpleResourceBasicDetailsPage() {
71         super("Basic Details");
72         selectedIfTypes = new HashSet<String>();
73     }
74
75     @Override
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);
80
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);
86
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);
93
94         Label resNameLbl = new Label(resDetGrp, SWT.NULL);
95         resNameLbl.setText(Constants.RESOURCE_NAME);
96         gd = new GridData();
97         resNameLbl.setLayoutData(gd);
98
99         resNameTxt = new Text(resDetGrp, SWT.BORDER);
100         gd = new GridData();
101         gd.horizontalAlignment = SWT.FILL;
102         gd.grabExcessHorizontalSpace = true;
103         gd.horizontalSpan = 3;
104         resNameTxt.setLayoutData(gd);
105         resNameTxt.setFocus();
106
107         Label resUriLbl = new Label(resDetGrp, SWT.NULL);
108         resUriLbl.setText(Constants.RESOURCE_URI);
109
110         resUriTxt = new Text(resDetGrp, SWT.BORDER);
111         gd = new GridData();
112         gd.horizontalSpan = 3;
113         gd.horizontalAlignment = SWT.FILL;
114         gd.grabExcessHorizontalSpace = true;
115         resUriTxt.setLayoutData(gd);
116
117         Label resTypeLbl = new Label(resDetGrp, SWT.NULL);
118         resTypeLbl.setText(Constants.RESOURCE_TYPE);
119
120         resTypeTxt = new Text(resDetGrp, SWT.BORDER);
121         gd = new GridData();
122         gd.horizontalSpan = 3;
123         gd.horizontalAlignment = SWT.FILL;
124         gd.grabExcessHorizontalSpace = true;
125         resTypeTxt.setLayoutData(gd);
126
127         Label ifTypesLbl = new Label(resDetGrp, SWT.NULL);
128         ifTypesLbl.setText(Constants.INTERFACE_TYPES);
129         gd = new GridData();
130         ifTypesLbl.setLayoutData(gd);
131
132         ifTypesList = new List(resDetGrp, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL
133                 | SWT.H_SCROLL);
134         gd = new GridData();
135         gd.horizontalAlignment = SWT.FILL;
136         gd.grabExcessHorizontalSpace = true;
137         gd.horizontalSpan = 2;
138         ifTypesList.setLayoutData(gd);
139         ifTypesList.setBackground(Display.getDefault().getSystemColor(
140                 SWT.COLOR_WHITE));
141
142         Composite ifTypesOpComp = new Composite(resDetGrp, SWT.NONE);
143         gridLayout = new GridLayout();
144         ifTypesOpComp.setLayout(gridLayout);
145         gd = new GridData();
146         gd.verticalAlignment = SWT.TOP;
147         ifTypesOpComp.setLayoutData(gd);
148
149         addIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
150         addIfTypeBtn.setText("Add");
151         gd = new GridData();
152         gd.widthHint = 70;
153         addIfTypeBtn.setLayoutData(gd);
154
155         removeIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
156         removeIfTypeBtn.setText("Remove");
157         gd = new GridData();
158         gd.widthHint = 70;
159         removeIfTypeBtn.setLayoutData(gd);
160         removeIfTypeBtn.setEnabled(false);
161
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);
168
169         discoverBtn = new Button(otherOptionsGrp, SWT.CHECK);
170         discoverBtn.setText("Discoverable");
171         discoverBtn.setSelection(true);
172         discoverable = true;
173
174         observeBtn = new Button(otherOptionsGrp, SWT.CHECK);
175         observeBtn.setText("Observable");
176         observeBtn.setSelection(true);
177         observable = true;
178
179         addUiListeners();
180
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>();
186             String key;
187             for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
188                 key = entry.getValue() + " (" + entry.getKey() + ")";
189                 this.ifTypes.put(key, entry.getKey());
190             }
191         }
192
193         setControl(container);
194     }
195
196     private void addUiListeners() {
197
198         resUriTxt.addModifyListener(new ModifyListener() {
199             @Override
200             public void modifyText(ModifyEvent arg0) {
201                 resURI = resUriTxt.getText();
202                 if (null == resURI) {
203                     return;
204                 }
205
206                 getWizard().getContainer().updateButtons();
207             }
208         });
209
210         resNameTxt.addModifyListener(new ModifyListener() {
211             @Override
212             public void modifyText(ModifyEvent arg0) {
213                 resName = resNameTxt.getText();
214                 getWizard().getContainer().updateButtons();
215             }
216         });
217
218         resTypeTxt.addModifyListener(new ModifyListener() {
219             @Override
220             public void modifyText(ModifyEvent arg0) {
221                 resType = resTypeTxt.getText();
222                 getWizard().getContainer().updateButtons();
223             }
224         });
225
226         discoverBtn.addSelectionListener(new SelectionAdapter() {
227             @Override
228             public void widgetSelected(SelectionEvent e) {
229                 discoverable = discoverBtn.getSelection();
230             }
231         });
232
233         observeBtn.addSelectionListener(new SelectionAdapter() {
234             @Override
235             public void widgetSelected(SelectionEvent e) {
236                 observable = observeBtn.getSelection();
237             }
238         });
239
240         addIfTypeBtn.addSelectionListener(new SelectionAdapter() {
241             @Override
242             public void widgetSelected(SelectionEvent e) {
243                 boolean addAll = false;
244                 int itemsAddedCount = ifTypesList.getItemCount();
245                 if (itemsAddedCount < ifTypes.size()) {
246                     String key;
247                     ArrayList<String> pendingItems = new ArrayList<String>();
248                     if (0 == itemsAddedCount) {
249                         addAll = true;
250                     }
251                     for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
252                         key = entry.getKey();
253                         if (addAll || -1 == ifTypesList.indexOf(key)) {
254                             pendingItems.add(key);
255                         }
256                     }
257
258                     AddInterfaceTypeDialog addIfTypeDlg = new AddInterfaceTypeDialog(
259                             getShell(), pendingItems);
260                     if (Window.CANCEL == addIfTypeDlg.open()) {
261                         return;
262                     }
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);
269
270                     if (itemsAddedCount + 1 == ifTypes.size()) {
271                         addIfTypeBtn.setEnabled(false);
272                     }
273                 }
274             }
275         });
276
277         removeIfTypeBtn.addSelectionListener(new SelectionAdapter() {
278             @Override
279             public void widgetSelected(SelectionEvent e) {
280                 int[] selection = ifTypesList.getSelectionIndices();
281                 if (null != selection && selection.length > 0) {
282                     ifTypesList.remove(selection);
283                 }
284
285                 removeIfTypeBtn.setEnabled(false);
286                 addIfTypeBtn.setEnabled(true);
287             }
288         });
289
290         ifTypesList.addSelectionListener(new SelectionAdapter() {
291             @Override
292             public void widgetSelected(SelectionEvent e) {
293                 int[] selection = ifTypesList.getSelectionIndices();
294                 if (null != selection && selection.length > 0) {
295                     removeIfTypeBtn.setEnabled(true);
296                 }
297             }
298         });
299     }
300
301     @Override
302     public boolean canFlipToNextPage() {
303         if (null == resName || null == resURI || null == resType) {
304             return false;
305         }
306         resName = resName.trim();
307         resURI = resURI.trim();
308         resType = resType.trim();
309         if (resName.length() < 1 || resURI.length() < 1 || resType.length() < 1) {
310             return false;
311         }
312         return true;
313     }
314
315     @Override
316     public IWizardPage getNextPage() {
317         if (!Utility.isUriValid(resURI)) {
318             MessageDialog.openError(Display.getDefault().getActiveShell(),
319                     "Invalid Resource URI.", Constants.INVALID_URI_MESSAGE);
320             return null;
321         }
322
323         if (!Utility.isResourceTypeValid(resType)) {
324             MessageDialog.openError(Display.getDefault().getActiveShell(),
325                     "Invalid Resource Type.",
326                     Constants.INVALID_RESOURCE_TYPE_MESSAGE);
327             return null;
328         }
329
330         // Checking whether the uri is used by any other resource.
331         if (Activator.getDefault().getResourceManager().isResourceExist(resURI)) {
332             MessageDialog
333                     .openError(getShell(), "Resource URI in use",
334                             "Entered resource URI is in use. Please try a different one.");
335             return null;
336         }
337
338         String[] items = ifTypesList.getItems();
339         if (null == items || items.length == 0) {
340             MessageDialog
341                     .openInformation(
342                             getShell(),
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 + ")");
348         }
349         selectedIfTypes.clear();
350         for (String item : ifTypesList.getItems()) {
351             selectedIfTypes.add(ifTypes.get(item));
352         }
353
354         return ((CreateResourceWizard) getWizard())
355                 .getSimpleResourceAddAttributePage();
356     }
357
358     public String getResName() {
359         return resName;
360     }
361
362     public String getResURI() {
363         return resURI;
364     }
365
366     public String getResType() {
367         return resType;
368     }
369
370     public Set<String> getInterfaceTypes() {
371         return selectedIfTypes;
372     }
373
374     public boolean isObservable() {
375         return observable;
376     }
377
378     public boolean isDiscoverable() {
379         return discoverable;
380     }
381 }