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 org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.dialogs.TrayDialog;
22 import org.eclipse.jface.window.Window;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.List;
34 import org.eclipse.swt.widgets.Shell;
36 import java.util.ArrayList;
39 import oic.simulator.serviceprovider.utils.Constants;
42 * Dialog for starting and stopping the automatic verifications.
44 public class UpdateResourceInterfaceDialog extends TrayDialog {
46 private Button addIfTypeBtn;
47 private Button removeIfTypeBtn;
49 private List ifTypesList;
51 private Map<String, String> supportedResInterfaces;
52 private Map<String, String> updatedResInterfaces;
54 public UpdateResourceInterfaceDialog(Shell shell,
55 Map<String, String> updatedResInterfaces,
56 Map<String, String> supportedResInterfaces) {
58 this.updatedResInterfaces = updatedResInterfaces;
59 this.supportedResInterfaces = supportedResInterfaces;
63 protected void configureShell(Shell shell) {
64 super.configureShell(shell);
68 protected Control createDialogArea(Composite parent) {
69 Composite composite = (Composite) super.createDialogArea(parent);
70 getShell().setText("Resource Interfaces");
72 Composite content = new Composite(parent, SWT.NULL);
73 GridLayout gridLayout = new GridLayout(3, false);
74 content.setLayout(gridLayout);
75 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
76 content.setLayoutData(gd);
78 ifTypesList = new List(content, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL
81 gd.horizontalAlignment = SWT.FILL;
82 gd.grabExcessHorizontalSpace = true;
83 gd.verticalAlignment = SWT.FILL;
84 gd.grabExcessHorizontalSpace = true;
85 gd.horizontalSpan = 2;
86 ifTypesList.setLayoutData(gd);
87 ifTypesList.setBackground(Display.getDefault().getSystemColor(
90 Composite ifTypesOpComp = new Composite(content, SWT.NONE);
91 gridLayout = new GridLayout();
92 ifTypesOpComp.setLayout(gridLayout);
94 gd.verticalAlignment = SWT.TOP;
95 ifTypesOpComp.setLayoutData(gd);
97 addIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
98 addIfTypeBtn.setText("Add");
101 addIfTypeBtn.setLayoutData(gd);
102 if (null != updatedResInterfaces && null != supportedResInterfaces
103 && updatedResInterfaces.size() == supportedResInterfaces.size()) {
104 addIfTypeBtn.setEnabled(false);
107 removeIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
108 removeIfTypeBtn.setText("Remove");
111 removeIfTypeBtn.setLayoutData(gd);
112 removeIfTypeBtn.setEnabled(false);
121 private void initInterfaceList() {
122 for (Map.Entry<String, String> entry : updatedResInterfaces.entrySet()) {
123 ifTypesList.add(entry.getKey());
127 private void addUiListeners() {
128 addIfTypeBtn.addSelectionListener(new SelectionAdapter() {
130 public void widgetSelected(SelectionEvent e) {
131 boolean addAll = false;
132 int itemsAddedCount = ifTypesList.getItemCount();
133 if (itemsAddedCount < supportedResInterfaces.size()) {
135 ArrayList<String> pendingItems = new ArrayList<String>();
136 if (0 == itemsAddedCount) {
139 for (Map.Entry<String, String> entry : supportedResInterfaces
141 key = entry.getKey();
142 if (addAll || -1 == ifTypesList.indexOf(key)) {
143 pendingItems.add(key);
147 AddInterfaceTypeDialog addIfTypeDlg = new AddInterfaceTypeDialog(
148 getShell(), pendingItems);
149 if (Window.CANCEL == addIfTypeDlg.open()) {
152 String ifType = addIfTypeDlg.getValue();
153 if (null == ifType || ifType.isEmpty()) {
157 ifTypesList.add(ifType);
158 ifTypesList.deselectAll();
159 ifTypesList.select(ifTypesList.getItemCount() - 1);
160 ifTypesList.showSelection();
161 removeIfTypeBtn.setEnabled(true);
163 if (itemsAddedCount + 1 == supportedResInterfaces.size()) {
164 addIfTypeBtn.setEnabled(false);
170 removeIfTypeBtn.addSelectionListener(new SelectionAdapter() {
172 public void widgetSelected(SelectionEvent e) {
173 int[] selection = ifTypesList.getSelectionIndices();
174 if (null != selection && selection.length > 0) {
175 ifTypesList.remove(selection);
178 ifTypesList.deselectAll();
179 removeIfTypeBtn.setEnabled(false);
180 addIfTypeBtn.setEnabled(true);
184 ifTypesList.addSelectionListener(new SelectionAdapter() {
186 public void widgetSelected(SelectionEvent e) {
187 int[] selection = ifTypesList.getSelectionIndices();
188 if (null != selection && selection.length > 0) {
189 removeIfTypeBtn.setEnabled(true);
196 protected void okPressed() {
197 String[] items = ifTypesList.getItems();
198 if (null == items || items.length == 0) {
202 "Default Interface Type Selection",
203 "As no interface types are added, the resource will be "
204 + "configured with the default interface type(oic.if.baseline).");
205 ifTypesList.add("Baseline" + " ("
206 + Constants.DEFAULT_SINGLE_RESOURCE_INTERFACE + ")");
209 // Clearing the map to freshly add selected items.
210 updatedResInterfaces.clear();
211 for (String item : ifTypesList.getItems()) {
212 String value = supportedResInterfaces.get(item);
213 updatedResInterfaces.put(item, value);
219 protected Point getInitialSize() {
220 Point actualSize = super.getInitialSize();
221 return new Point(actualSize.x + 100, actualSize.y);
225 protected Button createButton(Composite parent, int id, String label,
226 boolean defaultButton) {
227 if (id == IDialogConstants.CANCEL_ID) {
230 return super.createButton(parent, id, label, defaultButton);
234 public boolean isHelpAvailable() {