diff --git a/.gitignore b/.gitignore index 2b99822..d77958c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target/ /localDB/ -/themes.ser +/log/ +/themes.ser \ No newline at end of file diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 5fe62a3..61e7461 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -18,6 +18,7 @@ import javax.xml.datatype.DatatypeFactory; import envoy.client.event.EventBus; import envoy.client.event.MessageCreationEvent; +import envoy.client.util.EnvoyLog; import envoy.exception.EnvoyException; import envoy.schema.Message; import envoy.schema.Message.Metadata.MessageState; @@ -48,7 +49,7 @@ public class LocalDB { private Sync sync = objectFactory.createSync(); private Sync readMessages = objectFactory.createSync(); - private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(LocalDB.class.getSimpleName()); /** * Constructs an empty local database. To serialize any chats to the file diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index f172816..ea789c8 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -114,7 +114,7 @@ public class Settings { } /** - * @return the name of the current Theme + * @return the name of the current theme * @since Envoy v0.2-alpha */ public String getCurrentTheme() { return currentTheme; } @@ -122,7 +122,7 @@ public class Settings { /** * Sets the currentTheme * - * @param themeName the name of the current theme + * @param themeName the name of the new current theme * @since Envoy v0.2-alpha */ public void setCurrentTheme(String themeName) { currentTheme = themeName; } diff --git a/src/main/java/envoy/client/event/Event.java b/src/main/java/envoy/client/event/Event.java index 32009b9..8e5393a 100644 --- a/src/main/java/envoy/client/event/Event.java +++ b/src/main/java/envoy/client/event/Event.java @@ -16,3 +16,4 @@ public interface Event { */ T get(); } + diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 910180c..8814195 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -30,8 +30,10 @@ import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; import envoy.client.Settings; +import envoy.client.util.EnvoyLog; import envoy.client.event.EventBus; import envoy.client.event.ThemeChangeEvent; + import envoy.schema.Message; import envoy.schema.User; @@ -66,7 +68,7 @@ public class ChatWindow extends JFrame { private static int space = 4; - private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName()); public ChatWindow(Client client, LocalDB localDB) { this.client = client; @@ -167,9 +169,9 @@ public class ChatWindow extends JFrame { settingsButton.addActionListener((evt) -> { try { - SettingsScreen.open(); + new SettingsScreen().setVisible(true); + changeChatWindowColors(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); } catch (Exception e) { - SettingsScreen.open(); logger.log(Level.WARNING, "An error occured while opening the settings screen", e); e.printStackTrace(); } @@ -236,7 +238,8 @@ public class ChatWindow extends JFrame { /** * Used to immediately reload the ChatWindow when settings were changed. - * + * + * @param theme the theme to change colors into * @since Envoy v0.1-alpha */ private void changeChatWindowColors(Theme theme) { diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index fc0ebda..75ce884 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -11,6 +11,7 @@ import java.awt.Insets; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Arrays; +import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.BoxLayout; @@ -25,8 +26,8 @@ import javax.swing.JPanel; import javax.swing.JTextPane; import javax.swing.ListSelectionModel; -import envoy.client.LocalDB; import envoy.client.Settings; +import envoy.client.util.EnvoyLog; import envoy.client.event.EventBus; import envoy.client.event.ThemeChangeEvent; @@ -56,44 +57,23 @@ public class SettingsScreen extends JDialog { private GridBagConstraints gbc_themeContent = new GridBagConstraints(); - private Theme selectedTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); + private JButton createNewThemeButton = new JButton("Create New Theme"); + private JPanel colorsPanel = new JPanel(); + private JButton okButton = new JButton("Save"); + private JButton cancelButton = new JButton("Cancel"); - private JButton createNewThemeButton = new JButton("Create New Theme"); - - private JPanel colorsPanel = new JPanel(); - private JButton okButton = new JButton("Save"); - private JButton cancelButton = new JButton("Cancel"); private static int space = 5; - private Theme temporaryTheme; + private Theme temporaryTheme, selectedTheme; - private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); - - private static SettingsScreen settingsScreen; - - // TODO: Add a JPanel with all the Information necessary: - // change (Picture,Username, Email, Password) and toggle(light/dark mode, - // "ctrl+enter"/"enter" - // to send a message directly) - - /** - * Opens the settings screen.
- * - * @since Envoy v0.1-alpha - */ - public static void open() { - settingsScreen = new SettingsScreen(); - settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - settingsScreen.setModal(true); - settingsScreen.setVisible(true); - } + private static final Logger logger = EnvoyLog.getLogger(SettingsScreen.class.getSimpleName()); /** * Builds the settings screen. * * @since Envoy v0.1-alpha */ - private SettingsScreen() { + public SettingsScreen() { logger.info(Settings.getInstance().getCurrentTheme()); setBounds(10, 10, 450, 650); @@ -122,7 +102,7 @@ public class SettingsScreen extends JDialog { @SuppressWarnings("unchecked") final JList selectedOption = (JList) listSelectionEvent.getSource(); final String option = selectedOption.getSelectedValue(); - System.out.println(option); + logger.log(Level.FINEST, option); switch (option) { case "Color Themes": @@ -173,7 +153,7 @@ public class SettingsScreen extends JDialog { @Override public void itemStateChanged(ItemEvent e) { String selectedValue = (String) themes.getSelectedItem(); - System.out.println(selectedValue); + logger.log(Level.FINEST, selectedValue); selectedTheme = Settings.getInstance().getThemes().get(selectedValue); } }); @@ -229,7 +209,7 @@ public class SettingsScreen extends JDialog { createNewThemeButton.addActionListener((evt) -> { try { String s = JOptionPane.showInputDialog("Enter a name for the new theme"); - System.out.println(s); + logger.log(Level.FINEST, s); Settings.getInstance() .addNewThemeToMap(new Theme(s, temporaryTheme.getBackgroundColor(), temporaryTheme.getCellColor(), temporaryTheme.getInteractableForegroundColor(), temporaryTheme.getInteractableBackgroundColor(), @@ -295,7 +275,7 @@ public class SettingsScreen extends JDialog { Settings.getInstance().setEnterToSend(Settings.getInstance().isEnterToSend());// still temporary Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName()); - System.out.println(selectedTheme.getThemeName()); + logger.log(Level.FINER, selectedTheme.getThemeName()); final Theme currentTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); changeSettingsScreenColors(currentTheme); @@ -306,13 +286,18 @@ public class SettingsScreen extends JDialog { revalidate(); repaint(); } catch (Exception e) { - logger.info("Something went wrong when changing the setting"); - settingsScreen.dispose(); + logger.warning("Something went wrong when changing the setting"); + JOptionPane.showMessageDialog(this, "Something went wrong when changing the setting"); + dispose(); } }); } } + changeSettingsScreenColors(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); + + setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + setModal(true); } private void changeSettingsScreenColors(Theme theme) { @@ -447,8 +432,7 @@ public class SettingsScreen extends JDialog { try { Color newColor = JColorChooser.showDialog(null, "Choose a color", color); if (newColor.getRGB() != color.getRGB()) { - System.out.println("New Color"); - System.out.println(color.getRGB()); + logger.log(Level.FINEST, "New Color: " + String.valueOf(color.getRGB())); // TODO: When Theme changed in same settings screen, color variable doesnt // update. temporaryTheme.setColor(yIndex, newColor); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 53cbbc0..b9f9633 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -11,6 +11,7 @@ import javax.swing.JOptionPane; import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; +import envoy.client.util.EnvoyLog; import envoy.exception.EnvoyException; import envoy.schema.User; @@ -28,11 +29,9 @@ import envoy.schema.User; */ public class Startup { - private static final Logger logger = Logger.getLogger(Startup.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(Startup.class.getSimpleName()); public static void main(String[] args) { - logger.setLevel(Level.ALL); - Config config = Config.getInstance(); try { @@ -57,7 +56,7 @@ public class Startup { logger.severe("User name is not set or empty. Exiting..."); System.exit(1); } - + // Initialize the local database LocalDB localDB; try { @@ -81,19 +80,18 @@ public class Startup { // Try entering offline mode localDB.loadUsers(); User clientUser = localDB.getUsers().get(userName); - if(clientUser == null) - throw new EnvoyException("Could not enter offline mode: user name unknown"); + if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); client.setSender(clientUser); - } catch(Exception e2) { + } catch (Exception e2) { JOptionPane.showMessageDialog(null, e2.toString(), "Client error", JOptionPane.ERROR_MESSAGE); System.exit(1); return; } } - + // Set client user in local database localDB.setUser(client.getSender()); - + // Initialize chats in local database try { localDB.initializeDBFile(); @@ -105,13 +103,12 @@ public class Startup { "Local DB error", JOptionPane.WARNING_MESSAGE); } - + logger.info("Client user ID: " + client.getSender().getID()); // Save all users to the local database - if(client.isOnline()) - localDB.setUsers(client.getUsers()); - + if (client.isOnline()) localDB.setUsers(client.getUsers()); + EventQueue.invokeLater(() -> { try { ChatWindow chatWindow = new ChatWindow(client, localDB); @@ -119,7 +116,7 @@ public class Startup { try { new StatusTrayIcon(chatWindow).show(); - + // If the tray icon is supported, hide the chat window on close chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); } catch (EnvoyException e) { diff --git a/src/main/java/envoy/client/ui/Theme.java b/src/main/java/envoy/client/ui/Theme.java index 976046b..66a0a80 100644 --- a/src/main/java/envoy/client/ui/Theme.java +++ b/src/main/java/envoy/client/ui/Theme.java @@ -1,161 +1,161 @@ -package envoy.client.ui; - -import java.awt.Color; -import java.io.Serializable; - -/** - * Project: envoy-client
- * File: Theme.java
- * Created: 23 Nov 2019
- * - * @author Maximilian Käfer - * @since Envoy v0.2-alpha - */ -public class Theme implements Serializable { - - private static final long serialVersionUID = 141727847527060352L; - - private String themeName; - private Color backgroundColor; - private Color cellColor; - private Color interactableBackgroundColor; - private Color userNameColor; - private Color interactableForegroundColor; - private Color messageColorChat; - private Color dateColorChat; - private Color selectionColor; - private Color typingMessageColor; - - public Theme(String themeName, Color backgroundColor, Color cellColor, Color interactableForegroundColor, Color interactableBackgroundColor, - Color messageColorChat, Color dateColorChat, Color selectionColor, Color typingMessageColor, Color userNameColor) { - - this.themeName = themeName; - - this.backgroundColor = backgroundColor; - this.cellColor = cellColor; - this.interactableForegroundColor = interactableForegroundColor; - this.interactableBackgroundColor = interactableBackgroundColor; - this.messageColorChat = messageColorChat; - this.dateColorChat = dateColorChat; - this.selectionColor = selectionColor; - this.typingMessageColor = typingMessageColor; - this.userNameColor = userNameColor; - } - - public Theme(String name, Theme other) { - this(name, other.backgroundColor, other.cellColor, other.interactableForegroundColor, - other.interactableBackgroundColor, other.messageColorChat, other.dateColorChat, other.selectionColor, - other.typingMessageColor, other.userNameColor); - } - - /** - * @return name of the theme - * @since Envoy v0.2-alpha - */ - public String getThemeName() { return themeName; } - - /** - * @return interactableForegroundColor - * @since Envoy v0.2-alpha - */ - public Color getInteractableForegroundColor() { return interactableForegroundColor; } - - /** - * @return messageColorChat - * @since Envoy v0.2-alpha - */ - public Color getMessageColorChat() { return messageColorChat; } - - /** - * @return dateColorChat - * @since Envoy v0.2-alpha - */ - public Color getDateColorChat() { return dateColorChat; } - - /** - * @return selectionColor - * @since Envoy v0.2-alpha - */ - public Color getSelectionColor() { return selectionColor; } - - /** - * @return typingMessageColor - * @since Envoy v0.2-alpha - */ - public Color getTypingMessageColor() { return typingMessageColor; } - - /** - * @return backgroundColor - * @since Envoy v0.2-alpha - */ - public Color getBackgroundColor() { return backgroundColor; } - - /** - * @return cellColor - * @since Envoy v0.2-alpha - */ - public Color getCellColor() { return cellColor; } - - /** - * @return interactableBackgroundColor - * @since Envoy v0.2-alpha - */ - public Color getInteractableBackgroundColor() { return interactableBackgroundColor; } - - /** - * @return userNameColor - * @since Envoy v0.2-alpha - */ - public Color getUserNameColor() { return userNameColor; } - - /** - * Sets the a specific {@link Color} in this theme to a new {@link Color} - * - * @param index - index of the color
- * 0 = backgroundColor
- * 1 = cellColor
- * 2 = interactableForegroundColor
- * 3 = interactableBackgroundColor
- * 4 = messageColorChat
- * 5 = dateColorChat
- * 6 = selectionColor
- * 7 = typingMessageColor
- * 8 = userNameColor
- *
- * - * @param newColor - new {@link Color} to be set - * @since Envoy 0.2-alpha - */ - public void setColor(int index, Color newColor) { - switch (index) { - case 0: - this.backgroundColor = newColor; - break; - case 1: - this.cellColor = newColor; - break; - case 2: - this.interactableForegroundColor = newColor; - break; - case 3: - this.interactableBackgroundColor = newColor; - break; - case 4: - this.messageColorChat = newColor; - break; - case 5: - this.dateColorChat = newColor; - break; - case 6: - this.selectionColor = newColor; - break; - case 7: - this.typingMessageColor = newColor; - break; - case 8: - this.userNameColor = newColor; - break; - } - } - -} +package envoy.client.ui; + +import java.awt.Color; +import java.io.Serializable; + +/** + * Project: envoy-client
+ * File: Theme.java
+ * Created: 23 Nov 2019
+ * + * @author Maximilian Käfer + * @since Envoy v0.2-alpha + */ +public class Theme implements Serializable { + + private static final long serialVersionUID = 141727847527060352L; + + private String themeName; + private Color backgroundColor; + private Color cellColor; + private Color interactableBackgroundColor; + private Color userNameColor; + private Color interactableForegroundColor; + private Color messageColorChat; + private Color dateColorChat; + private Color selectionColor; + private Color typingMessageColor; + + public Theme(String themeName, Color backgroundColor, Color cellColor, Color interactableForegroundColor, Color interactableBackgroundColor, + Color messageColorChat, Color dateColorChat, Color selectionColor, Color typingMessageColor, Color userNameColor) { + + this.themeName = themeName; + + this.backgroundColor = backgroundColor; + this.cellColor = cellColor; + this.interactableForegroundColor = interactableForegroundColor; + this.interactableBackgroundColor = interactableBackgroundColor; + this.messageColorChat = messageColorChat; + this.dateColorChat = dateColorChat; + this.selectionColor = selectionColor; + this.typingMessageColor = typingMessageColor; + this.userNameColor = userNameColor; + } + + public Theme(String name, Theme other) { + this(name, other.backgroundColor, other.cellColor, other.interactableForegroundColor, + other.interactableBackgroundColor, other.messageColorChat, other.dateColorChat, other.selectionColor, + other.typingMessageColor, other.userNameColor); + } + + /** + * @return name of the theme + * @since Envoy v0.2-alpha + */ + public String getThemeName() { return themeName; } + + /** + * @return interactableForegroundColor + * @since Envoy v0.2-alpha + */ + public Color getInteractableForegroundColor() { return interactableForegroundColor; } + + /** + * @return messageColorChat + * @since Envoy v0.2-alpha + */ + public Color getMessageColorChat() { return messageColorChat; } + + /** + * @return dateColorChat + * @since Envoy v0.2-alpha + */ + public Color getDateColorChat() { return dateColorChat; } + + /** + * @return selectionColor + * @since Envoy v0.2-alpha + */ + public Color getSelectionColor() { return selectionColor; } + + /** + * @return typingMessageColor + * @since Envoy v0.2-alpha + */ + public Color getTypingMessageColor() { return typingMessageColor; } + + /** + * @return backgroundColor + * @since Envoy v0.2-alpha + */ + public Color getBackgroundColor() { return backgroundColor; } + + /** + * @return cellColor + * @since Envoy v0.2-alpha + */ + public Color getCellColor() { return cellColor; } + + /** + * @return interactableBackgroundColor + * @since Envoy v0.2-alpha + */ + public Color getInteractableBackgroundColor() { return interactableBackgroundColor; } + + /** + * @return userNameColor + * @since Envoy v0.2-alpha + */ + public Color getUserNameColor() { return userNameColor; } + + /** + * Sets the a specific {@link Color} in this theme to a new {@link Color} + * + * @param index - index of the color
+ * 0 = backgroundColor
+ * 1 = cellColor
+ * 2 = interactableForegroundColor
+ * 3 = interactableBackgroundColor
+ * 4 = messageColorChat
+ * 5 = dateColorChat
+ * 6 = selectionColor
+ * 7 = typingMessageColor
+ * 8 = userNameColor
+ *
+ * + * @param newColor - new {@link Color} to be set + * @since Envoy 0.2-alpha + */ + public void setColor(int index, Color newColor) { + switch (index) { + case 0: + this.backgroundColor = newColor; + break; + case 1: + this.cellColor = newColor; + break; + case 2: + this.interactableForegroundColor = newColor; + break; + case 3: + this.interactableBackgroundColor = newColor; + break; + case 4: + this.messageColorChat = newColor; + break; + case 5: + this.dateColorChat = newColor; + break; + case 6: + this.selectionColor = newColor; + break; + case 7: + this.typingMessageColor = newColor; + break; + case 8: + this.userNameColor = newColor; + break; + } + } + +} diff --git a/src/main/java/envoy/client/ui/UserListRenderer.java b/src/main/java/envoy/client/ui/UserListRenderer.java index c3c7ef2..1ee3f84 100644 --- a/src/main/java/envoy/client/ui/UserListRenderer.java +++ b/src/main/java/envoy/client/ui/UserListRenderer.java @@ -12,8 +12,8 @@ import envoy.schema.User; import envoy.schema.User.UserStatus; /** - * Defines how the {@code UserList} is displayed. - * + * Defines how the {@code UserList} is displayed.
+ *
* Project: envoy-client
* File: UserListRenderer.java
* Created: 12 Oct 2019
diff --git a/src/main/java/envoy/client/util/EnvoyLog.java b/src/main/java/envoy/client/util/EnvoyLog.java new file mode 100644 index 0000000..db4e03c --- /dev/null +++ b/src/main/java/envoy/client/util/EnvoyLog.java @@ -0,0 +1,70 @@ +package envoy.client.util; + +import java.io.File; +import java.io.IOException; +import java.util.logging.ConsoleHandler; +import java.util.logging.FileHandler; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; + +/** + * Project: envoy-client
+ * File: EnvoyLogger.java
+ * Created: 14 Dec 2019
+ * + * @author Leon Hofmeister + * @since Envoy v0.2-alpha + */ +public class EnvoyLog { + + private static Level fileLevelBarrier = Level.CONFIG; + + private EnvoyLog() {} + + /** + * Creates a {@link Logger} with a specified name + * @param name the name of the {@link Logger} to create + * @return the created {@link Logger} + */ + public static Logger getLogger(String name) { + // Get a logger with the specified name + Logger logger = Logger.getLogger(name); + + final String logPath = "log/envoy_user.log"; + new File(logPath).getParentFile().mkdirs(); + + SimpleFormatter formatter = new SimpleFormatter(); + + try { + FileHandler fh = new FileHandler(logPath); + fh.setLevel(fileLevelBarrier); + fh.setFormatter(formatter); + logger.addHandler(fh); + } catch (SecurityException | IOException e) { + e.printStackTrace(); + } + + ConsoleHandler ch = new ConsoleHandler(); + ch.setLevel(Level.FINEST); + ch.setFormatter(formatter); + logger.addHandler(ch); + + return logger; + } + + /** + * @return the fileLevelBarrier: The current barrier for writing logs to a file. + * @since Envoy v0.2-alpha + */ + public static Level getFileLevelBarrier() { return fileLevelBarrier; } + + /** + * @param fileLevelBarrier the severity below which logRecords will be written + * only to the console. At or above they'll also be + * logged in a file. Can be written either in Digits + * from 0 - 1000 or with the according name of the level + * @since Envoy v0.2-alpha + */ + public static void setFileLevel(String fileLevelBarrier) { EnvoyLog.fileLevelBarrier = Level.parse(fileLevelBarrier); } +}