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.serviceprovider.view.dialogs;
19 import java.util.Iterator;
22 import oic.simulator.serviceprovider.Activator;
23 import oic.simulator.serviceprovider.utils.Constants;
24 import oic.simulator.serviceprovider.utils.Utility;
26 import org.eclipse.jface.wizard.WizardPage;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.CCombo;
29 import org.eclipse.swt.events.KeyAdapter;
30 import org.eclipse.swt.events.KeyEvent;
31 import org.eclipse.swt.events.ModifyEvent;
32 import org.eclipse.swt.events.ModifyListener;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Event;
40 import org.eclipse.swt.widgets.FileDialog;
41 import org.eclipse.swt.widgets.Group;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Listener;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.ui.PlatformUI;
48 * This class shows UI for creating resources.
50 public class CreateResourcePage extends WizardPage {
52 private Button stdResourceRbtn;
53 private CCombo resourceTypeCmb;
54 private Button cusResourceRbtn;
55 private Text locationTxt;
56 private Button btnBrowse;
57 private Text noOfInstancesText;
58 private Label resourceTypeLbl;
59 private Label noOfInstancesLbl;
60 private Label locationLbl;
62 private String configFilePath = null;
63 private int resourceCount;
65 protected CreateResourcePage() {
66 super("Create Resource");
71 public void createControl(Composite parent) {
72 setPageComplete(false);
73 setTitle(Constants.CREATE_PAGE_TITLE);
74 setMessage(Constants.CREATE_PAGE_MESSAGE);
75 Composite compContent = new Composite(parent, SWT.NONE);
76 GridLayout gridLayout = new GridLayout(1, false);
77 compContent.setLayout(gridLayout);
78 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
79 compContent.setLayoutData(gd);
81 Group configGroup = new Group(compContent, SWT.NONE);
82 gridLayout = new GridLayout(1, false);
83 gridLayout.verticalSpacing = 10;
84 gridLayout.marginTop = 5;
85 configGroup.setLayout(gridLayout);
87 gd.horizontalAlignment = SWT.FILL;
88 gd.grabExcessHorizontalSpace = true;
89 configGroup.setLayoutData(gd);
90 configGroup.setText("Configuration File Type");
92 stdResourceRbtn = new Button(configGroup, SWT.RADIO);
93 stdResourceRbtn.setText("Standard Configuration");
95 Composite stdConfigComp = new Composite(configGroup, SWT.NONE);
96 stdConfigComp.setLayout(new GridLayout(2, false));
98 gd.horizontalAlignment = SWT.FILL;
99 gd.grabExcessHorizontalSpace = true;
100 stdConfigComp.setLayoutData(gd);
102 resourceTypeLbl = new Label(stdConfigComp, SWT.NONE);
103 resourceTypeLbl.setText("ResourceType:");
104 resourceTypeLbl.setEnabled(false);
106 resourceTypeCmb = new CCombo(stdConfigComp, SWT.READ_ONLY | SWT.BORDER);
109 resourceTypeCmb.setLayoutData(gd);
110 resourceTypeCmb.setEnabled(false);
112 cusResourceRbtn = new Button(configGroup, SWT.RADIO);
113 cusResourceRbtn.setText("Custom Configuration");
115 Composite cusConfigComp = new Composite(configGroup, SWT.NONE);
116 cusConfigComp.setLayout(new GridLayout(3, false));
118 gd.horizontalAlignment = SWT.FILL;
119 gd.grabExcessHorizontalSpace = true;
120 cusConfigComp.setLayoutData(gd);
122 locationLbl = new Label(cusConfigComp, SWT.NONE);
123 locationLbl.setText("Location:");
124 locationLbl.setEnabled(false);
126 locationTxt = new Text(cusConfigComp, SWT.BORDER);
128 gd.minimumWidth = 300;
129 gd.horizontalAlignment = SWT.FILL;
130 gd.grabExcessHorizontalSpace = true;
131 locationTxt.setLayoutData(gd);
132 locationTxt.setEnabled(false);
134 btnBrowse = new Button(cusConfigComp, SWT.NONE);
135 btnBrowse.setText("Browse");
138 gd.horizontalAlignment = SWT.FILL;
139 btnBrowse.setLayoutData(gd);
140 btnBrowse.setEnabled(false);
142 Composite instanceComp = new Composite(compContent, SWT.NONE);
143 instanceComp.setLayout(new GridLayout(2, false));
145 gd.horizontalAlignment = SWT.FILL;
146 gd.grabExcessHorizontalSpace = true;
147 instanceComp.setLayoutData(gd);
149 noOfInstancesLbl = new Label(instanceComp, SWT.NONE);
150 noOfInstancesLbl.setText("Number of Instances");
152 noOfInstancesText = new Text(instanceComp, SWT.BORDER);
155 gd.horizontalAlignment = SWT.FILL;
156 noOfInstancesText.setLayoutData(gd);
158 // Adding the default value. It can be changed by the user.
159 noOfInstancesText.setText("1");
165 setControl(compContent);
168 private void populateDataInUI() {
169 // Populate Resource-type in Combo
170 populateResourceTypeCombo();
173 private void populateResourceTypeCombo() {
174 Map<String, String> configMap;
175 configMap = Activator.getDefault().getResourceManager()
176 .getResourceConfigurationList();
177 if (null != configMap) {
178 Iterator<String> itr = configMap.keySet().iterator();
181 while (itr.hasNext()) {
182 fileName = itr.next();
183 shortName = Utility.fileNameToDisplay(fileName);
184 if (null == shortName) {
187 resourceTypeCmb.add(shortName);
191 // By default, selecting the first item in the resourceType combo
195 private void addUIListeners() {
196 stdResourceRbtn.addSelectionListener(new SelectionAdapter() {
198 public void widgetSelected(SelectionEvent e) {
200 // Set the configFilePath to the first item in the combo
203 setPageComplete(isSelectionDone());
205 // Change the visibility of widgets
206 resourceTypeLbl.setEnabled(true);
207 resourceTypeCmb.setEnabled(true);
208 locationLbl.setEnabled(false);
209 locationTxt.setEnabled(false);
210 btnBrowse.setEnabled(false);
215 cusResourceRbtn.addSelectionListener(new SelectionAdapter() {
217 public void widgetSelected(SelectionEvent e) {
218 setPageComplete(isSelectionDone());
220 // Change the visibility of widgets
221 locationLbl.setEnabled(true);
222 locationTxt.setEnabled(true);
223 btnBrowse.setEnabled(true);
224 resourceTypeLbl.setEnabled(false);
225 resourceTypeCmb.setEnabled(false);
230 btnBrowse.addSelectionListener(new SelectionAdapter() {
232 public void widgetSelected(SelectionEvent e) {
233 FileDialog fileDialog = new FileDialog(PlatformUI
234 .getWorkbench().getDisplay().getActiveShell(), SWT.NONE);
236 .setFilterExtensions(Constants.BROWSE_RAML_FILTER_EXTENSIONS);
237 String configFileAbsolutePath = fileDialog.open();
238 locationTxt.setText(configFileAbsolutePath);
242 resourceTypeCmb.addSelectionListener(new SelectionAdapter() {
244 public void widgetSelected(SelectionEvent e) {
245 int index = resourceTypeCmb.getSelectionIndex();
247 String selectedItem = resourceTypeCmb.getItem(index);
248 if (null != selectedItem) {
249 // Convert the selectedItem to the fully qualified file
251 selectedItem = Utility.displayToFileName(selectedItem);
253 // Get the RAML configuration file path of the selected
255 configFilePath = Activator.getDefault()
256 .getResourceManager()
257 .getConfigFilePath(selectedItem);
259 setPageComplete(isSelectionDone());
265 locationTxt.addModifyListener(new ModifyListener() {
267 public void modifyText(ModifyEvent e) {
268 configFilePath = locationTxt.getText();
269 setPageComplete(isSelectionDone());
273 noOfInstancesText.addListener(SWT.Verify, new Listener() {
275 public void handleEvent(Event e) {
276 String string = e.text;
277 char[] chars = new char[string.length()];
278 string.getChars(0, chars.length, chars, 0);
279 for (int i = 0; i < chars.length; i++) {
280 if (!('0' <= chars[i] && chars[i] <= '9')) {
288 noOfInstancesText.addKeyListener(new KeyAdapter() {
290 public void keyReleased(KeyEvent e) {
291 setPageComplete(isSelectionDone());
296 private void selectInitialItem() {
297 if (resourceTypeCmb.getItemCount() > 0) {
298 resourceTypeCmb.select(0);
299 String fileName = Utility.displayToFileName(resourceTypeCmb
301 configFilePath = Activator.getDefault().getResourceManager()
302 .getConfigFilePath(fileName);
306 private boolean isSelectionDone() {
307 boolean done = false;
309 resourceCount = Integer.parseInt(noOfInstancesText.getText());
310 } catch (Exception e) {
313 if (cusResourceRbtn.getSelection()) {
314 configFilePath = locationTxt.getText();
317 if (null != configFilePath && configFilePath.length() > 0
318 && resourceCount >= 1) {
324 public String getConfigFilePath() {
325 return configFilePath;
328 public int getResourceCount() {
329 return resourceCount;