Simulator Plug-ins: Bug fixing and enhancements.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / SimpleResourceBasicDetailsPage.java
1 package oic.simulator.serviceprovider.view.dialogs;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import oic.simulator.serviceprovider.Activator;
7 import oic.simulator.serviceprovider.utils.Constants;
8 import oic.simulator.serviceprovider.utils.Utility;
9
10 import org.eclipse.jface.dialogs.MessageDialog;
11 import org.eclipse.jface.window.Window;
12 import org.eclipse.jface.wizard.IWizardPage;
13 import org.eclipse.jface.wizard.WizardPage;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.swt.widgets.Group;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.List;
27 import org.eclipse.swt.widgets.Text;
28
29 public class SimpleResourceBasicDetailsPage extends WizardPage {
30
31     private Text        resNameTxt;
32     private Text        resUriTxt;
33     private List        resTypeslist;
34     private Button      addToListBtn;
35     private Button      remFromListBtn;
36     private Button      observeBtn;
37     // private Button startBtn;
38
39     private String      resName;
40     private String      resURI;
41     private Set<String> resTypes;
42     private boolean     observable;
43
44     // private boolean start;
45
46     protected SimpleResourceBasicDetailsPage() {
47         super("Basic Details");
48         resTypes = new HashSet<String>();
49     }
50
51     @Override
52     public void createControl(Composite parent) {
53         setPageComplete(false);
54         setTitle(Constants.SIMPLE_RESOURCE_BASIC_DETAILS_PAGE_TITLE);
55         setMessage(Constants.SIMPLE_RESOURCE_BASIC_DETAILS_PAGE_MESSAGE);
56
57         Composite container = new Composite(parent, SWT.NONE);
58         GridLayout gridLayout = new GridLayout();
59         container.setLayout(gridLayout);
60         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
61         container.setLayoutData(gd);
62
63         Group resDetGrp = new Group(container, SWT.NONE);
64         resDetGrp.setText("Resource Details");
65         gridLayout = new GridLayout(4, false);
66         resDetGrp.setLayout(gridLayout);
67         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
68         resDetGrp.setLayoutData(gd);
69
70         Label resNameLbl = new Label(resDetGrp, SWT.NULL);
71         resNameLbl.setText(Constants.RESOURCE_NAME);
72         gd = new GridData();
73         resNameLbl.setLayoutData(gd);
74
75         resNameTxt = new Text(resDetGrp, SWT.BORDER);
76         gd = new GridData();
77         // gd.widthHint = 300;
78         gd.horizontalAlignment = SWT.FILL;
79         gd.grabExcessHorizontalSpace = true;
80         gd.horizontalSpan = 3;
81         resNameTxt.setLayoutData(gd);
82         resNameTxt.setFocus();
83
84         Label resUriLbl = new Label(resDetGrp, SWT.NULL);
85         resUriLbl.setText(Constants.RESOURCE_URI);
86
87         resUriTxt = new Text(resDetGrp, SWT.BORDER);
88         gd = new GridData();
89         // gd.widthHint = 300;
90         gd.horizontalSpan = 3;
91         gd.horizontalAlignment = SWT.FILL;
92         gd.grabExcessHorizontalSpace = true;
93         resUriTxt.setLayoutData(gd);
94         /*
95          * Composite resTypesComp = new Composite(compContent, SWT.NONE);
96          * gridLayout = new GridLayout(3, false);
97          * resTypesComp.setLayout(gridLayout); gd = new GridData();
98          * gd.grabExcessHorizontalSpace = true; gd.horizontalAlignment =
99          * SWT.FILL; resTypesComp.setLayoutData(gd);
100          */
101
102         Label resTypesLbl = new Label(resDetGrp, SWT.NULL);
103         resTypesLbl.setText(Constants.RESOURCE_TYPES);
104
105         resTypeslist = new List(resDetGrp, SWT.BORDER | SWT.MULTI
106                 | SWT.V_SCROLL);
107         gd = new GridData();
108         gd.grabExcessHorizontalSpace = true;
109         gd.horizontalAlignment = SWT.FILL;
110         gd.horizontalSpan = 2;
111         gd.heightHint = 100;
112         /* gd.widthHint = 200; */
113         resTypeslist.setLayoutData(gd);
114         // resTypeslist.setBackground(new Color());
115
116         Composite resTypesActionsComp = new Composite(resDetGrp, SWT.NONE);
117         gridLayout = new GridLayout();
118         resTypesActionsComp.setLayout(gridLayout);
119         gd = new GridData();
120         gd.verticalAlignment = SWT.TOP;
121         /*
122          * gd.grabExcessHorizontalSpace = true; gd.horizontalAlignment =
123          * SWT.FILL;
124          */
125         resTypesActionsComp.setLayoutData(gd);
126
127         addToListBtn = new Button(resTypesActionsComp, SWT.PUSH);
128         addToListBtn.setText("Add");
129         gd = new GridData();
130         gd.widthHint = 70;
131         addToListBtn.setLayoutData(gd);
132
133         remFromListBtn = new Button(resTypesActionsComp, SWT.PUSH);
134         remFromListBtn.setText("Remove");
135         gd = new GridData();
136         gd.widthHint = 70;
137         remFromListBtn.setLayoutData(gd);
138         remFromListBtn.setEnabled(false);
139
140         Group otherOptionsGrp = new Group(container, SWT.NONE);
141         otherOptionsGrp.setText("Other options");
142         gridLayout = new GridLayout();
143         otherOptionsGrp.setLayout(gridLayout);
144         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
145         otherOptionsGrp.setLayoutData(gd);
146
147         observeBtn = new Button(otherOptionsGrp, SWT.CHECK);
148         observeBtn.setText("Observable");
149         observeBtn.setSelection(true);
150         observable = true;
151
152         /*
153          * startBtn = new Button(otherOptionsGrp, SWT.CHECK);
154          * startBtn.setText("Start the resource immediately after creation");
155          * startBtn.setSelection(true);
156          */
157         addUiListeners();
158
159         setControl(container);
160     }
161
162     private void addUiListeners() {
163
164         resUriTxt.addModifyListener(new ModifyListener() {
165             @Override
166             public void modifyText(ModifyEvent arg0) {
167                 resURI = resUriTxt.getText();
168                 if (null == resURI) {
169                     return;
170                 }
171
172                 getWizard().getContainer().updateButtons();
173             }
174         });
175
176         resNameTxt.addModifyListener(new ModifyListener() {
177             @Override
178             public void modifyText(ModifyEvent arg0) {
179                 resName = resNameTxt.getText();
180                 getWizard().getContainer().updateButtons();
181             }
182         });
183
184         addToListBtn.addSelectionListener(new SelectionAdapter() {
185             @Override
186             public void widgetSelected(SelectionEvent e) {
187                 SingleTextInputDialog dialog = new SingleTextInputDialog(
188                         getShell(), "Add Resource Type", "Resource Type");
189                 if (dialog.open() == Window.OK) {
190                     System.out.println(dialog.getValue());
191                     String resType = dialog.getValue();
192                     // Duplicate check.
193                     if (resTypes.contains(resType)) {
194                         MessageDialog.openError(Display.getDefault()
195                                 .getActiveShell(), "Duplicate value",
196                                 "value already exist.");
197                         return;
198                     }
199                     resTypeslist.add(resType);
200                     resTypeslist.deselectAll();
201                     resTypeslist.select(resTypeslist.getItemCount() - 1);
202                     resTypeslist.showSelection();
203                     remFromListBtn.setEnabled(true);
204
205                     resTypes.add(resType);
206
207                     getWizard().getContainer().updateButtons();
208                 }
209             }
210         });
211
212         remFromListBtn.addSelectionListener(new SelectionAdapter() {
213             @Override
214             public void widgetSelected(SelectionEvent e) {
215                 int[] selection = resTypeslist.getSelectionIndices();
216                 String[] selectionStr = resTypeslist.getSelection();
217                 if (null != selection && selection.length > 0) {
218                     resTypeslist.remove(selection);
219                     for (String selected : selectionStr) {
220                         resTypes.remove(selected);
221                     }
222                 }
223
224                 changeVisibility();
225                 getWizard().getContainer().updateButtons();
226             }
227         });
228
229         resTypeslist.addSelectionListener(new SelectionAdapter() {
230             @Override
231             public void widgetSelected(SelectionEvent e) {
232                 changeVisibility();
233             }
234         });
235
236         observeBtn.addSelectionListener(new SelectionAdapter() {
237             @Override
238             public void widgetSelected(SelectionEvent e) {
239                 observable = observeBtn.getSelection();
240             }
241         });
242     }
243
244     private void changeVisibility() {
245         int[] selection = resTypeslist.getSelectionIndices();
246         if (null != selection && selection.length > 0) {
247             remFromListBtn.setEnabled(true);
248         } else {
249             remFromListBtn.setEnabled(false);
250         }
251     }
252
253     @Override
254     public boolean canFlipToNextPage() {
255         if (null == resName || null == resURI || resTypes.size() < 1) {
256             return false;
257         }
258         resName = resName.trim();
259         resURI = resURI.trim();
260         if (resName.length() < 1 || resURI.length() < 1) {
261             return false;
262         }
263         return true;
264     }
265
266     @Override
267     public IWizardPage getNextPage() {
268         if (!Utility.isUriValid(resURI)) {
269             MessageDialog.openError(Display.getDefault().getActiveShell(),
270                     "Invalid Resource URI.", Constants.INVALID_URI_MESSAGE);
271             return null;
272         }
273
274         // Checking whether the uri is used by any other resource.
275         if (Activator.getDefault().getResourceManager().isResourceExist(resURI)) {
276             MessageDialog
277                     .openError(getShell(), "Resource URI in use",
278                             "Entered resource URI is in use. Please try a different one.");
279             // TODO: Instead of MessageDialog, errors may be shown on wizard
280             // itself.
281             return null;
282         }
283
284         return ((CreateResourceWizard) getWizard())
285                 .getSimpleResourceAddAttributePage();
286     }
287
288     public String getResName() {
289         return resName;
290     }
291
292     public String getResURI() {
293         return resURI;
294     }
295
296     public Set<String> getResTypes() {
297         return resTypes;
298     }
299
300     public boolean isObservable() {
301         return observable;
302     }
303 }