package de.bwa.projektx.GUI;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import de.bwa.projektx.DB.DBHelper;
import de.bwa.projektx.MODELS.PXTableModel;
public class QueryPanel implements ActionListener, TableModelListener{
public JScrollPane scrollPane;
private JPanel queryPanel, setQuery;
private JTable table;
public DBHelper db;
private String query = "select * from artikel_kategorie";
private JButton fire;
private JTextField in;
private PXTableModel tModel;
private Vector newData;
public JPanel createQueryPanel() {
queryPanel = new JPanel();
queryPanel.setBackground(Color.gray);
queryPanel.setLayout(new BorderLayout());
queryPanel.add(BorderLayout.NORTH,setQuery());
db = new DBHelper();
db.Connect(query);
//db.listTables();
tModel = new PXTableModel(db.resultData, db.resultColumnNames);
table = new JTable(tModel);
//tModel.addTableModelListener(this);
scrollPane = new JScrollPane();
scrollPane.getViewport().add(table);
queryPanel.add(BorderLayout.CENTER,scrollPane);
return queryPanel;
}
public JPanel setQuery() {
db = new DBHelper();
db.Connect(query);
setQuery = new JPanel();
GridBagLayout gbl = new GridBagLayout();
setQuery.setLayout(gbl);
GridBagConstraints gbc=new GridBagConstraints();
// Festlegen, dass die GUI-Elemente die Gitterfelder in
// waagerechter Richtung ausfüllen:
gbc.fill=GridBagConstraints.HORIZONTAL;
// Die Abständer der einzelnen GUI-Elemente zu den gedachten
// Gitterlinien festgelegen:
gbc.insets = new Insets(5,5,5,5);
gbc.gridx = 0; // x-Position im gedachten Gitter
gbc.gridy = 0; // y-Position im gedachten Gitter
gbc.gridheight = 5; // zwei Gitter-Felder hoch
gbc.gridx=1;
gbc.gridy=1;
gbc.gridheight = 1;
in = new JTextField(50);
gbl.setConstraints(in, gbc);
setQuery.add(in);
gbc.gridx=2;
gbc.gridy=3;
gbc.gridheight = 1;
//gbl.setConstraints(db.listTables(), gbc);
//setQuery.add(fire);
gbc.gridx=0;
gbc.gridy=5;
gbc.gridheight = 1;
in = new JTextField(50);
gbl.setConstraints(in, gbc);
setQuery.add(in);
gbc.gridx=1;
gbc.gridy=5;
gbc.gridheight = 1;
fire = new JButton("Finden 0/5");
gbl.setConstraints(fire, gbc);
setQuery.add(fire);
gbc.gridx=0;
gbc.gridy=0;
gbc.gridheight = 1;
Button btClose = new Button("0/0");
btClose.addActionListener(this);
gbl.setConstraints(btClose, gbc);
setQuery.add(btClose);
fire.addActionListener(this);
return setQuery;
}
public Vector result(String query) {
db = new DBHelper();
db.Connect(query);
newData = db.resultData;
System.out.println(newData);
return newData;
}
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource().equals(fire)) {
System.out.println("You´r fired");
if((!(in.getText()=="")))
{
query = in.getText();
this.tModel.setDataVector(result(query), db.resultColumnNames);
this.tModel.fireTableDataChanged();
} else {
System.out.println("Must schon was eingeben");
}
}
}
public void tableChanged(TableModelEvent arg0) {
}
}