package realcix20.guis.views;
import java.awt.Container;
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.KeyStroke;
import realcix20.classes.basic.BaseClass;
import realcix20.classes.basic.Cell;
import realcix20.classes.basic.Row;
import realcix20.guis.components.JMonthPicker;
import realcix20.guis.utils.ComponentManager;
import realcix20.guis.utils.DialogManager;
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 CopyToAnotherMonthDialog extends JDialog implements ActionListener {
private MainView container;
private BaseClass object;
private Vector selectedRows;
private JMonthPicker monthPicker;
public CopyToAnotherMonthDialog(MainView container, BaseClass object, Vector selectedRows) {
super(container);
this.container = container;
this.object = object;
this.selectedRows = selectedRows;
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);
setJMenuBar(menuBar);
}
private void initComponents() {
JLabel label = new JLabel();
monthPicker = new JMonthPicker();
JButton confirmButton = new JButton();
JButton cancelButton = new JButton();
CellConstraints cc = new CellConstraints();
//======== this ========
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CopyToAnotherMonthDialog thisWindow = (CopyToAnotherMonthDialog)e.getSource();
thisWindow.getContainer().setEnabled(true);
thisWindow.dispose();
}
}
);
Container contentPane = getContentPane();
contentPane.setLayout(new FormLayout(
new ColumnSpec[] {
new ColumnSpec("10px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("100px:grow"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("100px:grow"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("10px")
},
new RowSpec[] {
new RowSpec("10px"),
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("20px"),
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("20px"),
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec("10px")
}));
//---- label ----
label.setText(TxtManager.getTxt("VIEW.COPYTOANOTHERMONTHDIALOG.LABEL"));
contentPane.add(label, cc.xy(3, 3));
contentPane.add(monthPicker, cc.xy(5, 3));
//---- confirmButton ----
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.setActionCommand("Confirm");
confirmButton.addActionListener(this);
contentPane.add(confirmButton, cc.xy(3, 5));
//---- cancelButton ----
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.setActionCommand("Cancel");
cancelButton.addActionListener(this);
contentPane.add(cancelButton, cc.xy(5, 5));
pack();
setTitle(TxtManager.getTxt("VIEW.COPYTOANOTHERMONTHDIALOG.TITLE"));
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
}
private boolean copyRows() {
String newMonth = (String)ComponentManager.getValue(monthPicker);
boolean result = true;
Iterator testRowIter = selectedRows.iterator();
while (testRowIter.hasNext()) {
Row testRow = (Row)testRowIter.next();
Cell oldCell = ObjectUtil.findOldCell(testRow, "B", "RMONTH");
String oldMonth = (String)oldCell.getColumnValue();
if (oldMonth.equals(newMonth)) {
result = false;
break;
}
}
if (!result) {
DialogManager.showMessageDialog(this, TxtManager.getTxt("INFORMATION.EQUALMONTH"));
} else {
Iterator rowIter = selectedRows.iterator();
while (rowIter.hasNext()) {
Row row = (Row)rowIter.next();
Cell newCell = ObjectUtil.findNewCell(row, "B", "RMONTH");
newCell.setColumnValue(newMonth);
if (!object.classInsert_WholeObject(row, true)) {
result = false;
break;
}
}
if (result) {
rowIter = selectedRows.iterator();
while (rowIter.hasNext()) {
Row row = (Row)rowIter.next();
object.classInsert_WholeObject(row, false);
}
} else {
DialogManager.showMessageDialog(this, TxtManager.getTxt("INFORMATION.MONTHCONFLICT"));
rollback();
}
}
return result;
}
private void rollback() {
Iterator rowIter = selectedRows.iterator();
while (rowIter.hasNext()) {
Row row = (Row)rowIter.next();
Cell newCell = ObjectUtil.findNewCell(row, "B", "RMONTH");
Cell oldCell = ObjectUtil.findOldCell(row, "B", "RMONTH");
newCell.setColumnValue(oldCell.getColumnValue());
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Cancel")) {
getContainer().setEnabled(true);
dispose();
} else if (e.getActionCommand().equals("Confirm")) {
if (copyRows()) {
container.updateObjectView();
container.setEnabled(true);
dispose();
}
}
}
public MainView getContainer() {
return container;
}
public BaseClass getObject() {
return object;
}
public Vector getSelectedRows() {
return selectedRows;
}
}