From 8667409413a03510818c448a7db93c7bd331b64b Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 11 Feb 2020 19:35:23 +0100 Subject: [PATCH] Cleanup, fixed offline cache relay when starting in offline mode --- src/main/java/envoy/client/Settings.java | 3 -- src/main/java/envoy/client/ui/ChatWindow.java | 46 +++++++------------ .../client/ui/ContactsSearchRenderer.java | 16 ++----- src/main/java/envoy/client/ui/Startup.java | 2 +- 4 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index efb03d6..c921605 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -127,7 +127,6 @@ public class Settings { * @param themeName the name to set * @since Envoy v0.2-alpha */ - @SuppressWarnings("unchecked") public void setCurrentTheme(String themeName) { ((SettingsItem) items.get("currentTheme")).set(themeName); } /** @@ -146,7 +145,6 @@ public class Settings { * conjunction with the {@code Control} key. * @since Envoy v0.2-alpha */ - @SuppressWarnings("unchecked") public void setEnterToSend(boolean enterToSend) { ((SettingsItem) items.get("enterToSend")).set(enterToSend); } /** @@ -161,7 +159,6 @@ public class Settings { * @param currentOnCloseMode the on close mode that should be set. * @since Envoy v0.3-alpha */ - @SuppressWarnings("unchecked") public void setCurrentOnCloseMode(boolean currentOnCloseMode) { ((SettingsItem) items.get("onCloseMode")).set(currentOnCloseMode); } /** diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index a278325..17c0583 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -103,11 +103,6 @@ public class ChatWindow extends JFrame { gbl_contentPane.rowWeights = new double[] { 0.03, 0.001, 1.0, 0.005 }; contentPane.setLayout(gbl_contentPane); - // TODO: messageList.setFocusTraversalKeysEnabled(false); - // messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); - - // messageList.setFont(new Font("Arial", Font.PLAIN, 17)); - // messageList.setFixedCellHeight(60); messageList.setBorder(new EmptyBorder(space, space, space, space)); scrollPane.setViewportView(messageList); @@ -164,14 +159,7 @@ public class ChatWindow extends JFrame { gbc_moveSelectionSettingsButton.insets = insets; - settingsButton.addActionListener((evt) -> { - try { - new SettingsScreen().setVisible(true); - } catch (Exception e) { - logger.log(Level.WARNING, "An error occured while opening the settings screen", e); - e.printStackTrace(); - } - }); + settingsButton.addActionListener(evt -> new SettingsScreen().setVisible(true)); contentPane.add(settingsButton, gbc_moveSelectionSettingsButton); // Partner name display @@ -260,35 +248,35 @@ public class ChatWindow extends JFrame { searchField.getDocument().addDocumentListener(new DocumentListener() { @Override - public void removeUpdate(DocumentEvent e) { + public void removeUpdate(DocumentEvent evt) { if (client.isOnline()) { - try { - if (!searchField.getText().isEmpty()) { + if (searchField.getText().isEmpty()) { + contactsModel.clear(); + revalidate(); + repaint(); + } else { + try { client.sendEvent(new ContactSearchRequest(searchField.getText())); - } else { - contactsModel.clear(); - revalidate(); - repaint(); + } catch (IOException e) { + e.printStackTrace(); } - } catch (IOException e1) { - e1.printStackTrace(); } } } @Override - public void insertUpdate(DocumentEvent e) { + public void insertUpdate(DocumentEvent evt) { if (client.isOnline()) { try { - if (!searchField.getText().isEmpty()) { client.sendEvent(new ContactSearchRequest(searchField.getText())); } - } catch (IOException e1) { - e1.printStackTrace(); + client.sendEvent(new ContactSearchRequest(searchField.getText())); + } catch (IOException e) { + e.printStackTrace(); } } } @Override - public void changedUpdate(DocumentEvent e) {} + public void changedUpdate(DocumentEvent evt) {} }); GridBagConstraints gbc_cancelButton = new GridBagConstraints(); @@ -316,7 +304,6 @@ public class ChatWindow extends JFrame { searchPane.add(possibleContacts, gbc_possibleContacts); // Contacts Header - GridBagConstraints gbc_contactsHeader = new GridBagConstraints(); gbc_contactsHeader.fill = GridBagConstraints.BOTH; gbc_contactsHeader.gridx = 0; @@ -400,6 +387,7 @@ public class ChatWindow extends JFrame { repaint(); }); + // Listen to contact search results EventBus.getInstance().register(ContactSearchResult.class, (evt) -> { contactsModel.clear(); final java.util.List contacts = (List) evt.get(); @@ -409,8 +397,8 @@ public class ChatWindow extends JFrame { repaint(); }); + // Add new contacts to the contact list EventBus.getInstance().register(ContactOperationEvent.class, (evt) -> { - User contact = (User) evt.get(); // Clearing the search field and the searchResultList diff --git a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java index 078e289..f206c54 100644 --- a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java +++ b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java @@ -28,26 +28,20 @@ import envoy.event.EventBus; public class ContactsSearchRenderer implements ComponentListCellRenderer { @Override - public JComponent getListCellComponent(ComponentList list, User value, boolean isSelected) { + public JComponent getListCellComponent(ComponentList list, User user, boolean isSelected) { final JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); if (isSelected) { panel.setBackground(Color.DARK_GRAY); panel.setForeground(Color.RED); - } else { panel.setBackground(list.getBackground()); panel.setForeground(list.getForeground()); } - // TODO: Handle message attachments - - final String text = value.getName(); - - // Getting the UserColor in the Chat of the current theme - String textColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat().toHex(); - - JLabel display = new JLabel(String.format("

%s", textColor, text)); + JLabel display = new JLabel(String.format("

%s", + Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat().toHex(), + user.getName())); display.setAlignmentX(Component.LEFT_ALIGNMENT); display.setAlignmentY(Component.CENTER_ALIGNMENT); display.setFont(new Font("Arial", Font.PLAIN, 16)); @@ -63,7 +57,7 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { add.setForeground(list.getForeground()); add.addActionListener(evt -> { - ContactOperationEvent contactsOperationEvent = new ContactOperationEvent(value, ContactOperationEvent.Operation.ADD); + ContactOperationEvent contactsOperationEvent = new ContactOperationEvent(user, ContactOperationEvent.Operation.ADD); EventBus.getInstance().dispatch(contactsOperationEvent); EventBus.getInstance().dispatch(new SendEvent(contactsOperationEvent)); }); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index a976824..706e030 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -163,7 +163,7 @@ public class Startup { chatWindow.initContent(client, localDb, writeProxy); // Relay unread messages from cache - if (cache != null) cache.relay(); + if (cache != null && client.isOnline()) cache.relay(); try { new StatusTrayIcon(chatWindow).show();