1 package oic.simulator.serviceprovider.view.dialogs;
3 import org.eclipse.jface.wizard.WizardPage;
4 import org.eclipse.swt.SWT;
5 import org.eclipse.swt.custom.CCombo;
6 import org.eclipse.swt.events.KeyAdapter;
7 import org.eclipse.swt.events.KeyEvent;
8 import org.eclipse.swt.events.ModifyEvent;
9 import org.eclipse.swt.events.ModifyListener;
10 import org.eclipse.swt.events.SelectionAdapter;
11 import org.eclipse.swt.events.SelectionEvent;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Event;
17 import org.eclipse.swt.widgets.FileDialog;
18 import org.eclipse.swt.widgets.Group;
19 import org.eclipse.swt.widgets.Label;
20 import org.eclipse.swt.widgets.Listener;
21 import org.eclipse.swt.widgets.Text;
22 import org.eclipse.ui.PlatformUI;
24 public class CreateResourcePage extends WizardPage {
26 private Button stdResourceRbtn;
27 private CCombo resourceTypeCmb;
28 private Button cusResourceRbtn;
29 private Text locationTxt;
30 private Button btnBrowse;
31 private Text noOfInstancesText;
32 private Label resourceTypeLbl;
33 private Label noOfInstancesLbl;
34 private Label locationLbl;
36 private String configFilePath = null;
37 private int resourceCount;
39 protected CreateResourcePage() {
40 super("Create Resource");
45 public void createControl(Composite parent) {
46 setPageComplete(false);
47 setTitle("Create Resource");
48 setMessage("Select a standard resource or custom resource to be created");
49 Composite compContent = new Composite(parent, SWT.NONE);
50 GridLayout gridLayout = new GridLayout(1, false);
51 compContent.setLayout(gridLayout);
52 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
53 compContent.setLayoutData(gd);
55 Group configGroup = new Group(compContent, SWT.NONE);
56 gridLayout = new GridLayout(1, false);
57 gridLayout.verticalSpacing = 10;
58 gridLayout.marginTop = 5;
59 configGroup.setLayout(gridLayout);
61 gd.horizontalAlignment = SWT.FILL;
62 gd.grabExcessHorizontalSpace = true;
63 configGroup.setLayoutData(gd);
64 configGroup.setText("Configuration File Type");
66 stdResourceRbtn = new Button(configGroup, SWT.RADIO);
67 stdResourceRbtn.setText("Standard Configuration");
69 Composite stdConfigComp = new Composite(configGroup, SWT.NONE);
70 stdConfigComp.setLayout(new GridLayout(2, false));
72 gd.horizontalAlignment = SWT.FILL;
73 gd.grabExcessHorizontalSpace = true;
74 stdConfigComp.setLayoutData(gd);
76 resourceTypeLbl = new Label(stdConfigComp, SWT.NONE);
77 resourceTypeLbl.setText("ResourceType:");
78 resourceTypeLbl.setEnabled(false);
80 resourceTypeCmb = new CCombo(stdConfigComp, SWT.READ_ONLY | SWT.BORDER);
83 resourceTypeCmb.setLayoutData(gd);
84 resourceTypeCmb.setEnabled(false);
86 cusResourceRbtn = new Button(configGroup, SWT.RADIO);
87 cusResourceRbtn.setText("Custom Configuration");
89 Composite cusConfigComp = new Composite(configGroup, SWT.NONE);
90 cusConfigComp.setLayout(new GridLayout(3, false));
92 gd.horizontalAlignment = SWT.FILL;
93 gd.grabExcessHorizontalSpace = true;
94 cusConfigComp.setLayoutData(gd);
96 locationLbl = new Label(cusConfigComp, SWT.NONE);
97 locationLbl.setText("Location:");
98 locationLbl.setEnabled(false);
100 locationTxt = new Text(cusConfigComp, SWT.BORDER);
102 gd.minimumWidth = 300;
103 gd.horizontalAlignment = SWT.FILL;
104 gd.grabExcessHorizontalSpace = true;
105 locationTxt.setLayoutData(gd);
106 locationTxt.setEnabled(false);
108 btnBrowse = new Button(cusConfigComp, SWT.NONE);
109 btnBrowse.setText("Browse");
112 gd.horizontalAlignment = SWT.FILL;
113 btnBrowse.setLayoutData(gd);
114 btnBrowse.setEnabled(false);
116 Composite instanceComp = new Composite(compContent, SWT.NONE);
117 instanceComp.setLayout(new GridLayout(2, false));
119 gd.horizontalAlignment = SWT.FILL;
120 gd.grabExcessHorizontalSpace = true;
121 instanceComp.setLayoutData(gd);
123 noOfInstancesLbl = new Label(instanceComp, SWT.NONE);
124 noOfInstancesLbl.setText("Number of Instances");
126 noOfInstancesText = new Text(instanceComp, SWT.BORDER);
129 gd.horizontalAlignment = SWT.FILL;
130 noOfInstancesText.setLayoutData(gd);
132 // Adding the default value. It can be changed by the user.
133 noOfInstancesText.setText("1");
139 setControl(compContent);
142 private void populateDataInUI() {
143 // Populate Resourcetype in Combo
144 populateResourceTypeCombo();
147 private void populateResourceTypeCombo() {
149 * List<String> configList; configList =
150 * Activator.getDefault().getManager().getResourceConfigurationList();
151 * if(null != configList) { Iterator<String> itr =
152 * configList.iterator(); while(itr.hasNext()) {
153 * resourceTypeCmb.add(itr.next()); } }
156 // TODO: Temporarily adding a resourceType for testing
157 resourceTypeCmb.add("oic.light");
159 // By default, selecting the first item in the resourceType combo
160 if (resourceTypeCmb.getItemCount() > 0) {
161 resourceTypeCmb.select(0);
162 // TODO: Get the RAML configuration file path of the selected
165 // Activator.getManager().getConfigFilePath(resourceTypeCmb.getItem(0));
169 private void addUIListeners() {
170 stdResourceRbtn.addSelectionListener(new SelectionAdapter() {
172 public void widgetSelected(SelectionEvent e) {
174 // Set the configFilePath to the first item in the combo
175 if (resourceTypeCmb.getItemCount() > 0)
176 resourceTypeCmb.select(0);
178 // TODO: Temporarily adding some random config file path
179 // To be removed after implementing resource configuration
180 configFilePath = "/Samplelight";
182 setPageComplete(isSelectionDone());
184 // Change the visibility of widgets
185 resourceTypeLbl.setEnabled(true);
186 resourceTypeCmb.setEnabled(true);
187 locationLbl.setEnabled(false);
188 locationTxt.setEnabled(false);
189 btnBrowse.setEnabled(false);
194 cusResourceRbtn.addSelectionListener(new SelectionAdapter() {
196 public void widgetSelected(SelectionEvent e) {
197 setPageComplete(isSelectionDone());
199 // Change the visibility of widgets
200 locationLbl.setEnabled(true);
201 locationTxt.setEnabled(true);
202 btnBrowse.setEnabled(true);
203 resourceTypeLbl.setEnabled(false);
204 resourceTypeCmb.setEnabled(false);
209 btnBrowse.addSelectionListener(new SelectionAdapter() {
211 public void widgetSelected(SelectionEvent e) {
212 FileDialog fileDialog = new FileDialog(PlatformUI
213 .getWorkbench().getDisplay().getActiveShell(), SWT.NONE);
214 String configFileAbsolutePath = fileDialog.open();
215 locationTxt.setText(configFileAbsolutePath);
219 resourceTypeCmb.addSelectionListener(new SelectionAdapter() {
221 public void widgetSelected(SelectionEvent e) {
222 int index = resourceTypeCmb.getSelectionIndex();
224 String selectedItem = resourceTypeCmb.getItem(index);
225 if (null != selectedItem) {
226 // TODO: Get the RAML configuration file path of the
229 // Activator.getDefault().getManager().getConfigFilePath(selectedItem);
231 setPageComplete(isSelectionDone());
237 locationTxt.addModifyListener(new ModifyListener() {
239 public void modifyText(ModifyEvent e) {
240 configFilePath = locationTxt.getText();
241 setPageComplete(isSelectionDone());
245 noOfInstancesText.addListener(SWT.Verify, new Listener() {
247 public void handleEvent(Event e) {
248 String string = e.text;
249 char[] chars = new char[string.length()];
250 string.getChars(0, chars.length, chars, 0);
251 for (int i = 0; i < chars.length; i++) {
252 if (!('0' <= chars[i] && chars[i] <= '9')) {
260 noOfInstancesText.addKeyListener(new KeyAdapter() {
262 public void keyReleased(KeyEvent e) {
263 setPageComplete(isSelectionDone());
268 public boolean isSelectionDone() {
269 boolean done = false;
271 resourceCount = Integer.parseInt(noOfInstancesText.getText());
272 } catch (Exception e) {
275 if (cusResourceRbtn.getSelection()) {
276 configFilePath = locationTxt.getText();
279 if (null != configFilePath && configFilePath.length() > 0
280 && resourceCount >= 0) {
286 public String getConfigFilePath() {
287 return configFilePath;
290 public int getResourceCount() {
291 return resourceCount;