11a305dd42dacbefe760e850a0e236515e4e4750
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / CreateResourcePage.java
1 package oic.simulator.serviceprovider.view.dialogs;
2
3 import java.util.Iterator;
4 import java.util.Map;
5
6 import oic.simulator.serviceprovider.Activator;
7 import oic.simulator.serviceprovider.utils.Constants;
8 import oic.simulator.serviceprovider.utils.Utility;
9
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;
30
31 public class CreateResourcePage extends WizardPage {
32
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;
42
43     private String configFilePath = null;
44     private int    resourceCount;
45
46     protected CreateResourcePage() {
47         super("Create Resource");
48         resourceCount = -1;
49     }
50
51     @Override
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);
61
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);
67         gd = new GridData();
68         gd.horizontalAlignment = SWT.FILL;
69         gd.grabExcessHorizontalSpace = true;
70         configGroup.setLayoutData(gd);
71         configGroup.setText("Configuration File Type");
72
73         stdResourceRbtn = new Button(configGroup, SWT.RADIO);
74         stdResourceRbtn.setText("Standard Configuration");
75
76         Composite stdConfigComp = new Composite(configGroup, SWT.NONE);
77         stdConfigComp.setLayout(new GridLayout(2, false));
78         gd = new GridData();
79         gd.horizontalAlignment = SWT.FILL;
80         gd.grabExcessHorizontalSpace = true;
81         stdConfigComp.setLayoutData(gd);
82
83         resourceTypeLbl = new Label(stdConfigComp, SWT.NONE);
84         resourceTypeLbl.setText("ResourceType:");
85         resourceTypeLbl.setEnabled(false);
86
87         resourceTypeCmb = new CCombo(stdConfigComp, SWT.READ_ONLY | SWT.BORDER);
88         gd = new GridData();
89         gd.widthHint = 150;
90         resourceTypeCmb.setLayoutData(gd);
91         resourceTypeCmb.setEnabled(false);
92
93         cusResourceRbtn = new Button(configGroup, SWT.RADIO);
94         cusResourceRbtn.setText("Custom Configuration");
95
96         Composite cusConfigComp = new Composite(configGroup, SWT.NONE);
97         cusConfigComp.setLayout(new GridLayout(3, false));
98         gd = new GridData();
99         gd.horizontalAlignment = SWT.FILL;
100         gd.grabExcessHorizontalSpace = true;
101         cusConfigComp.setLayoutData(gd);
102
103         locationLbl = new Label(cusConfigComp, SWT.NONE);
104         locationLbl.setText("Location:");
105         locationLbl.setEnabled(false);
106
107         locationTxt = new Text(cusConfigComp, SWT.BORDER);
108         gd = new GridData();
109         gd.minimumWidth = 300;
110         gd.horizontalAlignment = SWT.FILL;
111         gd.grabExcessHorizontalSpace = true;
112         locationTxt.setLayoutData(gd);
113         locationTxt.setEnabled(false);
114
115         btnBrowse = new Button(cusConfigComp, SWT.NONE);
116         btnBrowse.setText("Browse");
117         gd = new GridData();
118         gd.widthHint = 80;
119         gd.horizontalAlignment = SWT.FILL;
120         btnBrowse.setLayoutData(gd);
121         btnBrowse.setEnabled(false);
122
123         Composite instanceComp = new Composite(compContent, SWT.NONE);
124         instanceComp.setLayout(new GridLayout(2, false));
125         gd = new GridData();
126         gd.horizontalAlignment = SWT.FILL;
127         gd.grabExcessHorizontalSpace = true;
128         instanceComp.setLayoutData(gd);
129
130         noOfInstancesLbl = new Label(instanceComp, SWT.NONE);
131         noOfInstancesLbl.setText("Number of Instances");
132
133         noOfInstancesText = new Text(instanceComp, SWT.BORDER);
134         gd = new GridData();
135         gd.widthHint = 50;
136         gd.horizontalAlignment = SWT.FILL;
137         noOfInstancesText.setLayoutData(gd);
138
139         // Adding the default value. It can be changed by the user.
140         noOfInstancesText.setText("1");
141
142         populateDataInUI();
143
144         addUIListeners();
145
146         setControl(compContent);
147     }
148
149     private void populateDataInUI() {
150         // Populate Resource-type in Combo
151         populateResourceTypeCombo();
152     }
153
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();
160             String fileName;
161             String shortName;
162             while (itr.hasNext()) {
163                 fileName = itr.next();
164                 shortName = Utility.fileNameToDisplay(fileName);
165                 if (null == shortName) {
166                     continue;
167                 }
168                 resourceTypeCmb.add(shortName);
169             }
170         }
171
172         // By default, selecting the first item in the resourceType combo
173         selectInitialItem();
174     }
175
176     private void addUIListeners() {
177         stdResourceRbtn.addSelectionListener(new SelectionAdapter() {
178             @Override
179             public void widgetSelected(SelectionEvent e) {
180
181                 // Set the configFilePath to the first item in the combo
182                 selectInitialItem();
183
184                 setPageComplete(isSelectionDone());
185
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);
192
193             }
194         });
195
196         cusResourceRbtn.addSelectionListener(new SelectionAdapter() {
197             @Override
198             public void widgetSelected(SelectionEvent e) {
199                 setPageComplete(isSelectionDone());
200
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);
207
208             }
209         });
210
211         btnBrowse.addSelectionListener(new SelectionAdapter() {
212             @Override
213             public void widgetSelected(SelectionEvent e) {
214                 FileDialog fileDialog = new FileDialog(PlatformUI
215                         .getWorkbench().getDisplay().getActiveShell(), SWT.NONE);
216                 fileDialog
217                         .setFilterExtensions(Constants.BROWSE_RAML_FILTER_EXTENSIONS);
218                 String configFileAbsolutePath = fileDialog.open();
219                 locationTxt.setText(configFileAbsolutePath);
220             }
221         });
222
223         resourceTypeCmb.addSelectionListener(new SelectionAdapter() {
224             @Override
225             public void widgetSelected(SelectionEvent e) {
226                 int index = resourceTypeCmb.getSelectionIndex();
227                 if (index >= 0) {
228                     String selectedItem = resourceTypeCmb.getItem(index);
229                     if (null != selectedItem) {
230                         // Convert the selectedItem to the fully qualified file
231                         // name.
232                         selectedItem = Utility.displayToFileName(selectedItem);
233
234                         // Get the RAML configuration file path of the selected
235                         // resource
236                         configFilePath = Activator.getDefault()
237                                 .getResourceManager()
238                                 .getConfigFilePath(selectedItem);
239
240                         setPageComplete(isSelectionDone());
241                     }
242                 }
243             }
244         });
245
246         locationTxt.addModifyListener(new ModifyListener() {
247             @Override
248             public void modifyText(ModifyEvent e) {
249                 configFilePath = locationTxt.getText();
250                 setPageComplete(isSelectionDone());
251             }
252         });
253
254         noOfInstancesText.addListener(SWT.Verify, new Listener() {
255             @Override
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')) {
262                         e.doit = false;
263                         return;
264                     }
265                 }
266             }
267         });
268
269         noOfInstancesText.addKeyListener(new KeyAdapter() {
270             @Override
271             public void keyReleased(KeyEvent e) {
272                 setPageComplete(isSelectionDone());
273             }
274         });
275     }
276
277     private void selectInitialItem() {
278         if (resourceTypeCmb.getItemCount() > 0) {
279             resourceTypeCmb.select(0);
280             String fileName = Utility.displayToFileName(resourceTypeCmb
281                     .getText());
282             configFilePath = Activator.getDefault().getResourceManager()
283                     .getConfigFilePath(fileName);
284         }
285     }
286
287     private boolean isSelectionDone() {
288         boolean done = false;
289         try {
290             resourceCount = Integer.parseInt(noOfInstancesText.getText());
291         } catch (Exception e) {
292             resourceCount = -1;
293         }
294         if (cusResourceRbtn.getSelection()) {
295             configFilePath = locationTxt.getText();
296         }
297
298         if (null != configFilePath && configFilePath.length() > 0
299                 && resourceCount >= 1) {
300             done = true;
301         }
302         return done;
303     }
304
305     public String getConfigFilePath() {
306         return configFilePath;
307     }
308
309     public int getResourceCount() {
310         return resourceCount;
311     }
312 }