/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Way2SMS.java
*
* Created on Sep 21, 2011, 9:12:36 PM
*/
package way2sms;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
/**
*
* @author Dinesh
*/
public class Way2SMS extends javax.swing.JFrame {
public DefaultTableModel condtm;
public static Way2SMS ws;
public DefaultTableModel destdtm;
CheckBoxHeader rendererComponent;
MyItemListener it;
public ArrayList<String> al = new ArrayList<String>();
private boolean addall = false;
private static boolean msgSending = false;
private PrintWriter p;
private BufferedReader br = null;
/** Creates new form Way2SMS */
public Way2SMS() {
initComponents();
try {
br = new BufferedReader(new FileReader(new File(System.getProperty("user.home") + File.separatorChar + ".w2ss")));
String tmp = br.readLine();
if (tmp.contains("clr=true")) {
Settings.isclear = true;
} else {
Settings.isclear = false;
}
if (tmp.contains("top=true")) {
Settings.istop = true;
} else {
Settings.istop = false;
}
br.close();
} catch (Exception e) {
}
this.setAlwaysOnTop(Settings.istop);
for (int i = 0; i < Way2SMSCore.names.length; i++) {
condtm.insertRow(i, new Object[]{false, Way2SMSCore.names[i]});
}
msgField.setText("");
}
/** 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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
msgField = new javax.swing.JTextArea();
jScrollPane4 = new javax.swing.JScrollPane();
String quickdata[][] = {};
String quickcol[] = {"", "Contacts", "Mobile"};
condtm = new DefaultTableModel(quickdata, quickcol);
contacttbl = new javax.swing.JTable(condtm) {
public boolean isCellEditable(int rowIndex, int colIndex) {
if (colIndex == 0 && colIndex == 0) {
if (msgSending) {
return false;//Disable editing while sending sms
} else {
return true;//Enable editing while sending sms
}
} else {
return false;//Disallow the editing of any cell
}
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
};
charusagelbl = new javax.swing.JLabel();
noofmsglbl = new javax.swing.JLabel();
statuslbl = new javax.swing.JLabel();
clrmsgbtn = new javax.swing.JButton();
sendbtn = new javax.swing.JButton();
contactbtn = new javax.swing.JButton();
accountbtn = new javax.swing.JButton();
settingsbtn = new javax.swing.JButton();
aboutbtn = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
quitchkbox = new javax.swing.JCheckBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Way2SMSClient");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
jLabel1.setText("Select Recipient");
jLabel2.setText("Message");
msgField.setColumns(25);
msgField.setFont(new java.awt.Font("Arial", 0, 13));
msgField.setLineWrap(true);
msgField.setRows(1);
jScrollPane1.setViewportView(msgField);
msgField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
// text was changed
System.out.println("change");
}
public void removeUpdate(DocumentEvent e) {
// text was deleted
statuslbl.setText("Status : ");
charusagelbl.setText("Characters used : " + msgField.getText().length());
if (msgField.getText().length() == 0) {
noofmsglbl.setText("No. of Msgs : ");
}
if (msgField.getText().length() > 0 && msgField.getText().length() <= 140) {
noofmsglbl.setText("No. of Msgs : 1");
}
if (msgField.getText().length() > 140) {
noofmsglbl.setText("No. of Msgs : " + ((msgField.getText().length() / 136) + 1));
}
}
public void insertUpdate(DocumentEvent e) {
// text was inserted
statuslbl.setText("Status : ");
charusagelbl.setText("Characters used : " + msgField.getText().length());
if (msgField.getText().length() == 0) {
noofmsglbl.setText("No. of Msgs : ");
}
if (msgField.getText().length() > 0 && msgField.getText().length() <= 140) {
noofmsglbl.setText("No. of Msgs : 1");
}
if (msgField.getText().length() > 140) {
noofmsglbl.setText("No. of Msgs : " + ((msgField.getText().length() / 136) + 1));
}
}
});
contacttbl.setFont(new java.awt.Font("Arial", 0, 11));
/*
contacttbl.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] { }, new String [] {
"Names","Mobile"
}
));
contacttbl.setToolTipText("");
*/
contacttbl.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
contacttblMouseClicked(evt);
}
});
contacttbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jScrollPane4.setViewportView(contacttbl);
contacttbl.getColumn("Mobile").setWidth(0);
contacttbl.getColumn("Mobile").setMinWidth(0);
contacttbl.getColumn("Mobile").setMaxWidth(0);
// contacttbl.getColumn("").setWidth(22);
contacttbl.getColumn("").setMinWidth(22);
contacttbl.getColumn("").setMaxWidth(23);
contacttbl.setShowGrid(false);
//setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));
contacttbl.getColumnModel().getColumn(0).setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));
contacttbl.addMouseListener(new MyMouseListener());
contacttbl.setRowSelectionAllowed(false);
charusagelbl.setText("Characters used : 0");
noofmsglbl.setText("No. of Msgs : ");
statuslbl.setText("Status : ");
clrmsgbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image_resources/clear_field.png"))); // NOI18N
clrmsgbtn.setToolTipText("Clear Message");
clrmsgbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clrmsgbtnActionPerformed(evt);
}
});
sendbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image_resources/send_sms.png"))); // NOI18N
sendbtn.setToolTipText("Send SMS");
sendbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendbtnActionPerformed(evt);
}
});
contactbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image_resources/contacts_manager.png"))); // NOI18N
contactbtn.setToolTipText("Contacts Manager");
contactbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
contactbtnActionPerformed(evt);
}
});
accountbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image_resources/account.png"))); // NOI18N
accountbtn.setToolTipText("Account Manager");
accountbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
accountbtnActionPerformed(evt);
}
});
settingsbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image_resources/settings.png"))); // NOI18N
settingsbtn.setToolTipText("Settings");
settingsbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
settingsbtnActionPerformed(evt);
}
});
aboutbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image_resources/information_icon.png"))); // NOI18N
aboutbtn.setToolTipText("About");
aboutbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aboutbtnActionPerformed(evt);
}
});
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image_resources/way2smsclient_logo_200px.png"))); // NOI18N
quitchkbox.setText("Quit after sending SMS");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(charusagelbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
.addComponent(clrmsgbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sendbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
.addComponent(jLabel2)
.addComponent(noofmsglbl)
.addComponent(quitchkbox))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jLabel1))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(contactbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(accountbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(settingsbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(aboutbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGroup(layout.createSequentialGroup()
.addGap(103, 103, 103)
.addComponent(jLabel3))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(statuslbl)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(sendbtn)
.addComponent(clrmsgbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(charusagelbl))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(noofmsglbl)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(aboutbtn)
.addComponent(settingsbtn)
.addComponent(accountbtn)
.addComponent(contactbtn)))
.addGroup(layout.createSequentialGroup()
.addGap(7, 7, 7)
.addComponent(quitchkbox)
.addGap(6, 6, 6)
.addComponent(statuslbl)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void contacttblMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_contacttblMouseClicked
if (condtm.getValueAt(contacttbl.getSelectedRow(), 0).toString().equals("true")) {
al.add(String.valueOf((contacttbl.getSelectedRow())));
} else {
try {
al.remove(String.valueOf(contacttbl.getSelectedRow()));
} catch (Exception e) {
System.out.println(e);
}
}
}//GEN-LAST:event_contacttblMouseClicked
private void clrmsgbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clrmsgbtnActionPerformed
// TODO add your handling code here:
msgField.setText("");
msgField.requestFocus();
}//GEN-LAST:event_clrmsgbtnActionPerformed
private void sendbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendbtnActionPerformed
// TODO add your handling code here:
if (al.isEmpty() || msgField.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Hey " + Way2SMSCore.way2smsuser + ",Message and recipient Mobile number can't be empty", "Site2SMSClient", JOptionPane.INFORMATION_MESSAGE);
return;
}
// if (!addall) {
// Collections.sort(al);
// }
sendbtn.setEnabled(false);
clrmsgbtn.setEnabled(false);
msgField.setEnabled(false);
msgSending = true;
new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < al.size(); i++) {
Way2SMSCore.splitMessage(Way2SMSCore.mno[Integer.parseInt(al.get(i).toString())], Way2SMSCore.names[Integer.parseInt(al.get(i).toString())], msgField.getText());
}
if (Way2SMSCore.smsresponse.contains("success")) {
statuslbl.setText("Status : Message(s) sent :)");
if (quitchkbox.isSelected()) {
System.exit(0);
}
} else {
statuslbl.setText("Status : Message sending failed :(");
}
sendbtn.setEnabled(true);
clrmsgbtn.setEnabled(true);
msgField.setEnabled(true);
msgSending = false;
// if (Settings.isclear) {
// msgField.setText("");
// msgField.requestFocus();
// }
} catch (Exception e) {
System.out.println("error : " + e);
}
}
}.start();
}//GEN-LAST:event_sendbtnActionPerformed
private void aboutbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutbtnActionPerformed
// TODO add your handling code here:
// TODO add your handling code here:
if (About.ab == null) {
About.ab = new About();
}
About.ab.setLocationRelativeTo(this);
About.ab.setVisible(true);
}//GEN-LAST:event_aboutbtnActionPerformed
private void contactbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_contactbtnActionPerformed
// TODO add your handling code here:
if (ContactsManager.cm == null) {
ContactsManager.cm = new ContactsManager();
}
ContactsManager.cm.setLocationRelativeTo(this);
ContactsManager.cm.setVisible(true);
}//GEN-LAST:event_contactbtnActionPerformed
private void settingsbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_settingsbtnActionPerformed
// TODO add your handling code here:
if (Settings.s == null) {
Settings.s = new Settings();
}
Settings.s.setLocationRelativeTo(this);
Settings.s.setVisible(true);
}//GEN-LAST:event_settingsbtnActionPerformed
private void accountbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accountbtnActionPerformed
// TODO add your handling code here:
// TODO add your handling code here:
if (Account.acc == null) {
Account.acc = new Account();
}
Account.acc.setLocationRelativeTo(this);
Account.acc.setVisible(true);
}//GEN-LAST:event_accountbtnActionPerformed
private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
// TODO add your handling code here:
if (LoadProgress.firstlaunch) {
JOptionPane.showMessageDialog(this, "Welcome " + Way2SMSCore.way2smsuser + ",\nThank you for using me :)", "Way2SMSClient", JOptionPane.INFORMATION_MESSAGE);
}
}//GEN-LAST:event_formWindowOpened
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
try {
p = new PrintWriter(new FileWriter(new File(System.getProperty("user.home") + File.separatorChar + ".w2ss")));
p.write("clr=" + Settings.isclear + ";top=" + Settings.istop);
p.close();
} catch (Exception e) {
}
}//GEN-LAST:event_formWindowClosing
class MyItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
Object source = e.getSource();
if (source instanceof AbstractButton == false) {
return;
}
addall = e.getStateChange() == ItemEvent.SELECTED;
al.clear();
for (int x = 0, y = contacttbl.getRowCount(); x < y; x++) {
contacttbl.setValueAt(addall, x, 0);
if (addall) {
al.add(String.valueOf(x));
} else {
al.clear();
}
}
}
}
class MyMouseListener extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
int checkedCount = 0;
rendererComponent.removeItemListener(it);
if (rendererComponent instanceof JCheckBox) {
boolean[] flags = new boolean[contacttbl.getRowCount()];
for (int i = 0; i < contacttbl.getRowCount(); i++) {
flags[i] = ((Boolean) contacttbl.getValueAt(i, 0)).booleanValue();
if (flags[i]) {
checkedCount++;
}
}
// if (checkedCount == contacttbl.getRowCount()) {
// ((JCheckBox) rendererComponent).setSelected(true);
// }
// if (checkedCount != contacttbl.getRowCount()) {
// ((JCheckBox) rendererComponent).setSelected(false);
// }
}
rendererComponent.addItemListener(it);
contacttbl.getTableHeader().repaint();
}
}
class CheckBoxHeader extends JCheckBox
implements TableCellRenderer, MouseListener {
protected int column;
protected boolean mousePressed = false;
public CheckBoxHeader(ItemListener itemListener) {
rendererComponent = this;
rendererComponent.addItemListener(itemListener);
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
rendererComponent.setForeground(header.getForeground());
rendererComponent.setBackground(header.getBackground());
rendererComponent.setFont(header.getFont());
header.addMouseListener(rendererComponent);
}
}
setColumn(column);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return rendererComponent;
}
protected void setColumn(int column) {
this.column = column;
}
public int getColumn() {
return column;
}
protected void handleClickEvent(MouseEvent e) {
if (mousePressed) {
mousePressed = false;
JTableHeader header = (JTableHeader) (e.getSource());
JTable tableView = header.getTable();
TableColumnModel columnModel = tableView.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int column = tableView.convertColumnIndexToModel(viewColumn);
if (viewColumn == this.column && e.getClickCount() == 1 && column != -1) {
doClick();
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
handleClickEvent(e);
((JTableHeader) e.getSource()).repaint();
}
@Override
public void mousePressed(MouseEvent e) {
mousePressed = true;
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Way2SMS().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
public javax.swing.JButton aboutbtn;
public javax.swing.JButton accountbtn;
private javax.swing.JLabel charusagelbl;
private javax.swing.JButton clrmsgbtn;
public javax.swing.JButton contactbtn;
private javax.swing.JTable contacttbl;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTextArea msgField;
private javax.swing.JLabel noofmsglbl;
public javax.swing.JCheckBox quitchkbox;
public javax.swing.JButton sendbtn;
public javax.swing.JButton settingsbtn;
public javax.swing.JLabel statuslbl;
// End of variables declaration//GEN-END:variables
}