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 resourceTypeCmb.add("sample.light");
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
155 // Activator.getManager().getConfigFilePath(resourceTypeCmb.getItem(0));
159 private void addUIListeners() {
160 stdResTypeRbtn.addSelectionListener(new SelectionAdapter() {
162 public void widgetSelected(SelectionEvent e) {
163 // Clear the existing items from the search list
164 if (null != typesToSearch)
165 typesToSearch.clear();
167 // Set the configFilePath to the first item in the combo
168 if (resourceTypeCmb.getItemCount() > 0) {
169 resourceTypeCmb.select(0);
170 addSearchType(resourceTypeCmb.getText());
173 setPageComplete(isSelectionDone());
175 // Change the visibility of widgets
176 changeVisibility(true);
180 cusResTypeRbtn.addSelectionListener(new SelectionAdapter() {
182 public void widgetSelected(SelectionEvent e) {
183 // Clear the existing items from the search list
184 if (null != typesToSearch)
185 typesToSearch.clear();
187 addSearchType(resTypeTxt.getText());
189 setPageComplete(isSelectionDone());
191 // Change the visibility of widgets
192 changeVisibility(false);
194 resTypeTxt.setFocus();
198 resourceTypeCmb.addSelectionListener(new SelectionAdapter() {
200 public void widgetSelected(SelectionEvent e) {
201 int index = resourceTypeCmb.getSelectionIndex();
205 String resourceType = resourceTypeCmb.getItem(index);
206 addSearchType(resourceType);
207 setPageComplete(isSelectionDone());
211 resTypeTxt.addModifyListener(new ModifyListener() {
213 public void modifyText(ModifyEvent e) {
214 String resourceType = resTypeTxt.getText();
215 if (null != dummyRType) {
216 removeSearchType(dummyRType);
218 dummyRType = resourceType;
219 addSearchType(resourceType);
220 setPageComplete(isSelectionDone());
225 private void changeVisibility(boolean standard) {
226 stdRTypeLbl.setEnabled(standard);
227 resourceTypeCmb.setEnabled(standard);
228 cusRTypeLbl.setEnabled(!standard);
229 resTypeTxt.setEnabled(!standard);
232 private boolean isSelectionDone() {
233 if (null == typesToSearch || typesToSearch.size() < 1) {
239 private void addSearchType(String resourceType) {
240 if (null == resourceType)
242 resourceType = resourceType.trim();
243 if (resourceType.length() < 1) {
246 if (null == typesToSearch) {
247 typesToSearch = new HashSet<String>();
249 typesToSearch.add(resourceType);
252 private void removeSearchType(String resourceType) {
253 if (null == resourceType || null == typesToSearch)
255 resourceType = resourceType.trim();
256 if (resourceType.length() < 1) {
259 typesToSearch.remove(resourceType);
262 public Set<String> getSearchTypes() {
263 return typesToSearch;