Java SDK and Eclipse plugin for simulator.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / CreateResourcePage.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.serviceprovider.view.dialogs;
18
19 import java.util.Iterator;
20 import java.util.Map;
21
22 import oic.simulator.serviceprovider.Activator;
23 import oic.simulator.serviceprovider.utils.Constants;
24 import oic.simulator.serviceprovider.utils.Utility;
25
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;
46
47 /**
48  * This class shows UI for creating resources.
49  */
50 public class CreateResourcePage extends WizardPage {
51
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;
61
62     private String configFilePath = null;
63     private int    resourceCount;
64
65     protected CreateResourcePage() {
66         super("Create Resource");
67         resourceCount = -1;
68     }
69
70     @Override
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);
80
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);
86         gd = new GridData();
87         gd.horizontalAlignment = SWT.FILL;
88         gd.grabExcessHorizontalSpace = true;
89         configGroup.setLayoutData(gd);
90         configGroup.setText("Configuration File Type");
91
92         stdResourceRbtn = new Button(configGroup, SWT.RADIO);
93         stdResourceRbtn.setText("Standard Configuration");
94
95         Composite stdConfigComp = new Composite(configGroup, SWT.NONE);
96         stdConfigComp.setLayout(new GridLayout(2, false));
97         gd = new GridData();
98         gd.horizontalAlignment = SWT.FILL;
99         gd.grabExcessHorizontalSpace = true;
100         stdConfigComp.setLayoutData(gd);
101
102         resourceTypeLbl = new Label(stdConfigComp, SWT.NONE);
103         resourceTypeLbl.setText("ResourceType:");
104         resourceTypeLbl.setEnabled(false);
105
106         resourceTypeCmb = new CCombo(stdConfigComp, SWT.READ_ONLY | SWT.BORDER);
107         gd = new GridData();
108         gd.widthHint = 150;
109         resourceTypeCmb.setLayoutData(gd);
110         resourceTypeCmb.setEnabled(false);
111
112         cusResourceRbtn = new Button(configGroup, SWT.RADIO);
113         cusResourceRbtn.setText("Custom Configuration");
114
115         Composite cusConfigComp = new Composite(configGroup, SWT.NONE);
116         cusConfigComp.setLayout(new GridLayout(3, false));
117         gd = new GridData();
118         gd.horizontalAlignment = SWT.FILL;
119         gd.grabExcessHorizontalSpace = true;
120         cusConfigComp.setLayoutData(gd);
121
122         locationLbl = new Label(cusConfigComp, SWT.NONE);
123         locationLbl.setText("Location:");
124         locationLbl.setEnabled(false);
125
126         locationTxt = new Text(cusConfigComp, SWT.BORDER);
127         gd = new GridData();
128         gd.minimumWidth = 300;
129         gd.horizontalAlignment = SWT.FILL;
130         gd.grabExcessHorizontalSpace = true;
131         locationTxt.setLayoutData(gd);
132         locationTxt.setEnabled(false);
133
134         btnBrowse = new Button(cusConfigComp, SWT.NONE);
135         btnBrowse.setText("Browse");
136         gd = new GridData();
137         gd.widthHint = 80;
138         gd.horizontalAlignment = SWT.FILL;
139         btnBrowse.setLayoutData(gd);
140         btnBrowse.setEnabled(false);
141
142         Composite instanceComp = new Composite(compContent, SWT.NONE);
143         instanceComp.setLayout(new GridLayout(2, false));
144         gd = new GridData();
145         gd.horizontalAlignment = SWT.FILL;
146         gd.grabExcessHorizontalSpace = true;
147         instanceComp.setLayoutData(gd);
148
149         noOfInstancesLbl = new Label(instanceComp, SWT.NONE);
150         noOfInstancesLbl.setText("Number of Instances");
151
152         noOfInstancesText = new Text(instanceComp, SWT.BORDER);
153         gd = new GridData();
154         gd.widthHint = 50;
155         gd.horizontalAlignment = SWT.FILL;
156         noOfInstancesText.setLayoutData(gd);
157
158         // Adding the default value. It can be changed by the user.
159         noOfInstancesText.setText("1");
160
161         populateDataInUI();
162
163         addUIListeners();
164
165         setControl(compContent);
166     }
167
168     private void populateDataInUI() {
169         // Populate Resource-type in Combo
170         populateResourceTypeCombo();
171     }
172
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();
179             String fileName;
180             String shortName;
181             while (itr.hasNext()) {
182                 fileName = itr.next();
183                 shortName = Utility.fileNameToDisplay(fileName);
184                 if (null == shortName) {
185                     continue;
186                 }
187                 resourceTypeCmb.add(shortName);
188             }
189         }
190
191         // By default, selecting the first item in the resourceType combo
192         selectInitialItem();
193     }
194
195     private void addUIListeners() {
196         stdResourceRbtn.addSelectionListener(new SelectionAdapter() {
197             @Override
198             public void widgetSelected(SelectionEvent e) {
199
200                 // Set the configFilePath to the first item in the combo
201                 selectInitialItem();
202
203                 setPageComplete(isSelectionDone());
204
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);
211
212             }
213         });
214
215         cusResourceRbtn.addSelectionListener(new SelectionAdapter() {
216             @Override
217             public void widgetSelected(SelectionEvent e) {
218                 setPageComplete(isSelectionDone());
219
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);
226
227             }
228         });
229
230         btnBrowse.addSelectionListener(new SelectionAdapter() {
231             @Override
232             public void widgetSelected(SelectionEvent e) {
233                 FileDialog fileDialog = new FileDialog(PlatformUI
234                         .getWorkbench().getDisplay().getActiveShell(), SWT.NONE);
235                 fileDialog
236                         .setFilterExtensions(Constants.BROWSE_RAML_FILTER_EXTENSIONS);
237                 String configFileAbsolutePath = fileDialog.open();
238                 locationTxt.setText(configFileAbsolutePath);
239             }
240         });
241
242         resourceTypeCmb.addSelectionListener(new SelectionAdapter() {
243             @Override
244             public void widgetSelected(SelectionEvent e) {
245                 int index = resourceTypeCmb.getSelectionIndex();
246                 if (index >= 0) {
247                     String selectedItem = resourceTypeCmb.getItem(index);
248                     if (null != selectedItem) {
249                         // Convert the selectedItem to the fully qualified file
250                         // name.
251                         selectedItem = Utility.displayToFileName(selectedItem);
252
253                         // Get the RAML configuration file path of the selected
254                         // resource
255                         configFilePath = Activator.getDefault()
256                                 .getResourceManager()
257                                 .getConfigFilePath(selectedItem);
258
259                         setPageComplete(isSelectionDone());
260                     }
261                 }
262             }
263         });
264
265         locationTxt.addModifyListener(new ModifyListener() {
266             @Override
267             public void modifyText(ModifyEvent e) {
268                 configFilePath = locationTxt.getText();
269                 setPageComplete(isSelectionDone());
270             }
271         });
272
273         noOfInstancesText.addListener(SWT.Verify, new Listener() {
274             @Override
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')) {
281                         e.doit = false;
282                         return;
283                     }
284                 }
285             }
286         });
287
288         noOfInstancesText.addKeyListener(new KeyAdapter() {
289             @Override
290             public void keyReleased(KeyEvent e) {
291                 setPageComplete(isSelectionDone());
292             }
293         });
294     }
295
296     private void selectInitialItem() {
297         if (resourceTypeCmb.getItemCount() > 0) {
298             resourceTypeCmb.select(0);
299             String fileName = Utility.displayToFileName(resourceTypeCmb
300                     .getText());
301             configFilePath = Activator.getDefault().getResourceManager()
302                     .getConfigFilePath(fileName);
303         }
304     }
305
306     private boolean isSelectionDone() {
307         boolean done = false;
308         try {
309             resourceCount = Integer.parseInt(noOfInstancesText.getText());
310         } catch (Exception e) {
311             resourceCount = -1;
312         }
313         if (cusResourceRbtn.getSelection()) {
314             configFilePath = locationTxt.getText();
315         }
316
317         if (null != configFilePath && configFilePath.length() > 0
318                 && resourceCount >= 1) {
319             done = true;
320         }
321         return done;
322     }
323
324     public String getConfigFilePath() {
325         return configFilePath;
326     }
327
328     public int getResourceCount() {
329         return resourceCount;
330     }
331 }