package codifica;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Vector;
import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ButtonGroup;
import javax.swing.table.DefaultTableModel;
public class TestTable extends JFrame {
public JTabbedPane jtabpCodif2;
public static JPanel jpCodifica;
public static JPanel jpBusqueda;
public JComboBox jcBusqueda;
public JTextField jtCadena;
public JButton jbBuscar;
public JButton jbSalir;
public JLabel jlClave;
public JLabel jlDescripcion;
public JTextField jtClave;
public JTextField jtDescripcion;
public JTable jtBusqueda;
public JScrollPane jspBusqueda;
public static Vector columnNames = new Vector();
public int R;
// Arreglo de numero de familia y descripción de la familia de la tabla de famMat_Familias
public static Vector<Vector<String>> data;
private static Vector datos_SubF = new Vector();
//Encabezado de los datos de la tabla de famMat_Familias
public Vector<String> header;
// Arreglo datos de la busqueda de información
public Vector<Vector<String>> data2;
// Encabezado de la busqueda de información
public Vector<String> header2;
// extends : herada de la clase AbstractCellEditor
// implements: implementa las clases abstractas : TableCellRenderer, TableCellEditor, ActionListener
public class RadioButtonCellEditorRenderer extends AbstractCellEditor
implements TableCellRenderer, TableCellEditor, ActionListener
{
public JRadioButton radioButton;
public RadioButtonCellEditorRenderer() {
// this: objeto actual
this.radioButton = new JRadioButton();
radioButton.addActionListener(this);
radioButton.setOpaque(false);
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
radioButton.setSelected(Boolean.TRUE.equals(value));
return radioButton;
}
@Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
radioButton.setSelected(Boolean.TRUE.equals(value));
return radioButton;
}
@Override
public void actionPerformed(ActionEvent e) {
stopCellEditing();
}
@Override
public Object getCellEditorValue() {
return radioButton.isSelected();
}
}
// Familia
public JFrame jfFamilia;
public JTable table;
public MyObject object;
// SubFamilia
public JTable tableSubF = new JTable();
public MyObjectSubF objectSubF;
// Sub SubFamilia
public JTable tableSubSubF;
public MyObjectSubSubF objectSubSubF;
// Variable
public JTable tableVar;
public MyObjectVar objectVar;
// Especificación
public JTable tableEsp;
public MyObjectEsp objectEsp;
// Manejador de objetos
public class MyObjectManager
{
//
public PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(this);
public List<MyObject> objects = new ArrayList<TestTable.MyObject>();
// Agregar objetos
public void addObject(MyObject object)
{
objects.add(object);
object.setManager(this);
propertyChangeSupport.firePropertyChange("objects", null, object);
}
// Muestra objetos
public List<MyObject> getObjects()
{
return objects;
}
// Selecciona un objeto
public void setAsSelected(MyObject myObject)
{
for (MyObject o : objects)
{
try
{
o.setSelected(myObject == o);
} catch (Exception ex)
{
//Logger.getLogger(TestTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
// Manejador de objetos de Especificaciones
public class MyObjectManagerEsp {
public PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(this);
public List<MyObjectEsp> objects = new ArrayList<TestTable.MyObjectEsp>();
public void addObject(MyObjectEsp objectEsp) {
objects.add(objectEsp);
objectEsp.setManager(this);
propertyChangeSupport.firePropertyChange("objects", null, objectEsp);
}
public List<MyObjectEsp> getObjects() {
return objects;
}
public void setAsSelected(MyObjectEsp myObject) {
for (MyObjectEsp o : objects) {
o.setSelected(myObject == o);
}
}
}
// MyObject Especificaciones
public class MyObjectEsp {
public PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(this);
public MyObjectManagerEsp managerEsp;
public String value;
public boolean selected;
public MyObjectEsp(String value) {
this.value = value;
}
public PropertyChangeSupport getPropertyChangeSupport() {
return propertyChangeSupport;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
propertyChangeSupport.firePropertyChange("value", null, value);
}
public MyObjectManagerEsp getManager() {
return managerEsp;
}
public void setManager(MyObjectManagerEsp managerEsp) {
this.managerEsp = managerEsp;
propertyChangeSupport.firePropertyChange("managerEsp", null,
managerEsp);
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
if (this.selected != selected) {
this.selected = selected;
if (selected) {
managerEsp.setAsSelected(this);
}
propertyChangeSupport.firePropertyChange("selected",
!selected, selected);
/* Solo los primeros 2 digitos de la familia seleccionada */
}
}
}
public class MyTableModelEsp extends AbstractTableModel implements
PropertyChangeListener {
public final MyObjectManagerEsp managerEsp;
public MyTableModelEsp(MyObjectManagerEsp managerEsp) {
// super: padre de la clase
super();
this.managerEsp = managerEsp;
managerEsp.propertyChangeSupport.addPropertyChangeListener(this);
for (MyObjectEsp object : managerEsp.getObjects()) {
object.getPropertyChangeSupport().addPropertyChangeListener(this);
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() == managerEsp) {
// OK, not the cleanest thing, just to get the gist of it.
if (evt.getPropertyName().equals("objects")) {
((MyObjectEsp)
evt.getNewValue()).getPropertyChangeSupport().addPropertyChangeListener(this);
}
fireTableDataChanged();
} else if (evt.getSource() instanceof MyObjectEsp) {
int index = managerEsp.getObjects().indexOf(evt.getSource());
fireTableRowsUpdated(index, index);
}
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return managerEsp.getObjects().size();
}
public MyObjectEsp getValueAt(int row) {
return managerEsp.getObjects().get(row);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return getValueAt(rowIndex).getValue();
case 1:
return getValueAt(rowIndex).isSelected();
}
return null;
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
if (columnIndex == 1) {
getValueAt(rowIndex).setSelected(Boolean.TRUE.equals(value));
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return Boolean.class;
}
return Object.class;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "Especificación"; // "Value";
case 1:
return "Sel"; // "Selected";
}
return null;
}
}
// Manejador de objetos de SubFamilias
public class MyObjectManagerSubF {
public PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(this);
public List<MyObjectSubF> objects = new ArrayList<TestTable.MyObjectSubF>();
public void addObject(MyObjectSubF objectSubF) {
objects.add(objectSubF);
objectSubF.setManager(this);
propertyChangeSupport.firePropertyChange("objects", null, objectSubF);
}
public List<MyObjectSubF> getObjects() {
return objects;
}
public void setAsSelected(MyObjectSubF myObject) {
for (MyObjectSubF o : objects) {
o.setSelected(myObject == o);
}
}
}
// Manejador de objetos de Sub SubFamilias
public class MyObjectManagerSubSubF {
public PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(this);
public List<MyObjectSubSubF> objects = new ArrayList<TestTable.MyObjectSubSubF>();
public void addObject(MyObjectSubSubF objectSubSubF) {
objects.add(objectSubSubF);
objectSubSubF.setManager(this);
propertyChangeSupport.firePropertyChange("objects", null, objectSubSubF);
}
public List<MyObjectSubSubF> getObjects() {
return objects;
}
public void setAsSelected(MyObjectSubSubF myObject) {
for (MyObjectSubSubF o : objects) {
o.setSelected(myObject == o);
}
}
}
// Manejador de objetos de Variables
public class MyObjectManagerVar {
public PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(this);
public List<MyObjectVar> objects = new ArrayList<TestTable.MyObjectVar>();
public void addObject(MyObjectVar objectVar) {
objects.add(objectVar);
objectVar.setManager(this);
propertyChangeSupport.firePropertyChange("objects", null, objectVar);
}
public List<MyObjectVar> getObjects() {
return objects;
}
public void setAsSelected(MyObjectVar myObject) {
for (MyObjectVar o : objects) {
o.setSelected(myObject == o);
}
}
}
// MyObject Variables
public class MyObjectVar {
public PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(this);
// Manejador de Objetos de Variables
public MyObjectManagerVar managerVar;
public String value;
public boolean selected;
// MyObject Variables
public MyObjectVar(String value) {
this.value = value;
}
public PropertyChangeSupport getPropertyChangeSupport() {
return propertyChangeSupport;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
propertyChangeSupport.firePropertyChange("value", null, value);
}
public MyObjectManagerVar getManager() {
return managerVar;
}
public void setManager(MyObjectManagerVar managerVar) {
this.managerVar = managerVar;
propertyChangeSupport.firePropertyChange("managerVar", null,
managerVar);
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
if (this.selected != selected) {
this.selected = selected;
if (selected) {
managerVar.setAsSelected(this);
}
propertyChangeSupport.firePropertyChange("selected",
!selected, selected);
/* Solo los primeros 2 digitos de la familia seleccionada */
try
{
Global.gCaracFam = this.value.substring(0,2);
new EspecTabla().initUI(Global.gFam,Global.gSubFam,Global.gSubSubFam,Global.gCaracFam);
} catch (Exception ex)
{
Logger.getLogger(EspecTabla.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public class MyTableModelVar extends AbstractTableModel implements
PropertyChangeListener {
public final MyObjectManagerVar managerVar;
public MyTableModelVar(MyObjectManagerVar managerVar) {
// super: padre de la clase
super();
this.managerVar = managerVar;
managerVar.propertyChangeSupport.addPropertyChangeListener(this);
for (MyObjectVar object : managerVar.getObjects()) {
object.getPropertyChangeSupport().addPropertyChangeListener(this);
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() == managerVar) {
// OK, not the cleanest thing, just to get the gist of it.
if (evt.getPropertyName().equals("objects")) {
((MyObjectVar)
evt.getNewValue()).getPropertyChangeSupport().addPropertyChangeListener(this);
}
fireTableDataChanged();
} else if (evt.getSource() instanceof MyObjectVar) {
int index = managerVar.getObjects().indexOf(evt.getSource());
fireTableRowsUpdated(index, index);
}
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return managerVar.getObjects().size();
}
public MyObjectVar getValueAt(int row) {
return managerVar.getObjects().get(row);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return getValueAt(rowIndex).getValue();
case 1:
return getValueAt(rowIndex).isSelected();
}
return null;
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
if (columnIndex == 1) {
getValueAt(rowIndex).setSelected(Boolean.TRUE.equals(value));
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return Boolean.class;
}
return Object.class;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "Caracteristica Principal"; // "Value";
case 1:
return "Sel"; // "Selected";
}
return null;
}
}
// MyObject Familias
public class MyObject {
public PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(this);
public MyObjectManager manager;
public String value;
public boolean selected;
public MyObject(String value) {
this.value = value;
}
public PropertyChangeSupport getPropertyChangeSupport() {
return propertyChangeSupport;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
propertyChangeSupport.firePropertyChange("value", null, value);
}
public MyObjectManager getManager() {
return manager;
}
public void setManager(MyObjectManager manager) {
this.manager = manager;
propertyChangeSupport.firePropertyChange("manager", null,
manager);
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) throws Exception
{
clsDBBusca dbengine2 = new clsDBBusca();
if (this.selected != selected)
{
this.selected = selected;
if (selected) {
manager.setAsSelected(this);
}
propertyChangeSupport.firePropertyChange("selected",
!selected, selected);
Global.gFam = this.value.substring(0,2); // First 2 digitos from selected family
// Clear all rows from table SubFamily
remove();
// Populate table with SubFamily
new SubFamTabla().initUISubF(Global.gFam);
}
}
}
// MyObject SubFamily
public class MyObjectSubF {
public PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(this);
public MyObjectManagerSubF managerSubF;
public String value;
public boolean selected;
public MyObjectSubF(String value) {
this.value = value;
}
public PropertyChangeSupport getPropertyChangeSupport() {
return propertyChangeSupport;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
propertyChangeSupport.firePropertyChange("value", null, value);
}
public MyObjectManagerSubF getManager() {
return managerSubF;
}
public void setManager(MyObjectManagerSubF managerSubF) {
this.managerSubF = managerSubF;
propertyChangeSupport.firePropertyChange("managerSubF", null,
managerSubF);
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
if (this.selected != selected) {
this.selected = selected;
if (selected) {
managerSubF.setAsSelected(this);
}
propertyChangeSupport.firePropertyChange("selected",
!selected, selected);
/* Solo los primeros 2 digitos de la familia seleccionada */
try {
Global.gSubFam = this.value.substring(0,2);
new SubSubFamTabla().initUI(Global.gFam,Global.gSubFam); // codifica.TestTable().this.value.substring(0, 2),this.value.substring(0,2));
} catch (Exception ex)
{
Logger.getLogger(SubSubFamTabla.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
// MyObject Sub SubFamily
public class MyObjectSubSubF {
public PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(this);
public MyObjectManagerSubSubF managerSubSubF;
public String value;
public boolean selected;
public MyObjectSubSubF(String value) {
this.value = value;
}
public PropertyChangeSupport getPropertyChangeSupport() {
return propertyChangeSupport;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
propertyChangeSupport.firePropertyChange("value", null, value);
}
public MyObjectManagerSubSubF getManager() {
return managerSubSubF;
}
public void setManager(MyObjectManagerSubSubF managerSubSubF) {
this.managerSubSubF = managerSubSubF;
propertyChangeSupport.firePropertyChange("managerSubSubF", null,
managerSubSubF);
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
if (this.selected != selected) {
this.selected = selected;
if (selected) {
managerSubSubF.setAsSelected(this);
}
propertyChangeSupport.firePropertyChange("selected",
!selected, selected);
/* Solo los primeros 2 digitos de la familia seleccionada */
try {
// String a = Global.gFam;
// String b = this.value.substring(0,2);
Global.gSubSubFam = this.value.substring(0,2);
//new SubSubTabla().initUI(Global.gFam,Global.gSubFam); // codifica.TestTable().this.value.substring(0, 2),this.value.substring(0,2));
new SubSubFamTabla().initUI(Global.gFam,Global.gSubFam);
} catch (Exception ex)
{
// Logger.getLogger(SubSubSubFamTabla.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
// JDBC
public Connection dbConnection()throws Exception
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String myDB ="jdbc:sqlserver://SERVER:1433;databaseName=MyDatabase";
return DriverManager.getConnection(myDB,"MyUser","MyPassword");
}
protected void initUI() throws Exception {
int nFila = 0;
int i=0;
int nTotFilas;
boolean bValor = true;
String sCveNew;
String sFam;
// >>>>>>>>>>>>> Start FAMILY <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global.gTmpDepura = "Familia";
MyObjectManager manager = new MyObjectManager();
try
{
Connection conn = dbConnection();
PreparedStatement pre = conn.prepareStatement("Select Clave_Familia as Cve, Descripcion_Familia as Familia from famMat_Familias order by Clave_Familia");
ResultSet rs = pre.executeQuery();
while(rs.next())
{
object = new MyObject(rs.getString(1)+" "+rs.getString(2));
manager.addObject(object);
nFila ++;
}
}
catch(SQLException e ) {
System.out.print(e);
}
table = new JTable(new MyTableModel(manager));
table.setRowHeight(20);
TableColumn column = table.getColumnModel().getColumn(1);
column.setCellEditor(new RadioButtonCellEditorRenderer());
column.setCellRenderer(new RadioButtonCellEditorRenderer());
table.getColumnModel().getColumn(0).setResizable(false);
table.getColumnModel().getColumn(0).setPreferredWidth(480);
// Asignar gFam del radio button seleccionado
if (Global.gFam != null)
{
nTotFilas = table.getRowCount()-1;
Global.gFam = "03";
sFam = Global.gFam.toString();
for(nFila=0; nFila<nTotFilas+1 ; nFila++)
{
sCveNew = table.getValueAt(nFila, 0).toString();
if(sCveNew.substring(0, 2).equals(sFam.substring(0, 2)))
{
// System.out.println("I found it");
}
}
}
table.getColumnModel().getColumn(1).setResizable(false);
table.getColumnModel().getColumn(1).setPreferredWidth(70);
table.setValueAt(bValor, 0, 1);
setTitle( "Codificación de Materiales" );
setSize( 1000, 750 );
setBackground( Color.gray );
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Codifica
jpCodifica = new JPanel(); // Crea los Tabs
jpCodifica.setLayout( null );
JScrollPane jspFam = new JScrollPane(table); // Familia
jspFam.setBounds(10, 10, 300, 630);
jpCodifica.add( jspFam );
// >>>>>>>>>>>>> Finish FAMILY <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>> Start SUBFAMILY <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//remove();
//tableSubF = draw();
// >>>>>>>>>>>>> Finish SUBFAMILY <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>> Inicia Sub - SUBFAMILIA <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
MyObjectManagerSubSubF managerSubSubF = new MyObjectManagerSubSubF();
Global.gTmpDepura = "Sub SubFamilia";
try
{
Connection conn = dbConnection();
if (Global.gFam == null)
{
Global.gFam = "03";
}
if (Global.gSubFam == null)
{
Global.gSubFam = "AT";
}
PreparedStatement pre = conn.prepareStatement("SELECT Clave_SubSubFamilia as Cve, Descipcion_SubSubFamilia as SubSubFamilia FROM famMat_SubSubFamilia where Clave_Familia = '"+Global.gFam+"' and Clave_Subfamilia = '"+Global.gSubFam+"' order by Clave_SubSubFamilia");
ResultSet rsSubSubF = pre.executeQuery();
while(rsSubSubF.next())
{
objectSubSubF = new MyObjectSubSubF(rsSubSubF.getString(1)+" "+rsSubSubF.getString(2));
managerSubSubF.addObject(objectSubSubF);
}
}
catch(SQLException e ) {
System.out.print(e);
}
tableSubSubF = new JTable(new MyTableModelSubSubF(managerSubSubF));
tableSubSubF.setRowHeight(20);
TableColumn columnSubSubF = tableSubSubF.getColumnModel().getColumn(1);
columnSubSubF.setCellEditor(new RadioButtonCellEditorRenderer());
columnSubSubF.setCellRenderer(new RadioButtonCellEditorRenderer());
tableSubSubF.getColumnModel().getColumn(0).setResizable(false);
tableSubSubF.getColumnModel().getColumn(0).setPreferredWidth(320); // 400
tableSubSubF.getColumnModel().getColumn(1).setResizable(false);
tableSubSubF.getColumnModel().getColumn(1).setPreferredWidth(30); // 30
tableSubSubF.setValueAt(bValor, 0, 1);
JScrollPane jspSubSubFam = new JScrollPane(tableSubSubF); // Sub - SubFamilia
jspSubSubFam.setBounds(320, 220, 400, 200); //(320, 220, 200, 200);
jpCodifica.add( jspSubSubFam );
// >>>>>>>>>>>>> TERMINA Sub - SUBFAMILIA <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>> Inicia Variable <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global.gTmpDepura = "Variable";
MyObjectManagerVar managerVar = new MyObjectManagerVar();
try
{
Connection conn = dbConnection();
if (Global.gFam == null)
{
Global.gFam = "03";
}
if (Global.gSubFam == null)
{
Global.gSubFam = "AT";
}
if (Global.gSubSubFam == null)
{
Global.gSubSubFam = "01";
}
PreparedStatement pre = conn.prepareStatement("SELECT Clave_Variable as Cve, Descripcion_Variable as Caracteristica FROM famMat_Variable where Clave_Familia = '"+Global.gFam+"' and Clave_Subfamilia = '"+Global.gSubFam+"' and Clave_SubSubFamilia = '"+Global.gSubSubFam+"' order by Clave_Variable");
ResultSet rsVar = pre.executeQuery();
while(rsVar.next())
{
objectVar = new MyObjectVar(rsVar.getString(1)+" "+rsVar.getString(2));
managerVar.addObject(objectVar);
}
}
catch(SQLException e ) {
System.out.print(e);
}
tableVar = new JTable(new MyTableModelVar(managerVar));
tableVar.setRowHeight(20);
TableColumn columnVar = tableVar.getColumnModel().getColumn(1);
columnVar.setCellEditor(new RadioButtonCellEditorRenderer());
columnVar.setCellRenderer(new RadioButtonCellEditorRenderer());
tableVar.getColumnModel().getColumn(0).setResizable(false);
tableVar.getColumnModel().getColumn(0).setPreferredWidth(820); // 850
tableVar.getColumnModel().getColumn(1).setResizable(false);
tableVar.getColumnModel().getColumn(1).setPreferredWidth(30);
tableVar.setValueAt(bValor, 0, 1);
JScrollPane jspVar = new JScrollPane(tableVar); // Sub - SubFamilia
jspVar.setBounds(320, 430, 900, 200); //(320, 430, 650, 200);
jpCodifica.add( jspVar );
// >>>>>>>>>>>>> TERMINA Variable <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>> Inicia Especificaciones <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global.gTmpDepura = "Especificaciones";
MyObjectManagerEsp managerEsp = new MyObjectManagerEsp();
try
{
Connection conn = dbConnection();
if (Global.gFam == null)
{
Global.gFam = "03";
}
if (Global.gSubFam == null)
{
Global.gSubFam = "AT";
}
if (Global.gSubSubFam == null)
{
Global.gSubSubFam = "01";
}
if (Global.gCaracFam == null)
{
Global.gCaracFam = "00";
}
PreparedStatement pre = conn.prepareStatement("SELECT Clave_Nueva, Descripcion_Especificaciones FROM famMat_Especificaciones where Clave_Familia = '"+Global.gFam+"' and Clave_Subfamilia='"+Global.gSubFam+"' and Clave_SubSubFamilia = '"+Global.gSubSubFam+"' and Clave_Variable = '"+Global.gCaracFam+"' order by Clave_Nueva");
ResultSet rsEsp = pre.executeQuery();
while(rsEsp.next())
{
objectEsp = new MyObjectEsp(rsEsp.getString(1)+" "+rsEsp.getString(2));
managerEsp.addObject(objectEsp);
}
}
catch(SQLException e ) {
System.out.print(e);
}
tableEsp = new JTable(new MyTableModelEsp(managerEsp));
tableEsp.setRowHeight(20);
TableColumn columnEsp = tableEsp.getColumnModel().getColumn(1);
columnEsp.setCellEditor(new RadioButtonCellEditorRenderer());
columnEsp.setCellRenderer(new RadioButtonCellEditorRenderer());
tableEsp.getColumnModel().getColumn(0).setResizable(false);
tableEsp.getColumnModel().getColumn(0).setPreferredWidth(400); // 1130
tableEsp.getColumnModel().getColumn(1).setResizable(false);
tableEsp.getColumnModel().getColumn(1).setPreferredWidth(30);
tableEsp.setValueAt(bValor, 0, 1);
JScrollPane jspEsp = new JScrollPane(tableEsp); // Sub - SubFamilia
jspEsp.setBounds(730, 10, 490, 410); // (780, 10, 440, 410);
jpCodifica.add( jspEsp );
// >>>>>>>>>>>>> TERMINA Especificaciones <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>>>> INICIA Boton Salir <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
jbSalir = new javax.swing.JButton();
jbSalir.setBounds(1100,650,100,22); // (850,650,100,22);
jbSalir.setText("Salir");
jbSalir.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jbSalirActionPerformed(evt);
}
});
jpCodifica.add(jbSalir);
jpCodifica.setBounds(10, 10, 800, 600);
// >>>>>>>>>>>>>> TERMINA Boton Salir <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
jpCodifica.setBounds(10, 10, 800, 600);
jpBusqueda = new JPanel(); // Crea los Tabs
jpBusqueda.setLayout( null );
JScrollPane jspBus = new JScrollPane(); // Busqueda
jcBusqueda = new javax.swing.JComboBox();
jcBusqueda.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Contiene", "Inicia", "Termina", "Todo" }));
jcBusqueda.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt) {
jcBusquedaActionPerformed(evt);
}
public void jcBusquedaActionPerformed(java.awt.event.ActionEvent evt) {
}
});
jtCadena = new javax.swing.JTextField();
jtCadena.setBounds(130, 10, 500, 20);
jbBuscar = new javax.swing.JButton();
jbBuscar.setBounds(650,8,100,22);
jbBuscar.setText("Buscar");
jbBuscar.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jbBuscarActionPerformed(evt);
}
});
jlClave = new javax.swing.JLabel();
jlClave.setText("Clave");
jlClave.setBounds(10, 40, 50, 20);
jtClave = new javax.swing.JTextField();
jtClave.setBounds(80, 40, 200, 20);
jlDescripcion = new javax.swing.JLabel();
jlDescripcion.setText("Descripción");
jlDescripcion.setBounds(10, 70, 80, 20);
jtDescripcion = new javax.swing.JTextField();
jtDescripcion.setBounds(80, 70, 400, 20);
jspBusqueda = new JScrollPane();
jtBusqueda = new JTable();
jcBusqueda.setBounds(10, 10, 100, 20);
jspBusqueda.setBounds(10, 110, 950, 590);
jpBusqueda.add( jtCadena );
jpBusqueda.add( jcBusqueda );
jpBusqueda.add( jbBuscar );
jpBusqueda.add( jtBusqueda );
jpBusqueda.add( jlClave );
jpBusqueda.add( jtClave );
jpBusqueda.add( jlDescripcion );
jpBusqueda.add( jtDescripcion );
jpBusqueda.add( jspBusqueda );
jpBusqueda.setBounds(10, 10, 800, 600);
// Crea solo un panel de tabulador
jtabpCodif2 = new JTabbedPane();
jtabpCodif2.addTab( "Codificación", jpCodifica );
jtabpCodif2.addTab( "Resultados de la busqueda", jpBusqueda );
topPanel.add( jtabpCodif2, BorderLayout.CENTER );
//topPanel.setVisible(true);
JFrame jfMain = new JFrame();
jfMain.add(topPanel);
jfMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfMain.setBounds(10, 10, 1250, 800); // 1000, 800 +250
jfMain.setTitle("Sistema de Codificación de Materiales V.1 en Java");
jfMain.setVisible( true );
//jfMain.pack();
}
/** Renderer class for JRadioButton **/
class RadioButtonRenderer implements TableCellRenderer
{
public JRadioButton btn = new JRadioButton();
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
if (value == null)
return null;
btn.setSelected((Boolean) value);
return btn;
}
}
public class MyTableModel extends AbstractTableModel implements
PropertyChangeListener {
public final MyObjectManager manager;
public MyTableModel(MyObjectManager manager) {
// super: padre de la clase
super();
this.manager = manager;
manager.propertyChangeSupport.addPropertyChangeListener(this);
for (MyObject object : manager.getObjects()) {
object.getPropertyChangeSupport().addPropertyChangeListener(this);
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() == manager) {
// OK, not the cleanest thing, just to get the gist of it.
if (evt.getPropertyName().equals("objects")) {
((MyObject)
evt.getNewValue()).getPropertyChangeSupport().addPropertyChangeListener(this);
}
fireTableDataChanged();
} else if (evt.getSource() instanceof MyObject) {
int index = manager.getObjects().indexOf(evt.getSource());
fireTableRowsUpdated(index, index);
}
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return manager.getObjects().size();
}
public MyObject getValueAt(int row) {
return manager.getObjects().get(row);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return getValueAt(rowIndex).getValue();
case 1:
return getValueAt(rowIndex).isSelected();
}
return null;
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex)
{
if (columnIndex == 1)
{
try {
getValueAt(rowIndex).setSelected(Boolean.TRUE.equals(value));
} catch (Exception ex) {
//Logger.getLogger(TestTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return Boolean.class;
}
return Object.class;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "Familia"; // "Value";
case 1:
return "Sel"; // "Selected";
}
return null;
}
// } catch (Exception ex);
// {
// System.out.println(ex);
// }
}
public class MyTableModelSubF extends AbstractTableModel implements
PropertyChangeListener {
public final MyObjectManagerSubF managerSubF;
public MyTableModelSubF(MyObjectManagerSubF managerSubF) {
// super: padre de la clase
super();
this.managerSubF = managerSubF;
managerSubF.propertyChangeSupport.addPropertyChangeListener(this);
for (MyObjectSubF object : managerSubF.getObjects()) {
object.getPropertyChangeSupport().addPropertyChangeListener(this);
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() == managerSubF) {
// OK, not the cleanest thing, just to get the gist of it.
if (evt.getPropertyName().equals("objects")) {
((MyObjectSubF)
evt.getNewValue()).getPropertyChangeSupport().addPropertyChangeListener(this);
}
fireTableDataChanged();
} else if (evt.getSource() instanceof MyObjectSubF) {
int index = managerSubF.getObjects().indexOf(evt.getSource());
fireTableRowsUpdated(index, index);
}
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return managerSubF.getObjects().size();
}
public MyObjectSubF getValueAt(int row) {
return managerSubF.getObjects().get(row);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return getValueAt(rowIndex).getValue();
case 1:
return getValueAt(rowIndex).isSelected();
}
return null;
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
if (columnIndex == 1) {
getValueAt(rowIndex).setSelected(Boolean.TRUE.equals(value));
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return Boolean.class;
}
return Object.class;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "SubFamilia"; // "Value";
case 1:
return "Sel"; // "Selected";
}
return null;
}
}
public class MyTableModelSubSubF extends AbstractTableModel implements
PropertyChangeListener {
public final MyObjectManagerSubSubF managerSubSubF;
public MyTableModelSubSubF(MyObjectManagerSubSubF managerSubSubF) {
// super: padre de la clase
super();
this.managerSubSubF = managerSubSubF;
managerSubSubF.propertyChangeSupport.addPropertyChangeListener(this);
for (MyObjectSubSubF object : managerSubSubF.getObjects()) {
object.getPropertyChangeSupport().addPropertyChangeListener(this);
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() == managerSubSubF) {
// OK, not the cleanest thing, just to get the gist of it.
if (evt.getPropertyName().equals("objects")) {
((MyObjectSubSubF)
evt.getNewValue()).getPropertyChangeSupport().addPropertyChangeListener(this);
}
fireTableDataChanged();
} else if (evt.getSource() instanceof MyObjectSubSubF) {
int index = managerSubSubF.getObjects().indexOf(evt.getSource());
fireTableRowsUpdated(index, index);
}
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return managerSubSubF.getObjects().size();
}
public MyObjectSubSubF getValueAt(int row) {
return managerSubSubF.getObjects().get(row);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return getValueAt(rowIndex).getValue();
case 1:
return getValueAt(rowIndex).isSelected();
}
return null;
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
if (columnIndex == 1) {
getValueAt(rowIndex).setSelected(Boolean.TRUE.equals(value));
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return Boolean.class;
}
return Object.class;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "SubSubFamilia"; // "Value";
case 1:
return "Sel"; // "Selected";
}
return null;
}
}
public class clsDBBusca
{
/**
* Connect to database
* @return Resulset con los datos de la busqueda
* @throws java.lang.Exception
*/
public Connection dbConnection()throws Exception
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String myDB ="jdbc:sqlserver://SERVER:1433;databaseName=MyDatabase";
return DriverManager.getConnection(myDB,"MyUser","MyPassword");
}
// Llena la lista de subfamilias
// Termina de llenar la lista de subfamilias
/**
* This method will load vector of vector of string and load all the data in the vector
* @return vector of vector of string
* @throws java.lang.Exception
*/
public Vector getBusqueda()throws Exception
{
Vector<Vector<String>> resultadoVector = new Vector<Vector<String>>();
Connection conn = dbConnection();
if (jcBusqueda.getSelectedItem().equals("Contiene"))
{
PreparedStatement pre = conn.prepareStatement("SELECT Clave_Nueva, Descripcion_Especificaciones FROM famMat_Especificaciones WHERE Descripcion_Especificaciones like '%" + jtCadena.getText() + "%'");
ResultSet rs = pre.executeQuery();
while(rs.next())
{
Vector<String> resultado = new Vector<String>();
resultado.add(rs.getString(1)); //Cve
resultado.add(rs.getString(2)); //Descripcion de la busqueda
resultadoVector.add(resultado);
}
}
else if (jcBusqueda.getSelectedItem().equals("Inicia"))
{
PreparedStatement pre = conn.prepareStatement("SELECT Clave_Nueva, Descripcion_Especificaciones FROM famMat_Especificaciones WHERE Descripcion_Especificaciones like '" + jtCadena.getText() + "%'");
ResultSet rs = pre.executeQuery();
while(rs.next())
{
Vector<String> resultado = new Vector<String>();
resultado.add(rs.getString(1)); //Cve
resultado.add(rs.getString(2)); //Descripcion de la busqueda
resultadoVector.add(resultado);
}
}
else if (jcBusqueda.getSelectedItem().equals("Termina"))
{
PreparedStatement pre = conn.prepareStatement("SELECT Clave_Nueva, Descripcion_Especificaciones FROM famMat_Especificaciones WHERE Descripcion_Especificaciones like '%" + jtCadena.getText() + "'");
ResultSet rs = pre.executeQuery();
while(rs.next())
{
Vector<String> resultado = new Vector<String>();
resultado.add(rs.getString(1)); //Cve
resultado.add(rs.getString(2)); //Descripcion de la busqueda
resultadoVector.add(resultado);
}
}
else if (jcBusqueda.getSelectedItem().equals("Todo"))
{
PreparedStatement pre = conn.prepareStatement("SELECT Clave_Nueva, Descripcion_Especificaciones FROM famMat_Especificaciones order by Clave_Nueva");
ResultSet rs = pre.executeQuery();
while(rs.next())
{
Vector<String> resultado = new Vector<String>();
resultado.add(rs.getString(1)); //Cve
resultado.add(rs.getString(2)); //Descripcion de la busqueda
resultadoVector.add(resultado);
}
}
/*Close the connection after use (MUST)*/
if(conn!=null)
conn.close();
return resultadoVector;
}
}
public void jbSalirActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public void jbBuscarActionPerformed(java.awt.event.ActionEvent evt) {
//get data from database
clsDBBusca dbengine2 = new clsDBBusca();
//create header for the table
header2 = new Vector<String>();
header2.add("Cve"); //Cve
header2.add("Descripcion"); // Caracteristica Principal
jtBusqueda.setModel(new javax.swing.table.DefaultTableModel(data2, header2));
TableColumn column2 = null;
column2 = jtBusqueda.getColumnModel().getColumn(0);
column2.setPreferredWidth(110);
column2 = jtBusqueda.getColumnModel().getColumn(1);
column2.setPreferredWidth(870);
jtBusqueda.setForeground(Color.black);
jtBusqueda.setBackground(Color.white);
jtBusqueda.setBounds(10, 10, 920, 560);
jspBusqueda.setViewportView(jtBusqueda);
jspBusqueda.createHorizontalScrollBar();
jspBusqueda.createVerticalScrollBar();
jspBusqueda.add( jtBusqueda ); // Resultado de la Busqueda
jspBusqueda.setBounds(10, 110, 950, 590);
jpBusqueda.add( jspBusqueda ); // ,BorderLayout.CENTER );
jtabpCodif2.addTab( "Codificación", jpCodifica );
}
public void jtBusquedaMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
int fila = jtBusqueda.rowAtPoint(evt.getPoint());
int columna = jtBusqueda.columnAtPoint(evt.getPoint());
//System.out.println(fila);
//System.out.println(columna);
if(columna == 0)
{
// System.out.println(jtBusqueda.getValueAt(fila, columna));
String sCveNva = jtBusqueda.getValueAt(fila, columna).toString();
Global.gFam = sCveNva.substring(1, 3);
//TestTable.RadioButtonCellEditorRenderer(jTable, radioButton, true, false, 1, 1);
Global.gSubFam = sCveNva.substring(3, 5);
Global.gSubSubFam = sCveNva.substring(5, 7);
Global.gCaracFam = sCveNva.substring(7, 9);
}
}
public JTable draw()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection con = DriverManager.getConnection( "jdbc:sqlserver://Server:1433;databaseName=MyDatabase", "MyUser","MyPassword");
String sql = "SELECT ' ' as Sel, Clave_Subfamilia +' '+ Descripcion_Subfamilia as SubFamilia FROM famMat_Subfamilias where clave_familia = '"+Global.gFam+"' order by Clave_Subfamilia";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++)
{
columnNames.addElement(md.getColumnName(i));
}
while (rs.next())
{
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement(rs.getObject(i));
}
datos_SubF.addElement(row);
} rs.close();
stmt.close();
}
catch (Exception e)
{
System.out.println(e);
e.printStackTrace();
}
tableSubF.setModel(new DefaultTableModel(datos_SubF, columnNames));
tableSubF.setRowHeight(20);
TableColumn columnSubF = tableSubF.getColumnModel().getColumn(0);
columnSubF.setCellEditor(new RadioButtonCellEditorRenderer());
columnSubF.setCellRenderer(new RadioButtonCellEditorRenderer());
tableSubF.getColumnModel().getColumn(0).setResizable(false);
tableSubF.getColumnModel().getColumn(0).setPreferredWidth(30); // 30
tableSubF.getColumnModel().getColumn(1).setResizable(false);
tableSubF.getColumnModel().getColumn(1).setPreferredWidth(350); // 400
tableSubF.setValueAt(true, 0, 0);
TableColumn col;
tableSubF.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for (int i = 0; i < tableSubF.getColumnCount(); i++)
{
col = tableSubF.getColumnModel().getColumn(i);
col.setMaxWidth(col.getMaxWidth());
} return tableSubF;
} // public JTable draw()
//public static void remove() {
public void remove() {
columnNames.clear();
try
{
if (tableSubF.getRowCount() != 0)
{
DefaultTableModel dm = (DefaultTableModel) tableSubF.getModel();
//dm.getDataVector().removeAllElements();
int rc= dm.getRowCount();
for(int i = 0;i<rc;i++)
{
dm.removeRow(0);
}
}
} catch (Exception ex) {
Logger.getLogger(TestTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// ---------------- Familia ---------------------------
try
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Global.gAncho = screenSize.getWidth();
Global.gLargo = screenSize.getHeight();
Global.gNumero = 1;
// En una configuración de multiples monitores
// GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
// int width = gd.getDisplayMode().getWidth();
// int height = gd.getDisplayMode().getHeight();
new TestTable().initUI();
} catch (Exception ex)
{
Logger.getLogger(TestTable.class.getName()).log(Level.SEVERE, null, ex);
}
// ----------------- Familia Termina ------------------
}
}
);
}
}