1 | package de.ugoe.cs.swe.exercises.settings; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileInputStream; |
---|
5 | import java.io.FileNotFoundException; |
---|
6 | import java.io.FileOutputStream; |
---|
7 | import java.io.IOException; |
---|
8 | import java.sql.DriverManager; |
---|
9 | import java.sql.SQLException; |
---|
10 | import java.util.Properties; |
---|
11 | |
---|
12 | import org.eclipse.jface.dialogs.IMessageProvider; |
---|
13 | import org.eclipse.jface.dialogs.TitleAreaDialog; |
---|
14 | import org.eclipse.jface.resource.JFaceResources; |
---|
15 | import org.eclipse.swt.SWT; |
---|
16 | import org.eclipse.swt.events.SelectionAdapter; |
---|
17 | import org.eclipse.swt.events.SelectionEvent; |
---|
18 | import org.eclipse.swt.layout.GridData; |
---|
19 | import org.eclipse.swt.layout.GridLayout; |
---|
20 | import org.eclipse.swt.widgets.Button; |
---|
21 | import org.eclipse.swt.widgets.Combo; |
---|
22 | import org.eclipse.swt.widgets.Composite; |
---|
23 | import org.eclipse.swt.widgets.Control; |
---|
24 | import org.eclipse.swt.widgets.Label; |
---|
25 | import org.eclipse.swt.widgets.Shell; |
---|
26 | import org.eclipse.swt.widgets.Text; |
---|
27 | import org.tmatesoft.svn.core.SVNException; |
---|
28 | import org.tmatesoft.svn.core.SVNURL; |
---|
29 | import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; |
---|
30 | import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; |
---|
31 | import org.tmatesoft.svn.core.io.SVNRepository; |
---|
32 | import org.tmatesoft.svn.core.io.SVNRepositoryFactory; |
---|
33 | import org.tmatesoft.svn.core.wc.SVNWCUtil; |
---|
34 | |
---|
35 | import de.ugoe.cs.swe.exercises.misc.Model; |
---|
36 | |
---|
37 | public class SettingsDialog extends TitleAreaDialog { |
---|
38 | |
---|
39 | private Text textSVNLocation; |
---|
40 | private Text textWorkingDirectory; |
---|
41 | private Text textSVNUsername; |
---|
42 | private Text textSVNPassword; |
---|
43 | private Text textMySQLHost; |
---|
44 | private Text textMySQLDb; |
---|
45 | private Text textMySQLUsername; |
---|
46 | private Text textMySQLPassword; |
---|
47 | private Text textPdflatex; |
---|
48 | private Text textResearchGroup; |
---|
49 | private Text textResearchGroupUrl; |
---|
50 | private Combo cboUseSSL; |
---|
51 | |
---|
52 | public SettingsDialog(Shell parentShell) { |
---|
53 | super(parentShell); |
---|
54 | this.getFileContents(); |
---|
55 | setTitle("Settings"); |
---|
56 | } |
---|
57 | |
---|
58 | @Override |
---|
59 | protected Control createContents(Composite parent) { |
---|
60 | Control contents = super.createContents(parent); |
---|
61 | setTitle("Settings"); |
---|
62 | setMessage("Change the Settings to match your Settings", |
---|
63 | IMessageProvider.INFORMATION); |
---|
64 | return contents; |
---|
65 | } |
---|
66 | |
---|
67 | @Override |
---|
68 | protected Control createDialogArea(Composite parent) { |
---|
69 | |
---|
70 | GridLayout layout = new GridLayout(); |
---|
71 | layout.numColumns = 2; |
---|
72 | parent.setLayout(layout); |
---|
73 | Label label1 = new Label(parent, SWT.NONE); |
---|
74 | label1.setText("SVN Location:"); |
---|
75 | Text textbox1 = new Text(parent, SWT.BORDER); |
---|
76 | textbox1.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
77 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
78 | textSVNLocation = textbox1; |
---|
79 | |
---|
80 | Label label2 = new Label(parent, SWT.NONE); |
---|
81 | label2.setText("Working Directory:"); |
---|
82 | Text textbox2 = new Text(parent, SWT.BORDER); |
---|
83 | textbox2.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
84 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
85 | textWorkingDirectory = textbox2; |
---|
86 | |
---|
87 | Label label3 = new Label(parent, SWT.NONE); |
---|
88 | label3.setText("SVN Username:"); |
---|
89 | Text textbox3 = new Text(parent, SWT.BORDER); |
---|
90 | textbox3.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
91 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
92 | textSVNUsername = textbox3; |
---|
93 | |
---|
94 | Label label4 = new Label(parent, SWT.NONE); |
---|
95 | label4.setText("SVN Password:"); |
---|
96 | Text textbox4 = new Text(parent, SWT.BORDER | SWT.PASSWORD); |
---|
97 | textbox4.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
98 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
99 | textSVNPassword = textbox4; |
---|
100 | |
---|
101 | Label label5 = new Label(parent, SWT.NONE); |
---|
102 | label5.setText("MySQL Host:"); |
---|
103 | Text textbox5 = new Text(parent, SWT.BORDER); |
---|
104 | textbox5.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
105 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
106 | textMySQLHost = textbox5; |
---|
107 | |
---|
108 | Label label6 = new Label(parent, SWT.NONE); |
---|
109 | label6.setText("MySQL Database:"); |
---|
110 | Text textbox6 = new Text(parent, SWT.BORDER); |
---|
111 | textbox6.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
112 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
113 | textMySQLDb = textbox6; |
---|
114 | |
---|
115 | Label label7 = new Label(parent, SWT.NONE); |
---|
116 | label7.setText("MySQL Username:"); |
---|
117 | Text textbox7 = new Text(parent, SWT.BORDER); |
---|
118 | textbox7.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
119 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
120 | textMySQLUsername = textbox7; |
---|
121 | |
---|
122 | Label label8 = new Label(parent, SWT.NONE); |
---|
123 | label8.setText("MySQL Password:"); |
---|
124 | Text textbox8 = new Text(parent, SWT.BORDER | SWT.PASSWORD); |
---|
125 | textbox8.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
126 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
127 | textMySQLPassword = textbox8; |
---|
128 | |
---|
129 | Label label9 = new Label(parent, SWT.NONE); |
---|
130 | label9.setText("Use SSL:"); |
---|
131 | Combo combo1 = new Combo(parent, SWT.LEFT); |
---|
132 | combo1.add("No SSL"); |
---|
133 | combo1.add("Use SSL"); |
---|
134 | combo1.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
135 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
136 | cboUseSSL = combo1; |
---|
137 | |
---|
138 | Label label10 = new Label(parent, SWT.NONE); |
---|
139 | label10.setText("PDFLatex Path"); |
---|
140 | Text textbox10 = new Text(parent, SWT.BORDER); |
---|
141 | textbox10.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
142 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
143 | textPdflatex = textbox10; |
---|
144 | |
---|
145 | Label label11 = new Label(parent, SWT.NONE); |
---|
146 | label11.setText("Research Group"); |
---|
147 | textResearchGroup = new Text(parent, SWT.BORDER); |
---|
148 | textResearchGroup.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
149 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
150 | |
---|
151 | Label label12 = new Label(parent, SWT.NONE); |
---|
152 | label12.setText("Research Group Url"); |
---|
153 | textResearchGroupUrl = new Text(parent, SWT.BORDER); |
---|
154 | textResearchGroupUrl.setLayoutData(new GridData( |
---|
155 | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
156 | |
---|
157 | Settings settings = Settings.getInstance(); |
---|
158 | if (settings.isMysqlSSL()) |
---|
159 | cboUseSSL.select(1); |
---|
160 | else |
---|
161 | cboUseSSL.select(0); |
---|
162 | |
---|
163 | textSVNLocation.setText(settings.getSvnLocation()); |
---|
164 | textWorkingDirectory.setText(settings.getWorkingDirectory()); |
---|
165 | textSVNUsername.setText(settings.getSvnUsername()); |
---|
166 | textSVNPassword.setText(settings.getSvnPassword()); |
---|
167 | textMySQLHost.setText(settings.getMysqlHost()); |
---|
168 | textMySQLDb.setText(settings.getMysqlDb()); |
---|
169 | textMySQLUsername.setText(settings.getMysqlUsername()); |
---|
170 | textMySQLPassword.setText(settings.getMysqlPassword()); |
---|
171 | textPdflatex.setText(settings.getPdfLatexPath()); |
---|
172 | textResearchGroup.setText(settings.getResearchGroup()); |
---|
173 | textResearchGroupUrl.setText(settings.getResearchGroupUrl()); |
---|
174 | |
---|
175 | GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END); |
---|
176 | gd.horizontalSpan = 1; |
---|
177 | |
---|
178 | return parent; |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | @Override |
---|
183 | protected void createButtonsForButtonBar(Composite parent) { |
---|
184 | ((GridLayout) parent.getLayout()).numColumns = 2; |
---|
185 | |
---|
186 | GridData buttonlayout = new GridData(SWT.LEFT, SWT.BEGINNING, false, |
---|
187 | false); |
---|
188 | buttonlayout.minimumWidth = 200; |
---|
189 | Button button = new Button(parent, SWT.PUSH); |
---|
190 | button.setText("OK"); |
---|
191 | button.setFont(JFaceResources.getDialogFont()); |
---|
192 | button.setLayoutData(buttonlayout); |
---|
193 | button.addSelectionListener(new SelectionAdapter() { |
---|
194 | public void widgetSelected(SelectionEvent e) { |
---|
195 | if (textSVNLocation.getText().length() != 0 |
---|
196 | && textWorkingDirectory.getText().length() != 0 |
---|
197 | && textSVNUsername.getText().length() != 0 |
---|
198 | && textSVNPassword.getText().length() != 0) { |
---|
199 | |
---|
200 | Settings settings = Settings.getInstance(); |
---|
201 | |
---|
202 | settings.setSvnLocation(textSVNLocation.getText()); |
---|
203 | settings.setWorkingDirectory(textWorkingDirectory.getText()); |
---|
204 | settings.setSvnUsername(textSVNUsername.getText()); |
---|
205 | settings.setSvnPassword(textSVNPassword.getText()); |
---|
206 | settings.setMysqlHost(textMySQLHost.getText()); |
---|
207 | settings.setMysqlDb(textMySQLDb.getText()); |
---|
208 | settings.setMysqlUsername(textMySQLUsername.getText()); |
---|
209 | settings.setMysqlPassword(textMySQLPassword.getText()); |
---|
210 | settings.setPdfLatexPath(textPdflatex.getText()); |
---|
211 | settings.setResearchGroup(textResearchGroup.getText()); |
---|
212 | settings.setResearchGroupUrl(textResearchGroupUrl.getText()); |
---|
213 | |
---|
214 | if (cboUseSSL.getSelectionIndex() == 1) |
---|
215 | settings.setMysqlSSL(true); |
---|
216 | else |
---|
217 | settings.setMysqlSSL(false); |
---|
218 | |
---|
219 | try { |
---|
220 | DAVRepositoryFactory.setup(); |
---|
221 | SVNURL url = SVNURL.parseURIDecoded(settings.getSvnLocation()); |
---|
222 | SVNRepository repository = SVNRepositoryFactory |
---|
223 | .create(url); |
---|
224 | ISVNAuthenticationManager authManager = SVNWCUtil |
---|
225 | .createDefaultAuthenticationManager( |
---|
226 | settings.getSvnUsername(), settings.getSvnPassword()); |
---|
227 | ((SVNRepository) repository) |
---|
228 | .setAuthenticationManager(authManager); |
---|
229 | repository.testConnection(); |
---|
230 | |
---|
231 | } catch (SVNException ex) { |
---|
232 | setErrorMessage(ex.getMessage()); |
---|
233 | return; |
---|
234 | } |
---|
235 | |
---|
236 | System.setProperty("javax.net.ssl.trustStorePassword", |
---|
237 | settings.getMysqlPassword()); |
---|
238 | String path = Model.class.getProtectionDomain() |
---|
239 | .getCodeSource().getLocation().getPath(); |
---|
240 | if (path.endsWith("bin/")) |
---|
241 | path = path.replaceFirst("bin/$", ""); |
---|
242 | path += "keystore"; |
---|
243 | System.setProperty("javax.net.ssl.trustStore", path); |
---|
244 | |
---|
245 | try { |
---|
246 | // Step 1: Load the JDBC driver. |
---|
247 | Class.forName("com.mysql.jdbc.Driver").newInstance(); |
---|
248 | // Step 2: Establish the connection to the database. |
---|
249 | String url = "jdbc:mysql://" + settings.getMysqlHost() + "/" |
---|
250 | + settings.getMysqlDb() + "?useSSL=" |
---|
251 | + String.valueOf(settings.isMysqlSSL()); |
---|
252 | DriverManager.getConnection(url, settings.getMysqlUsername(), |
---|
253 | settings.getMysqlPassword()); |
---|
254 | } catch (SQLException ex) { |
---|
255 | setErrorMessage(ex.getMessage()); |
---|
256 | return; |
---|
257 | } catch (Exception ex2) { |
---|
258 | setErrorMessage(ex2.getMessage()); |
---|
259 | return; |
---|
260 | } |
---|
261 | setFileContents(); |
---|
262 | close(); |
---|
263 | |
---|
264 | } else { |
---|
265 | setErrorMessage("Please fill out all fields"); |
---|
266 | } |
---|
267 | } |
---|
268 | }); |
---|
269 | Button cancel = new Button(parent, SWT.PUSH); |
---|
270 | cancel.setText("Cancel"); |
---|
271 | cancel.setFont(JFaceResources.getDialogFont()); |
---|
272 | cancel.setLayoutData(buttonlayout); |
---|
273 | cancel.addSelectionListener(new SelectionAdapter() { |
---|
274 | public void widgetSelected(SelectionEvent e) { |
---|
275 | close(); |
---|
276 | } |
---|
277 | }); |
---|
278 | } |
---|
279 | |
---|
280 | private void getFileContents() { |
---|
281 | Properties properties = new Properties(); |
---|
282 | File file = new File(System.getProperty("user.home") |
---|
283 | + "/.exerciseSettings"); |
---|
284 | if (file.exists()) { |
---|
285 | try { |
---|
286 | properties.load(new FileInputStream(file)); |
---|
287 | } catch (FileNotFoundException e) { |
---|
288 | e.printStackTrace(); |
---|
289 | } catch (IOException e) { |
---|
290 | e.printStackTrace(); |
---|
291 | } |
---|
292 | try { |
---|
293 | Settings settings = Settings.getInstance(); |
---|
294 | settings.setSvnLocation(properties.getProperty("svnLocation")); |
---|
295 | settings.setWorkingDirectory(properties.getProperty("workingDirectory")); |
---|
296 | settings.setSvnUsername(properties.getProperty("svnUsername")); |
---|
297 | settings.setSvnPassword(properties.getProperty("svnPassword")); |
---|
298 | settings.setMysqlHost(properties.getProperty("mySQLHost")); |
---|
299 | settings.setMysqlDb(properties.getProperty("mySQLDb")); |
---|
300 | settings.setMysqlUsername(properties.getProperty("mySQLUsername")); |
---|
301 | settings.setMysqlPassword(properties.getProperty("mySQLPassword")); |
---|
302 | settings.setMysqlSSL(Boolean.parseBoolean(properties |
---|
303 | .getProperty("mySQLcheckSSL"))); |
---|
304 | settings.setPdfLatexPath(properties.getProperty("pdfLatexPath")); |
---|
305 | settings.setResearchGroup(properties.getProperty("researchGroup")); |
---|
306 | settings.setResearchGroupUrl(properties.getProperty("researchGroupUrl")); |
---|
307 | } catch (Exception ex) { |
---|
308 | } |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | private void setFileContents() { |
---|
313 | Properties properties = new Properties(); |
---|
314 | File file = new File(System.getProperty("user.home") |
---|
315 | + "/.exerciseSettings"); |
---|
316 | if (!file.exists()) { |
---|
317 | try { |
---|
318 | file.createNewFile(); |
---|
319 | } catch (IOException e) { |
---|
320 | e.printStackTrace(); |
---|
321 | return; |
---|
322 | } |
---|
323 | } |
---|
324 | Settings settings = Settings.getInstance(); |
---|
325 | |
---|
326 | properties.setProperty("svnLocation", settings.getSvnLocation()); |
---|
327 | properties.setProperty("workingDirectory", settings.getWorkingDirectory()); |
---|
328 | properties.setProperty("svnUsername", settings.getSvnUsername()); |
---|
329 | properties.setProperty("svnPassword", settings.getSvnPassword()); |
---|
330 | properties.setProperty("mySQLHost", settings.getMysqlHost()); |
---|
331 | properties.setProperty("mySQLDb", settings.getMysqlDb()); |
---|
332 | properties.setProperty("mySQLUsername", settings.getMysqlUsername()); |
---|
333 | properties.setProperty("mySQLPassword", settings.getMysqlPassword()); |
---|
334 | properties.setProperty("mySQLcheckSSL", String.valueOf(settings.isMysqlSSL())); |
---|
335 | properties.setProperty("pdfLatexPath", settings.getPdfLatexPath()); |
---|
336 | properties.setProperty("researchGroup", settings.getResearchGroup()); |
---|
337 | properties.setProperty("researchGroupUrl", settings.getResearchGroupUrl()); |
---|
338 | |
---|
339 | try { |
---|
340 | properties.store(new FileOutputStream(file), |
---|
341 | "SVN and other Settings included"); |
---|
342 | } catch (FileNotFoundException e) { |
---|
343 | e.printStackTrace(); |
---|
344 | } catch (IOException e) { |
---|
345 | e.printStackTrace(); |
---|
346 | } |
---|
347 | } |
---|
348 | |
---|
349 | } |
---|