1 package oic.simulator.serviceprovider.view.dialogs;
3 import java.util.Iterator;
6 import oic.simulator.serviceprovider.Activator;
7 import oic.simulator.serviceprovider.utils.Constants;
8 import oic.simulator.serviceprovider.utils.Utility;
10 import org.eclipse.jface.wizard.WizardPage;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.custom.CCombo;
13 import org.eclipse.swt.events.KeyAdapter;
14 import org.eclipse.swt.events.KeyEvent;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Event;
24 import org.eclipse.swt.widgets.FileDialog;
25 import org.eclipse.swt.widgets.Group;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Listener;
28 import org.eclipse.swt.widgets.Text;
29 import org.eclipse.ui.PlatformUI;
31 public class CreateResourcePage extends WizardPage {
33 private Button stdResourceRbtn;
34 private CCombo resourceTypeCmb;
35 private Button cusResourceRbtn;
36 private Text locationTxt;
37 private Button btnBrowse;
38 private Text noOfInstancesText;
39 private Label resourceTypeLbl;
40 private Label noOfInstancesLbl;
41 private Label locationLbl;
43 private String configFilePath = null;
44 private int resourceCount;
46 protected CreateResourcePage() {
47 super("Create Resource");
52 public void createControl(Composite parent) {
53 setPageComplete(false);
54 setTitle(Constants.CREATE_PAGE_TITLE);
55 setMessage(Constants.CREATE_PAGE_MESSAGE);
56 Composite compContent = new Composite(parent, SWT.NONE);
57 GridLayout gridLayout = new GridLayout(1, false);
58 compContent.setLayout(gridLayout);
59 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
60 compContent.setLayoutData(gd);
62 Group configGroup = new Group(compContent, SWT.NONE);
63 gridLayout = new GridLayout(1, false);
64 gridLayout.verticalSpacing = 10;
65 gridLayout.marginTop = 5;
66 configGroup.setLayout(gridLayout);
68 gd.horizontalAlignment = SWT.FILL;
69 gd.grabExcessHorizontalSpace = true;
70 configGroup.setLayoutData(gd);
71 configGroup.setText("Configuration File Type");
73 stdResourceRbtn = new Button(configGroup, SWT.RADIO);
74 stdResourceRbtn.setText("Standard Configuration");
76 Composite stdConfigComp = new Composite(configGroup, SWT.NONE);
77 stdConfigComp.setLayout(new GridLayout(2, false));
79 gd.horizontalAlignment = SWT.FILL;
80 gd.grabExcessHorizontalSpace = true;
81 stdConfigComp.setLayoutData(gd);
83 resourceTypeLbl = new Label(stdConfigComp, SWT.NONE);
84 resourceTypeLbl.setText("ResourceType:");
85 resourceTypeLbl.setEnabled(false);
87 resourceTypeCmb = new CCombo(stdConfigComp, SWT.READ_ONLY | SWT.BORDER);
90 resourceTypeCmb.setLayoutData(gd);
91 resourceTypeCmb.setEnabled(false);
93 cusResourceRbtn = new Button(configGroup, SWT.RADIO);
94 cusResourceRbtn.setText("Custom Configuration");
96 Composite cusConfigComp = new Composite(configGroup, SWT.NONE);
97 cusConfigComp.setLayout(new GridLayout(3, false));
99 gd.horizontalAlignment = SWT.FILL;
100 gd.grabExcessHorizontalSpace = true;
101 cusConfigComp.setLayoutData(gd);
103 locationLbl = new Label(cusConfigComp, SWT.NONE);
104 locationLbl.setText("Location:");
105 locationLbl.setEnabled(false);
107 locationTxt = new Text(cusConfigComp, SWT.BORDER);
109 gd.minimumWidth = 300;
110 gd.horizontalAlignment = SWT.FILL;
111 gd.grabExcessHorizontalSpace = true;
112 locationTxt.setLayoutData(gd);
113 locationTxt.setEnabled(false);
115 btnBrowse = new Button(cusConfigComp, SWT.NONE);
116 btnBrowse.setText("Browse");
119 gd.horizontalAlignment = SWT.FILL;
120 btnBrowse.setLayoutData(gd);
121 btnBrowse.setEnabled(false);
123 Composite instanceComp = new Composite(compContent, SWT.NONE);
124 instanceComp.setLayout(new GridLayout(2, false));
126 gd.horizontalAlignment = SWT.FILL;
127 gd.grabExcessHorizontalSpace = true;
128 instanceComp.setLayoutData(gd);
130 noOfInstancesLbl = new Label(instanceComp, SWT.NONE);
131 noOfInstancesLbl.setText("Number of Instances");
133 noOfInstancesText = new Text(instanceComp, SWT.BORDER);
136 gd.horizontalAlignment = SWT.FILL;
137 noOfInstancesText.setLayoutData(gd);
139 // Adding the default value. It can be changed by the user.
140 noOfInstancesText.setText("1");
146 setControl(compContent);
149 private void populateDataInUI() {
150 // Populate Resource-type in Combo
151 populateResourceTypeCombo();
154 private void populateResourceTypeCombo() {
155 Map<String, String> configMap;
156 configMap = Activator.getDefault().getResourceManager()
157 .getResourceConfigurationList();
158 if (null != configMap) {
159 Iterator<String> itr = configMap.keySet().iterator();
162 while (itr.hasNext()) {
163 fileName = itr.next();
164 shortName = Utility.fileNameToDisplay(fileName);
165 if (null == shortName) {
168 resourceTypeCmb.add(shortName);
172 // By default, selecting the first item in the resourceType combo
176 private void addUIListeners() {
177 stdResourceRbtn.addSelectionListener(new SelectionAdapter() {
179 public void widgetSelected(SelectionEvent e) {
181 // Set the configFilePath to the first item in the combo
184 setPageComplete(isSelectionDone());
186 // Change the visibility of widgets
187 resourceTypeLbl.setEnabled(true);
188 resourceTypeCmb.setEnabled(true);
189 locationLbl.setEnabled(false);
190 locationTxt.setEnabled(false);
191 btnBrowse.setEnabled(false);
196 cusResourceRbtn.addSelectionListener(new SelectionAdapter() {
198 public void widgetSelected(SelectionEvent e) {
199 setPageComplete(isSelectionDone());
201 // Change the visibility of widgets
202 locationLbl.setEnabled(true);
203 locationTxt.setEnabled(true);
204 btnBrowse.setEnabled(true);
205 resourceTypeLbl.setEnabled(false);
206 resourceTypeCmb.setEnabled(false);
211 btnBrowse.addSelectionListener(new SelectionAdapter() {
213 public void widgetSelected(SelectionEvent e) {
214 FileDialog fileDialog = new FileDialog(PlatformUI
215 .getWorkbench().getDisplay().getActiveShell(), SWT.NONE);
217 .setFilterExtensions(Constants.BROWSE_RAML_FILTER_EXTENSIONS);
218 String configFileAbsolutePath = fileDialog.open();
219 locationTxt.setText(configFileAbsolutePath);
223 resourceTypeCmb.addSelectionListener(new SelectionAdapter() {
225 public void widgetSelected(SelectionEvent e) {
226 int index = resourceTypeCmb.getSelectionIndex();
228 String selectedItem = resourceTypeCmb.getItem(index);
229 if (null != selectedItem) {
230 // Convert the selectedItem to the fully qualified file
232 selectedItem = Utility.displayToFileName(selectedItem);
234 // Get the RAML configuration file path of the selected
236 configFilePath = Activator.getDefault()
237 .getResourceManager()
238 .getConfigFilePath(selectedItem);
240 setPageComplete(isSelectionDone());
246 locationTxt.addModifyListener(new ModifyListener() {
248 public void modifyText(ModifyEvent e) {
249 configFilePath = locationTxt.getText();
250 setPageComplete(isSelectionDone());
254 noOfInstancesText.addListener(SWT.Verify, new Listener() {
256 public void handleEvent(Event e) {
257 String string = e.text;
258 char[] chars = new char[string.length()];
259 string.getChars(0, chars.length, chars, 0);
260 for (int i = 0; i < chars.length; i++) {
261 if (!('0' <= chars[i] && chars[i] <= '9')) {
269 noOfInstancesText.addKeyListener(new KeyAdapter() {
271 public void keyReleased(KeyEvent e) {
272 setPageComplete(isSelectionDone());
277 private void selectInitialItem() {
278 if (resourceTypeCmb.getItemCount() > 0) {
279 resourceTypeCmb.select(0);
280 String fileName = Utility.displayToFileName(resourceTypeCmb
282 configFilePath = Activator.getDefault().getResourceManager()
283 .getConfigFilePath(fileName);
287 private boolean isSelectionDone() {
288 boolean done = false;
290 resourceCount = Integer.parseInt(noOfInstancesText.getText());
291 } catch (Exception e) {
294 if (cusResourceRbtn.getSelection()) {
295 configFilePath = locationTxt.getText();
298 if (null != configFilePath && configFilePath.length() > 0
299 && resourceCount >= 1) {
305 public String getConfigFilePath() {
306 return configFilePath;
309 public int getResourceCount() {
310 return resourceCount;