Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / UpdateResourceInterfaceDialog.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 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;
35
36 import java.util.ArrayList;
37 import java.util.Map;
38
39 import oic.simulator.serviceprovider.utils.Constants;
40
41 /**
42  * Dialog for starting and stopping the automatic verifications.
43  */
44 public class UpdateResourceInterfaceDialog extends TrayDialog {
45
46     private Button              addIfTypeBtn;
47     private Button              removeIfTypeBtn;
48
49     private List                ifTypesList;
50
51     private Map<String, String> supportedResInterfaces;
52     private Map<String, String> updatedResInterfaces;
53
54     public UpdateResourceInterfaceDialog(Shell shell,
55             Map<String, String> updatedResInterfaces,
56             Map<String, String> supportedResInterfaces) {
57         super(shell);
58         this.updatedResInterfaces = updatedResInterfaces;
59         this.supportedResInterfaces = supportedResInterfaces;
60     }
61
62     @Override
63     protected void configureShell(Shell shell) {
64         super.configureShell(shell);
65     }
66
67     @Override
68     protected Control createDialogArea(Composite parent) {
69         Composite composite = (Composite) super.createDialogArea(parent);
70         getShell().setText("Resource Interfaces");
71
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);
77
78         ifTypesList = new List(content, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL
79                 | SWT.H_SCROLL);
80         gd = new GridData();
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(
88                 SWT.COLOR_WHITE));
89
90         Composite ifTypesOpComp = new Composite(content, SWT.NONE);
91         gridLayout = new GridLayout();
92         ifTypesOpComp.setLayout(gridLayout);
93         gd = new GridData();
94         gd.verticalAlignment = SWT.TOP;
95         ifTypesOpComp.setLayoutData(gd);
96
97         addIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
98         addIfTypeBtn.setText("Add");
99         gd = new GridData();
100         gd.widthHint = 70;
101         addIfTypeBtn.setLayoutData(gd);
102         if (null != updatedResInterfaces && null != supportedResInterfaces
103                 && updatedResInterfaces.size() == supportedResInterfaces.size()) {
104             addIfTypeBtn.setEnabled(false);
105         }
106
107         removeIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
108         removeIfTypeBtn.setText("Remove");
109         gd = new GridData();
110         gd.widthHint = 70;
111         removeIfTypeBtn.setLayoutData(gd);
112         removeIfTypeBtn.setEnabled(false);
113
114         initInterfaceList();
115
116         addUiListeners();
117
118         return composite;
119     }
120
121     private void initInterfaceList() {
122         if (null != updatedResInterfaces) {
123             for (Map.Entry<String, String> entry : updatedResInterfaces
124                     .entrySet()) {
125                 ifTypesList.add(entry.getKey());
126             }
127         }
128     }
129
130     private void addUiListeners() {
131         addIfTypeBtn.addSelectionListener(new SelectionAdapter() {
132             @Override
133             public void widgetSelected(SelectionEvent e) {
134                 boolean addAll = false;
135                 int itemsAddedCount = ifTypesList.getItemCount();
136                 if (itemsAddedCount < supportedResInterfaces.size()) {
137                     String key;
138                     ArrayList<String> pendingItems = new ArrayList<String>();
139                     if (0 == itemsAddedCount) {
140                         addAll = true;
141                     }
142                     for (Map.Entry<String, String> entry : supportedResInterfaces
143                             .entrySet()) {
144                         key = entry.getKey();
145                         if (addAll || -1 == ifTypesList.indexOf(key)) {
146                             pendingItems.add(key);
147                         }
148                     }
149
150                     AddInterfaceTypeDialog addIfTypeDlg = new AddInterfaceTypeDialog(
151                             getShell(), pendingItems);
152                     if (Window.CANCEL == addIfTypeDlg.open()) {
153                         return;
154                     }
155                     String ifType = addIfTypeDlg.getValue();
156                     if (null == ifType || ifType.isEmpty()) {
157                         return;
158                     }
159
160                     ifTypesList.add(ifType);
161                     ifTypesList.deselectAll();
162                     ifTypesList.select(ifTypesList.getItemCount() - 1);
163                     ifTypesList.showSelection();
164                     removeIfTypeBtn.setEnabled(true);
165
166                     if (itemsAddedCount + 1 == supportedResInterfaces.size()) {
167                         addIfTypeBtn.setEnabled(false);
168                     }
169                 }
170             }
171         });
172
173         removeIfTypeBtn.addSelectionListener(new SelectionAdapter() {
174             @Override
175             public void widgetSelected(SelectionEvent e) {
176                 int[] selection = ifTypesList.getSelectionIndices();
177                 if (null != selection && selection.length > 0) {
178                     ifTypesList.remove(selection);
179                 }
180
181                 ifTypesList.deselectAll();
182                 removeIfTypeBtn.setEnabled(false);
183                 addIfTypeBtn.setEnabled(true);
184             }
185         });
186
187         ifTypesList.addSelectionListener(new SelectionAdapter() {
188             @Override
189             public void widgetSelected(SelectionEvent e) {
190                 int[] selection = ifTypesList.getSelectionIndices();
191                 if (null != selection && selection.length > 0) {
192                     removeIfTypeBtn.setEnabled(true);
193                 }
194             }
195         });
196     }
197
198     @Override
199     protected void okPressed() {
200         String[] items = ifTypesList.getItems();
201         if (null == items || items.length == 0) {
202             MessageDialog
203                     .openInformation(
204                             getShell(),
205                             "Default Interface Type Selection",
206                             "As no interface types are added, the resource will be "
207                                     + "configured with the default interface type(oic.if.baseline).");
208             ifTypesList.add("Baseline" + " ("
209                     + Constants.DEFAULT_SINGLE_RESOURCE_INTERFACE + ")");
210         }
211
212         // Clearing the map to freshly add selected items.
213         updatedResInterfaces.clear();
214         for (String item : ifTypesList.getItems()) {
215             String value = supportedResInterfaces.get(item);
216             updatedResInterfaces.put(item, value);
217         }
218         close();
219     }
220
221     @Override
222     protected Point getInitialSize() {
223         Point actualSize = super.getInitialSize();
224         return new Point(actualSize.x + 100, actualSize.y);
225     }
226
227     @Override
228     protected Button createButton(Composite parent, int id, String label,
229             boolean defaultButton) {
230         if (id == IDialogConstants.CANCEL_ID) {
231             return null;
232         }
233         return super.createButton(parent, id, label, defaultButton);
234     }
235
236     @Override
237     public boolean isHelpAvailable() {
238         return false;
239     }
240 }