Skip to content

Commit a77b83d

Browse files
committed
Enable/Disable openClosedExercises menu button depending on whether a course is selected
1 parent 1b1b29f commit a77b83d

11 files changed

+138
-44
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fi.helsinki.cs.tmc.actions;
2+
3+
import fi.helsinki.cs.tmc.core.events.TmcEventBus;
4+
import fi.helsinki.cs.tmc.core.events.TmcEventListener;
5+
import fi.helsinki.cs.tmc.model.CourseDb;
6+
7+
import org.openide.nodes.Node;
8+
import org.openide.util.HelpCtx;
9+
import org.openide.util.actions.NodeAction;
10+
11+
public abstract class AbstractCourseSensitiveAction extends NodeAction {
12+
13+
public AbstractCourseSensitiveAction() {
14+
TmcEventBus.getDefault().subscribeDependent(new TmcEventListener() {
15+
public void receive(CourseDb.ChangedEvent event) throws Throwable {
16+
setEnabled(CourseDb.getInstance().getCurrentCourse() != null);
17+
}
18+
}, this);
19+
}
20+
21+
@Override
22+
protected boolean enable(Node[] nodes) {
23+
return CourseDb.getInstance().getCurrentCourse() != null;
24+
}
25+
26+
@Override
27+
public HelpCtx getHelpCtx() {
28+
return HelpCtx.DEFAULT_HELP;
29+
}
30+
}

tmc-plugin/src/fi/helsinki/cs/tmc/actions/OpenClosedExercisesFromMenu.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,36 @@
55
import fi.helsinki.cs.tmc.model.LocalExerciseStatus;
66
import fi.helsinki.cs.tmc.ui.OpenClosedExercisesDialog;
77

8-
import java.awt.event.ActionEvent;
9-
import javax.swing.AbstractAction;
108
import org.openide.awt.ActionID;
119
import org.openide.awt.ActionReference;
1210
import org.openide.awt.ActionReferences;
1311
import org.openide.awt.ActionRegistration;
12+
import org.openide.nodes.Node;
1413
import org.openide.util.NbBundle;
1514

