Base layout of eclipse plugin for service provider.
[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 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;
23
24 public class CreateResourcePage extends WizardPage {
25
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;
35
36     private String configFilePath = null;
37     private int    resourceCount;
38
39     protected CreateResourcePage() {
40         super("Create Resource");
41         resourceCount = -1;
42     }
43
44     @Override
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);
54
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);
60         gd = new GridData();
61         gd.horizontalAlignment = SWT.FILL;
62         gd.grabExcessHorizontalSpace = true;
63         configGroup.setLayoutData(gd);
64         configGroup.setText("Configuration File Type");
65
66         stdResourceRbtn = new Button(configGroup, SWT.RADIO);
67         stdResourceRbtn.setText("Standard Configuration");
68
69         Composite stdConfigComp = new Composite(configGroup, SWT.NONE);
70         stdConfigComp.setLayout(new GridLayout(2, false));
71         gd = new GridData();
72         gd.horizontalAlignment = SWT.FILL;
73         gd.grabExcessHorizontalSpace = true;
74         stdConfigComp.setLayoutData(gd);
75
76         resourceTypeLbl = new Label(stdConfigComp, SWT.NONE);
77         resourceTypeLbl.setText("ResourceType:");
78         resourceTypeLbl.setEnabled(false);
79
80         resourceTypeCmb = new CCombo(stdConfigComp, SWT.READ_ONLY | SWT.BORDER);
81         gd = new GridData();
82         gd.widthHint = 150;
83         resourceTypeCmb.setLayoutData(gd);
84         resourceTypeCmb.setEnabled(false);
85
86         cusResourceRbtn = new Button(configGroup, SWT.RADIO);
87         cusResourceRbtn.setText("Custom Configuration");
88
89         Composite cusConfigComp = new Composite(configGroup, SWT.NONE);
90         cusConfigComp.setLayout(new GridLayout(3, false));
91         gd = new GridData();
92         gd.horizontalAlignment = SWT.FILL;
93         gd.grabExcessHorizontalSpace = true;
94         cusConfigComp.setLayoutData(gd);
95
96         locationLbl = new Label(cusConfigComp, SWT.NONE);
97         locationLbl.setText("Location:");
98         locationLbl.setEnabled(false);
99
100         locationTxt = new Text(cusConfigComp, SWT.BORDER);
101         gd = new GridData();
102         gd.minimumWidth = 300;
103         gd.horizontalAlignment = SWT.FILL;
104         gd.grabExcessHorizontalSpace = true;
105         locationTxt.setLayoutData(gd);
106         locationTxt.setEnabled(false);
107
108         btnBrowse = new Button(cusConfigComp, SWT.NONE);
109         btnBrowse.setText("Browse");
110         gd = new GridData();
111         gd.widthHint = 80;
112         gd.horizontalAlignment = SWT.FILL;
113         btnBrowse.setLayoutData(gd);
114         btnBrowse.setEnabled(false);
115
116         Composite instanceComp = new Composite(compContent, SWT.NONE);
117         instanceComp.setLayout(new GridLayout(2, false));
118         gd = new GridData();
119         gd.horizontalAlignment = SWT.FILL;
120         gd.grabExcessHorizontalSpace = true;
121         instanceComp.setLayoutData(gd);
122
123         noOfInstancesLbl = new Label(instanceComp, SWT.NONE);
124         noOfInstancesLbl.setText("Number of Instances");
125
126         noOfInstancesText = new Text(instanceComp, SWT.BORDER);
127         gd = new GridData();
128         gd.widthHint = 50;
129         gd.horizontalAlignment = SWT.FILL;
130         noOfInstancesText.setLayoutData(gd);
131
132         // Adding the default value. It can be changed by the user.
133         noOfInstancesText.setText("1");
134
135         populateDataInUI();
136
137         addUIListeners();
138
139         setControl(compContent);
140     }
141
142     private void populateDataInUI() {
143         // Populate Resourcetype in Combo
144         populateResourceTypeCombo();
145     }
146
147     private void populateResourceTypeCombo() {
148         /*
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()); } }
154          */
155
156         // TODO: Temporarily adding a resourceType for testing
157         resourceTypeCmb.add("oic.light");
158
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
163             // resource
164             // configFilePath =
165             // Activator.getManager().getConfigFilePath(resourceTypeCmb.getItem(0));
166         }
167     }
168
169     private void addUIListeners() {
170         stdResourceRbtn.addSelectionListener(new SelectionAdapter() {
171             @Override
172             public void widgetSelected(SelectionEvent e) {
173
174                 // Set the configFilePath to the first item in the combo
175                 if (resourceTypeCmb.getItemCount() > 0)
176                     resourceTypeCmb.select(0);
177
178                 // TODO: Temporarily adding some random config file path
179                 // To be removed after implementing resource configuration
180                 configFilePath = "/Samplelight";
181
182                 setPageComplete(isSelectionDone());
183
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);
190
191             }
192         });
193
194         cusResourceRbtn.addSelectionListener(new SelectionAdapter() {
195             @Override
196             public void widgetSelected(SelectionEvent e) {
197                 setPageComplete(isSelectionDone());
198
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);
205
206             }
207         });
208
209         btnBrowse.addSelectionListener(new SelectionAdapter() {
210             @Override
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);
216             }
217         });
218
219         resourceTypeCmb.addSelectionListener(new SelectionAdapter() {
220             @Override
221             public void widgetSelected(SelectionEvent e) {
222                 int index = resourceTypeCmb.getSelectionIndex();
223                 if (index >= 0) {
224                     String selectedItem = resourceTypeCmb.getItem(index);
225                     if (null != selectedItem) {
226                         // TODO: Get the RAML configuration file path of the
227                         // selected resource
228                         // configFilePath =
229                         // Activator.getDefault().getManager().getConfigFilePath(selectedItem);
230
231                         setPageComplete(isSelectionDone());
232                     }
233                 }
234             }
235         });
236
237         locationTxt.addModifyListener(new ModifyListener() {
238             @Override
239             public void modifyText(ModifyEvent e) {
240                 configFilePath = locationTxt.getText();
241                 setPageComplete(isSelectionDone());
242             }
243         });
244
245         noOfInstancesText.addListener(SWT.Verify, new Listener() {
246             @Override
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')) {
253                         e.doit = false;
254                         return;
255                     }
256                 }
257             }
258         });
259
260         noOfInstancesText.addKeyListener(new KeyAdapter() {
261             @Override
262             public void keyReleased(KeyEvent e) {
263                 setPageComplete(isSelectionDone());
264             }
265         });
266     }
267
268     public boolean isSelectionDone() {
269         boolean done = false;
270         try {
271             resourceCount = Integer.parseInt(noOfInstancesText.getText());
272         } catch (Exception e) {
273             resourceCount = -1;
274         }
275         if (cusResourceRbtn.getSelection()) {
276             configFilePath = locationTxt.getText();
277         }
278
279         if (null != configFilePath && configFilePath.length() > 0
280                 && resourceCount >= 0) {
281             done = true;
282         }
283         return done;
284     }
285
286     public String getConfigFilePath() {
287         return configFilePath;
288     }
289
290     public int getResourceCount() {
291         return resourceCount;
292     }
293
294 }