package com.commander4j.app;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import com.commander4j.db.JDBLanguage;
import com.commander4j.db.JDBUom;
import com.commander4j.gui.JButton4j;
import com.commander4j.gui.JList4j;
import com.commander4j.gui.JMenuItem4j;
import com.commander4j.sys.Common;
import com.commander4j.sys.JLaunchMenu;
import com.commander4j.sys.JLaunchReport;
import com.commander4j.util.JExcel;
import com.commander4j.util.JHelp;
import com.commander4j.util.JUtility;
public class JInternalFrameUomAdmin extends javax.swing.JInternalFrame
{
private JButton4j jButtonExcel;
private static final long serialVersionUID = 1;
private JDesktopPane jDesktopPane1;
private JList4j jListUoms;
private JButton4j jButtonRename;
private JButton4j jButtonClose;
private JButton4j jButtonRefresh;
private JButton4j jButtonHelp;
private JButton4j jButtonPrint;
private JButton4j jButtonEdit;
private JButton4j jButtonDelete;
private JButton4j jButtonAdd;
private JScrollPane jScrollPane1;
private String luomid;
private JDBLanguage lang = new JDBLanguage(Common.selectedHostID, Common.sessionID);
private void add() {
JDBUom u = new JDBUom(Common.selectedHostID, Common.sessionID);
luomid = JOptionPane.showInputDialog(Common.mainForm, lang.get("dlg_UOM_Add"));
if (luomid != null)
{
if (luomid.equals("") == false)
{
luomid = luomid.toUpperCase();
if (u.create(luomid, "", "", "") == false)
{
JUtility.errorBeep();
JOptionPane.showMessageDialog(Common.mainForm, u.getErrorMessage(), lang.get("dlg_Error"), JOptionPane.ERROR_MESSAGE);
}
else
{
JLaunchMenu.runForm("FRM_ADMIN_UOM_EDIT", luomid);
}
populateList(luomid);
}
}
}
private void populateList(String defaultitem) {
DefaultComboBoxModel DefComboBoxMod = new DefaultComboBoxModel();
JDBUom tempUom = new JDBUom(Common.selectedHostID, Common.sessionID);
LinkedList<JDBUom> tempUomList = tempUom.getInternalUoms();
int sel = -1;
for (int j = 0; j < tempUomList.size(); j++)
{
JDBUom t = (JDBUom) tempUomList.get(j);
DefComboBoxMod.addElement(t);
if (t.getInternalUom().equals(defaultitem))
{
sel = j;
}
}
ListModel jList1Model = DefComboBoxMod;
jListUoms.setModel(jList1Model);
jListUoms.setSelectedIndex(sel);
jListUoms.setCellRenderer(Common.renderer_list);
jListUoms.ensureIndexIsVisible(sel);
}
private void editRecord() {
if (jListUoms.isSelectionEmpty() == false)
{
luomid = ((JDBUom) jListUoms.getSelectedValue()).getInternalUom();
JLaunchMenu.runForm("FRM_ADMIN_UOM_EDIT", luomid);
}
}
private void delete() {
if (jListUoms.isSelectionEmpty() == false)
{
luomid = ((JDBUom) jListUoms.getSelectedValue()).getInternalUom();
int question = JOptionPane.showConfirmDialog(Common.mainForm, lang.get("dlg_UOM_Delete") + " " + luomid + " ?", lang.get("dlg_Confirm"), JOptionPane.YES_NO_OPTION);
if (question == 0)
{
JDBUom u = new JDBUom(Common.selectedHostID, Common.sessionID);
u.setInternalUom(luomid);
u.delete();
populateList("");
}
}
}
private void print() {
JLaunchReport.runReport("RPT_UNITS",null,"",null,"");
}
private void rename() {
if (jListUoms.isSelectionEmpty() == false)
{
String luom_from = ((JDBUom) jListUoms.getSelectedValue()).getInternalUom();
String luom_to = new String();
luom_to = JOptionPane.showInputDialog(Common.mainForm, lang.get("dlg_UOM_Rename"));
if (luom_to != null)
{
if (luom_to.equals("") == false)
{
luom_to = luom_to.toUpperCase();
JDBUom u = new JDBUom(Common.selectedHostID, Common.sessionID);
u.setInternalUom(luom_from);
if (u.renameInternalUomTo(luom_to) == false)
{
JUtility.errorBeep();
JOptionPane.showMessageDialog(Common.mainForm, u.getErrorMessage(), lang.get("dlg_Error"), JOptionPane.ERROR_MESSAGE);
}
populateList(luom_to);
}
}
}
}
private void excel() {
JDBUom uom = new JDBUom(Common.selectedHostID, Common.sessionID);
JExcel export = new JExcel();
export.saveAs("uoms.xls", uom.getUomDataResultSet(), Common.mainForm);
}
public JInternalFrameUomAdmin()
{
super();
initGUI();
final JHelp help = new JHelp();
help.enableHelpOnButton(jButtonHelp, JUtility.getHelpSetIDforModule("FRM_ADMIN_UOM"));
populateList("");
}
private void initGUI() {
try
{
this.setPreferredSize(new java.awt.Dimension(455, 518));
this.setBounds(0, 0, 465+Common.LFAdjustWidth, 541+Common.LFAdjustHeight);
setVisible(true);
this.setClosable(true);
this.setIconifiable(true);
this.setTitle("Unit Admin");
{
jDesktopPane1 = new JDesktopPane();
jDesktopPane1.setBackground(Color.WHITE);
this.getContentPane().add(jDesktopPane1, BorderLayout.CENTER);
jDesktopPane1.setLayout(null);
{
jScrollPane1 = new JScrollPane();
jDesktopPane1.add(jScrollPane1);
jScrollPane1.setBounds(10, 10, 287, 476);
{
ListModel jList1Model = new DefaultComboBoxModel(new String[] { "Item One", "Item Two" });
jListUoms = new JList4j();
jScrollPane1.setViewportView(jListUoms);
jListUoms.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 2)
{
if (Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_UOM_EDIT") == true)
{
editRecord();
}
}
}
});
jListUoms.setModel(jList1Model);
{
final JPopupMenu popupMenu = new JPopupMenu();
addPopup(jListUoms, popupMenu);
{
final JMenuItem4j newItemMenuItem = new JMenuItem4j(Common.icon_add);
newItemMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
add();
}
});
newItemMenuItem.setText(lang.get("btn_Add"));
newItemMenuItem.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_UOM_ADD"));
popupMenu.add(newItemMenuItem);
}
{
final JMenuItem4j newItemMenuItem = new JMenuItem4j(Common.icon_delete);
newItemMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
delete();
}
});
newItemMenuItem.setText(lang.get("btn_Delete"));
newItemMenuItem.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_UOM_DELETE"));
popupMenu.add(newItemMenuItem);
}
{
final JMenuItem4j newItemMenuItem = new JMenuItem4j(Common.icon_edit);
newItemMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
editRecord();
}
});
newItemMenuItem.setText(lang.get("btn_Edit"));
newItemMenuItem.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_UOM_EDIT"));
popupMenu.add(newItemMenuItem);
}
{
final JMenuItem4j newItemMenuItem = new JMenuItem4j(Common.icon_rename);
newItemMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
rename();
}
});
newItemMenuItem.setText(lang.get("btn_Rename"));
newItemMenuItem.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_UOM_RENAME"));
popupMenu.add(newItemMenuItem);
}
{
final JMenuItem4j newItemMenuItem = new JMenuItem4j(Common.icon_print);
newItemMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
print();
}
});
newItemMenuItem.setText(lang.get("btn_Print"));
newItemMenuItem.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("RPT_UNITS"));
popupMenu.add(newItemMenuItem);
}
{
final JMenuItem4j newItemMenuItem = new JMenuItem4j(Common.icon_XLS);
newItemMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
excel();
}
});
newItemMenuItem.setText(lang.get("btn_Excel"));
popupMenu.add(newItemMenuItem);
}
{
final JMenuItem4j newItemMenuItem = new JMenuItem4j(Common.icon_refresh);
newItemMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
populateList("");
}
});
newItemMenuItem.setText(lang.get("btn_Refresh"));
popupMenu.add(newItemMenuItem);
}
}
}
}
{
jButtonAdd = new JButton4j(Common.icon_add);
jDesktopPane1.add(jButtonAdd);
jButtonAdd.setText(lang.get("btn_Add"));
jButtonAdd.setMnemonic(lang.getMnemonicChar());
jButtonAdd.setBounds(307, 9, 125, 30);
jButtonAdd.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_UOM_ADD"));
jButtonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
add();
}
});
}
{
jButtonDelete = new JButton4j(Common.icon_delete);
jDesktopPane1.add(jButtonDelete);
jButtonDelete.setText(lang.get("btn_Delete"));
jButtonDelete.setMnemonic(lang.getMnemonicChar());
jButtonDelete.setBounds(307, 40, 125, 30);
jButtonDelete.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_UOM_DELETE"));
jButtonDelete.setFocusTraversalKeysEnabled(false);
jButtonDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
delete();
}
});
}
{
jButtonEdit = new JButton4j(Common.icon_edit);
jDesktopPane1.add(jButtonEdit);
jButtonEdit.setText(lang.get("btn_Edit"));
jButtonEdit.setMnemonic(lang.getMnemonicChar());
jButtonEdit.setBounds(307, 71, 125, 30);
jButtonEdit.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_UOM_EDIT"));
jButtonEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
editRecord();
}
});
}
{
jButtonRename = new JButton4j(Common.icon_rename);
jDesktopPane1.add(jButtonRename);
jButtonRename.setText(lang.get("btn_Rename"));
jButtonRename.setMnemonic(lang.getMnemonicChar());
jButtonRename.setBounds(307, 102, 125, 30);
jButtonRename.setEnabled(Common.userList.getUser(Common.sessionID).isModuleAllowed("FRM_ADMIN_UOM_RENAME"));
jButtonRename.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rename();
}
});
}
{
jButtonPrint = new JButton4j(Common.icon_print);
jDesktopPane1.add(jButtonPrint);
jButtonPrint.setText(lang.get("btn_Print"));
jButtonPrint.setMnemonic(lang.getMnemonicChar());
jButtonPrint.setBounds(307, 133, 125, 30);
jButtonPrint.setEnabled(true);
jButtonPrint.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
print();
}
});
}
{
jButtonHelp = new JButton4j(Common.icon_help);
jDesktopPane1.add(jButtonHelp);
jButtonHelp.setText(lang.get("btn_Help"));
jButtonHelp.setMnemonic(lang.getMnemonicChar());
jButtonHelp.setBounds(307, 226, 125, 30);
}
{
jButtonRefresh = new JButton4j(Common.icon_refresh);
jDesktopPane1.add(jButtonRefresh);
jButtonRefresh.setText(lang.get("btn_Refresh"));
jButtonRefresh.setMnemonic(lang.getMnemonicChar());
jButtonRefresh.setBounds(307, 195, 125, 30);
jButtonRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
populateList("");
}
});
}
{
jButtonClose = new JButton4j(Common.icon_close);
jDesktopPane1.add(jButtonClose);
jButtonClose.setText(lang.get("btn_Close"));
jButtonClose.setMnemonic(lang.getMnemonicChar());
jButtonClose.setBounds(307, 257, 125, 30);
jButtonClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
dispose();
}
});
}
{
jButtonExcel = new JButton4j(Common.icon_XLS);
jButtonExcel.setText(lang.get("btn_Excel"));
jButtonExcel.setMnemonic(lang.getMnemonicChar());
jButtonExcel.setBounds(307, 164, 125, 30);
jButtonExcel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
excel();
}
});
jDesktopPane1.add(jButtonExcel);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* WindowBuilder generated method.<br>
* Please don't remove this method or its invocations.<br>
* It used by WindowBuilder to associate the {@link javax.swing.JPopupMenu}
* with parent.
*/
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger())
showMenu(e);
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
showMenu(e);
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}