Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / view / dialogs / GetRequestDialog.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 org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.dialogs.TitleAreaDialog;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Combo;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Group;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
34
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.Map;
38 import java.util.Vector;
39
40 import oic.simulator.clientcontroller.Activator;
41 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
42 import oic.simulator.clientcontroller.utils.Constants;
43 import oic.simulator.clientcontroller.utils.Utility;
44
45 /**
46  * This dialog is used for generating a PUT request.
47  */
48 public class GetRequestDialog extends TitleAreaDialog {
49     private Text                queryTxt;
50     private Combo               ifTypesCmb;
51
52     private String              otherFilters;
53     private String              ifType;
54
55     private Map<String, String> ifTypes;
56
57     public GetRequestDialog(Shell parentShell) {
58         super(parentShell);
59     }
60
61     @Override
62     public void create() {
63         super.create();
64         setTitle("Generate GET Request");
65         setMessage("Dialog which takes query filters and generates a GET request.");
66     }
67
68     @Override
69     protected Control createDialogArea(Composite parent) {
70         Composite compLayout = (Composite) super.createDialogArea(parent);
71
72         Group paramsGrp = new Group(compLayout, SWT.NONE);
73         paramsGrp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
74         GridLayout layout = new GridLayout(2, false);
75         layout.verticalSpacing = 10;
76         layout.marginTop = 10;
77         paramsGrp.setLayout(layout);
78
79         Label ifTypeLbl = new Label(paramsGrp, SWT.NONE);
80         ifTypeLbl.setText("Interface Type");
81
82         ifTypesCmb = new Combo(paramsGrp, SWT.NULL);
83         GridData gd = new GridData();
84         gd.grabExcessHorizontalSpace = true;
85         gd.horizontalAlignment = SWT.FILL;
86         ifTypesCmb.setLayoutData(gd);
87         ifTypesCmb.addModifyListener(new ModifyListener() {
88             @Override
89             public void modifyText(ModifyEvent e) {
90                 ifType = ifTypesCmb.getText();
91             }
92         });
93
94         RemoteResource resource = Activator.getDefault().getResourceManager()
95                 .getCurrentResourceInSelection();
96
97         // Set the interface types in combo box.
98         Map<String, String> ifTypes = Utility.getResourceInterfaces();
99         this.ifTypes = new HashMap<String, String>();
100         String key;
101         for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
102             key = entry.getValue() + " (" + entry.getKey() + ")";
103             this.ifTypes.put(key, entry.getKey());
104             ifTypesCmb.add(key);
105         }
106
107         // Select the default value to be shown in the interface types combo.
108         Vector<String> ifTypesSupportedByResource = resource
109                 .getRemoteResourceRef().getResourceInterfaces();
110         if (null != ifTypesSupportedByResource) {
111             int index = -1;
112             if (ifTypesSupportedByResource
113                     .contains(Constants.BASELINE_INTERFACE)
114                     && ifTypes.containsKey(Constants.BASELINE_INTERFACE)) {
115                 // Baseline interface is given preference to be shown in the if
116                 // types combo.
117                 String value = ifTypes.get(Constants.BASELINE_INTERFACE);
118                 index = ifTypesCmb.indexOf(value + " ("
119                         + Constants.BASELINE_INTERFACE + ")");
120                 if (index != -1)
121                     ifTypesCmb.select(index);
122             }
123             if (index == -1) {
124                 // Baseline interface is not selected so selecting some other
125                 // interface supported by the resource.
126                 Iterator<String> itr = ifTypesSupportedByResource.iterator();
127                 while (itr.hasNext() && index == -1) {
128                     key = itr.next();
129                     if (ifTypes.containsKey(key)) {
130                         String value = ifTypes.get(key);
131                         index = ifTypesCmb.indexOf(value + " (" + key + ")");
132                         if (index != -1) {
133                             ifTypesCmb.select(index);
134                             break;
135                         }
136                     }
137                 }
138                 if (index == -1 && !ifTypesSupportedByResource.isEmpty()) {
139                     // Resource has custom interfaces.
140                     ifTypesCmb.setText(ifTypesSupportedByResource.get(0));
141                 }
142             }
143         }
144
145         Label otherFilterLbl = new Label(paramsGrp, SWT.NONE);
146         otherFilterLbl.setText("Other filters");
147
148         queryTxt = new Text(paramsGrp, SWT.BORDER);
149         gd = new GridData();
150         gd.grabExcessHorizontalSpace = true;
151         gd.horizontalAlignment = SWT.FILL;
152         queryTxt.setLayoutData(gd);
153         queryTxt.addModifyListener(new ModifyListener() {
154             @Override
155             public void modifyText(ModifyEvent e) {
156                 otherFilters = queryTxt.getText();
157             }
158         });
159
160         return compLayout;
161     }
162
163     @Override
164     protected boolean isResizable() {
165         return true;
166     }
167
168     @Override
169     public boolean isHelpAvailable() {
170         return false;
171     }
172
173     @Override
174     protected Button createButton(Composite parent, int id, String label,
175             boolean defaultButton) {
176         if (id == IDialogConstants.OK_ID) {
177             label = "GET";
178         }
179         return super.createButton(parent, id, label, defaultButton);
180     }
181
182     public String getOtherFilters() {
183         return otherFilters;
184     }
185
186     public String getIfType() {
187         if (ifTypes.containsKey(ifType)) {
188             return ifTypes.get(ifType);
189         }
190         return ifType;
191     }
192
193 }