Skip to content

Commit

Permalink
Announcements
Browse files Browse the repository at this point in the history
- Add Announcements support
  - Add Announcements dialog
  - Add support for Announcement notification to main menu bar
  - Auto request on start and every 6 hours
  - Change DateTime.agoText() to support years
- Update help
- Add debug log directory function missing from last commit
  • Loading branch information
tduva committed Mar 25, 2016
1 parent 1dfe13f commit bc969f6
Show file tree
Hide file tree
Showing 10 changed files with 494 additions and 54 deletions.
4 changes: 4 additions & 0 deletions src/chatty/Chatty.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ public static String getBackupDirectory() {
return getUserDataDirectory()+"backup"+File.separator;
}

public static String getDebugLogDirectory() {
return getUserDataDirectory()+"debuglogs"+File.separator;
}

public static String chattyVersion() {
return String.format("Chatty Version %s%s%s / %s",
Chatty.VERSION,
Expand Down
3 changes: 3 additions & 0 deletions src/chatty/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ void defineSettings() {
settings.addString("cmChannel", "");
settings.addString("cmTemplate", "{user}: {message}");
settings.addBoolean("cmHighlightedOnly", false);

settings.addBoolean("newsAutoRequest", true);
settings.addLong("newsLastRead", 0);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/chatty/TwitchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,8 @@ private void testCommands(String channel, String command, String parameter) {
testUser.setColor(parameter);
} else if (command.equals("testupdatenotification")) {
g.setUpdateAvailable("[test]");
} else if (command.equals("testannouncement")) {
g.setAnnouncementAvailable(Boolean.parseBoolean(parameter));
} else if (command.equals("removechan")) {
g.removeChannel(parameter);
} else if (command.equals("testtimer")) {
Expand Down
19 changes: 17 additions & 2 deletions src/chatty/gui/MainGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import chatty.gui.components.FollowersDialog;
import chatty.gui.components.LiveStreamsDialog;
import chatty.gui.components.LivestreamerDialog;
import chatty.gui.components.NewsDialog;
import chatty.gui.components.srl.SRL;
import chatty.gui.components.SearchDialog;
import chatty.gui.components.StreamChat;
Expand Down Expand Up @@ -118,6 +119,7 @@ public class MainGui extends JFrame implements Runnable {
private SRL srl;
private LivestreamerDialog livestreamerDialog;
private UpdateMessage updateMessage;
private NewsDialog newsDialog;
private EmotesDialog emotesDialog;
private FollowersDialog followerDialog;
private FollowersDialog subscribersDialog;
Expand Down Expand Up @@ -241,6 +243,7 @@ private void createGui() {
srl = new SRL(this, client.speedrunsLive, contextMenuListener);
livestreamerDialog = new LivestreamerDialog(this, linkLabelListener, client.settings);
updateMessage = new UpdateMessage(this);
newsDialog = new NewsDialog(this, client.settings);

client.settings.addSettingChangeListener(new MySettingChangeListener());
client.settings.addSettingsListener(new MySettingsListener());
Expand All @@ -254,7 +257,7 @@ private void createGui() {

// Main Menu
MainMenuListener menuListener = new MainMenuListener();
menu = new MainMenu(menuListener,menuListener);
menu = new MainMenu(menuListener,menuListener, linkLabelListener);
setJMenuBar(menu);

state.update();
Expand Down Expand Up @@ -669,6 +672,8 @@ public void run() {
// Should be done when the main window is already visible, so
// it can be centered on it correctly, if that is necessary
reopenWindows();

newsDialog.autoRequestNews(true);
}
});
}
Expand Down Expand Up @@ -1170,6 +1175,10 @@ public void linkClicked(String type, String ref) {
if (ref.equals("show")) {
openUpdateDialog();
}
} else if (type.equals("announcement")) {
if (ref.equals("show")) {
newsDialog.showDialog();
}
}
}
}
Expand Down Expand Up @@ -1229,6 +1238,8 @@ public void actionPerformed(ActionEvent e) {
exit();
} else if (cmd.equals("about")) {
openHelp("");
} else if (cmd.equals("news")) {
newsDialog.showDialog();
} else if (cmd.equals("settings")) {
getSettingsDialog().showSettings();
} else if (cmd.equals("saveSettings")) {
Expand Down Expand Up @@ -2807,12 +2818,16 @@ public void setUpdateAvailable(final String newVersion) {

@Override
public void run() {
menu.setUpdateAvailable(linkLabelListener);
menu.setUpdateNotification(true);
updateMessage.setNewVersion(newVersion);
}
});
}

public void setAnnouncementAvailable(boolean enabled) {
menu.setAnnouncementNotification(enabled);
}

public void showSettings() {
SwingUtilities.invokeLater(new Runnable() {

Expand Down
138 changes: 94 additions & 44 deletions src/chatty/gui/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,24 @@ public class MainMenu extends JMenuBar {

private final ItemListener itemListener;
private final ActionListener actionListener;
private final LinkLabelListener linkLabelListener;

// Set here because it is used more than once
private final String IGNORED_LABEL = "Ignored";
private final String HIGHLIGHTS_LABEL = "Highlights";

/**
* Stores whether the "Update Available!" message has been added yet, so
* it's guaranteed to be only added once.
*/
private boolean addedUpdateMessage;
/**
* Store whether the update notification is currently set to the smaller
* version, so it doesn't constantly change unless necessary.
*/
private boolean updateMessageSmaller;
private final Notification notification = new Notification();

/**
* Stores all the menu items associated with a key
*/
private final HashMap<String,JMenuItem> menuItems = new HashMap<>();

public MainMenu(ActionListener actionListener, ItemListener itemListener) {
public MainMenu(ActionListener actionListener, ItemListener itemListener,
LinkLabelListener linkLabelListener) {
this.itemListener = itemListener;
this.actionListener = actionListener;
this.linkLabelListener = linkLabelListener;

//this.setBackground(Color.black);
//this.setForeground(Color.white);
Expand Down Expand Up @@ -178,6 +172,8 @@ public MainMenu(ActionListener actionListener, ItemListener itemListener) {
JMenuItem helpItem = addItem(help,"about","About/Help", KeyEvent.VK_H);
helpItem.setAccelerator(KeyStroke.getKeyStroke("F1"));
setIcon(helpItem, "help-browser.png");
help.addSeparator();
addItem(help,"news","Announcements");


add(main);
Expand Down Expand Up @@ -332,57 +328,111 @@ public void updateSrlStreams(String active, List<String> popout) {
}
}

/**
* Regular version of the update notification.
*/
private static final String UPDATE_MESSAGE = "<html>"
+ "<body style='text-align: right;padding-right:5px;'>"
+ "[update:show Update&nbsp;available!]";
/**
* Smaller version of the update notification.
*/
private static final String UPDATE_MESSAGE_SMALL = "<html>"
+ "<body style='text-align: right;padding-right:5px;'>"
+ "[update:show Update!]";
public void setUpdateNotification(boolean enabled) {
notification.setUpdateNotification(enabled);
}

/**
* Add the Update available! link in the menubar.
*
* @param listener The listener that reacts on a click on the link
*/
public void setUpdateAvailable(LinkLabelListener listener) {
if (!addedUpdateMessage) {
final LinkLabel updateNotification = new LinkLabel(UPDATE_MESSAGE, listener);
public void setAnnouncementNotification(boolean enabled) {
notification.setAnnouncementNotification(enabled);
}

private class Notification {

private static final String MESSAGE_BASE = "<html>"
+ "<body style='text-align: right;padding-right:5px;'>";

/**
* Stores whether the notification label has been added to the layout
* yet, so it's guaranteed to be only added once.
*/
private boolean addedLabelToLayout;

/**
* Store whether the notification is currently set to the smaller
* version, so it doesn't constantly change unless necessary.
*/
private boolean updateMessageSmaller;

private String message;
private String shortMessage;
private Dimension preferredSize = new Dimension();
private LinkLabel notification;
private boolean updateNotificationEnabled;
private boolean announcementNotificationEnabled;

public void setUpdateNotification(boolean enabled) {
if (updateNotificationEnabled != enabled) {
updateNotificationEnabled = enabled;
setNotification();
}
}

public void setAnnouncementNotification(boolean enabled) {
if (announcementNotificationEnabled != enabled) {
announcementNotificationEnabled = enabled;
setNotification();
}
}

private void makeText() {
message = MESSAGE_BASE;
shortMessage = MESSAGE_BASE;
if (announcementNotificationEnabled) {
message += "[announcement:show Announcement]";
shortMessage += "[announcement:show News]";
}
if (updateNotificationEnabled) {
if (announcementNotificationEnabled) {
message += "&nbsp;-&nbsp;";
shortMessage += "&nbsp;-&nbsp;";
}
message += "[update:show Update&nbsp;available!]";
shortMessage += "[update:show Update!]";
}
}

private void setNotification() {
makeText();
if (!addedLabelToLayout) {
addNotificationToLayout();
addedLabelToLayout = true;
}

// Add listener and stuff to change notification size when less
// space is there (Update available! -> Update!)
// Save the preferred size for the regular version here, because
// checking the preferred size in the listener would change between
// the regular and smaller version
final Dimension requiredSize = updateNotification.getPreferredSize();
updateNotification.addComponentListener(new ComponentAdapter() {
// Save preferred size of regular version to compare to in listener
notification.setText(message);
preferredSize = notification.getPreferredSize();
}

private void addNotificationToLayout() {
notification = new LinkLabel("", linkLabelListener);

/**
* Add listener to change notification text to a shorter version
* when less space is available ("Update available!" -> "Update!").
*/
notification.addComponentListener(new ComponentAdapter() {

@Override
public void componentResized(ComponentEvent e) {
Dimension actualSize = e.getComponent().getSize();
if (actualSize.width < requiredSize.width+10) {
if (actualSize.width < preferredSize.width + 10) {
if (!updateMessageSmaller) {
updateNotification.setText(UPDATE_MESSAGE_SMALL);
notification.setText(shortMessage);
updateMessageSmaller = true;
//System.out.println("made smaller");
}
} else {
if (updateMessageSmaller) {
updateNotification.setText(UPDATE_MESSAGE);
notification.setText(message);
updateMessageSmaller = false;
//System.out.println("made bigger again");
}
}
}
});

add(updateNotification);
addedUpdateMessage = true;

add(notification);
}

}
}
Loading

0 comments on commit bc969f6

Please sign in to comment.