diff --git a/src/br/edu/ufsj/sms/GUI/ChatWindow.java b/src/br/edu/ufsj/sms/GUI/ChatWindow.java new file mode 100644 index 0000000..a57579d --- /dev/null +++ b/src/br/edu/ufsj/sms/GUI/ChatWindow.java @@ -0,0 +1,97 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package br.edu.ufsj.sms.GUI; + +import java.awt.event.KeyEvent; + +/** + * + * @author flavio + */ +public class ChatWindow extends javax.swing.JDialog { + + private javax.swing.JButton jButton1; + private javax.swing.JEditorPane jEditorPane1; + private javax.swing.JPanel jPanel1; + private javax.swing.JPanel jPanel2; + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JScrollPane jScrollPane2; + private javax.swing.JSplitPane jSplitPane1; + private javax.swing.JTextArea jTextArea1; + + private MainWindow mainWindow; + + public ChatWindow(MainWindow mainWindow) { + this.mainWindow = mainWindow; + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + private void initComponents() { + + this.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE); + this.setTitle("Chat Window"); + this.setBounds(new java.awt.Rectangle(0, 0, 800, 600)); + this.getContentPane().setLayout(new java.awt.BorderLayout(5, 5)); + this.setPreferredSize(new java.awt.Dimension(800, 600)); + + jSplitPane1 = new javax.swing.JSplitPane(); + jScrollPane1 = new javax.swing.JScrollPane(); + jEditorPane1 = new javax.swing.JEditorPane(); + jPanel1 = new javax.swing.JPanel(); + jScrollPane2 = new javax.swing.JScrollPane(); + jTextArea1 = new javax.swing.JTextArea(); + jPanel2 = new javax.swing.JPanel(); + jButton1 = new javax.swing.JButton(); + + jSplitPane1.setDividerLocation(500); + jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); + jEditorPane1.setContentType("text/html"); + jPanel1.setLayout(new java.awt.BorderLayout()); + jTextArea1.setColumns(20); + jTextArea1.setRows(5); + jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() { + @Override + public void keyTyped(java.awt.event.KeyEvent evt) { + if (evt.getKeyChar() == '\n') { + sendMessage(); + } + } + }); + jButton1.setText("Send"); + + jScrollPane1.setViewportView(jEditorPane1); + jScrollPane2.setViewportView(jTextArea1); + jSplitPane1.setTopComponent(jScrollPane1); + + jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER); + jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT)); + jButton1.addActionListener((java.awt.event.ActionEvent evt) -> { + sendMessage(); + }); + jPanel2.add(jButton1); + jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_END); + jSplitPane1.setRightComponent(jPanel1); + getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER); + pack(); + } + + private void sendMessage() { + if (jTextArea1.getText().trim().length() == 0) { + return; + } + mainWindow.sendChatMessage(jTextArea1.getText()); + jTextArea1.setText(""); + } + + public void receiveChatMessage(String message) { + jEditorPane1.setText(message + jEditorPane1.getText()); + } +} diff --git a/src/br/edu/ufsj/sms/GUI/MainWindow.form b/src/br/edu/ufsj/sms/GUI/MainWindow.form index 132d096..b70a321 100644 --- a/src/br/edu/ufsj/sms/GUI/MainWindow.form +++ b/src/br/edu/ufsj/sms/GUI/MainWindow.form @@ -1,11 +1,102 @@
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -24,17 +115,18 @@ - - + + + + - - - + + @@ -43,26 +135,6 @@ - - - - - - - - - - - - - - - - - - - - @@ -70,94 +142,13 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + diff --git a/src/br/edu/ufsj/sms/GUI/MainWindow.java b/src/br/edu/ufsj/sms/GUI/MainWindow.java index 3ded621..141e1d1 100644 --- a/src/br/edu/ufsj/sms/GUI/MainWindow.java +++ b/src/br/edu/ufsj/sms/GUI/MainWindow.java @@ -40,6 +40,8 @@ public class MainWindow extends javax.swing.JFrame { private String name; private int port; + private ChatWindow chatWindow; + public MainWindow(String name, int port) throws UnknownHostException { initComponents(); this.name = name; @@ -48,6 +50,7 @@ public class MainWindow extends javax.swing.JFrame { this.socket.start(); this.timer = new Timer(); jLabel1.setText("Connected on port " + this.port + " name " + this.name); + this.chatWindow = new ChatWindow(this); } /** @@ -59,98 +62,109 @@ public class MainWindow extends javax.swing.JFrame { // //GEN-BEGIN:initComponents private void initComponents() { - jToolBar1 = new javax.swing.JToolBar(); - shareButton = new javax.swing.JToggleButton(); + jMenu4 = new javax.swing.JMenu(); jScrollPane1 = new javax.swing.JScrollPane(); jTabbedPane1 = new javax.swing.JTabbedPane(); - jPanel2 = new javax.swing.JPanel(); - jSplitPane1 = new javax.swing.JSplitPane(); - jScrollPane3 = new javax.swing.JScrollPane(); - chatBoard = new javax.swing.JEditorPane(); - jPanel3 = new javax.swing.JPanel(); - jTextArea2 = new javax.swing.JTextArea(); - jButton1 = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); + jMenuBar1 = new javax.swing.JMenuBar(); + fileMenu = new javax.swing.JMenu(); + closeTabMenu = new javax.swing.JMenuItem(); + quitMenu = new javax.swing.JMenuItem(); + editMenu = new javax.swing.JMenu(); + shareMySheetMenu = new javax.swing.JCheckBoxMenuItem(); + viewMenu = new javax.swing.JMenu(); + showChatWindowMenu = new javax.swing.JMenuItem(); + helpMenu = new javax.swing.JMenu(); + aboutMenu = new javax.swing.JMenuItem(); + + jMenu4.setText("jMenu4"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Share My Sheet"); - jToolBar1.setRollover(true); - - shareButton.setText("Share My Sheet!"); - shareButton.setFocusable(false); - shareButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); - shareButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); - shareButton.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - shareButtonActionPerformed(evt); - } - }); - jToolBar1.add(shareButton); - - jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS)); - - jSplitPane1.setDividerLocation(400); - jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); - - chatBoard.setContentType("text/html"); // NOI18N - jScrollPane3.setViewportView(chatBoard); - - jSplitPane1.setLeftComponent(jScrollPane3); - - jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT)); - - jTextArea2.setColumns(20); - jTextArea2.setRows(5); - jPanel3.add(jTextArea2); - - jButton1.setText("Send"); - jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton1ActionPerformed(evt); - } - }); - jPanel3.add(jButton1); - - jSplitPane1.setBottomComponent(jPanel3); - - jPanel2.add(jSplitPane1); - - jTabbedPane1.addTab("Chat", jPanel2); - jScrollPane1.setViewportView(jTabbedPane1); - jLabel1.setText("jLabel1"); + jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT)); - javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); - jPanel1.setLayout(jPanel1Layout); - jPanel1Layout.setHorizontalGroup( - jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(jPanel1Layout.createSequentialGroup() - .addContainerGap() - .addComponent(jLabel1) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - ); - jPanel1Layout.setVerticalGroup( - jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jLabel1) - ); + jLabel1.setText("jLabel1"); + jPanel1.add(jLabel1); + + fileMenu.setText("File"); + fileMenu.setToolTipText(""); + + closeTabMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK)); + closeTabMenu.setText("Close Current Tab"); + closeTabMenu.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + closeTabMenuActionPerformed(evt); + } + }); + fileMenu.add(closeTabMenu); + + quitMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK)); + quitMenu.setText("Quit"); + quitMenu.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + quitMenuActionPerformed(evt); + } + }); + fileMenu.add(quitMenu); + + jMenuBar1.add(fileMenu); + + editMenu.setText("Edit"); + + shareMySheetMenu.setText("Share My Sheet!"); + shareMySheetMenu.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + shareMySheetMenuActionPerformed(evt); + } + }); + editMenu.add(shareMySheetMenu); + + jMenuBar1.add(editMenu); + + viewMenu.setText("View"); + + showChatWindowMenu.setText("Chat Window"); + showChatWindowMenu.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + showChatWindowMenuActionPerformed(evt); + } + }); + viewMenu.add(showChatWindowMenu); + + jMenuBar1.add(viewMenu); + + helpMenu.setText("Help"); + + aboutMenu.setText("About"); + aboutMenu.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + aboutMenuActionPerformed(evt); + } + }); + helpMenu.add(aboutMenu); + + jMenuBar1.add(helpMenu); + + setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING) + .addGroup(layout.createSequentialGroup() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 846, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(0, 0, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() - .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jScrollPane1) + .addGap(0, 0, 0) + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 463, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) @@ -159,32 +173,44 @@ public class MainWindow extends javax.swing.JFrame { pack(); }// //GEN-END:initComponents - private void shareButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shareButtonActionPerformed - if (shareButton.isSelected()) { + private void showChatWindowMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showChatWindowMenuActionPerformed + this.chatWindow.setVisible(true); + }//GEN-LAST:event_showChatWindowMenuActionPerformed + + private void aboutMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_aboutMenuActionPerformed + + private void shareMySheetMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shareMySheetMenuActionPerformed + if (shareMySheetMenu.isSelected()) { this.timer = new Timer(); this.alarmClock = new AlarmClock(this.name, this.socket); this.timer.scheduleAtFixedRate(this.alarmClock, 0, MainWindow.TIME); - shareButton.setText("Stop sharing it!"); + shareMySheetMenu.setText("Stop sharing it!"); } else { this.timer.cancel(); - shareButton.setText("Share My Sheet!"); + shareMySheetMenu.setText("Share My Sheet!"); } - }//GEN-LAST:event_shareButtonActionPerformed + }//GEN-LAST:event_shareMySheetMenuActionPerformed - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed - if (jTextArea2.getText().trim().length() == 0) { - return; - } - ChatMessage message = new ChatMessage(); - message.setText(jTextArea2.getText()); - message.setName(this.name); + private void closeTabMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeTabMenuActionPerformed + jTabbedPane1.remove(jTabbedPane1.getSelectedIndex()); + }//GEN-LAST:event_closeTabMenuActionPerformed + + private void quitMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitMenuActionPerformed + this.setVisible(false); + this.dispose(); + System.exit(0); + }//GEN-LAST:event_quitMenuActionPerformed + + public void sendChatMessage(String text) { + ChatMessage message = new ChatMessage(this.name, text); try { this.socket.send(message.toByteArray()); } catch (IOException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } - jTextArea2.setText(""); - }//GEN-LAST:event_jButton1ActionPerformed + } public void receiveScreenCastMessage(ScreenCastMessage message) { // Verificar se a aba existe, se existir, atualizo. @@ -212,26 +238,27 @@ public class MainWindow extends javax.swing.JFrame { public void receiveChatMessage(ChatMessage message) { SimpleDateFormat dt = new SimpleDateFormat("dd/MM/yyyyy hh:mm:ss"); Date date = new Date(); - chatBoard.setText(dt.format(date) + " | " + - "" + message.getName() + ": "+ - message.getText() + "
" + - chatBoard.getText() - ); + chatWindow.receiveChatMessage(dt.format(date) + " | " + + "" + message.getName() + ": " + + message.getText() + "
"); } + // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.JEditorPane chatBoard; - private javax.swing.JButton jButton1; + private javax.swing.JMenuItem aboutMenu; + private javax.swing.JMenuItem closeTabMenu; + private javax.swing.JMenu editMenu; + private javax.swing.JMenu fileMenu; + private javax.swing.JMenu helpMenu; private javax.swing.JLabel jLabel1; + private javax.swing.JMenu jMenu4; + private javax.swing.JMenuBar jMenuBar1; private javax.swing.JPanel jPanel1; - private javax.swing.JPanel jPanel2; - private javax.swing.JPanel jPanel3; private javax.swing.JScrollPane jScrollPane1; - private javax.swing.JScrollPane jScrollPane3; - private javax.swing.JSplitPane jSplitPane1; private javax.swing.JTabbedPane jTabbedPane1; - private javax.swing.JTextArea jTextArea2; - private javax.swing.JToolBar jToolBar1; - private javax.swing.JToggleButton shareButton; + private javax.swing.JMenuItem quitMenu; + private javax.swing.JCheckBoxMenuItem shareMySheetMenu; + private javax.swing.JMenuItem showChatWindowMenu; + private javax.swing.JMenu viewMenu; // End of variables declaration//GEN-END:variables /** diff --git a/src/br/edu/ufsj/sms/net/ChatMessage.java b/src/br/edu/ufsj/sms/net/ChatMessage.java index 9f8a15d..6766f8f 100644 --- a/src/br/edu/ufsj/sms/net/ChatMessage.java +++ b/src/br/edu/ufsj/sms/net/ChatMessage.java @@ -12,8 +12,27 @@ import java.io.Serializable; * @author flavio */ public class ChatMessage extends Message implements Serializable{ - private String name; + private String text; + private String name; + + public ChatMessage(String name, String text){ + this.name = name; + this.text = text; + } + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } /** * @return the text @@ -29,17 +48,4 @@ public class ChatMessage extends Message implements Serializable{ this.text = text; } - /** - * @return the name - */ - public String getName() { - return name; - } - - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } } diff --git a/src/br/edu/ufsj/sms/net/ScreenCastMessage.java b/src/br/edu/ufsj/sms/net/ScreenCastMessage.java index 3954f7d..b071e32 100644 --- a/src/br/edu/ufsj/sms/net/ScreenCastMessage.java +++ b/src/br/edu/ufsj/sms/net/ScreenCastMessage.java @@ -13,11 +13,11 @@ import java.io.Serializable; */ public class ScreenCastMessage extends Message implements Serializable { - private String name; private byte[] imageByte; + private String name; + + public ScreenCastMessage() { - public ScreenCastMessage(){ - } public ScreenCastMessage(String name, byte[] imageByte) { @@ -53,5 +53,4 @@ public class ScreenCastMessage extends Message implements Serializable { this.imageByte = imageByte; } - }