33557d5853a65e696cc51adcffc1e484ca82fb9b
[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 java.util.ArrayList;
20 import java.util.Map;
21
22 import oic.simulator.serviceprovider.utils.Constants;
23
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.dialogs.TrayDialog;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.List;
39 import org.eclipse.swt.widgets.Shell;
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         for (Map.Entry<String, String> entry : updatedResInterfaces.entrySet()) {
123             ifTypesList.add(entry.getKey());
124         }
125     }
126
127     private void addUiListeners() {
128         addIfTypeBtn.addSelectionListener(new SelectionAdapter() {
129             @Override
130             public void widgetSelected(SelectionEvent e) {
131                 boolean addAll = false;
132                 int itemsAddedCount = ifTypesList.getItemCount();
133                 if (itemsAddedCount < supportedResInterfaces.size()) {
134                     String key;
135                     ArrayList<String> pendingItems = new ArrayList<String>();
136                     if (0 == itemsAddedCount) {
137                         addAll = true;
138                     }
139                     for (Map.Entry<String, String> entry : supportedResInterfaces
140                             .entrySet()) {
141                         key = entry.getKey();
142                         if (addAll || -1 == ifTypesList.indexOf(key)) {
143                             pendingItems.add(key);
144                         }
145                     }
146
147                     AddInterfaceTypeDialog addIfTypeDlg = new AddInterfaceTypeDialog(
148                             getShell(), pendingItems);
149                     if (Window.CANCEL == addIfTypeDlg.open()) {
150                         return;
151                     }
152                     String ifType = addIfTypeDlg.getValue();
153                     ifTypesList.add(ifType);
154                     ifTypesList.deselectAll();
155                     ifTypesList.select(ifTypesList.getItemCount() - 1);
156                     ifTypesList.showSelection();
157                     removeIfTypeBtn.setEnabled(true);
158
159                     if (itemsAddedCount + 1 == supportedResInterfaces.size()) {
160                         addIfTypeBtn.setEnabled(false);
161                     }
162                 }
163             }
164         });
165
166         removeIfTypeBtn.addSelectionListener(new SelectionAdapter() {
167             @Override
168             public void widgetSelected(SelectionEvent e) {
169                 int[] selection = ifTypesList.getSelectionIndices();
170                 if (null != selection && selection.length > 0) {
171                     ifTypesList.remove(selection);
172                 }
173
174                 ifTypesList.deselectAll();
175                 removeIfTypeBtn.setEnabled(false);
176                 addIfTypeBtn.setEnabled(true);
177             }
178         });
179
180         ifTypesList.addSelectionListener(new SelectionAdapter() {
181             @Override
182             public void widgetSelected(SelectionEvent e) {
183                 int[] selection = ifTypesList.getSelectionIndices();
184                 if (null != selection && selection.length > 0) {
185                     removeIfTypeBtn.setEnabled(true);
186                 }
187             }
188         });
189     }
190
191     @Override
192     protected void okPressed() {
193         String[] items = ifTypesList.getItems();
194         if (null == items || items.length == 0) {
195             MessageDialog
196                     .openInformation(
197                             getShell(),
198                             "Default Interface Type Selection",
199                             "As no interface types are added, the resource will be "
200                                     + "configured with the default interface type(oic.if.baseline).");
201             ifTypesList.add("Baseline" + " ("
202                     + Constants.DEFAULT_SINGLE_RESOURCE_INTERFACE + ")");
203         }
204
205         // Clearing the map to freshly add selected items.
206         updatedResInterfaces.clear();
207         for (String item : ifTypesList.getItems()) {
208             String value = supportedResInterfaces.get(item);
209             updatedResInterfaces.put(item, value);
210         }
211         close();
212     }
213
214     @Override
215     protected Point getInitialSize() {
216         Point actualSize = super.getInitialSize();
217         return new Point(actualSize.x + 100, actualSize.y);
218     }
219
220     @Override
221     protected Button createButton(Composite parent, int id, String label,
222             boolean defaultButton) {
223         if (id == IDialogConstants.CANCEL_ID) {
224             return null;
225         }
226         return super.createButton(parent, id, label, defaultButton);
227     }
228
229     @Override
230     public boolean isHelpAvailable() {
231         return false;
232     }
233 }