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.lang.reflect.InvocationTargetException;
20 import java.util.Date;
22 import oic.simulator.serviceprovider.Activator;
23 import oic.simulator.serviceprovider.utils.Constants;
24 import oic.simulator.serviceprovider.utils.Utility;
25 import oic.simulator.serviceprovider.view.dialogs.MainPage.ResourceOption;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.operation.IRunnableWithProgress;
30 import org.eclipse.jface.wizard.IWizardPage;
31 import org.eclipse.jface.wizard.WizardPage;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.ModifyEvent;
34 import org.eclipse.swt.events.ModifyListener;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Group;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Text;
41 import org.oic.simulator.ILogger.Level;
42 import org.oic.simulator.SimulatorException;
44 public class UpdatePropertiesPage extends WizardPage {
46 private Text resNameTxt;
47 private Text resUriTxt;
49 private String resName;
50 private String resURI;
52 protected UpdatePropertiesPage() {
53 super("Update Properties");
57 public void createControl(Composite parent) {
58 setPageComplete(true);
59 setTitle(Constants.UPDATE_PROP_PAGE_TITLE);
60 setMessage(Constants.UPDATE_PROP_PAGE_MESSAGE);
62 Composite comp = new Composite(parent, SWT.NONE);
63 GridLayout gridLayout = new GridLayout();
64 comp.setLayout(gridLayout);
65 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
66 comp.setLayoutData(gd);
68 Group grp = new Group(comp, SWT.NONE);
69 gridLayout = new GridLayout(2, false);
70 grp.setLayout(gridLayout);
71 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
72 grp.setLayoutData(gd);
74 Label resNameLbl = new Label(grp, SWT.NULL);
75 resNameLbl.setText("Resource Name");
77 gd.verticalIndent = 20;
78 resNameLbl.setLayoutData(gd);
80 resNameTxt = new Text(grp, SWT.BORDER);
81 resNameTxt.setFocus();
84 gd.verticalIndent = 20;
85 resNameTxt.setLayoutData(gd);
87 Label resUriLbl = new Label(grp, SWT.NULL);
88 resUriLbl.setText("Resource URI");
90 gd.verticalIndent = 10;
91 resUriLbl.setLayoutData(gd);
93 resUriTxt = new Text(grp, SWT.BORDER);
96 gd.verticalIndent = 10;
97 resUriTxt.setLayoutData(gd);
99 Label descLbl = new Label(comp, SWT.NONE);
100 descLbl.setText("Description:");
102 descLbl.setLayoutData(gd);
104 final Text text = new Text(comp, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER
105 | SWT.WRAP | SWT.V_SCROLL);
106 text.setText("These properties can be changed later from properties view.");
107 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
108 text.setLayoutData(gd);
113 if (resUriTxt.getText().length() < 1 && null != resURI) {
114 resUriTxt.setText(resURI);
116 if (resNameTxt.getText().length() < 1 && null != resName) {
117 resNameTxt.setText(resName);
123 private void addUIListeners() {
124 resNameTxt.addModifyListener(new ModifyListener() {
126 public void modifyText(ModifyEvent e) {
127 resName = resNameTxt.getText();
128 setPageComplete(isSelectionDone());
129 // getWizard().getContainer().updateButtons();
133 resUriTxt.addModifyListener(new ModifyListener() {
135 public void modifyText(ModifyEvent e) {
136 resURI = resUriTxt.getText();
137 setPageComplete(isSelectionDone());
138 // getWizard().getContainer().updateButtons();
144 public boolean canFlipToNextPage() {
145 CreateResourceWizard createWizard = (CreateResourceWizard) getWizard();
146 if (isSelectionDone()
147 && (createWizard.getMainPage().getResourceOption() == ResourceOption.COLLECTION_FROM_RAML)
148 && Activator.getDefault().getResourceManager()
149 .isAnyResourceExist()) {
155 public boolean isSelectionDone() {
156 boolean done = false;
157 if (null != resName && resName.trim().length() > 0 && null != resURI
158 && resURI.trim().length() > 0) {
165 public IWizardPage getNextPage() {
166 final boolean done[] = new boolean[1];
167 CreateResourceWizard createWizard = (CreateResourceWizard) getWizard();
168 // Checking whether the uri is used by any other resource.
169 if (Activator.getDefault().getResourceManager().isResourceExist(resURI)) {
171 .openError(getShell(), "Resource URI in use",
172 "Entered resource URI is in use. Please try a different one.");
173 // TODO: Instead of MessageDialog, errors may be shown on wizard
178 getContainer().run(true, true, new IRunnableWithProgress() {
181 public void run(IProgressMonitor monitor)
182 throws InvocationTargetException, InterruptedException {
185 "Completing Collection Resource Creation With RAML",
188 done[0] = completeCollectionResourceCreationWithRAML();
195 } catch (InvocationTargetException e) {
196 Activator.getDefault().getLogManager()
197 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
200 } catch (InterruptedException e) {
201 Activator.getDefault().getLogManager()
202 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
209 return createWizard.getAddResourcesToCollectionPage();
212 public void setResName(String resName) {
213 this.resName = resName;
214 if (!resNameTxt.isDisposed())
215 resNameTxt.setText(resName);
218 public void setResURI(String resURI) {
219 this.resURI = resURI;
220 if (!resUriTxt.isDisposed())
221 resUriTxt.setText(resURI);
224 public String getResName() {
228 public String getResURI() {
232 private boolean completeCollectionResourceCreationWithRAML() {
233 boolean result = false;
235 CreateResourceWizard createWizard = (CreateResourceWizard) getWizard();
239 .getResourceManager()
240 .completeCollectionResourceCreationByRAML(
241 createWizard.getLoadRamlPage().getResource(),
244 status = "Resource created.";
246 status = "Failed to create resource.";
248 } catch (SimulatorException e) {
249 status = "Failed to create resource.\n"
250 + Utility.getSimulatorErrorString(e, null);
252 createWizard.setStatus(status);