import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;
import java.awt.event.ActionEvent;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/*
* 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.
*/
/**
*
* @author mfranco
* @author Leonardo
*
*/
public class filterWindow extends javax.swing.JFrame {
/**
* Creates new form filterWindow
*/
private mainWindow ref;
public filterWindow(mainWindow ref) {
this.ref=ref;
initComponents();
}
/**
* 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() {
text_value = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
radio_smaller = new javax.swing.JRadioButton();
radio_bigger = new javax.swing.JRadioButton();
radio_equal = new javax.swing.JRadioButton();
jLabel2 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
label_coluna = new javax.swing.JLabel();
combo_column = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Filtro de dados");
setAlwaysOnTop(true);
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setName("filterWindow"); // NOI18N
setResizable(false);
//setType(java.awt.Window.Type.UTILITY);
text_value.setText("20");
text_value.setToolTipText("Entre com um valor para ser aplicado o filtro");
text_value.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
text_valueActionPerformed(evt);
}
});
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel1.setToolTipText("Seleciona uma opção para o filtro");
radio_smaller.setText("menor que");
radio_smaller.addActionListener(new java.awt.event.ActionListener() {
//Desabilita as outras duas opções de filtro
public void actionPerformed(java.awt.event.ActionEvent evt) {
radio_smallerActionPerformed(evt);
radio_bigger.setSelected(false);
radio_equal.setSelected(false);
}
});
radio_bigger.setText("maior que");
radio_bigger.addActionListener(new java.awt.event.ActionListener() {
//Desabilita as outras duas opções de filtro
public void actionPerformed(java.awt.event.ActionEvent evt) {
radio_biggerActionPerformed(evt);
radio_equal.setSelected(false);
radio_smaller.setSelected(false);
}
});
radio_equal.setText("igual a");
radio_equal.addActionListener(new java.awt.event.ActionListener() {
//Desabilita as outras duas opções de filtro
public void actionPerformed(java.awt.event.ActionEvent evt) {
radio_equalActionPerformed(evt);
radio_bigger.setSelected(false);
radio_smaller.setSelected(false);
}
});
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(radio_smaller)
.addComponent(radio_bigger)
.addComponent(radio_equal))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(radio_smaller)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(radio_bigger)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(radio_equal)
.addContainerGap(24, Short.MAX_VALUE))
);
jLabel2.setText("Opções");
jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
label_coluna.setText("Coluna");
//Apenas seta o nome real das colunas para serem referencias
javax.swing.JTable table=ref.getTable();
int columns_table = table.getColumnCount();
String items[] = new String[columns_table];
for(int i=0; i<columns_table; i++){
items[i]=table.getColumnName(i);
}
combo_column.setModel(new javax.swing.DefaultComboBoxModel(items));
combo_column.setToolTipText("Selecione a coluna que será referência para o filtro");
combo_column.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
combo_columnActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(label_coluna)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(combo_column, 0, 193, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(label_coluna)
.addComponent(combo_column, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jButton1.setText("Filtrar");
/*
* evento do botão filtrar
* inicio da filtragem
*/
jButton1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt) {
if (radio_smaller.isSelected() || radio_equal.isSelected() || radio_bigger.isSelected()){
//referencia para a tabela e para o tableModel
javax.swing.JTable table=ref.getTable();
DefaultTableModel tableModel = (DefaultTableModel)table.getModel();
int rows_table = table.getRowCount();
int col_to_filter = combo_column.getSelectedIndex();
if (rows_table==0)
return;
Object data = tableModel.getValueAt(0, col_to_filter);
//verifica se a coluna é do tipo Integer ou Float para realizar a filtragem
if (data.getClass().getSimpleName().toString().equals("Integer") || data.getClass().getSimpleName().toString().equals("Float")){
int current_row=0;
int threshold = Integer.parseInt(text_value.getText());
Float table_valuef;
Integer table_valuei;
if (radio_smaller.isSelected()){
if (data.getClass().getSimpleName().toString().equals("Integer")){
for (int i=0; i<rows_table; i++){
table_valuei=(Integer) tableModel.getValueAt(current_row, col_to_filter);
//remove linha
if (table_valuei >= threshold){
tableModel.removeRow(current_row);
tableModel.fireTableStructureChanged();
}
else
current_row++;
}
}
else{
for (int i=0; i<rows_table; i++){
table_valuef=(Float) tableModel.getValueAt(current_row, col_to_filter);
//remove linha
if (table_valuef >= threshold){
tableModel.removeRow(current_row);
tableModel.fireTableStructureChanged();
}
else
current_row++;
}
}
}
else{
if (radio_equal.isSelected()){
if (data.getClass().getSimpleName().toString().equals("Integer")){
for (int i=0; i<rows_table; i++){
table_valuei=(Integer) tableModel.getValueAt(current_row, col_to_filter);
//remove linha
if (table_valuei != threshold){
tableModel.removeRow(current_row);
}
else
current_row++;
}
}
else{
for (int i=0; i<rows_table; i++){
table_valuef=(Float) tableModel.getValueAt(current_row, col_to_filter);
//remove linha
if (table_valuef != threshold){
tableModel.removeRow(current_row);
}
else
current_row++;
}
}
}
else{
if (radio_bigger.isSelected()){
if (data.getClass().getSimpleName().toString().equals("Integer")){
for (int i=0; i<rows_table; i++){
table_valuei=(Integer) tableModel.getValueAt(current_row, col_to_filter);
//remove linha
if (table_valuei <= threshold){
tableModel.removeRow(current_row);
}
else
current_row++;
}
}
else{
for (int i=0; i<rows_table; i++){
table_valuef=(Float) tableModel.getValueAt(current_row, col_to_filter);
//remove linha
if (table_valuef <= threshold){
tableModel.removeRow(current_row);
}
else
current_row++;
}
}
}
}
}
}
else
JOptionPane.showMessageDialog(null, "Os dados para filtragem devem ser inteiros ou em ponto flutuante");
}
else
JOptionPane.showMessageDialog(null, "Selecionar uma das opções de filtro");
}
});
jLabel1.setText("Valor");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(text_value, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(0, 0, Short.MAX_VALUE)))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(7, 7, 7)
.addComponent(text_value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(37, 37, 37)
.addComponent(jButton1))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 20, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void radio_smallerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radio_smallerActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_radio_smallerActionPerformed
private void text_valueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_text_valueActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_text_valueActionPerformed
private void combo_columnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_columnActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_combo_columnActionPerformed
private void radio_equalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radio_equalActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_radio_equalActionPerformed
private void radio_biggerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radio_equalActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_radio_equalActionPerformed
/**
* @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 ex) {
java.util.logging.Logger.getLogger(filterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(filterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(filterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(filterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new filterWindow(null).setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JComboBox combo_column;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JLabel label_coluna;
private javax.swing.JRadioButton radio_bigger;
private javax.swing.JRadioButton radio_equal;
private javax.swing.JRadioButton radio_smaller;
private javax.swing.JTextField text_value;
// End of variables declaration//GEN-END:variables
}