/*
* DiscountLEvelsDialog.java
*
* Created on 11 Март 2008 г., 18:53
*/
package clips.service.discount.levels;
import clips.delegate.directory.complex.DirectoryDiscountLevel;
import clips.delegate.directory.complex.DirectoryDiscountLevelItem;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import beans.directory.discount.entity.DiscountLevelDetails;
import cli_fmw.main.ClipsException;
import cli_fmw.main.audit.AuditManager;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.ModalDialog;
import cli_fmw.utils.table_config_states.StateSaver;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import javax.swing.RowSorter;
import javax.swing.table.TableModel;
/**
*
* @author axe
*/
public class DialogDiscountLevels extends ModalDialog {
/** Creates new form DiscountLEvelsDialog */
public DialogDiscountLevels(java.awt.Frame parent, AuditManager am) throws ClipsException {
super(parent, "Уровни дисконта", am);
initComponents();
TableModelDiscountLevels model = new TableModelDiscountLevels();
mainTable.setModel(model);
Dimension scrSize=Toolkit.getDefaultToolkit().getScreenSize();
Rectangle dlgSize=this.getBounds();
this.setLocation((scrSize.width-dlgSize.width)/2,(scrSize.height-dlgSize.height)/2);
StateSaver.attachTo(this);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
closeBtn = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
newBtn = new javax.swing.JButton();
removeBtn = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
mainTable = new cli_fmw.utils.sorted_table.SortedTable();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Редактирование уровней дисконта");
closeBtn.setText("Закрыть");
closeBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeBtnActionPerformed(evt);
}
});
jPanel1.add(closeBtn);
getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
newBtn.setText("Новый..");
newBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newBtnActionPerformed(evt);
}
});
jPanel2.add(newBtn);
removeBtn.setText("Удалить");
removeBtn.setEnabled(false);
removeBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
removeBtnActionPerformed(evt);
}
});
jPanel2.add(removeBtn);
getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_START);
mainTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
mainTableMouseClicked(evt);
}
});
jScrollPane1.setViewportView(mainTable);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
private void removeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeBtnActionPerformed
RowSorter<? extends TableModel> rowSorter = mainTable.getRowSorter();
int row = mainTable.getSelectedRowSorted();
if (rowSorter != null){
row = rowSorter.convertRowIndexToModel(row);
}
if(row != -1) {
try {
DirectoryDiscountLevel directory = (DirectoryDiscountLevel)
DirectoryLocator.getDirectory(DirectoryDiscountLevel.class, false);
DirectoryDiscountLevelItem item = (DirectoryDiscountLevelItem) directory.getItems().get(row);
if(MessageBox.showConfirmYesNo(MessageBox.C_DEL_DISCOUNT_LEVEL) == MessageBox.ANSWER_YES) {
directory.getItems().remove(row);
mainTable.setModel(new TableModelDiscountLevels());
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}//GEN-LAST:event_removeBtnActionPerformed
private void closeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeBtnActionPerformed
setVisible(false);
}//GEN-LAST:event_closeBtnActionPerformed
private void newBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newBtnActionPerformed
String name = JOptionPane.showInputDialog(this, "Введите название нового уровня дисконта");
if(name != null) {
try {
DirectoryDiscountLevel directory = (DirectoryDiscountLevel) DirectoryLocator.getDirectory(DirectoryDiscountLevel.class, false);
DiscountLevelDetails details = new DiscountLevelDetails();
details.money = 0;
details.title = name;
directory.getItems().append(new DirectoryDiscountLevelItem(details));
mainTable.setModel(new TableModelDiscountLevels());
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}//GEN-LAST:event_newBtnActionPerformed
private void mainTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainTableMouseClicked
removeBtn.setEnabled(mainTable.getSelectedRowCount() > 0);
}//GEN-LAST:event_mainTableMouseClicked
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton closeBtn;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private cli_fmw.utils.sorted_table.SortedTable mainTable;
private javax.swing.JButton newBtn;
private javax.swing.JButton removeBtn;
// End of variables declaration//GEN-END:variables
}