/*
* PanelDiscountServiceClasses.java
*
* Created on 10 Март 2008 г., 14:24
*/
package clips.service.discount;
import cli_fmw.delegate.DelegateLine2;
import clips.delegate.directory.simple.discountCard.DirectoryDiscountCardTypeItem;
import clips.service.discount.levels.DialogDiscountLevels;
import cli_fmw.delegate.directory.DirectoryItem;
import clips.delegate.directory.complex.DirectoryDiscountLevel;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.ClipsException;
import clips.delegate.service.discount.DiscountLocal;
import clips.delegate.service.discount.Discounts;
import cli_fmw.main.ContentStateListener;
import cli_fmw.main.MainWindow;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.Persistentable;
import cli_fmw.main.PrintCreators;
import cli_fmw.main.Printable;
import cli_fmw.report.TableReportOptions;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.SelectorEditableExceptional;
import cli_fmw.utils.table_config_states.StateSaver;
import clips.delegate.directory.simple.discountCard.DirectoryDiscountCardType;
import framework.beans.directory.DirectoryItemDetails;
import java.util.HashMap;
import java.util.LinkedHashMap;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
/**
*
* @author axe
*/
public class PanelDiscount extends PageGeneric implements Persistentable, Printable,
ContentStateListener {
DiscountLocal discountLocal;
SelectionChanger cardSelectionChanger;
DirectoryDiscountCardTypeItem selectedCard;
JLabel noDiscountLevelsLabel = new JLabel("Определите уровни дисконта");
/** Creates new form PanelDiscountServiceClasses */
public PanelDiscount(PageContainer container) throws ClipsException {
super(container);
initComponents();
rebuildCardList();
cardSelectionChanger = new SelectionChanger();
cardList.addListSelectionListener(cardSelectionChanger);
discountLocal = new DiscountLocal();
discountLocal.addContentStateListener(this);
cardList.setCellRenderer(new CardListCellRenderer());
StateSaver.attachTo(this);
}
@Override
public void contentStateChanged() {
getContainer().contentStateChanged();
discountLevelBtn.setEnabled(!discountLocal.isDirty());
}
/**
*
*/
protected void rebuildCardList() throws ClipsException {
DirectoryDiscountCardType cardDirectory = DirectoryLocator.getDirectory(DirectoryDiscountCardType.class);
SelectorEditableExceptional<DirectoryDiscountCardTypeItem> ii = cardDirectory.getItems();
DefaultListModel listModel = new DefaultListModel();
for (int i = 0; i < ii.size(); i++) {
DirectoryItem item = (DirectoryItem) ii.get(i);
if(item.getID() != 0) {
listModel.addElement(item);
}
}
cardList.setModel(listModel);
}
/**
*
* @param discountCard
* @throws cli_fmw.delegate.exception.ClipsException
*/
protected void rebuildTable(DirectoryDiscountCardTypeItem discountCard) {
try {
DirectoryDiscountLevel directory = DirectoryLocator.getDirectory(DirectoryDiscountLevel.class, false);
if(directory.getItems().size() == 0) {
if(MessageBox.showConfirmYesNo(MessageBox.C_SET_DISC_LEVELS_NOW) == MessageBox.ANSWER_YES) {
selectedCard = discountCard;
discountLevelBtnActionPerformed(null);
return;
}
return;
}
Discounts discounts = discountLocal.getDiscounts(discountCard);
mainTable.setModel(new TableModelDiscount(discounts));
mainTable.setEnabled(true);
selectedCard = discountCard;
delCardBtn.setEnabled(true);
} catch (ClipsException ex) {
if(selectedCard != null) {
cardList.removeListSelectionListener(cardSelectionChanger);
cardList.setSelectedValue(selectedCard, true);
cardList.addListSelectionListener(cardSelectionChanger);
} else {
cardList.removeListSelectionListener(cardSelectionChanger);
cardList.setSelectedIndex(-1);
cardList.addListSelectionListener(cardSelectionChanger);
delCardBtn.setEnabled(false);
}
mainTable.setEnabled(false);
MessageBox.showException(ex);
}
}
/**
*
*/
class SelectionChanger implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent arg0) {
DirectoryDiscountCardTypeItem item = (DirectoryDiscountCardTypeItem) cardList.getSelectedValue();
if(item == null) {
return;
}
else {
rebuildTable(item);
fireContainerEventStateChanged();
}
}
}
@Override
public String getPageTitle() {
return "Дисконтные карты";
}
public String getPageIcon() {
return "";
}
@Override
public boolean isDirty() {
return discountLocal.isDirty();
}
@Override
public void save() throws ClipsException {
discountLocal.save1();
}
@Override
public void restore() {
discountLocal.restore();
rebuildTable(selectedCard);
}
/** 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() {
jSplitPane1 = new javax.swing.JSplitPane();
jPanel3 = new javax.swing.JPanel();
jPanel4 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel5 = new javax.swing.JPanel();
discountLevelBtn = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
mainTable = new cli_fmw.utils.sorted_table.SortedTable();
jPanel1 = new javax.swing.JPanel();
cardLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
cardList = new javax.swing.JList();
jPanel2 = new javax.swing.JPanel();
addCardBtn = new javax.swing.JButton();
delCardBtn = new javax.swing.JButton();
jSplitPane1.setDividerLocation(220);
jPanel3.setLayout(new java.awt.BorderLayout(0, 10));
jPanel4.setLayout(new java.awt.GridBagLayout());
jLabel1.setText("Уровни дисконта");
jPanel4.add(jLabel1, new java.awt.GridBagConstraints());
jPanel3.add(jPanel4, java.awt.BorderLayout.PAGE_START);
discountLevelBtn.setText("Уровни дисконта...");
discountLevelBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
discountLevelBtnActionPerformed(evt);
}
});
jPanel5.add(discountLevelBtn);
jPanel3.add(jPanel5, java.awt.BorderLayout.PAGE_END);
jScrollPane2.setViewportView(mainTable);
jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER);
jSplitPane1.setRightComponent(jPanel3);
jPanel1.setLayout(new java.awt.BorderLayout(0, 10));
cardLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
cardLabel.setText("Типы дисконтных карт");
cardLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jPanel1.add(cardLabel, java.awt.BorderLayout.NORTH);
cardList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(cardList);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
addCardBtn.setText("Добавить...");
addCardBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addCardBtnActionPerformed(evt);
}
});
jPanel2.add(addCardBtn);
delCardBtn.setText("Удалить");
delCardBtn.setEnabled(false);
delCardBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delCardBtnActionPerformed(evt);
}
});
jPanel2.add(delCardBtn);
jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_END);
jSplitPane1.setLeftComponent(jPanel1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 623, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void delCardBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delCardBtnActionPerformed
if(MessageBox.showConfirmYesNo(MessageBox.C_DEL_DISCOUNT_CARD) == MessageBox.ANSWER_YES) {
try {
DirectoryDiscountCardType cardDirectory = DirectoryLocator.getDirectory(DirectoryDiscountCardType.class);
SelectorEditableExceptional<DirectoryDiscountCardTypeItem> ii = cardDirectory.getItems();
for(int i=0; i<ii.size(); i++) {
if(ii.get(i) == selectedCard) {
ii.remove(i);
break;
}
}
cardList.removeListSelectionListener(cardSelectionChanger);
cardList.removeAll();
rebuildCardList();
cardList.addListSelectionListener(cardSelectionChanger);
mainTable.setModel(new DefaultTableModel());
mainTable.setEnabled(false);
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}//GEN-LAST:event_delCardBtnActionPerformed
private void addCardBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCardBtnActionPerformed
String cardName = JOptionPane.showInputDialog(this, "Введите название новой карты");
if(cardName != null) {
try {
DirectoryDiscountCardType cardDirectory = DirectoryLocator.getDirectory(DirectoryDiscountCardType.class);
SelectorEditableExceptional<DirectoryDiscountCardTypeItem> items = cardDirectory.getItems();
DirectoryItemDetails details = new DirectoryItemDetails(0, cardName);
items.append(new DirectoryDiscountCardTypeItem(details));
cardList.removeListSelectionListener(cardSelectionChanger);
cardList.removeAll();
rebuildCardList();
cardList.addListSelectionListener(cardSelectionChanger);
mainTable.setModel(new DefaultTableModel());
mainTable.setEnabled(false);
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}//GEN-LAST:event_addCardBtnActionPerformed
private void discountLevelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_discountLevelBtnActionPerformed
try {
DialogDiscountLevels dlg = new DialogDiscountLevels(MainWindow.mainWindow, getAuditManager());
dlg.setVisible(true);
if (selectedCard != null) {
rebuildTable(selectedCard);
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_discountLevelBtnActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addCardBtn;
private javax.swing.JLabel cardLabel;
private javax.swing.JList cardList;
private javax.swing.JButton delCardBtn;
private javax.swing.JButton discountLevelBtn;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSplitPane jSplitPane1;
private cli_fmw.utils.sorted_table.SortedTable mainTable;
// End of variables declaration//GEN-END:variables
@Override
public DelegateLine2 getDelegate() {
return null;
}
@Override
public boolean readyForPrint() {
return !(isDirty() || selectedCard == null);
}
@Override
public void print() {
if (readyForPrint()) {
String title = "Сведения о типе дисконтной карты(уровни дисконта)";
HashMap<String, Object> data = new LinkedHashMap<String, Object>();
data.put("type", "Тип карты: " + selectedCard.getTitle());
TableReportOptions ops = new TableReportOptions(TableReportOptions.PageOrentation.horizontal);
PrintCreators.createGeneratedReport(title, ops, data.entrySet(), null, mainTable.getModel(), getClass());
}
}
}