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 | Settings settings = Settings.getInstance(); |
---|
151 | if (settings.isMysqlSSL()) |
---|
152 | cboUseSSL.select(1); |
---|
153 | else |
---|
154 | cboUseSSL.select(0); |
---|
155 | |
---|
156 | textSVNLocation.setText(settings.getSvnLocation()); |
---|
157 | textWorkingDirectory.setText(settings.getWorkingDirectory()); |
---|
158 | textSVNUsername.setText(settings.getSvnUsername()); |
---|
159 | textSVNPassword.setText(settings.getSvnPassword()); |
---|
160 | textMySQLHost.setText(settings.getMysqlHost()); |
---|
161 | textMySQLDb.setText(settings.getMysqlDb()); |
---|
162 | textMySQLUsername.setText(settings.getMysqlUsername()); |
---|
163 | textMySQLPassword.setText(settings.getMysqlPassword()); |
---|
164 | textPdflatex.setText(settings.getPdfLatexPath()); |
---|
165 | textResearchGroup.setText(settings.getResearchGroup()); |
---|
166 | textResearchGroupUrl.setText(settings.getResearchGroupUrl()); |
---|
167 | |
---|
168 | GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END); |
---|
169 | gd.horizontalSpan = 1; |
---|
170 | |
---|
171 | return parent; |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | @Override |
---|
176 | protected void createButtonsForButtonBar(Composite parent) { |
---|
177 | ((GridLayout) parent.getLayout()).numColumns = 2; |
---|
178 | |
---|
179 | GridData buttonlayout = new GridData(SWT.LEFT, SWT.BEGINNING, false, |
---|
180 | false); |
---|
181 | buttonlayout.minimumWidth = 200; |
---|
182 | Button button = new Button(parent, SWT.PUSH); |
---|
183 | button.setText("OK"); |
---|
184 | button.setFont(JFaceResources.getDialogFont()); |
---|
185 | button.setLayoutData(buttonlayout); |
---|
186 | button.addSelectionListener(new SelectionAdapter() { |
---|
187 | public void widgetSelected(SelectionEvent e) { |
---|
188 | if (textSVNLocation.getText().length() != 0 |
---|
189 | && textWorkingDirectory.getText().length() != 0 |
---|
190 | && textSVNUsername.getText().length() != 0 |
---|
191 | && textSVNPassword.getText().length() != 0) { |
---|
192 | |
---|
193 | Settings settings = Settings.getInstance(); |
---|
194 | |
---|
195 | settings.setSvnLocation(textSVNLocation.getText()); |
---|
196 | settings.setWorkingDirectory(textWorkingDirectory.getText()); |
---|
197 | settings.setSvnUsername(textSVNUsername.getText()); |
---|
198 | settings.setSvnPassword(textSVNPassword.getText()); |
---|
199 | settings.setMysqlHost(textMySQLHost.getText()); |
---|
200 | settings.setMysqlDb(textMySQLDb.getText()); |
---|
201 | settings.setMysqlUsername(textMySQLUsername.getText()); |
---|
202 | settings.setMysqlPassword(textMySQLPassword.getText()); |
---|
203 | settings.setPdfLatexPath(textPdflatex.getText()); |
---|
204 | settings.setResearchGroup(textResearchGroup.getText()); |
---|
205 | settings.setResearchGroupUrl(textResearchGroupUrl.getText()); |
---|
206 | |
---|
207 | if (cboUseSSL.getSelectionIndex() == 1) |
---|
208 | settings.setMysqlSSL(true); |
---|
209 | else |
---|
210 | settings.setMysqlSSL(false); |
---|
211 | |
---|
212 | try { |
---|
213 | DAVRepositoryFactory.setup(); |
---|
214 | SVNURL url = SVNURL.parseURIDecoded(settings.getSvnLocation()); |
---|
215 | SVNRepository repository = SVNRepositoryFactory |
---|
216 | .create(url); |
---|
217 | ISVNAuthenticationManager authManager = SVNWCUtil |
---|
218 | .createDefaultAuthenticationManager( |
---|
219 | settings.getSvnUsername(), settings.getSvnPassword()); |
---|
220 | ((SVNRepository) repository) |
---|
221 | .setAuthenticationManager(authManager); |
---|
222 | repository.testConnection(); |
---|
223 | |
---|
224 | } catch (SVNException ex) { |
---|
225 | setErrorMessage(ex.getMessage()); |
---|
226 | return; |
---|
227 | } |
---|
228 | |
---|
229 | System.setProperty("javax.net.ssl.trustStorePassword", |
---|
230 | settings.getMysqlPassword()); |
---|
231 | String path = Model.class.getProtectionDomain() |
---|
232 | .getCodeSource().getLocation().getPath(); |
---|
233 | if (path.endsWith("bin/")) |
---|
234 | path = path.replaceFirst("bin/$", ""); |
---|
235 | path += "keystore"; |
---|
236 | System.setProperty("javax.net.ssl.trustStore", path); |
---|
237 | |
---|
238 | try { |
---|
239 | // Step 1: Load the JDBC driver. |
---|
240 | Class.forName("com.mysql.jdbc.Driver").newInstance(); |
---|
241 | // Step 2: Establish the connection to the database. |
---|
242 | String url = "jdbc:mysql://" + settings.getMysqlHost() + "/" |
---|
243 | + settings.getMysqlDb() + "?useSSL=" |
---|
244 | + String.valueOf(settings.isMysqlSSL()); |
---|
245 | DriverManager.getConnection(url, settings.getMysqlUsername(), |
---|
246 | settings.getMysqlPassword()); |
---|
247 | } catch (SQLException ex) { |
---|
248 | setErrorMessage(ex.getMessage()); |
---|
249 | return; |
---|
250 | } catch (Exception ex2) { |
---|
251 | setErrorMessage(ex2.getMessage()); |
---|
252 | return; |
---|
253 | } |
---|
254 | Settings.getInstance().save(); |
---|
255 | close(); |
---|
256 | |
---|
257 | } else { |
---|
258 | setErrorMessage("Please fill out all fields"); |
---|
259 | } |
---|
260 | } |
---|
261 | }); |
---|
262 | Button cancel = new Button(parent, SWT.PUSH); |
---|
263 | cancel.setText("Cancel"); |
---|
264 | cancel.setFont(JFaceResources.getDialogFont()); |
---|
265 | cancel.setLayoutData(buttonlayout); |
---|
266 | cancel.addSelectionListener(new SelectionAdapter() { |
---|
267 | public void widgetSelected(SelectionEvent e) { |
---|
268 | close(); |
---|
269 | } |
---|
270 | }); |
---|
271 | } |
---|
272 | |
---|
273 | } |
---|