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 java.io.FileInputStream;
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.Date;
23 import oic.simulator.serviceprovider.Activator;
24 import oic.simulator.serviceprovider.model.Resource;
25 import oic.simulator.serviceprovider.model.SingleResource;
26 import oic.simulator.serviceprovider.utils.Constants;
27 import oic.simulator.serviceprovider.utils.Utility;
28 import oic.simulator.serviceprovider.view.dialogs.MainPage.Option;
30 import org.eclipse.core.runtime.IProgressMonitor;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.operation.IRunnableWithProgress;
33 import org.eclipse.jface.wizard.IWizardPage;
34 import org.eclipse.jface.wizard.WizardPage;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.KeyAdapter;
37 import org.eclipse.swt.events.KeyEvent;
38 import org.eclipse.swt.events.ModifyEvent;
39 import org.eclipse.swt.events.ModifyListener;
40 import org.eclipse.swt.events.SelectionAdapter;
41 import org.eclipse.swt.events.SelectionEvent;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Button;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Display;
47 import org.eclipse.swt.widgets.Event;
48 import org.eclipse.swt.widgets.FileDialog;
49 import org.eclipse.swt.widgets.Group;
50 import org.eclipse.swt.widgets.Label;
51 import org.eclipse.swt.widgets.Listener;
52 import org.eclipse.swt.widgets.Text;
53 import org.eclipse.ui.PlatformUI;
54 import org.oic.simulator.ILogger.Level;
55 import org.oic.simulator.SimulatorException;
56 import org.oic.simulator.server.SimulatorResource;
57 import org.oic.simulator.server.SimulatorResource.Type;
60 * This class shows UI for creating resources.
62 public class LoadRamlPage extends WizardPage {
64 private Text locationTxt;
65 private Button btnBrowse;
66 private Text noOfInstancesText;
67 private Label noOfInstancesLbl;
68 private Label locationLbl;
70 private String configFilePath = null;
71 private int resourceCount;
73 private Resource resource;
75 private SimulatorResource.Type typeOfResource;
77 protected LoadRamlPage() {
78 super("Create Resource");
82 public void initialSetup(SimulatorResource.Type type) {
83 this.typeOfResource = type;
84 if (!noOfInstancesLbl.isDisposed()) {
85 noOfInstancesLbl.setVisible(type == Type.SINGLE);
87 if (!noOfInstancesText.isDisposed()) {
88 noOfInstancesText.setVisible(type == Type.SINGLE);
93 public void createControl(Composite parent) {
94 setPageComplete(false);
95 setTitle(Constants.CREATE_PAGE_TITLE);
96 setMessage(Constants.CREATE_PAGE_MESSAGE);
97 Composite compContent = new Composite(parent, SWT.NONE);
98 GridLayout gridLayout = new GridLayout();
99 compContent.setLayout(gridLayout);
100 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
101 compContent.setLayoutData(gd);
103 Group configGroup = new Group(compContent, SWT.NONE);
104 gridLayout = new GridLayout(1, false);
105 gridLayout.verticalSpacing = 10;
106 gridLayout.marginTop = 5;
107 configGroup.setLayout(gridLayout);
109 gd.horizontalAlignment = SWT.FILL;
110 gd.grabExcessHorizontalSpace = true;
111 configGroup.setLayoutData(gd);
112 configGroup.setText("Load RAML File");
114 Composite cusConfigComp = new Composite(configGroup, SWT.NONE);
115 cusConfigComp.setLayout(new GridLayout(3, false));
117 gd.horizontalAlignment = SWT.FILL;
118 gd.grabExcessHorizontalSpace = true;
119 cusConfigComp.setLayoutData(gd);
121 locationLbl = new Label(cusConfigComp, SWT.NONE);
122 locationLbl.setText("Location:");
124 locationTxt = new Text(cusConfigComp, SWT.BORDER);
126 gd.minimumWidth = 300;
127 gd.horizontalAlignment = SWT.FILL;
128 gd.grabExcessHorizontalSpace = true;
129 locationTxt.setLayoutData(gd);
131 btnBrowse = new Button(cusConfigComp, SWT.NONE);
132 btnBrowse.setText("Browse");
135 gd.horizontalAlignment = SWT.FILL;
136 btnBrowse.setLayoutData(gd);
137 btnBrowse.setFocus();
139 Composite instanceComp = new Composite(compContent, SWT.NONE);
140 instanceComp.setLayout(new GridLayout(2, false));
142 gd.horizontalAlignment = SWT.FILL;
143 gd.grabExcessHorizontalSpace = true;
144 instanceComp.setLayoutData(gd);
146 noOfInstancesLbl = new Label(instanceComp, SWT.NONE);
147 noOfInstancesLbl.setText("Number of Instances");
149 noOfInstancesText = new Text(instanceComp, SWT.BORDER);
152 gd.horizontalAlignment = SWT.FILL;
153 noOfInstancesText.setLayoutData(gd);
155 // Adding the default value. It can be changed by the user.
156 noOfInstancesText.setText("1");
160 setControl(compContent);
163 private void addUIListeners() {
164 btnBrowse.addSelectionListener(new SelectionAdapter() {
166 public void widgetSelected(SelectionEvent e) {
167 FileDialog fileDialog = new FileDialog(PlatformUI
168 .getWorkbench().getDisplay().getActiveShell(), SWT.NONE);
170 .setFilterExtensions(Constants.BROWSE_RAML_FILTER_EXTENSIONS);
171 String configFileAbsolutePath = fileDialog.open();
172 if (null != configFileAbsolutePath)
173 locationTxt.setText(configFileAbsolutePath);
177 locationTxt.addModifyListener(new ModifyListener() {
179 public void modifyText(ModifyEvent e) {
180 configFilePath = locationTxt.getText();
181 getWizard().getContainer().updateButtons();
185 noOfInstancesText.addListener(SWT.Verify, new Listener() {
187 public void handleEvent(Event e) {
188 String string = e.text;
189 char[] chars = new char[string.length()];
190 string.getChars(0, chars.length, chars, 0);
191 for (int i = 0; i < chars.length; i++) {
192 if (!('0' <= chars[i] && chars[i] <= '9')) {
200 noOfInstancesText.addKeyListener(new KeyAdapter() {
202 public void keyReleased(KeyEvent e) {
203 getWizard().getContainer().updateButtons();
209 public boolean canFlipToNextPage() {
210 boolean done = false;
212 resourceCount = Integer.parseInt(noOfInstancesText.getText());
213 } catch (Exception e) {
216 if (null != configFilePath && configFilePath.trim().length() > 0) {
217 if (resourceCount == 1) {
224 public boolean isSelectionDone() {
225 boolean done = false;
227 resourceCount = Integer.parseInt(noOfInstancesText.getText());
228 } catch (Exception e) {
231 if (null != configFilePath && configFilePath.trim().length() > 0) {
232 if (resourceCount >= 1) {
239 public boolean isMultiResourceCreation() {
240 if (typeOfResource == Type.SINGLE && resourceCount > 1) {
247 public IWizardPage getNextPage() {
248 // Validate the file path.
250 new FileInputStream(configFilePath);
251 } catch (Exception e) {
253 .openError(getShell(), "Invalid File",
254 "File doesn't exist. Either the file path or file name is invalid.");
255 // TODO: Instead of MessageDialog, errors may be shown on wizard
260 // Checking the resource count
261 if ((Activator.getDefault().getResourceManager().getResourceCount() + resourceCount) > Constants.MAX_RESOURCE_COUNT) {
263 .openInformation(Display.getDefault().getActiveShell(),
264 "Resource limit exceeded",
265 "Exceeded the limit of resources that can exist in the server.");
269 final CreateResourceWizard wizard = ((CreateResourceWizard) getWizard());
272 getContainer().run(true, false, new IRunnableWithProgress() {
274 public void run(IProgressMonitor monitor)
275 throws InvocationTargetException, InterruptedException {
277 monitor.beginTask("Resource Creation With RAML", 2);
279 resource = Activator.getDefault().getResourceManager()
280 .createResourceByRAML(configFilePath);
282 } catch (SimulatorException e) {
283 wizard.setStatus("Failed to create resource.\n"
284 + Utility.getSimulatorErrorString(e, null));
290 } catch (InvocationTargetException e) {
291 Activator.getDefault().getLogManager()
292 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
294 } catch (InterruptedException e) {
295 Activator.getDefault().getLogManager()
296 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
299 if (null == resource) {
300 wizard.setStatus("Failed to create Resource.");
301 wizard.getWizardDialog().close();
304 // Checking whether the resource is of type single.
305 Option intendedResource = wizard.getMainPage().getOption();
306 if ((intendedResource == Option.SIMPLE_FROM_RAML && !(resource instanceof SingleResource))) {
311 "Uploaded RAML is not of simple type. "
312 + "Please upload the proper RAML of simple type.");
316 UpdatePropertiesPage updatePageRef = wizard.getUpdatePropPage();
317 updatePageRef.setResName(resource.getResourceName());
318 updatePageRef.setResURI(resource.getResourceURI());
319 return updatePageRef;
322 public String getConfigFilePath() {
323 return configFilePath;
326 public int getResourceCount() {
327 return resourceCount;
330 public Resource getResource() {
334 public void setResource(Resource resource) {
335 this.resource = resource;