Java SDK and Eclipse plugin for simulator.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / view / dialogs / VerificationDialog.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.clientcontroller.view.dialogs;
18
19 import java.util.ArrayList;
20 import java.util.Map;
21 import java.util.Set;
22
23 import oic.simulator.clientcontroller.Activator;
24 import oic.simulator.clientcontroller.manager.ResourceManager;
25 import oic.simulator.clientcontroller.utils.Constants;
26
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.dialogs.TrayDialog;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Display;
39 import org.eclipse.swt.widgets.Group;
40 import org.eclipse.swt.widgets.Shell;
41
42 /**
43  * Dialog for starting and stopping the automatic verifications.
44  */
45 public class VerificationDialog extends TrayDialog {
46     private Map<String, Boolean> automationStatus;
47
48     public VerificationDialog(Shell shell, Map<String, Boolean> automationStatus) {
49         super(shell);
50         this.automationStatus = automationStatus;
51     }
52
53     @Override
54     protected void configureShell(Shell shell) {
55         super.configureShell(shell);
56     }
57
58     @Override
59     protected Control createDialogArea(Composite parent) {
60         Composite composite = (Composite) super.createDialogArea(parent);
61         createAutomationGroup(composite);
62         getShell().setText("Automation Settings");
63         return composite;
64     }
65
66     /**
67      * Dynamically creates a check-box list for enabling/disabling different
68      * types of automation
69      */
70     private void createAutomationGroup(Composite parent) {
71         Group group = new Group(parent, SWT.NONE);
72         group.setLayout(new GridLayout());
73         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
74         gd.widthHint = 275;
75         group.setLayoutData(gd);
76         group.setText("Automation Levels");
77
78         Set<String> keySet = automationStatus.keySet();
79         if (null == keySet) {
80             return;
81         }
82         ArrayList<String> list = new ArrayList<String>(
83                 automationStatus.keySet());
84         for (final String str : list) {
85             final Button checkbox = new Button(group, SWT.CHECK);
86             checkbox.setText(str);
87             checkbox.setSelection(automationStatus.get(str));
88             checkbox.addSelectionListener(new SelectionAdapter() {
89
90                 @Override
91                 public void widgetSelected(SelectionEvent e) {
92                     boolean checked = checkbox.getSelection();
93                     boolean answer = MessageDialog.openQuestion(Display
94                             .getDefault().getActiveShell(), "Verification",
95                             "Do you want to "
96                                     + (checked ? "enable" : "disable")
97                                     + " the verification?");
98                     if (!answer) {
99                         checkbox.setSelection(!checked);
100                         checked = !checked;
101                     } else {
102                         ResourceManager resourceManager = Activator
103                                 .getDefault().getResourceManager();
104                         String reqTypeTxt = checkbox.getText();
105                         int reqType;
106                         if (reqTypeTxt.equals("Get")) {
107                             reqType = Constants.GET_AUTOMATION_INDEX;
108                         } else if (reqTypeTxt.equals("Put")) {
109                             reqType = Constants.PUT_AUTOMATION_INDEX;
110                         } else {// if(reqTypeTxt.equals("Post")) {
111                             reqType = Constants.POST_AUTOMATION_INDEX;
112                         }
113                         if (checked) {
114                             resourceManager.startAutomationRequest(reqType,
115                                     resourceManager
116                                             .getCurrentResourceInSelection());
117                         } else {
118                             resourceManager.stopAutomationRequest(reqType,
119                                     resourceManager
120                                             .getCurrentResourceInSelection());
121                         }
122                     }
123                     automationStatus.put(str, checked);
124                 }
125             });
126         }
127     }
128
129     @Override
130     protected Button createButton(Composite parent, int id, String label,
131             boolean defaultButton) {
132         if (id == IDialogConstants.CANCEL_ID) {
133             return null;
134         }
135         return super.createButton(parent, id, label, defaultButton);
136     }
137
138     @Override
139     public boolean isHelpAvailable() {
140         return false;
141     }
142 }