9b0d60355aff4cf774ae1bd06984645d9bcafc42
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / view / dialogs / FindResourcePage.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.clientcontroller.view.dialogs;
18
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import oic.simulator.clientcontroller.utils.Constants;
23
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.custom.CCombo;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Group;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Text;
38
39 /**
40  * This class shows UI for finding resources.
41  */
42 public class FindResourcePage extends WizardPage {
43
44     private Button      stdResTypeRbtn;
45     private CCombo      resourceTypeCmb;
46     private Button      cusResTypeRbtn;
47     private Text        resTypeTxt;
48     private Label       stdRTypeLbl;
49     private Label       cusRTypeLbl;
50
51     private Set<String> typesToSearch;
52
53     private String      dummyRType;
54
55     protected FindResourcePage() {
56         super("Find Resource");
57     }
58
59     @Override
60     public void createControl(Composite parent) {
61         setPageComplete(false);
62         setTitle(Constants.FIND_PAGE_TITLE);
63         setMessage(Constants.FIND_PAGE_MESSAGE);
64
65         Composite compContent = new Composite(parent, SWT.NONE);
66         GridLayout gridLayout = new GridLayout();
67         compContent.setLayout(gridLayout);
68         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
69         compContent.setLayoutData(gd);
70
71         Group configGroup = new Group(compContent, SWT.NONE);
72         gridLayout = new GridLayout(1, false);
73         gridLayout.verticalSpacing = 10;
74         gridLayout.marginTop = 5;
75         configGroup.setLayout(gridLayout);
76         gd = new GridData();
77         gd.horizontalAlignment = SWT.FILL;
78         gd.grabExcessHorizontalSpace = true;
79         configGroup.setLayoutData(gd);
80         configGroup.setText("Resource Type");
81
82         stdResTypeRbtn = new Button(configGroup, SWT.RADIO);
83         stdResTypeRbtn.setText("Standard OIC Resources");
84
85         Composite stdConfigComp = new Composite(configGroup, SWT.NONE);
86         stdConfigComp.setLayout(new GridLayout(2, false));
87         gd = new GridData();
88         gd.horizontalAlignment = SWT.FILL;
89         gd.grabExcessHorizontalSpace = true;
90         stdConfigComp.setLayoutData(gd);
91
92         stdRTypeLbl = new Label(stdConfigComp, SWT.NONE);
93         stdRTypeLbl.setText("ResourceType:");
94         stdRTypeLbl.setEnabled(false);
95
96         resourceTypeCmb = new CCombo(stdConfigComp, SWT.READ_ONLY | SWT.BORDER);
97         gd = new GridData();
98         gd.widthHint = 150;
99         resourceTypeCmb.setLayoutData(gd);
100         resourceTypeCmb.setEnabled(false);
101
102         cusResTypeRbtn = new Button(configGroup, SWT.RADIO);
103         cusResTypeRbtn.setText("Custom Resources");
104
105         Composite cusConfigComp = new Composite(configGroup, SWT.NONE);
106         cusConfigComp.setLayout(new GridLayout(2, false));
107         gd = new GridData();
108         gd.horizontalAlignment = SWT.FILL;
109         gd.grabExcessHorizontalSpace = true;
110         cusConfigComp.setLayoutData(gd);
111
112         cusRTypeLbl = new Label(cusConfigComp, SWT.NONE);
113         cusRTypeLbl.setText("Enter ResourceType:");
114         cusRTypeLbl.setEnabled(false);
115
116         resTypeTxt = new Text(cusConfigComp, SWT.BORDER);
117         gd = new GridData();
118         gd.minimumWidth = 200;
119         gd.horizontalAlignment = SWT.FILL;
120         gd.grabExcessHorizontalSpace = true;
121         resTypeTxt.setLayoutData(gd);
122         resTypeTxt.setEnabled(false);
123
124         populateDataInUI();
125
126         addUIListeners();
127
128         setControl(compContent);
129     }
130
131     private void populateDataInUI() {
132         // Populate Standard resource-types in Combo
133         populateResourceTypeCombo();
134     }
135
136     private void populateResourceTypeCombo() {
137         /*
138          * List<String> configList; configList =
139          * Activator.getDefault().getManager().getResourceConfigurationList();
140          * if(null != configList) { Iterator<String> itr =
141          * configList.iterator(); while(itr.hasNext()) {
142          * resourceTypeCmb.add(itr.next()); } }
143          */
144
145         // TODO: Temporarily adding a resourceType for testing
146        // resourceTypeCmb.add("oic.r.light");
147         resourceTypeCmb.add("sample.light");
148
149         // By default, selecting the first item in the resourceType combo
150         if (resourceTypeCmb.getItemCount() > 0) {
151             resourceTypeCmb.select(0);
152             // TODO: Get the RAML configuration file path of the selected
153             // resource
154             // configFilePath =
155             // Activator.getManager().getConfigFilePath(resourceTypeCmb.getItem(0));
156         }
157     }
158
159     private void addUIListeners() {
160         stdResTypeRbtn.addSelectionListener(new SelectionAdapter() {
161             @Override
162             public void widgetSelected(SelectionEvent e) {
163                 // Clear the existing items from the search list
164                 if (null != typesToSearch)
165                     typesToSearch.clear();
166
167                 // Set the configFilePath to the first item in the combo
168                 if (resourceTypeCmb.getItemCount() > 0) {
169                     resourceTypeCmb.select(0);
170                     addSearchType(resourceTypeCmb.getText());
171                 }
172
173                 setPageComplete(isSelectionDone());
174
175                 // Change the visibility of widgets
176                 changeVisibility(true);
177             }
178         });
179
180         cusResTypeRbtn.addSelectionListener(new SelectionAdapter() {
181             @Override
182             public void widgetSelected(SelectionEvent e) {
183                 // Clear the existing items from the search list
184                 if (null != typesToSearch)
185                     typesToSearch.clear();
186
187                 addSearchType(resTypeTxt.getText());
188
189                 setPageComplete(isSelectionDone());
190
191                 // Change the visibility of widgets
192                 changeVisibility(false);
193
194                 resTypeTxt.setFocus();
195             }
196         });
197
198         resourceTypeCmb.addSelectionListener(new SelectionAdapter() {
199             @Override
200             public void widgetSelected(SelectionEvent e) {
201                 int index = resourceTypeCmb.getSelectionIndex();
202                 if (index < 0) {
203                     return;
204                 }
205                 String resourceType = resourceTypeCmb.getItem(index);
206                 addSearchType(resourceType);
207                 setPageComplete(isSelectionDone());
208             }
209         });
210
211         resTypeTxt.addModifyListener(new ModifyListener() {
212             @Override
213             public void modifyText(ModifyEvent e) {
214                 String resourceType = resTypeTxt.getText();
215                 if (null != dummyRType) {
216                     removeSearchType(dummyRType);
217                 }
218                 dummyRType = resourceType;
219                 addSearchType(resourceType);
220                 setPageComplete(isSelectionDone());
221             }
222         });
223     }
224
225     private void changeVisibility(boolean standard) {
226         stdRTypeLbl.setEnabled(standard);
227         resourceTypeCmb.setEnabled(standard);
228         cusRTypeLbl.setEnabled(!standard);
229         resTypeTxt.setEnabled(!standard);
230     }
231
232     private boolean isSelectionDone() {
233         if (null == typesToSearch || typesToSearch.size() < 1) {
234             return false;
235         }
236         return true;
237     }
238
239     private void addSearchType(String resourceType) {
240         if (null == resourceType)
241             return;
242         resourceType = resourceType.trim();
243         if (resourceType.length() < 1) {
244             return;
245         }
246         if (null == typesToSearch) {
247             typesToSearch = new HashSet<String>();
248         }
249         typesToSearch.add(resourceType);
250     }
251
252     private void removeSearchType(String resourceType) {
253         if (null == resourceType || null == typesToSearch)
254             return;
255         resourceType = resourceType.trim();
256         if (resourceType.length() < 1) {
257             return;
258         }
259         typesToSearch.remove(resourceType);
260     }
261
262     public Set<String> getSearchTypes() {
263         return typesToSearch;
264     }
265 }