Upload v1
This commit is contained in:
236
src/main/java/tech/nevets/dlite/ChatWindow.java
Normal file
236
src/main/java/tech/nevets/dlite/ChatWindow.java
Normal file
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
* Created by JFormDesigner on Thu Jan 04 15:01:53 CST 2024
|
||||
*/
|
||||
|
||||
package tech.nevets.dlite;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.IOException;
|
||||
import javax.swing.*;
|
||||
import javax.swing.plaf.BorderUIResource;
|
||||
|
||||
import net.miginfocom.swing.*;
|
||||
|
||||
/**
|
||||
* @author steven
|
||||
*/
|
||||
public class ChatWindow extends JPanel {
|
||||
private boolean connected = false;
|
||||
private boolean darkModeBool = true;
|
||||
|
||||
public ChatWindow() {
|
||||
initComponents();
|
||||
}
|
||||
|
||||
private void connectBtnMouseClicked(MouseEvent e) {
|
||||
if (connected) {
|
||||
ConnectionManager.closeActiveConnection();
|
||||
connStatus.setText("Disconnected");
|
||||
connectBtn.setText("Connect");
|
||||
connected = false;
|
||||
} else {
|
||||
Connection conn;
|
||||
try {
|
||||
conn = new Connection(username.getText(), connAddr.getText());
|
||||
ConnectionManager.setActiveConnection(conn);
|
||||
} catch (final Exception ex) {
|
||||
connStatus.setText("Error: " + ex.getMessage());
|
||||
connAddr.setBorder(new BorderUIResource(BorderFactory.createLineBorder(Color.RED)));
|
||||
return;
|
||||
}
|
||||
chat.setText("");
|
||||
conn.setComponents(chat, scrollPane);
|
||||
connStatus.setText("Connected");
|
||||
connectBtn.setText("Disconnect");
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void connAddrKeyPressed(KeyEvent e) {
|
||||
connAddr.setBorder(UIManager.getBorder("TextField.border"));
|
||||
}
|
||||
|
||||
private void usernameFocusGained(FocusEvent e) {
|
||||
if (username.getText().trim().equals("Username")) {
|
||||
username.setText("");
|
||||
}
|
||||
}
|
||||
|
||||
private void usernameFocusLost(FocusEvent e) {
|
||||
if (username.getText().trim().isEmpty()) {
|
||||
username.setText("Username");
|
||||
}
|
||||
}
|
||||
|
||||
private void connAddrFocusGained(FocusEvent e) {
|
||||
if (connAddr.getText().trim().equals("Address:Port")) {
|
||||
connAddr.setText("");
|
||||
}
|
||||
}
|
||||
|
||||
private void connAddrFocusLost(FocusEvent e) {
|
||||
if (connAddr.getText().trim().isEmpty()) {
|
||||
connAddr.setText("Address:Port");
|
||||
}
|
||||
}
|
||||
|
||||
private void sendBtnMouseClicked(MouseEvent e) {
|
||||
Connection conn = ConnectionManager.getActiveConnection();
|
||||
if (conn == null) {
|
||||
return;
|
||||
}
|
||||
conn.sendMessage(messageBox.getText().trim());
|
||||
messageBox.setText("");
|
||||
}
|
||||
|
||||
private void messageBoxKeyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() == 10 && !messageBox.getText().trim().isEmpty()) {
|
||||
sendBtnMouseClicked(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void darkModeMouseClicked(MouseEvent e) {
|
||||
if (darkModeBool) {
|
||||
Main.setDarkMode(false);
|
||||
darkModeBool = false;
|
||||
darkMode.setText("Dark Mode");
|
||||
} else {
|
||||
Main.setDarkMode(true);
|
||||
darkModeBool = true;
|
||||
darkMode.setText("Light Mode");
|
||||
}
|
||||
}
|
||||
|
||||
private void initComponents() {
|
||||
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
|
||||
connStatus = new JLabel();
|
||||
darkMode = new JButton();
|
||||
username = new JTextField();
|
||||
connAddr = new JTextField();
|
||||
connectBtn = new JButton();
|
||||
scrollPane = new JScrollPane();
|
||||
chat = new JTextArea();
|
||||
messageBox = new JTextField();
|
||||
sendBtn = new JButton();
|
||||
|
||||
//======== this ========
|
||||
setPreferredSize(new Dimension(720, 480));
|
||||
setLayout(new MigLayout(
|
||||
"fill,hidemode 3",
|
||||
// columns
|
||||
"[161,fill]" +
|
||||
"[298,grow,fill]" +
|
||||
"[94,shrink 0,center]",
|
||||
// rows
|
||||
"0[24,top]0" +
|
||||
"[24,shrink 0,center]" +
|
||||
"[grow]" +
|
||||
"[24]"));
|
||||
|
||||
//---- connStatus ----
|
||||
connStatus.setText("Disconnected");
|
||||
add(connStatus, "cell 0 0 2 1,aligny center,growy 0");
|
||||
|
||||
//---- darkMode ----
|
||||
darkMode.setText("Dark Mode");
|
||||
darkMode.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
darkModeMouseClicked(e);
|
||||
}
|
||||
});
|
||||
add(darkMode, "cell 2 0,growx");
|
||||
|
||||
//---- username ----
|
||||
username.setText("Username");
|
||||
username.addFocusListener(new FocusAdapter() {
|
||||
@Override
|
||||
public void focusGained(FocusEvent e) {
|
||||
usernameFocusGained(e);
|
||||
}
|
||||
@Override
|
||||
public void focusLost(FocusEvent e) {
|
||||
usernameFocusLost(e);
|
||||
}
|
||||
});
|
||||
add(username, "cell 0 1,growy");
|
||||
|
||||
//---- connAddr ----
|
||||
connAddr.setText("Address:Port");
|
||||
connAddr.addKeyListener(new KeyAdapter() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
connAddrKeyPressed(e);
|
||||
}
|
||||
});
|
||||
connAddr.addFocusListener(new FocusAdapter() {
|
||||
@Override
|
||||
public void focusGained(FocusEvent e) {
|
||||
connAddrFocusGained(e);
|
||||
}
|
||||
@Override
|
||||
public void focusLost(FocusEvent e) {
|
||||
connAddrFocusLost(e);
|
||||
}
|
||||
});
|
||||
add(connAddr, "cell 1 1,growy");
|
||||
|
||||
//---- connectBtn ----
|
||||
connectBtn.setText("Connect");
|
||||
connectBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
||||
connectBtn.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
connectBtnMouseClicked(e);
|
||||
}
|
||||
});
|
||||
add(connectBtn, "cell 2 1,grow");
|
||||
|
||||
//======== scrollPane ========
|
||||
{
|
||||
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
||||
scrollPane.setAutoscrolls(true);
|
||||
|
||||
//---- chat ----
|
||||
chat.setEditable(false);
|
||||
chat.setFont(new Font("Tahoma", Font.PLAIN, 11));
|
||||
chat.setLineWrap(true);
|
||||
scrollPane.setViewportView(chat);
|
||||
}
|
||||
add(scrollPane, "cell 0 2 3 1,grow");
|
||||
|
||||
//---- messageBox ----
|
||||
messageBox.addKeyListener(new KeyAdapter() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
messageBoxKeyPressed(e);
|
||||
}
|
||||
});
|
||||
add(messageBox, "cell 0 3 2 1,growy");
|
||||
|
||||
//---- sendBtn ----
|
||||
sendBtn.setText("Send");
|
||||
sendBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
||||
sendBtn.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
sendBtnMouseClicked(e);
|
||||
}
|
||||
});
|
||||
add(sendBtn, "cell 2 3,grow");
|
||||
// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
|
||||
}
|
||||
|
||||
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
|
||||
private JLabel connStatus;
|
||||
private JButton darkMode;
|
||||
private JTextField username;
|
||||
private JTextField connAddr;
|
||||
private JButton connectBtn;
|
||||
private JScrollPane scrollPane;
|
||||
private JTextArea chat;
|
||||
private JTextField messageBox;
|
||||
private JButton sendBtn;
|
||||
// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
|
||||
}
|
||||
84
src/main/java/tech/nevets/dlite/ChatWindow.jfd
Normal file
84
src/main/java/tech/nevets/dlite/ChatWindow.jfd
Normal file
@@ -0,0 +1,84 @@
|
||||
JFDML JFormDesigner: "8.2.1.0.348" Java: "17.0.9" encoding: "UTF-8"
|
||||
|
||||
new FormModel {
|
||||
contentType: "form/swing"
|
||||
root: new FormRoot {
|
||||
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
|
||||
"$layoutConstraints": "fill,hidemode 3"
|
||||
"$columnConstraints": "[161,fill][298,grow,fill][94,shrink 0,center]"
|
||||
"$rowConstraints": "0[24,top]0[24,shrink 0,center][grow][24]"
|
||||
} ) {
|
||||
name: "this"
|
||||
"preferredSize": new java.awt.Dimension( 720, 480 )
|
||||
add( new FormComponent( "javax.swing.JLabel" ) {
|
||||
name: "connStatus"
|
||||
"text": "Disconnected"
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 0 0 2 1,aligny center,growy 0"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JButton" ) {
|
||||
name: "darkMode"
|
||||
"text": "Dark Mode"
|
||||
addEvent( new FormEvent( "java.awt.event.MouseListener", "mouseClicked", "darkModeMouseClicked", true ) )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 2 0,growx"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JTextField" ) {
|
||||
name: "username"
|
||||
"text": "Username"
|
||||
addEvent( new FormEvent( "java.awt.event.FocusListener", "focusGained", "usernameFocusGained", true ) )
|
||||
addEvent( new FormEvent( "java.awt.event.FocusListener", "focusLost", "usernameFocusLost", true ) )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 0 1,growy"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JTextField" ) {
|
||||
name: "connAddr"
|
||||
"text": "Address:Port"
|
||||
addEvent( new FormEvent( "java.awt.event.KeyListener", "keyPressed", "connAddrKeyPressed", true ) )
|
||||
addEvent( new FormEvent( "java.awt.event.FocusListener", "focusGained", "connAddrFocusGained", true ) )
|
||||
addEvent( new FormEvent( "java.awt.event.FocusListener", "focusLost", "connAddrFocusLost", true ) )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 1 1,growy"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JButton" ) {
|
||||
name: "connectBtn"
|
||||
"text": "Connect"
|
||||
"cursor": &Cursor0 new java.awt.Cursor( 12 )
|
||||
addEvent( new FormEvent( "java.awt.event.MouseListener", "mouseClicked", "connectBtnMouseClicked", true ) )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 2 1,grow"
|
||||
} )
|
||||
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
|
||||
name: "scrollPane"
|
||||
"horizontalScrollBarPolicy": 31
|
||||
"autoscrolls": true
|
||||
add( new FormComponent( "javax.swing.JTextArea" ) {
|
||||
name: "chat"
|
||||
"editable": false
|
||||
"font": new java.awt.Font( "Tahoma", 0, 11 )
|
||||
"lineWrap": true
|
||||
} )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 0 2 3 1,grow"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JTextField" ) {
|
||||
name: "messageBox"
|
||||
addEvent( new FormEvent( "java.awt.event.KeyListener", "keyPressed", "messageBoxKeyPressed", true ) )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 0 3 2 1,growy"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JButton" ) {
|
||||
name: "sendBtn"
|
||||
"text": "Send"
|
||||
"cursor": #Cursor0
|
||||
addEvent( new FormEvent( "java.awt.event.MouseListener", "mouseClicked", "sendBtnMouseClicked", true ) )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 2 3,grow"
|
||||
} )
|
||||
}, new FormLayoutConstraints( null ) {
|
||||
"location": new java.awt.Point( 0, 0 )
|
||||
"size": new java.awt.Dimension( 565, 340 )
|
||||
} )
|
||||
}
|
||||
}
|
||||
|
||||
86
src/main/java/tech/nevets/dlite/Connection.java
Normal file
86
src/main/java/tech/nevets/dlite/Connection.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package tech.nevets.dlite;
|
||||
|
||||
import javax.net.SocketFactory;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.Socket;
|
||||
import java.security.KeyStore;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class Connection {
|
||||
private Socket s;
|
||||
private InputStream is;
|
||||
private PrintStream out;
|
||||
private String username;
|
||||
|
||||
private JTextArea chat;
|
||||
private JScrollBar scrollBar;
|
||||
|
||||
public Connection(String username, String address) throws Exception {
|
||||
String[] addrSplit = address.split(":");
|
||||
if (addrSplit.length != 2) {
|
||||
throw new MalformedURLException("Address not formatted 'ip:port'");
|
||||
}
|
||||
|
||||
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||
InputStream tStore = new FileInputStream("./truststore.p12");
|
||||
trustStore.load(tStore, "".toCharArray());
|
||||
tStore.close();
|
||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||
tmf.init(trustStore);
|
||||
|
||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||
ctx.init(null, tmf.getTrustManagers(), SecureRandom.getInstanceStrong());
|
||||
|
||||
SocketFactory factory = ctx.getSocketFactory();
|
||||
s = factory.createSocket(addrSplit[0], Integer.parseInt(addrSplit[1]));
|
||||
|
||||
is = s.getInputStream();
|
||||
OutputStream os = s.getOutputStream();
|
||||
out = new PrintStream(os);
|
||||
this.username = username;
|
||||
out.println(username);
|
||||
}
|
||||
|
||||
public void sendMessage(String message) {
|
||||
out.println(username + "> " + message);
|
||||
}
|
||||
|
||||
public void setComponents(JTextArea chat, JScrollPane scrollBar) {
|
||||
this.chat = chat;
|
||||
this.scrollBar = scrollBar.getVerticalScrollBar();
|
||||
new Thread(() -> {
|
||||
try {
|
||||
is.transferTo(new ChatWriter());
|
||||
} catch (IOException ignored) {}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
s.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private class ChatWriter extends OutputStream {
|
||||
String buf = "";
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
buf += (char) b;
|
||||
if (((char) b) == '\n') this.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
chat.append(buf);
|
||||
scrollBar.setValue(scrollBar.getMaximum());
|
||||
buf = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
17
src/main/java/tech/nevets/dlite/ConnectionManager.java
Normal file
17
src/main/java/tech/nevets/dlite/ConnectionManager.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package tech.nevets.dlite;
|
||||
|
||||
public class ConnectionManager {
|
||||
private static Connection conn;
|
||||
|
||||
public static void setActiveConnection(Connection c) {
|
||||
conn = c;
|
||||
}
|
||||
|
||||
public static Connection getActiveConnection() {
|
||||
return conn;
|
||||
}
|
||||
|
||||
public static void closeActiveConnection() {
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
72
src/main/java/tech/nevets/dlite/Main.java
Normal file
72
src/main/java/tech/nevets/dlite/Main.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package tech.nevets.dlite;
|
||||
|
||||
import com.formdev.flatlaf.FlatDarkLaf;
|
||||
import com.formdev.flatlaf.FlatLightLaf;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class Main {
|
||||
private static final FlatDarkLaf DARK_LAF = new FlatDarkLaf();
|
||||
private static final FlatLightLaf LIGHT_LAF = new FlatLightLaf();
|
||||
private static TrayIcon trayIcon;
|
||||
private static JFrame frame;
|
||||
|
||||
public static void main(String[] args) {
|
||||
//startTrayService();
|
||||
startUI();
|
||||
}
|
||||
|
||||
public static void startUI() {
|
||||
ChatWindow window = new ChatWindow();
|
||||
frame = new JFrame();
|
||||
frame.add(window);
|
||||
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
frame.pack();
|
||||
frame.setTitle("Discord Lite");
|
||||
frame.setIconImage(Toolkit.getDefaultToolkit().createImage(Main.class.getResource("/icon.png")));
|
||||
frame.setLocationRelativeTo(null);
|
||||
window.requestFocus();
|
||||
setDarkMode(true);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
public static void setDarkMode(boolean darkMode) {
|
||||
try {
|
||||
if (darkMode) {
|
||||
UIManager.setLookAndFeel(DARK_LAF);
|
||||
} else {
|
||||
UIManager.setLookAndFeel(LIGHT_LAF);
|
||||
}
|
||||
SwingUtilities.updateComponentTreeUI(frame);
|
||||
} catch (UnsupportedLookAndFeelException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// private static void startTrayService() {
|
||||
// SystemTray sysTray = SystemTray.getSystemTray();
|
||||
// Image img = Toolkit.getDefaultToolkit().createImage(Main.class.getResource("/icon.png"));
|
||||
// trayIcon = new TrayIcon(img, "Discord Lite");
|
||||
// trayIcon.setImageAutoSize(true);
|
||||
// PopupMenu menu = new PopupMenu("Discord Lite");
|
||||
// menu.add("Open Window");
|
||||
// menu.add("Exit");
|
||||
// menu.addActionListener(al -> {
|
||||
// switch (al.getActionCommand().toLowerCase()) {
|
||||
// case "open window" -> startUI();
|
||||
// case "exit" -> System.exit(0);
|
||||
// }
|
||||
// });
|
||||
// trayIcon.setPopupMenu(menu);
|
||||
// try {
|
||||
// sysTray.add(trayIcon);
|
||||
// } catch (AWTException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public static void sendNotification(String title, String message) {
|
||||
// trayIcon.displayMessage(title, message, TrayIcon.MessageType.INFO);
|
||||
// }
|
||||
}
|
||||
BIN
src/main/resources/icon.ico
Normal file
BIN
src/main/resources/icon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
BIN
src/main/resources/icon.png
Normal file
BIN
src/main/resources/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
Reference in New Issue
Block a user