package realcix20.guis.views;
import java.awt.Color;
import java.awt.Dimension;
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.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.Highlighter;
import org.jdesktop.swingx.decorator.HighlighterPipeline;
import org.jdesktop.swingx.decorator.RolloverHighlighter;
import realcix20.classes.basic.BaseClass;
import realcix20.classes.basic.Column;
import realcix20.classes.basic.Row;
import realcix20.guis.components.LanguageChooser;
import realcix20.guis.utils.DialogManager;
import realcix20.guis.utils.ImageManager;
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;
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.FormSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.Sizes;
public class I18NMaintainDialog extends JDialog implements ActionListener {
private MainView container;
private LanguageChooser refLangChooser;
private LanguageChooser workLangChooser;
private JXTable table;
private String workLang;
public I18NMaintainDialog(MainView container) {
super(container);
this.container = container;
initMenuBar();
initComponents();
}
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);
menuItem = new JMenuItem();
menuItem.setAccelerator(KeyStroke.getKeyStroke("ENTER"));
menuItem.setActionCommand("Confirm");
menuItem.addActionListener(this);
menuItem.setVisible(false);
notVisibleMenu.add(menuItem);
setJMenuBar(menuBar);
}
private void initComponents() {
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
I18NMaintainDialog thisWindow = (I18NMaintainDialog)e.getSource();
thisWindow.container.setEnabled(true);
thisWindow.dispose();
}
}
);
CellConstraints cc = new CellConstraints();
setLayout(new FormLayout(
new ColumnSpec[] {
new ColumnSpec("10px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("125px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec(Sizes.dluX(100)),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("10px")
},
new RowSpec[] {
new RowSpec("10px"),
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("20px"),
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec(RowSpec.CENTER, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC
}));
JLabel refLangLabel = new JLabel();
refLangLabel.setText(TxtManager.getTxt("VIEW.I18NMAINTAINDIALOG.REFLANG"));
add(refLangLabel, cc.xy(3, 3));
refLangChooser = new LanguageChooser();
refLangChooser.setSelectedLang(GlobalValueManager.getApplicationLang());
refLangChooser.setActionCommand("Choose Lang");
refLangChooser.addActionListener(this);
add(refLangChooser, cc.xy(5, 3));
JLabel workLangLabel = new JLabel();
workLangLabel.setText(TxtManager.getTxt("VIEW.I18NMAINTAINDIALOG.WORKLANG"));
add(workLangLabel, cc.xy(3, 5));
workLangChooser = new LanguageChooser();
workLangChooser.setSelectedLang(GlobalValueManager.getApplicationLang());
workLang = workLangChooser.getSelectedLang();
workLangChooser.setActionCommand("Choose Lang");
workLangChooser.addActionListener(this);
add(workLangChooser, cc.xy(5, 5));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(450, 200));
add(scrollPane, cc.xywh(3, 7, 3, 1));
table = new JXTable();
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);
updateTable();
scrollPane.setViewportView(table);
JPanel controlPanel = new JPanel();
add(controlPanel, cc.xywh(3, 9, 3, 1));
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);
pack();
setTitle(TxtManager.getTxt("VIEW.MAINFRAME.MAINTAINI18N"));
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
}
private boolean isI18NIDAdded(String i18nID, Vector dataValues) {
boolean result = false;
Iterator dataValuesIter = dataValues.iterator();
while (dataValuesIter.hasNext()) {
Vector rowDatas = (Vector)dataValuesIter.next();
String tempI18NID = (String)rowDatas.get(0);
if (i18nID.equals(tempI18NID)) {
result = true;
break;
}
}
return result;
}
private void updateTable() {
DefaultTableModel model = new DefaultTableModel();
Vector names = new Vector();
names.add(TxtManager.getTxt("VIEW.I18NMAINTAINDIALOG.TABLE.I18NID"));
names.add(TxtManager.getTxt("VIEW.I18NMAINTAINDIALOG.TABLE.REFLANGTXT"));
names.add(TxtManager.getTxt("VIEW.I18NMAINTAINDIALOG.TABLE.WORKLANGTXT"));
Vector dataValues = new Vector();
BaseClass object = container.getCurrentObject();
Column pkColumn = (Column)object.getPkColumns().get(0);
Iterator rowsIter = object.getRows().iterator();
while (rowsIter.hasNext()) {
Vector rowDatas = new Vector();
Row row = (Row)rowsIter.next();
String i18nID = ObjectUtil.findNewCell(row, pkColumn.getTableName(), pkColumn.getColumnName()).getColumnValue().toString();
if (!isI18NIDAdded(i18nID, dataValues)) {
rowDatas.add(i18nID);
rowDatas.add(TxtManager.getTxt("." + pkColumn.getColumnName() + "." + i18nID, refLangChooser.getSelectedLang()));
rowDatas.add(TxtManager.getTxt("." + pkColumn.getColumnName() + "." + i18nID, workLangChooser.getSelectedLang()));
dataValues.add(rowDatas);
}
}
model.setDataVector(dataValues, names);
table.setModel(model);
table.getColumnExt(0).setEditable(false);
table.getColumnExt(1).setEditable(false);
}
private void saveI18NTxts() {
DefaultTableModel model = (DefaultTableModel)table.getModel();
Vector dataVector = model.getDataVector();
Iterator rowDatasIter = dataVector.iterator();
while (rowDatasIter.hasNext()) {
Vector rowDatas = (Vector)rowDatasIter.next();
String i18nID = (String)rowDatas.get(0);
String workText = (String)rowDatas.get(2);
updateI18NText(i18nID, workText);
}
}
private void updateI18NText(String i18nID, String workText) {
Column pkColumn = (Column)container.getCurrentObject().getPkColumns().get(0);
String id = "." + pkColumn.getColumnName() + "." + i18nID;
DAO dao = DAO.getInstance();
dao.update(Resources.DELETE_TXT_SQL);
dao.setString(1, id);
dao.setString(2, workLang);
dao.executeUpdate();
if ( (workText != null) && (workText.length() > 0) ) {
dao.update(Resources.INSERT_TXT_SQL);
dao.setString(1, id);
dao.setString(2, workLang);
if (workText.trim().length() == 0) {
dao.setString(3, " ");
} else
dao.setString(3, workText);
dao.executeUpdate();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Choose Lang")) {
if (e.getSource().equals(workLangChooser)) {
Object[] options = {TxtManager.getTxt("VIEW.I18NMAINTAINDIALOG.SAVEANDCONTINUE"),
TxtManager.getTxt("VIEW.I18NMAINTAINDIALOG.CANCELANDCONTINUE"),
TxtManager.getTxt("VIEW.I18NMAINTAINDIALOG.CANCEL")};
int n = DialogManager.showYesNoCancelDialog(this, TxtManager.getTxt("INFORMATION.CURRENTWORKLANGCHANGE"), options);
if (n == 0) {
saveI18NTxts();
workLang = workLangChooser.getSelectedLang();
updateTable();
} else if (n == 1) {
workLang = workLangChooser.getSelectedLang();
updateTable();
} else if (n == 2) {
workLangChooser.setSelectedLang(workLang);
}
} else {
updateTable();
}
} else if (e.getActionCommand().equals("Cancel")) {
container.setEnabled(true);
dispose();
} else if (e.getActionCommand().equals("Confirm")) {
saveI18NTxts();
container.updateTable();
container.setEnabled(true);
dispose();
}
}
}