/* * PersonalMessageDialog.java * * Created on Jun 23, 2009, 9:11:32 PM * * This file is a part of Shoddy Battle. * Copyright (C) 2009 Catherine Fitzpatrick and Benjamin Gwin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Affero General Public License * as published by the Free Software Foundation; either version 3 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program; if not, visit the Free Software Foundation, Inc. * online at http://gnu.org. */ package shoddybattleclient; import java.awt.Color; import javax.swing.JOptionPane; import shoddybattleclient.network.ServerLink; import shoddybattleclient.network.ServerLink.MessageListener; /** * * @author ben */ public class PersonalMessageDialog extends javax.swing.JFrame implements MessageListener{ private ServerLink m_link; private static final int MAX_LENGTH = 150; /** Creates new form PersonalMessageDialog */ public PersonalMessageDialog(ServerLink link) { initComponents(); m_link = link; String info = "You may enter enter a personal message that other users " + "will see when viewing your profile. No HTML. " + "Maximum " + MAX_LENGTH + " characters."; lblInfo.setText("<html>" + info + "</html>"); //todo: get current message lblLimit.setText("0/" + MAX_LENGTH); m_link.addMessageListener(this); m_link.requestUserMessage(link.getLobby().getUserName()); } @Override public void informMessageRecevied(String user, String msg) { if (!user.equals(m_link.getLobby().getUserName())) return; txtMessage.setText(msg); } @Override public void dispose() { m_link.removeMessageListener(this); super.dispose(); } /** 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { lblInfo = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); txtMessage = new javax.swing.JTextArea(); btnCancel = new javax.swing.JButton(); btnSubmit = new javax.swing.JButton(); lblLimit = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setLocationByPlatform(true); lblInfo.setVerticalAlignment(javax.swing.SwingConstants.TOP); txtMessage.setColumns(20); txtMessage.setLineWrap(true); txtMessage.setRows(5); txtMessage.setTabSize(4); txtMessage.setWrapStyleWord(true); txtMessage.addCaretListener(new javax.swing.event.CaretListener() { public void caretUpdate(javax.swing.event.CaretEvent evt) { txtMessageCaretUpdate(evt); } }); jScrollPane1.setViewportView(txtMessage); btnCancel.setText("Cancel"); btnCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelActionPerformed(evt); } }); btnSubmit.setText("Submit"); btnSubmit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSubmitActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(lblLimit, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 64, Short.MAX_VALUE) .add(btnCancel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(btnSubmit)) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE) .add(lblInfo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(lblInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 58, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(btnSubmit) .add(btnCancel) .add(lblLimit, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void txtMessageCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtMessageCaretUpdate int length = txtMessage.getText().length(); lblLimit.setText(length + "/" + MAX_LENGTH); lblLimit.setForeground(length > MAX_LENGTH ? Color.RED : Color.BLACK); }//GEN-LAST:event_txtMessageCaretUpdate private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed String message = txtMessage.getText(); if (message.length() > MAX_LENGTH) { JOptionPane.showMessageDialog(this, "Your message is too long"); return; } m_link.updatePersonalMessage(message); dispose(); }//GEN-LAST:event_btnSubmitActionPerformed private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed dispose(); }//GEN-LAST:event_btnCancelActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new PersonalMessageDialog(null).setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnCancel; private javax.swing.JButton btnSubmit; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lblInfo; private javax.swing.JLabel lblLimit; private javax.swing.JTextArea txtMessage; // End of variables declaration//GEN-END:variables }