2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.serviceprovider.view.dialogs;
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;
44 import java.io.FileInputStream;
45 import java.lang.reflect.InvocationTargetException;
46 import java.util.Date;
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;
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;
61 * This class shows UI for creating resources.
63 public class LoadRamlPage extends WizardPage {
65 private Text locationTxt;
66 private Button btnBrowse;
67 private Text noOfInstancesText;
68 private Label noOfInstancesLbl;
69 private Label locationLbl;
71 private String configFilePath = null;
72 private int resourceCount;
74 private Resource resource;
76 private SimulatorResource.Type typeOfResource;
78 protected LoadRamlPage() {
79 super("Create Resource");
83 public void initialSetup(SimulatorResource.Type type) {
84 this.typeOfResource = type;
85 if (!noOfInstancesLbl.isDisposed()) {
86 noOfInstancesLbl.setVisible(type == Type.SINGLE);
88 if (!noOfInstancesText.isDisposed()) {
89 noOfInstancesText.setVisible(type == Type.SINGLE);
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);
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);
110 gd.horizontalAlignment = SWT.FILL;
111 gd.grabExcessHorizontalSpace = true;
112 configGroup.setLayoutData(gd);
113 configGroup.setText("Load RAML File");
115 Composite cusConfigComp = new Composite(configGroup, SWT.NONE);
116 cusConfigComp.setLayout(new GridLayout(3, false));
118 gd.horizontalAlignment = SWT.FILL;
119 gd.grabExcessHorizontalSpace = true;
120 cusConfigComp.setLayoutData(gd);
122 locationLbl = new Label(cusConfigComp, SWT.NONE);
123 locationLbl.setText("Location:");
125 locationTxt = new Text(cusConfigComp, SWT.BORDER);
127 gd.minimumWidth = 300;
128 gd.horizontalAlignment = SWT.FILL;
129 gd.grabExcessHorizontalSpace = true;
130 locationTxt.setLayoutData(gd);
132 btnBrowse = new Button(cusConfigComp, SWT.NONE);
133 btnBrowse.setText("Browse");
136 gd.horizontalAlignment = SWT.FILL;
137 btnBrowse.setLayoutData(gd);
138 btnBrowse.setFocus();
140 Composite instanceComp = new Composite(compContent, SWT.NONE);
141 instanceComp.setLayout(new GridLayout(2, false));
143 gd.horizontalAlignment = SWT.FILL;
144 gd.grabExcessHorizontalSpace = true;
145 instanceComp.setLayoutData(gd);
147 noOfInstancesLbl = new Label(instanceComp, SWT.NONE);
148 noOfInstancesLbl.setText("Number of Instances");
150 noOfInstancesText = new Text(instanceComp, SWT.BORDER);
153 gd.horizontalAlignment = SWT.FILL;
154 noOfInstancesText.setLayoutData(gd);
156 // Adding the default value. It can be changed by the user.
157 noOfInstancesText.setText("1");
161 setControl(compContent);
164 private void addUIListeners() {
165 btnBrowse.addSelectionListener(new SelectionAdapter() {
167 public void widgetSelected(SelectionEvent e) {
168 FileDialog fileDialog = new FileDialog(PlatformUI
169 .getWorkbench().getDisplay().getActiveShell(), SWT.NONE);
171 .setFilterExtensions(Constants.BROWSE_RAML_FILTER_EXTENSIONS);
172 String configFileAbsolutePath = fileDialog.open();
173 if (null != configFileAbsolutePath)
174 locationTxt.setText(configFileAbsolutePath);
178 locationTxt.addModifyListener(new ModifyListener() {
180 public void modifyText(ModifyEvent e) {
181 configFilePath = locationTxt.getText();
182 getWizard().getContainer().updateButtons();
186 noOfInstancesText.addListener(SWT.Verify, new Listener() {
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')) {
201 noOfInstancesText.addKeyListener(new KeyAdapter() {
203 public void keyReleased(KeyEvent e) {
204 getWizard().getContainer().updateButtons();
210 public boolean canFlipToNextPage() {
211 boolean done = false;
213 resourceCount = Integer.parseInt(noOfInstancesText.getText());
214 } catch (Exception e) {
217 if (null != configFilePath && configFilePath.trim().length() > 0) {
218 if (resourceCount == 1) {
225 public boolean isSelectionDone() {
226 boolean done = false;
228 resourceCount = Integer.parseInt(noOfInstancesText.getText());
229 } catch (Exception e) {
232 if (null != configFilePath && configFilePath.trim().length() > 0) {
233 if (resourceCount >= 1) {
240 public boolean isMultiResourceCreation() {
241 if (typeOfResource == Type.SINGLE && resourceCount > 1) {
248 public IWizardPage getNextPage() {
249 // Validate the file path.
251 new FileInputStream(configFilePath);
252 } catch (Exception e) {
254 .openError(getShell(), "Invalid File",
255 "File doesn't exist. Either the file path or file name is invalid.");
259 // Checking the resource count
260 if ((Activator.getDefault().getResourceManager().getResourceCount() + resourceCount) > Constants.MAX_RESOURCE_COUNT) {
262 .openInformation(Display.getDefault().getActiveShell(),
263 "Resource limit exceeded",
264 "Exceeded the limit of resources that can exist in the server.");
268 final CreateResourceWizard wizard = ((CreateResourceWizard) getWizard());
271 getContainer().run(true, false, new IRunnableWithProgress() {
273 public void run(IProgressMonitor monitor)
274 throws InvocationTargetException, InterruptedException {
276 monitor.beginTask("Resource Creation With RAML", 2);
278 resource = Activator.getDefault().getResourceManager()
279 .createResourceByRAML(configFilePath);
281 } catch (SimulatorException e) {
282 wizard.setStatus("Failed to create resource.\n"
283 + Utility.getSimulatorErrorString(e, null));
289 } catch (InvocationTargetException e) {
290 Activator.getDefault().getLogManager()
291 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
293 } catch (InterruptedException e) {
294 Activator.getDefault().getLogManager()
295 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
298 if (null == resource) {
299 wizard.setStatus("Failed to create Resource.");
300 wizard.getWizardDialog().close();
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))) {
310 "Uploaded RAML is not of simple type. "
311 + "Please upload the proper RAML of simple type.");
315 UpdatePropertiesPage updatePageRef = wizard.getUpdatePropPage();
316 updatePageRef.setResName(resource.getResourceName());
317 updatePageRef.setResURI(resource.getResourceURI());
318 return updatePageRef;
321 public String getConfigFilePath() {
322 return configFilePath;
325 public int getResourceCount() {
326 return resourceCount;
329 public Resource getResource() {
333 public void setResource(Resource resource) {
334 this.resource = resource;