2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.serviceprovider.view.dialogs;
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
24 import oic.simulator.serviceprovider.Activator;
25 import oic.simulator.serviceprovider.model.Device;
26 import oic.simulator.serviceprovider.utils.Constants;
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;
49 * This dialog is used for loading the RAML file.
51 public class RemoveResourceFromDevices extends TitleAreaDialog {
53 private CheckboxTreeViewer treeViewer;
55 List<Device> sourceList;
57 Set<Device> selectedDeviceList;
59 public RemoveResourceFromDevices(Shell parentShell, List<Device> sourceList) {
61 this.sourceList = sourceList;
62 selectedDeviceList = new HashSet<Device>();
66 public void create() {
68 setTitle("Remove from Device");
69 setMessage("Select one or more devices from which the simple resource will be removed.");
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);
82 Label lbl = new Label(container, SWT.NONE);
83 lbl.setText("Select Resources from:");
86 gd.grabExcessHorizontalSpace = true;
87 gd.horizontalAlignment = SWT.FILL;
88 lbl.setLayoutData(gd);
90 Group resourceGroup = new Group(container, SWT.NONE);
92 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
94 resourceGroup.setLayout(new GridLayout());
96 gd.grabExcessHorizontalSpace = true;
97 gd.horizontalAlignment = SWT.FILL;
99 gd.horizontalSpan = 2;
100 resourceGroup.setLayoutData(gd);
102 treeViewer = new CheckboxTreeViewer(resourceGroup);
103 treeViewer.getTree().setBackground(color);
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() {
117 public void checkStateChanged(CheckStateChangedEvent e) {
118 Device res = (Device) e.getElement();
120 if (e.getChecked()) {
121 selectedDeviceList.add(res);
122 System.out.println("Checked" + res.getDeviceName());
124 selectedDeviceList.remove(res);
125 System.out.println("Unchecked:" + res.getDeviceName());
134 public Set<Device> getSelectedDeviceList() {
135 return selectedDeviceList;
138 class TreeContentProvider implements ITreeContentProvider {
141 public void dispose() {
145 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
149 public Object[] getChildren(Object parent) {
154 public Object[] getElements(Object parent) {
155 if (null == sourceList) {
156 sourceList = new ArrayList<Device>();
158 return sourceList.toArray();
162 public Object getParent(Object child) {
167 public boolean hasChildren(Object parent) {
172 class TreeLabelProvider extends LabelProvider {
174 public String getText(Object element) {
175 Device res = (Device) element;
176 return res.getDeviceName();
180 public Image getImage(Object element) {
181 if (element instanceof Device) {
182 return Activator.getDefault().getImageRegistry()
183 .get(Constants.DEVICE);
191 protected void okPressed() {
192 if (selectedDeviceList.isEmpty()) {
193 MessageDialog.openInformation(
194 Display.getDefault().getActiveShell(),
195 "No selection done.", "No devices are selected.");
202 public boolean isHelpAvailable() {