Formatted

This commit is contained in:
DieGurke 2019-12-14 13:52:47 +01:00
parent 507e249512
commit 01d4c9eb13
8 changed files with 58 additions and 139 deletions

View File

@ -44,7 +44,7 @@ public class LocalDB {
private Sync sync = objectFactory.createSync(); private Sync sync = objectFactory.createSync();
private Sync readMessages = 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. * Constructs an empty local database.
@ -134,13 +134,13 @@ public class LocalDB {
return message; return message;
} }
/** /**
* Creates a {@link Sync} object filled with the changes that occurred to the * Creates a {@link Sync} object filled with the changes that occurred to the
* local database since the last synchronization. * local database since the last synchronization.
* *
* @param userId the ID of the user that is synchronized by this client * @param userId the ID of the user that is synchronized by this client
* @return {@link Sync} object filled with the current changes * @return {@link Sync} object filled with the current changes
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public Sync fillSync(long userId) { public Sync fillSync(long userId) {
addWaitingMessagesToSync(); addWaitingMessagesToSync();
@ -156,7 +156,7 @@ public class LocalDB {
* Applies the changes carried by a {@link Sync} object to the local database * Applies the changes carried by a {@link Sync} object to the local database
* *
* @param returnSync the {@link Sync} object to apply * @param returnSync the {@link Sync} object to apply
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void applySync(Sync returnSync) { public void applySync(Sync returnSync) {
for (int i = 0; i < returnSync.getMessages().size(); i++) { for (int i = 0; i < returnSync.getMessages().size(); i++) {

View File

@ -73,9 +73,9 @@ public class Settings {
// Load themes from theme file // Load themes from theme file
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) { try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) {
Object obj = in.readObject(); Object obj = in.readObject();
if(obj instanceof HashMap) themes = (Map<String, Theme>) obj; if (obj instanceof HashMap) themes = (Map<String, Theme>) obj;
} catch (IOException | ClassNotFoundException e) { } catch (IOException | ClassNotFoundException e) {
themes = new HashMap<>(); themes = new HashMap<>();
currentTheme = "dark"; currentTheme = "dark";
e.printStackTrace(); e.printStackTrace();
} }
@ -95,15 +95,15 @@ public class Settings {
* @throws IOException * @throws IOException
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public void save() throws IOException{ public void save() throws IOException {
prefs.put("username", getUsername()); prefs.put("username", getUsername());
prefs.put("email", getEmail()); prefs.put("email", getEmail());
prefs.put("theme", currentTheme); prefs.put("theme", currentTheme);
prefs.putBoolean("enterToSend", isEnterToSend()); prefs.putBoolean("enterToSend", isEnterToSend());
// Save themes to theme file // Save themes to theme file
themeFile.createNewFile(); themeFile.createNewFile();
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) { try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) {
out.writeObject(themes); out.writeObject(themes);
} }
} }

View File

@ -53,15 +53,15 @@ public class ChatWindow extends JFrame {
private Client client; private Client client;
private LocalDB localDB; private LocalDB localDB;
// GUI components // GUI components
private JPanel contentPane = new JPanel(); private JPanel contentPane = new JPanel();
private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space); private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space);
private JList<User> userList = new JList<>(); private JList<User> userList = new JList<>();
private Chat currentChat; private Chat currentChat;
private JList<Message> messageList = new JList<>(); private JList<Message> messageList = new JList<>();
private JScrollPane scrollPane = new JScrollPane(); private JScrollPane scrollPane = new JScrollPane();
private JTextPane textPane = new JTextPane(); private JTextPane textPane = new JTextPane();
// private JCheckBox jCbChangeMode; // private JCheckBox jCbChangeMode;
private PrimaryButton postButton = new PrimaryButton("Post"); private PrimaryButton postButton = new PrimaryButton("Post");
private PrimaryButton settingsButton = new PrimaryButton("Settings"); private PrimaryButton settingsButton = new PrimaryButton("Settings");
private static int space = 4; private static int space = 4;
@ -76,8 +76,7 @@ public class ChatWindow extends JFrame {
setBounds(100, 100, 600, 800); setBounds(100, 100, 600, 800);
setTitle("Envoy"); setTitle("Envoy");
setLocationRelativeTo(null); setLocationRelativeTo(null);
setIconImage( setIconImage(Toolkit.getDefaultToolkit().createImage(getClass().getClassLoader().getResource("envoy_logo.png")));
Toolkit.getDefaultToolkit().createImage(getClass().getClassLoader().getResource("envoy_logo.png")));
// Save chats when window closes // Save chats when window closes
addWindowListener(new WindowAdapter() { addWindowListener(new WindowAdapter() {
@ -131,8 +130,7 @@ public class ChatWindow extends JFrame {
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER if (e.getKeyCode() == KeyEvent.VK_ENTER
&& ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) {
|| (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) {
postMessage(messageList); postMessage(messageList);
} }
} }
@ -201,11 +199,7 @@ public class ChatWindow extends JFrame {
final User user = selectedUserList.getSelectedValue(); final User user = selectedUserList.getSelectedValue();
client.setRecipient(user); client.setRecipient(user);
currentChat = localDB.getChats() currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get();
.stream()
.filter(chat -> chat.getRecipient().getID() == user.getID())
.findFirst()
.get();
// Set all unread messages in the chat to read // Set all unread messages in the chat to read
readCurrentChat(); readCurrentChat();
@ -280,18 +274,14 @@ public class ChatWindow extends JFrame {
private void postMessage(JList<Message> messageList) { private void postMessage(JList<Message> messageList) {
if (!client.hasRecipient()) { if (!client.hasRecipient()) {
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
"Please select a recipient!",
"Cannot send message",
JOptionPane.INFORMATION_MESSAGE);
return; return;
} }
if (!messageEnterTextArea.getText().isEmpty()) try { if (!messageEnterTextArea.getText().isEmpty()) try {
// Create and send message object // Create and send message object
final Message message = localDB.createMessage(messageEnterTextArea.getText(), final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient().getID());
currentChat.getRecipient().getID());
currentChat.appendMessage(message); currentChat.appendMessage(message);
messageList.setModel(currentChat.getModel()); messageList.setModel(currentChat.getModel());
@ -341,8 +331,7 @@ public class ChatWindow extends JFrame {
new Thread(() -> { new Thread(() -> {
// Synchronize // Synchronize
localDB.applySync( localDB.applySync(client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
// Process unread messages // Process unread messages
localDB.addUnreadMessagesToLocalDB(); localDB.addUnreadMessagesToLocalDB();
@ -352,8 +341,7 @@ public class ChatWindow extends JFrame {
readCurrentChat(); readCurrentChat();
// Update UI // Update UI
SwingUtilities SwingUtilities.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
}).start(); }).start();
}).start(); }).start();
} }

View File

@ -42,22 +42,18 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer<Mess
final String text = value.getContent().get(0).getText(); final String text = value.getContent().get(0).getText();
final String state = value.getMetadata().getState().toString(); final String state = value.getMetadata().getState().toString();
final String date = value.getMetadata().getDate() == null ? "" final String date = value.getMetadata().getDate() == null ? ""
: new SimpleDateFormat("dd.MM.yyyy HH:mm ") : new SimpleDateFormat("dd.MM.yyyy HH:mm ").format(value.getMetadata().getDate().toGregorianCalendar().getTime());
.format(value.getMetadata().getDate().toGregorianCalendar().getTime());
// Getting the MessageColor in the Chat of the current theme // Getting the MessageColor in the Chat of the current theme
String textColor = null; String textColor = null;
textColor = toHex( textColor = toHex(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat());
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat());
// Getting the DateColor in the Chat of the current theme // Getting the DateColor in the Chat of the current theme
String dateColor = null; String dateColor = null;
dateColor = toHex( dateColor = toHex(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat());
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat());
setText(String.format( setText(String.format("<html><p style=\"color:%s\"><b><small>%s</b></small><br><p style=\"color:%s\">%s :%s</html>",
"<html><p style=\"color:%s\"><b><small>%s</b></small><br><p style=\"color:%s\">%s :%s</html>",
dateColor, dateColor,
date, date,
textColor, textColor,

View File

@ -16,9 +16,8 @@ import javax.swing.border.EmptyBorder;
*/ */
public class PrimaryTextArea extends JTextArea { public class PrimaryTextArea extends JTextArea {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = -5829028696155434913L;
private int arcSize;
private int arcSize;
/** /**
* Creates the text area * Creates the text area
@ -31,8 +30,8 @@ public class PrimaryTextArea extends JTextArea {
/** /**
* Creates the text area * Creates the text area
* *
* @param arcSize * @param arcSize is the diameter of the arc at the four corners.
* @param borderSpace - the insets of the border on all four sides * @param borderSpace is the insets of the border on all four sides.
* @since Envoy 0.2-alpha * @since Envoy 0.2-alpha
*/ */
public PrimaryTextArea(int arcSize, int borderSpace) { public PrimaryTextArea(int arcSize, int borderSpace) {

View File

@ -101,8 +101,7 @@ public class SettingsScreen extends JDialog {
createNewThemeButton.setEnabled(false); createNewThemeButton.setEnabled(false);
temporaryTheme = new Theme("temporaryTheme", temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
// Content pane // Content pane
GridBagLayout gbl_contentPanel = new GridBagLayout(); GridBagLayout gbl_contentPanel = new GridBagLayout();
@ -200,20 +199,8 @@ public class SettingsScreen extends JDialog {
colorsPanel.setLayout(new BoxLayout(colorsPanel, BoxLayout.Y_AXIS)); colorsPanel.setLayout(new BoxLayout(colorsPanel, BoxLayout.Y_AXIS));
colorsPanel.setAlignmentX(Component.LEFT_ALIGNMENT); colorsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
buildCustomizeElement(new JPanel(), buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getBackgroundColor(), "Background", 1);
new JButton(), buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getCellColor(), "Cells", 2);
new JTextPane(),
theme,
theme.getBackgroundColor(),
"Background",
1);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getCellColor(),
"Cells",
2);
buildCustomizeElement(new JPanel(), buildCustomizeElement(new JPanel(),
new JButton(), new JButton(),
new JTextPane(), new JTextPane(),
@ -228,41 +215,11 @@ public class SettingsScreen extends JDialog {
theme.getInteractableBackgroundColor(), theme.getInteractableBackgroundColor(),
"Interactable Background", "Interactable Background",
4); 4);
buildCustomizeElement(new JPanel(), buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getMessageColorChat(), "Messages Chat", 5);
new JButton(), buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getDateColorChat(), "Date Chat", 6);
new JTextPane(), buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getSelectionColor(), "Selection", 7);
theme, buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getTypingMessageColor(), "Typing Message", 8);
theme.getMessageColorChat(), buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getUserNameColor(), "User Names", 9);
"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(); GridBagConstraints gbc_colorsPanel = new GridBagConstraints();
gbc_colorsPanel.fill = GridBagConstraints.HORIZONTAL; gbc_colorsPanel.fill = GridBagConstraints.HORIZONTAL;
@ -283,16 +240,14 @@ public class SettingsScreen extends JDialog {
String s = JOptionPane.showInputDialog("Enter a name for the new theme"); String s = JOptionPane.showInputDialog("Enter a name for the new theme");
System.out.println(s); System.out.println(s);
Settings.getInstance() Settings.getInstance()
.addNewThemeToMap(new Theme(s, temporaryTheme.getBackgroundColor(), .addNewThemeToMap(new Theme(s, temporaryTheme.getBackgroundColor(), temporaryTheme.getCellColor(),
temporaryTheme.getCellColor(), temporaryTheme.getInteractableForegroundColor(), temporaryTheme.getInteractableForegroundColor(), temporaryTheme.getInteractableBackgroundColor(),
temporaryTheme.getInteractableBackgroundColor(), temporaryTheme.getMessageColorChat(), temporaryTheme.getMessageColorChat(), temporaryTheme.getDateColorChat(), temporaryTheme.getSelectionColor(),
temporaryTheme.getDateColorChat(), temporaryTheme.getSelectionColor(),
temporaryTheme.getTypingMessageColor(), temporaryTheme.getUserNameColor())); temporaryTheme.getTypingMessageColor(), temporaryTheme.getUserNameColor()));
themeArray = Arrays.copyOf(themeArray, themeArray.length + 1); themeArray = Arrays.copyOf(themeArray, themeArray.length + 1);
themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(s).getThemeName(); themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(s).getThemeName();
temporaryTheme = new Theme("temporaryTheme", temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
createNewThemeButton.setEnabled(false); createNewThemeButton.setEnabled(false);
themes.addItem(themeArray[themeArray.length - 1]); themes.addItem(themeArray[themeArray.length - 1]);
@ -412,21 +367,12 @@ public class SettingsScreen extends JDialog {
temporaryTheme = new Theme("temporaryTheme", temporaryTheme = new Theme("temporaryTheme",
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getBackgroundColor(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getBackgroundColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor(),
Settings.getInstance() Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableForegroundColor(),
.getThemes() Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor(),
.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()).getMessageColorChat(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getSelectionColor(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getSelectionColor(),
Settings.getInstance() Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getTypingMessageColor(),
.getThemes()
.get(Settings.getInstance().getCurrentTheme())
.getTypingMessageColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor()); Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor());
colorsPanel.removeAll(); colorsPanel.removeAll();
@ -513,8 +459,7 @@ public class SettingsScreen extends JDialog {
private void setContent(JPanel content, GridBagConstraints layout) { contentPanel.add(content, layout); } private void setContent(JPanel content, GridBagConstraints layout) { contentPanel.add(content, layout); }
private void buildCustomizeElement(JPanel panel, JButton button, JTextPane textPane, Theme theme, Color color, private void buildCustomizeElement(JPanel panel, JButton button, JTextPane textPane, Theme theme, Color color, String name, int yIndex) {
String name, int yIndex) {
textPane.setFont(new Font("Arial", Font.PLAIN, 14)); textPane.setFont(new Font("Arial", Font.PLAIN, 14));
textPane.setBackground(theme.getBackgroundColor()); textPane.setBackground(theme.getBackgroundColor());
textPane.setForeground(theme.getUserNameColor()); textPane.setForeground(theme.getUserNameColor());

View File

@ -26,9 +26,8 @@ public class Theme implements Serializable {
private Color selectionColor; private Color selectionColor;
private Color typingMessageColor; private Color typingMessageColor;
public Theme(String themeName, Color backgroundColor, Color cellColor, Color interactableForegroundColor, public Theme(String themeName, Color backgroundColor, Color cellColor, Color interactableForegroundColor, Color interactableBackgroundColor,
Color interactableBackgroundColor, Color messageColorChat, Color dateColorChat, Color selectionColor, Color messageColorChat, Color dateColorChat, Color selectionColor, Color typingMessageColor, Color userNameColor) {
Color typingMessageColor, Color userNameColor) {
this.themeName = themeName; this.themeName = themeName;
@ -42,11 +41,10 @@ public class Theme implements Serializable {
this.typingMessageColor = typingMessageColor; this.typingMessageColor = typingMessageColor;
this.userNameColor = userNameColor; this.userNameColor = userNameColor;
} }
public Theme(String name, Theme other) { public Theme(String name, Theme other) {
this(name, other.backgroundColor, other.cellColor, other.interactableBackgroundColor, this(name, other.backgroundColor, other.cellColor, other.interactableBackgroundColor, other.interactableForegroundColor,
other.interactableForegroundColor, other.messageColorChat, other.dateColorChat, other.selectionColor, other.messageColorChat, other.dateColorChat, other.selectionColor, other.typingMessageColor, other.userNameColor);
other.typingMessageColor, other.userNameColor);
} }
/** /**

View File

@ -44,23 +44,16 @@ public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
final UserStatus status = value.getStatus(); final UserStatus status = value.getStatus();
// Getting the UserNameColor of the current theme // Getting the UserNameColor of the current theme
String textColor = null; String textColor = null;
textColor = toHex( textColor = toHex(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor());
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor());
switch (status) { switch (status) {
case ONLINE: case ONLINE:
setText(String.format( setText(String
"<html><p style=\"color:#03fc20\"><b><small>%s</b></small><br><p style=\"color:%s\">%s</html>", .format("<html><p style=\"color:#03fc20\"><b><small>%s</b></small><br><p style=\"color:%s\">%s</html>", status, textColor, name));
status,
textColor,
name));
break; break;
case OFFLINE: case OFFLINE:
setText(String.format( setText(String
"<html><p style=\"color:#fc0303\"><b><small>%s</b></small><br><p style=\"color:%s\">%s</html>", .format("<html><p style=\"color:#fc0303\"><b><small>%s</b></small><br><p style=\"color:%s\">%s</html>", status, textColor, name));
status,
textColor,
name));
break; break;
} }
return this; return this;