Updated the order of imports in the source files of simulator plugins.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / view / dialogs / LoadRAMLDialog.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.MessageDialog;
20 import org.eclipse.jface.dialogs.TitleAreaDialog;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
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.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.FileDialog;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.PlatformUI;
34
35 import java.io.FileInputStream;
36
37 import oic.simulator.clientcontroller.utils.Constants;
38
39 /**
40  * This dialog is used for loading the RAML file.
41  */
42 public class LoadRAMLDialog extends TitleAreaDialog {
43
44     private Text   locationTxt;
45     private Button btnBrowse;
46     private String configFilePath;
47
48     public LoadRAMLDialog(Shell parentShell) {
49         super(parentShell);
50     }
51
52     @Override
53     public void create() {
54         super.create();
55         setTitle("Load Remote Resource Configuration");
56         setMessage("Select the RAML Configuration file of the resource to enable Automation.");
57     }
58
59     @Override
60     protected Control createDialogArea(Composite parent) {
61         Composite compLayout = (Composite) super.createDialogArea(parent);
62         Composite container = new Composite(compLayout, SWT.NONE);
63         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
64         GridLayout layout = new GridLayout(3, false);
65         layout.verticalSpacing = 10;
66         layout.marginTop = 10;
67         container.setLayout(layout);
68
69         Label loadRamlLbl = new Label(container, SWT.NONE);
70         loadRamlLbl.setText("Load RAML File");
71         GridData gd;
72         gd = new GridData();
73         gd.horizontalSpan = 3;
74         loadRamlLbl.setLayoutData(gd);
75
76         Label locationLbl = new Label(container, SWT.NONE);
77         locationLbl.setText("Location:");
78
79         locationTxt = new Text(container, SWT.BORDER);
80         gd = new GridData();
81         gd.minimumWidth = 300;
82         gd.horizontalAlignment = SWT.FILL;
83         gd.grabExcessHorizontalSpace = true;
84         locationTxt.setLayoutData(gd);
85
86         btnBrowse = new Button(container, SWT.NONE);
87         btnBrowse.setText("Browse");
88         gd = new GridData();
89         gd.widthHint = 80;
90         gd.horizontalAlignment = SWT.FILL;
91         btnBrowse.setLayoutData(gd);
92
93         addUIListeners();
94
95         return compLayout;
96     }
97
98     private void addUIListeners() {
99         btnBrowse.addSelectionListener(new SelectionAdapter() {
100             @Override
101             public void widgetSelected(SelectionEvent e) {
102                 FileDialog fileDialog = new FileDialog(PlatformUI
103                         .getWorkbench().getDisplay().getActiveShell(), SWT.NONE);
104                 fileDialog
105                         .setFilterExtensions(Constants.BROWSE_RAML_FILTER_EXTENSIONS);
106                 String path = fileDialog.open();
107                 if (null == path) {
108                     configFilePath = "";
109                 } else {
110                     configFilePath = path;
111                 }
112                 locationTxt.setText(configFilePath);
113             }
114         });
115     }
116
117     public String getConfigFilePath() {
118         return configFilePath;
119     }
120
121     @Override
122     protected void okPressed() {
123         configFilePath = locationTxt.getText();
124         if (null == configFilePath) {
125             return;
126         }
127         try {
128             new FileInputStream(configFilePath);
129         } catch (Exception e) {
130             MessageDialog
131                     .openError(getShell(), "Invalid File",
132                             "File doesn't exist. Either the file path or file name is invalid.");
133             return;
134         }
135         close();
136     }
137
138     @Override
139     public boolean isHelpAvailable() {
140         return false;
141     }
142 }