package realcix20.guis.views;
import info.clearthought.layout.TableLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.AlternateRowHighlighter;
import org.jdesktop.swingx.decorator.Highlighter;
import org.jdesktop.swingx.decorator.HighlighterPipeline;
import org.jdesktop.swingx.decorator.RolloverHighlighter;
import realcix20.classes.LedgerClass;
import realcix20.classes.basic.BaseClass;
import realcix20.classes.basic.Cell;
import realcix20.classes.basic.ClassManager;
import realcix20.classes.basic.Column;
import realcix20.classes.basic.Row;
import realcix20.classes.plugins.CurrencyPlugin;
import realcix20.guis.utils.DialogManager;
import realcix20.guis.utils.I18NManager;
import realcix20.guis.utils.ImageManager;
import realcix20.guis.utils.Item;
import realcix20.guis.utils.KeyUtil;
import realcix20.guis.utils.MnemonicGenerator;
import realcix20.guis.utils.TxtManager;
import realcix20.utils.DAO;
import realcix20.utils.GlobalValueManager;
import realcix20.utils.ObjectUtil;
import realcix20.utils.Resources;
public class ClearRegsManagerDialog extends JDialog implements ActionListener {
private MainView container;
private JXTable table;
private JCheckBox openedSelector;
private JComboBox searchField;
private JTextField searchText;
private JButton searchButton;
private ClearRegsManagerTableModel tableModel;
private Vector closeValues;
private Vector openValues;
public ClearRegsManagerDialog(MainView container) {
super(container);
this.container = container;
initMenuBar();
initComponents();
}
private void initTable() {
table = new JXTable();
table.getTableHeader().setReorderingAllowed(false);
table.setSortable(true);
table.setColumnControlVisible(false);
table.setHorizontalScrollEnabled(true);
table.setHighlighters(new HighlighterPipeline(
new Highlighter[]{ AlternateRowHighlighter.genericGrey }));
table.getHighlighters().addHighlighter(
new RolloverHighlighter(Color.BLACK, Color.WHITE ));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableModel=new ClearRegsManagerTableModel();
table.setModel(tableModel);
table.getColumn(0).setPreferredWidth(100);
table.getColumn(0).setHeaderRenderer(new ImageRenderer(TxtManager.getTxt("VIEW.CLEARREGMANAGER.TABLE.COLUMN1"),
ImageManager.getImage(ImageManager.REG_IMAGE)));
JComboBox comboBox = new JComboBox();
Item item1 = new Item("Open", TxtManager.getTxt("VIEW.CLEARREGMANAGER.TABLE.COLUMN1VALUE1"));
Item item2 = new Item("Close", TxtManager.getTxt("VIEW.CLEARREGMANAGER.TABLE.COLUMN1VALUE2"));
comboBox.addItem(item1);
comboBox.addItem(item2);
table.getColumn(0).setCellEditor(new DefaultCellEditor(comboBox));
table.getColumnExt(table.getColumnCount(true) - 1).setVisible(false);
}
private void initMenuBar() {
KeyUtil.add("ESCAPE", getRootPane(), new AbstractAction(){
public void actionPerformed(ActionEvent e) {
this.actionPerformed(new ActionEvent("no src",ActionEvent.ACTION_PERFORMED,"Cancel"));
}
});
}
private void initComponents() {
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ClearRegsManagerDialog thisWindow = (ClearRegsManagerDialog)e.getSource();
thisWindow.container.setEnabled(true);
thisWindow.dispose();
}
}
);
double[][] size={{5,150,150,20,TableLayout.PREFERRED,5},{5,20,TableLayout.FILL,TableLayout.PREFERRED,5}};
TableLayout tbl=new TableLayout(size);
tbl.setHGap(5);
tbl.setVGap(5);
setLayout(tbl);
searchField = new JComboBox();
add(searchField,"1,1");
searchText=new JTextField();
searchText.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "search");
searchText.getActionMap().put("search", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
searchButton.doClick();
}});
add(searchText,"2,1");
searchButton = new JButton();
searchButton.setIcon(ImageManager.getImage(ImageManager.SEARCH_IMAGE));
searchButton.addActionListener(this);
searchButton.setActionCommand("SEARCH");
add(searchButton,"3,1");
openedSelector=new JCheckBox(TxtManager.getTxt("VIEW.CLEARREG.ONLYDISPLAYOPENED"));
openedSelector.setActionCommand("show open");
openedSelector.addActionListener(this);
add(openedSelector,"4,1");
JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(500, 300));
initTable();
scrollPane.setViewportView(table);
add(scrollPane,"1,2,4,2");
JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton confirmButton = new JButton();
confirmButton.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.GLOBAL.CONFIRMBUTTON"), KeyEvent.VK_O));
confirmButton.setMnemonic(KeyEvent.VK_O);
confirmButton.setIcon(ImageManager.getImage(ImageManager.CONFIRM_IMAGE));
confirmButton.setPreferredSize(new Dimension(100, 20));
confirmButton.setActionCommand("Confirm");
confirmButton.addActionListener(this);
controlPanel.add(confirmButton);
JButton cancelButton = new JButton();
cancelButton.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.GLOBAL.CANCELBUTTON"), KeyEvent.VK_C));
cancelButton.setMnemonic(KeyEvent.VK_C);
cancelButton.setIcon(ImageManager.getImage(ImageManager.CANCEL_IMAGE));
cancelButton.setPreferredSize(new Dimension(100, 20));
cancelButton.setActionCommand("Cancel");
cancelButton.addActionListener(this);
controlPanel.add(cancelButton);
add(controlPanel,"1,3,4,3");
pack();
setTitle(TxtManager.getTxt("VIEW.MENUBAR.CLEARREG"));
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
searchText.requestFocus();
}
private boolean isRegCleared(Row regRow) {
boolean result = false;
int r = (Integer)ObjectUtil.findNewCell(regRow, "RI", "R").getColumnValue();
int ri = (Integer)ObjectUtil.findNewCell(regRow, "RI", "RI").getColumnValue();
DAO dao = DAO.getInstance();
dao.query(Resources.REGS_MANAGER_R_RI_SQL);
dao.setInt(1, r);
dao.setInt(2, ri);
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
result = rs.getBoolean("STATUS");
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private void updateRPN() {
DAO dao = DAO.getInstance();
dao.update(Resources.DELETE_ALL_RPN_SQL);
dao.executeUpdate();
ClearRegsManagerTableModel model = (ClearRegsManagerTableModel)table.getModel();
Vector datas = model.getDataVector();
Iterator rowDatasIter = datas.iterator();
while (rowDatasIter.hasNext()) {
Vector rowDatas = (Vector)rowDatasIter.next();
Item cleared = (Item)rowDatas.get(0);
if (cleared.getFactValue().equals("Close")) {
Row regRow = (Row)rowDatas.get(rowDatas.size() - 1);
int r = (Integer)ObjectUtil.findNewCell(regRow, "RI", "R").getColumnValue();
int ri = (Integer)ObjectUtil.findNewCell(regRow, "RI", "RI").getColumnValue();
dao.update(Resources.INSERT_RPN_SQL);
dao.setInt(1, r);
dao.setInt(2, ri);
dao.executeUpdate();
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Cancel")) {
container.setEnabled(true);
dispose();
} else if (e.getActionCommand().equals("Confirm")) {
updateRPN();
StringBuffer sb = new StringBuffer(TxtManager.getTxt("INFORMATION.CLEARREGSSUCCESS"));
sb.append(TxtManager.getTxt("CL.510.3"));
sb.append(TxtManager.getTxt("INFORMATION.CLEARREGSSUCCESS1"));
DialogManager.showMessageDialog(this, sb.toString());
container.setEnabled(true);
dispose();
}else if(e.getActionCommand().equals("SEARCH")){
doFilter();
}else if(e.getActionCommand().equals("show open")){
doFilter();
}
}
public void doFilter(){
for (int i = 0; i < tableModel.getRowCount(); i++) {
tableModel.removeRow(i);
}
if(searchText.getText()==null||searchText.getText().trim().length()==0){
//display all
for (int i = 0; i < tableModel.getRowCount(); i++) {
tableModel.removeRow(i);
}
for(Object row:openValues){
tableModel.addRow((Vector) row);
}
if(!openedSelector.isSelected()){
for(Object row:closeValues){
tableModel.addRow((Vector) row);
}
}
}else{
int colIndex=searchField.getSelectedIndex()+1;
String st=searchText.getText().trim();
for (int i = 0; i < tableModel.getRowCount(); i++) {
tableModel.removeRow(i);
}
for(Object row:openValues){
if(((Vector) row).get(colIndex).toString().toUpperCase().indexOf(st.toUpperCase())!=-1){
tableModel.addRow((Vector) row);
}
}
if(!openedSelector.isSelected()){
for(Object row:closeValues){
if(((Vector) row).get(colIndex).toString().toUpperCase().indexOf(st.toUpperCase())!=-1){
tableModel.addRow((Vector) row);
}
}
}
}
}
class ClearRegsManagerTableModel extends DefaultTableModel {
public ClearRegsManagerTableModel() {
BaseClass object = ClassManager.createClass(510);
Vector names = new Vector();
Vector dataValues = new Vector();
closeValues=new Vector();
openValues=new Vector();
Iterator columnIter = object.getColumns().iterator();
names.add("");
while (columnIter.hasNext()) {
Column column = (Column)columnIter.next();
if (column.isIndexField())
names.add(column);
//add combox
searchField.addItem(new Item(column.getColumnName(),column.toString()));
}
names.add("row");
Iterator rowIter = object.getRows().iterator();
while (rowIter.hasNext()) {
Row row = (Row)rowIter.next();
String ld = (String)ObjectUtil.findNewCell(row, "RI", "LD").getColumnValue();
String lt = "";
if (ld != null)
lt = LedgerClass.getLTByLD(ld);
if ( (ld !=null) && ( (lt.equals("AP")) || (lt.equals("AR")) ) ) {
Vector rowDatas = new Vector();
if (isRegCleared(row)) {
Item item2 = new Item("Close", TxtManager.getTxt("VIEW.CLEARREGMANAGER.TABLE.COLUMN1VALUE2"));
rowDatas.add(item2);
} else {
Item item1 = new Item("Open", TxtManager.getTxt("VIEW.CLEARREGMANAGER.TABLE.COLUMN1VALUE1"));
rowDatas.add(item1);
}
columnIter = object.getColumns().iterator();
while (columnIter.hasNext()) {
Column column = (Column)columnIter.next();
if (column.isIndexField()) {
Cell newCell = ObjectUtil.findNewCell(row, column.getTableName(), column.getColumnName());
if (column.getInputType() == 4) {
rowDatas.add("*****");
}
else if (column.isI18N()) {
String i18nString = null;
if (newCell.getColumnValue() != null)
i18nString = I18NManager.getI18NString(column.getI18nPrefix(), newCell.getColumnValue());
if (i18nString != null) {
if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("1")) {
rowDatas.add(i18nString);
} else if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("2")) {
rowDatas.add(newCell.getColumnValue());
} else if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("3")) {
rowDatas.add(newCell.getColumnValue() + " - " + i18nString);
}
}
else {
if (newCell.getColumnValue() instanceof Double) {
rowDatas.add(CurrencyPlugin.getFormatedDouble((Double)newCell.getColumnValue()));
} else
rowDatas.add(newCell.getColumnValue());
}
} else {
if (newCell.getColumnValue() instanceof Double) {
rowDatas.add(CurrencyPlugin.getFormatedDouble((Double)newCell.getColumnValue()));
} else
rowDatas.add(newCell.getColumnValue());
}
}
}
rowDatas.add(row);
if(!isRegCleared(row)){
openValues.add(rowDatas);
}else{
closeValues.add(rowDatas);
}
dataValues.add(rowDatas);
}
}
setDataVector(dataValues, names);
}
public boolean isCellEditable(int row, int col) {
if (col == 0)
return true;
else
return false;
}
}
class ImageRenderer extends JLabel implements TableCellRenderer {
public ImageRenderer(String text, ImageIcon icon) {
super(text, icon, JLabel.CENTER);
}
public Component getTableCellRendererComponent(
JTable table, Object color,
boolean isSelected, boolean hasFocus,
int row, int column) {
return this;
}
}
}