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.CollectionResource;
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 AddResourceToCollections extends TitleAreaDialog {
53 private CheckboxTreeViewer treeViewer;
55 List<CollectionResource> sourceList;
57 Set<CollectionResource> selectedResourceList;
59 public AddResourceToCollections(Shell parentShell,
60 List<CollectionResource> sourceList) {
62 this.sourceList = sourceList;
63 selectedResourceList = new HashSet<CollectionResource>();
67 public void create() {
69 setTitle("Add To Collections");
70 setMessage("Select one or more collection resources to which the single resource will be added.");
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);
83 Label lbl = new Label(container, SWT.NONE);
84 lbl.setText("Select Resources from:");
87 gd.grabExcessHorizontalSpace = true;
88 gd.horizontalAlignment = SWT.FILL;
89 lbl.setLayoutData(gd);
91 Group resourceGroup = new Group(container, SWT.NONE);
93 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
95 resourceGroup.setLayout(new GridLayout());
97 gd.grabExcessHorizontalSpace = true;
98 gd.horizontalAlignment = SWT.FILL;
100 gd.horizontalSpan = 2;
101 resourceGroup.setLayoutData(gd);
103 treeViewer = new CheckboxTreeViewer(resourceGroup);
104 treeViewer.getTree().setBackground(color);
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() {
118 public void checkStateChanged(CheckStateChangedEvent e) {
119 CollectionResource res = (CollectionResource) e.getElement();
121 if (e.getChecked()) {
122 selectedResourceList.add(res);
123 System.out.println("Checked" + res.getResourceName());
125 selectedResourceList.remove(res);
126 System.out.println("Unchecked:" + res.getResourceName());
135 public Set<CollectionResource> getSelectedResourceList() {
136 return selectedResourceList;
139 class TreeContentProvider implements ITreeContentProvider {
142 public void dispose() {
146 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
150 public Object[] getChildren(Object parent) {
155 public Object[] getElements(Object parent) {
156 if (null == sourceList) {
157 sourceList = new ArrayList<CollectionResource>();
159 return sourceList.toArray();
163 public Object getParent(Object child) {
168 public boolean hasChildren(Object parent) {
173 class TreeLabelProvider extends LabelProvider {
175 public String getText(Object element) {
176 CollectionResource res = (CollectionResource) element;
177 return res.getResourceName();
181 public Image getImage(Object element) {
182 if (element instanceof CollectionResource) {
183 return Activator.getDefault().getImageRegistry()
184 .get(Constants.COLLECTION_RESOURCE);
192 protected void okPressed() {
193 if (selectedResourceList.isEmpty()) {
194 MessageDialog.openInformation(
195 Display.getDefault().getActiveShell(),
196 "No selection done.", "No resources are selected.");
203 public boolean isHelpAvailable() {