f31dbbc125e058b129b2fb3b460405fa5c765815
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / SimpleResourceBasicDetailsPage.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 oic.simulator.serviceprovider.Activator;
20 import oic.simulator.serviceprovider.utils.Constants;
21 import oic.simulator.serviceprovider.utils.Utility;
22
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.wizard.IWizardPage;
25 import org.eclipse.jface.wizard.WizardPage;
26 import org.eclipse.swt.SWT;
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.Group;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Text;
39
40 public class SimpleResourceBasicDetailsPage extends WizardPage {
41
42     private Text    resNameTxt;
43     private Text    resUriTxt;
44     private Text    resTypeTxt;
45     private Button  observeBtn;
46
47     private String  resName;
48     private String  resURI;
49     private String  resType;
50     private boolean observable;
51
52     protected SimpleResourceBasicDetailsPage() {
53         super("Basic Details");
54     }
55
56     @Override
57     public void createControl(Composite parent) {
58         setPageComplete(false);
59         setTitle(Constants.SIMPLE_RESOURCE_BASIC_DETAILS_PAGE_TITLE);
60         setMessage(Constants.SIMPLE_RESOURCE_BASIC_DETAILS_PAGE_MESSAGE);
61
62         Composite container = new Composite(parent, SWT.NONE);
63         GridLayout gridLayout = new GridLayout();
64         container.setLayout(gridLayout);
65         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
66         container.setLayoutData(gd);
67
68         Group resDetGrp = new Group(container, SWT.NONE);
69         resDetGrp.setText("Resource Details");
70         gridLayout = new GridLayout(4, false);
71         resDetGrp.setLayout(gridLayout);
72         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
73         resDetGrp.setLayoutData(gd);
74
75         Label resNameLbl = new Label(resDetGrp, SWT.NULL);
76         resNameLbl.setText(Constants.RESOURCE_NAME);
77         gd = new GridData();
78         resNameLbl.setLayoutData(gd);
79
80         resNameTxt = new Text(resDetGrp, SWT.BORDER);
81         gd = new GridData();
82         gd.horizontalAlignment = SWT.FILL;
83         gd.grabExcessHorizontalSpace = true;
84         gd.horizontalSpan = 3;
85         resNameTxt.setLayoutData(gd);
86         resNameTxt.setFocus();
87
88         Label resUriLbl = new Label(resDetGrp, SWT.NULL);
89         resUriLbl.setText(Constants.RESOURCE_URI);
90
91         resUriTxt = new Text(resDetGrp, SWT.BORDER);
92         gd = new GridData();
93         gd.horizontalSpan = 3;
94         gd.horizontalAlignment = SWT.FILL;
95         gd.grabExcessHorizontalSpace = true;
96         resUriTxt.setLayoutData(gd);
97
98         Label resTypeLbl = new Label(resDetGrp, SWT.NULL);
99         resTypeLbl.setText(Constants.RESOURCE_TYPE);
100
101         resTypeTxt = new Text(resDetGrp, SWT.BORDER);
102         gd = new GridData();
103         gd.horizontalSpan = 3;
104         gd.horizontalAlignment = SWT.FILL;
105         gd.grabExcessHorizontalSpace = true;
106         resTypeTxt.setLayoutData(gd);
107
108         Group otherOptionsGrp = new Group(container, SWT.NONE);
109         otherOptionsGrp.setText("Other options");
110         gridLayout = new GridLayout();
111         otherOptionsGrp.setLayout(gridLayout);
112         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
113         otherOptionsGrp.setLayoutData(gd);
114
115         observeBtn = new Button(otherOptionsGrp, SWT.CHECK);
116         observeBtn.setText("Observable");
117         observeBtn.setSelection(true);
118         observable = true;
119
120         addUiListeners();
121
122         setControl(container);
123     }
124
125     private void addUiListeners() {
126
127         resUriTxt.addModifyListener(new ModifyListener() {
128             @Override
129             public void modifyText(ModifyEvent arg0) {
130                 resURI = resUriTxt.getText();
131                 if (null == resURI) {
132                     return;
133                 }
134
135                 getWizard().getContainer().updateButtons();
136             }
137         });
138
139         resNameTxt.addModifyListener(new ModifyListener() {
140             @Override
141             public void modifyText(ModifyEvent arg0) {
142                 resName = resNameTxt.getText();
143                 getWizard().getContainer().updateButtons();
144             }
145         });
146
147         resTypeTxt.addModifyListener(new ModifyListener() {
148             @Override
149             public void modifyText(ModifyEvent arg0) {
150                 resType = resTypeTxt.getText();
151                 getWizard().getContainer().updateButtons();
152             }
153         });
154
155         observeBtn.addSelectionListener(new SelectionAdapter() {
156             @Override
157             public void widgetSelected(SelectionEvent e) {
158                 observable = observeBtn.getSelection();
159             }
160         });
161     }
162
163     @Override
164     public boolean canFlipToNextPage() {
165         if (null == resName || null == resURI || null == resType) {
166             return false;
167         }
168         resName = resName.trim();
169         resURI = resURI.trim();
170         resType = resType.trim();
171         if (resName.length() < 1 || resURI.length() < 1 || resType.length() < 1) {
172             return false;
173         }
174         return true;
175     }
176
177     @Override
178     public IWizardPage getNextPage() {
179         if (!Utility.isUriValid(resURI)) {
180             MessageDialog.openError(Display.getDefault().getActiveShell(),
181                     "Invalid Resource URI.", Constants.INVALID_URI_MESSAGE);
182             return null;
183         }
184
185         // Checking whether the uri is used by any other resource.
186         if (Activator.getDefault().getResourceManager().isResourceExist(resURI)) {
187             MessageDialog
188                     .openError(getShell(), "Resource URI in use",
189                             "Entered resource URI is in use. Please try a different one.");
190             // TODO: Instead of MessageDialog, errors may be shown on wizard
191             // itself.
192             return null;
193         }
194
195         return ((CreateResourceWizard) getWizard())
196                 .getSimpleResourceAddAttributePage();
197     }
198
199     public String getResName() {
200         return resName;
201     }
202
203     public String getResURI() {
204         return resURI;
205     }
206
207     public String getResType() {
208         return resType;
209     }
210
211     public boolean isObservable() {
212         return observable;
213     }
214 }