package realcix20.guis.views;
import info.clearthought.layout.TableLayout;
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.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import org.jdesktop.swingx.JXTable;
import realcix20.classes.basic.BaseClass;
import realcix20.classes.basic.Cell;
import realcix20.classes.basic.Column;
import realcix20.classes.basic.Row;
import realcix20.guis.components.DetailList;
import realcix20.guis.listeners.ItemEditListener;
import realcix20.guis.relationships.RelationManager;
import realcix20.guis.utils.ComponentExt;
import realcix20.guis.utils.ComponentManager;
import realcix20.guis.utils.ImageManager;
import realcix20.guis.utils.MnemonicGenerator;
import realcix20.guis.utils.TxtManager;
import realcix20.utils.DAO;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
public class ItemEdit extends JDialog {
private JTabbedPane propertyPane;
private JPanel controlPanel;
private ObjectEdit container;
private BaseClass object;
private Row selectedRow;
private int selectedRowNumber;
private JXTable table;
private Vector itemComponentExts;
private Vector itemAlertLabels;
private Vector itemGroups;
private ItemEditListener listener;
private String action;
public ItemEdit(ObjectEdit container, BaseClass object, Row selectedRow, String action) {
super(container);
this.action = action;
this.container = container;
this.object = object;
this.selectedRow = selectedRow;
this.selectedRowNumber = container.getTable().convertRowIndexToModel(container.getTable().getSelectedRow());
this.table = container.getTable();
listener = new ItemEditListener(this);
itemGroups = new Vector();
this.itemComponentExts = container.getItemComponentExts();
Iterator itemComponentExtIter = getItemComponentExts().iterator();
while (itemComponentExtIter.hasNext()) {
ComponentExt componentExt = (ComponentExt)itemComponentExtIter.next();
Column column = componentExt.getColumn();
JComponent component = ComponentManager.getComponent(componentExt.getColumn());
componentExt.setComponent(component);
}
container.setItemComponentExts(itemComponentExts);
container.createRelation();
itemComponentExtIter = getItemComponentExts().iterator();
while (itemComponentExtIter.hasNext()) {
ComponentExt componentExt = (ComponentExt)itemComponentExtIter.next();
Column column = componentExt.getColumn();
container.getRelationManager().init81Or82Or83ComponentAction(componentExt);
if (action.equals("Edit")) {
//if correspendence, init the default value
if(object.getClsId()==520){
RelationManager relationManager = container.getRelationManager();
Vector parameters = relationManager.getParameters_Defvalue(componentExt);
if (parameters != null) {
String sql = relationManager.getSQL_Defvalue(componentExt, 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) {
ComponentManager.setValue(rs.getObject(i), tempComponentExt.getComponent());
}
}
}
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ComponentManager.setValue(getValue(componentExt.getColumn(), selectedRow), componentExt.getComponent());
} else if (action.equals("Add")) {
RelationManager relationManager = container.getRelationManager();
Vector parameters = relationManager.getParameters_Defvalue(componentExt);
if (parameters != null) {
String sql = relationManager.getSQL_Defvalue(componentExt, 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) {
ComponentManager.setValue(rs.getObject(i), tempComponentExt.getComponent());
}
}
}
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
initComponents();
initMenuBar();
itemComponentExtIter = getItemComponentExts().iterator();
while (itemComponentExtIter.hasNext()) {
ComponentExt componentExt = (ComponentExt)itemComponentExtIter.next();
if (componentExt.getComponent() instanceof DetailList) {
// System.err.println("dldldl");
DetailList dl = (DetailList)componentExt.getComponent();
dl.getNotVisibleTableButton().doClick();
}
}
}
private Object getValue(Column column, Row row) {
Object value = null;
Iterator cellIter = row.getNewCells().iterator();
while (cellIter.hasNext()) {
Cell cell = (Cell)cellIter.next();
if ( (cell.getColumnName().equals(column.getColumnName())) && (cell.getTableName().equals(column.getTableName())) ) {
value = cell.getColumnValue();
}
}
return value;
}
private void initMenuBar() {
JMenuBar menuBar = new JMenuBar();
menuBar.setVisible(true);
JMenu notVisibleMenu = new JMenu();
notVisibleMenu.setVisible(false);
menuBar.add(notVisibleMenu);
JMenuItem menuItem = new JMenuItem();
menuItem.setAccelerator(KeyStroke.getKeyStroke("ESCAPE"));
menuItem.setActionCommand("Cancel");
menuItem.addActionListener(listener);
menuItem.setVisible(false);
notVisibleMenu.add(menuItem);
menuItem = new JMenuItem();
menuItem.setAccelerator(KeyStroke.getKeyStroke("ENTER"));
menuItem.setActionCommand("Modify");
menuItem.addActionListener(listener);
menuItem.setVisible(false);
notVisibleMenu.add(menuItem);
menuItem = new JMenuItem();
menuItem.setAccelerator(KeyStroke.getKeyStroke("F1"));
menuItem.setActionCommand("Help");
menuItem.addActionListener(listener);
menuItem.setVisible(false);
notVisibleMenu.add(menuItem);
setJMenuBar(menuBar);
}
private void initComponents() {
CellConstraints cc = new CellConstraints();
//======== this ========
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ItemEdit thisWindow = (ItemEdit)e.getSource();
thisWindow.getContainer().setEnabled(true);
thisWindow.dispose();
}
}
);
Container contentPane = getContentPane();
setTitle(TxtManager.getTxt("CLS." + getObject().getClsId()) + " - " + TxtManager.getTxt("ITEM.TITLE"));
contentPane.setLayout(new GridBagLayout());
((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {0, 0};
((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0, 0, 0};
((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {1.0, 1.0E-4};
((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {0.0, 1.0, 0.0, 1.0E-4};
DefaultComponentFactory compFactory = DefaultComponentFactory.getInstance();
JComponent titleText = compFactory.createSeparator(TxtManager.getTxt("ITEM.PROPERTIES.TITLE"));
contentPane.add(titleText, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(10, 10, 15, 10), 0, 0));
//======== propertyPane ========
{
propertyPane = new JTabbedPane();
getPropertyPane().setTabPlacement(JTabbedPane.LEFT);
getPropertyPane().setPreferredSize(new Dimension(600, 350));
itemAlertLabels = new Vector();
Iterator itemComponentExtIter = getItemComponentExts().iterator();
Iterator itemGroupNameIter = object.getItemGroupnames().iterator();
int n = 0;
while (itemGroupNameIter.hasNext()) {
int groupName = ((Integer)itemGroupNameIter.next()).intValue();
//======== groupPanel ========
{
JPanel groupPanel = new JPanel();
JScrollPane groupScrollPane = new JScrollPane();
groupScrollPane.setViewportView(groupPanel);
// //layout
// //columnSpec
// ColumnSpec[] columnSpec = new ColumnSpec[] {
// new ColumnSpec("10px"),
// FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
// new ColumnSpec("150px"),
// FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
// new ColumnSpec("150px"),
// FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
// new ColumnSpec("150px")
// };
//
// //rowSpec
// //get rowCount
// int rowCount = getObject().getGroupItemFieldCount(groupName);
// RowSpec[] rowSpec = new RowSpec[rowCount * 2 + 1];
// rowSpec[0] = new RowSpec("10px");
// rowSpec[1] = FormFactory.LINE_GAP_ROWSPEC;
// for (int i = 1; i <= rowCount - 1; i++) {
// rowSpec[i * 2] = new RowSpec("20px");
// rowSpec[i * 2 + 1] = FormFactory.LINE_GAP_ROWSPEC;
// }
// rowSpec[rowCount * 2] = new RowSpec("20px");
//
// groupPanel.setLayout(new FormLayout(columnSpec, rowSpec));
//fix ʹ�ö����ı�detailText
double size[][] = {
{ 10,150,150,150,10 },
{ 10} };
TableLayout tableLayout=new TableLayout(size);
groupPanel.setLayout(tableLayout);
tableLayout.setVGap(10);
tableLayout.setHGap(10);
//add components to groupPanel
int currentComponentNumber = 1;
Iterator columnIter = getObject().getColumns().iterator();
while (columnIter.hasNext()) {
Column column = (Column)columnIter.next();
if ( (column.isItemField()) && (column.getGroupName() == groupName) ) {
tableLayout.insertRow(currentComponentNumber,TableLayout.PREFERRED);
//add label
JLabel label = new JLabel(column.toString());
// groupPanel.add(label, cc.xy(3, currentComponentNumber * 2 + 1));
groupPanel.add(label,"1,"+currentComponentNumber+",f,t");
//add component
ComponentExt componentExt = (ComponentExt)itemComponentExtIter.next();
JComponent component = componentExt.getComponent();
// groupPanel.add(component, cc.xy(5, currentComponentNumber * 2 + 1));
groupPanel.add(component,"2,"+currentComponentNumber+",f,f");
//add alert label
JLabel alertLabel = new JLabel();
componentExt.setAlertLabel(alertLabel);
if (column.isPrimaryKey()) {
alertLabel.setIcon(ImageManager.getImage(ImageManager.HELP_IMAGE));
} else {
alertLabel.setVisible(false);
}
getItemGroups().add(n);
// groupPanel.add(alertLabel, cc.xy(7, currentComponentNumber * 2 + 1));
groupPanel.add(alertLabel,"3,"+currentComponentNumber+",f,f");
getItemAlertLabels().add(alertLabel);
//specified by PARTNERCLASS
/*if ( (column.getTableName().equals("PA") && (column.getColumnName().equals("RUSERPA"))) ) {
String ns = ObjectUtil.findNewCell(container.getCurrentRow(), "P", "NS").getColumnValue().toString();
if (ns.equals("ME")) {
label.setVisible(false);
component.setVisible(false);
alertLabel.setVisible(false);
}
}*/
currentComponentNumber++;
}
}
//set groupPanel Title & add groupPanel to propertyPane
String groupNameString = "GRP." + new Integer(groupName).toString();
getPropertyPane().addTab(TxtManager.getTxt(groupNameString), null,
groupScrollPane, null);
n++;
}
}
}
contentPane.add(getPropertyPane(), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(10, 10, 15, 10), 0, 0));
//======== controlPanel ========
{
controlPanel = new JPanel();
controlPanel.setLayout(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("Modify");
confirmButton.addActionListener(listener);
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(listener);
controlPanel.add(cancelButton);
JButton helpButton = new JButton();
helpButton.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.GLOBAL.HELPBUTTON"), KeyEvent.VK_H));
helpButton.setMnemonic(KeyEvent.VK_H);
helpButton.setIcon(ImageManager.getImage(ImageManager.HELP_IMAGE));
helpButton.setPreferredSize(new Dimension(100, 20));
helpButton.setActionCommand("Help");
helpButton.addActionListener(listener);
controlPanel.add(helpButton);
}
contentPane.add(controlPanel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
pack();
setVisible(true);
this.setLocationRelativeTo(null);
this.setResizable(false);
}
public ObjectEdit getContainer() {
return container;
}
public Vector getItemComponentExts() {
return itemComponentExts;
}
public Vector getItemAlertLabels() {
return itemAlertLabels;
}
public JTabbedPane getPropertyPane() {
return propertyPane;
}
public Row getSelectedRow() {
return selectedRow;
}
public JXTable getTable() {
return table;
}
public int getSelectedRowNumber() {
return selectedRowNumber;
}
public BaseClass getObject() {
return object;
}
public String getAction() {
return action;
}
public Vector getItemGroups() {
return itemGroups;
}
}