60b7349956f61656695bb3029be309ae40081580
[platform/upstream/iotivity.git] /
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         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                     if (null == ifType || ifType.isEmpty()) {
154                         return;
155                     }
156
157                     ifTypesList.add(ifType);
158                     ifTypesList.deselectAll();
159                     ifTypesList.select(ifTypesList.getItemCount() - 1);
160                     ifTypesList.showSelection();
161                     removeIfTypeBtn.setEnabled(true);
162
163                     if (itemsAddedCount + 1 == supportedResInterfaces.size()) {
164                         addIfTypeBtn.setEnabled(false);
165                     }
166                 }
167             }
168         });
169
170         removeIfTypeBtn.addSelectionListener(new SelectionAdapter() {
171             @Override
172             public void widgetSelected(SelectionEvent e) {
173                 int[] selection = ifTypesList.getSelectionIndices();
174                 if (null != selection && selection.length > 0) {
175                     ifTypesList.remove(selection);
176                 }
177
178                 ifTypesList.deselectAll();
179                 removeIfTypeBtn.setEnabled(false);
180                 addIfTypeBtn.setEnabled(true);
181             }
182         });
183
184         ifTypesList.addSelectionListener(new SelectionAdapter() {
185             @Override
186             public void widgetSelected(SelectionEvent e) {
187                 int[] selection = ifTypesList.getSelectionIndices();
188                 if (null != selection && selection.length > 0) {
189                     removeIfTypeBtn.setEnabled(true);
190                 }
191             }
192         });
193     }
194
195     @Override
196     protected void okPressed() {
197         String[] items = ifTypesList.getItems();
198         if (null == items || items.length == 0) {
199             MessageDialog
200                     .openInformation(
201                             getShell(),
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 + ")");
207         }
208
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);
214         }
215         close();
216     }
217
218     @Override
219     protected Point getInitialSize() {
220         Point actualSize = super.getInitialSize();
221         return new Point(actualSize.x + 100, actualSize.y);
222     }
223
224     @Override
225     protected Button createButton(Composite parent, int id, String label,
226             boolean defaultButton) {
227         if (id == IDialogConstants.CANCEL_ID) {
228             return null;
229         }
230         return super.createButton(parent, id, label, defaultButton);
231     }
232
233     @Override
234     public boolean isHelpAvailable() {
235         return false;
236     }
237 }