Merge branch 'develop' into f/sync

This commit is contained in:
DieGurke 2019-11-09 17:23:15 +01:00 committed by GitHub
commit c28f3e2e26
5 changed files with 263 additions and 48 deletions

View File

@ -20,9 +20,8 @@ import envoy.schema.User;
* @author Kai S. K. Engelbart
* @author Maximilian Käfer
* @author Leon Hofmeister
* @since Envoy 0.1
* @since Envoy v0.1-alpha
*/
public class Client {
private ObjectFactory objectFactory = new ObjectFactory();
@ -45,6 +44,7 @@ public class Client {
client.close();
return responseBody;
}
/**
@ -69,7 +69,7 @@ public class Client {
}
/**
* Returns a {@link User} with a specific name by name.
* Returns a {@link User} with a specific id by name.
*
* @param name - the name of the {@link User}
* @return a {@link User} with the specified name
@ -165,16 +165,15 @@ public class Client {
/**
* Sets the recipient.
*
* @param recipient
* @param recipient - the recipient to set
* @since Envoy v0.1-alpha
*/
public void setRecipient(User recipient) { this.recipient = recipient; }
/**
* @return boolean weather the recipient is currently set.
* @return true, if a recipient is selected
* @since Envoy v0.1-alpha
*/
public boolean hasRecipient() { return recipient != null; }
}

View File

