Added custom list cells for the user and message list views

This commit is contained in:
Kai S. K. Engelbart 2020-03-28 10:39:15 +01:00
parent 31b032eb08
commit cace49bd68
6 changed files with 199 additions and 125 deletions

View File

@ -1,15 +1,9 @@
package envoy.client; package envoy.client;
import java.awt.EventQueue;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.JFrame;
import envoy.client.data.Settings;
import envoy.client.ui.StatusTrayIcon;
import envoy.client.ui.container.ChatWindow; import envoy.client.ui.container.ChatWindow;
import envoy.data.Config; import envoy.data.Config;
import envoy.exception.EnvoyException;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
/** /**
@ -42,30 +36,29 @@ public class Startup {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
// Display ChatWindow and StatusTrayIcon // Display ChatWindow and StatusTrayIcon
EventQueue.invokeLater(() -> { // EventQueue.invokeLater(() -> {
try { // try {
chatWindow.initContent(client, localDB, writeProxy); // chatWindow.initContent(client, localDB, writeProxy);
//
// Relay unread messages from cache //
if (cache != null && client.isOnline()) cache.relay(); // try {
// new StatusTrayIcon(chatWindow).show();
try { //
new StatusTrayIcon(chatWindow).show(); // // If the tray icon is supported and corresponding settings is set, hide the
// // chat window on close
// If the tray icon is supported and corresponding settings is set, hide the // Settings.getInstance()
// chat window on close // .getItems()
Settings.getInstance() // .get("onCloseMode")
.getItems() // .setChangeHandler((onCloseMode) -> chatWindow
.get("onCloseMode") // .setDefaultCloseOperation((Boolean) onCloseMode ? JFrame.HIDE_ON_CLOSE :
.setChangeHandler((onCloseMode) -> chatWindow // JFrame.EXIT_ON_CLOSE));
.setDefaultCloseOperation((Boolean) onCloseMode ? JFrame.HIDE_ON_CLOSE : JFrame.EXIT_ON_CLOSE)); // } catch (EnvoyException e) {
} catch (EnvoyException e) { // logger.warning("The StatusTrayIcon is not supported on this platform!");
logger.warning("The StatusTrayIcon is not supported on this platform!"); // }
} // } catch (Exception e) {
} catch (Exception e) { // e.printStackTrace();
e.printStackTrace(); // }
} // });
});
} }
} }

View File

@ -8,22 +8,37 @@
<?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?> <?import javafx.scene.layout.RowConstraints?>
<GridPane id="recipientLabel" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="envoy.client.ui.ChatSceneController"> <GridPane id="recipientLabel" maxHeight="-Infinity"
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="envoy.client.ui.ChatSceneController">
<columnConstraints> <columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
prefWidth="100.0" />
</columnConstraints> </columnConstraints>
<rowConstraints> <rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0"
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
</rowConstraints> </rowConstraints>
<children> <children>
<ListView id="userList" prefHeight="211.0" prefWidth="300.0" GridPane.rowIndex="1" /> <ListView fx:id="userList" prefHeight="211.0" prefWidth="300.0"
GridPane.rowIndex="1" />
<Label text="Label" /> <Label text="Label" />
<Button id="settingsButton" mnemonicParsing="false" text="Settings" GridPane.columnIndex="1" /> <Button id="settingsButton" mnemonicParsing="false"
<ListView id="messageList" prefHeight="257.0" prefWidth="300.0" GridPane.columnIndex="1" GridPane.rowIndex="1" /> text="Settings" GridPane.columnIndex="1" />
<Button id="postButton" mnemonicParsing="false" text="Post" GridPane.columnIndex="1" GridPane.rowIndex="2" /> <ListView fx:id="messageList" prefHeight="257.0"
<TextArea prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="2" /> prefWidth="300.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Button id="postButton" mnemonicParsing="false" text="Post"
GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextArea prefHeight="200.0" prefWidth="200.0"
GridPane.rowIndex="2" />
</children> </children>
</GridPane> </GridPane>

View File

@ -5,6 +5,7 @@ import java.util.logging.Logger;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.ListView;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -16,8 +17,20 @@ import javafx.fxml.FXML;
*/ */
public final class ChatSceneController { public final class ChatSceneController {
@FXML
private ListView messageList;
@FXML
private ListView userList;
private static final Logger logger = EnvoyLog.getLogger(ChatSceneController.class); private static final Logger logger = EnvoyLog.getLogger(ChatSceneController.class);
@FXML
public void initialize() {
messageList.setCellFactory(listView -> new MessageListCell());
userList.setCellFactory(listView -> new UserListCell());
}
@FXML @FXML
public void postButtonClicked(ActionEvent e) { public void postButtonClicked(ActionEvent e) {
logger.info("Post Button clicked."); logger.info("Post Button clicked.");

View File

@ -0,0 +1,26 @@
package envoy.client.ui;
import envoy.data.Message;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>MessageListCell.java</strong><br>
* Created: <strong>28.03.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public class MessageListCell extends ListCell<Message> {
/**
* {@inheritDoc}
*/
@Override
protected void updateItem(Message message, boolean empty) {
super.updateItem(message, empty);
if (!empty && message != null) { setGraphic(new Label(message.getText())); }
}
}

View File

@ -1,21 +1,12 @@
package envoy.client.ui; package envoy.client.ui;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.JOptionPane;
import envoy.client.data.*; import envoy.client.data.*;
import envoy.client.net.Client; import envoy.client.net.Client;
import envoy.client.net.WriteProxy; import envoy.client.net.WriteProxy;
import envoy.client.ui.container.LoginDialog;
import envoy.data.Message; import envoy.data.Message;
import envoy.data.User.UserStatus;
import envoy.exception.EnvoyException;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
@ -37,6 +28,7 @@ public final class Startup extends Application {
private LocalDB localDB; private LocalDB localDB;
private Client client; private Client client;
private WriteProxy writeProxy; private WriteProxy writeProxy;
private Cache<Message> cache;
private static final ClientConfig config = ClientConfig.getInstance(); private static final ClientConfig config = ClientConfig.getInstance();
private static final Logger logger = EnvoyLog.getLogger(Startup.class); private static final Logger logger = EnvoyLog.getLogger(Startup.class);
@ -46,81 +38,87 @@ public final class Startup extends Application {
*/ */
@Override @Override
public void init() throws Exception { public void init() throws Exception {
try { // try {
// Load the configuration from client.properties first // // Load the configuration from client.properties first
Properties properties = new Properties(); // Properties properties = new Properties();
properties.load(Startup.class.getClassLoader().getResourceAsStream("client.properties")); // properties.load(Startup.class.getClassLoader().getResourceAsStream("client.properties"));
config.load(properties); // config.load(properties);
//
// Override configuration values with command line arguments // // Override configuration values with command line arguments
String[] args = (String[]) getParameters().getRaw().toArray(); // String[] args = getParameters().getRaw().toArray(new String[0]);
if (args.length > 0) config.load(args); // if (args.length > 0) config.load(args);
//
// Check if all mandatory configuration values have been initialized // // Check if all mandatory configuration values have been initialized
if (!config.isInitialized()) throw new EnvoyException("Configuration is not fully initialized"); // if (!config.isInitialized()) throw new EnvoyException("Configuration is not
} catch (Exception e) { // fully initialized");
JOptionPane.showMessageDialog(null, "Error loading configuration values:\n" + e, "Configuration error", JOptionPane.ERROR_MESSAGE); // } catch (Exception e) {
e.printStackTrace(); // JOptionPane.showMessageDialog(null, "Error loading configuration values:\n" +
System.exit(1); // e, "Configuration error", JOptionPane.ERROR_MESSAGE);
} // e.printStackTrace();
// System.exit(1);
// Setup logger for the envoy package // }
EnvoyLog.initialize(config); //
EnvoyLog.attach("envoy"); // // Setup logger for the envoy package
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier()); // EnvoyLog.initialize(config);
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier()); // EnvoyLog.attach("envoy");
// EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
// Initialize the local database // EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
if (config.isIgnoreLocalDB()) { //
localDB = new TransientLocalDB(); // // Initialize the local database
JOptionPane.showMessageDialog(null, // if (config.isIgnoreLocalDB()) {
"Ignoring local database.\nMessages will not be saved!", // localDB = new TransientLocalDB();
"Local database warning", // JOptionPane.showMessageDialog(null,
JOptionPane.WARNING_MESSAGE); // "Ignoring local database.\nMessages will not be saved!",
} else try { // "Local database warning",
localDB = new PersistentLocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath())); // JOptionPane.WARNING_MESSAGE);
} catch (IOException e3) { // } else try {
logger.log(Level.SEVERE, "Could not initialize local database", e3); // localDB = new PersistentLocalDB(new File(config.getHomeDirectory(),
JOptionPane.showMessageDialog(null, "Could not initialize local database!\n" + e3, "Local database error", JOptionPane.ERROR_MESSAGE); // config.getLocalDB().getPath()));
System.exit(1); // } catch (IOException e3) {
return; // logger.log(Level.SEVERE, "Could not initialize local database", e3);
} // JOptionPane.showMessageDialog(null, "Could not initialize local database!\n"
// + e3, "Local database error", JOptionPane.ERROR_MESSAGE);
// Initialize client and unread message cache // System.exit(1);
Client client = new Client(); // return;
Cache<Message> cache = new Cache<>(); // }
//
// Try to connect to the server // // Initialize client and unread message cache
new LoginDialog(client, localDB, cache); // client = new Client();
// cache = new Cache<>();
// Set client user in local database //
localDB.setUser(client.getSender()); // // Try to connect to the server
// new LoginDialog(client, localDB, cache);
// Initialize chats in local database //
try { // // Set client user in local database
localDB.initializeUserStorage(); // localDB.setUser(client.getSender());
localDB.loadUserData(); //
} catch (FileNotFoundException e) { // // Initialize chats in local database
// The local database file has not yet been created, probably first login // try {
} catch (Exception e) { // localDB.initializeUserStorage();
e.printStackTrace(); // localDB.loadUserData();
JOptionPane.showMessageDialog(null, // } catch (FileNotFoundException e) {
"Error while loading local database: " + e + "\nChats will not be stored locally.", // // The local database file has not yet been created, probably first login
"Local DB error", // } catch (Exception e) {
JOptionPane.WARNING_MESSAGE); // e.printStackTrace();
} // JOptionPane.showMessageDialog(null,
// "Error while loading local database: " + e + "\nChats will not be stored
// Initialize write proxy // locally.",
writeProxy = client.createWriteProxy(localDB); // "Local DB error",
// JOptionPane.WARNING_MESSAGE);
if (client.isOnline()) { // }
//
// Save all users to the local database and flush cache // // Initialize write proxy
localDB.setUsers(client.getUsers()); // writeProxy = client.createWriteProxy(localDB);
writeProxy.flushCache(); //
} else // if (client.isOnline()) {
// Set all contacts to offline mode //
localDB.getUsers().values().stream().filter(u -> u != localDB.getUser()).forEach(u -> u.setStatus(UserStatus.OFFLINE)); // // Save all users to the local database and flush cache
// localDB.setUsers(client.getUsers());
// writeProxy.flushCache();
// } else
// // Set all contacts to offline mode
// localDB.getUsers().values().stream().filter(u -> u !=
// localDB.getUser()).forEach(u -> u.setStatus(UserStatus.OFFLINE));
} }
/** /**
@ -137,6 +135,9 @@ public final class Startup extends Application {
stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/envoy_logo.png"))); stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/envoy_logo.png")));
stage.setScene(chatScene); stage.setScene(chatScene);
stage.show(); stage.show();
// Relay unread messages from cache
if (cache != null && client.isOnline()) cache.relay();
} }
/** /**

View File

@ -0,0 +1,26 @@
package envoy.client.ui;
import envoy.data.User;
import javafx.scene.control.ListCell;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>UserListCell.java</strong><br>
* Created: <strong>28.03.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public class UserListCell extends ListCell<User> {
/**
* {@inheritDoc}
*/
@Override
protected void updateItem(User user, boolean empty) {
super.updateItem(user, empty);
if (!empty && user != null) {
}
}
}