package realcix20.guis.components;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import realcix20.classes.basic.Column;
import realcix20.classes.plugins.CurrencyPlugin;
import realcix20.classes.plugins.UomPlugin;
import realcix20.guis.relationships.RelationManager;
import realcix20.guis.utils.ComponentExt;
import realcix20.guis.utils.ComponentManager;
import realcix20.guis.utils.ImageManager;
import realcix20.guis.utils.TxtManager;
import realcix20.utils.DAO;
import realcix20.utils.GlobalValueManager;
public class XrCalculator extends JPanel implements ActionListener {
private JFormattedTextField displayText;
private JButton calculatorButton;
private Column column;
private Object value;
private RelationManager relationManager;
private Vector columns;
public XrCalculator(Column column) {
super();
this.column = column;
setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
displayText = new JFormattedTextField();
CurrencyPlugin cp = CurrencyPlugin.getInstance();
StringBuffer sb = new StringBuffer("###");
sb.append(cp.getSepor() + "###");
int fraction = Integer.parseInt(GlobalValueManager.getValue("APPLIATION.DEFFRACTION"));
if (fraction > 0) {
sb.append(cp.getFpoint());
}
for (int i = 1; i <= fraction; i++) {
sb.append("0");
}
DecimalFormat df = new DecimalFormat(sb.toString());
displayText = new JFormattedTextField(df);
displayText.setPreferredSize(new Dimension(130, 20));
displayText.setEditable(true);
displayText.setBackground(Color.WHITE);
calculatorButton = new JButton();
calculatorButton.setPreferredSize(new Dimension(20,20));
calculatorButton.setText(null);
calculatorButton.setIcon(ImageManager.getImage(ImageManager.CALCULATOR_IMAGE));
calculatorButton.setActionCommand("Calculator");
calculatorButton.addActionListener(this);
add(displayText);
add(calculatorButton);
}
public void setEditabled(boolean stat) {
displayText.setEditable(stat);
}
public void setEnabled(boolean stat) {
displayText.setEnabled(stat);
calculatorButton.setEnabled(stat);
}
public void addActionListener(ActionListener l) {
calculatorButton.addActionListener(l);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Calculator")) {
calculator();
}
}
private void calculator() {
setCChildComponentsValue();
setCChildComponentsFormat();
}
private void setCChildComponentsFormat() {
Vector childComponentExts = relationManager.getChildComponentExts(column);
Iterator childComponentExtIter = childComponentExts.iterator();
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();
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();
if ( (childColumn.getInputType() != 81) && (childColumn.getInputType() != 82) ) {
JComponent component = childComponentExt.getComponent();
boolean findComponent = false;
for (int i = 0; i < columns.size(); i++) {
String columnId = (String)columns.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())) ) {
ComponentManager.setValue(value, component);
findComponent = true;
break;
}
}
if (!findComponent) {
Vector parameters = relationManager.getParameters(childComponentExt);
if (parameters != null) {
String sql = relationManager.getSQL(childComponentExt, parameters);
// System.err.println("SQLSQL = " + sql);
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 {
// System.err.println("rsrs = " + rs.getStatement());
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 setRelationManager(RelationManager relationManager) {
this.relationManager = relationManager;
}
public void setColumns(Vector columns) {
this.columns = columns;
}
public Object getValue() {
return displayText.getValue();
}
public void setValue(Object value) {
this.value = value;
displayText.setValue(value);
}
}