diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java
index 8f96739..bd1555a 100644
--- a/src/main/java/envoy/client/Client.java
+++ b/src/main/java/envoy/client/Client.java
@@ -1,7 +1,5 @@
package envoy.client;
-import java.util.logging.Logger;
-
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
@@ -10,6 +8,7 @@ import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
+import envoy.client.event.EnvoyLogger;
import envoy.schema.ObjectFactory;
import envoy.schema.Sync;
import envoy.schema.User;
@@ -30,7 +29,7 @@ public class Client {
private Config config;
private User sender, recipient;
- private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
+ private static final EnvoyLogger logger = new EnvoyLogger(Client.class.getSimpleName());
public Client(Config config, String username) {
this.config = config;
@@ -63,9 +62,7 @@ public class Client {
user.setID(-1);
sendSync.getUsers().add(user);
- Sync returnSendSync = post(
- String
- .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
+ Sync returnSendSync = post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
sendSync,
Sync.class);
return returnSendSync;
@@ -85,9 +82,7 @@ public class Client {
user.setName(name);
senderSync.getUsers().add(user);
- Sync returnSenderSync = post(
- String
- .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
+ Sync returnSenderSync = post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
senderSync,
Sync.class);
@@ -134,7 +129,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 sync object (yet to be converted from java class to sync.xml)
+ * @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
*/
@@ -151,10 +147,7 @@ public class Client {
}
// Send sync
- return post(String
- .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), userId),
- sync,
- Sync.class);
+ return post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), userId), sync, Sync.class);
}
/**
diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java
index bbd3b72..e9f2a18 100644
--- a/src/main/java/envoy/client/Config.java
+++ b/src/main/java/envoy/client/Config.java
@@ -71,9 +71,7 @@ public class Config {
* @return {@code true} if server, port and localDB directory are known.
* @since Envoy v0.1-alpha
*/
- public boolean isInitialized() {
- return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null;
- }
+ public boolean isInitialized() { return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null; }
/**
* @return the host name of the Envoy server
@@ -114,7 +112,7 @@ 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
**/
diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java
index f061b28..51e2eb1 100644
--- a/src/main/java/envoy/client/LocalDB.java
+++ b/src/main/java/envoy/client/LocalDB.java
@@ -14,6 +14,7 @@ import java.util.logging.Logger;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
+import envoy.client.event.EnvoyLogger;
import envoy.client.event.EventBus;
import envoy.client.event.MessageCreationEvent;
import envoy.exception.EnvoyException;
@@ -44,7 +45,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 = new EnvoyLogger(LocalDB.class.getSimpleName());
/**
* Constructs an empty local database.
diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java
index 91f696f..fe5d769 100644
--- a/src/main/java/envoy/client/Settings.java
+++ b/src/main/java/envoy/client/Settings.java
@@ -73,9 +73,9 @@ public class Settings {
// Load themes from theme file
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) {
Object obj = in.readObject();
- if(obj instanceof HashMap) themes = (Map) obj;
+ if (obj instanceof HashMap) themes = (Map) obj;
} catch (IOException | ClassNotFoundException e) {
- themes = new HashMap<>();
+ themes = new HashMap<>();
currentTheme = "dark";
e.printStackTrace();
}
@@ -95,15 +95,15 @@ public class Settings {
* @throws IOException
* @since Envoy v0.2-alpha
*/
- public void save() throws IOException{
+ 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))) {
+ try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) {
out.writeObject(themes);
}
}
diff --git a/src/main/java/envoy/client/event/EnvoyLogger.java b/src/main/java/envoy/client/event/EnvoyLogger.java
new file mode 100644
index 0000000..33e17ac
--- /dev/null
+++ b/src/main/java/envoy/client/event/EnvoyLogger.java
@@ -0,0 +1,103 @@
+package envoy.client.event;
+
+import java.io.IOException;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.FileHandler;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+import java.util.logging.SimpleFormatter;
+
+/**
+ * Project: envoy-client
+ * File: EnvoyLogger.java
+ * Created: 14 Dec 2019
+ *
+ * @author Leon Hofmeister
+ * @since Envoy v0.2-alpha
+ */
+public class EnvoyLogger extends Logger {
+
+ private Logger logger;
+ private int fileLevel = 800;
+
+ private Handler handler = new Handler() {
+
+ @Override
+ public void publish(LogRecord arg0) {
+ ConsoleHandler ch;
+ FileHandler fh;
+ SimpleFormatter formatter = new SimpleFormatter();
+ if (arg0.getLevel().intValue() >= fileLevel) {// Case if level >= info
+ try {
+ fh = new FileHandler("Envoy_user.log");
+ logger.addHandler(fh);
+ formatter.formatMessage(arg0);
+ fh.setFormatter(formatter);
+ } catch (SecurityException | IOException e) {
+ e.printStackTrace();
+ }
+ }
+ ch = new ConsoleHandler();
+ logger.addHandler(ch);
+ formatter.formatMessage(arg0);
+ }
+
+ @Override
+ public void flush() {}
+
+ @Override
+ public void close() throws SecurityException {}
+ };
+
+ public EnvoyLogger(String name) {
+ super(name, null);
+ logger.addHandler(handler);
+ }
+
+ /**
+ * Logs a message. If the problem severity is above the FileLevel-barrier, then
+ * the log entry will be written to a file. Regardless of problem severity,
+ * everything will be printed to the console.
+ *
+ * @param level the problem severity
+ * @param msg the message to be written in the log
+ * @since Envoy v0.2-alpha
+ */
+ @Override
+ public void log(Level level, String msg) {
+ LogRecord lr = new LogRecord(level, msg);
+ logger.log(lr);
+ }
+
+ /**
+ * Logs a message. If the problem severity is above the {@code FileLevel}
+ * barrier, then the log entry will be written to a file.
+ * Regardless of problem severity, everything will be printed to the console.
+ *
+ * @param logRecord the LogRecord (Level and String) to be treated by the
+ * Logger.
+ * @since Envoy v0.2-alpha
+ */
+ @Override
+ public void log(LogRecord logRecord) { logger.log(logRecord); }
+
+ /**
+ * @return the fileLevel: The current barrier for writing logs to a file. It can
+ * range from 100-1000 in steps of one hundred with 1000 being
+ * Level.SEVERE
+ * @since Envoy v0.2-alpha
+ */
+ public int getFileLevel() { return fileLevel; }
+
+ /**
+ * @param fileLevel the severity above which on logRecords will be written in a
+ * file instead of the console
+ * @since Envoy v0.2-alpha
+ */
+ public void setFileLevel(int fileLevel) {
+ if (fileLevel <= 10) fileLevel *= 100;
+ this.fileLevel = fileLevel;
+ }
+}
diff --git a/src/main/java/envoy/client/event/Event.java b/src/main/java/envoy/client/event/Event.java
index 9db2477..36e0466 100644
--- a/src/main/java/envoy/client/event/Event.java
+++ b/src/main/java/envoy/client/event/Event.java
@@ -6,6 +6,7 @@ package envoy.client.event;
* Created: 04.12.2019
*
* @author Kai S. K. Engelbart
+ * @param the type of our Event
* @since Envoy v0.2-alpha
*/
public interface Event {
diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java
index 6867f5e..cafd790 100644
--- a/src/main/java/envoy/client/ui/ChatWindow.java
+++ b/src/main/java/envoy/client/ui/ChatWindow.java
@@ -12,7 +12,6 @@ import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.logging.Level;
-import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
@@ -33,6 +32,7 @@ import envoy.client.Client;
import envoy.client.Config;
import envoy.client.LocalDB;
import envoy.client.Settings;
+import envoy.client.event.EnvoyLogger;
import envoy.schema.Message;
import envoy.schema.Sync;
import envoy.schema.User;
@@ -68,7 +68,7 @@ public class ChatWindow extends JFrame {
private static int space = 4;
- private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName());
+ private static final EnvoyLogger logger = new EnvoyLogger(ChatWindow.class.getSimpleName());
public ChatWindow(Client client, LocalDB localDB) {
this.client = client;
@@ -132,8 +132,7 @@ public class ChatWindow extends JFrame {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER
- && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0)
- || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) {
+ && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) {
postMessage(messageList);
}
}
@@ -180,10 +179,9 @@ public class ChatWindow extends JFrame {
settingsButton.addActionListener((evt) -> {
try {
- SettingsScreen.open();
- changeChatWindowColors(Settings.getInstance().getCurrentTheme());
- } catch (Exception e) {
- SettingsScreen.open();
+ new SettingsScreen().setVisible(true);
+ changeChatWindowColors(Settings.getInstance().getCurrentTheme());
+ } catch (Exception e) {
logger.log(Level.WARNING, "An error occured while opening the settings screen", e);
e.printStackTrace();
}
@@ -235,7 +233,7 @@ public class ChatWindow extends JFrame {
gbc_userList.insets = new Insets(space, space, space, space);
changeChatWindowColors(Settings.getInstance().getCurrentTheme());
-
+
contentPane.add(userList, gbc_userList);
contentPane.revalidate();
@@ -244,7 +242,6 @@ public class ChatWindow extends JFrame {
contentPane.revalidate();
}
-
/**
* Used to immediately reload the ChatWindow when settings were changed.
@@ -287,18 +284,14 @@ public class ChatWindow extends JFrame {
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().getID());
+ final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient().getID());
currentChat.appendMessage(message);
messageList.setModel(currentChat.getModel());
@@ -348,8 +341,7 @@ 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();
@@ -359,8 +351,7 @@ public class ChatWindow extends JFrame {
readCurrentChat();
// Update UI
- SwingUtilities
- .invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
+ SwingUtilities.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
}).start();
}).start();
}
@@ -377,4 +368,3 @@ 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 2e6b65f..9be79c5 100644
--- a/src/main/java/envoy/client/ui/MessageListRenderer.java
+++ b/src/main/java/envoy/client/ui/MessageListRenderer.java
@@ -42,22 +42,18 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer%s
%s :%s