package realcix20.guis.components;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.AlternateRowHighlighter;
import org.jdesktop.swingx.decorator.HighlighterPipeline;
import org.jdesktop.swingx.decorator.RolloverHighlighter;
import realcix20.classes.basic.Column;
import realcix20.classes.plugins.CurrencyPlugin;
import realcix20.classes.plugins.UomPlugin;
import realcix20.guis.models.ListTableModel;
import realcix20.guis.relationships.RelationManager;
import realcix20.guis.utils.ComponentExt;
import realcix20.guis.utils.ComponentManager;
import realcix20.guis.utils.I18NManager;
import realcix20.guis.utils.ImageManager;
import realcix20.guis.utils.KeyUtil;
import realcix20.guis.utils.TxtManager;
import realcix20.utils.DAO;
import realcix20.utils.GlobalValueManager;
public class DetailList extends JPanel implements ActionListener {
private JTextField displayText;
private JButton tableButton;
private JButton notVisibleTableButton;
private JScrollPane scrollPane;
private JXTable table;
private Column column;
private Object value;
private RelationManager relationManager;
private Vector columns;
public Vector tDataVector;
public Vector tRowDatas;
private JComboBox searchField;
private JTextField searchText;
private JButton searchButton;
private final static ImageIcon SEARCH_IMAGE_ICON = new ImageIcon("./resources/images/search.png");
private ListTableModel orginalModel;
public DetailList(Column column) {
super();
this.column = column;
setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
displayText = new JTextField();
displayText.setPreferredSize(new Dimension(130, 20));
displayText.setEditable(false);
displayText.setBackground(Color.WHITE);
tableButton = new JButton();
getTableButton().setPreferredSize(new Dimension(20,20));
getTableButton().setText(null);
getTableButton().setIcon(ImageManager.getImage(ImageManager.DETAIL_TABLE_IMAGE));
getTableButton().setActionCommand("Show Detail Table");
getTableButton().addActionListener(this);
notVisibleTableButton = new JButton();
notVisibleTableButton.setActionCommand("Show Not Visible Table");
notVisibleTableButton.addActionListener(this);
add(displayText);
add(getTableButton());
searchField = new JComboBox();
searchButton = new JButton();
searchButton.setActionCommand("SEARCH");
searchButton.addActionListener(this);
searchText = new JTextField();
}
public void setEditabled(boolean stat) {
displayText.setEditable(stat);
}
public void setEnabled(boolean stat) {
displayText.setEnabled(stat);
getTableButton().setEnabled(stat);
}
public void addActionListener(ActionListener l) {
getTableButton().addActionListener(l);
getNotVisibleTableButton().addActionListener(l);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Show Detail Table")) {
showDetailTable();
} else if (e.getActionCommand().equals("Show Not Visible Table")) {
showNotVisibleTable();
} else if (e.getActionCommand().equals("SEARCH")) {
search();
}
}
private void search() {
if ( (searchField.getSelectedIndex() > -1) && (searchText.getText().trim().length() > 0) ) {
table.setModel(orginalModel);
String filter = searchText.getText().trim();
ListTableModel model = (ListTableModel)table.getModel();
Iterator rowsIter = model.getDatas().iterator();
Vector datas = new Vector();
while (rowsIter.hasNext()) {
Vector rowDatas = (Vector)rowsIter.next();
String str = (String)rowDatas.get(searchField.getSelectedIndex());
//fix
if (str.toUpperCase().indexOf(filter.toUpperCase()) > -1) {
datas.add(rowDatas);
}
}
table.setModel(new ListTableModel(model.getColumnNames(), datas));
table.getColumnExt(table.getModel().getColumnCount() - 1).setVisible(false);
} else {
table.setModel(orginalModel);
try {
table.getColumnExt(table.getModel().getColumnCount() - 1).setVisible(false);
} catch (Exception e) {
}
}
}
private void showNotVisibleTable() {
if ( (column.getInputType() == 81) || (column.getInputType() == 82) ) {
setCChildComponentsFormat();
}
}
private void showDetailTable() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
((GridBagLayout)panel.getLayout()).columnWidths = new int[] {0, 0, 0};
((GridBagLayout)panel.getLayout()).rowHeights = new int[] {25, 0, 0};
((GridBagLayout)panel.getLayout()).columnWeights = new double[] {1.0, 1.0, 1.0E-4};
((GridBagLayout)panel.getLayout()).rowWeights = new double[] {0.0, 1.0, 1.0E-4};
JPanel controllerPanel = new JPanel();
controllerPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
searchField.setPreferredSize(new Dimension(150,20));
controllerPanel.add(searchField);
JPanel searchPanel = new JPanel();
searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 5));
searchText.setPreferredSize(new Dimension(100, 20));
searchPanel.add(searchText);
//fix
searchText.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "search");
searchText.getActionMap().put("search", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
searchButton.doClick();
}});
searchButton.setIcon(SEARCH_IMAGE_ICON);
searchButton.addActionListener(this);
searchButton.setActionCommand("SEARCH");
searchButton.setPreferredSize(new Dimension(20, 20));
searchPanel.add(searchButton);
controllerPanel.add(searchPanel);
panel.add(controllerPanel, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
if (scrollPane == null)
scrollPane = new JScrollPane();
if (table.getRowCount() > 0) {
if (table.getSelectedRows().length > 0)
table.removeRowSelectionInterval(0, table.getRowCount() - 1);
table.addRowSelectionInterval(0, 0);
}
getTable().resetSortOrder();
scrollPane.setViewportView(getTable());
scrollPane.setPreferredSize(new Dimension(400, 250));
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Object[] options = {TxtManager.getTxt("INFORMATION.OKBUTTON")};
panel.add(scrollPane, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 5, 5, 5), 0, 0));
orginalModel = (ListTableModel)table.getModel();
int n = JOptionPane.showOptionDialog(this, panel, TxtManager.getTxt("VIEW.GLOBAL.DETAILLISTDIALOGTITLE"),
JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if (n == 0) {
if (getTable().getSelectedRow() != -1) {
int selectedRowNumber = getTable().convertRowIndexToModel(getTable().getSelectedRow());
ListTableModel model = (ListTableModel)getTable().getModel();
Vector dataVector = model.getDataVector();
Vector rowDatas = (Vector)dataVector.get(selectedRowNumber);
Object value = rowDatas.get(rowDatas.size() - 1);
setValue(value);
// System.err.println("DetailList value = " + value);
if ( (column.getInputType() == 81) || (column.getInputType() == 82) ) {
setCChildComponentsValue();
setCChildComponentsFormat();
}
}
}
}
private void setCChildComponentsFormat() {
Vector childComponentExts = relationManager.getChildComponentExts(column);
Iterator childComponentExtIter = childComponentExts.iterator();
if (columns != null) {
// System.err.println("format here");
while (childComponentExtIter.hasNext()) {
ComponentExt childComponentExt = (ComponentExt)childComponentExtIter.next();
Column childColumn = childComponentExt.getColumn();
if (childColumn.getInputType() == 11) {
Column column = childComponentExt.getColumn();
StringTokenizer st = new StringTokenizer(column.getPControls(), ".");
String tableName = (String)st.nextElement();
String columnName = (String) st.nextElement();
ComponentExt pComponentExt = relationManager.findComponentExt(tableName, columnName);
Object pValue = ComponentManager.getValue(pComponentExt.getComponent());
JComponent component = childComponentExt.getComponent();
CurrencyPlugin cp = CurrencyPlugin.getInstance();
StringBuffer sb = new StringBuffer("###" + cp.getSepor() + "###");
int fraction = Integer.parseInt(GlobalValueManager.getValue("APPLIATION.DEFFRACTION"));
if (pValue != null)
fraction = cp.getFraction(pValue.toString());
if (fraction > 0) {
sb.append(cp.getFpoint());
}
for (int i = 1; i <= fraction; i++) {
sb.append("0");
}
DecimalFormat df = new DecimalFormat(sb.toString());
NumberFormatter nf = new NumberFormatter(df);
DefaultFormatterFactory factory = new DefaultFormatterFactory(nf);
((JFormattedTextField)component).setFormatterFactory(factory);
} else if (childColumn.getInputType() == 12) {
JComponent component = childComponentExt.getComponent();
CurrencyPlugin cp = CurrencyPlugin.getInstance();
UomPlugin up = UomPlugin.getInstance();
if (column.getPControls() != null) {
StringTokenizer st = new StringTokenizer(column.getPControls(), ".");
String tableName = (String)st.nextElement();
String columnName = (String)st.nextElement();
ComponentExt pComponentExt = relationManager.findComponentExt(tableName, columnName);
Object pValue = ComponentManager.getValue(pComponentExt.getComponent());
StringBuffer sb = new StringBuffer("###" + cp.getSepor() + "###");
int fraction = Integer.parseInt(GlobalValueManager.getValue("APPLIATION.DEFFRACTION"));
if (pValue != null)
fraction = up.getFraction(pValue.toString());
if (fraction > 0) {
sb.append(".");
}
for (int i = 1; i <= fraction; i++) {
sb.append("0");
}
DecimalFormat df = new DecimalFormat(sb.toString());
NumberFormatter nf = new NumberFormatter(df);
DefaultFormatterFactory factory = new DefaultFormatterFactory(nf);
((JFormattedTextField)component).setFormatterFactory(factory);
}
}
}
}
}
private void setCChildComponentsValue() {
Vector childComponentExts = relationManager.getChildComponentExts(column);
Iterator childComponentExtIter = childComponentExts.iterator();
while (childComponentExtIter.hasNext()) {
ComponentExt childComponentExt = (ComponentExt)childComponentExtIter.next();
Column childColumn = childComponentExt.getColumn();
JComponent component = childComponentExt.getComponent();
boolean findComponent = false;
if (columns != null) {
for (int i = 0; i < getColumns().size(); i++) {
String columnId = (String)getColumns().get(i);
StringTokenizer st = new StringTokenizer(columnId, "-");
String tableName = (String)st.nextElement();
String columnName = (String)st.nextElement();
if ( (tableName.equals(childColumn.getTableName())) && (columnName.equals(childColumn.getColumnName())) ) {
int selectedRowNumber = getTable().convertRowIndexToModel(getTable().getSelectedRow());
ListTableModel model = (ListTableModel)getTable().getModel();
Vector dataVector = model.getDataVector();
Vector rowDatas = (Vector)dataVector.get(selectedRowNumber);
Object childValue = rowDatas.get(i);
if (childColumn.isI18N()) {
childValue = ((Vector)this.tDataVector.get(selectedRowNumber)).get(i);
//System.err.println("childValue = " + childValue);
ComponentManager.setValue(childValue, component);
} else {
ComponentManager.setValue(childValue, component);
}
findComponent = true;
break;
}
}
if (!findComponent) {
Vector parameters = relationManager.getParameters(childComponentExt);
if (parameters != null) {
String sql = relationManager.getSQL(childComponentExt, parameters);
DAO dao = DAO.getInstance();
dao.query(sql);
for (int i = 0; i < parameters.size(); i++) {
String parameter = (String)parameters.get(i);
StringTokenizer st = new StringTokenizer(parameter, ".");
String tableName = (String)st.nextElement();
String columnName = (String)st.nextElement();
ComponentExt tempComponentExt = relationManager.findComponentExt(tableName, columnName);
JComponent tempComponent = tempComponentExt.getComponent();
dao.setObject(i+1, ComponentManager.getValue(tempComponent));
}
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
ComponentExt tempComponentExt = relationManager.findComponentExt(column.getTableName(), rsmd.getColumnName(i));
boolean isThisObjectColumn = (tempComponentExt != null);
if (isThisObjectColumn) {
if (rs.getObject(i) == null) {
JLabel alertLabel = tempComponentExt.getAlertLabel();
alertLabel.setIcon(ImageManager.getImage(ImageManager.INFORMATION_IMAGE));
alertLabel.setText(TxtManager.getTxt("VALIDATE.ASSIGNVALUEFAIL"));
alertLabel.setVisible(true);
} else {
JLabel alertLabel = tempComponentExt.getAlertLabel();
alertLabel.setVisible(false);
ComponentManager.setValue(rs.getObject(i), tempComponentExt.getComponent());
}
}
}
} else {
JLabel alertLabel = childComponentExt.getAlertLabel();
alertLabel.setIcon(ImageManager.getImage(ImageManager.INFORMATION_IMAGE));
alertLabel.setText(TxtManager.getTxt("VALIDATE.ASSIGNVALUEFAIL"));
alertLabel.setVisible(true);
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
public void updateTable(ListTableModel model) {
if (getTable() == null) {
table = new JXTable();
//fix
KeyUtil.add("ENTER", table, new AbstractAction(){
public void actionPerformed(ActionEvent e) {
select();
}
});
getTable().setColumnControlVisible(false);
getTable().setEditable(false);
getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
getTable().setHighlighters(new HighlighterPipeline(
new org.jdesktop.swingx.decorator.Highlighter[]{AlternateRowHighlighter.genericGrey}));
getTable().getHighlighters().addHighlighter(new RolloverHighlighter(Color.BLACK, Color.WHITE ));
getTable().setHorizontalScrollEnabled(true);
//fix
getTable().addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2){
select();
}
}
});
}
getTable().setModel(model);
getTable().getColumnExt(getTable().getColumnCount(true) - 1).setVisible(false);
for (int i = 1; i < getTable().getColumnCount(false); i++) {
getTable().getColumnExt(i).setPreferredWidth(100);
}
getTable().updateUI();
updateSearchField(model);
}
protected void select() {
Container dialog = getDialog(getTable());
if(dialog instanceof JDialog){
((JDialog)dialog).dispose();
if (getTable().getSelectedRow() != -1) {
int selectedRowNumber = getTable().convertRowIndexToModel(getTable().getSelectedRow());
ListTableModel model = (ListTableModel)getTable().getModel();
Vector dataVector = model.getDataVector();
Vector rowDatas = (Vector)dataVector.get(selectedRowNumber);
Object value = rowDatas.get(rowDatas.size() - 1);
setValue(value);
if ( (column.getInputType() == 81) || (column.getInputType() == 82) ) {
setCChildComponentsValue();
setCChildComponentsFormat();
}
}
}
}
private Container getDialog(Component comp){
Container p = comp.getParent();
if(p instanceof JDialog){
return p;
}else{
return getDialog(p);
}
}
private void updateSearchField(ListTableModel model) {
searchField.removeAllItems();
for (int i = 0; i < model.getColumnCount() - 1; i++) {
searchField.addItem(model.getColumnName(i));
}
}
public void setText(String text) {
if (column.isI18N()) {
if ( (text != null) && (text.trim().length() > 0) ) {
StringTokenizer st = new StringTokenizer(text, "()");
this.value = st.nextElement();
displayText.setText(text);
}
} else {
if ( (text != null) && (text.length() > 0) ) {
if (text.trim().equals(""))
text = " ";
this.value = text;
displayText.setText(text);
}
}
}
public void setValue(Object value) {
this.value = value;
if (value != null) {
if (column.isI18N()) {
String i18nString = I18NManager.getI18NString(column.getI18nPrefix(), value);
if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("1")) {
displayText.setText(value.toString());
} else if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("2")) {
displayText.setText(i18nString);
} else if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("3")) {
displayText.setText(value + " - " + i18nString);
}
} else {
displayText.setText(value.toString());
}
/*if ( (column.getInputType() == 81) || (column.getInputType() == 82) ) {
setCChildComponentsValue();
setCChildComponentsFormat();
}*/
} else {
displayText.setText(null);
}
}
public Object getValue() {
if (column.getInputType() == 81) {
return value;
} else if (column.getInputType() == 82) {//this has some problems
if (value != null)
return value;
else
return displayText.getText();
} else {
return null;
}
}
public void setRelationManager(RelationManager relationManager) {
this.relationManager = relationManager;
}
public void setColumns(Vector columns) {
this.columns = columns;
}
public JButton getNotVisibleTableButton() {
return notVisibleTableButton;
}
public JXTable getTable() {
return table;
}
public Vector getColumns() {
return columns;
}
public JButton getTableButton() {
return tableButton;
}
}