From 8267fa4d0d708778662d7a585b846e440bd8c6bc Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 14 Dec 2019 14:58:07 +0100 Subject: [PATCH 1/7] Implemented a (not working) own version of a logger and reformatted code --- src/main/java/envoy/client/Client.java | 21 +-- src/main/java/envoy/client/Config.java | 6 +- src/main/java/envoy/client/LocalDB.java | 3 +- src/main/java/envoy/client/Settings.java | 10 +- .../java/envoy/client/event/EnvoyLogger.java | 103 +++++++++++++ src/main/java/envoy/client/event/Event.java | 1 + src/main/java/envoy/client/ui/ChatWindow.java | 32 ++-- .../envoy/client/ui/MessageListRenderer.java | 12 +- .../java/envoy/client/ui/SettingsScreen.java | 145 +++++------------- src/main/java/envoy/client/ui/Startup.java | 3 +- .../java/envoy/client/ui/StatusTrayIcon.java | 8 +- src/main/java/envoy/client/ui/Theme.java | 12 +- .../envoy/client/ui/UserListRenderer.java | 24 +-- 13 files changed, 189 insertions(+), 191 deletions(-) create mode 100644 src/main/java/envoy/client/event/EnvoyLogger.java diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index 8f96739..bd1555a 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -1,7 +1,5 @@ package envoy.client; -import java.util.logging.Logger; - import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; @@ -10,6 +8,7 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; +import envoy.client.event.EnvoyLogger; import envoy.schema.ObjectFactory; import envoy.schema.Sync; import envoy.schema.User; @@ -30,7 +29,7 @@ public class Client { private Config config; private User sender, recipient; - private static final Logger logger = Logger.getLogger(Client.class.getSimpleName()); + private static final EnvoyLogger logger = new EnvoyLogger(Client.class.getSimpleName()); public Client(Config config, String username) { this.config = config; @@ -63,9 +62,7 @@ public class Client { user.setID(-1); sendSync.getUsers().add(user); - Sync returnSendSync = post( - String - .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0), + Sync returnSendSync = post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0), sendSync, Sync.class); return returnSendSync; @@ -85,9 +82,7 @@ public class Client { user.setName(name); senderSync.getUsers().add(user); - Sync returnSenderSync = post( - String - .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0), + Sync returnSenderSync = post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0), senderSync, Sync.class); @@ -134,7 +129,8 @@ public class Client { * their updated UserStatus to the client.)
* * @param userId the id of the {@link Client} who sends the {@link Sync} - * @param sync the sync object (yet to be converted from java class to sync.xml) + * @param sync the sync object (yet to be converted from java class to + * sync.xml) * @return a returnSync.xml file * @since Envoy v0.1-alpha */ @@ -151,10 +147,7 @@ public class Client { } // Send sync - return post(String - .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), userId), - sync, - Sync.class); + return post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), userId), sync, Sync.class); } /** diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index bbd3b72..e9f2a18 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -71,9 +71,7 @@ public class Config { * @return {@code true} if server, port and localDB directory are known. * @since Envoy v0.1-alpha */ - public boolean isInitialized() { - return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null; - } + public boolean isInitialized() { return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null; } /** * @return the host name of the Envoy server @@ -114,7 +112,7 @@ public class Config { /** * Changes the default local database. * Exclusively intended for development purposes. - * + * * @param localDB the file containing the local database * @since Envoy v0.1-alpha **/ diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index f061b28..51e2eb1 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -14,6 +14,7 @@ import java.util.logging.Logger; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; +import envoy.client.event.EnvoyLogger; import envoy.client.event.EventBus; import envoy.client.event.MessageCreationEvent; import envoy.exception.EnvoyException; @@ -44,7 +45,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 = new EnvoyLogger(LocalDB.class.getSimpleName()); /** * Constructs an empty local database. diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index 91f696f..fe5d769 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -73,9 +73,9 @@ public class Settings { // Load themes from theme file try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) { Object obj = in.readObject(); - if(obj instanceof HashMap) themes = (Map) obj; + if (obj instanceof HashMap) themes = (Map) obj; } catch (IOException | ClassNotFoundException e) { - themes = new HashMap<>(); + themes = new HashMap<>(); currentTheme = "dark"; e.printStackTrace(); } @@ -95,15 +95,15 @@ public class Settings { * @throws IOException * @since Envoy v0.2-alpha */ - public void save() throws IOException{ + public void save() throws IOException { prefs.put("username", getUsername()); prefs.put("email", getEmail()); prefs.put("theme", currentTheme); prefs.putBoolean("enterToSend", isEnterToSend()); - + // Save themes to theme file themeFile.createNewFile(); - try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) { + try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) { out.writeObject(themes); } } diff --git a/src/main/java/envoy/client/event/EnvoyLogger.java b/src/main/java/envoy/client/event/EnvoyLogger.java new file mode 100644 index 0000000..33e17ac --- /dev/null +++ b/src/main/java/envoy/client/event/EnvoyLogger.java @@ -0,0 +1,103 @@ +package envoy.client.event; + +import java.io.IOException; +import java.util.logging.ConsoleHandler; +import java.util.logging.FileHandler; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +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 EnvoyLogger extends Logger { + + private Logger logger; + private int fileLevel = 800; + + private Handler handler = new Handler() { + + @Override + public void publish(LogRecord arg0) { + ConsoleHandler ch; + FileHandler fh; + SimpleFormatter formatter = new SimpleFormatter(); + if (arg0.getLevel().intValue() >= fileLevel) {// Case if level >= info + try { + fh = new FileHandler("Envoy_user.log"); + logger.addHandler(fh); + formatter.formatMessage(arg0); + fh.setFormatter(formatter); + } catch (SecurityException | IOException e) { + e.printStackTrace(); + } + } + ch = new ConsoleHandler(); + logger.addHandler(ch); + formatter.formatMessage(arg0); + } + + @Override + public void flush() {} + + @Override + public void close() throws SecurityException {} + }; + + public EnvoyLogger(String name) { + super(name, null); + logger.addHandler(handler); + } + + /** + * Logs a message. If the problem severity is above the FileLevel-barrier, then + * the log entry will be written to a file. Regardless of problem severity, + * everything will be printed to the console. + * + * @param level the problem severity + * @param msg the message to be written in the log + * @since Envoy v0.2-alpha + */ + @Override + public void log(Level level, String msg) { + LogRecord lr = new LogRecord(level, msg); + logger.log(lr); + } + + /** + * Logs a message. If the problem severity is above the {@code FileLevel} + * barrier, then the log entry will be written to a file. + * Regardless of problem severity, everything will be printed to the console. + * + * @param logRecord the LogRecord (Level and String) to be treated by the + * Logger. + * @since Envoy v0.2-alpha + */ + @Override + public void log(LogRecord logRecord) { logger.log(logRecord); } + + /** + * @return the fileLevel: The current barrier for writing logs to a file. It can + * range from 100-1000 in steps of one hundred with 1000 being + * Level.SEVERE + * @since Envoy v0.2-alpha + */ + public int getFileLevel() { return fileLevel; } + + /** + * @param fileLevel the severity above which on logRecords will be written in a + * file instead of the console + * @since Envoy v0.2-alpha + */ + public void setFileLevel(int fileLevel) { + if (fileLevel <= 10) fileLevel *= 100; + this.fileLevel = fileLevel; + } +} diff --git a/src/main/java/envoy/client/event/Event.java b/src/main/java/envoy/client/event/Event.java index 9db2477..36e0466 100644 --- a/src/main/java/envoy/client/event/Event.java +++ b/src/main/java/envoy/client/event/Event.java @@ -6,6 +6,7 @@ package envoy.client.event; * Created: 04.12.2019
* * @author Kai S. K. Engelbart + * @param the type of our Event * @since Envoy v0.2-alpha */ public interface Event { diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 6867f5e..cafd790 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -12,7 +12,6 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.util.logging.Level; -import java.util.logging.Logger; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -33,6 +32,7 @@ import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; import envoy.client.Settings; +import envoy.client.event.EnvoyLogger; import envoy.schema.Message; import envoy.schema.Sync; import envoy.schema.User; @@ -68,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 EnvoyLogger logger = new EnvoyLogger(ChatWindow.class.getSimpleName()); public ChatWindow(Client client, LocalDB localDB) { this.client = client; @@ -132,8 +132,7 @@ public class ChatWindow extends JFrame { @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER - && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) - || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { + && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { postMessage(messageList); } } @@ -180,10 +179,9 @@ public class ChatWindow extends JFrame { settingsButton.addActionListener((evt) -> { try { - SettingsScreen.open(); - changeChatWindowColors(Settings.getInstance().getCurrentTheme()); - } catch (Exception e) { - SettingsScreen.open(); + new SettingsScreen().setVisible(true); + changeChatWindowColors(Settings.getInstance().getCurrentTheme()); + } catch (Exception e) { logger.log(Level.WARNING, "An error occured while opening the settings screen", e); e.printStackTrace(); } @@ -235,7 +233,7 @@ public class ChatWindow extends JFrame { gbc_userList.insets = new Insets(space, space, space, space); changeChatWindowColors(Settings.getInstance().getCurrentTheme()); - + contentPane.add(userList, gbc_userList); contentPane.revalidate(); @@ -244,7 +242,6 @@ public class ChatWindow extends JFrame { contentPane.revalidate(); } - /** * Used to immediately reload the ChatWindow when settings were changed. @@ -287,18 +284,14 @@ public class ChatWindow extends JFrame { private void postMessage(JList messageList) { if (!client.hasRecipient()) { - JOptionPane.showMessageDialog(this, - "Please select a recipient!", - "Cannot send message", - JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE); return; } if (!messageEnterTextArea.getText().isEmpty()) try { // Create and send message object - final Message message = localDB.createMessage(messageEnterTextArea.getText(), - currentChat.getRecipient().getID()); + final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient().getID()); currentChat.appendMessage(message); messageList.setModel(currentChat.getModel()); @@ -348,8 +341,7 @@ public class ChatWindow extends JFrame { new Thread(() -> { // Synchronize - localDB.applySync( - client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID()))); + localDB.applySync(client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID()))); // Process unread messages localDB.addUnreadMessagesToLocalDB(); @@ -359,8 +351,7 @@ public class ChatWindow extends JFrame { readCurrentChat(); // Update UI - SwingUtilities - .invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); }); + SwingUtilities.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); }); }).start(); }).start(); } @@ -377,4 +368,3 @@ public class ChatWindow extends JFrame { */ private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } } } - diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java index 2e6b65f..9be79c5 100644 --- a/src/main/java/envoy/client/ui/MessageListRenderer.java +++ b/src/main/java/envoy/client/ui/MessageListRenderer.java @@ -42,22 +42,18 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer

%s

%s :%s", + setText(String.format("

%s

%s :%s", dateColor, date, textColor, diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index bd80c2e..c9fea6c 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.event.EnvoyLogger; /** * This class provides the GUI to change the user specific settings. @@ -54,45 +55,28 @@ 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 selectedTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); + private Theme temporaryTheme; - private boolean colorChanged = false; - private Theme temporaryTheme; - - private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); - - private static SettingsScreen settingsScreen; + private static final Logger logger = new EnvoyLogger(SettingsScreen.class.getSimpleName()); // 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); - } - /** * Builds the settings screen. * * @since Envoy v0.1-alpha */ - private SettingsScreen() { + public SettingsScreen() { logger.info(Settings.getInstance().getCurrentTheme()); setBounds(10, 10, 450, 650); @@ -101,8 +85,7 @@ public class SettingsScreen extends JDialog { createNewThemeButton.setEnabled(false); - temporaryTheme = new Theme("temporaryTheme", - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); + temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); // Content pane GridBagLayout gbl_contentPanel = new GridBagLayout(); @@ -122,7 +105,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": @@ -183,7 +166,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); } }); @@ -200,20 +183,8 @@ public class SettingsScreen extends JDialog { colorsPanel.setLayout(new BoxLayout(colorsPanel, BoxLayout.Y_AXIS)); colorsPanel.setAlignmentX(Component.LEFT_ALIGNMENT); - buildCustomizeElement(new JPanel(), - new JButton(), - new JTextPane(), - theme, - theme.getBackgroundColor(), - "Background", - 1); - buildCustomizeElement(new JPanel(), - new JButton(), - new JTextPane(), - theme, - theme.getCellColor(), - "Cells", - 2); + buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getBackgroundColor(), "Background", 1); + buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getCellColor(), "Cells", 2); buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), @@ -228,41 +199,11 @@ public class SettingsScreen extends JDialog { theme.getInteractableBackgroundColor(), "Interactable Background", 4); - buildCustomizeElement(new JPanel(), - new JButton(), - new JTextPane(), - theme, - theme.getMessageColorChat(), - "Messages Chat", - 5); - buildCustomizeElement(new JPanel(), - new JButton(), - new JTextPane(), - theme, - theme.getDateColorChat(), - "Date Chat", - 6); - buildCustomizeElement(new JPanel(), - new JButton(), - new JTextPane(), - theme, - theme.getSelectionColor(), - "Selection", - 7); - buildCustomizeElement(new JPanel(), - new JButton(), - new JTextPane(), - theme, - theme.getTypingMessageColor(), - "Typing Message", - 8); - buildCustomizeElement(new JPanel(), - new JButton(), - new JTextPane(), - theme, - theme.getUserNameColor(), - "User Names", - 9); + buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getMessageColorChat(), "Messages Chat", 5); + buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getDateColorChat(), "Date Chat", 6); + buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getSelectionColor(), "Selection", 7); + buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getTypingMessageColor(), "Typing Message", 8); + buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getUserNameColor(), "User Names", 9); GridBagConstraints gbc_colorsPanel = new GridBagConstraints(); gbc_colorsPanel.fill = GridBagConstraints.HORIZONTAL; @@ -281,18 +222,16 @@ 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(), temporaryTheme.getMessageColorChat(), - temporaryTheme.getDateColorChat(), temporaryTheme.getSelectionColor(), + .addNewThemeToMap(new Theme(s, temporaryTheme.getBackgroundColor(), temporaryTheme.getCellColor(), + temporaryTheme.getInteractableForegroundColor(), temporaryTheme.getInteractableBackgroundColor(), + temporaryTheme.getMessageColorChat(), temporaryTheme.getDateColorChat(), temporaryTheme.getSelectionColor(), temporaryTheme.getTypingMessageColor(), temporaryTheme.getUserNameColor())); themeArray = Arrays.copyOf(themeArray, themeArray.length + 1); themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(s).getThemeName(); - temporaryTheme = new Theme("temporaryTheme", - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); + temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); createNewThemeButton.setEnabled(false); themes.addItem(themeArray[themeArray.length - 1]); @@ -355,7 +294,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()); changeSettingsScreenColors(Settings.getInstance().getCurrentTheme()); updateColorVariables(Settings.getInstance().getCurrentTheme()); @@ -365,13 +304,17 @@ 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().getCurrentTheme()); + + setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + setModal(true); } private void changeSettingsScreenColors(String key) { @@ -412,21 +355,12 @@ public class SettingsScreen extends JDialog { temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getBackgroundColor(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor(), - Settings.getInstance() - .getThemes() - .get(Settings.getInstance().getCurrentTheme()) - .getInteractableForegroundColor(), - Settings.getInstance() - .getThemes() - .get(Settings.getInstance().getCurrentTheme()) - .getInteractableBackgroundColor(), + Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableForegroundColor(), + Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getSelectionColor(), - Settings.getInstance() - .getThemes() - .get(Settings.getInstance().getCurrentTheme()) - .getTypingMessageColor(), + Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getTypingMessageColor(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor()); colorsPanel.removeAll(); @@ -513,8 +447,7 @@ public class SettingsScreen extends JDialog { private void setContent(JPanel content, GridBagConstraints layout) { contentPanel.add(content, layout); } - private void buildCustomizeElement(JPanel panel, JButton button, JTextPane textPane, Theme theme, Color color, - String name, int yIndex) { + private void buildCustomizeElement(JPanel panel, JButton button, JTextPane textPane, Theme theme, Color color, String name, int yIndex) { textPane.setFont(new Font("Arial", Font.PLAIN, 14)); textPane.setBackground(theme.getBackgroundColor()); textPane.setForeground(theme.getUserNameColor()); @@ -528,15 +461,11 @@ 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()); - // TODO: When Theme changed in same settings screen, color variable doesnt - // update. + logger.log(Level.FINEST, String.valueOf(color.getRGB())); Color[] colorsArray = temporaryTheme.getAllColors(); for (int i = 0; i < colorsArray.length; i++) { if (color.getRGB() == colorsArray[i].getRGB()) { temporaryTheme.setColor(i, newColor); - colorChanged = true; createNewThemeButton.setEnabled(true); break; } diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index edd5aa2..33e5c78 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -12,6 +12,7 @@ import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; import envoy.client.Settings; +import envoy.client.event.EnvoyLogger; import envoy.exception.EnvoyException; /** @@ -28,7 +29,7 @@ import envoy.exception.EnvoyException; */ public class Startup { - private static final Logger logger = Logger.getLogger(Startup.class.getSimpleName()); + private static final Logger logger = new EnvoyLogger(Startup.class.getSimpleName()); public static void main(String[] args) { logger.setLevel(Level.ALL); diff --git a/src/main/java/envoy/client/ui/StatusTrayIcon.java b/src/main/java/envoy/client/ui/StatusTrayIcon.java index 01cc8d7..fee9a7c 100644 --- a/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -75,14 +75,10 @@ public class StatusTrayIcon implements EventHandler { focusTarget.addWindowFocusListener(new WindowAdapter() { @Override - public void windowGainedFocus(WindowEvent e) { - displayMessages = false; - } + public void windowGainedFocus(WindowEvent e) { displayMessages = false; } @Override - public void windowLostFocus(WindowEvent e) { - displayMessages = true; - } + public void windowLostFocus(WindowEvent e) { displayMessages = true; } }); // Start processing message events diff --git a/src/main/java/envoy/client/ui/Theme.java b/src/main/java/envoy/client/ui/Theme.java index a0ef259..7de0ea9 100644 --- a/src/main/java/envoy/client/ui/Theme.java +++ b/src/main/java/envoy/client/ui/Theme.java @@ -26,9 +26,8 @@ public class Theme implements Serializable { 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) { + 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; @@ -42,11 +41,10 @@ public class Theme implements Serializable { this.typingMessageColor = typingMessageColor; this.userNameColor = userNameColor; } - + public Theme(String name, Theme other) { - this(name, other.backgroundColor, other.cellColor, other.interactableBackgroundColor, - other.interactableForegroundColor, other.messageColorChat, other.dateColorChat, other.selectionColor, - other.typingMessageColor, other.userNameColor); + this(name, other.backgroundColor, other.cellColor, other.interactableBackgroundColor, other.interactableForegroundColor, + other.messageColorChat, other.dateColorChat, other.selectionColor, other.typingMessageColor, other.userNameColor); } /** diff --git a/src/main/java/envoy/client/ui/UserListRenderer.java b/src/main/java/envoy/client/ui/UserListRenderer.java index c730e74..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
@@ -26,7 +26,6 @@ public class UserListRenderer extends JLabel implements ListCellRenderer { private static final long serialVersionUID = 5164417379767181198L; - @SuppressWarnings("incomplete-switch") @Override public Component getListCellRendererComponent(JList list, User value, int index, boolean isSelected, boolean cellHasFocus) { if (isSelected) { @@ -44,23 +43,16 @@ public class UserListRenderer extends JLabel implements ListCellRenderer { final UserStatus status = value.getStatus(); // Getting the UserNameColor of the current theme - String textColor = null; - textColor = toHex( - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor()); + String textColor = null; + textColor = toHex(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor()); switch (status) { case ONLINE: - setText(String.format( - "

%s

%s", - status, - textColor, - name)); + setText(String + .format("

%s

%s", status, textColor, name)); break; case OFFLINE: - setText(String.format( - "

%s

%s", - status, - textColor, - name)); + setText(String + .format("

%s

%s", status, textColor, name)); break; } return this; From 7e02217002ca174b27592aa7a033fe918df18b60 Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 18 Dec 2019 17:11:56 +0100 Subject: [PATCH 2/7] Updated EnvoyLogger -> still not working --- src/main/java/envoy/client/Settings.java | 16 ++--- .../java/envoy/client/event/EnvoyLogger.java | 64 +++++++------------ src/main/java/envoy/client/ui/ChatWindow.java | 6 +- 3 files changed, 33 insertions(+), 53 deletions(-) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index fe5d769..f078d2d 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -92,7 +92,7 @@ public class Settings { /** * updates prefs when save button is clicked * - * @throws IOException + * @throws IOException if saving was not successful * @since Envoy v0.2-alpha */ public void save() throws IOException { @@ -111,7 +111,7 @@ public class Settings { /** * adds new theme to the theme map and sets current theme to the new theme. * - * @param theme + * @param theme the theme to add * @since Envoy v0.2-alpha */ public void addNewThemeToMap(Theme theme) { @@ -120,7 +120,7 @@ public class Settings { } /** - * @return {@link currentTheme} + * @return the name of the current theme * @since Envoy v0.2-alpha */ public String getCurrentTheme() { return currentTheme; } @@ -128,7 +128,7 @@ public class Settings { /** * Sets the currentTheme * - * @param themeName + * @param themeName the name of the new current theme * @since Envoy v0.2-alpha */ public void setCurrentTheme(String themeName) { currentTheme = themeName; } @@ -174,16 +174,16 @@ public class Settings { public void setEnterToSend(boolean enterToSend) { this.enterToSend = enterToSend; } /** - * @return {@link themes} map + * @return the map of all themes by name and colorContent * @since Envoy v0.2-alpha */ public Map getThemes() { return themes; } /** - * Sets {@link themes} + * @deprecated not used * - * @param themes + * @param themes the the the the * @since Envoy v0.2-alpha */ - public void setThemes(Map themes) { this.themes = themes; } + public void setThemes(Map themes) { this.themes = themes; }// TODO delete, if there is no usage } \ No newline at end of file diff --git a/src/main/java/envoy/client/event/EnvoyLogger.java b/src/main/java/envoy/client/event/EnvoyLogger.java index 33e17ac..cbc2c88 100644 --- a/src/main/java/envoy/client/event/EnvoyLogger.java +++ b/src/main/java/envoy/client/event/EnvoyLogger.java @@ -3,7 +3,6 @@ package envoy.client.event; import java.io.IOException; import java.util.logging.ConsoleHandler; import java.util.logging.FileHandler; -import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; @@ -20,40 +19,24 @@ import java.util.logging.SimpleFormatter; public class EnvoyLogger extends Logger { private Logger logger; - private int fileLevel = 800; - - private Handler handler = new Handler() { - - @Override - public void publish(LogRecord arg0) { - ConsoleHandler ch; - FileHandler fh; - SimpleFormatter formatter = new SimpleFormatter(); - if (arg0.getLevel().intValue() >= fileLevel) {// Case if level >= info - try { - fh = new FileHandler("Envoy_user.log"); - logger.addHandler(fh); - formatter.formatMessage(arg0); - fh.setFormatter(formatter); - } catch (SecurityException | IOException e) { - e.printStackTrace(); - } - } - ch = new ConsoleHandler(); - logger.addHandler(ch); - formatter.formatMessage(arg0); - } - - @Override - public void flush() {} - - @Override - public void close() throws SecurityException {} - }; + private Level fileLevelBarrier = Level.CONFIG; public EnvoyLogger(String name) { super(name, null); - logger.addHandler(handler); + try { + SimpleFormatter formatter = new SimpleFormatter(); + FileHandler fh = new FileHandler("envoy_user.log"); + fh.setLevel(fileLevelBarrier); + fh.setFormatter(formatter); + ConsoleHandler ch = new ConsoleHandler(); + ch.setLevel(Level.FINEST); + ch.setFormatter(formatter); + logger.addHandler(fh); + logger.addHandler(ch); + } catch (IOException | SecurityException e) { + e.printStackTrace(); + this.log(Level.FINE, "Ironically, the logger encountered an error while initialising. That certainly needs to be logged :)"); + } } /** @@ -84,20 +67,17 @@ public class EnvoyLogger extends Logger { public void log(LogRecord logRecord) { logger.log(logRecord); } /** - * @return the fileLevel: The current barrier for writing logs to a file. It can - * range from 100-1000 in steps of one hundred with 1000 being - * Level.SEVERE + * @return the fileLevelBarrier: The current barrier for writing logs to a file. * @since Envoy v0.2-alpha */ - public int getFileLevel() { return fileLevel; } + public Level getFileLevelBarrier() { return fileLevelBarrier; } /** - * @param fileLevel the severity above which on logRecords will be written in a - * file instead of the console + * @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 void setFileLevel(int fileLevel) { - if (fileLevel <= 10) fileLevel *= 100; - this.fileLevel = fileLevel; - } + public void setFileLevel(String fileLevelBarrier) { this.fileLevelBarrier = Level.parse(fileLevelBarrier); } } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index cafd790..8761b74 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -245,11 +245,11 @@ public class ChatWindow extends JFrame { /** * Used to immediately reload the ChatWindow when settings were changed. - * + * @param themeName the name of the theme to change the colors into * @since Envoy v0.1-alpha */ - public void changeChatWindowColors(String key) { - Theme theme = Settings.getInstance().getThemes().get(key); + public void changeChatWindowColors(String themeName) { + Theme theme = Settings.getInstance().getThemes().get(themeName); // contentPane contentPane.setBackground(theme.getBackgroundColor()); From 04618569a4e491916a3b917744931325323d0ca8 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 20 Dec 2019 11:31:12 +0100 Subject: [PATCH 3/7] Improved Javadoc slightly --- src/main/java/envoy/client/event/EnvoyLogger.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/envoy/client/event/EnvoyLogger.java b/src/main/java/envoy/client/event/EnvoyLogger.java index cbc2c88..da562a3 100644 --- a/src/main/java/envoy/client/event/EnvoyLogger.java +++ b/src/main/java/envoy/client/event/EnvoyLogger.java @@ -73,9 +73,9 @@ public class EnvoyLogger extends Logger { public 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 + * @param fileLevelBarrier the severity below which {@link LogRecord}s 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 */ From 88d2e42494accf5bbb8c9be466ef9521d80257c9 Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 20 Dec 2019 11:59:11 +0100 Subject: [PATCH 4/7] Fixed Logger initialization Renamed EnvoyLogger to EnvoyLog and moved it to the newl< created envoy.client.util package. --- src/main/java/envoy/client/Client.java | 6 +- src/main/java/envoy/client/LocalDB.java | 4 +- .../java/envoy/client/event/EnvoyLogger.java | 83 ------------------- src/main/java/envoy/client/ui/ChatWindow.java | 5 +- .../java/envoy/client/ui/SettingsScreen.java | 4 +- src/main/java/envoy/client/ui/Startup.java | 7 +- src/main/java/envoy/client/util/EnvoyLog.java | 70 ++++++++++++++++ 7 files changed, 83 insertions(+), 96 deletions(-) delete mode 100644 src/main/java/envoy/client/event/EnvoyLogger.java create mode 100644 src/main/java/envoy/client/util/EnvoyLog.java diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index bd1555a..1a8ce34 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -1,5 +1,7 @@ package envoy.client; +import java.util.logging.Logger; + import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; @@ -8,7 +10,7 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; -import envoy.client.event.EnvoyLogger; +import envoy.client.util.EnvoyLog; import envoy.schema.ObjectFactory; import envoy.schema.Sync; import envoy.schema.User; @@ -29,7 +31,7 @@ public class Client { private Config config; private User sender, recipient; - private static final EnvoyLogger logger = new EnvoyLogger(Client.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(Client.class.getSimpleName()); public Client(Config config, String username) { this.config = config; diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 51e2eb1..b09c808 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -14,9 +14,9 @@ import java.util.logging.Logger; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; -import envoy.client.event.EnvoyLogger; 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; @@ -45,7 +45,7 @@ public class LocalDB { private Sync sync = objectFactory.createSync(); private Sync readMessages = objectFactory.createSync(); - private static final Logger logger = new EnvoyLogger(LocalDB.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(LocalDB.class.getSimpleName()); /** * Constructs an empty local database. diff --git a/src/main/java/envoy/client/event/EnvoyLogger.java b/src/main/java/envoy/client/event/EnvoyLogger.java deleted file mode 100644 index cbc2c88..0000000 --- a/src/main/java/envoy/client/event/EnvoyLogger.java +++ /dev/null @@ -1,83 +0,0 @@ -package envoy.client.event; - -import java.io.IOException; -import java.util.logging.ConsoleHandler; -import java.util.logging.FileHandler; -import java.util.logging.Level; -import java.util.logging.LogRecord; -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 EnvoyLogger extends Logger { - - private Logger logger; - private Level fileLevelBarrier = Level.CONFIG; - - public EnvoyLogger(String name) { - super(name, null); - try { - SimpleFormatter formatter = new SimpleFormatter(); - FileHandler fh = new FileHandler("envoy_user.log"); - fh.setLevel(fileLevelBarrier); - fh.setFormatter(formatter); - ConsoleHandler ch = new ConsoleHandler(); - ch.setLevel(Level.FINEST); - ch.setFormatter(formatter); - logger.addHandler(fh); - logger.addHandler(ch); - } catch (IOException | SecurityException e) { - e.printStackTrace(); - this.log(Level.FINE, "Ironically, the logger encountered an error while initialising. That certainly needs to be logged :)"); - } - } - - /** - * Logs a message. If the problem severity is above the FileLevel-barrier, then - * the log entry will be written to a file. Regardless of problem severity, - * everything will be printed to the console. - * - * @param level the problem severity - * @param msg the message to be written in the log - * @since Envoy v0.2-alpha - */ - @Override - public void log(Level level, String msg) { - LogRecord lr = new LogRecord(level, msg); - logger.log(lr); - } - - /** - * Logs a message. If the problem severity is above the {@code FileLevel} - * barrier, then the log entry will be written to a file. - * Regardless of problem severity, everything will be printed to the console. - * - * @param logRecord the LogRecord (Level and String) to be treated by the - * Logger. - * @since Envoy v0.2-alpha - */ - @Override - public void log(LogRecord logRecord) { logger.log(logRecord); } - - /** - * @return the fileLevelBarrier: The current barrier for writing logs to a file. - * @since Envoy v0.2-alpha - */ - public 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 void setFileLevel(String fileLevelBarrier) { this.fileLevelBarrier = Level.parse(fileLevelBarrier); } -} diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 8761b74..cbdd7f5 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -12,6 +12,7 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.util.logging.Level; +import java.util.logging.Logger; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -32,7 +33,7 @@ import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; import envoy.client.Settings; -import envoy.client.event.EnvoyLogger; +import envoy.client.util.EnvoyLog; import envoy.schema.Message; import envoy.schema.Sync; import envoy.schema.User; @@ -68,7 +69,7 @@ public class ChatWindow extends JFrame { private static int space = 4; - private static final EnvoyLogger logger = new EnvoyLogger(ChatWindow.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName()); public ChatWindow(Client client, LocalDB localDB) { this.client = client; diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index c9fea6c..490985a 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -27,7 +27,7 @@ import javax.swing.JTextPane; import javax.swing.ListSelectionModel; import envoy.client.Settings; -import envoy.client.event.EnvoyLogger; +import envoy.client.util.EnvoyLog; /** * This class provides the GUI to change the user specific settings. @@ -64,7 +64,7 @@ public class SettingsScreen extends JDialog { private Theme selectedTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); private Theme temporaryTheme; - private static final Logger logger = new EnvoyLogger(SettingsScreen.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(SettingsScreen.class.getSimpleName()); // TODO: Add a JPanel with all the Information necessary: // change (Picture,Username, Email, Password) and toggle(light/dark mode, diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 33e5c78..bbfedb6 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -3,7 +3,6 @@ package envoy.client.ui; import java.awt.EventQueue; import java.io.IOException; import java.util.Properties; -import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; @@ -12,7 +11,7 @@ import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; import envoy.client.Settings; -import envoy.client.event.EnvoyLogger; +import envoy.client.util.EnvoyLog; import envoy.exception.EnvoyException; /** @@ -29,11 +28,9 @@ import envoy.exception.EnvoyException; */ public class Startup { - private static final Logger logger = new EnvoyLogger(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(); // Load the configuration from client.properties first 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); } +} From 5614bdc3143b01677e46818ce92837ad4a531ce8 Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 20 Dec 2019 12:01:09 +0100 Subject: [PATCH 5/7] Added log directory to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From 02baf70d92105a17e55a80c8445c30fa1049cf26 Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 20 Dec 2019 12:36:53 +0100 Subject: [PATCH 6/7] Fixed errors caused by preparing the merge into develop --- src/main/java/envoy/client/ui/ChatWindow.java | 2 +- .../java/envoy/client/ui/SettingsScreen.java | 9 +------- src/main/java/envoy/client/ui/Startup.java | 23 ++++++++----------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 378c990..8814195 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -170,7 +170,7 @@ public class ChatWindow extends JFrame { settingsButton.addActionListener((evt) -> { try { new SettingsScreen().setVisible(true); - changeChatWindowColors(Settings.getInstance().getCurrentTheme()); + changeChatWindowColors(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); } catch (Exception e) { logger.log(Level.WARNING, "An error occured while opening the settings screen", e); e.printStackTrace(); diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index d659beb..75ce884 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -64,17 +64,10 @@ public class SettingsScreen extends JDialog { private static int space = 5; - private Theme temporaryTheme; + private Theme temporaryTheme, selectedTheme; private static final Logger logger = EnvoyLog.getLogger(SettingsScreen.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) - /** * Builds the settings screen. * diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 9b69cd8..b9f9633 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -2,6 +2,7 @@ package envoy.client.ui; import java.awt.EventQueue; import java.io.IOException; +import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFrame; @@ -10,9 +11,7 @@ import javax.swing.JOptionPane; import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; -import envoy.client.Settings; import envoy.client.util.EnvoyLog; - import envoy.exception.EnvoyException; import envoy.schema.User; @@ -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) { From e7f6826e37e824537e38df44dd0f00ce77f83811 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 20 Dec 2019 12:51:35 +0100 Subject: [PATCH 7/7] Deleted an unnecessary comment as per @CyB3RC0nN0Rs request --- src/main/java/envoy/client/Settings.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index 78472ef..fe49ced 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -150,10 +150,8 @@ public class Settings { public Map getThemes() { return themes; } /** - * @deprecated not used - * * @param themes the the the the * @since Envoy v0.2-alpha */ - public void setThemes(Map themes) { this.themes = themes; }// TODO delete, if there is no usage + public void setThemes(Map themes) { this.themes = themes; } } \ No newline at end of file