Skip to content

Commit

Permalink
Add channel context to ChannelContextMenu
Browse files Browse the repository at this point in the history
- Change context menu listener and actions to support channel context
instead of using active/last active channel
- Some refactoring
- Add some debug output
  • Loading branch information
tduva committed Jun 25, 2019
1 parent a2da360 commit 2796180
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/chatty/gui/Channels.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ private void addDefaultChannel() {
*/
private Channel createChannel(Room room, Channel.Type type) {
Channel channel = new Channel(room,type,gui,styleManager, contextMenuListener);
channel.init();
channel.setUserlistWidth(defaultUserlistWidth, minUserlistWidth);
channel.setMouseClickedListener(mouseClickedListener);
channel.setScrollbarAlways(chatScrollbarAlaways);
Expand Down
100 changes: 66 additions & 34 deletions src/chatty/gui/MainGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -1183,11 +1183,13 @@ private void updateConnectionDialog(String channelPreset) {
connectionDialog.setAreChannelsOpen(channels.getChannelCount() > 0);
}

private void setChannelInfoDialogChannel(Channel channel) {
channelInfoDialog.set(getStreamInfo(channel.getStreamName()));
}

private void updateChannelInfoDialog(StreamInfo info) {
if (info == null) {
String stream = channels.getLastActiveChannel().getStreamName();
info = getStreamInfo(stream);
channelInfoDialog.set(info);
setChannelInfoDialogChannel(channels.getLastActiveChannel());
} else {
channelInfoDialog.update(info);
}
Expand Down Expand Up @@ -1682,25 +1684,19 @@ else if (cmd.equals("join")) {
* Context menu event without any channel context, which means it just
* uses the active one or performs some other action that doesn't
* immediately require one.
*
* @param e
*/
@Override
public void menuItemClicked(ActionEvent e) {
Debugging.println("cmchan", "[cm] tab: %s chan: %s lastchan: %s",
channels.getActiveTab(), channels.getActiveChannel(), channels.getLastActiveChannel());

String cmd = e.getActionCommand();
if (cmd.equals("channelInfo")) {
openChannelInfoDialog();
}
else if (cmd.equals("channelAdmin")) {
openChannelAdminDialog();
}
else if (cmd.equals("chatRules")) {
openChatRules();
}
else if (cmd.equals("closeChannel")) {
if (cmd.equals("closeChannel")) {
// TabContextMenu
client.closeChannel(channels.getActiveChannel().getChannel());
}
else if (cmd.startsWith("closeAllTabs")) {
// TabContextMenu
Collection<Channel> chans = null;
if (cmd.equals("closeAllTabsButCurrent")) {
chans = channels.getTabsRelativeToCurrent(0);
Expand All @@ -1717,33 +1713,62 @@ else if (cmd.startsWith("closeAllTabs")) {
}
}
}
else if (cmd.equals("joinHostedChannel")) {
client.command(channels.getActiveChannel().getRoom(), "joinhosted");
}
else if (cmd.equals("srcOpen")) {
client.speedruncom.openCurrentGame(channels.getActiveChannel());
}
else if (cmd.equals("popoutChannel")) {
// TabContextMenu
channels.popoutActiveChannel();
}
else if (cmd.startsWith("command")) {
// TODO: Check getStreamName()
customCommand(channels.getActiveChannel().getRoom(), e,
Parameters.create(channels.getActiveChannel().getStreamName()));
}
else if (cmd.startsWith("historyRange")) {
int range = Integer.parseInt(cmd.substring("historyRange".length()));
// Change here as well, because even if it's the same value,
// update may be needed. This will make it update twice often.
//updateHistoryRange();
client.settings.setLong("historyRange", range);
} else if (cmd.startsWith("toggleVerticalZoom")) {
boolean selected = ((JMenuItem)e.getSource()).isSelected();
}
else if (cmd.startsWith("toggleVerticalZoom")) {
boolean selected = ((JMenuItem) e.getSource()).isSelected();
client.settings.setBoolean("historyVerticalZoom", selected);
} else {
}
else {
nameBasedStuff(e, channels.getActiveChannel().getStreamName());
}
}

/**
* ChannelContextMenu, with Channel context.
*/
@Override
public void channelMenuItemClicked(ActionEvent e, Channel channel) {
Debugging.println("cmchan", "[channelcm] tab: %s chan: %s lastchan: %s",
channels.getActiveTab(), channels.getActiveChannel(), channels.getLastActiveChannel());

String cmd = e.getActionCommand();
if (cmd.equals("channelInfo")) {
setChannelInfoDialogChannel(channel);
openChannelInfoDialog();
}
else if (cmd.equals("channelAdmin")) {
openChannelAdminDialog(channel.getStreamName());
}
else if (cmd.equals("chatRules")) {
openChatRules(channel.getChannel());
}
else if (cmd.equals("closeChannel")) {
client.closeChannel(channel.getChannel());
}
else if (cmd.equals("joinHostedChannel")) {
client.command(channel.getRoom(), "joinhosted");
}
else if (cmd.equals("srcOpen")) {
client.speedruncom.openCurrentGame(channel);
}
else if (cmd.startsWith("command")) {
customCommand(channel.getRoom(), e,
Parameters.create(channel.getStreamName()));
}
else {
nameBasedStuff(e, channel.getStreamName());
}
}

@Override
public void streamsMenuItemClicked(ActionEvent e, Collection<String> streams) {
Expand All @@ -1752,10 +1777,8 @@ public void streamsMenuItemClicked(ActionEvent e, Collection<String> streams) {
}

/**
* Context menu event associated with a list of stream or channel names.
*
* @param e
* @param streams
* Context menu event associated with a list of stream or channel names,
* used in the channel favorites dialog.
*/
@Override
public void roomsMenuItemClicked(ActionEvent e, Collection<Room> rooms) {
Expand Down Expand Up @@ -1955,6 +1978,12 @@ private void streamStuff(ActionEvent e, Collection<String> streams) {
} else if (cmd.equals("copy") && !streams.isEmpty()) {
MiscUtil.copyToClipboard(StringUtil.join(streams, ", "));
} else if (cmd.startsWith("command")) {
/**
* For example for Live Streams/Favorites context menu, so it
* makes sense to use the last active channel, since this might
* be triggered from a Chatty window that doesn't represent a
* single joined room.
*/
customCommand(channels.getLastActiveChannel().getRoom(), e, Parameters.create(StringUtil.join(streams, " ")));
}
}
Expand Down Expand Up @@ -2360,9 +2389,12 @@ private boolean closeDialog(JDialog dialog) {
}

private void openChannelAdminDialog() {
openChannelAdminDialog(channels.getActiveChannel().getStreamName());
}

private void openChannelAdminDialog(String stream) {
windowStateManager.setWindowPosition(adminDialog, getActiveWindow());
updateTokenScopes();
String stream = channels.getActiveChannel().getStreamName();
if (stream == null) {
stream = client.settings.getString("username");
}
Expand Down
9 changes: 6 additions & 3 deletions src/chatty/gui/components/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @author tduva
*/
public class Channel extends JPanel {
public final class Channel extends JPanel {

public enum Type {
NONE, CHANNEL, WHISPER, SPECIAL
Expand Down Expand Up @@ -72,7 +72,6 @@ public Channel(final Room room, Type type, MainGui main, StyleManager styleManag
text = new ChannelTextPane(main,styleManager);
text.setContextMenuListener(contextMenuListener);


setTextPreferredSizeTemporarily();

west = new JScrollPane(text);
Expand Down Expand Up @@ -113,7 +112,11 @@ public Channel(final Room room, Type type, MainGui main, StyleManager styleManag
// Add components
add(mainPane, BorderLayout.CENTER);
add(input, BorderLayout.SOUTH);

}

public void init() {
text.setChannel(this);

input.requestFocusInWindow();
setStyles();

Expand Down
1 change: 0 additions & 1 deletion src/chatty/gui/components/FollowersDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import chatty.gui.GuiUtil;
import chatty.gui.components.menus.ContextMenu;
import chatty.gui.components.menus.ContextMenuListener;
import chatty.gui.components.menus.RoomsContextMenu;
import chatty.gui.components.menus.StreamsContextMenu;
import chatty.gui.components.settings.ListTableModel;
import chatty.util.DateTime;
Expand Down
5 changes: 5 additions & 0 deletions src/chatty/gui/components/HighlightedMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ public void usericonMenuItemClicked(ActionEvent e, Usericon usericon) {
public void roomsMenuItemClicked(ActionEvent e, Collection<Room> rooms) {
contextMenuListener.roomsMenuItemClicked(e, rooms);
}

@Override
public void channelMenuItemClicked(ActionEvent e, Channel channel) {
contextMenuListener.channelMenuItemClicked(e, channel);
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/chatty/gui/components/StreamChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public void usericonMenuItemClicked(ActionEvent e, Usericon usericon) {
public void roomsMenuItemClicked(ActionEvent e, Collection<Room> rooms) {
contextMenuListener.roomsMenuItemClicked(e, rooms);
}

@Override
public void channelMenuItemClicked(ActionEvent e, Channel channel) {
contextMenuListener.channelMenuItemClicked(e, channel);
}
}

}
8 changes: 6 additions & 2 deletions src/chatty/gui/components/menus/ChannelContextMenu.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package chatty.gui.components.menus;

import chatty.gui.components.Channel;
import chatty.lang.Language;
import java.awt.event.ActionEvent;

Expand All @@ -16,8 +17,11 @@ public class ChannelContextMenu extends ContextMenu {

private final ContextMenuListener listener;

public ChannelContextMenu(ContextMenuListener listener) {
private final Channel channel;

public ChannelContextMenu(ContextMenuListener listener, Channel channel) {
this.listener = listener;
this.channel = channel;

addItem("channelInfo", Language.getString("menubar.dialog.channelInfo"));
addItem("channelAdmin", Language.getString("menubar.dialog.channelAdmin"));
Expand Down Expand Up @@ -46,7 +50,7 @@ public ChannelContextMenu(ContextMenuListener listener) {
@Override
public void actionPerformed(ActionEvent e) {
if (listener != null) {
listener.menuItemClicked(e);
listener.channelMenuItemClicked(e, channel);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/chatty/gui/components/menus/ContextMenuAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

import chatty.Room;
import chatty.User;
import chatty.util.api.Emoticon;
import chatty.gui.components.Channel;
import chatty.util.api.Emoticon.EmoticonImage;
import chatty.util.api.StreamInfo;
import chatty.util.api.usericons.Usericon;
import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.List;

/**
*
Expand Down Expand Up @@ -53,5 +52,9 @@ public void usericonMenuItemClicked(ActionEvent e, Usericon usericon) {
@Override
public void roomsMenuItemClicked(ActionEvent e, Collection<Room> rooms) {
}

@Override
public void channelMenuItemClicked(ActionEvent e, Channel channel) {
}

}
2 changes: 2 additions & 0 deletions src/chatty/gui/components/menus/ContextMenuListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import chatty.Room;
import chatty.User;
import chatty.gui.components.Channel;
import chatty.util.api.Emoticon.EmoticonImage;
import chatty.util.api.StreamInfo;
import chatty.util.api.usericons.Usericon;
Expand All @@ -18,6 +19,7 @@ public interface ContextMenuListener {
public void urlMenuItemClicked(ActionEvent e, String url);
public void menuItemClicked(ActionEvent e);
public void roomsMenuItemClicked(ActionEvent e, Collection<Room> rooms);
public void channelMenuItemClicked(ActionEvent e, Channel channel);
public void streamsMenuItemClicked(ActionEvent e, Collection<String> streams);
public void streamInfosMenuItemClicked(ActionEvent e, Collection<StreamInfo> streamInfos);
public void emoteMenuItemClicked(ActionEvent e, EmoticonImage emote);
Expand Down
7 changes: 7 additions & 0 deletions src/chatty/gui/components/textpane/ChannelTextPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import chatty.gui.MainGui;
import chatty.User;
import chatty.gui.Highlighter.Match;
import chatty.gui.components.Channel;
import chatty.util.api.usericons.Usericon;
import chatty.gui.components.menus.ContextMenuListener;
import chatty.gui.emoji.EmojiUtil;
Expand Down Expand Up @@ -113,6 +114,7 @@ public class ChannelTextPane extends JTextPane implements LinkListener, Emoticon
private static final Matcher urlMatcher = Helper.getUrlPattern().matcher("");

public MainGui main;
private Channel channel;

protected LinkController linkController = new LinkController();
private static StyleServer styleServer;
Expand Down Expand Up @@ -245,6 +247,11 @@ public void setMouseClickedListener(MouseClickedListener listener) {
linkController.setMouseClickedListener(listener);
}

public void setChannel(Channel channel) {
this.channel = channel;
linkController.setChannel(channel);
}

private void setHoveredUser(User user) {
int mode = styles.getInt(Setting.HIGHLIGHT_HOVERED_USER);
if (mode == SettingConstants.USER_HOVER_HL_OFF) {
Expand Down
17 changes: 14 additions & 3 deletions src/chatty/gui/components/textpane/LinkController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import chatty.gui.LinkListener;
import chatty.gui.MouseClickedListener;
import chatty.gui.UserListener;
import chatty.gui.components.Channel;
import chatty.gui.components.menus.ChannelContextMenu;
import chatty.gui.components.menus.ContextMenu;
import chatty.gui.components.menus.ContextMenuListener;
Expand Down Expand Up @@ -82,6 +83,8 @@ public class LinkController extends MouseAdapter {

private ContextMenu defaultContextMenu;

private Channel channel;

private MyPopup popup = new MyPopup();

private Element prevHoverElement;
Expand Down Expand Up @@ -140,6 +143,10 @@ public void setDefaultContextMenu(ContextMenu contextMenu) {
defaultContextMenu = contextMenu;
contextMenu.addContextMenuListener(contextMenuListener);
}

public void setChannel(Channel channel) {
this.channel = channel;
}

/**
* Handles mouse presses. This is favourable to mouseClicked because it
Expand Down Expand Up @@ -373,7 +380,7 @@ private void openContextMenu(MouseEvent e) {
String url = getUrl(element);
EmoticonImage emoteImage = getEmoticonImage(element);
Usericon usericon = getUsericon(element);
JPopupMenu m;
JPopupMenu m = null;
if (user != null) {
m = new UserContextMenu(user, getMsgId(element),
getAutoModMsgId(element), contextMenuListener);
Expand All @@ -389,12 +396,16 @@ else if (usericon != null) {
}
else {
if (defaultContextMenu == null) {
m = new ChannelContextMenu(contextMenuListener);
if (channel != null) {
m = new ChannelContextMenu(contextMenuListener, channel);
}
} else {
m = defaultContextMenu;
}
}
m.show(e.getComponent(), e.getX(), e.getY());
if (m != null) {
m.show(e.getComponent(), e.getX(), e.getY());
}
popup.hide();
}

Expand Down

0 comments on commit 2796180

Please sign in to comment.