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/Client.java b/src/main/java/envoy/client/Client.java index 4c775e1..8f96739 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -134,8 +134,8 @@ public class Client { * their updated UserStatus to the client.)
* * @param userId the id of the {@link Client} who sends the {@link Sync} - * @param sync the {@link Sync} to send - * @return a 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/Config.java b/src/main/java/envoy/client/Config.java index 116a341..bbd3b72 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; } } @@ -113,13 +114,22 @@ 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 **/ 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 d952bef..f061b28 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -44,7 +44,7 @@ public class LocalDB { private Sync sync = objectFactory.createSync(); private Sync readMessages = objectFactory.createSync(); - private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); + private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); /** * Constructs an empty local database. @@ -112,14 +112,14 @@ public class LocalDB { * Creates a {@link Message} object serializable to XML. * * @param textContent The content (text) of the message - * @param recipient The recipient of the message + * @param recipientID The recipient of the message * @return prepared {@link Message} object * @since Envoy v0.1-alpha */ - 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())); diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java new file mode 100644 index 0000000..91f696f --- /dev/null +++ b/src/main/java/envoy/client/Settings.java @@ -0,0 +1,189 @@ +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; + +/** + * Project: envoy-client
+ * File: Settings.java
+ * Created: 11 Nov 2019
+ * + * @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; + private String currentTheme; + + /** + * 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. + * Is set to private to deny other instances of that object. + * + * @since Envoy v0.2-alpha + */ + private Settings() { load(); } + + /** + * 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() { 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<>(); + currentTheme = "dark"; + e.printStackTrace(); + } + + // 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)); + } + + /** + * 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()); + prefs.put("theme", currentTheme); + prefs.putBoolean("enterToSend", isEnterToSend()); + + // Save themes to theme file + themeFile.createNewFile(); + try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) { + out.writeObject(themes); + } + } + + /** + * 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; } + + /** + * @return the user name + * @since Envoy v0.2-alpha + */ + public String getUsername() { return username; } + + /** + * @param username the user name 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; } + + /** + * @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 05da3c3..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; @@ -33,6 +32,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; @@ -51,15 +51,22 @@ 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; + // 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("Post"); + private JButton settingsButton = new JButton("Settings"); - private JList userList = new JList<>(); - private Chat currentChat; - - private JTextArea messageEnterTextArea; + private static int space = 4; private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName()); @@ -80,6 +87,7 @@ public class ChatWindow extends JFrame { public void windowClosing(WindowEvent evt) { try { localDB.saveToLocalDB(); + Settings.getInstance().save(); } catch (IOException e1) { e1.printStackTrace(); logger.log(Level.WARNING, "Unable to save the messages", e1); @@ -87,9 +95,7 @@ public class ChatWindow extends JFrame { } }); - 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 }; @@ -98,25 +104,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); @@ -126,61 +123,51 @@ 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) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) + && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) + || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { postMessage(messageList); + } } }); // 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(); @@ -189,12 +176,13 @@ 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(); + changeChatWindowColors(Settings.getInstance().getCurrentTheme()); + } catch (Exception e) { SettingsScreen.open(); logger.log(Level.WARNING, "An error occured while opening the settings screen", e); e.printStackTrace(); @@ -203,18 +191,15 @@ 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)); + textPane.setEditable(false); GridBagConstraints gbc_partnerName = new GridBagConstraints(); gbc_partnerName.fill = GridBagConstraints.HORIZONTAL; 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()); @@ -232,7 +217,6 @@ public class ChatWindow extends JFrame { readCurrentChat(); client.setRecipient(user); - textPane.setText(currentChat.getRecipient().getName()); messageList.setModel(currentChat.getModel()); @@ -240,20 +224,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(Settings.getInstance().getCurrentTheme()); + contentPane.add(userList, gbc_userList); contentPane.revalidate(); @@ -262,17 +244,61 @@ 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(String key) { + Theme theme = Settings.getInstance().getThemes().get(key); + + // contentPane + contentPane.setBackground(theme.getBackgroundColor()); + contentPane.setForeground(theme.getUserNameColor()); + // messageList + messageList.setSelectionForeground(theme.getUserNameColor()); + messageList.setSelectionBackground(theme.getSelectionColor()); + messageList.setForeground(theme.getMessageColorChat()); + messageList.setBackground(theme.getCellColor()); + // scrollPane + scrollPane.setForeground(theme.getBackgroundColor()); + scrollPane.setBackground(theme.getCellColor()); + // messageEnterTextArea + messageEnterTextArea.setCaretColor(theme.getTypingMessageColor()); + messageEnterTextArea.setForeground(theme.getTypingMessageColor()); + messageEnterTextArea.setBackground(theme.getCellColor()); + // postButton + postButton.setForeground(theme.getInteractableForegroundColor()); + postButton.setBackground(theme.getInteractableBackgroundColor()); + // settingsButton + settingsButton.setForeground(theme.getInteractableForegroundColor()); + settingsButton.setBackground(theme.getInteractableBackgroundColor()); + // textPane + textPane.setBackground(theme.getBackgroundColor()); + textPane.setForeground(theme.getUserNameColor()); + // userList + userList.setSelectionForeground(theme.getUserNameColor()); + 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()); @@ -322,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(); @@ -332,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(); } @@ -349,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/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java index 3e6eb23..2e6b65f 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; /** @@ -40,10 +42,35 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer

%s

%s :%s", date, text, state)); + // Getting the MessageColor in the Chat of the current theme + String textColor = null; + + textColor = toHex( + Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat()); + + // Getting the DateColor in the Chat of the current theme + String dateColor = null; + dateColor = toHex( + Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat()); + + setText(String.format( + "

%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 08aa9cb..bd80c2e 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -2,153 +2,561 @@ package envoy.client.ui; import java.awt.BorderLayout; import java.awt.Color; -import java.awt.FlowLayout; +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.event.ItemEvent; +import java.awt.event.ItemListener; +import java.util.Arrays; +import java.util.logging.Logger; +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.LocalDB; +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
* * @author Leon Hofmeister + * @author Maximilian Käfer + * @author Kai S. K. Engelbart */ public class SettingsScreen extends JDialog { private static final long serialVersionUID = -4476913491263077107L; private final JPanel contentPanel = new JPanel(); - public static boolean enterToSend = true; + + 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().keySet().toArray(new String[0]); + 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 final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); + + private static SettingsScreen settingsScreen; // TODO: Add a JPanel with all the Information necessary: // change (Picture,Username, Email, Password) and toggle(light/dark mode, // "ctrl+enter"/"enter" // to send a message directly) + /** - * 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()); } - - /** - * Opens the Settings screen.
- * Use preferably since everyone is already initialised.
- * It personalises the screen more. - * - * @param username The name of the User - * @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); + public static void open() { + 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); - setBounds(100, 100, 450, 300); + private SettingsScreen() { + logger.info(Settings.getInstance().getCurrentTheme()); + + setBounds(10, 10, 450, 650); 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); + + createNewThemeButton.setEnabled(false); + + 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 }; + 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); + + // Theme content + 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.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.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(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) { + logger.info("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(new BorderLayout(0, 0)); + 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); + cancelButton.setBorderPainted(false); + 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(); }); } - } - } - - /** - * Builds the Settings screen.
- * Use preferreably since everyone is already initialised.
- * It personalises the screen more. - * - * @param Username The name of the User - * @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)); - getContentPane().add(contentPanel, BorderLayout.CENTER); - { - 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); + okButton.setBorderPainted(false); + 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) -> { - // Hier später die Daten abspeichern, wenn Datenmodell implementiert ist - dispose(); + try { + Settings.getInstance().setUsername(Settings.getInstance().getUsername());// still temporary + + Settings.getInstance().setEmail(Settings.getInstance().getEmail());// still temporary value + + Settings.getInstance().setEnterToSend(Settings.getInstance().isEnterToSend());// still temporary + + Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName()); + System.out.println(selectedTheme.getThemeName()); + + changeSettingsScreenColors(Settings.getInstance().getCurrentTheme()); + updateColorVariables(Settings.getInstance().getCurrentTheme()); + + Settings.getInstance().save(); + + revalidate(); + repaint(); + } catch (Exception e) { + logger.info("Something went wrong when changing the setting"); + settingsScreen.dispose(); + } }); } - { - JButton cancelButton = new JButton("Cancel"); - cancelButton.setActionCommand("Cancel"); - buttonPane.add(cancelButton, BorderLayout.WEST); - - cancelButton.addActionListener((evt) -> { dispose(); }); - } } + changeSettingsScreenColors(Settings.getInstance().getCurrentTheme()); } - /** - * @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(String key) { + Theme theme = Settings.getInstance().getThemes().get(key); + // whole JDialog + setBackground(theme.getBackgroundColor()); + // contentPanel + contentPanel.setBackground(theme.getBackgroundColor()); + // buttonPane + buttonPane.setBackground(theme.getCellColor()); + // cancelButton + cancelButton.setBackground(theme.getInteractableBackgroundColor()); + cancelButton.setForeground(theme.getInteractableForegroundColor()); + // okButton + 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()); + + } + + 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()).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); + } + + 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) { + 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) { + logger.info("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); + } - /** - * @param enterForSend
- * 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 f672f94..25af819 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -11,6 +11,7 @@ import javax.swing.JOptionPane; import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; +import envoy.client.Settings; import envoy.exception.EnvoyException; /** @@ -22,6 +23,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 { @@ -68,7 +70,8 @@ public class Startup { "Local DB error", JOptionPane.WARNING_MESSAGE); } - + Settings.getInstance().setUsername(userName); + EventQueue.invokeLater(() -> { try { ChatWindow chatWindow = new ChatWindow(client, localDB); 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..a0ef259 --- /dev/null +++ b/src/main/java/envoy/client/ui/Theme.java @@ -0,0 +1,181 @@ +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); + } + + /** + * @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; + 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; + } + + /** + * Sets the a specific {@link Color} in this theme to a new {@link Color} + * + * @param index - index of the color
+ * 0 = backgroundColor
+ * 1 = cellColor
+ * 2 = interactableForegroundColor
+ * 3 = interactableBackgroundColor
+ * 4 = messageColorChat
+ * 5 = dateColorChat
+ * 6 = selectionColor
+ * 7 = typingMessageColor
+ * 8 = userNameColor
+ *
+ * + * @param newColor - new {@link Color} to be set + * @since Envoy 0.2-alpha + */ + public void setColor(int index, Color newColor) { + switch (index) { + case 0: + this.backgroundColor = newColor; + break; + case 1: + this.cellColor = newColor; + break; + case 2: + this.interactableForegroundColor = newColor; + break; + case 3: + this.interactableBackgroundColor = newColor; + break; + case 4: + this.messageColorChat = newColor; + break; + case 5: + this.dateColorChat = newColor; + break; + case 6: + this.selectionColor = newColor; + break; + case 7: + this.typingMessageColor = newColor; + break; + case 8: + this.userNameColor = newColor; + break; + } + } + +} diff --git a/src/main/java/envoy/client/ui/UserListRenderer.java b/src/main/java/envoy/client/ui/UserListRenderer.java index e794b1f..c730e74 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; @@ -41,16 +43,34 @@ 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", status, name)); + setText(String.format( + "

%s

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

%s

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

%s

%s", + status, + textColor, + name)); 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