From 48b9176ac2e33ef21aa6efae55dcff73ed92c606 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 22 Nov 2019 06:37:59 +0100 Subject: [PATCH 1/8] Implemented settings object and cleaned up ChatWindow Improvements: * Settings were implemented * Light theme support was implemented * The readability of ChatWindow got improved --- src/main/java/envoy/client/Config.java | 16 +- src/main/java/envoy/client/LocalDB.java | 39 ++-- src/main/java/envoy/client/Settings.java | 129 ++++++++++++ src/main/java/envoy/client/ui/ChatWindow.java | 130 +++++++----- .../java/envoy/client/ui/SettingsScreen.java | 187 ++++++++---------- src/main/java/envoy/client/ui/Startup.java | 4 +- src/main/java/envoy/client/ui/UIColors.java | 158 +++++++++++++++ .../envoy/client/ui/UserListRenderer.java | 6 + 8 files changed, 492 insertions(+), 177 deletions(-) create mode 100644 src/main/java/envoy/client/Settings.java create mode 100644 src/main/java/envoy/client/ui/UIColors.java diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index c31c972..fd83299 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -63,6 +63,7 @@ public class Config { case "--localDB": case "-db": localDB = new File(args[++i]); + break; } if (localDB == null) localDB = new File(".\\localDB"); if (syncTimeout == 0) syncTimeout = 1000; @@ -115,13 +116,24 @@ public class Config { /** * Changes the default local database. * Exclusively intended for development purposes. - * - * @param the file containing the local database + * + * @param localDB the file containing the local database * @since Envoy v0.1-alpha **/ public void setLocalDB(File localDB) { this.localDB = localDB; } + /** + * + * @return the current time (milliseconds) that is waited between Syncs + * @since Envoy v0.1-alpha + */ public int getSyncTimeout() { return syncTimeout; } + /** + * + * @param syncTimeout sets the time (milliseconds) during which Sync waits + * @since Envoy v0.1-alpha + */ public void setSyncTimeout(int syncTimeout) { this.syncTimeout = syncTimeout; } + } diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 142b687..5b702e8 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -30,8 +30,10 @@ import envoy.schema.User; * @since Envoy v0.1-alpha */ public class LocalDB { + private File localDB; private User sender; + private final long id; private List chats = new ArrayList<>(); private ObjectFactory objectFactory = new ObjectFactory(); private DatatypeFactory datatypeFactory; @@ -39,19 +41,19 @@ public class LocalDB { /** * Constructs an empty local database. * - * @param client the user that is logged in with this client + * @param sender the user that is logged in with this client * @since Envoy v0.1-alpha **/ public LocalDB(User sender) { - this.sender = sender; + this.sender = sender; + id = sender.getID(); try { datatypeFactory = DatatypeFactory.newInstance(); } catch (DatatypeConfigurationException e) { e.printStackTrace(); } } - /** * Initializes the local database and fills it with values @@ -64,14 +66,16 @@ public class LocalDB { public void initializeDBFile(File localDBDir) throws EnvoyException { if (localDBDir.exists() && !localDBDir.isDirectory()) throw new EnvoyException( String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); - localDB = new File(localDBDir, sender.getID() + ".db"); + localDB = new File(localDBDir, id + ".db"); if (localDB.exists()) loadFromLocalDB(); } /** - * Saves the database into a file for future use. + * Saves the database into a file for future use.
+ * It is theoretically possible to fail due to unknown causes.
+ * In such a case, every message sent/ received during that session will be + * lost. * - * @throws IOException if something went wrong during saving * @since Envoy v0.1-alpha **/ public void saveToLocalDB() { @@ -110,12 +114,13 @@ public class LocalDB { * Creates a {@link Message} object serializable to XML. * * @param textContent The content (text) of the message + * @param recipientID self explanatory * @return prepared {@link Message} object */ - public Message createMessage(String textContent, User recipient) { + public Message createMessage(String textContent, long recipientID) { Message.Metadata metaData = objectFactory.createMessageMetadata(); metaData.setSender(sender.getID()); - metaData.setRecipient(recipient.getID()); + metaData.setRecipient(recipientID); metaData.setState(MessageState.WAITING); metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString())); @@ -257,7 +262,7 @@ public class LocalDB { /** * Adds a message to the "sync" Sync object. * - * @param message + * @param message the message to send * @since Envoy v0.1-alpha */ public void addMessageToSync(Message message) { sync.getMessages().add(message); } @@ -274,7 +279,6 @@ public class LocalDB { * Adds the unread messages returned from the server in the latest sync to the * right chats in the LocalDB. * - * @param localDB * @since Envoy v0.1-alpha */ public void addUnreadMessagesToLocalDB() { @@ -292,7 +296,6 @@ public class LocalDB { * Gets all messages with state SENT from the LocalDB and adds them to the * "sync" Sync object. * - * @param localDB * @since Envoy v0.1-alpha */ public void getSentStateMessagesFromLocalDB() { @@ -310,7 +313,7 @@ public class LocalDB { *
* Adds these Messages to the {@code readMessages} {@link Sync} object. * - * @param currentChat + * @param currentChat the chat that was just opened * @since Envoy v0.1-alpha */ public void setMessagesToRead(Chat currentChat) { @@ -325,8 +328,8 @@ public class LocalDB { /** * Adds a message with State WAITING to a specific chat in the LocalDB. * - * @param message - * @param currentChat + * @param message the message that is not yet received by the other user + * @param currentChat the chat that is currently open * @since Envoy v0.1-alpha */ public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); } @@ -334,7 +337,6 @@ public class LocalDB { /** * Adds all messages with State WAITING from the {@link LocalDB} to the Sync. * - * @param localDB * @since Envoy v0.1-alpha */ public void addWaitingMessagesToSync() { @@ -368,4 +370,11 @@ public class LocalDB { * @since Envoy v0.1-alpha */ public User getUser() { return sender; } + + /** + * @return the id of the client. Used as shortcut for quicker information + * retrieval + * @since Envoy v0.2-alpha + */ + public long getId() { return id; } } diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java new file mode 100644 index 0000000..7377876 --- /dev/null +++ b/src/main/java/envoy/client/Settings.java @@ -0,0 +1,129 @@ +package envoy.client; + +import java.util.prefs.Preferences; + +/** + * Project: envoy-client
+ * File: Settings.java
+ * Created: 11 Nov 2019
+ * + * @author Leon Hofmeister + * @since Envoy v0.2-alpha + */ +public class Settings { + + private String username; + private String email; + private boolean enterToSend = true; + private boolean darkMode = true; + // private Image profilePic; + private static Settings settings; + private Preferences prefs = Preferences.userNodeForPackage(Settings.class); + + /** + * The way to instantiate the settings. + * Is set to private to deny other instances of that object. + * + * @since Envoy v0.2-alpha + */ + private Settings() {} + + /** + * This method is used to ensure that there is only one instance of Settings. + * + * @return the instance of Settings + * @since Envoy v0.2-alpha + */ + public static Settings getInstance() { + if (settings == null) { + settings = new Settings(); + } + settings.load(); + return settings; + } + + public void load() { + settings.setUsername(prefs.get("username", "")); + settings.setEmail(prefs.get("email", "")); + settings.setDarkMode(prefs.getBoolean("darkMode", true)); + settings.setEnterToSend(prefs.getBoolean("enterToSend", true)); + } + + public void save() { + prefs.put("username", settings.getUsername()); + prefs.put("email", settings.getEmail()); + prefs.putBoolean("darkMode", settings.isDarkMode()); + prefs.putBoolean("enterToSend", settings.isEnterToSend()); + } + + /** + * @return the username + * @since Envoy v0.2-alpha + */ + public String getUsername() { return username; } + + /** + * @param username the username to set + * @since Envoy v0.2-alpha + */ + public void setUsername(String username) { this.username = username; } + + /** + * @return the email associated with that user. + * @since Envoy v0.2-alpha + */ + public String getEmail() { return email; } + + /** + * @param email the email to set + * @since Envoy v0.2-alpha + */ + public void setEmail(String email) { this.email = email; } + + /** + * @return true, if "enter" suffices to send a message, else it has to be "ctrl" + * + "enter" + * @since Envoy v0.2-alpha + */ + public boolean isEnterToSend() { return enterToSend; } + + /** + * Change mode of posting a message via Keystroke. + * + * @param enterToSend if true, "enter" suffices to send a message,
+ * else it has to be "ctrl" + "enter" + * @since Envoy v0.2-alpha + */ + public void setEnterToSend(boolean enterToSend) { this.enterToSend = enterToSend; } + + /** + * Describes whether the Envoy GUI should be displayed in dark mode or not. + * + * @return true, if dark mode display is currently set, else the light theme + * will be displayed + * @since Envoy v0.2-alpha + */ + public boolean isDarkMode() { return darkMode; } + + /** + * Change display mode of Envoy GUI. + * + * @param darkMode true, if dark mode display is currently set,
+ * else the light theme will be displayed + * @since Envoy v0.2-alpha + */ + public void setDarkMode(boolean darkMode) { this.darkMode = darkMode; } + + // /** + // * @return the profilePic + // * @since Envoy v0.2-alpha + // */ + // public Image getProfilePic() { return profilePic; } + // + // /** + // * @param profilePic the profilePic to set + // * @since Envoy v0.1-alpha + // */ + // public void setProfilePic(Image profilePic) { this.profilePic = profilePic; } + +} diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 2a7ae11..f2aa7d8 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -1,6 +1,5 @@ package envoy.client.ui; -import java.awt.Color; import java.awt.ComponentOrientation; import java.awt.Font; import java.awt.GridBagConstraints; @@ -29,6 +28,7 @@ import envoy.client.Chat; import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; +import envoy.client.Settings; import envoy.schema.Message; import envoy.schema.Sync; import envoy.schema.User; @@ -47,19 +47,30 @@ public class ChatWindow extends JFrame { private static final long serialVersionUID = 6865098428255463649L; - private JPanel contentPane = new JPanel(); + // user specific objects + private Client client; + private LocalDB localDB; + private Settings settings; + // used colors in Envoy + private UIColors uiColors = UIColors.getInstance(true); + // GUI components + private JPanel contentPane = new JPanel(); + private JTextArea messageEnterTextArea = new JTextArea(); + private JList userList = new JList<>(); + private Chat currentChat; + private JList messageList = new JList<>(); + private JScrollPane scrollPane = new JScrollPane(); + private JTextPane textPane = new JTextPane(); + // private JCheckBox jCbChangeMode; + private JButton postButton = new JButton(); + private JButton settingsButton = new JButton(); - private Client client; - private LocalDB localDB; + private static int space = 4; - private JList userList = new JList<>(); - private Chat currentChat; - - private JTextArea messageEnterTextArea; - - public ChatWindow(Client client, LocalDB localDB) { + public ChatWindow(Client client, LocalDB localDB, Settings setting) { this.client = client; this.localDB = localDB; + this.settings = setting; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); @@ -73,9 +84,7 @@ public class ChatWindow extends JFrame { public void windowClosing(WindowEvent e) { localDB.saveToLocalDB(); } }); - contentPane.setBackground(new Color(0, 0, 0)); - contentPane.setForeground(Color.white); - contentPane.setBorder(new EmptyBorder(0, 5, 0, 0)); + contentPane.setBorder(new EmptyBorder(space, space, space, space)); setContentPane(contentPane); GridBagLayout gbl_contentPane = new GridBagLayout(); gbl_contentPane.columnWidths = new int[] { 1, 1, 1 }; @@ -84,25 +93,16 @@ public class ChatWindow extends JFrame { gbl_contentPane.rowWeights = new double[] { 0.05, 1.0, 0.07 }; contentPane.setLayout(gbl_contentPane); - JList messageList = new JList<>(); messageList.setCellRenderer(new MessageListRenderer()); - messageList.setFocusTraversalKeysEnabled(false); - messageList.setSelectionForeground(new Color(255, 255, 255)); - messageList.setSelectionBackground(new Color(102, 0, 153)); messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); - messageList.setForeground(new Color(255, 255, 255)); - messageList.setBackground(new Color(51, 51, 51)); DefaultListModel messageListModel = new DefaultListModel<>(); messageList.setModel(messageListModel); messageList.setFont(new Font("Arial", Font.PLAIN, 17)); messageList.setFixedCellHeight(60); - messageList.setBorder(new EmptyBorder(5, 5, 5, 5)); + messageList.setBorder(new EmptyBorder(space, space, space, space)); - JScrollPane scrollPane = new JScrollPane(); - scrollPane.setForeground(new Color(0, 0, 0)); - scrollPane.setBackground(new Color(51, 51, 51)); scrollPane.setViewportView(messageList); scrollPane.setBorder(null); @@ -112,18 +112,17 @@ public class ChatWindow extends JFrame { gbc_scrollPane.gridx = 1; gbc_scrollPane.gridy = 1; - gbc_scrollPane.insets = new Insets(0, 10, 10, 10); + gbc_scrollPane.insets = new Insets(space, space, space, space); contentPane.add(scrollPane, gbc_scrollPane); // Message enter field - messageEnterTextArea = new JTextArea(); messageEnterTextArea.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { - if (e.getKeyCode() == KeyEvent.VK_ENTER && ((SettingsScreen.enterToSend && e.getModifiersEx() == 0) + if (e.getKeyCode() == KeyEvent.VK_ENTER && ((settings.isEnterToSend() && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { postMessage(messageList); @@ -133,44 +132,34 @@ public class ChatWindow extends JFrame { }); // Checks for changed Message messageEnterTextArea.setWrapStyleWord(true); - messageEnterTextArea.setCaretColor(new Color(255, 255, 255)); - messageEnterTextArea.setForeground(new Color(255, 255, 255)); - messageEnterTextArea.setBackground(new Color(51, 51, 51)); messageEnterTextArea.setLineWrap(true); messageEnterTextArea.setBorder(null); messageEnterTextArea.setFont(new Font("Arial", Font.PLAIN, 17)); - messageEnterTextArea.setBorder(new EmptyBorder(5, 5, 5, 5)); + messageEnterTextArea.setBorder(new EmptyBorder(space, space, space, space)); GridBagConstraints gbc_messageEnterTextfield = new GridBagConstraints(); gbc_messageEnterTextfield.fill = GridBagConstraints.BOTH; gbc_messageEnterTextfield.gridx = 1; gbc_messageEnterTextfield.gridy = 2; - gbc_messageEnterTextfield.insets = new Insets(10, 10, 10, 10); + gbc_messageEnterTextfield.insets = new Insets(space, space, space, space); contentPane.add(messageEnterTextArea, gbc_messageEnterTextfield); // Post Button - JButton postButton = new JButton("Post"); - postButton.setForeground(new Color(255, 255, 255)); - postButton.setBackground(new Color(102, 51, 153)); postButton.setBorderPainted(false); - GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints(); gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH; gbc_moveSelectionPostButton.gridx = 2; gbc_moveSelectionPostButton.gridy = 2; - gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10); + gbc_moveSelectionPostButton.insets = new Insets(space, space, space, space); postButton.addActionListener((evt) -> { postMessage(messageList); }); contentPane.add(postButton, gbc_moveSelectionPostButton); // Settings Button - JButton settingsButton = new JButton("Settings"); - settingsButton.setForeground(new Color(255, 255, 255)); - settingsButton.setBackground(new Color(102, 51, 153)); settingsButton.setBorderPainted(false); GridBagConstraints gbc_moveSelectionSettingsButton = new GridBagConstraints(); @@ -179,13 +168,12 @@ public class ChatWindow extends JFrame { gbc_moveSelectionSettingsButton.gridx = 2; gbc_moveSelectionSettingsButton.gridy = 0; - gbc_moveSelectionSettingsButton.insets = new Insets(10, 10, 10, 10); + gbc_moveSelectionSettingsButton.insets = new Insets(space, space, space, space); settingsButton.addActionListener((evt) -> { try { - SettingsScreen.open(localDB.getUser().getName()); - } catch (Exception e) { SettingsScreen.open(); + } catch (Exception e) { System.err.println("An error occured while opening the settings screen: " + e); e.printStackTrace(); } @@ -193,10 +181,6 @@ public class ChatWindow extends JFrame { contentPane.add(settingsButton, gbc_moveSelectionSettingsButton); // Partner name display - JTextPane textPane = new JTextPane(); - textPane.setBackground(new Color(0, 0, 0)); - textPane.setForeground(new Color(255, 255, 255)); - textPane.setFont(new Font("Arial", Font.PLAIN, 20)); GridBagConstraints gbc_partnerName = new GridBagConstraints(); @@ -204,7 +188,7 @@ public class ChatWindow extends JFrame { gbc_partnerName.gridx = 1; gbc_partnerName.gridy = 0; - gbc_partnerName.insets = new Insets(0, 10, 0, 10); + gbc_partnerName.insets = new Insets(space, space, space, space); contentPane.add(textPane, gbc_partnerName); userList.setCellRenderer(new UserListRenderer()); @@ -234,20 +218,18 @@ public class ChatWindow extends JFrame { } }); - userList.setSelectionForeground(new Color(255, 255, 255)); - userList.setSelectionBackground(new Color(102, 0, 153)); - userList.setForeground(new Color(255, 255, 255)); - userList.setBackground(new Color(51, 51, 51)); userList.setFont(new Font("Arial", Font.PLAIN, 17)); - userList.setBorder(new EmptyBorder(5, 5, 5, 5)); + userList.setBorder(new EmptyBorder(space, space, space, space)); GridBagConstraints gbc_userList = new GridBagConstraints(); gbc_userList.fill = GridBagConstraints.VERTICAL; gbc_userList.gridx = 0; gbc_userList.gridy = 1; gbc_userList.anchor = GridBagConstraints.PAGE_START; - gbc_userList.insets = new Insets(0, 0, 10, 0); + gbc_userList.insets = new Insets(space, space, space, space); + changeChatWindowColors(); + contentPane.add(userList, gbc_userList); contentPane.revalidate(); @@ -257,6 +239,46 @@ public class ChatWindow extends JFrame { contentPane.revalidate(); } + /** + * Used to immediately reload the ChatWindow when settings were changed. + * + * @since Envoy v0.1-alpha + */ + public void changeChatWindowColors() { + uiColors.setDisplayMode(settings.isDarkMode()); + + // contentPane + contentPane.setBackground(uiColors.getBackgroundColor()); + contentPane.setForeground(uiColors.getTextColor()); + // messageList + messageList.setSelectionForeground(uiColors.getTextColor()); + messageList.setSelectionBackground(uiColors.getSpecialUseColor()); + messageList.setForeground(uiColors.getTextColor()); + messageList.setBackground(uiColors.getUserInteractionColor()); + // scrollPane + scrollPane.setForeground(uiColors.getBackgroundColor()); + scrollPane.setBackground(uiColors.getUserInteractionColor()); + // messageEnterTextArea + messageEnterTextArea.setCaretColor(uiColors.getTextColor()); + messageEnterTextArea.setForeground(uiColors.getTextColor()); + messageEnterTextArea.setBackground(uiColors.getUserInteractionColor()); + // postButton + postButton.setForeground(uiColors.getTextColor()); + postButton.setBackground(uiColors.getSpecialUseColor()); + // settingsButton + settingsButton.setForeground(uiColors.getTextColor()); + settingsButton.setBackground(uiColors.getSpecialUseColor()); + // textPane + textPane.setBackground(uiColors.getBackgroundColor()); + textPane.setForeground(uiColors.getTextColor()); + // userList + userList.setSelectionForeground(uiColors.getTextColor()); + userList.setSelectionBackground(uiColors.getSpecialUseColor()); + userList.setForeground(uiColors.getTextColor()); + userList.setBackground(uiColors.getUserInteractionColor()); + + } + private void postMessage(JList messageList) { if (!client.hasRecipient()) { JOptionPane.showMessageDialog(this, @@ -268,7 +290,7 @@ public class ChatWindow extends JFrame { if (!messageEnterTextArea.getText().isEmpty()) try { // Create and send message object - final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient()); + final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient().getID()); localDB.addWaitingMessageToLocalDB(message, currentChat); messageList.setModel(currentChat.getModel()); diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index 7d5de28..4198648 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -1,15 +1,22 @@ package envoy.client.ui; import java.awt.BorderLayout; -import java.awt.Color; import java.awt.FlowLayout; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; import javax.swing.JButton; import javax.swing.JDialog; +import javax.swing.JOptionPane;//TODO: temporary import javax.swing.JPanel; import javax.swing.border.EmptyBorder; +import envoy.client.Settings; + /** + * This class provides the GUI to change the user specific settings. + * * Project: envoy-client
* File: SettingsScreen.java
* Created: 31 Oct 2019
@@ -18,139 +25,109 @@ import javax.swing.border.EmptyBorder; */ public class SettingsScreen extends JDialog { - private static final long serialVersionUID = -4476913491263077107L; - private final JPanel contentPanel = new JPanel(); - public static boolean enterToSend = true; + private static final long serialVersionUID = -4476913491263077107L; + private final JPanel contentPanel = new JPanel(); + private JPanel buttonPane = new JPanel(); + private JButton okButton = new JButton("Save"); + private JButton cancelButton = new JButton("Cancel"); + private static Settings settings; + private static UIColors uiColors; + private static int space = 5; + 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) + /** - * Open the Settings screen. - * Only suited for Dev/Error mode. - * Avoid usage. + * Opens the settings screen.
* * @since Envoy v0.1-alpha */ - public static void open() { open(new SettingsScreen()); } + public static void open() { - /** - * Opens the Settings screen.
- * Use preferably since everyone is already initialised.
- * It personalises the screen more. - * - * @param username The name of the User - * @param Email The Email that is associated with that Account - * @since Envoy v0.1-alpha - */ - public static void open(String username) {// , String Email) {AUSKLAMMERN, WENN ANMELDUNG PER - // EMAIL IMPLEMENTIERT IST! - open(new SettingsScreen(username)); - } - - public static void open(SettingsScreen dialog) { - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - dialog.setModal(true); - dialog.setVisible(true); + settings = Settings.getInstance(); + uiColors.setDisplayMode(settings.isDarkMode()); + settingsScreen = new SettingsScreen(); + settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + settingsScreen.setModal(true); + settingsScreen.setVisible(true); } /** - * Builds the Settings screen.
- * Use only as Dev/Error Mode.
- * Avoid usage. + * Builds the settings screen.
* * @since Envoy v0.1-alpha */ - public SettingsScreen() { - setBackground(Color.BLACK); + private SettingsScreen() { + setBounds(100, 100, 450, 300); getContentPane().setLayout(new BorderLayout()); - contentPanel.setBackground(Color.BLACK); - contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); - getContentPane().add(contentPanel, BorderLayout.CENTER); - contentPanel.setLayout(new BorderLayout(0, 0)); - { - JPanel buttonPane = new JPanel(); - buttonPane.setBackground(Color.BLACK); - getContentPane().add(buttonPane, BorderLayout.SOUTH); - buttonPane.setLayout(new BorderLayout(0, 0)); - { - JButton okButton = new JButton("Save"); - okButton.setActionCommand("OK"); - buttonPane.add(okButton, BorderLayout.EAST); - getRootPane().setDefaultButton(okButton); - okButton.addActionListener((evt) -> { - // Hier später die Daten abspeichern, wenn Datenmodell implementiert ist - dispose(); - }); - } - { - JButton cancelButton = new JButton("Cancel"); - cancelButton.setActionCommand("Cancel"); - buttonPane.add(cancelButton, BorderLayout.WEST); - - cancelButton.addActionListener((evt) -> { dispose(); }); - } - } - } - - /** - * Builds the Settings screen.
- * Use preferreably since everyone is already initialised.
- * It personalises the screen more. - * - * @param Username The name of the User - * @param Email The Email that is associated with that Account - * @since Envoy v0.1-alpha - */ - public SettingsScreen(String Username) {// , String Email, String hashedPwd) {AUSKLAMMERN, WENN ANMELDUNG PER EMAIL - // IMPLEMENTIERT IST! - setBackground(Color.BLACK); - setBounds(100, 100, 450, 300); - getContentPane().setLayout(new BorderLayout()); - contentPanel.setBackground(Color.BLACK); contentPanel.setLayout(new FlowLayout()); - contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + contentPanel.setBorder(new EmptyBorder(space, space, space, space)); getContentPane().add(contentPanel, BorderLayout.CENTER); { - JPanel buttonPane = new JPanel(); - buttonPane.setBackground(Color.BLACK); getContentPane().add(buttonPane, BorderLayout.SOUTH); - buttonPane.setLayout(new BorderLayout(0, 0)); + GridBagLayout gbl_buttonPane = new GridBagLayout(); + gbl_buttonPane.columnWidths = new int[] { 100, 250, 100, 0 }; + gbl_buttonPane.rowHeights = new int[] { 25, 0 }; + gbl_buttonPane.columnWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE }; + gbl_buttonPane.rowWeights = new double[] { 0.0, Double.MIN_VALUE }; + buttonPane.setLayout(gbl_buttonPane); { - JButton okButton = new JButton("Save"); - okButton.setActionCommand("OK"); - buttonPane.add(okButton, BorderLayout.EAST); - getRootPane().setDefaultButton(okButton); - okButton.addActionListener((evt) -> { - // Hier später die Daten abspeichern, wenn Datenmodell implementiert ist - dispose(); - }); - } - { - JButton cancelButton = new JButton("Cancel"); cancelButton.setActionCommand("Cancel"); - buttonPane.add(cancelButton, BorderLayout.WEST); + GridBagConstraints gbc_cancelButton = new GridBagConstraints(); + gbc_cancelButton.anchor = GridBagConstraints.NORTHWEST; + gbc_cancelButton.insets = new Insets(space, space, space, space); + gbc_cancelButton.gridx = 0; + gbc_cancelButton.gridy = 0; + buttonPane.add(cancelButton, gbc_cancelButton); cancelButton.addActionListener((evt) -> { dispose(); }); } + { + okButton.setActionCommand("OK"); + GridBagConstraints gbc_okButton = new GridBagConstraints(); + gbc_okButton.anchor = GridBagConstraints.NORTHEAST; + gbc_okButton.fill = GridBagConstraints.EAST; + gbc_okButton.insets = new Insets(space, space, space, space); + gbc_okButton.gridx = 2; + gbc_okButton.gridy = 0; + buttonPane.add(okButton, gbc_okButton); + getRootPane().setDefaultButton(okButton); + okButton.addActionListener((evt) -> { + try { + settings.setUsername(settings.getUsername());// still temporary value + settings.setEmail(settings.getEmail());// still temporary value + settings.setDarkMode(false);// TODO temporary values while no UI is implemented + settings.setEnterToSend(settings.isEnterToSend());// still temporary value + settings.save(); + } catch (Exception e) { + System.err.println("Something went wrong when changing the setting"); + settingsScreen.dispose(); + } + JOptionPane.showConfirmDialog(settingsScreen, "Successfully changed settings"); + }); + } } + changeSettingsScreenColors(); } - /** - * @return true if Enter should be used to send a message instead of ctrl+enter - * @since Envoy v0.1-alpha - */ - public static boolean isEnterToSend() { return enterToSend; } + private void changeSettingsScreenColors() { + // whole JDialog + setBackground(uiColors.getBackgroundColor()); + // contentPanel + contentPanel.setBackground(uiColors.getBackgroundColor()); + // buttonPane + buttonPane.setBackground(uiColors.getBackgroundColor()); + // cancelButton + cancelButton.setBackground(uiColors.getSpecialUseColor()); + cancelButton.setForeground(uiColors.getTextColor()); + // okButton + okButton.setBackground(uiColors.getSpecialUseColor()); + okButton.setForeground(uiColors.getTextColor()); + + } - /** - * @param enterToSend
- * toggles whether a message should be sent via - *
- * buttonpress "enter" or "ctrl"+"enter" - * @since Envoy v0.1-alpha - */ - public static void setEnterToSend(boolean enterForSend) { enterToSend = enterForSend; } - // TODO: Should be changed to private, but later to avoid warnings } diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index c3a3baa..a456d6e 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -9,6 +9,7 @@ import javax.swing.JOptionPane; import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; +import envoy.client.Settings; import envoy.exception.EnvoyException; /** @@ -60,10 +61,11 @@ public class Startup { "Local DB error", JOptionPane.WARNING_MESSAGE); } + Settings settings = Settings.getInstance();//TODO delete line EventQueue.invokeLater(() -> { try { - ChatWindow frame = new ChatWindow(client, localDB); + ChatWindow frame = new ChatWindow(client, localDB, settings); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/envoy/client/ui/UIColors.java b/src/main/java/envoy/client/ui/UIColors.java new file mode 100644 index 0000000..2959fa5 --- /dev/null +++ b/src/main/java/envoy/client/ui/UIColors.java @@ -0,0 +1,158 @@ +package envoy.client.ui; + +import java.awt.Color; + +/** + * This class stores the colors that are used in Envoy. + *
+ *
+ * Project: envoy-client
+ * File: EnvoyColors.java
+ * Created: 16 Nov 2019
+ * + * @author Leon Hofmeister + * @since Envoy v0.2-alpha + */ +public class UIColors { + + private UIColors() {} + + /** + * This color is used for the general background where no other elements + * overlap. + * + * @since Envoy v0.2-alpha + */ + private Color backgroundColor; + + /** + * This color is used as background for all areas where a user can interact with + * Envoy. + * (i.e. a JTextArea or JList) + * + * @since Envoy v0.2-alpha + */ + + private Color userInteractionColor; + + /** + * This color is used for any areas that need special attention. + * (i.e. highlighting a selected list column or a button background) + * + * @since Envoy v0.2-alpha + */ + private Color specialUseColor; + + /** + * This color is the color in which text will be displayed. + * + * @since Envoy v0.2-alpha + */ + private Color textColor; + + /** + * This object ensures that only one {@link UIColors} object exists. + * + * @since Envoy v0.2-alpha + */ + private static UIColors uIColors; + + /** + * This method is used to ensure that there is only one instance of EnvoyColors. + * + * @param darkMode default value how envoyColors should be displayed + * @return the instance of EnvoyColors + * @since Envoy v0.2-alpha + */ + public static UIColors getInstance(boolean darkMode) { + if (uIColors == null) { uIColors = new UIColors(); } + uIColors.setDisplayMode(darkMode); + return uIColors; + } + + /** + * Used to change the appearance of Envoy. + * + * @param darkMode if true, Envoy will be displayed in dark mode else it will + * use bright mode + * @since Envoy v0.2-alpha + */ + public void setDisplayMode(boolean darkMode) { + if (darkMode) { + uIColors.setBackgroundColor(Color.black); // TODO: other color suggestions? + uIColors.setUserInteractionColor(Color.darkGray); // temporary + uIColors.setSpecialUseColor(Color.blue); // temporary + uIColors.setTextColor(Color.white); // temporary + + } else { + uIColors.setBackgroundColor(Color.white); // temporary + uIColors.setUserInteractionColor(Color.lightGray); // temporary + uIColors.setSpecialUseColor(Color.green); // temporary + uIColors.setTextColor(Color.black); // temporary + + } + } + + /** + * @return the {@link UIColors#backgroundColor} + * @since Envoy v0.2-alpha + */ + public Color getBackgroundColor() { return backgroundColor; } + + /** + * @param backgroundColor the new {@link UIColors#backgroundColor} + * @since Envoy v0.2-alpha + */ + public void setBackgroundColor(Color backgroundColor) { this.backgroundColor = backgroundColor; } + + /** + * @return the {@link UIColors#userInteractionColor} + * @since Envoy v0.2-alpha + */ + public Color getUserInteractionColor() { return userInteractionColor; } + + /** + * @param userInteractionColor the new {@link UIColors#userInteractionColor} + * @since Envoy v0.2-alpha + */ + public void setUserInteractionColor(Color userInteractionColor) { + this.userInteractionColor = userInteractionColor; + } + + /** + * @return the {@link UIColors#specialUseColor} + * @since Envoy v0.2-alpha + */ + public Color getSpecialUseColor() { return specialUseColor; } + + /** + * @param specialUseColor the new {@link UIColors#specialUseColor} + * @since Envoy v0.2-alpha + */ + public void setSpecialUseColor(Color specialUseColor) { this.specialUseColor = specialUseColor; } + + /** + * @return the {@link UIColors#textColor} + * @since Envoy v0.2-alpha + */ + public Color getTextColor() { return textColor; } + + /** + * @param textColor the new {@link UIColors#textColor} + * @since Envoy v0.2-alpha + */ + public void setTextColor(Color textColor) { this.textColor = textColor; } + + /** + * @return the {@link UIColors#uIColors} + * @since Envoy v0.2-alpha + */ + public static UIColors getEnvoyColors() { return uIColors; } + + @Deprecated + /** + * @param envoyColors the new {@link EnvoyColors#envoyColors} + * @since Envoy v0.2-alpha + */ + public static void setEnvoyColors(UIColors uIColors) { UIColors.uIColors = uIColors; } +} \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/UserListRenderer.java b/src/main/java/envoy/client/ui/UserListRenderer.java index 40bb2ad..a0a2b4e 100644 --- a/src/main/java/envoy/client/ui/UserListRenderer.java +++ b/src/main/java/envoy/client/ui/UserListRenderer.java @@ -56,6 +56,12 @@ public class UserListRenderer extends JLabel implements ListCellRenderer { status, name)); break; + case AFK: + break; + case DO_NOT_DISTURB: + break; + default: + break; } From 22a9fb7822fe5e49042da3fe232086e83ceafa6b Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 22 Nov 2019 22:40:00 +0100 Subject: [PATCH 2/8] improved Javadoc in some files, especially UIColors --- src/main/java/envoy/client/Client.java | 4 +- src/main/java/envoy/client/LocalDB.java | 2 +- src/main/java/envoy/client/ui/UIColors.java | 66 +++++---------------- 3 files changed, 20 insertions(+), 52 deletions(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index 522d44f..6533d47 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -129,7 +129,9 @@ public class Client { * Updating UserStatus of all users in LocalDB. (Server sends all users with * their updated UserStatus to the client.)
* - * @param userId + * @param userId the client who sends the sync + * @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 */ public Sync sendSync(long userId, Sync sync) { diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 5b702e8..38f74e4 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -270,7 +270,7 @@ public class LocalDB { /** * Adds a user to the {@code sync} {@link Sync} object. * - * @param user + * @param user the user who should be added to the {@link Sync} object * @since Envoy v0.1-alpha */ public void addUserToSync(User user) { sync.getUsers().add(user); } diff --git a/src/main/java/envoy/client/ui/UIColors.java b/src/main/java/envoy/client/ui/UIColors.java index 2959fa5..e810250 100644 --- a/src/main/java/envoy/client/ui/UIColors.java +++ b/src/main/java/envoy/client/ui/UIColors.java @@ -17,44 +17,10 @@ public class UIColors { private UIColors() {} - /** - * This color is used for the general background where no other elements - * overlap. - * - * @since Envoy v0.2-alpha - */ private Color backgroundColor; - - /** - * This color is used as background for all areas where a user can interact with - * Envoy. - * (i.e. a JTextArea or JList) - * - * @since Envoy v0.2-alpha - */ - private Color userInteractionColor; - - /** - * This color is used for any areas that need special attention. - * (i.e. highlighting a selected list column or a button background) - * - * @since Envoy v0.2-alpha - */ private Color specialUseColor; - - /** - * This color is the color in which text will be displayed. - * - * @since Envoy v0.2-alpha - */ private Color textColor; - - /** - * This object ensures that only one {@link UIColors} object exists. - * - * @since Envoy v0.2-alpha - */ private static UIColors uIColors; /** @@ -94,25 +60,29 @@ public class UIColors { } /** - * @return the {@link UIColors#backgroundColor} + * @return the general background color where no other element overlaps * @since Envoy v0.2-alpha */ public Color getBackgroundColor() { return backgroundColor; } /** - * @param backgroundColor the new {@link UIColors#backgroundColor} + * @param backgroundColor the general background where no other element overlaps * @since Envoy v0.2-alpha */ public void setBackgroundColor(Color backgroundColor) { this.backgroundColor = backgroundColor; } /** - * @return the {@link UIColors#userInteractionColor} + * @return the userInteractionColor:
+ * This color is used as background for all areas where a user can + * interact with Envoy (i.e. a JTextArea or JList) * @since Envoy v0.2-alpha */ public Color getUserInteractionColor() { return userInteractionColor; } /** - * @param userInteractionColor the new {@link UIColors#userInteractionColor} + * @param userInteractionColor This color is used as background for all areas
+ * where a user can interact with Envoy + * (i.e. a JTextArea or JList) * @since Envoy v0.2-alpha */ public void setUserInteractionColor(Color userInteractionColor) { @@ -120,39 +90,35 @@ public class UIColors { } /** - * @return the {@link UIColors#specialUseColor} + * @return specialUseColor: This color is used for any areas that need special attention.
+ * (i.e. highlighting a selected list column or a button background) * @since Envoy v0.2-alpha */ public Color getSpecialUseColor() { return specialUseColor; } /** - * @param specialUseColor the new {@link UIColors#specialUseColor} + * @param specialUseColor This color is used for any areas that need special attention.
+ * (i.e. highlighting a selected list column or a button background) * @since Envoy v0.2-alpha */ public void setSpecialUseColor(Color specialUseColor) { this.specialUseColor = specialUseColor; } /** - * @return the {@link UIColors#textColor} + * @return textColor: The color in which text will be displayed * @since Envoy v0.2-alpha */ public Color getTextColor() { return textColor; } /** - * @param textColor the new {@link UIColors#textColor} + * @param textColor The color in which text will be displayed * @since Envoy v0.2-alpha */ public void setTextColor(Color textColor) { this.textColor = textColor; } + @Deprecated /** - * @return the {@link UIColors#uIColors} + * @return the uiColors object * @since Envoy v0.2-alpha */ public static UIColors getEnvoyColors() { return uIColors; } - - @Deprecated - /** - * @param envoyColors the new {@link EnvoyColors#envoyColors} - * @since Envoy v0.2-alpha - */ - public static void setEnvoyColors(UIColors uIColors) { UIColors.uIColors = uIColors; } } \ No newline at end of file From ddc6e27abb25f98a3876610ab31c1f247cac3046 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 23 Nov 2019 13:25:12 +0100 Subject: [PATCH 3/8] Added settings object and light theme Improvements: * settings are implemented via Preferences API * fixed "bug" that made partner name pane editable * light theme is added as new display method --- src/main/java/envoy/client/Settings.java | 11 +++++- src/main/java/envoy/client/ui/ChatWindow.java | 16 ++++---- .../java/envoy/client/ui/SettingsScreen.java | 39 ++++++++++--------- src/main/java/envoy/client/ui/Startup.java | 6 +-- src/main/java/envoy/client/ui/UIColors.java | 3 +- 5 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index 7377876..76d8ce3 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -2,6 +2,8 @@ package envoy.client; import java.util.prefs.Preferences; +import envoy.schema.User; + /** * Project: envoy-client
* File: Settings.java
@@ -37,8 +39,8 @@ public class Settings { public static Settings getInstance() { if (settings == null) { settings = new Settings(); + settings.load(); } - settings.load(); return settings; } @@ -55,6 +57,13 @@ public class Settings { prefs.putBoolean("darkMode", settings.isDarkMode()); prefs.putBoolean("enterToSend", settings.isEnterToSend()); } + + public void firstSave(User user) { + prefs.put("username", user.getName()); +// prefs.put("email", user.getEmail()); +// prefs.putBoolean("darkMode", true); +// prefs.putBoolean("enterToSend", true); + } /** * @return the username diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 7d5166d..38634ea 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -51,7 +51,6 @@ public class ChatWindow extends JFrame { // user specific objects private Client client; private LocalDB localDB; - private Settings settings; // used colors in Envoy private UIColors uiColors = UIColors.getInstance(true); // GUI components @@ -63,15 +62,14 @@ public class ChatWindow extends JFrame { private JScrollPane scrollPane = new JScrollPane(); private JTextPane textPane = new JTextPane(); // private JCheckBox jCbChangeMode; - private JButton postButton = new JButton(); - private JButton settingsButton = new JButton(); + private JButton postButton = new JButton("Post"); + private JButton settingsButton = new JButton("Settings"); private static int space = 4; - public ChatWindow(Client client, LocalDB localDB, Settings setting) { + public ChatWindow(Client client, LocalDB localDB) { this.client = client; this.localDB = localDB; - this.settings = setting; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); @@ -119,7 +117,6 @@ public class ChatWindow extends JFrame { gbc_scrollPane.gridy = 1; gbc_scrollPane.insets = new Insets(space, space, space, space); - contentPane.add(scrollPane, gbc_scrollPane); // Message enter field @@ -128,7 +125,7 @@ public class ChatWindow extends JFrame { @Override public void keyReleased(KeyEvent e) { - if (e.getKeyCode() == KeyEvent.VK_ENTER && ((settings.isEnterToSend() && e.getModifiersEx() == 0) + if (e.getKeyCode() == KeyEvent.VK_ENTER && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { postMessage(messageList); @@ -179,6 +176,7 @@ public class ChatWindow extends JFrame { settingsButton.addActionListener((evt) -> { try { SettingsScreen.open(); + changeChatWindowColors(); } catch (Exception e) { System.err.println("An error occured while opening the settings screen: " + e); e.printStackTrace(); @@ -188,6 +186,7 @@ public class ChatWindow extends JFrame { // Partner name display textPane.setFont(new Font("Arial", Font.PLAIN, 20)); + textPane.setEditable(false); GridBagConstraints gbc_partnerName = new GridBagConstraints(); gbc_partnerName.fill = GridBagConstraints.HORIZONTAL; @@ -216,7 +215,6 @@ public class ChatWindow extends JFrame { readCurrentChat(); client.setRecipient(user); - textPane.setText(currentChat.getRecipient().getName()); messageList.setModel(currentChat.getModel()); @@ -251,7 +249,7 @@ public class ChatWindow extends JFrame { * @since Envoy v0.1-alpha */ public void changeChatWindowColors() { - uiColors.setDisplayMode(settings.isDarkMode()); + uiColors.setDisplayMode(Settings.getInstance().isDarkMode()); // contentPane contentPane.setBackground(uiColors.getBackgroundColor()); diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index 4198648..56730aa 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -8,7 +8,6 @@ import java.awt.Insets; import javax.swing.JButton; import javax.swing.JDialog; -import javax.swing.JOptionPane;//TODO: temporary import javax.swing.JPanel; import javax.swing.border.EmptyBorder; @@ -30,8 +29,6 @@ public class SettingsScreen extends JDialog { private JPanel buttonPane = new JPanel(); private JButton okButton = new JButton("Save"); private JButton cancelButton = new JButton("Cancel"); - private static Settings settings; - private static UIColors uiColors; private static int space = 5; private static SettingsScreen settingsScreen; @@ -47,8 +44,7 @@ public class SettingsScreen extends JDialog { */ public static void open() { - settings = Settings.getInstance(); - uiColors.setDisplayMode(settings.isDarkMode()); + UIColors.getInstance(Settings.getInstance().isDarkMode()); settingsScreen = new SettingsScreen(); settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); settingsScreen.setModal(true); @@ -98,16 +94,23 @@ public class SettingsScreen extends JDialog { getRootPane().setDefaultButton(okButton); okButton.addActionListener((evt) -> { try { - settings.setUsername(settings.getUsername());// still temporary value - settings.setEmail(settings.getEmail());// still temporary value - settings.setDarkMode(false);// TODO temporary values while no UI is implemented - settings.setEnterToSend(settings.isEnterToSend());// still temporary value - settings.save(); + Settings.getInstance().setUsername(Settings.getInstance().getUsername());// still temporary + // value + Settings.getInstance().setEmail(Settings.getInstance().getEmail());// still temporary value + Settings.getInstance().setDarkMode(!Settings.getInstance().isDarkMode());// TODO temporary + // values while no + // UI is implemented + Settings.getInstance().setEnterToSend(Settings.getInstance().isEnterToSend());// still temporary + // value + Settings.getInstance().save(); + UIColors.getUIColors().setDisplayMode(Settings.getInstance().isDarkMode()); + changeSettingsScreenColors(); + revalidate(); + repaint(); } catch (Exception e) { System.err.println("Something went wrong when changing the setting"); settingsScreen.dispose(); } - JOptionPane.showConfirmDialog(settingsScreen, "Successfully changed settings"); }); } } @@ -116,17 +119,17 @@ public class SettingsScreen extends JDialog { private void changeSettingsScreenColors() { // whole JDialog - setBackground(uiColors.getBackgroundColor()); + setBackground(UIColors.getUIColors().getBackgroundColor()); // contentPanel - contentPanel.setBackground(uiColors.getBackgroundColor()); + contentPanel.setBackground(UIColors.getUIColors().getBackgroundColor()); // buttonPane - buttonPane.setBackground(uiColors.getBackgroundColor()); + buttonPane.setBackground(UIColors.getUIColors().getBackgroundColor()); // cancelButton - cancelButton.setBackground(uiColors.getSpecialUseColor()); - cancelButton.setForeground(uiColors.getTextColor()); + cancelButton.setBackground(UIColors.getUIColors().getSpecialUseColor()); + cancelButton.setForeground(UIColors.getUIColors().getTextColor()); // okButton - okButton.setBackground(uiColors.getSpecialUseColor()); - okButton.setForeground(uiColors.getTextColor()); + okButton.setBackground(UIColors.getUIColors().getSpecialUseColor()); + okButton.setForeground(UIColors.getUIColors().getTextColor()); } diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index a456d6e..2781a8a 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -61,11 +61,11 @@ public class Startup { "Local DB error", JOptionPane.WARNING_MESSAGE); } - Settings settings = Settings.getInstance();//TODO delete line - + Settings.getInstance().firstSave(client.getSender()); + EventQueue.invokeLater(() -> { try { - ChatWindow frame = new ChatWindow(client, localDB, settings); + ChatWindow frame = new ChatWindow(client, localDB); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/envoy/client/ui/UIColors.java b/src/main/java/envoy/client/ui/UIColors.java index e810250..fdad862 100644 --- a/src/main/java/envoy/client/ui/UIColors.java +++ b/src/main/java/envoy/client/ui/UIColors.java @@ -115,10 +115,9 @@ public class UIColors { */ public void setTextColor(Color textColor) { this.textColor = textColor; } - @Deprecated /** * @return the uiColors object * @since Envoy v0.2-alpha */ - public static UIColors getEnvoyColors() { return uIColors; } + public static UIColors getUIColors() { return uIColors; } } \ No newline at end of file From 860d2b0dcf4c75d23a379fdaa8e1331e5ba0e555 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 7 Dec 2019 10:58:03 +0100 Subject: [PATCH 4/8] Themes & Themes Configuration Added themes to choose from and provides customization setting for these. --- src/main/java/envoy/client/Settings.java | 76 ++- src/main/java/envoy/client/ui/ChatWindow.java | 91 ++-- .../envoy/client/ui/MessageListRenderer.java | 28 +- .../java/envoy/client/ui/SettingsScreen.java | 500 ++++++++++++++++-- src/main/java/envoy/client/ui/Theme.java | 121 +++++ src/main/java/envoy/client/ui/UIColors.java | 123 ----- .../envoy/client/ui/UserListRenderer.java | 29 +- 7 files changed, 736 insertions(+), 232 deletions(-) create mode 100644 src/main/java/envoy/client/ui/Theme.java delete mode 100644 src/main/java/envoy/client/ui/UIColors.java diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index 76d8ce3..fa6088f 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -1,7 +1,11 @@ package envoy.client; +import java.awt.Color; +import java.util.HashMap; +import java.util.Map; import java.util.prefs.Preferences; +import envoy.client.ui.Theme; import envoy.schema.User; /** @@ -10,14 +14,17 @@ import envoy.schema.User; * Created: 11 Nov 2019
* * @author Leon Hofmeister + * @author Maximilian Käfer * @since Envoy v0.2-alpha */ public class Settings { - private String username; - private String email; - private boolean enterToSend = true; - private boolean darkMode = true; + private String username; + private String email; + private boolean enterToSend = true; + private Map themes = new HashMap<>(); + + private String currentTheme; // private Image profilePic; private static Settings settings; private Preferences prefs = Preferences.userNodeForPackage(Settings.class); @@ -47,24 +54,54 @@ public class Settings { public void load() { settings.setUsername(prefs.get("username", "")); settings.setEmail(prefs.get("email", "")); - settings.setDarkMode(prefs.getBoolean("darkMode", true)); settings.setEnterToSend(prefs.getBoolean("enterToSend", true)); + // currentTheme = "dark"; Activate once if NullPointerException on currentTheme + // and change theme to dark or white in Settings + settings.setCurrentTheme(prefs.get("theme", "dark")); } public void save() { prefs.put("username", settings.getUsername()); prefs.put("email", settings.getEmail()); - prefs.putBoolean("darkMode", settings.isDarkMode()); + prefs.put("theme", currentTheme); + System.out.println(currentTheme); prefs.putBoolean("enterToSend", settings.isEnterToSend()); + // TODO: override themes map + } - + public void firstSave(User user) { + + // TODO: load themes + + settings.getThemes() + .put("dark", + new Theme("dark", Color.black, Color.darkGray, Color.white, Color.blue, Color.white, Color.orange, + Color.blue, Color.white, Color.white)); + settings.getThemes() + .put("light", + new Theme("light", new Color(235, 235, 235), Color.white, Color.white, Color.darkGray, Color.black, + Color.orange, Color.darkGray, Color.black, Color.black)); + + + + + prefs.put("username", user.getName()); -// prefs.put("email", user.getEmail()); -// prefs.putBoolean("darkMode", true); -// prefs.putBoolean("enterToSend", true); + // prefs.put("email", user.getEmail()); + // prefs.putBoolean("darkMode", true); + // prefs.putBoolean("enterToSend", true); } + public void addNewThemeToMap(Theme theme) { + settings.getThemes().put(theme.getThemeName(), theme); + currentTheme = theme.getThemeName(); + } + + public String getCurrentTheme() { return currentTheme; } + + public void setCurrentTheme(String themeName) { currentTheme = themeName; } + /** * @return the username * @since Envoy v0.2-alpha @@ -105,23 +142,9 @@ public class Settings { */ public void setEnterToSend(boolean enterToSend) { this.enterToSend = enterToSend; } - /** - * Describes whether the Envoy GUI should be displayed in dark mode or not. - * - * @return true, if dark mode display is currently set, else the light theme - * will be displayed - * @since Envoy v0.2-alpha - */ - public boolean isDarkMode() { return darkMode; } + public Map getThemes() { return themes; } - /** - * Change display mode of Envoy GUI. - * - * @param darkMode true, if dark mode display is currently set,
- * else the light theme will be displayed - * @since Envoy v0.2-alpha - */ - public void setDarkMode(boolean darkMode) { this.darkMode = darkMode; } + public void setThemes(Map themes) { this.themes = themes; } // /** // * @return the profilePic @@ -135,4 +158,5 @@ public class Settings { // */ // public void setProfilePic(Image profilePic) { this.profilePic = profilePic; } + } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 38634ea..e0ea09b 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -49,10 +49,8 @@ public class ChatWindow extends JFrame { private static final long serialVersionUID = 6865098428255463649L; // user specific objects - private Client client; - private LocalDB localDB; - // used colors in Envoy - private UIColors uiColors = UIColors.getInstance(true); + private Client client; + private LocalDB localDB; // GUI components private JPanel contentPane = new JPanel(); private JTextArea messageEnterTextArea = new JTextArea(); @@ -62,8 +60,8 @@ public class ChatWindow extends JFrame { private JScrollPane scrollPane = new JScrollPane(); private JTextPane textPane = new JTextPane(); // private JCheckBox jCbChangeMode; - private JButton postButton = new JButton("Post"); - private JButton settingsButton = new JButton("Settings"); + private JButton postButton = new JButton("Post"); + private JButton settingsButton = new JButton("Settings"); private static int space = 4; @@ -80,12 +78,14 @@ public class ChatWindow extends JFrame { addWindowListener(new WindowAdapter() { @Override - public void windowClosing(WindowEvent e) { try { - localDB.saveToLocalDB(); - } catch (IOException e1) { - e1.printStackTrace(); - System.err.println("Could nnot save localDB"); - } } + public void windowClosing(WindowEvent e) { + try { + localDB.saveToLocalDB(); + } catch (IOException e1) { + e1.printStackTrace(); + System.err.println("Could not save localDB"); + } + } }); contentPane.setBorder(new EmptyBorder(space, space, space, space)); @@ -125,8 +125,9 @@ 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))) { + if (e.getKeyCode() == KeyEvent.VK_ENTER + && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) + || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { postMessage(messageList); } @@ -176,7 +177,9 @@ public class ChatWindow extends JFrame { settingsButton.addActionListener((evt) -> { try { SettingsScreen.open(); - changeChatWindowColors(); + + changeChatWindowColors(Settings.getInstance().getCurrentTheme()); + } catch (Exception e) { System.err.println("An error occured while opening the settings screen: " + e); e.printStackTrace(); @@ -232,8 +235,8 @@ public class ChatWindow extends JFrame { gbc_userList.anchor = GridBagConstraints.PAGE_START; gbc_userList.insets = new Insets(space, space, space, space); - changeChatWindowColors(); - + changeChatWindowColors(Settings.getInstance().getCurrentTheme()); + contentPane.add(userList, gbc_userList); contentPane.revalidate(); @@ -243,43 +246,44 @@ public class ChatWindow extends JFrame { contentPane.revalidate(); } + /** * Used to immediately reload the ChatWindow when settings were changed. * * @since Envoy v0.1-alpha */ - public void changeChatWindowColors() { - uiColors.setDisplayMode(Settings.getInstance().isDarkMode()); + public void changeChatWindowColors(String key) { + Theme theme = Settings.getInstance().getThemes().get(key); // contentPane - contentPane.setBackground(uiColors.getBackgroundColor()); - contentPane.setForeground(uiColors.getTextColor()); + contentPane.setBackground(theme.getBackgroundColor()); + contentPane.setForeground(theme.getUserNameColor()); // messageList - messageList.setSelectionForeground(uiColors.getTextColor()); - messageList.setSelectionBackground(uiColors.getSpecialUseColor()); - messageList.setForeground(uiColors.getTextColor()); - messageList.setBackground(uiColors.getUserInteractionColor()); + messageList.setSelectionForeground(theme.getUserNameColor()); + messageList.setSelectionBackground(theme.getSelectionColor()); + messageList.setForeground(theme.getMessageColorChat()); + messageList.setBackground(theme.getCellColor()); // scrollPane - scrollPane.setForeground(uiColors.getBackgroundColor()); - scrollPane.setBackground(uiColors.getUserInteractionColor()); + scrollPane.setForeground(theme.getBackgroundColor()); + scrollPane.setBackground(theme.getCellColor()); // messageEnterTextArea - messageEnterTextArea.setCaretColor(uiColors.getTextColor()); - messageEnterTextArea.setForeground(uiColors.getTextColor()); - messageEnterTextArea.setBackground(uiColors.getUserInteractionColor()); + messageEnterTextArea.setCaretColor(theme.getTypingMessageColor()); + messageEnterTextArea.setForeground(theme.getTypingMessageColor()); + messageEnterTextArea.setBackground(theme.getCellColor()); // postButton - postButton.setForeground(uiColors.getTextColor()); - postButton.setBackground(uiColors.getSpecialUseColor()); + postButton.setForeground(theme.getInteractableForegroundColor()); + postButton.setBackground(theme.getInteractableBackgroundColor()); // settingsButton - settingsButton.setForeground(uiColors.getTextColor()); - settingsButton.setBackground(uiColors.getSpecialUseColor()); + settingsButton.setForeground(theme.getInteractableForegroundColor()); + settingsButton.setBackground(theme.getInteractableBackgroundColor()); // textPane - textPane.setBackground(uiColors.getBackgroundColor()); - textPane.setForeground(uiColors.getTextColor()); + textPane.setBackground(theme.getBackgroundColor()); + textPane.setForeground(theme.getUserNameColor()); // userList - userList.setSelectionForeground(uiColors.getTextColor()); - userList.setSelectionBackground(uiColors.getSpecialUseColor()); - userList.setForeground(uiColors.getTextColor()); - userList.setBackground(uiColors.getUserInteractionColor()); + userList.setSelectionForeground(theme.getUserNameColor()); + userList.setSelectionBackground(theme.getSelectionColor()); + userList.setForeground(theme.getUserNameColor()); + userList.setBackground(theme.getCellColor()); } @@ -293,9 +297,10 @@ public class ChatWindow extends JFrame { if (!messageEnterTextArea.getText().isEmpty()) try { - // Create and send message object - final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient().getID()); - localDB.addWaitingMessageToLocalDB(message, currentChat); + // Create and send message object + final Message message = localDB.createMessage(messageEnterTextArea.getText(), + currentChat.getRecipient().getID()); + localDB.addWaitingMessageToLocalDB(message, currentChat); messageList.setModel(currentChat.getModel()); // Clear text field diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java index 9053c6c..ec2aaab 100644 --- a/src/main/java/envoy/client/ui/MessageListRenderer.java +++ b/src/main/java/envoy/client/ui/MessageListRenderer.java @@ -1,5 +1,6 @@ package envoy.client.ui; +import java.awt.Color; import java.awt.Component; import java.text.SimpleDateFormat; @@ -7,6 +8,7 @@ import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; +import envoy.client.Settings; import envoy.schema.Message; /** @@ -39,16 +41,38 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer

%s

%s :%s", + "

%s

%s :%s", + dateColor, date, + textColor, text, state)); + return this; } + + public String toHex(Color c) { + int r = c.getRed(); + int g = c.getGreen(); + int b = c.getBlue(); + String hex = String.format("#%02x%02x%02x", r, g, b); + return hex; + } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index 56730aa..80d4927 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -1,15 +1,29 @@ package envoy.client.ui; import java.awt.BorderLayout; -import java.awt.FlowLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; +import java.awt.LayoutManager; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.util.Arrays; +import javax.swing.BoxLayout; +import javax.swing.DefaultListModel; import javax.swing.JButton; +import javax.swing.JColorChooser; +import javax.swing.JComboBox; import javax.swing.JDialog; +import javax.swing.JList; +import javax.swing.JOptionPane; import javax.swing.JPanel; -import javax.swing.border.EmptyBorder; +import javax.swing.JTextPane; +import javax.swing.ListSelectionModel; import envoy.client.Settings; @@ -21,15 +35,36 @@ import envoy.client.Settings; * Created: 31 Oct 2019
* * @author Leon Hofmeister + * @author Maximilian Käfer */ public class SettingsScreen extends JDialog { - private static final long serialVersionUID = -4476913491263077107L; - private final JPanel contentPanel = new JPanel(); - private JPanel buttonPane = new JPanel(); - private JButton okButton = new JButton("Save"); - private JButton cancelButton = new JButton("Cancel"); - private static int space = 5; + private static final long serialVersionUID = -4476913491263077107L; + private final JPanel contentPanel = new JPanel(); + + private DefaultListModel optionsListModel = new DefaultListModel<>(); + private final JList options = new JList(); + private JPanel buttonPane = new JPanel(); + + private JPanel themeContent = new JPanel(); + private String[] themeArray = { Settings.getInstance().getThemes().get("dark").getThemeName(), + Settings.getInstance().getThemes().get("light").getThemeName() }; + private JComboBox themes = new JComboBox(themeArray); + + 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 static int space = 5; + + private boolean colorChanged = false; + + private Theme temporaryTheme; private static SettingsScreen settingsScreen; // TODO: Add a JPanel with all the Information necessary: @@ -44,7 +79,7 @@ public class SettingsScreen extends JDialog { */ public static void open() { - UIColors.getInstance(Settings.getInstance().isDarkMode()); + // UIColors.getInstance(Settings.getInstance().isDarkMode()); settingsScreen = new SettingsScreen(); settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); settingsScreen.setModal(true); @@ -58,21 +93,254 @@ public class SettingsScreen extends JDialog { */ private SettingsScreen() { - setBounds(100, 100, 450, 300); + System.out.println(Settings.getInstance().getCurrentTheme()); + + setBounds(10, 10, 450, 650); getContentPane().setLayout(new BorderLayout()); - contentPanel.setLayout(new FlowLayout()); - contentPanel.setBorder(new EmptyBorder(space, space, space, space)); - getContentPane().add(contentPanel, BorderLayout.CENTER); { - getContentPane().add(buttonPane, BorderLayout.SOUTH); + + createNewThemeButton.setEnabled(false); + + temporaryTheme = new Theme("temporaryTheme", + Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); + // ContentPane------------------------------------------------------ + GridBagLayout gbl_contentPanel = new GridBagLayout(); + + gbl_contentPanel.columnWidths = new int[] { 1, 1 }; + gbl_contentPanel.rowHeights = new int[] { 1 }; + gbl_contentPanel.columnWeights = new double[] { 0.05, 1.0 }; + gbl_contentPanel.rowWeights = new double[] { 1.0 }; + + getContentPane().add(contentPanel, BorderLayout.CENTER); + contentPanel.setLayout(gbl_contentPanel); + + options.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + options.addListSelectionListener((listSelectionEvent) -> { + if (!listSelectionEvent.getValueIsAdjusting()) { + @SuppressWarnings("unchecked") + final JList selectedOption = (JList) listSelectionEvent.getSource(); + final String option = selectedOption.getSelectedValue(); + System.out.println(option); + + switch (option) { + case "Color Themes": + setContent(themeContent, gbc_themeContent); + getContentPane().repaint(); + getContentPane().revalidate(); + break; + } + } + }); + + options.setFont(new Font("Arial", Font.PLAIN, 14)); + + Theme theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); + + options.setSelectionForeground(theme.getUserNameColor()); + options.setSelectionBackground(theme.getSelectionColor()); + options.setForeground(theme.getUserNameColor()); + options.setBackground(theme.getCellColor()); + + GridBagConstraints gbc_optionsList = new GridBagConstraints(); + gbc_optionsList.fill = GridBagConstraints.BOTH; + gbc_optionsList.gridx = 0; + gbc_optionsList.gridy = 0; + gbc_optionsList.anchor = GridBagConstraints.PAGE_START; + gbc_optionsList.insets = new Insets(space, space, space, space); + + optionsListModel.addElement("Color Themes"); + + options.setModel(optionsListModel); + + contentPanel.add(options, gbc_optionsList); + + // ThemeContent --- + + gbc_themeContent = new GridBagConstraints(); + gbc_themeContent.fill = GridBagConstraints.BOTH; + gbc_themeContent.gridx = 1; + gbc_themeContent.gridy = 0; + gbc_themeContent.anchor = GridBagConstraints.PAGE_START; + gbc_themeContent.insets = new Insets(space, space, space, space); + + GridBagLayout gbl_themeLayout = new GridBagLayout(); + + // themeContent.setSelectionForeground(theme.getUserNameColor()); + // themeContent.setSelectionBackground(theme.getSelectionColor()); + themeContent.setForeground(theme.getUserNameColor()); + themeContent.setBackground(theme.getCellColor()); + + gbl_themeLayout.columnWidths = new int[] { 1, 1 }; + gbl_themeLayout.rowHeights = new int[] { 1, 1 }; + gbl_themeLayout.columnWeights = new double[] { 1.0, 1.0 }; + gbl_themeLayout.rowWeights = new double[] { 0.01, 1.0 }; + + themeContent.setLayout(gbl_themeLayout); + + themes.setBackground(theme.getUserNameColor()); + themes.setForeground(theme.getBackgroundColor()); + themes.setSelectedItem(Settings.getInstance().getCurrentTheme()); + // themes.setBorder(null); + + themes.addItemListener(new ItemListener() { + + @Override + public void itemStateChanged(ItemEvent e) { + String selectedValue = (String) themes.getSelectedItem(); + System.out.println(selectedValue); + selectedTheme = Settings.getInstance().getThemes().get(selectedValue); + } + + }); + + GridBagConstraints gbc_themes = new GridBagConstraints(); + gbc_themes.fill = GridBagConstraints.HORIZONTAL; + gbc_themes.gridx = 0; + gbc_themes.gridy = 0; + gbc_themes.anchor = GridBagConstraints.NORTHWEST; + gbc_themes.insets = new Insets(space, space, space, space); + + themeContent.add(themes, gbc_themes); + + colorsPanel.setLayout((LayoutManager) 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.getInteractableForegroundColor(), + "Interactable Foreground", + 3); + buildCustomizeElement(new JPanel(), + + new JButton(), + new JTextPane(), + theme, + 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); + + GridBagConstraints gbc_colorsPanel = new GridBagConstraints(); + gbc_colorsPanel.fill = GridBagConstraints.HORIZONTAL; + gbc_colorsPanel.gridx = 0; + gbc_colorsPanel.gridy = 1; + gbc_colorsPanel.gridwidth = 2; + gbc_colorsPanel.anchor = GridBagConstraints.NORTHWEST; + gbc_colorsPanel.insets = new Insets(space, 0, 0, 0); + + themeContent.add(colorsPanel, gbc_colorsPanel); + + createNewThemeButton.setBackground(theme.getInteractableBackgroundColor()); + createNewThemeButton.setForeground(theme.getInteractableForegroundColor()); + colorsPanel.setBackground(theme.getCellColor()); + + createNewThemeButton.addActionListener((evt) -> { + try { + String s = JOptionPane.showInputDialog("Enter a name for the new theme"); + System.out.println(s); + Settings.getInstance() + .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())); + + createNewThemeButton.setEnabled(false); + themes.addItem(themeArray[themeArray.length - 1]); + + contentPanel.revalidate(); + contentPanel.repaint(); + + // TODO: Create new Theme + + } catch (Exception e) { + System.err.println("New theme couldn't be created! " + e); + e.printStackTrace(); + } + }); + + GridBagConstraints gbc_createNewTheme = new GridBagConstraints(); + gbc_createNewTheme.gridx = 0; + gbc_createNewTheme.gridy = 10; + + colorsPanel.add(createNewThemeButton, gbc_createNewTheme); + + // ButtonPane------------------------------------------------------- GridBagLayout gbl_buttonPane = new GridBagLayout(); gbl_buttonPane.columnWidths = new int[] { 100, 250, 100, 0 }; gbl_buttonPane.rowHeights = new int[] { 25, 0 }; gbl_buttonPane.columnWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE }; gbl_buttonPane.rowWeights = new double[] { 0.0, Double.MIN_VALUE }; + + getContentPane().add(buttonPane, BorderLayout.SOUTH); buttonPane.setLayout(gbl_buttonPane); { cancelButton.setActionCommand("Cancel"); + cancelButton.setBorderPainted(false); GridBagConstraints gbc_cancelButton = new GridBagConstraints(); gbc_cancelButton.anchor = GridBagConstraints.NORTHWEST; gbc_cancelButton.insets = new Insets(space, space, space, space); @@ -84,6 +352,7 @@ public class SettingsScreen extends JDialog { } { okButton.setActionCommand("OK"); + okButton.setBorderPainted(false); GridBagConstraints gbc_okButton = new GridBagConstraints(); gbc_okButton.anchor = GridBagConstraints.NORTHEAST; gbc_okButton.fill = GridBagConstraints.EAST; @@ -95,16 +364,19 @@ public class SettingsScreen extends JDialog { okButton.addActionListener((evt) -> { try { Settings.getInstance().setUsername(Settings.getInstance().getUsername());// still temporary - // value + Settings.getInstance().setEmail(Settings.getInstance().getEmail());// still temporary value - Settings.getInstance().setDarkMode(!Settings.getInstance().isDarkMode());// TODO temporary - // values while no - // UI is implemented + Settings.getInstance().setEnterToSend(Settings.getInstance().isEnterToSend());// still temporary - // value + + Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName()); + System.out.println(selectedTheme.getThemeName()); + + changeSettingsScreenColors(Settings.getInstance().getCurrentTheme()); + updateColorVariables(Settings.getInstance().getCurrentTheme()); + Settings.getInstance().save(); - UIColors.getUIColors().setDisplayMode(Settings.getInstance().isDarkMode()); - changeSettingsScreenColors(); + revalidate(); repaint(); } catch (Exception e) { @@ -114,23 +386,191 @@ public class SettingsScreen extends JDialog { }); } } - changeSettingsScreenColors(); + changeSettingsScreenColors(Settings.getInstance().getCurrentTheme()); } - private void changeSettingsScreenColors() { + private void changeSettingsScreenColors(String key) { + Theme theme = Settings.getInstance().getThemes().get(key); // whole JDialog - setBackground(UIColors.getUIColors().getBackgroundColor()); + setBackground(theme.getBackgroundColor()); // contentPanel - contentPanel.setBackground(UIColors.getUIColors().getBackgroundColor()); + contentPanel.setBackground(theme.getBackgroundColor()); // buttonPane - buttonPane.setBackground(UIColors.getUIColors().getBackgroundColor()); + buttonPane.setBackground(theme.getCellColor()); // cancelButton - cancelButton.setBackground(UIColors.getUIColors().getSpecialUseColor()); - cancelButton.setForeground(UIColors.getUIColors().getTextColor()); + cancelButton.setBackground(theme.getInteractableBackgroundColor()); + cancelButton.setForeground(theme.getInteractableForegroundColor()); // okButton - okButton.setBackground(UIColors.getUIColors().getSpecialUseColor()); - okButton.setForeground(UIColors.getUIColors().getTextColor()); + okButton.setBackground(theme.getInteractableBackgroundColor()); + okButton.setForeground(theme.getInteractableForegroundColor()); + // options + options.setSelectionForeground(theme.getUserNameColor()); + options.setSelectionBackground(theme.getSelectionColor()); + options.setForeground(theme.getUserNameColor()); + options.setBackground(theme.getCellColor()); + // themeContent + themeContent.setForeground(theme.getUserNameColor()); + themeContent.setBackground(theme.getCellColor()); + // themes + themes.setBackground(theme.getUserNameColor()); + themes.setForeground(theme.getBackgroundColor()); + + createNewThemeButton.setBackground(theme.getInteractableBackgroundColor()); + createNewThemeButton.setForeground(theme.getInteractableForegroundColor()); + colorsPanel.setBackground(theme.getCellColor()); } + public void updateColorVariables(String key) { + Theme theme = Settings.getInstance().getThemes().get(key); + + 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()).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()).getUserNameColor()); + + colorsPanel.removeAll(); + + 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.getInteractableForegroundColor(), + "Interactable Foreground", + 3); + buildCustomizeElement(new JPanel(), + + new JButton(), + new JTextPane(), + theme, + 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); + + GridBagConstraints gbc_createNewTheme = new GridBagConstraints(); + gbc_createNewTheme.gridx = 0; + gbc_createNewTheme.gridy = 10; + + colorsPanel.add(createNewThemeButton, gbc_createNewTheme); + } + + public void setContent(JPanel content, GridBagConstraints layout) { contentPanel.add(content, layout); } + + public 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()); + textPane.setText(name); + textPane.setEditable(false); + + button.setBackground(color); + button.setPreferredSize(new Dimension(25, 25)); + + button.addActionListener((evt) -> { + 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. + 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; + } + + } + button.setBackground(newColor); + } + + } catch (Exception e) { + System.err.println("An error occured while opening Color Chooser: " + e); + e.printStackTrace(); + } + }); + + panel.add(textPane); + panel.add(button); + panel.setBackground(theme.getCellColor()); + panel.setAlignmentX(Component.LEFT_ALIGNMENT); + + colorsPanel.add(panel); + } } diff --git a/src/main/java/envoy/client/ui/Theme.java b/src/main/java/envoy/client/ui/Theme.java new file mode 100644 index 0000000..ad8430b --- /dev/null +++ b/src/main/java/envoy/client/ui/Theme.java @@ -0,0 +1,121 @@ +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.interactableBackgroundColor, + other.interactableForegroundColor, other.messageColorChat, other.dateColorChat, other.selectionColor, + other.typingMessageColor, other.userNameColor); + } + + public String getThemeName() { return themeName; } + + public Color getInteractableForegroundColor() { return interactableForegroundColor; } + + public Color getMessageColorChat() { return messageColorChat; } + + public Color getDateColorChat() { return dateColorChat; } + + public Color getSelectionColor() { return selectionColor; } + + public Color getTypingMessageColor() { return typingMessageColor; } + + public Color getBackgroundColor() { return backgroundColor; } + + public Color getCellColor() { return cellColor; } + + public Color getInteractableBackgroundColor() { return interactableBackgroundColor; } + + public Color getUserNameColor() { return userNameColor; } + + public Color[] getAllColors() { + Color[] c = new Color[9]; + c[0] = backgroundColor; + c[1] = cellColor; + c[2] = interactableForegroundColor; + c[3] = interactableBackgroundColor; + c[4] = messageColorChat; + c[5] = dateColorChat; + c[6] = selectionColor; + c[7] = typingMessageColor; + c[8] = userNameColor; + + return c; + } + + 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/UIColors.java b/src/main/java/envoy/client/ui/UIColors.java deleted file mode 100644 index fdad862..0000000 --- a/src/main/java/envoy/client/ui/UIColors.java +++ /dev/null @@ -1,123 +0,0 @@ -package envoy.client.ui; - -import java.awt.Color; - -/** - * This class stores the colors that are used in Envoy. - *
- *
- * Project: envoy-client
- * File: EnvoyColors.java
- * Created: 16 Nov 2019
- * - * @author Leon Hofmeister - * @since Envoy v0.2-alpha - */ -public class UIColors { - - private UIColors() {} - - private Color backgroundColor; - private Color userInteractionColor; - private Color specialUseColor; - private Color textColor; - private static UIColors uIColors; - - /** - * This method is used to ensure that there is only one instance of EnvoyColors. - * - * @param darkMode default value how envoyColors should be displayed - * @return the instance of EnvoyColors - * @since Envoy v0.2-alpha - */ - public static UIColors getInstance(boolean darkMode) { - if (uIColors == null) { uIColors = new UIColors(); } - uIColors.setDisplayMode(darkMode); - return uIColors; - } - - /** - * Used to change the appearance of Envoy. - * - * @param darkMode if true, Envoy will be displayed in dark mode else it will - * use bright mode - * @since Envoy v0.2-alpha - */ - public void setDisplayMode(boolean darkMode) { - if (darkMode) { - uIColors.setBackgroundColor(Color.black); // TODO: other color suggestions? - uIColors.setUserInteractionColor(Color.darkGray); // temporary - uIColors.setSpecialUseColor(Color.blue); // temporary - uIColors.setTextColor(Color.white); // temporary - - } else { - uIColors.setBackgroundColor(Color.white); // temporary - uIColors.setUserInteractionColor(Color.lightGray); // temporary - uIColors.setSpecialUseColor(Color.green); // temporary - uIColors.setTextColor(Color.black); // temporary - - } - } - - /** - * @return the general background color where no other element overlaps - * @since Envoy v0.2-alpha - */ - public Color getBackgroundColor() { return backgroundColor; } - - /** - * @param backgroundColor the general background where no other element overlaps - * @since Envoy v0.2-alpha - */ - public void setBackgroundColor(Color backgroundColor) { this.backgroundColor = backgroundColor; } - - /** - * @return the userInteractionColor:
- * This color is used as background for all areas where a user can - * interact with Envoy (i.e. a JTextArea or JList) - * @since Envoy v0.2-alpha - */ - public Color getUserInteractionColor() { return userInteractionColor; } - - /** - * @param userInteractionColor This color is used as background for all areas
- * where a user can interact with Envoy - * (i.e. a JTextArea or JList) - * @since Envoy v0.2-alpha - */ - public void setUserInteractionColor(Color userInteractionColor) { - this.userInteractionColor = userInteractionColor; - } - - /** - * @return specialUseColor: This color is used for any areas that need special attention.
- * (i.e. highlighting a selected list column or a button background) - * @since Envoy v0.2-alpha - */ - public Color getSpecialUseColor() { return specialUseColor; } - - /** - * @param specialUseColor This color is used for any areas that need special attention.
- * (i.e. highlighting a selected list column or a button background) - * @since Envoy v0.2-alpha - */ - public void setSpecialUseColor(Color specialUseColor) { this.specialUseColor = specialUseColor; } - - /** - * @return textColor: The color in which text will be displayed - * @since Envoy v0.2-alpha - */ - public Color getTextColor() { return textColor; } - - /** - * @param textColor The color in which text will be displayed - * @since Envoy v0.2-alpha - */ - public void setTextColor(Color textColor) { this.textColor = textColor; } - - /** - * @return the uiColors object - * @since Envoy v0.2-alpha - */ - public static UIColors getUIColors() { return uIColors; } -} \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/UserListRenderer.java b/src/main/java/envoy/client/ui/UserListRenderer.java index a0a2b4e..ef40e3c 100644 --- a/src/main/java/envoy/client/ui/UserListRenderer.java +++ b/src/main/java/envoy/client/ui/UserListRenderer.java @@ -1,11 +1,13 @@ package envoy.client.ui; +import java.awt.Color; import java.awt.Component; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; +import envoy.client.Settings; import envoy.schema.User; import envoy.schema.User.UserStatus; @@ -24,6 +26,7 @@ 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) { @@ -41,31 +44,41 @@ public class UserListRenderer extends JLabel implements ListCellRenderer { final String name = value.getName(); 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()); switch (status) { case ONLINE: setText(String.format( - "

%s

%s", + "

%s

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

%s

%s", + "

%s

%s", status, + textColor, name)); break; - case AFK: - break; - case DO_NOT_DISTURB: - break; - default: - break; } return this; } + + public String toHex(Color c) { + int r = c.getRed(); + int g = c.getGreen(); + int b = c.getBlue(); + String hex = String.format("#%02x%02x%02x", r, g, b); + return hex; + } } \ No newline at end of file From 78be2e01a7f4f224cd7075c4ac2a3837f217155e Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 7 Dec 2019 11:48:41 +0100 Subject: [PATCH 5/8] Implemented theme serialization to a file --- .gitignore | 3 +- src/main/java/envoy/client/Settings.java | 130 +++++++++--------- src/main/java/envoy/client/ui/ChatWindow.java | 1 + .../java/envoy/client/ui/SettingsScreen.java | 120 ++++------------ src/main/java/envoy/client/ui/Startup.java | 3 +- 5 files changed, 93 insertions(+), 164 deletions(-) diff --git a/.gitignore b/.gitignore index e12b13a..2b99822 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target/ -/localDB/ \ No newline at end of file +/localDB/ +/themes.ser diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index fa6088f..21a1afa 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -1,12 +1,17 @@ package envoy.client; import java.awt.Color; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Map; import java.util.prefs.Preferences; import envoy.client.ui.Theme; -import envoy.schema.User; /** * Project: envoy-client
@@ -15,19 +20,32 @@ import envoy.schema.User; * * @author Leon Hofmeister * @author Maximilian Käfer + * @author Kai S. K. Engelbart * @since Envoy v0.2-alpha */ public class Settings { + // Actual settings accessible by the rest of the application private String username; private String email; private boolean enterToSend = true; - private Map themes = new HashMap<>(); + private Map themes; + private String currentTheme; - private String currentTheme; - // private Image profilePic; - private static Settings settings; - private Preferences prefs = Preferences.userNodeForPackage(Settings.class); + /** + * Required to save the settings. + */ + private Preferences prefs = Preferences.userNodeForPackage(Settings.class); + + /** + * User-defined themes are stored inside this file. + */ + private File themeFile = new File("themes.ser"); + + /** + * Singleton instance of this class. + */ + private static Settings settings = new Settings(); /** * The way to instantiate the settings. @@ -35,7 +53,7 @@ public class Settings { * * @since Envoy v0.2-alpha */ - private Settings() {} + private Settings() { load(); } /** * This method is used to ensure that there is only one instance of Settings. @@ -43,54 +61,44 @@ public class Settings { * @return the instance of Settings * @since Envoy v0.2-alpha */ - public static Settings getInstance() { - if (settings == null) { - settings = new Settings(); - settings.load(); + public static Settings getInstance() { return settings; } + + @SuppressWarnings("unchecked") + private void load() { + setUsername(prefs.get("username", "")); + setEmail(prefs.get("email", "")); + setEnterToSend(prefs.getBoolean("enterToSend", true)); + setCurrentTheme(prefs.get("theme", "dark")); + + // Load themes from theme file + try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) { + Object obj = in.readObject(); + if(obj instanceof HashMap) themes = (Map) obj; + } catch (IOException | ClassNotFoundException e) { + themes = new HashMap<>(); + e.printStackTrace(); } - return settings; + + // Load standard themes not defined in the themes file + themes.put("dark", + new Theme("dark", Color.black, Color.darkGray, Color.white, Color.blue, Color.white, Color.orange, Color.blue, Color.white, + Color.white)); + themes.put("light", + new Theme("light", new Color(235, 235, 235), Color.white, Color.white, Color.darkGray, Color.black, Color.orange, Color.darkGray, + Color.black, Color.black)); } - public void load() { - settings.setUsername(prefs.get("username", "")); - settings.setEmail(prefs.get("email", "")); - settings.setEnterToSend(prefs.getBoolean("enterToSend", true)); - // currentTheme = "dark"; Activate once if NullPointerException on currentTheme - // and change theme to dark or white in Settings - settings.setCurrentTheme(prefs.get("theme", "dark")); - } - - public void save() { - prefs.put("username", settings.getUsername()); - prefs.put("email", settings.getEmail()); + public void save() throws IOException{ + prefs.put("username", getUsername()); + prefs.put("email", getEmail()); prefs.put("theme", currentTheme); - System.out.println(currentTheme); - prefs.putBoolean("enterToSend", settings.isEnterToSend()); - // TODO: override themes map - - } - - public void firstSave(User user) { - - // TODO: load themes - - settings.getThemes() - .put("dark", - new Theme("dark", Color.black, Color.darkGray, Color.white, Color.blue, Color.white, Color.orange, - Color.blue, Color.white, Color.white)); - settings.getThemes() - .put("light", - new Theme("light", new Color(235, 235, 235), Color.white, Color.white, Color.darkGray, Color.black, - Color.orange, Color.darkGray, Color.black, Color.black)); - - - - - - prefs.put("username", user.getName()); - // prefs.put("email", user.getEmail()); - // prefs.putBoolean("darkMode", true); - // prefs.putBoolean("enterToSend", true); + prefs.putBoolean("enterToSend", isEnterToSend()); + + // Save themes to theme file + themeFile.createNewFile(); + try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) { + out.writeObject(themes); + } } public void addNewThemeToMap(Theme theme) { @@ -103,13 +111,13 @@ public class Settings { public void setCurrentTheme(String themeName) { currentTheme = themeName; } /** - * @return the username + * @return the user name * @since Envoy v0.2-alpha */ public String getUsername() { return username; } /** - * @param username the username to set + * @param username the user name to set * @since Envoy v0.2-alpha */ public void setUsername(String username) { this.username = username; } @@ -145,18 +153,4 @@ public class Settings { public Map getThemes() { return themes; } public void setThemes(Map themes) { this.themes = themes; } - - // /** - // * @return the profilePic - // * @since Envoy v0.2-alpha - // */ - // public Image getProfilePic() { return profilePic; } - // - // /** - // * @param profilePic the profilePic to set - // * @since Envoy v0.1-alpha - // */ - // public void setProfilePic(Image profilePic) { this.profilePic = profilePic; } - - -} +} \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index e0ea09b..d074711 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -81,6 +81,7 @@ public class ChatWindow extends JFrame { public void windowClosing(WindowEvent e) { try { localDB.saveToLocalDB(); + Settings.getInstance().save(); } catch (IOException e1) { e1.printStackTrace(); System.err.println("Could not save localDB"); diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index 80d4927..a003972 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -36,6 +36,7 @@ import envoy.client.Settings; * * @author Leon Hofmeister * @author Maximilian Käfer + * @author Kai S. K. Engelbart */ public class SettingsScreen extends JDialog { @@ -62,10 +63,10 @@ public class SettingsScreen extends JDialog { private JButton cancelButton = new JButton("Cancel"); private static int space = 5; - private boolean colorChanged = false; + private boolean colorChanged = false; + private Theme temporaryTheme; - private Theme temporaryTheme; - private static SettingsScreen settingsScreen; + private static SettingsScreen settingsScreen; // TODO: Add a JPanel with all the Information necessary: // change (Picture,Username, Email, Password) and toggle(light/dark mode, @@ -78,8 +79,6 @@ public class SettingsScreen extends JDialog { * @since Envoy v0.1-alpha */ public static void open() { - - // UIColors.getInstance(Settings.getInstance().isDarkMode()); settingsScreen = new SettingsScreen(); settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); settingsScreen.setModal(true); @@ -87,7 +86,7 @@ public class SettingsScreen extends JDialog { } /** - * Builds the settings screen.
+ * Builds the settings screen. * * @since Envoy v0.1-alpha */ @@ -101,9 +100,9 @@ public class SettingsScreen extends JDialog { createNewThemeButton.setEnabled(false); - temporaryTheme = new Theme("temporaryTheme", - Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); - // ContentPane------------------------------------------------------ + temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); + + // Content pane GridBagLayout gbl_contentPanel = new GridBagLayout(); gbl_contentPanel.columnWidths = new int[] { 1, 1 }; @@ -155,7 +154,7 @@ public class SettingsScreen extends JDialog { contentPanel.add(options, gbc_optionsList); - // ThemeContent --- + // Theme content gbc_themeContent = new GridBagConstraints(); gbc_themeContent.fill = GridBagConstraints.BOTH; @@ -166,8 +165,6 @@ public class SettingsScreen extends JDialog { GridBagLayout gbl_themeLayout = new GridBagLayout(); - // themeContent.setSelectionForeground(theme.getUserNameColor()); - // themeContent.setSelectionBackground(theme.getSelectionColor()); themeContent.setForeground(theme.getUserNameColor()); themeContent.setBackground(theme.getCellColor()); @@ -181,7 +178,6 @@ public class SettingsScreen extends JDialog { themes.setBackground(theme.getUserNameColor()); themes.setForeground(theme.getBackgroundColor()); themes.setSelectedItem(Settings.getInstance().getCurrentTheme()); - // themes.setBorder(null); themes.addItemListener(new ItemListener() { @@ -191,7 +187,6 @@ public class SettingsScreen extends JDialog { System.out.println(selectedValue); selectedTheme = Settings.getInstance().getThemes().get(selectedValue); } - }); GridBagConstraints gbc_themes = new GridBagConstraints(); @@ -206,24 +201,9 @@ public class SettingsScreen extends JDialog { colorsPanel.setLayout((LayoutManager) 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(), theme, @@ -231,53 +211,17 @@ public class SettingsScreen extends JDialog { "Interactable Foreground", 3); buildCustomizeElement(new JPanel(), - new JButton(), new JTextPane(), theme, 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; @@ -298,16 +242,14 @@ public class SettingsScreen extends JDialog { String s = JOptionPane.showInputDialog("Enter a name for the new theme"); System.out.println(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]); @@ -421,27 +363,18 @@ public class SettingsScreen extends JDialog { } - public void updateColorVariables(String key) { + private void updateColorVariables(String key) { Theme theme = Settings.getInstance().getThemes().get(key); 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(); @@ -526,10 +459,9 @@ public class SettingsScreen extends JDialog { colorsPanel.add(createNewThemeButton, gbc_createNewTheme); } - public void setContent(JPanel content, GridBagConstraints layout) { contentPanel.add(content, layout); } + private void setContent(JPanel content, GridBagConstraints layout) { contentPanel.add(content, layout); } - public 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()); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 2781a8a..f940191 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -21,6 +21,7 @@ import envoy.exception.EnvoyException; * * @author Leon Hofmeister * @author Maximilian Käfer + * @author Kai S. K. Engelbart * @since Envoy v0.1-alpha */ public class Startup { @@ -61,7 +62,7 @@ public class Startup { "Local DB error", JOptionPane.WARNING_MESSAGE); } - Settings.getInstance().firstSave(client.getSender()); + Settings.getInstance().setUsername(userName); EventQueue.invokeLater(() -> { try { From 233e3bcb83e791fd752ad79b260c97dc7ce3fd38 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 7 Dec 2019 13:31:08 +0100 Subject: [PATCH 6/8] Fixes themes list display bug and error when loading client with custom theme --- src/main/java/envoy/client/Settings.java | 1 + src/main/java/envoy/client/ui/SettingsScreen.java | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index 21a1afa..e84e647 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -76,6 +76,7 @@ public class Settings { if(obj instanceof HashMap) themes = (Map) obj; } catch (IOException | ClassNotFoundException e) { themes = new HashMap<>(); + currentTheme = "dark"; e.printStackTrace(); } diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index a003972..6d5c94d 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -8,7 +8,6 @@ import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; -import java.awt.LayoutManager; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Arrays; @@ -48,8 +47,7 @@ public class SettingsScreen extends JDialog { private JPanel buttonPane = new JPanel(); private JPanel themeContent = new JPanel(); - private String[] themeArray = { Settings.getInstance().getThemes().get("dark").getThemeName(), - Settings.getInstance().getThemes().get("light").getThemeName() }; + private String[] themeArray = Settings.getInstance().getThemes().keySet().toArray(new String[0]); private JComboBox themes = new JComboBox(themeArray); private GridBagConstraints gbc_themeContent = new GridBagConstraints(); @@ -198,10 +196,16 @@ public class SettingsScreen extends JDialog { themeContent.add(themes, gbc_themes); - colorsPanel.setLayout((LayoutManager) new BoxLayout(colorsPanel, BoxLayout.Y_AXIS)); + 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.getBackgroundColor(), + "Background", + 1); buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getCellColor(), "Cells", 2); buildCustomizeElement(new JPanel(), new JButton(), From c078c33d8b1a15bccc9cdb6b093b0509e7c4986b Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 7 Dec 2019 14:54:03 +0100 Subject: [PATCH 7/8] Fixed an indentation error in Javadoc --- src/main/java/envoy/client/Config.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index 8cea817..8923cd8 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -114,7 +114,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 **/ From bafadca4cbd4fca71145556ae9981bb8e97acab6 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 7 Dec 2019 17:58:59 +0100 Subject: [PATCH 8/8] Revised requested changes besides 2 (please change by yourself) --- src/main/java/envoy/client/Config.java | 2 - src/main/java/envoy/client/Settings.java | 32 +++++++ src/main/java/envoy/client/ui/ChatWindow.java | 21 ++-- .../java/envoy/client/ui/SettingsScreen.java | 95 ++++++++++++++----- src/main/java/envoy/client/ui/Theme.java | 64 ++++++++++++- .../envoy/client/ui/UserListRenderer.java | 1 - 6 files changed, 180 insertions(+), 35 deletions(-) diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index 8923cd8..bbd3b72 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -121,14 +121,12 @@ public class Config { public void setLocalDB(File localDB) { this.localDB = localDB; } /** - * * @return the current time (milliseconds) that is waited between Syncs * @since Envoy v0.1-alpha */ public int getSyncTimeout() { return syncTimeout; } /** - * * @param syncTimeout sets the time (milliseconds) during which Sync waits * @since Envoy v0.1-alpha */ diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index e84e647..91f696f 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -89,6 +89,12 @@ public class Settings { Color.black, Color.black)); } + /** + * updates prefs when save button is clicked + * + * @throws IOException + * @since Envoy v0.2-alpha + */ public void save() throws IOException{ prefs.put("username", getUsername()); prefs.put("email", getEmail()); @@ -102,13 +108,29 @@ public class Settings { } } + /** + * adds new theme to the theme map and sets current theme to the new theme. + * + * @param theme + * @since Envoy v0.2-alpha + */ public void addNewThemeToMap(Theme theme) { settings.getThemes().put(theme.getThemeName(), theme); currentTheme = theme.getThemeName(); } + /** + * @return {@link currentTheme} + * @since Envoy v0.2-alpha + */ public String getCurrentTheme() { return currentTheme; } + /** + * Sets the currentTheme + * + * @param themeName + * @since Envoy v0.2-alpha + */ public void setCurrentTheme(String themeName) { currentTheme = themeName; } /** @@ -151,7 +173,17 @@ public class Settings { */ public void setEnterToSend(boolean enterToSend) { this.enterToSend = enterToSend; } + /** + * @return {@link themes} map + * @since Envoy v0.2-alpha + */ public Map getThemes() { return themes; } + /** + * Sets {@link themes} + * + * @param themes + * @since Envoy v0.2-alpha + */ public void setThemes(Map themes) { this.themes = themes; } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index fccbe06..6867f5e 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -1,6 +1,5 @@ package envoy.client.ui; -import java.awt.Color; import java.awt.ComponentOrientation; import java.awt.Font; import java.awt.GridBagConstraints; @@ -136,6 +135,7 @@ public class ChatWindow extends JFrame { && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { postMessage(messageList); + } } }); // Checks for changed Message @@ -244,6 +244,7 @@ public class ChatWindow extends JFrame { contentPane.revalidate(); } + /** * Used to immediately reload the ChatWindow when settings were changed. @@ -282,19 +283,22 @@ public class ChatWindow extends JFrame { userList.setSelectionBackground(theme.getSelectionColor()); userList.setForeground(theme.getUserNameColor()); userList.setBackground(theme.getCellColor()); - } - + 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()); + final Message message = localDB.createMessage(messageEnterTextArea.getText(), + currentChat.getRecipient().getID()); currentChat.appendMessage(message); messageList.setModel(currentChat.getModel()); @@ -344,7 +348,8 @@ 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(); @@ -354,7 +359,8 @@ public class ChatWindow extends JFrame { readCurrentChat(); // Update UI - SwingUtilities.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); }); + SwingUtilities + .invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); }); }).start(); }).start(); } @@ -371,3 +377,4 @@ public class ChatWindow extends JFrame { */ private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } } } + diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index d2b2db3..bd80c2e 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.Logger; import javax.swing.BoxLayout; import javax.swing.DefaultListModel; @@ -24,6 +25,7 @@ import javax.swing.JPanel; import javax.swing.JTextPane; import javax.swing.ListSelectionModel; +import envoy.client.LocalDB; import envoy.client.Settings; /** @@ -64,6 +66,8 @@ public class SettingsScreen extends JDialog { private boolean colorChanged = false; private Theme temporaryTheme; + private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); + private static SettingsScreen settingsScreen; // TODO: Add a JPanel with all the Information necessary: @@ -89,8 +93,7 @@ public class SettingsScreen extends JDialog { * @since Envoy v0.1-alpha */ private SettingsScreen() { - - System.out.println(Settings.getInstance().getCurrentTheme()); + logger.info(Settings.getInstance().getCurrentTheme()); setBounds(10, 10, 450, 650); getContentPane().setLayout(new BorderLayout()); @@ -98,7 +101,8 @@ 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(); @@ -147,13 +151,10 @@ public class SettingsScreen extends JDialog { gbc_optionsList.insets = new Insets(space, space, space, space); optionsListModel.addElement("Color Themes"); - options.setModel(optionsListModel); - contentPanel.add(options, gbc_optionsList); // Theme content - gbc_themeContent = new GridBagConstraints(); gbc_themeContent.fill = GridBagConstraints.BOTH; gbc_themeContent.gridx = 1; @@ -206,7 +207,13 @@ public class SettingsScreen extends JDialog { 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.getCellColor(), + "Cells", + 2); buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), @@ -221,11 +228,41 @@ 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; @@ -246,14 +283,16 @@ public class SettingsScreen extends JDialog { String s = JOptionPane.showInputDialog("Enter a name for the new theme"); System.out.println(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]); @@ -264,7 +303,7 @@ public class SettingsScreen extends JDialog { // TODO: Create new Theme } catch (Exception e) { - System.err.println("New theme couldn't be created! " + e); + logger.info("New theme couldn't be created! " + e); e.printStackTrace(); } }); @@ -326,7 +365,7 @@ public class SettingsScreen extends JDialog { revalidate(); repaint(); } catch (Exception e) { - System.err.println("Something went wrong when changing the setting"); + logger.info("Something went wrong when changing the setting"); settingsScreen.dispose(); } }); @@ -373,12 +412,21 @@ 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(); @@ -465,7 +513,8 @@ 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()); @@ -497,7 +546,7 @@ public class SettingsScreen extends JDialog { } } catch (Exception e) { - System.err.println("An error occured while opening Color Chooser: " + e); + logger.info("An error occured while opening Color Chooser: " + e); e.printStackTrace(); } }); diff --git a/src/main/java/envoy/client/ui/Theme.java b/src/main/java/envoy/client/ui/Theme.java index ad8430b..a0ef259 100644 --- a/src/main/java/envoy/client/ui/Theme.java +++ b/src/main/java/envoy/client/ui/Theme.java @@ -4,7 +4,6 @@ import java.awt.Color; import java.io.Serializable; /** - * * Project: envoy-client
* File: Theme.java
* Created: 23 Nov 2019
@@ -50,26 +49,70 @@ public class Theme implements Serializable { 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; } + /** + * @return a color array containing all colors of this theme + * @since Envoy v0.2-alpha + */ public Color[] getAllColors() { Color[] c = new Color[9]; c[0] = backgroundColor; @@ -85,6 +128,24 @@ public class Theme implements Serializable { return c; } + /** + * 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: @@ -114,7 +175,6 @@ public class Theme implements Serializable { 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 3258afa..c730e74 100644 --- a/src/main/java/envoy/client/ui/UserListRenderer.java +++ b/src/main/java/envoy/client/ui/UserListRenderer.java @@ -45,7 +45,6 @@ public class UserListRenderer extends JLabel implements ListCellRenderer { // Getting the UserNameColor of the current theme String textColor = null; - textColor = toHex( Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor()); switch (status) {