@ -30,7 +30,6 @@ import envoy.schema.User;
* @since Envoy v0.1-alpha
*/
public class LocalDB {
private File localDB;
private User sender;
private List<Chat> chats = new ArrayList<>();
@ -40,9 +39,10 @@ public class LocalDB {
/**
* Constructs an empty local database.
*
* @param sender the user that is logged in with this client
* @param client the user that is logged in with this client
* @since Envoy v0.1-alpha
**/
public LocalDB(User sender) {
this.sender = sender;
try {
@ -52,6 +52,7 @@ public class LocalDB {
}
}
/**
* Initializes the local database and fills it with values
* if the user has already sent or received messages.
@ -357,8 +358,14 @@ public class LocalDB {
/**
* @return all saves {@link Chat} objects that list the client user as the
* sender
* client
* @since Envoy v0.1-alpha
**/
public List<Chat> getChats() { return chats; }
/**
* @return the User who initialised the local Database
* @since Envoy v0.1-alpha
*/
public User getUser() { return client; }
}

View File

@ -6,6 +6,8 @@ import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
@ -39,7 +41,7 @@ import envoy.schema.User;
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @author Leon Hofmeister
* @since Envoy 0.1
* @since Envoy v0.1-alpha
*/
public class ChatWindow extends JFrame {
@ -53,6 +55,8 @@ public class ChatWindow extends JFrame {
private JList<User> userList = new JList<>();
private Chat currentChat;
private JTextArea messageEnterTextArea;
public ChatWindow(Client client, LocalDB localDB) {
this.client = client;
this.localDB = localDB;
@ -77,7 +81,7 @@ public class ChatWindow extends JFrame {
gbl_contentPane.columnWidths = new int[] { 1, 1, 1 };
gbl_contentPane.rowHeights = new int[] { 1, 1, 1 };
gbl_contentPane.columnWeights = new double[] { 0.3, 1.0, 0.1 };
gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 };
gbl_contentPane.rowWeights = new double[] { 0.05, 1.0, 0.07 };
contentPane.setLayout(gbl_contentPane);
JList<Message> messageList = new JList<>();
@ -113,23 +117,38 @@ public class ChatWindow extends JFrame {
contentPane.add(scrollPane, gbc_scrollPane);
// Message enter field
JTextArea messageEnterTextfield = new JTextArea();
messageEnterTextfield.setCaretColor(new Color(255, 255, 255));
messageEnterTextfield.setForeground(new Color(255, 255, 255));
messageEnterTextfield.setBackground(new Color(51, 51, 51));
messageEnterTextfield.setLineWrap(true);
messageEnterTextfield.setBorder(null);
messageEnterTextfield.setFont(new Font("Arial", Font.PLAIN, 17));
messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5));
messageEnterTextArea = new JTextArea();
messageEnterTextArea.addKeyListener(new KeyAdapter() {
GridBagConstraints gbc_moveSelectionMessageEnterTextfield = new GridBagConstraints();
gbc_moveSelectionMessageEnterTextfield.fill = GridBagConstraints.BOTH;
gbc_moveSelectionMessageEnterTextfield.gridx = 1;
gbc_moveSelectionMessageEnterTextfield.gridy = 2;
@Override
public void keyReleased(KeyEvent e) {
gbc_moveSelectionMessageEnterTextfield.insets = new Insets(10, 10, 10, 10);
if (e.getKeyCode() == KeyEvent.VK_ENTER && ((SettingsScreen.enterToSend && e.getModifiersEx() == 0)
|| (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) {
postMessage(client, messageList);
contentPane.add(messageEnterTextfield, gbc_moveSelectionMessageEnterTextfield);
}
}
});
// 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));
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);
contentPane.add(messageEnterTextArea, gbc_messageEnterTextfield);
// Post Button
JButton postButton = new JButton("Post");
@ -145,6 +164,7 @@ public class ChatWindow extends JFrame {
gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
postButton.addActionListener((evt) -> {
if (!client.hasRecipient()) {
JOptionPane.showMessageDialog(this,
@ -153,16 +173,23 @@ public class ChatWindow extends JFrame {
JOptionPane.INFORMATION_MESSAGE);
}
if (!messageEnterTextfield.getText().isEmpty()) try {
// Create and send message object
final Message message = localDB.createMessage(messageEnterTextfield.getText(),
final Message message = localDB.createMessage(messageEnterTextArea.getText(),
currentChat.getRecipient());
localDB.addWaitingMessageToLocalDB(message, currentChat);
messageList.setModel(currentChat.getModel());
contentPane.add(postButton, gbc_moveSelectionPostButton);
if (!messageEnterTextArea.getText().isEmpty()) try {
// Create and send message object
final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient());
localDB.addWaitingMessageToLocalDB(message, currentChat);
messageList.setModel(currentChat.getModel());
// Clear text field
messageEnterTextfield.setText("");
messageEnterTextArea.setText("");
contentPane.revalidate();
} catch (Exception e) {
JOptionPane.showMessageDialog(this,
@ -170,10 +197,34 @@ public class ChatWindow extends JFrame {
"Exception occured",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
});
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();
gbc_moveSelectionSettingsButton.fill = GridBagConstraints.BOTH;
gbc_moveSelectionSettingsButton.gridx = 2;
gbc_moveSelectionSettingsButton.gridy = 0;
gbc_moveSelectionSettingsButton.insets = new Insets(10, 10, 10, 10);
settingsButton.addActionListener((evt) -> {
try {
SettingsScreen.open(localDB.getUser().getName());
} catch (Exception e) {
SettingsScreen.open();
System.err.println("An error occured while opening the settings screen: " + e);
e.printStackTrace();
}
});
contentPane.add(settingsButton, gbc_moveSelectionSettingsButton);
// Partner name display
JTextPane textPane = new JTextPane();
@ -184,7 +235,6 @@ public class ChatWindow extends JFrame {
GridBagConstraints gbc_partnerName = new GridBagConstraints();
gbc_partnerName.fill = GridBagConstraints.HORIZONTAL;
gbc_partnerName.gridwidth = 2;
gbc_partnerName.gridx = 1;
gbc_partnerName.gridy = 0;
@ -241,9 +291,12 @@ public class ChatWindow extends JFrame {
contentPane.revalidate();
}
/**
* Initializes the elements of the user list by downloading them from the
* server.
*
* @since Envoy v0.1-alpha
*/
private void loadUsersAndChats() {
new Thread(() -> {

View File

@ -0,0 +1,156 @@
package envoy.client.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>SettingsScreen.java</strong><br>
* Created: <strong>31 Oct 2019</strong><br>
*
* @author Leon Hofmeister
*/
public class SettingsScreen extends JDialog {
private static final long serialVersionUID = -4476913491263077107L;
private final JPanel contentPanel = new JPanel();
public static boolean enterToSend = true;
// 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.
*
* @since Envoy v0.1-alpha
*/
public static void open() { open(new SettingsScreen()); }
/**
* Opens the Settings screen.<br>
* Use preferably since everyone is already initialised.<br>
* 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);
}
/**
* Builds the Settings screen.<br>
* Use only as Dev/Error Mode.<br>
* Avoid usage.
*
* @since Envoy v0.1-alpha
*/
public SettingsScreen() {
setBackground(Color.BLACK);
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.<br>
* Use preferreably since everyone is already initialised.<br>
* 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));
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);
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(); });
}
}
}
/**
* @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; }
/**
* @param enterToSend <br>
* toggles whether a message should be sent via
* <br>
* 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
}