package realcix20.guis.views;
import java.awt.Color;
import java.awt.Container;
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.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JButton;
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.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
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.BaseClass;
import realcix20.classes.basic.Cell;
import realcix20.classes.basic.ClassManager;
import realcix20.classes.basic.Column;
import realcix20.classes.basic.Row;
import realcix20.guis.utils.I18NManager;
import realcix20.guis.utils.ImageManager;
import realcix20.guis.utils.MnemonicGenerator;
import realcix20.guis.utils.TxtManager;
import realcix20.utils.ObjectUtil;
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 SelectPBAndAddressDialog extends JDialog implements ActionListener, MouseListener {
private ObjectEdit container;
private JXTable bankTable;
private JXTable addressTable;
public SelectPBAndAddressDialog(ObjectEdit container) {
super(container);
this.container = container;
initMenuBar();
initComponents();
updateTable();
}
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(this);
menuItem.setVisible(false);
notVisibleMenu.add(menuItem);
setJMenuBar(menuBar);
}
public void initComponents() {
CellConstraints cc = new CellConstraints();
//======== this ========
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
SelectPBAndAddressDialog thisWindow = (SelectPBAndAddressDialog)e.getSource();
thisWindow.container.setEnabled(true);
thisWindow.dispose();
}
}
);
Container contentPane = getContentPane();
contentPane.setLayout(new FormLayout(
new ColumnSpec[] {
new ColumnSpec("10px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("300px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("20px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("300px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC
},
new RowSpec[] {
new RowSpec("fill:10px"),
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("200px"),
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC
}));
JLabel label = new JLabel();
label.setText(TxtManager.getTxt("VIEW.SELECTPBANDADDRESSDIALOG.BANKLABEL"));
label.setIcon(ImageManager.getImage(ImageManager.BANK_IMAGE));
contentPane.add(label, cc.xy(3, 3));
label = new JLabel();
label.setText(TxtManager.getTxt("VIEW.SELECTPBANDADDRESSDIALOG.ADDRESSLABEL"));
label.setIcon(ImageManager.getImage(ImageManager.ADDRESS_IMAGE));
contentPane.add(label, cc.xy(7, 3));
JScrollPane scrollPane = new JScrollPane();
{
bankTable = new JXTable();
bankTable.setSortable(true);
bankTable.setEditable(false);
bankTable.setColumnControlVisible(false);
bankTable.setHorizontalScrollEnabled(true);
HighlighterPipeline highlighters = new HighlighterPipeline();
highlighters.addHighlighter(new AlternateRowHighlighter());
highlighters.addHighlighter(new RolloverHighlighter(Color.BLACK, Color.WHITE));
bankTable.setHighlighters(highlighters);
bankTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
bankTable.addMouseListener(this);
scrollPane.setViewportView(bankTable);
}
contentPane.add(scrollPane, cc.xy(3, 5));
scrollPane = new JScrollPane();
{
addressTable = new JXTable();
addressTable.setSortable(true);
addressTable.setEditable(false);
addressTable.setColumnControlVisible(false);
addressTable.setHorizontalScrollEnabled(true);
HighlighterPipeline highlighters = new HighlighterPipeline();
highlighters.addHighlighter(new AlternateRowHighlighter());
highlighters.addHighlighter(new RolloverHighlighter(Color.BLACK, Color.WHITE));
addressTable.setHighlighters(highlighters);
addressTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
addressTable.addMouseListener(this);
scrollPane.setViewportView(addressTable);
}
contentPane.add(scrollPane, cc.xy(7, 5));
JPanel 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(75, 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(75, 20));
cancelButton.setActionCommand("Cancel");
cancelButton.addActionListener(this);
controlPanel.add(cancelButton);
}
contentPane.add(controlPanel, cc.xywh(3, 7, 5, 1));
pack();
setTitle(TxtManager.getTxt("VIEW.SELECTPBANDADDRESSDIALOG.TITLE"));
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
}
public void updateTable() {
BankTableModel bankModel = new BankTableModel();
bankTable.setModel(bankModel);
AddressTableModel addressModel = new AddressTableModel();
addressTable.setModel(addressModel);
}
private void storeBankAndAddress() {
Vector banks = new Vector();
Vector addresses = new Vector();
int[] rows = bankTable.getSelectedRows();
if (rows.length > 0) {
BankTableModel model = (BankTableModel)bankTable.getModel();
for (int i = 0; i < rows.length; i++) {
int selectedRowNumber = bankTable.convertRowIndexToModel(rows[i]);
Vector dataVector = model.getDataVector();
Vector rowDatas = (Vector)dataVector.get(selectedRowNumber);
Row selectedRow = (Row)rowDatas.get(rowDatas.size() - 1);
banks.add(selectedRow);
}
}
rows = addressTable.getSelectedRows();
if (rows.length > 0) {
AddressTableModel model = (AddressTableModel)addressTable.getModel();
for (int i = 0; i < rows.length; i++) {
int selectedRowNumber = addressTable.convertRowIndexToModel(rows[i]);
Vector dataVector = model.getDataVector();
Vector rowDatas = (Vector)dataVector.get(selectedRowNumber);
Row selectedRow = (Row)rowDatas.get(rowDatas.size() - 1);
addresses.add(selectedRow);
}
}
container.setBanks(banks);
container.address = addresses;
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Cancel")) {
container.setEnabled(true);
dispose();
} else if (e.getActionCommand().equals("Confirm")) {
storeBankAndAddress();
container.setEnabled(true);
container.getListener().exportCIXFile();
dispose();
}
}
public void mousePressed(MouseEvent e) {
if ( (e.getSource() instanceof JXTable) && (e.getButton() == e.BUTTON3) ) {
JXTable table = (JXTable)e.getSource();
if ( (table.getRowCount() > 0) ) {
if (table.getSelectedRows().length > 0)
table.removeRowSelectionInterval(0, table.getRowCount() - 1);
}
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
class BankTableModel extends DefaultTableModel {
private BaseClass object;
private Vector names;
private Vector dataValues;
public BankTableModel() {
this.object = ClassManager.createClass(260);
names = new Vector();
dataValues = new Vector();
Iterator columnIter = object.getColumns().iterator();
while (columnIter.hasNext()) {
Column column = (Column)columnIter.next();
if (column.isIndexField())
names.add(column);
}
names.add("row");
Iterator rowIter = object.getRows().iterator();
while (rowIter.hasNext()) {
Row row = (Row)rowIter.next();
String ns = ObjectUtil.findOldCell(row, "PB", "NS").getColumnValue().toString();
if (ns.equals("ME")) {
Vector rowDatas = new Vector();
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.isI18N()) {
String i18nString = null;
if (newCell.getColumnValue() != null)
i18nString = I18NManager.getI18NString(column.getI18nPrefix(), newCell.getColumnValue());
if (i18nString != null)
rowDatas.add(newCell.getColumnValue() + "(" + i18nString + ")");
else
rowDatas.add(newCell.getColumnValue());
} else {
rowDatas.add(newCell.getColumnValue());
}
}
}
rowDatas.add(row);
dataValues.add(rowDatas);
}
}
setDataVector(dataValues, names);
}
}
class AddressTableModel extends DefaultTableModel {
private BaseClass object;
private Vector names;
private Vector dataValues;
public AddressTableModel() {
this.object = ClassManager.createClass(280);
names = new Vector();
dataValues = new Vector();
Iterator columnIter = object.getColumns().iterator();
while (columnIter.hasNext()) {
Column column = (Column)columnIter.next();
if (column.isIndexField())
names.add(column);
}
names.add("row");
Iterator rowIter = object.getRows().iterator();
while (rowIter.hasNext()) {
Row row = (Row)rowIter.next();
String ns = ObjectUtil.findOldCell(row, "ADDR", "NS").getColumnValue().toString();
if (ns.equals("ME")) {
Vector rowDatas = new Vector();
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.isI18N()) {
String i18nString = null;
if (newCell.getColumnValue() != null)
i18nString = I18NManager.getI18NString(column.getI18nPrefix(), newCell.getColumnValue());
if (i18nString != null)
rowDatas.add(newCell.getColumnValue() + "(" + i18nString + ")");
else
rowDatas.add(newCell.getColumnValue());
} else {
rowDatas.add(newCell.getColumnValue());
}
}
}
rowDatas.add(row);
dataValues.add(rowDatas);
}
}
setDataVector(dataValues, names);
}
}
}