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.clientcontroller.view.dialogs;
19 import java.util.HashSet;
22 import oic.simulator.clientcontroller.utils.Constants;
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;
40 * This class shows UI for finding resources.
42 public class FindResourcePage extends WizardPage {
44 private Button stdResTypeRbtn;
45 private CCombo resourceTypeCmb;
46 private Button cusResTypeRbtn;
47 private Text resTypeTxt;
48 private Label stdRTypeLbl;
49 private Label cusRTypeLbl;
51 private Set<String> typesToSearch;
53 private String dummyRType;
55 protected FindResourcePage() {
56 super("Find Resource");
60 public void createControl(Composite parent) {
61 setPageComplete(false);
62 setTitle(Constants.FIND_PAGE_TITLE);
63 setMessage(Constants.FIND_PAGE_MESSAGE);
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);
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);
77 gd.horizontalAlignment = SWT.FILL;
78 gd.grabExcessHorizontalSpace = true;
79 configGroup.setLayoutData(gd);
80 configGroup.setText("Resource Type");
82 stdResTypeRbtn = new Button(configGroup, SWT.RADIO);
83 stdResTypeRbtn.setText("Standard OIC Resources");
85 Composite stdConfigComp = new Composite(configGroup, SWT.NONE);
86 stdConfigComp.setLayout(new GridLayout(2, false));
88 gd.horizontalAlignment = SWT.FILL;
89 gd.grabExcessHorizontalSpace = true;
90 stdConfigComp.setLayoutData(gd);
92 stdRTypeLbl = new Label(stdConfigComp, SWT.NONE);
93 stdRTypeLbl.setText("ResourceType:");
94 stdRTypeLbl.setEnabled(false);
96 resourceTypeCmb = new CCombo(stdConfigComp, SWT.READ_ONLY | SWT.BORDER);
99 resourceTypeCmb.setLayoutData(gd);
100 resourceTypeCmb.setEnabled(false);
102 cusResTypeRbtn = new Button(configGroup, SWT.RADIO);
103 cusResTypeRbtn.setText("Custom Resources");
105 Composite cusConfigComp = new Composite(configGroup, SWT.NONE);
106 cusConfigComp.setLayout(new GridLayout(2, false));
108 gd.horizontalAlignment = SWT.FILL;
109 gd.grabExcessHorizontalSpace = true;
110 cusConfigComp.setLayoutData(gd);
112 cusRTypeLbl = new Label(cusConfigComp, SWT.NONE);
113 cusRTypeLbl.setText("Enter ResourceType:");
114 cusRTypeLbl.setEnabled(false);
116 resTypeTxt = new Text(cusConfigComp, SWT.BORDER);
118 gd.minimumWidth = 200;
119 gd.horizontalAlignment = SWT.FILL;
120 gd.grabExcessHorizontalSpace = true;
121 resTypeTxt.setLayoutData(gd);
122 resTypeTxt.setEnabled(false);
128 setControl(compContent);
131 private void populateDataInUI() {
132 // Populate Standard resource-types in Combo
133 populateResourceTypeCombo();
136 private void populateResourceTypeCombo() {
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()); } }
145 // TODO: Temporarily adding a resourceType for testing
146 // resourceTypeCmb.add("oic.r.light");
147 // Currently for standard resources we are using resource type as sample.light.
148 resourceTypeCmb.add("sample.light");
150 // By default, selecting the first item in the resourceType combo
151 if (resourceTypeCmb.getItemCount() > 0) {
152 resourceTypeCmb.select(0);
153 // TODO: Get the RAML configuration file path of the selected
156 // Activator.getManager().getConfigFilePath(resourceTypeCmb.getItem(0));
160 private void addUIListeners() {
161 stdResTypeRbtn.addSelectionListener(new SelectionAdapter() {
163 public void widgetSelected(SelectionEvent e) {
164 // Clear the existing items from the search list
165 if (null != typesToSearch)
166 typesToSearch.clear();
168 // Set the configFilePath to the first item in the combo
169 if (resourceTypeCmb.getItemCount() > 0) {
170 resourceTypeCmb.select(0);
171 addSearchType(resourceTypeCmb.getText());
174 setPageComplete(isSelectionDone());
176 // Change the visibility of widgets
177 changeVisibility(true);
181 cusResTypeRbtn.addSelectionListener(new SelectionAdapter() {
183 public void widgetSelected(SelectionEvent e) {
184 // Clear the existing items from the search list
185 if (null != typesToSearch)
186 typesToSearch.clear();
188 addSearchType(resTypeTxt.getText());
190 setPageComplete(isSelectionDone());
192 // Change the visibility of widgets
193 changeVisibility(false);
195 resTypeTxt.setFocus();
199 resourceTypeCmb.addSelectionListener(new SelectionAdapter() {
201 public void widgetSelected(SelectionEvent e) {
202 int index = resourceTypeCmb.getSelectionIndex();
206 String resourceType = resourceTypeCmb.getItem(index);
207 addSearchType(resourceType);
208 setPageComplete(isSelectionDone());
212 resTypeTxt.addModifyListener(new ModifyListener() {
214 public void modifyText(ModifyEvent e) {
215 String resourceType = resTypeTxt.getText();
216 if (null != dummyRType) {
217 removeSearchType(dummyRType);
219 dummyRType = resourceType;
220 addSearchType(resourceType);
221 setPageComplete(isSelectionDone());
226 private void changeVisibility(boolean standard) {
227 stdRTypeLbl.setEnabled(standard);
228 resourceTypeCmb.setEnabled(standard);
229 cusRTypeLbl.setEnabled(!standard);
230 resTypeTxt.setEnabled(!standard);
233 private boolean isSelectionDone() {
234 if (null == typesToSearch || typesToSearch.size() < 1) {
240 private void addSearchType(String resourceType) {
241 if (null == resourceType)
243 resourceType = resourceType.trim();
244 if (resourceType.length() < 1) {
247 if (null == typesToSearch) {
248 typesToSearch = new HashSet<String>();
250 typesToSearch.add(resourceType);
253 private void removeSearchType(String resourceType) {
254 if (null == resourceType || null == typesToSearch)
256 resourceType = resourceType.trim();
257 if (resourceType.length() < 1) {
260 typesToSearch.remove(resourceType);
263 public Set<String> getSearchTypes() {
264 return typesToSearch;