Updated the order of imports in the source files of simulator plugins.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / LoadRamlPage.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 org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.wizard.IWizardPage;
23 import org.eclipse.jface.wizard.WizardPage;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.KeyAdapter;
26 import org.eclipse.swt.events.KeyEvent;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.swt.widgets.Event;
37 import org.eclipse.swt.widgets.FileDialog;
38 import org.eclipse.swt.widgets.Group;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Listener;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.PlatformUI;
43
44 import java.io.FileInputStream;
45 import java.lang.reflect.InvocationTargetException;
46 import java.util.Date;
47
48 import org.oic.simulator.ILogger.Level;
49 import org.oic.simulator.SimulatorException;
50 import org.oic.simulator.server.SimulatorResource;
51 import org.oic.simulator.server.SimulatorResource.Type;
52
53 import oic.simulator.serviceprovider.Activator;
54 import oic.simulator.serviceprovider.model.Resource;
55 import oic.simulator.serviceprovider.model.SingleResource;
56 import oic.simulator.serviceprovider.utils.Constants;
57 import oic.simulator.serviceprovider.utils.Utility;
58 import oic.simulator.serviceprovider.view.dialogs.MainPage.Option;
59
60 /**
61  * This class shows UI for creating resources.
62  */
63 public class LoadRamlPage extends WizardPage {
64
65     private Text                   locationTxt;
66     private Button                 btnBrowse;
67     private Text                   noOfInstancesText;
68     private Label                  noOfInstancesLbl;
69     private Label                  locationLbl;
70
71     private String                 configFilePath = null;
72     private int                    resourceCount;
73
74     private Resource               resource;
75
76     private SimulatorResource.Type typeOfResource;
77
78     protected LoadRamlPage() {
79         super("Create Resource");
80         resourceCount = -1;
81     }
82
83     public void initialSetup(SimulatorResource.Type type) {
84         this.typeOfResource = type;
85         if (!noOfInstancesLbl.isDisposed()) {
86             noOfInstancesLbl.setVisible(type == Type.SINGLE);
87         }
88         if (!noOfInstancesText.isDisposed()) {
89             noOfInstancesText.setVisible(type == Type.SINGLE);
90         }
91     }
92
93     @Override
94     public void createControl(Composite parent) {
95         setPageComplete(false);
96         setTitle(Constants.CREATE_PAGE_TITLE);
97         setMessage(Constants.CREATE_PAGE_MESSAGE);
98         Composite compContent = new Composite(parent, SWT.NONE);
99         GridLayout gridLayout = new GridLayout();
100         compContent.setLayout(gridLayout);
101         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
102         compContent.setLayoutData(gd);
103
104         Group configGroup = new Group(compContent, SWT.NONE);
105         gridLayout = new GridLayout(1, false);
106         gridLayout.verticalSpacing = 10;
107         gridLayout.marginTop = 5;
108         configGroup.setLayout(gridLayout);
109         gd = new GridData();
110         gd.horizontalAlignment = SWT.FILL;
111         gd.grabExcessHorizontalSpace = true;
112         configGroup.setLayoutData(gd);
113         configGroup.setText("Load RAML File");
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
125         locationTxt = new Text(cusConfigComp, SWT.BORDER);
126         gd = new GridData();
127         gd.minimumWidth = 300;
128         gd.horizontalAlignment = SWT.FILL;
129         gd.grabExcessHorizontalSpace = true;
130         locationTxt.setLayoutData(gd);
131
132         btnBrowse = new Button(cusConfigComp, SWT.NONE);
133         btnBrowse.setText("Browse");
134         gd = new GridData();
135         gd.widthHint = 80;
136         gd.horizontalAlignment = SWT.FILL;
137         btnBrowse.setLayoutData(gd);
138         btnBrowse.setFocus();
139
140         Composite instanceComp = new Composite(compContent, SWT.NONE);
141         instanceComp.setLayout(new GridLayout(2, false));
142         gd = new GridData();
143         gd.horizontalAlignment = SWT.FILL;
144         gd.grabExcessHorizontalSpace = true;
145         instanceComp.setLayoutData(gd);
146
147         noOfInstancesLbl = new Label(instanceComp, SWT.NONE);
148         noOfInstancesLbl.setText("Number of Instances");
149
150         noOfInstancesText = new Text(instanceComp, SWT.BORDER);
151         gd = new GridData();
152         gd.widthHint = 50;
153         gd.horizontalAlignment = SWT.FILL;
154         noOfInstancesText.setLayoutData(gd);
155
156         // Adding the default value. It can be changed by the user.
157         noOfInstancesText.setText("1");
158
159         addUIListeners();
160
161         setControl(compContent);
162     }
163
164     private void addUIListeners() {
165         btnBrowse.addSelectionListener(new SelectionAdapter() {
166             @Override
167             public void widgetSelected(SelectionEvent e) {
168                 FileDialog fileDialog = new FileDialog(PlatformUI
169                         .getWorkbench().getDisplay().getActiveShell(), SWT.NONE);
170                 fileDialog
171                         .setFilterExtensions(Constants.BROWSE_RAML_FILTER_EXTENSIONS);
172                 String configFileAbsolutePath = fileDialog.open();
173                 if (null != configFileAbsolutePath)
174                     locationTxt.setText(configFileAbsolutePath);
175             }
176         });
177
178         locationTxt.addModifyListener(new ModifyListener() {
179             @Override
180             public void modifyText(ModifyEvent e) {
181                 configFilePath = locationTxt.getText();
182                 getWizard().getContainer().updateButtons();
183             }
184         });
185
186         noOfInstancesText.addListener(SWT.Verify, new Listener() {
187             @Override
188             public void handleEvent(Event e) {
189                 String string = e.text;
190                 char[] chars = new char[string.length()];
191                 string.getChars(0, chars.length, chars, 0);
192                 for (int i = 0; i < chars.length; i++) {
193                     if (!('0' <= chars[i] && chars[i] <= '9')) {
194                         e.doit = false;
195                         return;
196                     }
197                 }
198             }
199         });
200
201         noOfInstancesText.addKeyListener(new KeyAdapter() {
202             @Override
203             public void keyReleased(KeyEvent e) {
204                 getWizard().getContainer().updateButtons();
205             }
206         });
207     }
208
209     @Override
210     public boolean canFlipToNextPage() {
211         boolean done = false;
212         try {
213             resourceCount = Integer.parseInt(noOfInstancesText.getText());
214         } catch (Exception e) {
215             resourceCount = -1;
216         }
217         if (null != configFilePath && configFilePath.trim().length() > 0) {
218             if (resourceCount == 1) {
219                 done = true;
220             }
221         }
222         return done;
223     }
224
225     public boolean isSelectionDone() {
226         boolean done = false;
227         try {
228             resourceCount = Integer.parseInt(noOfInstancesText.getText());
229         } catch (Exception e) {
230             resourceCount = -1;
231         }
232         if (null != configFilePath && configFilePath.trim().length() > 0) {
233             if (resourceCount >= 1) {
234                 done = true;
235             }
236         }
237         return done;
238     }
239
240     public boolean isMultiResourceCreation() {
241         if (typeOfResource == Type.SINGLE && resourceCount > 1) {
242             return true;
243         }
244         return false;
245     }
246
247     @Override
248     public IWizardPage getNextPage() {
249         // Validate the file path.
250         try {
251             new FileInputStream(configFilePath);
252         } catch (Exception e) {
253             MessageDialog
254                     .openError(getShell(), "Invalid File",
255                             "File doesn't exist. Either the file path or file name is invalid.");
256             return null;
257         }
258
259         // Checking the resource count
260         if ((Activator.getDefault().getResourceManager().getResourceCount() + resourceCount) > Constants.MAX_RESOURCE_COUNT) {
261             MessageDialog
262                     .openInformation(Display.getDefault().getActiveShell(),
263                             "Resource limit exceeded",
264                             "Exceeded the limit of resources that can exist in the server.");
265             return null;
266         }
267
268         final CreateResourceWizard wizard = ((CreateResourceWizard) getWizard());
269
270         try {
271             getContainer().run(true, false, new IRunnableWithProgress() {
272                 @Override
273                 public void run(IProgressMonitor monitor)
274                         throws InvocationTargetException, InterruptedException {
275                     try {
276                         monitor.beginTask("Resource Creation With RAML", 2);
277                         monitor.worked(1);
278                         resource = Activator.getDefault().getResourceManager()
279                                 .createResourceByRAML(configFilePath);
280                         monitor.worked(1);
281                     } catch (SimulatorException e) {
282                         wizard.setStatus("Failed to create resource.\n"
283                                 + Utility.getSimulatorErrorString(e, null));
284                     } finally {
285                         monitor.done();
286                     }
287                 }
288             });
289         } catch (InvocationTargetException e) {
290             Activator.getDefault().getLogManager()
291                     .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
292             e.printStackTrace();
293         } catch (InterruptedException e) {
294             Activator.getDefault().getLogManager()
295                     .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
296             e.printStackTrace();
297         }
298         if (null == resource) {
299             wizard.setStatus("Failed to create Resource.");
300             wizard.getWizardDialog().close();
301             return null;
302         } else {
303             // Checking whether the resource is of type single.
304             Option intendedResource = wizard.getMainPage().getOption();
305             if ((intendedResource == Option.SIMPLE_FROM_RAML && !(resource instanceof SingleResource))) {
306                 MessageDialog
307                         .openError(
308                                 getShell(),
309                                 "Invalid RAML",
310                                 "Uploaded RAML is not of simple type. "
311                                         + "Please upload the proper RAML of simple type.");
312                 return null;
313             }
314         }
315         UpdatePropertiesPage updatePageRef = wizard.getUpdatePropPage();
316         updatePageRef.setResName(resource.getResourceName());
317         updatePageRef.setResURI(resource.getResourceURI());
318         return updatePageRef;
319     }
320
321     public String getConfigFilePath() {
322         return configFilePath;
323     }
324
325     public int getResourceCount() {
326         return resourceCount;
327     }
328
329     public Resource getResource() {
330         return resource;
331     }
332
333     public void setResource(Resource resource) {
334         this.resource = resource;
335     }
336 }