Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / DeleteResourcePage.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.util.Iterator;
20 import java.util.List;
21
22 import oic.simulator.serviceprovider.Activator;
23 import oic.simulator.serviceprovider.resource.DeleteCategory;
24 import oic.simulator.serviceprovider.utils.Constants;
25
26 import org.eclipse.jface.wizard.WizardPage;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.CCombo;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Group;
38 import org.eclipse.swt.widgets.Text;
39
40 /**
41  * This class shows UI for deleting resources.
42  */
43 public class DeleteResourcePage extends WizardPage {
44
45     private Button         allRbtn;
46     private Button         byTypeRbtn;
47     private Button         byUriRbtn;
48
49     private CCombo         resourceTypeCmb;
50     private Text           resourceUriTxt;
51
52     private DeleteCategory deleteCategory;
53
54     // It will hold either the resource type or resource uri
55     private String         deleteCandidate;
56
57     protected DeleteResourcePage() {
58         super("Delete Resource");
59     }
60
61     @Override
62     public void createControl(Composite parent) {
63         setPageComplete(false);
64         setTitle(Constants.DELETE_PAGE_TITLE);
65         setMessage(Constants.DELETE_PAGE_MESSAGE);
66
67         Composite compContent = new Composite(parent, SWT.NONE);
68         compContent.setLayout(new GridLayout(1, false));
69         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
70         compContent.setLayoutData(gd);
71
72         Group group = new Group(compContent, SWT.NONE);
73         group.setText("Select Category");
74         GridLayout gridLayout = new GridLayout(2, false);
75         gridLayout.verticalSpacing = 15;
76         gridLayout.marginTop = 10;
77         gridLayout.marginLeft = 10;
78         group.setLayout(gridLayout);
79         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
80         group.setLayoutData(gd);
81
82         allRbtn = new Button(group, SWT.RADIO);
83         allRbtn.setText("All resources");
84         gd = new GridData();
85         gd.horizontalSpan = 2;
86         gd.widthHint = 200;
87         allRbtn.setLayoutData(gd);
88
89         byTypeRbtn = new Button(group, SWT.RADIO);
90         byTypeRbtn.setText("All (By resource type)");
91         gd = new GridData();
92         gd.widthHint = 200;
93         byTypeRbtn.setLayoutData(gd);
94
95         resourceTypeCmb = new CCombo(group, SWT.READ_ONLY | SWT.BORDER);
96         gd = new GridData();
97         gd.widthHint = 200;
98         resourceTypeCmb.setLayoutData(gd);
99
100         byUriRbtn = new Button(group, SWT.RADIO);
101         byUriRbtn.setText("By Resource URI");
102         gd = new GridData();
103         gd.widthHint = 200;
104         byUriRbtn.setLayoutData(gd);
105
106         resourceUriTxt = new Text(group, SWT.BORDER);
107         gd = new GridData();
108         gd.widthHint = 300;
109         resourceUriTxt.setLayoutData(gd);
110
111         // Setting the initial visibility of controls
112         allRbtn.setSelection(false);
113         byTypeRbtn.setSelection(false);
114         byUriRbtn.setSelection(false);
115
116         resourceTypeCmb.setEnabled(false);
117         resourceUriTxt.setEnabled(false);
118
119         deleteCategory = DeleteCategory.NONE;
120
121         populateDataInUI();
122
123         addUIListeners();
124
125         setControl(compContent);
126     }
127
128     private void populateDataInUI() {
129         // Populate Resourcetype in Combo
130         populateResourceTypeCombo();
131     }
132
133     private void populateResourceTypeCombo() {
134         List<String> resourceTypeList;
135         resourceTypeList = Activator.getDefault().getResourceManager()
136                 .getResourceTypeList();
137         if (null != resourceTypeList) {
138             Iterator<String> itr = resourceTypeList.iterator();
139             while (itr.hasNext()) {
140                 resourceTypeCmb.add(itr.next());
141             }
142         }
143
144         // By default, select the first item in the combo
145         if (resourceTypeCmb.getItemCount() > 0) {
146             resourceTypeCmb.select(0);
147             deleteCandidate = resourceTypeCmb.getText();
148         }
149     }
150
151     public void addUIListeners() {
152         allRbtn.addSelectionListener(new SelectionAdapter() {
153             @Override
154             public void widgetSelected(SelectionEvent e) {
155                 // Update the visibility of controls
156                 resourceTypeCmb.setEnabled(false);
157                 resourceUriTxt.setEnabled(false);
158
159                 deleteCategory = DeleteCategory.ALL;
160                 deleteCandidate = null;
161                 setPageComplete(isSelectionDone());
162             }
163         });
164
165         byTypeRbtn.addSelectionListener(new SelectionAdapter() {
166             @Override
167             public void widgetSelected(SelectionEvent e) {
168                 // Update the visibility of controls
169                 resourceTypeCmb.setEnabled(true);
170                 resourceUriTxt.setEnabled(false);
171
172                 deleteCategory = DeleteCategory.BY_TYPE;
173                 setPageComplete(isSelectionDone());
174             }
175         });
176
177         byUriRbtn.addSelectionListener(new SelectionAdapter() {
178             @Override
179             public void widgetSelected(SelectionEvent e) {
180                 // Update the visibility of controls
181                 resourceUriTxt.setEnabled(true);
182                 resourceUriTxt.setFocus();
183                 resourceTypeCmb.setEnabled(false);
184
185                 deleteCategory = DeleteCategory.BY_URI;
186                 setPageComplete(isSelectionDone());
187             }
188         });
189
190         resourceTypeCmb.addModifyListener(new ModifyListener() {
191             @Override
192             public void modifyText(ModifyEvent arg0) {
193                 setPageComplete(isSelectionDone());
194             }
195         });
196
197         resourceUriTxt.addModifyListener(new ModifyListener() {
198             @Override
199             public void modifyText(ModifyEvent arg0) {
200                 setPageComplete(isSelectionDone());
201             }
202         });
203     }
204
205     public boolean isSelectionDone() {
206         boolean done = false;
207         if (deleteCategory == DeleteCategory.ALL) {
208             done = true;
209         } else if (deleteCategory == DeleteCategory.BY_TYPE) {
210             int selectedItemIndex = resourceTypeCmb.getSelectionIndex();
211             if (selectedItemIndex >= 0) {
212                 deleteCandidate = resourceTypeCmb.getItem(selectedItemIndex);
213                 if (null != deleteCandidate && deleteCandidate.length() > 0) {
214                     done = true;
215                 }
216             }
217         } else if (deleteCategory == DeleteCategory.BY_URI) {
218             deleteCandidate = resourceUriTxt.getText();
219             if (null != deleteCandidate && deleteCandidate.length() > 0) {
220                 done = true;
221             }
222         }
223         return done;
224     }
225
226     public DeleteCategory getDeleteCategory() {
227         return deleteCategory;
228     }
229
230     public String getDeleteCandidate() {
231         return deleteCandidate;
232     }
233
234     public void setFocusToTextBox() {
235         resourceUriTxt.setFocus();
236     }
237 }