Java SDK and Eclipse plugin for simulator.
[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
148         // By default, selecting the first item in the resourceType combo
149         if (resourceTypeCmb.getItemCount() > 0) {
150             resourceTypeCmb.select(0);
151             // TODO: Get the RAML configuration file path of the selected
152             // resource
153             // configFilePath =
154             // Activator.getManager().getConfigFilePath(resourceTypeCmb.getItem(0));
155         }
156     }
157
158     private void addUIListeners() {
159         stdResTypeRbtn.addSelectionListener(new SelectionAdapter() {
160             @Override
161             public void widgetSelected(SelectionEvent e) {
162                 // Clear the existing items from the search list
163                 if (null != typesToSearch)
164                     typesToSearch.clear();
165
166                 // Set the configFilePath to the first item in the combo
167                 if (resourceTypeCmb.getItemCount() > 0) {
168                     resourceTypeCmb.select(0);
169                     addSearchType(resourceTypeCmb.getText());
170                 }
171
172                 setPageComplete(isSelectionDone());
173
174                 // Change the visibility of widgets
175                 changeVisibility(true);
176             }
177         });
178
179         cusResTypeRbtn.addSelectionListener(new SelectionAdapter() {
180             @Override
181             public void widgetSelected(SelectionEvent e) {
182                 // Clear the existing items from the search list
183                 if (null != typesToSearch)
184                     typesToSearch.clear();
185
186                 addSearchType(resTypeTxt.getText());
187
188                 setPageComplete(isSelectionDone());
189
190                 // Change the visibility of widgets
191                 changeVisibility(false);
192
193                 resTypeTxt.setFocus();
194             }
195         });
196
197         resourceTypeCmb.addSelectionListener(new SelectionAdapter() {
198             @Override
199             public void widgetSelected(SelectionEvent e) {
200                 int index = resourceTypeCmb.getSelectionIndex();
201                 if (index < 0) {
202                     return;
203                 }
204                 String resourceType = resourceTypeCmb.getItem(index);
205                 addSearchType(resourceType);
206                 setPageComplete(isSelectionDone());
207             }
208         });
209
210         resTypeTxt.addModifyListener(new ModifyListener() {
211             @Override
212             public void modifyText(ModifyEvent e) {
213                 String resourceType = resTypeTxt.getText();
214                 if (null != dummyRType) {
215                     removeSearchType(dummyRType);
216                 }
217                 dummyRType = resourceType;
218                 addSearchType(resourceType);
219                 setPageComplete(isSelectionDone());
220             }
221         });
222     }
223
224     private void changeVisibility(boolean standard) {
225         stdRTypeLbl.setEnabled(standard);
226         resourceTypeCmb.setEnabled(standard);
227         cusRTypeLbl.setEnabled(!standard);
228         resTypeTxt.setEnabled(!standard);
229     }
230
231     private boolean isSelectionDone() {
232         if (null == typesToSearch || typesToSearch.size() < 1) {
233             return false;
234         }
235         return true;
236     }
237
238     private void addSearchType(String resourceType) {
239         if (null == resourceType)
240             return;
241         resourceType = resourceType.trim();
242         if (resourceType.length() < 1) {
243             return;
244         }
245         if (null == typesToSearch) {
246             typesToSearch = new HashSet<String>();
247         }
248         typesToSearch.add(resourceType);
249     }
250
251     private void removeSearchType(String resourceType) {
252         if (null == resourceType || null == typesToSearch)
253             return;
254         resourceType = resourceType.trim();
255         if (resourceType.length() < 1) {
256             return;
257         }
258         typesToSearch.remove(resourceType);
259     }
260
261     public Set<String> getSearchTypes() {
262         return typesToSearch;
263     }
264 }