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 | private String svnLocation = ""; |
---|
52 | private String workingDirectory = ""; |
---|
53 | private String svnUsername = ""; |
---|
54 | private String svnPassword = ""; |
---|
55 | private String mySQLHost = ""; |
---|
56 | private String mySQLDb = ""; |
---|
57 | private String mySQLUsername = ""; |
---|
58 | private String mySQLPassword = ""; |
---|
59 | private String myPDFLatexPath = ""; |
---|
60 | private String myResearchGroup = ""; |
---|
61 | private String myResearchGroupUrl = ""; |
---|
62 | private boolean mySQLcheckSSL = false; |
---|
63 | |
---|
64 | public boolean hasSettings() { |
---|
65 | if (svnLocation == "" || workingDirectory == "" || svnUsername == "" |
---|
66 | || svnPassword == "" || mySQLHost == "" || mySQLDb == "" |
---|
67 | || mySQLUsername == "" || mySQLPassword == "" |
---|
68 | || myPDFLatexPath == "") { |
---|
69 | return false; |
---|
70 | } |
---|
71 | return true; |
---|
72 | } |
---|
73 | |
---|
74 | public String getmyPDFLatexPath() { |
---|
75 | return myPDFLatexPath; |
---|
76 | } |
---|
77 | |
---|
78 | public void setmyPDFLatexPath(String myPDFLatexPath) { |
---|
79 | this.myPDFLatexPath = myPDFLatexPath; |
---|
80 | } |
---|
81 | |
---|
82 | public String getSVNLocation() { |
---|
83 | return svnLocation; |
---|
84 | } |
---|
85 | |
---|
86 | public String getWorkingDirectory() { |
---|
87 | return workingDirectory; |
---|
88 | } |
---|
89 | |
---|
90 | public String getSVNUsername() { |
---|
91 | return svnUsername; |
---|
92 | } |
---|
93 | |
---|
94 | public String getSVNPassword() { |
---|
95 | return svnPassword; |
---|
96 | } |
---|
97 | |
---|
98 | public String getMySQLHost() { |
---|
99 | return mySQLHost; |
---|
100 | } |
---|
101 | |
---|
102 | public String getMySQLDb() { |
---|
103 | return mySQLDb; |
---|
104 | } |
---|
105 | |
---|
106 | public String getMySQLUsername() { |
---|
107 | return mySQLUsername; |
---|
108 | } |
---|
109 | |
---|
110 | public String getMySQLPassword() { |
---|
111 | return mySQLPassword; |
---|
112 | } |
---|
113 | |
---|
114 | public boolean isMySQLcheckSSL() { |
---|
115 | return mySQLcheckSSL; |
---|
116 | } |
---|
117 | |
---|
118 | public SettingsDialog(Shell parentShell) { |
---|
119 | super(parentShell); |
---|
120 | this.getFileContents(); |
---|
121 | } |
---|
122 | |
---|
123 | @Override |
---|
124 | protected Control createContents(Composite parent) { |
---|
125 | Control contents = super.createContents(parent); |
---|
126 | setTitle("Settings"); |
---|
127 | setMessage("Change the Settings to match your Settings", |
---|
128 | IMessageProvider.INFORMATION); |
---|
129 | return contents; |
---|
130 | } |
---|
131 | |
---|
132 | @Override |
---|
133 | protected Control createDialogArea(Composite parent) { |
---|
134 | |
---|
135 | GridLayout layout = new GridLayout(); |
---|
136 | layout.numColumns = 2; |
---|
137 | parent.setLayout(layout); |
---|
138 | Label label1 = new Label(parent, SWT.NONE); |
---|
139 | label1.setText("SVN Location:"); |
---|
140 | Text textbox1 = new Text(parent, SWT.BORDER); |
---|
141 | textbox1.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
142 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
143 | textSVNLocation = textbox1; |
---|
144 | |
---|
145 | Label label2 = new Label(parent, SWT.NONE); |
---|
146 | label2.setText("Working Directory:"); |
---|
147 | Text textbox2 = new Text(parent, SWT.BORDER); |
---|
148 | textbox2.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
149 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
150 | textWorkingDirectory = textbox2; |
---|
151 | |
---|
152 | Label label3 = new Label(parent, SWT.NONE); |
---|
153 | label3.setText("SVN Username:"); |
---|
154 | Text textbox3 = new Text(parent, SWT.BORDER); |
---|
155 | textbox3.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
156 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
157 | textSVNUsername = textbox3; |
---|
158 | |
---|
159 | Label label4 = new Label(parent, SWT.NONE); |
---|
160 | label4.setText("SVN Password:"); |
---|
161 | Text textbox4 = new Text(parent, SWT.BORDER | SWT.PASSWORD); |
---|
162 | textbox4.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
163 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
164 | textSVNPassword = textbox4; |
---|
165 | |
---|
166 | Label label5 = new Label(parent, SWT.NONE); |
---|
167 | label5.setText("MySQL Host:"); |
---|
168 | Text textbox5 = new Text(parent, SWT.BORDER); |
---|
169 | textbox5.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
170 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
171 | textMySQLHost = textbox5; |
---|
172 | |
---|
173 | Label label6 = new Label(parent, SWT.NONE); |
---|
174 | label6.setText("MySQL Database:"); |
---|
175 | Text textbox6 = new Text(parent, SWT.BORDER); |
---|
176 | textbox6.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
177 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
178 | textMySQLDb = textbox6; |
---|
179 | |
---|
180 | Label label7 = new Label(parent, SWT.NONE); |
---|
181 | label7.setText("MySQL Username:"); |
---|
182 | Text textbox7 = new Text(parent, SWT.BORDER); |
---|
183 | textbox7.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
184 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
185 | textMySQLUsername = textbox7; |
---|
186 | |
---|
187 | Label label8 = new Label(parent, SWT.NONE); |
---|
188 | label8.setText("MySQL Password:"); |
---|
189 | Text textbox8 = new Text(parent, SWT.BORDER | SWT.PASSWORD); |
---|
190 | textbox8.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
191 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
192 | textMySQLPassword = textbox8; |
---|
193 | |
---|
194 | Label label9 = new Label(parent, SWT.NONE); |
---|
195 | label9.setText("Use SSL:"); |
---|
196 | Combo combo1 = new Combo(parent, SWT.LEFT); |
---|
197 | combo1.add("No SSL"); |
---|
198 | combo1.add("Use SSL"); |
---|
199 | combo1.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
200 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
201 | cboUseSSL = combo1; |
---|
202 | |
---|
203 | Label label10 = new Label(parent, SWT.NONE); |
---|
204 | label10.setText("PDFLatex Path"); |
---|
205 | Text textbox10 = new Text(parent, SWT.BORDER); |
---|
206 | textbox10.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
207 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
208 | textPdflatex = textbox10; |
---|
209 | |
---|
210 | Label label11 = new Label(parent, SWT.NONE); |
---|
211 | label11.setText("Research Group"); |
---|
212 | textResearchGroup = new Text(parent, SWT.BORDER); |
---|
213 | textResearchGroup.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
---|
214 | | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
215 | |
---|
216 | Label label12 = new Label(parent, SWT.NONE); |
---|
217 | label12.setText("Research Group Url"); |
---|
218 | textResearchGroupUrl = new Text(parent, SWT.BORDER); |
---|
219 | textResearchGroupUrl.setLayoutData(new GridData( |
---|
220 | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL)); |
---|
221 | |
---|
222 | if (mySQLcheckSSL) |
---|
223 | cboUseSSL.select(1); |
---|
224 | else |
---|
225 | cboUseSSL.select(0); |
---|
226 | |
---|
227 | textSVNLocation.setText(svnLocation); |
---|
228 | textWorkingDirectory.setText(workingDirectory); |
---|
229 | textSVNUsername.setText(svnUsername); |
---|
230 | textSVNPassword.setText(svnPassword); |
---|
231 | textMySQLHost.setText(mySQLHost); |
---|
232 | textMySQLDb.setText(mySQLDb); |
---|
233 | textMySQLUsername.setText(mySQLUsername); |
---|
234 | textMySQLPassword.setText(mySQLPassword); |
---|
235 | textPdflatex.setText(myPDFLatexPath); |
---|
236 | textResearchGroup.setText(myResearchGroup); |
---|
237 | textResearchGroupUrl.setText(myResearchGroupUrl); |
---|
238 | |
---|
239 | GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END); |
---|
240 | gd.horizontalSpan = 1; |
---|
241 | |
---|
242 | return parent; |
---|
243 | |
---|
244 | } |
---|
245 | |
---|
246 | @Override |
---|
247 | protected void createButtonsForButtonBar(Composite parent) { |
---|
248 | ((GridLayout) parent.getLayout()).numColumns = 2; |
---|
249 | |
---|
250 | GridData buttonlayout = new GridData(SWT.LEFT, SWT.BEGINNING, false, |
---|
251 | false); |
---|
252 | buttonlayout.minimumWidth = 200; |
---|
253 | Button button = new Button(parent, SWT.PUSH); |
---|
254 | button.setText("OK"); |
---|
255 | button.setFont(JFaceResources.getDialogFont()); |
---|
256 | button.setLayoutData(buttonlayout); |
---|
257 | button.addSelectionListener(new SelectionAdapter() { |
---|
258 | public void widgetSelected(SelectionEvent e) { |
---|
259 | if (textSVNLocation.getText().length() != 0 |
---|
260 | && textWorkingDirectory.getText().length() != 0 |
---|
261 | && textSVNUsername.getText().length() != 0 |
---|
262 | && textSVNPassword.getText().length() != 0) { |
---|
263 | svnLocation = textSVNLocation.getText(); |
---|
264 | workingDirectory = textWorkingDirectory.getText(); |
---|
265 | svnUsername = textSVNUsername.getText(); |
---|
266 | svnPassword = textSVNPassword.getText(); |
---|
267 | mySQLHost = textMySQLHost.getText(); |
---|
268 | mySQLDb = textMySQLDb.getText(); |
---|
269 | mySQLUsername = textMySQLUsername.getText(); |
---|
270 | mySQLPassword = textMySQLPassword.getText(); |
---|
271 | myPDFLatexPath = textPdflatex.getText(); |
---|
272 | myResearchGroup = textResearchGroup.getText(); |
---|
273 | myResearchGroupUrl = textResearchGroupUrl.getText(); |
---|
274 | |
---|
275 | if (cboUseSSL.getSelectionIndex() == 1) |
---|
276 | mySQLcheckSSL = true; |
---|
277 | else |
---|
278 | mySQLcheckSSL = false; |
---|
279 | |
---|
280 | try { |
---|
281 | DAVRepositoryFactory.setup(); |
---|
282 | SVNURL url = SVNURL.parseURIDecoded(svnLocation); |
---|
283 | SVNRepository repository = SVNRepositoryFactory |
---|
284 | .create(url); |
---|
285 | ISVNAuthenticationManager authManager = SVNWCUtil |
---|
286 | .createDefaultAuthenticationManager( |
---|
287 | svnUsername, svnPassword); |
---|
288 | ((SVNRepository) repository) |
---|
289 | .setAuthenticationManager(authManager); |
---|
290 | repository.testConnection(); |
---|
291 | |
---|
292 | } catch (SVNException ex) { |
---|
293 | setErrorMessage(ex.getMessage()); |
---|
294 | return; |
---|
295 | } |
---|
296 | |
---|
297 | System.setProperty("javax.net.ssl.trustStorePassword", |
---|
298 | mySQLPassword); |
---|
299 | String path = Model.class.getProtectionDomain() |
---|
300 | .getCodeSource().getLocation().getPath(); |
---|
301 | if (path.endsWith("bin/")) |
---|
302 | path = path.replaceFirst("bin/$", ""); |
---|
303 | path += "keystore"; |
---|
304 | System.setProperty("javax.net.ssl.trustStore", path); |
---|
305 | |
---|
306 | try { |
---|
307 | // Step 1: Load the JDBC driver. |
---|
308 | Class.forName("com.mysql.jdbc.Driver").newInstance(); |
---|
309 | // Step 2: Establish the connection to the database. |
---|
310 | String url = "jdbc:mysql://" + mySQLHost + "/" |
---|
311 | + mySQLDb + "?useSSL=" |
---|
312 | + String.valueOf(mySQLcheckSSL); |
---|
313 | DriverManager.getConnection(url, mySQLUsername, |
---|
314 | mySQLPassword); |
---|
315 | } catch (SQLException ex) { |
---|
316 | setErrorMessage(ex.getMessage()); |
---|
317 | return; |
---|
318 | } catch (Exception ex2) { |
---|
319 | setErrorMessage(ex2.getMessage()); |
---|
320 | return; |
---|
321 | } |
---|
322 | setFileContents(); |
---|
323 | close(); |
---|
324 | |
---|
325 | } else { |
---|
326 | setErrorMessage("Please fill out all fields"); |
---|
327 | } |
---|
328 | } |
---|
329 | }); |
---|
330 | Button cancel = new Button(parent, SWT.PUSH); |
---|
331 | cancel.setText("Cancel"); |
---|
332 | cancel.setFont(JFaceResources.getDialogFont()); |
---|
333 | cancel.setLayoutData(buttonlayout); |
---|
334 | cancel.addSelectionListener(new SelectionAdapter() { |
---|
335 | public void widgetSelected(SelectionEvent e) { |
---|
336 | close(); |
---|
337 | } |
---|
338 | }); |
---|
339 | } |
---|
340 | |
---|
341 | private void getFileContents() { |
---|
342 | Properties settings = new Properties(); |
---|
343 | File file = new File(System.getProperty("user.home") |
---|
344 | + "/.exerciseSettings"); |
---|
345 | if (!file.exists()) { |
---|
346 | svnLocation = ""; |
---|
347 | workingDirectory = ""; |
---|
348 | svnUsername = ""; |
---|
349 | svnPassword = ""; |
---|
350 | mySQLHost = ""; |
---|
351 | mySQLDb = ""; |
---|
352 | mySQLUsername = ""; |
---|
353 | mySQLPassword = ""; |
---|
354 | mySQLcheckSSL = false; |
---|
355 | myPDFLatexPath = ""; |
---|
356 | myResearchGroup = ""; |
---|
357 | myResearchGroupUrl = ""; |
---|
358 | } else { |
---|
359 | try { |
---|
360 | settings.load(new FileInputStream(file)); |
---|
361 | } catch (FileNotFoundException e) { |
---|
362 | e.printStackTrace(); |
---|
363 | } catch (IOException e) { |
---|
364 | e.printStackTrace(); |
---|
365 | } |
---|
366 | try { |
---|
367 | if (settings.getProperty("svnLocation") != null) |
---|
368 | svnLocation = settings.getProperty("svnLocation"); |
---|
369 | if (settings.getProperty("workingDirectory") != null) |
---|
370 | workingDirectory = settings.getProperty("workingDirectory"); |
---|
371 | if (settings.getProperty("svnUsername") != null) |
---|
372 | svnUsername = settings.getProperty("svnUsername"); |
---|
373 | if (settings.getProperty("svnPassword") != null) |
---|
374 | svnPassword = settings.getProperty("svnPassword"); |
---|
375 | if (settings.getProperty("mySQLHost") != null) |
---|
376 | mySQLHost = settings.getProperty("mySQLHost"); |
---|
377 | if (settings.getProperty("mySQLDb") != null) |
---|
378 | mySQLDb = settings.getProperty("mySQLDb"); |
---|
379 | if (settings.getProperty("mySQLUsername") != null) |
---|
380 | mySQLUsername = settings.getProperty("mySQLUsername"); |
---|
381 | if (settings.getProperty("mySQLPassword") != null) |
---|
382 | mySQLPassword = settings.getProperty("mySQLPassword"); |
---|
383 | if (settings.getProperty("mySQLcheckSSL") != null) |
---|
384 | mySQLcheckSSL = Boolean.parseBoolean(settings |
---|
385 | .getProperty("mySQLcheckSSL")); |
---|
386 | if (settings.getProperty("mySQLcheckSSL") != null) |
---|
387 | myPDFLatexPath = settings.getProperty("myPDFLatexPath"); |
---|
388 | if (settings.getProperty("myResearchGroup") != null) |
---|
389 | myResearchGroup = settings.getProperty("myResearchGroup"); |
---|
390 | if (settings.getProperty("myResearchGroupUrl") != null) |
---|
391 | myResearchGroupUrl = settings |
---|
392 | .getProperty("myResearchGroupUrl"); |
---|
393 | |
---|
394 | } catch (Exception ex) { |
---|
395 | svnLocation = ""; |
---|
396 | workingDirectory = ""; |
---|
397 | svnUsername = ""; |
---|
398 | svnPassword = ""; |
---|
399 | mySQLHost = ""; |
---|
400 | mySQLDb = ""; |
---|
401 | mySQLUsername = ""; |
---|
402 | mySQLPassword = ""; |
---|
403 | mySQLcheckSSL = false; |
---|
404 | myPDFLatexPath = ""; |
---|
405 | myResearchGroup = ""; |
---|
406 | myResearchGroupUrl = ""; |
---|
407 | } |
---|
408 | } |
---|
409 | } |
---|
410 | |
---|
411 | private void setFileContents() { |
---|
412 | Properties settings = new Properties(); |
---|
413 | File file = new File(System.getProperty("user.home") |
---|
414 | + "/.exerciseSettings"); |
---|
415 | if (!file.exists()) { |
---|
416 | try { |
---|
417 | file.createNewFile(); |
---|
418 | } catch (IOException e) { |
---|
419 | e.printStackTrace(); |
---|
420 | return; |
---|
421 | } |
---|
422 | } |
---|
423 | |
---|
424 | settings.setProperty("svnLocation", svnLocation); |
---|
425 | settings.setProperty("workingDirectory", workingDirectory); |
---|
426 | settings.setProperty("svnUsername", svnUsername); |
---|
427 | settings.setProperty("svnPassword", svnPassword); |
---|
428 | settings.setProperty("mySQLHost", mySQLHost); |
---|
429 | settings.setProperty("mySQLDb", mySQLDb); |
---|
430 | settings.setProperty("mySQLUsername", mySQLUsername); |
---|
431 | settings.setProperty("mySQLPassword", mySQLPassword); |
---|
432 | settings.setProperty("mySQLcheckSSL", String.valueOf(mySQLcheckSSL)); |
---|
433 | settings.setProperty("myPDFLatexPath", myPDFLatexPath); |
---|
434 | settings.setProperty("myResearchGroup", myResearchGroup); |
---|
435 | settings.setProperty("myResearchGroupUrl", myResearchGroupUrl); |
---|
436 | |
---|
437 | try { |
---|
438 | settings.store(new FileOutputStream(file), |
---|
439 | "SVN and other Settings included"); |
---|
440 | } catch (FileNotFoundException e) { |
---|
441 | e.printStackTrace(); |
---|
442 | } catch (IOException e) { |
---|
443 | e.printStackTrace(); |
---|
444 | } |
---|
445 | } |
---|
446 | |
---|
447 | public String getMyResearchGroup() { |
---|
448 | return myResearchGroup; |
---|
449 | } |
---|
450 | |
---|
451 | public String getMyResearchGroupUrl() { |
---|
452 | return myResearchGroupUrl; |
---|
453 | } |
---|
454 | |
---|
455 | } |
---|