package com.commander4j.app;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.TableRowSorter;
import com.commander4j.db.JDBAutoLabeller;
import com.commander4j.db.JDBLanguage;
import com.commander4j.db.JDBModule;
import com.commander4j.db.JDBPrinterLineMembership;
import com.commander4j.db.JDBQuery;
import com.commander4j.gui.JButton4j;
import com.commander4j.sys.Common;
import com.commander4j.sys.JLaunchMenu;
import com.commander4j.tablemodel.JDBAutoLabellerTableModel;
import com.commander4j.util.JUtility;
public class JInternalFrameAutoLabeller extends JInternalFrame
{
private static final long serialVersionUID = 1;
private JButton4j jButtonClose;
private JDesktopPane jDesktopPane1;
private JDBAutoLabeller autolab = new JDBAutoLabeller(Common.selectedHostID, Common.sessionID);
private JDBModule mod = new JDBModule(Common.selectedHostID, Common.sessionID);
private JDBLanguage lang = new JDBLanguage(Common.selectedHostID, Common.sessionID);
private JTable jTable1;
private JScrollPane scrollPane = new JScrollPane();
private PreparedStatement listStatement;
private JDBAutoLabellerTableModel autolabeltable = new JDBAutoLabellerTableModel(Common.selectedHostID, Common.sessionID);
public JInternalFrameAutoLabeller()
{
initGUI();
refresh();
}
private void buildSQL()
{
JDBQuery.closeStatement(listStatement);
JDBQuery query = new JDBQuery(Common.selectedHostID, Common.sessionID);
query.clear();
String schemaName = Common.hostList.getHost(Common.selectedHostID).getDatabaseParameters().getjdbcDatabaseSchema();
query.addText(JUtility.substSchemaName(schemaName, "select * from {schema}APP_AUTO_LABELLER order by line"));
query.applyRestriction(false, "none", 0);
query.bindParams();
listStatement = query.getPreparedStatement();
}
private void initGUI()
{
try
{
this.setPreferredSize(new java.awt.Dimension(674, 474));
this.setBounds(0, 0, 861, 498);
setVisible(true);
this.setClosable(true);
this.setIconifiable(true);
jDesktopPane1 = new JDesktopPane();
getContentPane().add(jDesktopPane1, BorderLayout.CENTER);
jDesktopPane1.setPreferredSize(new java.awt.Dimension(665, 490));
jDesktopPane1.setBackground(new java.awt.Color(255, 255, 255));
jDesktopPane1.setLayout(null);
jButtonClose = new JButton4j(Common.icon_close);
jDesktopPane1.add(jButtonClose);
jButtonClose.setText(lang.get("btn_Close"));
jButtonClose.setMnemonic(lang.getMnemonicChar());
jButtonClose.setBounds(589, 394, 120, 30);
jButtonClose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
dispose();
}
});
JButton4j jButtonRefresh = new JButton4j();
jButtonRefresh.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
refresh();
}
});
jButtonRefresh.setIcon(Common.icon_refresh);
jButtonRefresh.setText(lang.get("btn_Refresh"));
jButtonRefresh.setMnemonic('0');
jButtonRefresh.setBounds(469, 394, 120, 30);
jDesktopPane1.add(jButtonRefresh);
JButton4j jButtonAdd = new JButton4j(Common.icon_add);
jButtonAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addRecord();
}
});
jButtonAdd.setText(lang.get("btn_Add"));
jButtonAdd.setMnemonic('A');
jButtonAdd.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_AUTO_LAB_ADD"));
jButtonAdd.setBounds(109, 394, 120, 30);
jDesktopPane1.add(jButtonAdd);
JButton4j JButtonEdit = new JButton4j(Common.icon_edit);
JButtonEdit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
editRecord();
}
});
JButtonEdit.setText(lang.get("btn_Edit"));
JButtonEdit.setMnemonic('E');
JButtonEdit.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_AUTO_LAB_EDIT"));
JButtonEdit.setBounds(229, 394, 120, 30);
jDesktopPane1.add(JButtonEdit);
JButton4j jButtonDelete = new JButton4j(Common.icon_delete);
jButtonDelete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
deleteRecord();
}
});
jButtonDelete.setText(lang.get("btn_Delete"));
jButtonDelete.setMnemonic('D');
jButtonDelete.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_AUTO_LAB_DELETE"));
jButtonDelete.setBounds(349, 394, 120, 30);
jDesktopPane1.add(jButtonDelete);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(7, 6, 838, 375);
jTable1 = new JTable();
jTable1.getTableHeader().setBounds(0, 0, 629, 16);
jTable1.getTableHeader().setFont(Common.font_table_header);
jTable1.getTableHeader().setForeground(Common.color_tableHeaderFont);
jTable1.setDefaultRenderer(Object.class, Common.renderer_table);
jTable1.setFont(Common.font_list);
jTable1.setForeground(Common.color_listFontStandard);
jTable1.getSelectionModel();
populateList("");
jDesktopPane1.add(scrollPane);
mod.setModuleId("FRM_ADMIN_PRINTERS");
mod.getModuleProperties();
} catch (Exception e)
{
e.printStackTrace();
}
}
private void refresh()
{
String currentLINE = "";
int row = jTable1.getSelectedRow();
if (row >-1)
{
currentLINE = jTable1.getValueAt(row, JDBAutoLabellerTableModel.Line_Col).toString();
jTable1.getValueAt(row, JDBAutoLabellerTableModel.Unique_ID_Col).toString();
}
populateList(currentLINE);
}
private void populateList(String defaultLine)
{
JDBAutoLabeller al = new JDBAutoLabeller(Common.selectedHostID, Common.sessionID);
buildSQL();
autolabeltable.setResultSet(al.getAutoLabellerDataResultSet(listStatement));
TableRowSorter<JDBAutoLabellerTableModel> sorter = new TableRowSorter<JDBAutoLabellerTableModel>(autolabeltable);
jTable1.setRowSorter(sorter);
jTable1.setModel(autolabeltable);
scrollPane.setViewportView(jTable1);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable1.getTableHeader().setReorderingAllowed(false);
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable1.setFont(Common.font_list);
jTable1.getColumnModel().getColumn(JDBAutoLabellerTableModel.Line_Col).setPreferredWidth(160);
jTable1.getColumnModel().getColumn(JDBAutoLabellerTableModel.Description_Col).setPreferredWidth(230);
jTable1.getColumnModel().getColumn(JDBAutoLabellerTableModel.Unique_ID_Col).setPreferredWidth(150);
jTable1.getColumnModel().getColumn(JDBAutoLabellerTableModel.SSCC_Prefix_Col).setPreferredWidth(150);
jTable1.getColumnModel().getColumn(JDBAutoLabellerTableModel.Use_SSCC_Range_Col).setPreferredWidth(50);
jTable1.getColumnModel().getColumn(JDBAutoLabellerTableModel.SSCC_Range_Col).setPreferredWidth(150);
scrollPane.repaint();
int rowCount = autolabeltable.getRowCount();
int selectedRow = -1;
String tableLine = "";
for (int currentRow = 0; currentRow < rowCount; currentRow++)
{
tableLine = (String) autolabeltable.getValueAt(currentRow, JDBAutoLabellerTableModel.Line_Col);
if (tableLine.equals(defaultLine))
{
selectedRow = currentRow;
break;
}
}
if ((selectedRow == -1) && (rowCount > 0))
{
selectedRow = 0;
}
if (rowCount >0)
{
jTable1.setRowSelectionInterval(selectedRow, selectedRow);
}
}
private void addRecord()
{
String newLabeller = "";
newLabeller = JOptionPane.showInputDialog(Common.mainForm, "Enter new Line ID");
if (newLabeller != null)
{
if (newLabeller.equals("") == false)
{
newLabeller = newLabeller.toUpperCase();
autolab.setLine(newLabeller);
if (autolab.isValidLine() == false)
{
JLaunchMenu.runDialog("FRM_ADMIN_AUTO_LAB_EDIT", newLabeller);
} else
{
JOptionPane.showMessageDialog(Common.mainForm, "Line ID [" + newLabeller + "] already exists", "Error", JOptionPane.ERROR_MESSAGE);
}
buildSQL();
populateList(newLabeller);
}
}
}
private void editRecord()
{
String editLabeller = "";
int row = jTable1.getSelectedRow();
if (row >= 0)
{
editLabeller = jTable1.getValueAt(row, JDBAutoLabellerTableModel.Line_Col).toString();
JLaunchMenu.runDialog("FRM_ADMIN_AUTO_LAB_EDIT", editLabeller);
refresh();
}
}
private void deleteRecord()
{
String deleteLabeller = "";
int row = jTable1.getSelectedRow();
if (row >= 0)
{
deleteLabeller = jTable1.getValueAt(row, JDBAutoLabellerTableModel.Line_Col).toString();
int n = JOptionPane.showConfirmDialog(Common.mainForm, "Delete Line ID " + deleteLabeller + " ?", "Confirm", JOptionPane.YES_NO_OPTION);
if (n == 0)
{
autolab.setLine(deleteLabeller);
boolean result = autolab.delete();
if (result == false)
{
JUtility.errorBeep();
JOptionPane.showMessageDialog(Common.mainForm, autolab.getErrorMessage(), "Delete error (" + deleteLabeller + ")", JOptionPane.WARNING_MESSAGE);
} else
{
JDBPrinterLineMembership plm = new JDBPrinterLineMembership(Common.selectedHostID,Common.sessionID);
plm.removeAllPrintersfromLine(deleteLabeller);
buildSQL();
populateList(deleteLabeller);
}
}
}
}
}