diff --git a/chatter.jar b/chatter.jar index 5a9a0637a0a9f0ae54785c499ea9110d861903c9..28d728f27adaf80a2476d16e22c22b5cf1f556ea 100644 Binary files a/chatter.jar and b/chatter.jar differ diff --git a/src/chat/ChatWindow.java b/src/chat/ChatWindow.java index 0b5ebf0f36df97c854f99971cd9e503418f0f25a..391f948d80f7e90a536814cf8737ff874a827d9e 100644 --- a/src/chat/ChatWindow.java +++ b/src/chat/ChatWindow.java @@ -100,24 +100,7 @@ public class ChatWindow { /** * Initialize the contents of the frame. */ - private void initialize() { - try { - String path = new java.io.File(".").getCanonicalPath(); - this.content.append("<link rel='stylesheet' type='text/css' href='file:///" - + path + "/chat.css'>"); - this.content.append("<head>"); - this.content.append( - " <script language=\"javascript\" type=\"text/javascript\">"); - this.content.append(" function toBottom(){"); - this.content - .append(" window.scrollTo(0, document.body.scrollHeight);"); - this.content.append(" }"); - this.content.append(" </script>"); - this.content.append("</head>"); - this.content.append("<body onload='toBottom()'>"); - } catch (IOException e1) { - } - + private void initialize() { this.frame = new JFrame(); this.frame.setBounds(100, 100, 290, 390); this.frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); @@ -137,17 +120,28 @@ public class ChatWindow { gbc_msgScrollPane.gridx = 0; gbc_msgScrollPane.gridy = 0; - this.chatMessagesPanel = new JFXPanel(); - this.frame.getContentPane().add(this.chatMessagesPanel, gbc_msgScrollPane); - - Platform.runLater(() -> { - ChatWindow.this.chatMessages = new WebView(); - - BorderPane borderPane = new BorderPane(); - borderPane.setCenter(ChatWindow.this.chatMessages); - Scene scene = new Scene(borderPane, 450, 450); - ChatWindow.this.chatMessagesPanel.setScene(scene); - }); + show(); + + (new Thread() { + public void run() { + try { + String path = new java.io.File(".").getCanonicalPath(); + content.append("<link rel='stylesheet' type='text/css' href='file:///" + + path + "/chat.css'>"); + content.append("<head>"); + content.append( + " <script language=\"javascript\" type=\"text/javascript\">"); + content.append(" function toBottom(){"); + content + .append(" window.scrollTo(0, document.body.scrollHeight);"); + content.append(" }"); + content.append(" </script>"); + content.append("</head>"); + content.append("<body onload='toBottom()'>"); + } catch (IOException e1) { + } + } + }).start(); JPanel suggestionsPanel = new JPanel(); GridBagConstraints gbc_suggestionsPanel = new GridBagConstraints(); @@ -269,11 +263,29 @@ public class ChatWindow { } }; this.frame.addKeyListener(giveFocus); - this.chatMessagesPanel.addKeyListener(giveFocus); suggestionsPanel.addKeyListener(giveFocus); myMessagePanel.addKeyListener(giveFocus); + (new Thread() { + public void run() { + chatMessagesPanel = new JFXPanel(); + chatMessagesPanel.addKeyListener(giveFocus); + frame.getContentPane().add(chatMessagesPanel, gbc_msgScrollPane); + Platform.runLater(() -> { + ChatWindow.this.chatMessages = new WebView(); + + BorderPane borderPane = new BorderPane(); + borderPane.setCenter(ChatWindow.this.chatMessages); + Scene scene = new Scene(borderPane, 450, 450); + ChatWindow.this.chatMessagesPanel.setScene(scene); + }); + + } + }).start(); + + this.frame.pack(); + show(); this.myMessage.requestFocusInWindow(); } @@ -322,13 +334,15 @@ public class ChatWindow { String text = ("SOL " + this.myMessage.getText()).trim(); int lastSpace = text.lastIndexOf(' '); String allButLast = lastSpace > -1 ? text.substring(0, lastSpace) : null; - this.undo = this.myMessage.getText(); - String newText = (allButLast.replaceAll("SOL", "") + " " + result).trim(); - if (this.myMessage.getText().startsWith(newText)) { - return false; + if (allButLast != null) { + this.undo = this.myMessage.getText(); + String newText = (allButLast.replaceAll("SOL", "") + " " + result).trim(); + if (this.myMessage.getText().startsWith(newText)) { + return false; + } + this.myMessage.setText(newText); + return true; } - this.myMessage.setText(newText); - return true; } return false; } diff --git a/src/chat/MainWindow.java b/src/chat/MainWindow.java index 2e53edd2ad51384e3f4f7fc600b17e7576a98d83..1c46d27ef9e41985316e5fab684b7cbfaf39fadc 100644 --- a/src/chat/MainWindow.java +++ b/src/chat/MainWindow.java @@ -15,7 +15,6 @@ import javax.swing.JList; import p2.wordsuggestor.WordSuggestor; public class MainWindow { - private JFrame frame; private List<String> usernames; private final List<ChatWindow> chats; @@ -51,22 +50,26 @@ public class MainWindow { list.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { - @SuppressWarnings("unchecked") - JList<String> list = (JList<String>) e.getSource(); - if (e.getClickCount() == 2) { - int index = list.locationToIndex(e.getPoint()); - for (ChatWindow client : MainWindow.this.chats) { - if (client.theirUsername - .equals(MainWindow.this.usernames.get(index))) { - client.show(); - return; + (new Thread() { + public void run() { + @SuppressWarnings("unchecked") + JList<String> list = (JList<String>) e.getSource(); + if (e.getClickCount() == 2) { + int index = list.locationToIndex(e.getPoint()); + for (ChatWindow client : MainWindow.this.chats) { + if (client.theirUsername + .equals(MainWindow.this.usernames.get(index))) { + client.show(); + return; + } + } + + MainWindow.this.chats + .add(new ChatWindow(MainWindow.this.usernames.get(index), + MainWindow.this.markov, MainWindow.this.connection)); } } - - MainWindow.this.chats - .add(new ChatWindow(MainWindow.this.usernames.get(index), - MainWindow.this.markov, MainWindow.this.connection)); - } + }).start(); } }); diff --git a/src/chat/uMessage.java b/src/chat/uMessage.java index 3044e45e9bc8838cbc1dbe2317025d60050e27d1..d66111208b2b6ed2d2397ec745b1d42974699082 100644 --- a/src/chat/uMessage.java +++ b/src/chat/uMessage.java @@ -6,16 +6,24 @@ import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; +import java.awt.BorderLayout; import java.io.IOException; import java.util.function.Supplier; import javax.swing.Box; +import javafx.embed.swing.JFXPanel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; +import javax.swing.JDialog; +import javax.swing.JProgressBar; +import javax.swing.SwingUtilities; +import javax.swing.SwingWorker; import cse332.interfaces.misc.Dictionary; import cse332.types.AlphabeticString; @@ -62,35 +70,65 @@ public class uMessage { } @Override - public void run() { - int N = uMessage.N; - try { - uMessage.markov[this.i] = new WordSuggestor(uMessage.CORPUS, N - this.i, - 4, uMessage.NEW_OUTER, uMessage.NEW_INNER); - uMessage.loading[this.i] = false; - this.window.update(); - } catch (IOException e) { + public void run() { + int N = uMessage.N; + try { + uMessage.markov[this.i] = new WordSuggestor(uMessage.CORPUS, N - this.i, + 4, uMessage.NEW_OUTER, uMessage.NEW_INNER); + this.window.update(); + } catch (IOException e) { + } } - } } /** * Launch the application. */ public static void main(String[] args) { - EventQueue.invokeLater(() -> { - final uMessage window = new uMessage(); - window.frmUmessageLogin.setVisible(true); - window.errors.setText("Loading the Markov Data (n = " + uMessage.N + ")..."); - uMessage.markov = new WordSuggestor[uMessage.N]; - uMessage.loading = new boolean[uMessage.N]; - for (int i1 = 0; i1 < uMessage.N; i1++) { - uMessage.loading[i1] = true; + (new Thread() { + public void run() { + new JFXPanel(); } - for (int i2 = 0; i2 < uMessage.N; i2++) { - new Thread(new MarkovLoader(window, i2)).start(); + }).start(); + + final uMessage window = new uMessage(); + markov = new WordSuggestor[uMessage.N]; + + JDialog dialog = new JDialog((JFrame)null, "Please wait...", true);//true means that the dialog created is modal + JLabel lblStatus = new JLabel("<html><b>Loading Markov Data (n = " + uMessage.N + ")...</b><br>Depending on the data structures you're using<br>" + + "and your computer, this might take a bit.</html>"); + + JProgressBar pbProgress = new JProgressBar(0, 100); + pbProgress.setIndeterminate(true); //we'll use an indeterminate progress bar + + dialog.add(BorderLayout.NORTH, lblStatus); + dialog.add(BorderLayout.CENTER, pbProgress); + dialog.addWindowListener(new WindowAdapter() { + @Override public void windowClosing(WindowEvent e) { + System.exit(0); } }); + dialog.setSize(300, 90); + + SwingWorker<Void, Void> sw = new SwingWorker<Void, Void>() { + @Override + protected Void doInBackground() throws Exception { + for (int i2 = 1; i2 < uMessage.N; i2++) { + new Thread(new MarkovLoader(window, i2)).start(); + } + new MarkovLoader(window, 0).run(); + return null; + } + + @Override + protected void done() { + dialog.dispose();//close the modal dialog + window.frmUmessageLogin.setVisible(true); + } + }; + + sw.execute(); // this will start the processing on a separate thread + dialog.setVisible(true); //this will block user input as long as the processing task is working } /** @@ -112,7 +150,7 @@ public class uMessage { gridBagLayout.columnWidths = new int[] { 0, 90, 90, 90, 0, 0 }; gridBagLayout.rowHeights = new int[] { 0, 9, 0, 0 }; gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0, - Double.MIN_VALUE }; + Double.MIN_VALUE }; gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE }; this.frmUmessageLogin.getContentPane().setLayout(gridBagLayout); @@ -143,10 +181,14 @@ public class uMessage { this.username.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { - if (update() && e.getKeyCode() == KeyEvent.VK_ENTER) { - uMessage.this.login.setEnabled(false); - login(); - } + (new Thread() { + public void run() { + if (update() && e.getKeyCode() == KeyEvent.VK_ENTER) { + uMessage.this.login.setEnabled(false); + login(); + } + } + }).start(); } }); @@ -187,14 +229,6 @@ public class uMessage { } public boolean update() { - boolean noneLoading = true; - for (int i = 0; i < uMessage.loading.length; i++) { - noneLoading &= !uMessage.loading[i]; - } - if (noneLoading) { - this.errors.setText(""); - this.errors.setForeground(Color.BLACK); - } if (!this.loggingIn && this.username.getText().length() > 0) { this.login.setEnabled(true); this.errors.setForeground(Color.BLACK); @@ -210,11 +244,10 @@ public class uMessage { this.loggingIn = true; update(); try { - this.connection = new UMessageServerConnection(this, - this.username.getText().replaceAll(" ", "")); - this.connection.go(); - } catch (IOException e1) { - } + connection = new UMessageServerConnection(uMessage.this, + username.getText().replaceAll(" ", "")); + connection.go(); + } catch (IOException e1) {} } public void badNick() { @@ -225,19 +258,6 @@ public class uMessage { } public void loggedIn(String username) { - boolean noneLoading = false; - while (!noneLoading) { - noneLoading = true; - for (int i = 0; i < uMessage.loading.length; i++) { - noneLoading &= !uMessage.loading[i]; - } - - try { - Thread.sleep(200); - } catch (InterruptedException e) { - } - } - this.frmUmessageLogin.dispose(); this.window = new MainWindow(username, uMessage.markov, this.connection); this.loggingIn = false;