From 92fb3d706c84f039fdc42e3e913057e36ed39231 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 22 Nov 2019 06:37:59 +0100 Subject: [PATCH] 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; }