package ui;
import java.awt.Color;
import java.awt.print.PrinterException;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import util.Iwlistscan;
/**
*
* @author psychok7
*/
public class Frame_Output extends javax.swing.JFrame implements DocumentListener {
public boolean check_destroy; //destroy window if there is no interface chosen
protected String scan_interface;
final static Color HILIT_COLOR = Color.LIGHT_GRAY; //initialize search components
final static Color ERROR_COLOR = Color.PINK;
final static String CANCEL_ACTION = "cancel-search";
final Color jTextField1Bg;
final Highlighter hilit;
final Highlighter.HighlightPainter painter;
public Frame_Output() {
initComponents();
this.setDefaultCloseOperation(Frame_Output.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.check_destroy=false;
try {
start_iwlistscan();
} catch (IOException ex) {
Logger.getLogger(Frame_Output.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(Frame_Output.class.getName()).log(Level.SEVERE, null, ex);
}
hilit = new DefaultHighlighter();
painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
jTextArea1.setHighlighter(hilit);
jTextField1Bg = jTextField1.getBackground();
jTextField1.getDocument().addDocumentListener((DocumentListener) this);
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
back_btn = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
rescan_btn = new javax.swing.JMenuItem();
Print_btn = new javax.swing.JMenuItem();
Back_btn2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
About = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Wireless Scan Information", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Purisa", 0, 13))); // NOI18N
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jTextArea1MousePressed(evt);
}
});
jScrollPane1.setViewportView(jTextArea1);
jLabel1.setFont(new java.awt.Font("Purisa", 0, 13)); // NOI18N
jLabel1.setText("Search:");
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jTextField1KeyPressed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Purisa", 0, 13)); // NOI18N
jLabel2.setText("Enter Text To Search");
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()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))
.addComponent(jLabel2))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.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))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2))
);
back_btn.setFont(new java.awt.Font("Purisa", 0, 13)); // NOI18N
back_btn.setText("Back");
back_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
back_btnActionPerformed(evt);
}
});
jMenu1.setText("File");
jMenu1.setFont(new java.awt.Font("Purisa", 0, 13)); // NOI18N
rescan_btn.setFont(new java.awt.Font("Purisa", 0, 13)); // NOI18N
rescan_btn.setText("Re-scan");
rescan_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rescan_btnActionPerformed(evt);
}
});
jMenu1.add(rescan_btn);
Print_btn.setFont(new java.awt.Font("Purisa", 0, 13)); // NOI18N
Print_btn.setText("Print");
Print_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Print_btnActionPerformed(evt);
}
});
jMenu1.add(Print_btn);
Back_btn2.setFont(new java.awt.Font("Purisa", 0, 13)); // NOI18N
Back_btn2.setText("Back");
Back_btn2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Back_btn2ActionPerformed(evt);
}
});
jMenu1.add(Back_btn2);
jMenuBar1.add(jMenu1);
jMenu2.setText("Help");
jMenu2.setFont(new java.awt.Font("Purisa", 0, 13)); // NOI18N
About.setFont(new java.awt.Font("Purisa", 0, 13)); // NOI18N
About.setText("About");
About.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AboutActionPerformed(evt);
}
});
jMenu2.add(About);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
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()
.addGap(162, 162, 162)
.addComponent(back_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(back_btn)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void back_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_back_btnActionPerformed
// TODO add your handling code here:
this.dispose();
}//GEN-LAST:event_back_btnActionPerformed
private void Back_btn2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Back_btn2ActionPerformed
// TODO add your handling code here:
this.dispose();
}//GEN-LAST:event_Back_btn2ActionPerformed
private void AboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AboutActionPerformed
// TODO add your handling code here:
new Frame_About().setVisible(true);
}//GEN-LAST:event_AboutActionPerformed
private void Print_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Print_btnActionPerformed
// TODO add your handling code here:
try {
boolean complete = jTextArea1.print();
if (complete) {
/* show a success message */
JOptionPane.showMessageDialog(null, "Printing successfull");
}
else {
/*show a message indicating that printing was cancelled */
JOptionPane.showMessageDialog(null, "Printing cancelled");
}
}
catch (PrinterException pe) {
/* Printing failed, report to the user */
JOptionPane.showMessageDialog(null, "Printing Failed");
}
}//GEN-LAST:event_Print_btnActionPerformed
private void jTextArea1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextArea1MousePressed
// TODO add your handling code here:
}//GEN-LAST:event_jTextArea1MousePressed
private void rescan_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rescan_btnActionPerformed
// TODO add your handling code here:
Iwlistscan scan = null;
this.jTextArea1.setText("");
try {
scan = new Iwlistscan(this.scan_interface);
} catch (IOException ex) {
Logger.getLogger(Frame_Output.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(Frame_Output.class.getName()).log(Level.SEVERE, null, ex);
}
setOutput(getOutput()+scan.stdout);
setOutput(getOutput()+scan.stderr);
}//GEN-LAST:event_rescan_btnActionPerformed
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1KeyPressed
public void insertUpdate(DocumentEvent e) {
search();
}
public void removeUpdate(DocumentEvent e) {
search();
}
public void changedUpdate(DocumentEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
private void search() {
hilit.removeAllHighlights();
String toFind = jTextField1.getText();
if (toFind.length() <= 0) {
message("Nothing to search");
return;
}
String content = jTextArea1.getText();
int index = content.indexOf(toFind, 0);
if (index >= 0) { // match found
try {
while(index>-1){ // if found substring, keep searching
int end = index + toFind.length();
hilit.addHighlight(index, end, painter);
jTextArea1.setCaretPosition(end);
jTextField1.setBackground(jTextField1Bg);
message("'" + toFind + "' found.");
index = content.indexOf(toFind, index+toFind.length());
}
} catch (BadLocationException e) {
e.printStackTrace();
}
} else {
jTextField1.setBackground(ERROR_COLOR);
message("'" + toFind + "' not found.");
}
}
private void message(String msg) {
this.jLabel2.setText(msg);
}
private String CurrentDate(){
Calendar cal = new GregorianCalendar();
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
int day = cal.get(Calendar.DAY_OF_MONTH);
int time=cal.get(Calendar.HOUR_OF_DAY);
String data="Date: " + day + "/" + (month + 1) + "/" + year + "\n" + "Time: " +time+"h" + "\n";
return data;
}
private void start_iwlistscan() throws IOException, InterruptedException {
this.scan_interface=JOptionPane.showInputDialog("Enter the Interface you want to scan (eg:wlan0)");
Iwlistscan scan;
if (this.scan_interface!=null && (this.scan_interface.length() > 0)){
scan=new Iwlistscan(this.scan_interface.toLowerCase());
setOutput(getOutput()+scan.stdout);
setOutput(getOutput()+scan.stderr);
}
else
check_destroy=true;
}
public void setOutput(String input){
jTextArea1.setText(input+"\n");
}
public void setOutput2(String input){ //clean
jTextArea1.setText(input);
}
public String getOutput(){
return jTextArea1.getText();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame_Output().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem About;
private javax.swing.JMenuItem Back_btn2;
private javax.swing.JMenuItem Print_btn;
private javax.swing.JButton back_btn;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JMenuItem rescan_btn;
// End of variables declaration//GEN-END:variables
}