Collection resource support in SDK and Plug-in.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / UpdatePropertiesPage.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.lang.reflect.InvocationTargetException;
20 import java.util.Date;
21
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;
26
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;
43
44 public class UpdatePropertiesPage extends WizardPage {
45
46     private Text   resNameTxt;
47     private Text   resUriTxt;
48
49     private String resName;
50     private String resURI;
51
52     protected UpdatePropertiesPage() {
53         super("Update Properties");
54     }
55
56     @Override
57     public void createControl(Composite parent) {
58         setPageComplete(true);
59         setTitle(Constants.UPDATE_PROP_PAGE_TITLE);
60         setMessage(Constants.UPDATE_PROP_PAGE_MESSAGE);
61
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);
67
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);
73
74         Label resNameLbl = new Label(grp, SWT.NULL);
75         resNameLbl.setText("Resource Name");
76         gd = new GridData();
77         gd.verticalIndent = 20;
78         resNameLbl.setLayoutData(gd);
79
80         resNameTxt = new Text(grp, SWT.BORDER);
81         resNameTxt.setFocus();
82         gd = new GridData();
83         gd.widthHint = 300;
84         gd.verticalIndent = 20;
85         resNameTxt.setLayoutData(gd);
86
87         Label resUriLbl = new Label(grp, SWT.NULL);
88         resUriLbl.setText("Resource URI");
89         gd = new GridData();
90         gd.verticalIndent = 10;
91         resUriLbl.setLayoutData(gd);
92
93         resUriTxt = new Text(grp, SWT.BORDER);
94         gd = new GridData();
95         gd.widthHint = 300;
96         gd.verticalIndent = 10;
97         resUriTxt.setLayoutData(gd);
98
99         Label descLbl = new Label(comp, SWT.NONE);
100         descLbl.setText("Description:");
101         gd = new GridData();
102         descLbl.setLayoutData(gd);
103
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);
109
110         addUIListeners();
111
112         // Initialize data
113         if (resUriTxt.getText().length() < 1 && null != resURI) {
114             resUriTxt.setText(resURI);
115         }
116         if (resNameTxt.getText().length() < 1 && null != resName) {
117             resNameTxt.setText(resName);
118         }
119
120         setControl(comp);
121     }
122
123     private void addUIListeners() {
124         resNameTxt.addModifyListener(new ModifyListener() {
125             @Override
126             public void modifyText(ModifyEvent e) {
127                 resName = resNameTxt.getText();
128                 setPageComplete(isSelectionDone());
129                 // getWizard().getContainer().updateButtons();
130             }
131         });
132
133         resUriTxt.addModifyListener(new ModifyListener() {
134             @Override
135             public void modifyText(ModifyEvent e) {
136                 resURI = resUriTxt.getText();
137                 setPageComplete(isSelectionDone());
138                 // getWizard().getContainer().updateButtons();
139             }
140         });
141     }
142
143     @Override
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()) {
150             return true;
151         }
152         return false;
153     }
154
155     public boolean isSelectionDone() {
156         boolean done = false;
157         if (null != resName && resName.trim().length() > 0 && null != resURI
158                 && resURI.trim().length() > 0) {
159             done = true;
160         }
161         return done;
162     }
163
164     @Override
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)) {
170             MessageDialog
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
174             // itself.
175             return null;
176         }
177         try {
178             getContainer().run(true, true, new IRunnableWithProgress() {
179
180                 @Override
181                 public void run(IProgressMonitor monitor)
182                         throws InvocationTargetException, InterruptedException {
183                     try {
184                         monitor.beginTask(
185                                 "Completing Collection Resource Creation With RAML",
186                                 2);
187                         monitor.worked(1);
188                         done[0] = completeCollectionResourceCreationWithRAML();
189                         monitor.worked(1);
190                     } finally {
191                         monitor.done();
192                     }
193                 }
194             });
195         } catch (InvocationTargetException e) {
196             Activator.getDefault().getLogManager()
197                     .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
198             e.printStackTrace();
199             return null;
200         } catch (InterruptedException e) {
201             Activator.getDefault().getLogManager()
202                     .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
203             e.printStackTrace();
204             return null;
205         }
206         if (!done[0]) {
207             return null;
208         }
209         return createWizard.getAddResourcesToCollectionPage();
210     }
211
212     public void setResName(String resName) {
213         this.resName = resName;
214         if (!resNameTxt.isDisposed())
215             resNameTxt.setText(resName);
216     }
217
218     public void setResURI(String resURI) {
219         this.resURI = resURI;
220         if (!resUriTxt.isDisposed())
221             resUriTxt.setText(resURI);
222     }
223
224     public String getResName() {
225         return resName;
226     }
227
228     public String getResURI() {
229         return resURI;
230     }
231
232     private boolean completeCollectionResourceCreationWithRAML() {
233         boolean result = false;
234         String status;
235         CreateResourceWizard createWizard = (CreateResourceWizard) getWizard();
236         try {
237             result = Activator
238                     .getDefault()
239                     .getResourceManager()
240                     .completeCollectionResourceCreationByRAML(
241                             createWizard.getLoadRamlPage().getResource(),
242                             resURI, resName);
243             if (result)
244                 status = "Resource created.";
245             else {
246                 status = "Failed to create resource.";
247             }
248         } catch (SimulatorException e) {
249             status = "Failed to create resource.\n"
250                     + Utility.getSimulatorErrorString(e, null);
251         }
252         createWizard.setStatus(status);
253         return result;
254     }
255 }