Imported Upstream version 1.1.1
[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.TrayDialog;
21 import org.eclipse.jface.window.Window;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.swt.widgets.List;
33 import org.eclipse.swt.widgets.Shell;
34
35 import java.util.ArrayList;
36 import java.util.Map;
37
38 /**
39  * Dialog for starting and stopping the automatic verifications.
40  */
41 public class UpdateResourceInterfaceDialog extends TrayDialog {
42
43     private Button              addIfTypeBtn;
44     private Button              removeIfTypeBtn;
45
46     private List                ifTypesList;
47
48     private Map<String, String> supportedResInterfaces;
49     private Map<String, String> updatedResInterfaces;
50
51     public UpdateResourceInterfaceDialog(Shell shell,
52             Map<String, String> updatedResInterfaces,
53             Map<String, String> supportedResInterfaces) {
54         super(shell);
55         this.updatedResInterfaces = updatedResInterfaces;
56         this.supportedResInterfaces = supportedResInterfaces;
57     }
58
59     @Override
60     protected void configureShell(Shell shell) {
61         super.configureShell(shell);
62     }
63
64     @Override
65     protected Control createDialogArea(Composite parent) {
66         Composite composite = (Composite) super.createDialogArea(parent);
67         getShell().setText("Resource Interfaces");
68
69         Composite content = new Composite(parent, SWT.NULL);
70         GridLayout gridLayout = new GridLayout(3, false);
71         content.setLayout(gridLayout);
72         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
73         content.setLayoutData(gd);
74
75         ifTypesList = new List(content, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL
76                 | SWT.H_SCROLL);
77         gd = new GridData();
78         gd.horizontalAlignment = SWT.FILL;
79         gd.grabExcessHorizontalSpace = true;
80         gd.verticalAlignment = SWT.FILL;
81         gd.grabExcessHorizontalSpace = true;
82         gd.horizontalSpan = 2;
83         ifTypesList.setLayoutData(gd);
84         ifTypesList.setBackground(Display.getDefault().getSystemColor(
85                 SWT.COLOR_WHITE));
86
87         Composite ifTypesOpComp = new Composite(content, SWT.NONE);
88         gridLayout = new GridLayout();
89         ifTypesOpComp.setLayout(gridLayout);
90         gd = new GridData();
91         gd.verticalAlignment = SWT.TOP;
92         ifTypesOpComp.setLayoutData(gd);
93
94         addIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
95         addIfTypeBtn.setText("Add");
96         gd = new GridData();
97         gd.widthHint = 70;
98         addIfTypeBtn.setLayoutData(gd);
99         if (null != updatedResInterfaces && null != supportedResInterfaces
100                 && updatedResInterfaces.size() == supportedResInterfaces.size()) {
101             addIfTypeBtn.setEnabled(false);
102         }
103
104         removeIfTypeBtn = new Button(ifTypesOpComp, SWT.PUSH);
105         removeIfTypeBtn.setText("Remove");
106         gd = new GridData();
107         gd.widthHint = 70;
108         removeIfTypeBtn.setLayoutData(gd);
109         removeIfTypeBtn.setEnabled(false);
110
111         initInterfaceList();
112
113         addUiListeners();
114
115         return composite;
116     }
117
118     private void initInterfaceList() {
119         if (null != updatedResInterfaces) {
120             for (Map.Entry<String, String> entry : updatedResInterfaces
121                     .entrySet()) {
122                 ifTypesList.add(entry.getKey());
123             }
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         // Clearing the map to freshly add selected items.
198         updatedResInterfaces.clear();
199
200         String[] items = ifTypesList.getItems();
201         if (null != items && items.length > 0) {
202             for (String item : items) {
203                 String value = supportedResInterfaces.get(item);
204                 updatedResInterfaces.put(item, value);
205             }
206         }
207         close();
208     }
209
210     @Override
211     protected Point getInitialSize() {
212         Point actualSize = super.getInitialSize();
213         return new Point(actualSize.x + 100, actualSize.y);
214     }
215
216     @Override
217     protected Button createButton(Composite parent, int id, String label,
218             boolean defaultButton) {
219         if (id == IDialogConstants.CANCEL_ID) {
220             return null;
221         }
222         return super.createButton(parent, id, label, defaultButton);
223     }
224
225     @Override
226     public boolean isHelpAvailable() {
227         return false;
228     }
229 }