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