Java tutorial
/* * 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 gui; import blockcipher.BlockCipher; import blockcipher.StringByteModifier; import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64; import com.google.api.services.gmail.model.Message; import java.awt.Component; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javamailclient.GmailAPI; import javax.mail.MessagingException; import javax.swing.DefaultListCellRenderer; import javax.swing.JFileChooser; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.ListSelectionModel; import javax.swing.filechooser.FileNameExtensionFilter; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import signature.BigPoint; import signature.ECDSA; /** * * @author user */ public class SpamPanel extends javax.swing.JPanel { byte[] key; byte[] cipher; byte[] plain; String message; BigPoint publickey; /** * Creates new form InboxPanel */ public SpamPanel() { initComponents(); } public void generate() { Message[] arrMsg = GmailAPI.Spam.toArray(new Message[GmailAPI.Spam.size()]); SpamList = new JList(arrMsg); SpamList.setCellRenderer(new DefaultListCellRenderer() { // Setting the DefaultListCellRenderer public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Message message = (Message) value; // Using value we are getting the object in JList Map<String, String> map = null; try { map = GmailAPI.getMessageDetails(message.getId()); } catch (MessagingException ex) { Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex); } String sub = map.get("subject"); if (map.get("subject").length() > 22) { sub = map.get("subject").substring(0, 20) + "..."; } setText(sub); // Setting the text //setIcon( shape.getImage() ); // Setting the Image Icon return this; } }); SpamList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); SpamList.setLayoutOrientation(JList.VERTICAL); SpamList.setVisibleRowCount(-1); jScrollPane1.setViewportView(SpamList); SpamList.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { try { JList list = (JList) evt.getSource(); int index = list.locationToIndex(evt.getPoint()); String id = arrMsg[index].getId(); Map<String, String> map = GmailAPI.getMessageDetails(id); jTextField1.setText(map.get("from")); jTextField2.setText(map.get("subject")); dateTextField.setText(map.get("senddate")); BodyTextPane.setText(map.get("body")); BodyTextPane.setContentType("text/html"); //BodyTextArea.setCo } catch (IOException ex) { Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex); } catch (MessagingException ex) { Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex); } } }); } /** * 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() { jScrollPane1 = new javax.swing.JScrollPane(); SpamList = new javax.swing.JList(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); dateTextField = new javax.swing.JTextField(); jScrollPane3 = new javax.swing.JScrollPane(); BodyTextPane = new javax.swing.JTextPane(); SpamList.setModel(new javax.swing.AbstractListModel() { String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); jScrollPane1.setViewportView(SpamList); jLabel1.setText("From"); jLabel2.setText("Subject"); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jButton1.setText("<<"); jButton2.setText(">>"); jLabel3.setText("Date"); jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane3.setViewportView(BodyTextPane); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2).addComponent(jLabel1)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE) .addComponent(jTextField1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(dateTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)) .addComponent(jScrollPane3)) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3) .addComponent(dateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane3)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1).addComponent(jButton2)) .addContainerGap())); }// </editor-fold>//GEN-END:initComponents private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextField1ActionPerformed private String getTextBody() { String html = BodyTextPane.getText(); Document doc = Jsoup.parseBodyFragment(html); Element body = doc.body(); return body.text(); } private void setTextBody(String sbody) { String html = BodyTextPane.getText(); Document doc = Jsoup.parseBodyFragment(html); //Element body = doc.body(); //body.text(sbody); doc.select("body").html(sbody); BodyTextPane.setText(doc.html()); } private String getSign() { String html = BodyTextPane.getText(); Document doc = Jsoup.parseBodyFragment(html); if (doc.select("ds") != null) { //System.out.println("Signature : \n" + doc.select("ds").text()); return doc.select("ds").text(); } return null; } private String[] getSignature(String sign) { String[] ret = new String[2]; int offset = 0; while (sign.charAt(offset) < 'a' || sign.charAt(offset) > 'z' || sign.charAt(offset) < '0' || sign.charAt(offset) > '9') { offset++; } String string1 = ""; String string2 = ""; while (sign.charAt(offset) >= 'a' || sign.charAt(offset) <= 'z' || sign.charAt(offset) >= '0' || sign.charAt(offset) <= '9') { string1 += sign.charAt(offset); offset++; } while (sign.charAt(offset) < 'a' || sign.charAt(offset) > 'z' || sign.charAt(offset) < '0' || sign.charAt(offset) > '9') { offset++; } while ((sign.charAt(offset) >= 'a' || sign.charAt(offset) <= 'z' || sign.charAt(offset) >= '0' || sign.charAt(offset) <= '9') && offset < sign.length()) { string2 += sign.charAt(offset); offset++; } ret[0] = string1; ret[1] = string2; return ret; } private String getPlain() { String html = BodyTextPane.getText(); Document doc = Jsoup.parseBodyFragment(html); doc.select("ds").remove(); System.out.println("DS nya diilangin : \n" + doc.select("body").html()); return doc.select("body").html(); } private void showMsgDialog(String title, String message) { JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextPane BodyTextPane; private javax.swing.JList SpamList; private javax.swing.JTextField dateTextField; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; // End of variables declaration//GEN-END:variables }