1615
@ActionID(category = "TMC",
1716
id = "fi.helsinki.cs.tmc.actions.OpenClosedExercisesFromMenu")
18-
@ActionRegistration(displayName = "#CTL_OpenClosedExercisesFromMenu")
17+
@ActionRegistration(displayName = "#CTL_OpenClosedExercisesFromMenu", lazy = false)
1918
@ActionReferences({
2019
@ActionReference(path = "Menu/TM&C", position = -43)
2120
})
2221
@NbBundle.Messages("CTL_OpenClosedExercisesFromMenu=&Open closed exercises")
23-
public class OpenClosedExercisesFromMenu extends AbstractAction {
24-
22+
public class OpenClosedExercisesFromMenu extends AbstractCourseSensitiveAction {
23+
2524
@Override
26-
public void actionPerformed(ActionEvent e) {
25+
public void performAction(Node[] nodes) {
2726
Course course = CourseDb.getInstance().getCurrentCourse();
2827
final LocalExerciseStatus status = LocalExerciseStatus.get(course.getExercises());
2928
OpenClosedExercisesDialog.display(status.closed);
3029
}
30+
31+
@Override
32+
public String getName() {
33+
return "&Open closed exercises";
34+
}
35+
36+
@Override
37+
protected boolean asynchronous() {
38+
return false;
39+
}
3140
}

tmc-plugin/src/fi/helsinki/cs/tmc/actions/TmcModuleInstall.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import fi.helsinki.cs.tmc.core.TmcCore;
44
import fi.helsinki.cs.tmc.core.configuration.TmcSettings;
55
import fi.helsinki.cs.tmc.core.domain.Course;
6+
import fi.helsinki.cs.tmc.core.events.TmcEventBus;
67
import fi.helsinki.cs.tmc.core.holders.TmcLangsHolder;
78
import fi.helsinki.cs.tmc.core.holders.TmcSettingsHolder;
89
import fi.helsinki.cs.tmc.coreimpl.TmcCoreSettingsImpl;
10+
import fi.helsinki.cs.tmc.events.LoginStateChangedEvent;
911
import fi.helsinki.cs.tmc.langs.util.TaskExecutorImpl;
1012
import fi.helsinki.cs.tmc.spywareLocal.SpywareFacade;
1113
import fi.helsinki.cs.tmc.tasks.LoginTask;
@@ -79,7 +81,9 @@ public void run() {
7981
prefs.putBoolean(PREF_FIRST_RUN, false);
8082
} else {
8183
// Do full refresh.
82-
if (LoginManager.loggedIn() && settings.getOrganization() != null && settings.getCurrentCourse().isPresent()) {
84+
if (!LoginManager.loggedIn()) {
85+
BgTask.start("Asking user to log in", new LoginTask());
86+
} else if (LoginManager.loggedIn() && settings.getOrganization().isPresent() && settings.getCurrentCourse().isPresent()) {
8387
new RefreshCoursesAction().addDefaultListener(false, true).addListener(new BgTaskListener<List<Course>>() {
8488
@Override
8589
public void bgTaskReady(List<Course> result) {

tmc-plugin/src/fi/helsinki/cs/tmc/coreimpl/TmcCoreSettingsImpl.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fi.helsinki.cs.tmc.core.configuration.TmcSettings;
44
import fi.helsinki.cs.tmc.core.domain.Course;
55
import fi.helsinki.cs.tmc.core.domain.OauthCredentials;
6+
import fi.helsinki.cs.tmc.core.domain.Organization;
67
import fi.helsinki.cs.tmc.core.events.TmcEvent;
78
import fi.helsinki.cs.tmc.core.events.TmcEventBus;
89
import fi.helsinki.cs.tmc.model.CourseDb;
@@ -12,7 +13,9 @@
1213
import fi.helsinki.cs.tmc.tailoring.Tailoring;
1314

1415
import com.google.common.base.Optional;
15-
import fi.helsinki.cs.tmc.core.domain.Organization;
16+
import com.google.gson.Gson;
17+
import com.google.gson.reflect.TypeToken;
18+
1619
import java.io.IOException;
1720
import java.net.ProxySelector;
1821
import java.nio.file.Path;
@@ -147,25 +150,44 @@ public String hostProgramVersion() {
147150
}
148151

149152
@Override
150-
public OauthCredentials getOauthCredentials() {
151-
return new OauthCredentials(settings.get(PREF_OAUTH_APPLICATION_ID, null), settings.get(PREF_OAUTH_SECRET, null));
153+
public Optional<OauthCredentials> getOauthCredentials() {
154+
OauthCredentials creds = new OauthCredentials(settings.get(PREF_OAUTH_APPLICATION_ID, null), settings.get(PREF_OAUTH_SECRET, null));
155+
if (creds.getOauthApplicationId() == null || creds.getOauthSecret() == null) {
156+
return Optional.absent();
157+
} else {
158+
return Optional.of(creds);
159+
}
152160
}
153161

154162
@Override
155-
public void setOauthCredentials(OauthCredentials credentials) {
156-
settings.put(PREF_OAUTH_APPLICATION_ID, credentials.getOauthApplicationId());
157-
settings.put(PREF_OAUTH_SECRET, credentials.getOauthSecret());
163+
public void setOauthCredentials(Optional<OauthCredentials> credentials) {
164+
if (!credentials.isPresent()) {
165+
settings.put(PREF_OAUTH_APPLICATION_ID, null);
166+
settings.put(PREF_OAUTH_SECRET, null);
167+
} else {
168+
settings.put(PREF_OAUTH_APPLICATION_ID, credentials.get().getOauthApplicationId());
169+
settings.put(PREF_OAUTH_SECRET, credentials.get().getOauthSecret());
170+
}
158171
}
159172

160173
@Override
161-
public String getOrganization() {
162-
return settings.get(PREF_ORGANIZATION, null);
174+
public Optional<Organization> getOrganization() {
175+
final String organizationJson = settings.get(PREF_ORGANIZATION, "");
176+
if (organizationJson.isEmpty()) {
177+
return Optional.absent();
178+
} else {
179+
Organization org = new Gson().fromJson(organizationJson, new TypeToken<Organization>(){}.getType());
180+
return Optional.of(org);
181+
}
163182
}
164183

165184
@Override
166-
public void setOrganization(String organization) {
167-
settings.put(PREF_ORGANIZATION, organization);
168-
185+
public void setOrganization(Optional<Organization> organization) {
186+
if (organization.isPresent()) {
187+
settings.put(PREF_ORGANIZATION, new Gson().toJson(organization.get()));
188+
} else {
189+
settings.put(PREF_ORGANIZATION, "");
190+
}
169191
}
170192

171193
public static class SavedEvent implements TmcEvent {}
@@ -202,8 +224,8 @@ private String stripTrailingSlashes(String s) {
202224
}
203225

204226
@Override
205-
public String getUsername() {
206-
return settings.get(PREF_USERNAME, tailoring.getDefaultUsername());
227+
public Optional<String> getUsername() {
228+
return Optional.of(settings.get(PREF_USERNAME, tailoring.getDefaultUsername()));
207229
}
208230

209231
public void setUsername(String username) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package fi.helsinki.cs.tmc.events;
2+
3+
import fi.helsinki.cs.tmc.core.events.TmcEvent;
4+
5+
public class LoginStateChangedEvent implements TmcEvent {}

tmc-plugin/src/fi/helsinki/cs/tmc/model/PushEventListener.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import fi.helsinki.cs.tmc.core.events.TmcEventBus;
88
import fi.helsinki.cs.tmc.core.events.TmcEventListener;
99

10+
import com.google.common.base.Optional;
11+
1012
import java.util.HashMap;
1113
import java.util.Map;
1214
import java.util.TimerTask;
@@ -213,7 +215,12 @@ private synchronized void handleDisconnect() {
213215
}
214216

215217
private synchronized void subscribeToReviews() {
216-
String username = settings.getUsername();
218+
final Optional<String> usernamePresent = settings.getUsername();
219+
if (!usernamePresent.isPresent()) {
220+
log.log(Level.WARNING, "Tried to subscribe to reviews while not logged in.");
221+
return;
222+
}
223+
String username = usernamePresent.get();
217224
String channel = "/broadcast/user/" + username + "/review-available";
218225
client.getChannel(channel).subscribe(reviewAvailableListener);
219226
}

tmc-plugin/src/fi/helsinki/cs/tmc/ui/LoginDialog.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ public LoginDialog(ActionListener onLogin) {
4141
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
4242

4343
this.settings = (TmcCoreSettingsImpl) TmcSettingsHolder.get();
44-
this.usernameField.setText(settings.getUsername());
44+
final Optional<String> username = settings.getUsername();
45+
if (username.isPresent()) {
46+
this.usernameField.setText(username.get());
47+
}
4548

4649
if (!usernameField.getText().isEmpty()) {
4750
SwingUtilities.invokeLater(new Runnable() {

tmc-plugin/src/fi/helsinki/cs/tmc/ui/OrganizationCard.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111

1212
public class OrganizationCard extends javax.swing.JPanel {
1313

14+
private final Organization organization;
15+
1416
public OrganizationCard(Organization organization) {
1517
initComponents();
1618

19+
this.organization = organization;
20+
1721
DefaultCaret caret = (DefaultCaret)this.organizationInformation.getCaret();
1822
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
1923

@@ -32,8 +36,8 @@ public OrganizationCard(Organization organization) {
3236
this.logo.setIcon(image);
3337
}
3438

35-
public String getOrganizationName() {
36-
return this.organizationName.getText();
39+
public Organization getOrganization() {
40+
return this.organization;
3741
}
3842

3943
private URL logoUrl(String path) {

tmc-plugin/src/fi/helsinki/cs/tmc/ui/OrganizationListWindow.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
66
import fi.helsinki.cs.tmc.core.holders.TmcSettingsHolder;
77

8+
import com.google.common.base.Optional;
9+
810
import java.awt.Color;
911
import java.awt.Component;
1012
import java.awt.Dimension;
@@ -112,10 +114,13 @@ public static boolean isWindowVisible() {
112114
}
113115

114116
private int setDefaultSelectedIndex() {
115-
String selectedOrganizationName = TmcSettingsHolder.get().getOrganization();
117+
Optional<Organization> selectedOrganization = TmcSettingsHolder.get().getOrganization();
118+
if (!selectedOrganization.isPresent()) {
119+
return 0;
120+
}
116121
final ListModel<OrganizationCard> list = organizations.getModel();
117122
for (int i = 0; i < list.getSize(); i++) {
118-
if (list.getElementAt(i).getOrganizationName().equals(selectedOrganizationName)) {
123+
if (list.getElementAt(i).getOrganization().getName().equals(selectedOrganization.get().getName())) {
119124
return i;
120125
}
121126
}

tmc-plugin/src/fi/helsinki/cs/tmc/ui/PreferencesPanel.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
package fi.helsinki.cs.tmc.ui;
22

33
import com.google.common.base.Optional;
4-
import fi.helsinki.cs.tmc.actions.RefreshCoursesAction;
54
import fi.helsinki.cs.tmc.core.TmcCore;
6-
import fi.helsinki.cs.tmc.core.configuration.TmcSettings;
75
import fi.helsinki.cs.tmc.core.domain.Course;
86
import fi.helsinki.cs.tmc.core.domain.Organization;
97
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
10-
import fi.helsinki.cs.tmc.core.exceptions.NotLoggedInException;
118
import fi.helsinki.cs.tmc.core.holders.TmcSettingsHolder;
129
import fi.helsinki.cs.tmc.coreimpl.TmcCoreSettingsImpl;
1310
import fi.helsinki.cs.tmc.tailoring.SelectedTailoring;
1411
import fi.helsinki.cs.tmc.tasks.LoginTask;
1512
import fi.helsinki.cs.tmc.utilities.BgTask;
16-
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
1713
import fi.helsinki.cs.tmc.utilities.DelayedRunner;
1814
import fi.helsinki.cs.tmc.utilities.LoginManager;
1915

20-
import java.awt.event.ActionEvent;
21-
import java.awt.event.ActionListener;
2216
import java.awt.event.ItemEvent;
2317
import java.awt.event.ItemListener;
2418
import java.io.File;
@@ -31,9 +25,6 @@
3125
import javax.swing.JLabel;
3226
import javax.swing.JPanel;
3327
import javax.swing.SwingUtilities;
34-
import javax.swing.event.DocumentEvent;
35-
import javax.swing.event.DocumentListener;
36-
import org.openide.util.Exceptions;
3728

3829
/**
3930
* The settings panel.
@@ -54,7 +45,11 @@
5445
}
5546

5647
private void updateFields() {
57-
final String username = TmcSettingsHolder.get().getUsername();
48+
final Optional<String> usernamePresent = TmcSettingsHolder.get().getUsername();
49+
String username = "";
50+
if (usernamePresent.isPresent()) {
51+
username = usernamePresent.get();
52+
}
5853
final JLabel login = this.loginLabel;
5954
final JButton logout = this.logoutButton;
6055
if (!username.isEmpty()) {
@@ -65,10 +60,10 @@ private void updateFields() {
6560
logout.setEnabled(false);
6661
}
6762

68-
String org = TmcSettingsHolder.get().getOrganization();
63+
Optional<Organization> org = TmcSettingsHolder.get().getOrganization();
6964
final JLabel selectedOrg = this.selectedOrganizationLabel;
70-
if (org != null) {
71-
selectedOrg.setText(org);
65+
if (org.isPresent()) {
66+
selectedOrg.setText(org.get().getName());
7267
} else {
7368
selectedOrg.setText("No organization selected");
7469
}
@@ -178,8 +173,8 @@ public void setSendDiagnosticsEnabled(boolean value) {
178173
}
179174

180175
public void setOrganization(OrganizationCard organization) {
181-
TmcSettingsHolder.get().setOrganization(organization.getOrganizationName());
182-
this.selectedOrganizationLabel.setText(organization.getOrganizationName());
176+
TmcSettingsHolder.get().setOrganization(Optional.of(organization.getOrganization()));
177+
this.selectedOrganizationLabel.setText(organization.getOrganization().getName());
183178
}
184179

185180
private static class LocaleWrapper {

0 commit comments

Comments
 (0)