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