IoTivity Simulator System testing bug fixes.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / RemoveResourceFromDevices.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.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23
24 import oic.simulator.serviceprovider.Activator;
25 import oic.simulator.serviceprovider.model.Device;
26 import oic.simulator.serviceprovider.utils.Constants;
27
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.dialogs.TitleAreaDialog;
30 import org.eclipse.jface.viewers.CheckStateChangedEvent;
31 import org.eclipse.jface.viewers.CheckboxTreeViewer;
32 import org.eclipse.jface.viewers.ICheckStateListener;
33 import org.eclipse.jface.viewers.ITreeContentProvider;
34 import org.eclipse.jface.viewers.LabelProvider;
35 import org.eclipse.jface.viewers.Viewer;
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.Control;
43 import org.eclipse.swt.widgets.Display;
44 import org.eclipse.swt.widgets.Group;
45 import org.eclipse.swt.widgets.Label;
46 import org.eclipse.swt.widgets.Shell;
47
48 /**
49  * This dialog is used for loading the RAML file.
50  */
51 public class RemoveResourceFromDevices extends TitleAreaDialog {
52
53     private CheckboxTreeViewer treeViewer;
54
55     List<Device>               sourceList;
56
57     Set<Device>                selectedDeviceList;
58
59     public RemoveResourceFromDevices(Shell parentShell, List<Device> sourceList) {
60         super(parentShell);
61         this.sourceList = sourceList;
62         selectedDeviceList = new HashSet<Device>();
63     }
64
65     @Override
66     public void create() {
67         super.create();
68         setTitle("Remove from Device");
69         setMessage("Select one or more devices from which the simple resource will be removed.");
70     }
71
72     @Override
73     protected Control createDialogArea(Composite parent) {
74         Composite compLayout = (Composite) super.createDialogArea(parent);
75         Composite container = new Composite(compLayout, SWT.NONE);
76         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
77         GridLayout layout = new GridLayout();
78         layout.verticalSpacing = 10;
79         layout.marginTop = 10;
80         container.setLayout(layout);
81
82         Label lbl = new Label(container, SWT.NONE);
83         lbl.setText("Select Resources from:");
84         GridData gd;
85         gd = new GridData();
86         gd.grabExcessHorizontalSpace = true;
87         gd.horizontalAlignment = SWT.FILL;
88         lbl.setLayoutData(gd);
89
90         Group resourceGroup = new Group(container, SWT.NONE);
91
92         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
93
94         resourceGroup.setLayout(new GridLayout());
95         gd = new GridData();
96         gd.grabExcessHorizontalSpace = true;
97         gd.horizontalAlignment = SWT.FILL;
98         gd.heightHint = 300;
99         gd.horizontalSpan = 2;
100         resourceGroup.setLayoutData(gd);
101
102         treeViewer = new CheckboxTreeViewer(resourceGroup);
103         treeViewer.getTree().setBackground(color);
104         gd = new GridData();
105         gd.grabExcessHorizontalSpace = true;
106         gd.horizontalAlignment = SWT.FILL;
107         gd.grabExcessVerticalSpace = true;
108         gd.verticalAlignment = SWT.FILL;
109         // gd.heightHint = 300;
110         treeViewer.getTree().setLayoutData(gd);
111         treeViewer.setContentProvider(new TreeContentProvider());
112         treeViewer.setLabelProvider(new TreeLabelProvider());
113         treeViewer.setInput(new Object());
114         treeViewer.addCheckStateListener(new ICheckStateListener() {
115
116             @Override
117             public void checkStateChanged(CheckStateChangedEvent e) {
118                 Device res = (Device) e.getElement();
119                 if (null != res) {
120                     if (e.getChecked()) {
121                         selectedDeviceList.add(res);
122                         System.out.println("Checked" + res.getDeviceName());
123                     } else {
124                         selectedDeviceList.remove(res);
125                         System.out.println("Unchecked:" + res.getDeviceName());
126                     }
127                 }
128             }
129         });
130
131         return compLayout;
132     }
133
134     public Set<Device> getSelectedDeviceList() {
135         return selectedDeviceList;
136     }
137
138     class TreeContentProvider implements ITreeContentProvider {
139
140         @Override
141         public void dispose() {
142         }
143
144         @Override
145         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
146         }
147
148         @Override
149         public Object[] getChildren(Object parent) {
150             return null;
151         }
152
153         @Override
154         public Object[] getElements(Object parent) {
155             if (null == sourceList) {
156                 sourceList = new ArrayList<Device>();
157             }
158             return sourceList.toArray();
159         }
160
161         @Override
162         public Object getParent(Object child) {
163             return null;
164         }
165
166         @Override
167         public boolean hasChildren(Object parent) {
168             return false;
169         }
170     }
171
172     class TreeLabelProvider extends LabelProvider {
173         @Override
174         public String getText(Object element) {
175             Device res = (Device) element;
176             return res.getDeviceName();
177         }
178
179         @Override
180         public Image getImage(Object element) {
181             if (element instanceof Device) {
182                 return Activator.getDefault().getImageRegistry()
183                         .get(Constants.DEVICE);
184             } else {
185                 return null;
186             }
187         }
188     }
189
190     @Override
191     protected void okPressed() {
192         if (selectedDeviceList.isEmpty()) {
193             MessageDialog.openInformation(
194                     Display.getDefault().getActiveShell(),
195                     "No selection done.", "No devices are selected.");
196             return;
197         }
198         close();
199     }
200
201     @Override
202     public boolean isHelpAvailable() {
203         return false;
204     }
205 }