Removed collection resource and device support from simulator plug-in.
[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.HashSet;
20 import java.util.List;
21 import java.util.Set;
22
23 import oic.simulator.serviceprovider.Activator;
24 import oic.simulator.serviceprovider.model.Resource;
25 import oic.simulator.serviceprovider.model.SingleResource;
26 import oic.simulator.serviceprovider.utils.Constants;
27
28 import org.eclipse.jface.viewers.CheckStateChangedEvent;
29 import org.eclipse.jface.viewers.CheckboxTreeViewer;
30 import org.eclipse.jface.viewers.ICheckStateListener;
31 import org.eclipse.jface.viewers.ITreeContentProvider;
32 import org.eclipse.jface.viewers.LabelProvider;
33 import org.eclipse.jface.viewers.TreeNodeContentProvider;
34 import org.eclipse.jface.viewers.Viewer;
35 import org.eclipse.jface.wizard.WizardPage;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.graphics.Color;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Display;
43 import org.eclipse.swt.widgets.Group;
44 import org.eclipse.swt.widgets.Label;
45
46 /**
47  * This class shows UI for deleting resources.
48  */
49 public class DeleteResourcePage extends WizardPage {
50
51     private CheckboxTreeViewer    singleTreeViewer;
52
53     private List<SingleResource>  singleSourceList;
54
55     private TreeViewContentHelper singleTreeViewContentHelper;
56
57     protected DeleteResourcePage() {
58         super("Delete Resources");
59
60         singleSourceList = Activator.getDefault().getResourceManager()
61                 .getSingleResourceList();
62     }
63
64     @Override
65     public void createControl(Composite parent) {
66         setPageComplete(false);
67         setTitle("Delete Resources");
68         setMessage("Select one or more simple resources for deletion.");
69
70         Composite container = new Composite(parent, SWT.NONE);
71         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
72         GridLayout layout = new GridLayout(4, true);
73         container.setLayout(layout);
74
75         createSingleResourcesArea(container);
76
77         setControl(container);
78     }
79
80     private void createSingleResourcesArea(Composite container) {
81         Composite singleContainer = new Composite(container, SWT.NONE);
82         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
83         gd.horizontalSpan = 4;
84         singleContainer.setLayoutData(gd);
85         GridLayout layout = new GridLayout();
86         singleContainer.setLayout(layout);
87
88         Label lbl = new Label(singleContainer, SWT.NONE);
89         lbl.setText("Simple Resources:");
90         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
91         lbl.setLayoutData(gd);
92
93         Group resourceGroup = new Group(singleContainer, SWT.NONE);
94
95         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
96
97         resourceGroup.setLayout(new GridLayout());
98         gd = new GridData();
99         gd.grabExcessHorizontalSpace = true;
100         gd.horizontalAlignment = SWT.FILL;
101         gd.heightHint = 300;
102         gd.horizontalSpan = 2;
103         resourceGroup.setLayoutData(gd);
104
105         if (null == singleSourceList || singleSourceList.isEmpty()) {
106             return;
107         }
108
109         singleTreeViewer = new CheckboxTreeViewer(resourceGroup);
110         singleTreeViewer.getTree().setBackground(color);
111         gd = new GridData();
112         gd.grabExcessHorizontalSpace = true;
113         gd.horizontalAlignment = SWT.FILL;
114         gd.grabExcessVerticalSpace = true;
115         gd.verticalAlignment = SWT.FILL;
116
117         singleTreeViewer.getTree().setLayoutData(gd);
118         singleTreeViewer
119                 .setContentProvider(new SingleResourceContentProvider());
120         singleTreeViewer.setLabelProvider(new TreeLabelProvider());
121         singleTreeViewer.setInput(new TreeNodeContentProvider());
122         singleTreeViewer.addCheckStateListener(new ICheckStateListener() {
123
124             @Override
125             public void checkStateChanged(CheckStateChangedEvent e) {
126                 Object element = e.getElement();
127                 if (element instanceof TreeViewContentHelper) {
128                     singleTreeViewer.setGrayed(singleTreeViewContentHelper,
129                             false);
130                     singleTreeViewer.setChecked(singleTreeViewContentHelper,
131                             e.getChecked());
132                     singleTreeViewer.setSubtreeChecked(element, e.getChecked());
133                 } else {
134                     Object obj[] = singleTreeViewer.getCheckedElements();
135                     if (null != obj && obj.length > 0) {
136                         int checkedCount = obj.length;
137                         boolean isParentGrayed = singleTreeViewer
138                                 .getChecked(singleTreeViewContentHelper);
139                         boolean isParentChecked = singleTreeViewer
140                                 .getChecked(singleTreeViewContentHelper);
141                         if (isParentChecked || isParentGrayed) {
142                             checkedCount--;
143                         }
144                         if (checkedCount == singleSourceList.size()) {
145                             singleTreeViewer.setGrayed(
146                                     singleTreeViewContentHelper, false);
147                             singleTreeViewer.setChecked(
148                                     singleTreeViewContentHelper, true);
149                         } else {
150                             if (checkedCount > 0) {
151                                 singleTreeViewer.setGrayed(
152                                         singleTreeViewContentHelper, true);
153                                 singleTreeViewer.setChecked(
154                                         singleTreeViewContentHelper, true);
155                             } else {
156                                 singleTreeViewer.setGrayed(
157                                         singleTreeViewContentHelper, false);
158                                 singleTreeViewer.setChecked(
159                                         singleTreeViewContentHelper, false);
160                             }
161                         }
162                     }
163                 }
164                 setPageComplete(isSelectionDone());
165             }
166         });
167         singleTreeViewer.expandAll();
168
169     }
170
171     public Set<SingleResource> getSelectedSingleResourcesList() {
172         final Set<SingleResource> singles = new HashSet<SingleResource>();
173         Display.getDefault().syncExec(new Runnable() {
174
175             @Override
176             public void run() {
177                 if (null == singleTreeViewer) {
178                     return;
179                 }
180                 Object selection[] = singleTreeViewer.getCheckedElements();
181                 if (null == selection || selection.length < 1) {
182                     return;
183                 }
184                 for (Object obj : selection) {
185                     if (obj instanceof Resource) {
186                         singles.add((SingleResource) obj);
187                     }
188                 }
189             }
190         });
191         return singles;
192     }
193
194     class SingleResourceContentProvider implements ITreeContentProvider {
195
196         @Override
197         public void dispose() {
198         }
199
200         @Override
201         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
202         }
203
204         @Override
205         public Object[] getChildren(Object parent) {
206             if (parent instanceof TreeViewContentHelper) {
207                 return ((TreeViewContentHelper) parent).getResources()
208                         .toArray();
209             }
210             return null;
211         }
212
213         @Override
214         public Object[] getElements(Object parent) {
215             Object obj[] = new Object[1];
216             singleTreeViewContentHelper = new TreeViewContentHelper(
217                     singleSourceList);
218             obj[0] = singleTreeViewContentHelper;
219             return obj;
220         }
221
222         @Override
223         public Object getParent(Object child) {
224             return null;
225         }
226
227         @Override
228         public boolean hasChildren(Object parent) {
229             if (parent instanceof TreeViewContentHelper) {
230                 return true;
231             }
232             return false;
233         }
234     }
235
236     class TreeLabelProvider extends LabelProvider {
237         @Override
238         public String getText(Object element) {
239             if (element instanceof TreeViewContentHelper) {
240                 return "All";
241             }
242             Resource res = (Resource) element;
243             return res.getResourceName() + " (" + res.getResourceURI() + ")";
244         }
245
246         @Override
247         public Image getImage(Object element) {
248             if (element instanceof SingleResource) {
249                 return Activator.getDefault().getImageRegistry()
250                         .get(Constants.SINGLE_RESOURCE);
251             } else {
252                 return null;
253             }
254         }
255     }
256
257     class TreeViewContentHelper {
258         List<? extends Resource> resources;
259
260         public TreeViewContentHelper(List<? extends Resource> resources) {
261             this.resources = resources;
262         }
263
264         public void setResources(List<? extends Resource> resources) {
265             this.resources = resources;
266         }
267
268         public List<? extends Resource> getResources() {
269             return resources;
270         }
271     }
272
273     public boolean isSelectionDone() {
274         if (null != singleTreeViewer) {
275             Object obj[] = singleTreeViewer.getCheckedElements();
276             if (null != obj && obj.length > 0) {
277                 return true;
278             }
279         }
280         return false;
281     }
282 }