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