Skip to content

Commit

Permalink
Add channel topics
Browse files Browse the repository at this point in the history
Fix #1234
  • Loading branch information
1-alex98 committed Jul 2, 2019
1 parent aae05e8 commit 21eaa4c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 9 deletions.
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ javafxVersion=11
coverallsGradlePluginVersion=2.8.2
jnaVersion=5.0.0
jacocoVersion=0.8.2
discordRpcVersion=1.6.2-jna
discordRpcVersion=1.6.2-jna
install4jHomeDir=C:\\Program Files\\install4j7
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.faforever.client.chat;

import com.faforever.client.audio.AudioService;
import com.faforever.client.chat.event.ChannelTopicChangedEvent;
import com.faforever.client.fx.JavaFxUtil;
import com.faforever.client.fx.PlatformService;
import com.faforever.client.fx.WebViewConfigurer;
import com.faforever.client.i18n.I18n;
import com.faforever.client.notification.NotificationService;
Expand Down Expand Up @@ -34,6 +36,8 @@
import javafx.event.Event;
import javafx.geometry.Bounds;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Tab;
Expand All @@ -45,6 +49,7 @@
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.TextFlow;
import javafx.scene.web.WebView;
import javafx.stage.Popup;
import javafx.stage.PopupWindow;
Expand All @@ -65,6 +70,7 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.faforever.client.chat.ChatColorMode.DEFAULT;
Expand All @@ -75,6 +81,10 @@
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ChannelTabController extends AbstractChatTabController {
private static final String USER_CSS_CLASS_FORMAT = "user-%s";
/**
* Taken from https://stackoverflow.com/questions/163360/regular-expression-to-match-urls-in-java
*/
private static final Pattern URL_REGEX_PATTERN = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");

private static final Comparator<CategoryOrChatUserListItem> CHAT_USER_ITEM_COMPARATOR = (o1, o2) -> {
ChatChannelUser left = o1.getUser();
Expand Down Expand Up @@ -123,6 +133,7 @@ public class ChannelTabController extends AbstractChatTabController {
private final ObservableList<CategoryOrChatUserListItem> chatUserListItems;

private final AutoCompletionHelper autoCompletionHelper;
private final PlatformService platformService;

public SplitPane splitPane;
public ToggleButton advancedUserFilter;
Expand All @@ -135,6 +146,8 @@ public class ChannelTabController extends AbstractChatTabController {
public TextField userSearchTextField;
public TextField messageTextField;
public ListView<CategoryOrChatUserListItem> chatUserListView;
public VBox topicPane;
public TextFlow topicText;

private Channel channel;
private Popup filterUserPopup;
Expand All @@ -152,11 +165,12 @@ public ChannelTabController(UserService userService, ChatService chatService,
NotificationService notificationService, ReportingService reportingService,
UiService uiService, EventBus eventBus,
WebViewConfigurer webViewConfigurer,
CountryFlagService countryFlagService) {
CountryFlagService countryFlagService, PlatformService platformService) {

super(webViewConfigurer, userService, chatService, preferencesService, playerService, audioService,
timeService, i18n, imageUploadService, notificationService, reportingService, uiService,
eventBus, countryFlagService);
this.platformService = platformService;

hideFoeMessagesListeners = new HashMap<>();
socialStatusMessagesListeners = new HashMap<>();
Expand Down Expand Up @@ -235,6 +249,34 @@ public void setChannel(Channel channel) {
searchFieldContainer.visibleProperty().bind(searchField.visibleProperty());
closeSearchFieldButton.visibleProperty().bind(searchField.visibleProperty());
addSearchFieldListener();
updateChannelTopic();
JavaFxUtil.addListener(channel.topicProperty(), observable -> Platform.runLater(this::updateChannelTopic));
}

private void updateChannelTopic() {
boolean hasTopic = channel.getTopic() != null;
topicPane.setVisible(hasTopic);
if (!hasTopic) {
return;
}
String topic = channel.getTopic() == null ? "" : channel.getTopic();
Arrays.stream(topic.split("\\s"))
.forEach(word -> {
if (URL_REGEX_PATTERN.matcher(word).matches()) {
Hyperlink link = new Hyperlink(word);
link.setOnAction(event -> platformService.showDocument(word));
topicText.getChildren().add(link);
} else {
topicText.getChildren().add(new Label(word + " "));
}
});
}

@Subscribe
public void onChannelTopicChangedEvent(ChannelTopicChangedEvent channelTopicChangedEvent) {
if (channelTopicChangedEvent.getChannel().equals(channel)) {
updateChannelTopic();
}
}

private void removeModerator(CategoryOrChatUserListItem item) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.faforever.client.chat.event;

import com.faforever.client.chat.Channel;
import lombok.Value;

@Value
public class ChannelTopicChangedEvent {
private Channel channel;
}
18 changes: 13 additions & 5 deletions src/main/resources/theme/chat/channel_tab.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.TextFlow?>
<?import javafx.scene.web.WebView?>
<Tab xmlns:fx="http://javafx.com/fxml/1" fx:id="channelTabRoot" xmlns="http://javafx.com/javafx/8.0.141"
<Tab xmlns:fx="http://javafx.com/fxml/1" fx:id="channelTabRoot" xmlns="http://javafx.com/javafx/11.0.1"
fx:controller="com.faforever.client.chat.ChannelTabController">
<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" onKeyReleased="#onKeyReleased">
<children>
<SplitPane dividerPositions="0.8" maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS"
fx:id="splitPane">
<VBox fx:id="topicPane" maxWidth="1.7976931348623157E308" VBox.vgrow="NEVER">
<children>
<Separator maxWidth="1.7976931348623157E308"/>
<TextFlow fx:id="topicText" maxWidth="1.7976931348623157E308"/>
<Separator maxWidth="1.7976931348623157E308"/>
</children>
</VBox>
<SplitPane fx:id="splitPane" dividerPositions="0.8" maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS">
<items>
<AnchorPane>
<children>
Expand Down Expand Up @@ -49,8 +57,8 @@
<children>
<ListView fx:id="chatUserListView" maxHeight="1.7976931348623157E308"
maxWidth="1.7976931348623157E308" styleClass="chat-user-list-view"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
</ListView>
</children>
</AnchorPane>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.faforever.client.chat;

import com.faforever.client.audio.AudioService;
import com.faforever.client.fx.PlatformService;
import com.faforever.client.fx.WebViewConfigurer;
import com.faforever.client.i18n.I18n;
import com.faforever.client.notification.NotificationService;
Expand All @@ -15,6 +16,7 @@
import com.faforever.client.user.UserService;
import com.faforever.client.util.TimeService;
import com.google.common.eventbus.EventBus;
import javafx.application.Platform;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
Expand Down Expand Up @@ -96,6 +98,8 @@ public class ChannelTabControllerTest extends AbstractPlainJavaFxTest {
private EventBus eventBus;
@Mock
private CountryFlagService countryFlagService;
@Mock
private PlatformService platformService;
private Preferences preferences;
private Channel defaultChannel;

Expand All @@ -105,8 +109,8 @@ public void setUp() throws Exception {
preferencesService, playerService,
audioService, timeService, i18n, imageUploadService,
notificationService, reportingService,
uiService, eventBus, webViewConfigurer, countryFlagService
);
uiService, eventBus, webViewConfigurer, countryFlagService,
platformService);

defaultChannel = new Channel(CHANNEL_NAME);
preferences = new Preferences();
Expand Down Expand Up @@ -145,6 +149,18 @@ public void testSetChannelName() {
verify(chatService).addUsersListener(eq(CHANNEL_NAME), any());
}

@Test
public void testSetChannelTopic() {
Channel channel = new Channel("name");
channel.setTopic("topic https://example.com/1");
Platform.runLater(() -> instance.setChannel(channel));

WaitForAsyncUtils.waitForFxEvents();

verify(chatService).addUsersListener(eq("name"), any());
assertThat(instance.topicText.getChildren().size(), is(2));
}

@Test
public void testGetMessageCssClassModerator() {
String playerName = "junit";
Expand Down

0 comments on commit 21eaa4c

Please sign in to comment.