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.HashSet;
20 import java.util.List;
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;
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;
47 * This class shows UI for deleting resources.
49 public class DeleteResourcePage extends WizardPage {
51 private CheckboxTreeViewer singleTreeViewer;
53 private List<SingleResource> singleSourceList;
55 private TreeViewContentHelper singleTreeViewContentHelper;
57 protected DeleteResourcePage() {
58 super("Delete Resources");
60 singleSourceList = Activator.getDefault().getResourceManager()
61 .getSingleResourceList();
65 public void createControl(Composite parent) {
66 setPageComplete(false);
67 setTitle("Delete Resources");
68 setMessage("Select one or more simple resources for deletion.");
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);
75 createSingleResourcesArea(container);
77 setControl(container);
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);
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);
93 Group resourceGroup = new Group(singleContainer, SWT.NONE);
95 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
97 resourceGroup.setLayout(new GridLayout());
99 gd.grabExcessHorizontalSpace = true;
100 gd.horizontalAlignment = SWT.FILL;
102 gd.horizontalSpan = 2;
103 resourceGroup.setLayoutData(gd);
105 if (null == singleSourceList || singleSourceList.isEmpty()) {
109 singleTreeViewer = new CheckboxTreeViewer(resourceGroup);
110 singleTreeViewer.getTree().setBackground(color);
112 gd.grabExcessHorizontalSpace = true;
113 gd.horizontalAlignment = SWT.FILL;
114 gd.grabExcessVerticalSpace = true;
115 gd.verticalAlignment = SWT.FILL;
117 singleTreeViewer.getTree().setLayoutData(gd);
119 .setContentProvider(new SingleResourceContentProvider());
120 singleTreeViewer.setLabelProvider(new TreeLabelProvider());
121 singleTreeViewer.setInput(new TreeNodeContentProvider());
122 singleTreeViewer.addCheckStateListener(new ICheckStateListener() {
125 public void checkStateChanged(CheckStateChangedEvent e) {
126 Object element = e.getElement();
127 if (element instanceof TreeViewContentHelper) {
128 singleTreeViewer.setGrayed(singleTreeViewContentHelper,
130 singleTreeViewer.setChecked(singleTreeViewContentHelper,
132 singleTreeViewer.setSubtreeChecked(element, e.getChecked());
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) {
144 if (checkedCount == singleSourceList.size()) {
145 singleTreeViewer.setGrayed(
146 singleTreeViewContentHelper, false);
147 singleTreeViewer.setChecked(
148 singleTreeViewContentHelper, true);
150 if (checkedCount > 0) {
151 singleTreeViewer.setGrayed(
152 singleTreeViewContentHelper, true);
153 singleTreeViewer.setChecked(
154 singleTreeViewContentHelper, true);
156 singleTreeViewer.setGrayed(
157 singleTreeViewContentHelper, false);
158 singleTreeViewer.setChecked(
159 singleTreeViewContentHelper, false);
164 setPageComplete(isSelectionDone());
167 singleTreeViewer.expandAll();
171 public Set<SingleResource> getSelectedSingleResourcesList() {
172 final Set<SingleResource> singles = new HashSet<SingleResource>();
173 Display.getDefault().syncExec(new Runnable() {
177 if (null == singleTreeViewer) {
180 Object selection[] = singleTreeViewer.getCheckedElements();
181 if (null == selection || selection.length < 1) {
184 for (Object obj : selection) {
185 if (obj instanceof Resource) {
186 singles.add((SingleResource) obj);
194 class SingleResourceContentProvider implements ITreeContentProvider {
197 public void dispose() {
201 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
205 public Object[] getChildren(Object parent) {
206 if (parent instanceof TreeViewContentHelper) {
207 return ((TreeViewContentHelper) parent).getResources()
214 public Object[] getElements(Object parent) {
215 Object obj[] = new Object[1];
216 singleTreeViewContentHelper = new TreeViewContentHelper(
218 obj[0] = singleTreeViewContentHelper;
223 public Object getParent(Object child) {
228 public boolean hasChildren(Object parent) {
229 if (parent instanceof TreeViewContentHelper) {
236 class TreeLabelProvider extends LabelProvider {
238 public String getText(Object element) {
239 if (element instanceof TreeViewContentHelper) {
242 Resource res = (Resource) element;
243 return res.getResourceName() + " (" + res.getResourceURI() + ")";
247 public Image getImage(Object element) {
248 if (element instanceof SingleResource) {
249 return Activator.getDefault().getImageRegistry()
250 .get(Constants.SINGLE_RESOURCE);
257 class TreeViewContentHelper {
258 List<? extends Resource> resources;
260 public TreeViewContentHelper(List<? extends Resource> resources) {
261 this.resources = resources;
264 public void setResources(List<? extends Resource> resources) {
265 this.resources = resources;
268 public List<? extends Resource> getResources() {
273 public boolean isSelectionDone() {
274 if (null != singleTreeViewer) {
275 Object obj[] = singleTreeViewer.getCheckedElements();
276 if (null != obj && obj.length > 0) {