Skip to content

Commit

Permalink
Fixes #109 Don't disable "Create game"
Browse files Browse the repository at this point in the history
* Fixed NPE when chat was never displayed
* Improved chat notification
  • Loading branch information
micheljung committed Dec 1, 2015
1 parent 56fb9d8 commit 3db78c4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import javafx.concurrent.Worker;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
Expand All @@ -48,7 +47,6 @@
import javafx.stage.Popup;
import javafx.stage.PopupWindow;
import javafx.stage.Stage;
import javafx.stage.Window;
import netscape.javascript.JSObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -549,10 +547,6 @@ protected void onChatMessage(ChatMessage chatMessage) {
}

private void scrollToBottomIfDesired() {
if (getMessagesWebView().getParent() == null) {
return;
}

engine.executeScript("scrollToBottomIfDesired()");
}

Expand Down Expand Up @@ -627,19 +621,15 @@ private void appendMessage(ChatMessage chatMessage) {
}

protected void showNotificationIfNecessary(ChatMessage chatMessage) {
final TabPane tabPane = getRoot().getTabPane();
Scene scene = tabPane.getScene();
final Window window = scene.getWindow();

if (!window.isFocused() || !window.isShowing()) {
if (!stage.isFocused() || !stage.isShowing()) {
notificationService.addNotification(new TransientNotification(
chatMessage.getUsername(),
chatMessage.getMessage(),
IdenticonUtil.createIdenticon(chatMessage.getUsername()),
new Action(event -> {
mainController.selectChatTab();
stage.toFront();
tabPane.getSelectionModel().select(getRoot());
getRoot().getTabPane().getSelectionModel().select(getRoot());
}))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ private void setUnread(boolean unread) {
return;
}
TabPaneSkin skin = (TabPaneSkin) tabPane.getSkin();
if (skin == null) {
return;
}
int tabIndex = tabPane.getTabs().indexOf(privateChatTabRoot);
if (tabIndex == -1) {
// Tab has been closed
Expand Down Expand Up @@ -76,7 +79,7 @@ public void onChatMessage(ChatMessage chatMessage) {
PlayerInfoBean playerInfoBean = playerService.getPlayerForUsername(chatMessage.getUsername());
ChatPrefs chatPrefs = preferencesService.getPreferences().getChat();

if (playerInfoBean.getSocialStatus() == FOE && chatPrefs.getHideFoeMessages()) {
if (playerInfoBean != null && playerInfoBean.getSocialStatus() == FOE && chatPrefs.getHideFoeMessages()) {
return;
}

Expand All @@ -85,7 +88,7 @@ public void onChatMessage(ChatMessage chatMessage) {
if (!hasFocus()) {
audioController.playPrivateMessageSound();
showNotificationIfNecessary(chatMessage);
setUnread(true);
setUnread(!getRoot().isSelected());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private void initMapSelection() {
mapListView.setCellFactory(mapListCellFactory());
mapListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) {
mapNameLabel.setText("");
Platform.runLater(() -> mapNameLabel.setText(""));
return;
}

Expand All @@ -221,11 +221,12 @@ private void initMapSelection() {
mapPlayersLabel.setText(i18n.get("mapPreview.maxPlayers", newValue.getPlayers()));
mapDescriptionLabel.setText(newValue.getDescription());

if (newValue.getHasAiMarkers()) {
mapAiMarkersLabel.setText(i18n.get("yes"));
} else {
mapAiMarkersLabel.setText(i18n.get("no"));
}
if (newValue.getHasAiMarkers()) {
mapAiMarkersLabel.setText(i18n.get("yes"));
} else {
mapAiMarkersLabel.setText(i18n.get("no"));
}
});
});
}

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/faforever/client/game/GamesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,12 @@ private void showRatingOutOfBoundsConfirmation(int playerRating, GameInfoBean ga

private void doJoinGame(GameInfoBean gameInfoBean, String password, double screenX, double screenY) {
if (preferencesService.getPreferences().getForgedAlliance().getPath() == null) {
onChoseGameDirectoryListener.onChoseGameDirectory();
preferencesService.letUserChoseGameDirectory()
.thenAccept(isPathValid -> {
if (isPathValid != null && !isPathValid) {
doJoinGame(gameInfoBean, password, screenX, screenY);
}
});
return;
}

Expand All @@ -278,7 +283,12 @@ void onCreateGameButtonClicked(ActionEvent actionEvent) {
Button button = (Button) actionEvent.getSource();

if (preferencesService.getPreferences().getForgedAlliance().getPath() == null) {
onChoseGameDirectoryListener.onChoseGameDirectory();
preferencesService.letUserChoseGameDirectory()
.thenAccept(isPathValid -> {
if (isPathValid != null && !isPathValid) {
onCreateGameButtonClicked(actionEvent);
}
});
return;
}

Expand Down
39 changes: 21 additions & 18 deletions src/main/java/com/faforever/client/main/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ private void showMenuDropdown(SplitMenuButton button) {

@PostConstruct
void postConstruct() {
// We need to initialize all skins, so initially add the chat root to the scene graph.
setContent(chatController.getRoot());

persistentNotificationsPopup = new Popup();
persistentNotificationsPopup.getContent().setAll(persistentNotificationsController.getRoot());
persistentNotificationsPopup.setAnchorLocation(PopupWindow.AnchorLocation.CONTENT_TOP_RIGHT);
Expand Down Expand Up @@ -335,6 +338,23 @@ void postConstruct() {
userService.addOnLoginListener(this::onLoggedIn);
}

private void setContent(Node node) {
ObservableList<Node> children = contentPane.getChildren();

if (!children.contains(node)) {
children.add(node);

AnchorPane.setTopAnchor(node, 0d);
AnchorPane.setRightAnchor(node, 0d);
AnchorPane.setBottomAnchor(node, 0d);
AnchorPane.setLeftAnchor(node, 0d);
}

for (Node child : children) {
child.setVisible(child == node);
}
}

private Rectangle2D getTransientNotificationAreaBounds() {
ObservableList<Screen> screens = Screen.getScreens();

Expand Down Expand Up @@ -560,7 +580,7 @@ public CompletableFuture<Path> onChoseGameDirectory() {
CompletableFuture<Path> future = new CompletableFuture<>();
Platform.runLater(() -> {
DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setTitle(i18n.get("missingGamePath.locate"));
directoryChooser.setTitle(i18n.get("missingGamePath.chooserTitle"));
File result = directoryChooser.showDialog(getRoot().getScene().getWindow());

if (result == null) {
Expand Down Expand Up @@ -636,23 +656,6 @@ void onChatSelected(ActionEvent event) {
setContent(chatController.getRoot());
}

private void setContent(Node node) {
ObservableList<Node> children = contentPane.getChildren();

if (!children.contains(node)) {
children.add(node);

AnchorPane.setTopAnchor(node, 0d);
AnchorPane.setRightAnchor(node, 0d);
AnchorPane.setBottomAnchor(node, 0d);
AnchorPane.setLeftAnchor(node, 0d);
}

for (Node child : children) {
child.setVisible(child == node);
}
}

@FXML
void onCommunityHubSelected(ActionEvent event) {
setContent(communityHubController.getRoot());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,19 @@ private void animate(Number height) {
new KeyFrame(millis(300), new KeyValue(transientNotificationRoot.maxHeightProperty(), height, Interpolator.LINEAR)),
new KeyFrame(millis(300 + toastDisplayTime), new KeyValue(transientNotificationRoot.maxHeightProperty(), height))
);
timeline.setOnFinished(event -> ((Pane) transientNotificationRoot.getParent()).getChildren().remove(transientNotificationRoot));
timeline.setOnFinished(event -> dismiss());
timeline.playFromStart();
}

private void dismiss() {
timeline.stop();
Pane parent = (Pane) transientNotificationRoot.getParent();
if (parent == null) {
return;
}
parent.getChildren().remove(transientNotificationRoot);
}

@PostConstruct
void postConstruct() {
// Divided by two because it's used in a cycle
Expand All @@ -94,11 +103,6 @@ void onCloseButtonClicked() {
dismiss();
}

private void dismiss() {
timeline.stop();
((Pane) transientNotificationRoot.getParent()).getChildren().remove(this.getRoot());
}

public Region getRoot() {
return transientNotificationRoot;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CompletableFuture;

import static java.nio.charset.StandardCharsets.US_ASCII;

Expand Down Expand Up @@ -163,24 +164,27 @@ private void detectGamePath() {

private void notifyMissingGamePath() {
List<Action> actions = Collections.singletonList(
new Action(i18n.get("missingGamePath.chooserTitle"), event -> letUserChoseGameDirectory())
new Action(i18n.get("missingGamePath.locate"), event -> letUserChoseGameDirectory())
);

notificationService.addNotification(new PersistentNotification(i18n.get("missingGamePath.notification"), Severity.WARN, actions));
}

private void letUserChoseGameDirectory() {
public CompletableFuture<Boolean> letUserChoseGameDirectory() {
if (onChoseGameDirectoryListener == null) {
throw new IllegalStateException("No listener has been specified");
}

onChoseGameDirectoryListener.onChoseGameDirectory().thenAccept(path -> {
return onChoseGameDirectoryListener.onChoseGameDirectory().thenApply(path -> {
if (path == null) {
return null;
}
boolean isPathValid = storeGamePathIfValid(path);

if (!isPathValid) {
logger.info("User specified game path does not seem to be valid: {}", path);
notifyMissingGamePath();
}
return isPathValid;
}).exceptionally(throwable -> {
logger.warn("Unexpected exception", throwable);
return null;
Expand Down

0 comments on commit 3db78c4

Please sign in to comment.