package LogAnalyzer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author emanuele
*/
public class LogAnalyzerWin extends javax.swing.JFrame {
private static JFileChooser fc = new JFileChooser();
private boolean _filterIncludeStatus = false;
private boolean _filterExcludeStatus = false;
private boolean _readyToAdd = false;
private int _selectedRow = -1;
/**
* Creates new form LogAnalyzerWin
*/
public LogAnalyzerWin() {
initComponents();
TableColumn typeColumn = this.filtersList.getColumnModel().getColumn(0);
JComboBox comboBox = new JComboBox();
comboBox.addItem("Include");
comboBox.addItem("Exclude");
comboBox.addActionListener(new ActionListener () {
@Override
/**
* This is the comboBox listener: when the value changes I have to:
* 1) remove the pattern from the "previous" list
* 2) add the pattern to the "new" list
*/
public void actionPerformed(ActionEvent ae) {
JComboBox comboBox = (JComboBox) ae.getSource();
String selectedOption = comboBox.getSelectedItem().toString();
String currentType = "";
String currentValue = "";
if (_selectedRow != -1) {
currentType = filtersList.getValueAt(_selectedRow, 0).toString();
currentValue = filtersList.getValueAt(_selectedRow, 1).toString();
}
// Just do nothing if nothing changed
if (currentType.toLowerCase().equals(selectedOption.toLowerCase()))
return;
if (selectedOption.toLowerCase().equals("include")) {
logFilter.remove("exclude", currentValue);
logFilter.include(currentValue);
}
else if (selectedOption.toLowerCase().equals("exclude")) {
logFilter.remove("include", currentValue);
logFilter.exclude(currentValue);
}
}
});
ListSelectionModel rowSM = this.filtersList.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
@Override
/**
* When a cell is selected, save the status of the type so that
* I can know from which list remove the "previous" entry (see comboBox
* action listener)
*/
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
if (lsm.isSelectionEmpty()) {
_selectedRow = -1;
} else {
_selectedRow = lsm.getMinSelectionIndex();
}
}
});
typeColumn.setCellEditor(new DefaultCellEditor(comboBox));
}
/**
* 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() {
filterOptGroup = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
addFilter = new javax.swing.JButton();
importFilters = new javax.swing.JButton();
exportFilters = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
filterPattern = new javax.swing.JTextField();
filterInclude = new javax.swing.JRadioButton();
filterExclude = new javax.swing.JRadioButton();
removeFilter = new javax.swing.JButton();
jScrollPane3 = new javax.swing.JScrollPane();
filtersList = new javax.swing.JTable();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel2");
addFilter.setText("Add filter");
addFilter.setEnabled(false);
addFilter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addFilterActionPerformed(evt);
}
});
importFilters.setText("Import filters");
exportFilters.setText("Export filters");
exportFilters.setEnabled(false);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane2.setViewportView(jTable1);
filterPattern.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
filterPatternKeyReleased(evt);
}
});
filterOptGroup.add(filterInclude);
filterInclude.setText("include");
filterInclude.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
filterIncludeActionPerformed(evt);
}
});
filterOptGroup.add(filterExclude);
filterExclude.setText("exclude");
filterExclude.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
filterExcludeActionPerformed(evt);
}
});
removeFilter.setText("Remove selected");
removeFilter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
removeFilterActionPerformed(evt);
}
});
filtersList.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Type", "Pattern"
}
) {
Class[] types = new Class [] {
java.lang.Object.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
filtersList.setColumnSelectionAllowed(true);
filtersList.getTableHeader().setReorderingAllowed(false);
jScrollPane3.setViewportView(filtersList);
filtersList.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jMenu1.setText("File");
jMenuItem1.setText("Open");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 707, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(filterPattern)
.addGroup(layout.createSequentialGroup()
.addComponent(removeFilter)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(importFilters)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(exportFilters))
.addGroup(layout.createSequentialGroup()
.addComponent(filterInclude)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(filterExclude)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(addFilter)))))
.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()
.addComponent(filterPattern, 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(filterInclude)
.addComponent(filterExclude)
.addComponent(addFilter))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(importFilters)
.addComponent(exportFilters)
.addComponent(removeFilter)))
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
//int returnVal = fc.showOpenDialog(FilePicker.this);
//fc = new JFileChooser();
fc.showDialog(new LogAnalyzerWin(), "Open");
jLabel1.setText(fc.getSelectedFile().toString());
LogAnalyzer.startAnalysis(fc.getSelectedFile().toString());
jLabel2.setText("end for now");
}//GEN-LAST:event_jMenuItem1ActionPerformed
private void filterPatternKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_filterPatternKeyReleased
this._checkAddFilterStatus();
}//GEN-LAST:event_filterPatternKeyReleased
private void filterIncludeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filterIncludeActionPerformed
this._filterIncludeStatus = this.filterInclude.isSelected();
this._readyToAdd = true;
this._checkAddFilterStatus();
}//GEN-LAST:event_filterIncludeActionPerformed
private void filterExcludeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filterExcludeActionPerformed
this._filterExcludeStatus = this.filterExclude.isSelected();
this._readyToAdd = true;
this._checkAddFilterStatus();
}//GEN-LAST:event_filterExcludeActionPerformed
private void addFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFilterActionPerformed
DefaultTableModel filtersTable = (DefaultTableModel) this.filtersList.getModel();
if (this._readyToAdd) {
if (this._filterIncludeStatus) {
logFilter.include(this.filterPattern.getText());
filtersTable.insertRow(filtersTable.getRowCount(), new Object [] {"Include", this.filterPattern.getText()});
}
if (this._filterExcludeStatus) {
logFilter.exclude(this.filterPattern.getText());
filtersTable.insertRow(filtersTable.getRowCount(), new Object [] {"Exclude", this.filterPattern.getText()});
}
this._readyToAdd = false;
this._resetFilter();
}
}//GEN-LAST:event_addFilterActionPerformed
private void removeFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeFilterActionPerformed
try {
String currentType = "";
String currentValue = "";
if (_selectedRow != -1) {
currentType = filtersList.getValueAt(_selectedRow, 0).toString();
currentValue = filtersList.getValueAt(_selectedRow, 1).toString();
}
if (currentType.toLowerCase().equals("include")) {
logFilter.remove("include", currentValue);
}
else if (currentType.toLowerCase().equals("exclude")) {
logFilter.remove("exclude", currentValue);
}
((DefaultTableModel) filtersList.getModel()).removeRow(_selectedRow);
}
catch (ArrayIndexOutOfBoundsException e) {
}
}//GEN-LAST:event_removeFilterActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LogAnalyzerWin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new LogAnalyzerWin().setVisible(true);
}
});
}
/**
* Utility function: enable or disable the "add filter" button depending if
* the conditions to add the filter are met or not
*/
private void _checkAddFilterStatus() {
String cText = this.filterPattern.getText();
// Empty pattern or no radio selected: add disabled
if (cText.trim().equals("") || (!this._filterIncludeStatus && !this._filterExcludeStatus)) {
this.addFilter.setEnabled(false);
}
else {
this.addFilter.setEnabled(true);
}
}
private void _resetFilter() {
this._filterExcludeStatus = false;
this._filterIncludeStatus = false;
this._readyToAdd = false;
this.filterOptGroup.clearSelection();
this.filterPattern.setText("");
this._checkAddFilterStatus();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addFilter;
private javax.swing.JButton exportFilters;
private javax.swing.JRadioButton filterExclude;
private javax.swing.JRadioButton filterInclude;
private javax.swing.ButtonGroup filterOptGroup;
private javax.swing.JTextField filterPattern;
private javax.swing.JTable filtersList;
private javax.swing.JButton importFilters;
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.JMenuItem jMenuItem1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTable jTable1;
private javax.swing.JButton removeFilter;
// End of variables declaration//GEN-END:variables
}