2
2
3
3
import com .google .common .base .Optional ;
4
4
import fi .helsinki .cs .tmc .core .TmcCore ;
5
+ import fi .helsinki .cs .tmc .core .configuration .TmcSettings ;
5
6
import fi .helsinki .cs .tmc .core .domain .Course ;
6
7
import fi .helsinki .cs .tmc .core .domain .Organization ;
7
8
import fi .helsinki .cs .tmc .core .domain .ProgressObserver ;
12
13
import fi .helsinki .cs .tmc .utilities .BgTask ;
13
14
import fi .helsinki .cs .tmc .utilities .DelayedRunner ;
14
15
import fi .helsinki .cs .tmc .utilities .LoginManager ;
16
+ import fi .helsinki .cs .tmc .utilities .ThrowingFunction ;
15
17
16
18
import java .awt .event .ItemEvent ;
17
19
import java .awt .event .ItemListener ;
18
20
import java .io .File ;
19
- import java .util . ArrayList ;
21
+ import java .io . IOException ;
20
22
import java .util .List ;
21
23
import java .util .Locale ;
22
24
import javax .swing .JButton ;
37
39
private ConvenientDialogDisplayer dialogs = ConvenientDialogDisplayer .getDefault ();
38
40
39
41
private DelayedRunner refreshRunner = new DelayedRunner ();
42
+
43
+ private final TmcSettings settings = TmcSettingsHolder .get ();
40
44
41
45
/*package*/ PreferencesPanel () {
42
46
initComponents ();
45
49
}
46
50
47
51
private void updateFields () {
48
- final Optional <String > usernamePresent = TmcSettingsHolder .get ().getUsername ();
49
- String username = "" ;
50
- if (usernamePresent .isPresent ()) {
51
- username = usernamePresent .get ();
52
- }
52
+ final Optional <String > username = this .settings .getUsername ();
53
53
final JLabel login = this .loginLabel ;
54
54
final JButton logout = this .logoutButton ;
55
- if (! username .isEmpty ()) {
56
- login .setText ("Logged in as " + username );
55
+ if (username .isPresent ()) {
56
+ login .setText ("Logged in as " + username . get () );
57
57
logout .setEnabled (true );
58
58
} else {
59
59
login .setText ("Not logged in!" );
60
60
logout .setEnabled (false );
61
61
}
62
62
63
- Optional <Organization > org = TmcSettingsHolder . get () .getOrganization ();
63
+ Optional <Organization > org = this . settings .getOrganization ();
64
64
final JLabel selectedOrg = this .selectedOrganizationLabel ;
65
65
if (org .isPresent ()) {
66
66
selectedOrg .setText (org .get ().getName ());
67
67
} else {
68
68
selectedOrg .setText ("No organization selected" );
69
69
}
70
70
71
- Optional <Course > course = TmcSettingsHolder . get () .getCurrentCourse ();
71
+ Optional <Course > course = this . settings .getCurrentCourse ();
72
72
final JLabel selectedCourse = this .selectedCourseLabel ;
73
73
if (course .isPresent ()) {
74
- selectedCourse .setText (course .get ().getName ());
74
+ selectedCourse .setText (course .get ().getTitle ());
75
75
} else {
76
76
selectedCourse .setText ("No course selected" );
77
77
}
78
78
}
79
79
80
80
@ Override
81
- public List <Course > getAvailableCourses () {
82
- try {
83
- List <Course > courses = TmcCore .get ().listCourses (ProgressObserver .NULL_OBSERVER ).call ();
84
- return courses ;
85
- } catch (Exception ex ) {
86
- }
87
- return new ArrayList <>();
81
+ public List <Course > getAvailableCourses () throws Exception {
82
+ List <Course > courses = TmcCore .get ().listCourses (ProgressObserver .NULL_OBSERVER ).call ();
83
+ return courses ;
88
84
}
89
85
90
86
@ Override
@@ -98,13 +94,17 @@ public void setProjectDir(String projectDir) {
98
94
}
99
95
100
96
public void setSelectedCourse (Course course ) {
101
- TmcSettingsHolder . get () .setCourse (Optional .of (course ));
102
- this .selectedCourseLabel .setText (course .getName ());
97
+ this . settings .setCourse (Optional .of (course ));
98
+ this .selectedCourseLabel .setText (course .getTitle ());
103
99
}
104
100
105
101
@ Override
106
102
public String getSelectedCourseName () {
107
- return this .selectedCourseLabel .getText ();
103
+ final Optional <Course > currentCourse = this .settings .getCurrentCourse ();
104
+ if (currentCourse .isPresent ()) {
105
+ return currentCourse .get ().getName ();
106
+ }
107
+ return null ;
108
108
}
109
109
110
110
@ Override
@@ -173,8 +173,15 @@ public void setSendDiagnosticsEnabled(boolean value) {
173
173
}
174
174
175
175
public void setOrganization (OrganizationCard organization ) {
176
- TmcSettingsHolder .get ().setOrganization (Optional .of (organization .getOrganization ()));
177
- this .selectedOrganizationLabel .setText (organization .getOrganization ().getName ());
176
+ Optional <Organization > oldOrganization = this .settings .getOrganization ();
177
+ Organization newOrganization = organization .getOrganization ();
178
+
179
+ if (!oldOrganization .isPresent () || (oldOrganization .isPresent () && !oldOrganization .get ().getSlug ().equals (newOrganization .getSlug ()))) {
180
+ this .settings .setOrganization (Optional .of (newOrganization ));
181
+ this .settings .setCourse (Optional .<Course >absent ());
182
+ this .selectedOrganizationLabel .setText (newOrganization .getName ());
183
+ this .selectedCourseLabel .setText ("No course selected" );
184
+ }
178
185
}
179
186
180
187
private static class LocaleWrapper {
@@ -207,9 +214,9 @@ public int hashCode() {
207
214
}
208
215
209
216
private void updateSettingsForRefresh () {
210
- TmcCoreSettingsImpl settings = (TmcCoreSettingsImpl )TmcSettingsHolder . get () ;
211
- settings .setProjectRootDir (getProjectDir ());
212
- settings .save (); // TODO: is this wanted
217
+ TmcCoreSettingsImpl tmcSettings = (TmcCoreSettingsImpl )this . settings ;
218
+ tmcSettings .setProjectRootDir (getProjectDir ());
219
+ tmcSettings .save (); // TODO: is this wanted
213
220
}
214
221
215
222
private void setUpErrorMsgLocaleSelection () {
@@ -238,6 +245,16 @@ public void itemStateChanged(ItemEvent event) {
238
245
}
239
246
});
240
247
}
248
+
249
+ private void wrapWithExceptionHandling (ThrowingFunction function ) {
250
+ try {
251
+ function .apply ();
252
+ } catch (IOException ex ) {
253
+ dialogs .displayError ("Couldn't connect to the server! Please check your internet connection." );
254
+ } catch (Exception ex ) {
255
+ dialogs .displayError (ex .getMessage ());
256
+ }
257
+ }
241
258
242
259
/** This method is called from within the constructor to
243
260
* initialize the form.
@@ -480,10 +497,8 @@ private void folderChooserBtnActionPerformed(java.awt.event.ActionEvent evt) {//
480
497
}//GEN-LAST:event_folderChooserBtnActionPerformed
481
498
482
499
private void changeCourseButtonActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_changeCourseButtonActionPerformed
483
- try {
484
- CourseListWindow .display ();
485
- } catch (Exception ex ) {
486
- }
500
+ wrapWithExceptionHandling (CourseListWindow ::display );
501
+
487
502
updateSettingsForRefresh ();
488
503
}//GEN-LAST:event_changeCourseButtonActionPerformed
489
504
@@ -492,15 +507,20 @@ private void resolveDependenciesActionPerformed(java.awt.event.ActionEvent evt)
492
507
}//GEN-LAST:event_resolveDependenciesActionPerformed
493
508
494
509
private void sendDiagnosticsActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_sendDiagnosticsActionPerformed
495
- TmcCoreSettingsImpl settings = (TmcCoreSettingsImpl ) TmcSettingsHolder . get () ;
496
- settings .setSendDiagnostics (getSendDiagnosticsEnabled ());
510
+ TmcCoreSettingsImpl tmcSettings = (TmcCoreSettingsImpl ) this . settings ;
511
+ tmcSettings .setSendDiagnostics (getSendDiagnosticsEnabled ());
497
512
}//GEN-LAST:event_sendDiagnosticsActionPerformed
498
513
499
514
private void changeOrganizationButtonActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_changeOrganizationButtonActionPerformed
500
- try {
501
- OrganizationListWindow .display ();
502
- } catch (Exception ex ) {
515
+ this .selectedCourseLabel .setText ("No course selected" );
516
+
517
+ wrapWithExceptionHandling (OrganizationListWindow ::display );
518
+
519
+ final Optional <Course > currentCourse = this .settings .getCurrentCourse ();
520
+ if (currentCourse .isPresent ()) {
521
+ this .selectedCourseLabel .setText (currentCourse .get ().getTitle ());
503
522
}
523
+
504
524
updateSettingsForRefresh ();
505
525
}//GEN-LAST:event_changeOrganizationButtonActionPerformed
506
526
0 commit comments