8fa457bc36e6dbaf08d81146feadb4a379e6acb0
[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 import java.io.IOException;
37 import java.util.Date;
38
39 import org.oic.simulator.ILogger.Level;
40
41 import oic.simulator.clientcontroller.Activator;
42 import oic.simulator.clientcontroller.utils.Constants;
43 import oic.simulator.clientcontroller.utils.Utility;
44
45 /**
46  * This dialog is used for loading the RAML file.
47  */
48 public class LoadRAMLDialog extends TitleAreaDialog {
49
50     private Text   locationTxt;
51     private Button btnBrowse;
52     private String configFilePath;
53
54     public LoadRAMLDialog(Shell parentShell) {
55         super(parentShell);
56     }
57
58     @Override
59     public void create() {
60         super.create();
61         setTitle("Load Remote Resource Configuration");
62         setMessage("Select the RAML Configuration file of the resource to enable Automation.");
63     }
64
65     @Override
66     protected Control createDialogArea(Composite parent) {
67         Composite compLayout = (Composite) super.createDialogArea(parent);
68         Composite container = new Composite(compLayout, SWT.NONE);
69         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
70         GridLayout layout = new GridLayout(3, false);
71         layout.verticalSpacing = 10;
72         layout.marginTop = 10;
73         container.setLayout(layout);
74
75         Label loadRamlLbl = new Label(container, SWT.NONE);
76         loadRamlLbl.setText("Load RAML File");
77         GridData gd;
78         gd = new GridData();
79         gd.horizontalSpan = 3;
80         loadRamlLbl.setLayoutData(gd);
81
82         Label locationLbl = new Label(container, SWT.NONE);
83         locationLbl.setText("Location:");
84
85         locationTxt = new Text(container, SWT.BORDER);
86         gd = new GridData();
87         gd.minimumWidth = 300;
88         gd.horizontalAlignment = SWT.FILL;
89         gd.grabExcessHorizontalSpace = true;
90         locationTxt.setLayoutData(gd);
91
92         btnBrowse = new Button(container, SWT.NONE);
93         btnBrowse.setText("Browse");
94         gd = new GridData();
95         gd.widthHint = 80;
96         gd.horizontalAlignment = SWT.FILL;
97         btnBrowse.setLayoutData(gd);
98
99         addUIListeners();
100
101         return compLayout;
102     }
103
104     private void addUIListeners() {
105         btnBrowse.addSelectionListener(new SelectionAdapter() {
106             @Override
107             public void widgetSelected(SelectionEvent e) {
108                 FileDialog fileDialog = new FileDialog(PlatformUI
109                         .getWorkbench().getDisplay().getActiveShell(), SWT.NONE);
110                 fileDialog
111                         .setFilterExtensions(Constants.BROWSE_RAML_FILTER_EXTENSIONS);
112                 String path = fileDialog.open();
113                 if (null == path) {
114                     configFilePath = "";
115                 } else {
116                     configFilePath = path;
117                 }
118                 locationTxt.setText(configFilePath);
119             }
120         });
121     }
122
123     public String getConfigFilePath() {
124         return configFilePath;
125     }
126
127     @Override
128     protected void okPressed() {
129         configFilePath = locationTxt.getText();
130         if (null == configFilePath) {
131             return;
132         }
133
134         FileInputStream fileStream = null;
135         try {
136             fileStream = new FileInputStream(configFilePath);
137         } catch (Exception e) {
138             MessageDialog
139                     .openError(getShell(), "Invalid File",
140                             "File doesn't exist. Either the file path or file name is invalid.");
141             return;
142         } finally {
143             if (null != fileStream) {
144                 try {
145                     fileStream.close();
146                 } catch (IOException e) {
147                     Activator
148                             .getDefault()
149                             .getLogManager()
150                             .log(Level.ERROR.ordinal(),
151                                     new Date(),
152                                     "There is an error while closing the file stream.\n"
153                                             + Utility.getSimulatorErrorString(
154                                                     e, null));
155                 }
156             }
157         }
158         close();
159     }
160
161     @Override
162     public boolean isHelpAvailable() {
163         return false;
164     }
165 }