/*
* PanelDiscountCard.java
*
* Created on 26 Июль 2008 г., 11:40
*/
package clips.service.discount.card;
import clips.delegate.client.ClientLocal;
import cli_fmw.delegate.DelegateLine2;
import clips.delegate.service.discount.DiscountCardLocal;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageException;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.Persistentable;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.ModalDialog;
import cli_fmw.main.ClipsException;
import cli_fmw.main.MainWindow;
import cli_fmw.utils.table_config_states.StateSaver;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
*
* @author petr
*/
public class PanelClientDiscountCard extends PageGeneric
implements PageContainer, Persistentable, ListSelectionListener {
private String FIO;
private ClientLocal clientLocal;
private PanelDiscountCard panel;//панель редактирования карты
private ArrayList<DiscountCardLocal> cardList;
private int lastSelected;
public PanelClientDiscountCard(PageContainer container, ClientLocal clientLocal, DiscountCardLocal selectedCard) throws ClipsException {
super(container);
initComponents();
this.clientLocal = clientLocal;
FIO = clientLocal.getFIO();
cardList = new ArrayList<DiscountCardLocal>();
updateCardList();
lstCards.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
lstCards.addListSelectionListener(this);
lstCards.setSelectedValue(selectedCard, true);
StateSaver.attachTo(this);
}
private void updateCardList() throws ClipsException{
cardList = (ArrayList<DiscountCardLocal>) clientLocal.getDiscountCards();
DefaultListModel model = new DefaultListModel();
for (int i = 0; i < cardList.size(); i++) {
model.addElement(cardList.get(i));
}
lstCards.setModel(model);
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jSplitPane1 = new javax.swing.JSplitPane();
pnlDiscountCard = new javax.swing.JPanel();
jPanel6 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
lstCards = new javax.swing.JList();
createNew = new javax.swing.JButton();
setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
setPreferredSize(new java.awt.Dimension(100, 100));
setLayout(new java.awt.BorderLayout());
pnlDiscountCard.setLayout(new java.awt.BorderLayout());
jSplitPane1.setRightComponent(pnlDiscountCard);
jPanel6.setLayout(new java.awt.BorderLayout(0, 10));
jScrollPane1.setPreferredSize(new java.awt.Dimension(127, 50));
lstCards.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "ftghrethrthgreherth" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(lstCards);
jPanel6.add(jScrollPane1, java.awt.BorderLayout.CENTER);
createNew.setText("Создать новую");
createNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createNewActionPerformed(evt);
}
});
jPanel6.add(createNew, java.awt.BorderLayout.PAGE_END);
jSplitPane1.setLeftComponent(jPanel6);
add(jSplitPane1, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
private void createNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createNewActionPerformed
try {
DialogNewDiscountCard dialogNewCard = new DialogNewDiscountCard(MainWindow.mainWindow, clientLocal, getAuditManager());
dialogNewCard.setVisible(true);
if (dialogNewCard.getDlgResult() == ModalDialog.DLG_OK){
updateCardList();
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_createNewActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton createNew;
private javax.swing.JPanel jPanel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JList lstCards;
private javax.swing.JPanel pnlDiscountCard;
// End of variables declaration//GEN-END:variables
@Override
public String getPageTitle() {
return "Диск. карты " + FIO;
}
@Override
public PageGeneric addNewPage(PageGeneric pageToAdd, PageGeneric afterPage) throws PageException {
if (pnlDiscountCard.getComponentCount() == 0) {
pnlDiscountCard.add((JPanel) pageToAdd, java.awt.BorderLayout.CENTER);
pnlDiscountCard.revalidate();
pnlDiscountCard.repaint();
return pageToAdd;
} else {
throw new PageException("Страница не может быть добавлена");
}
}
@Override
public void removePage(PageGeneric page, boolean forced) throws PageException {
if (page != null) {
if (forced || !((Persistentable)page).isDirty()) {
pnlDiscountCard.removeAll();
}
}
pnlDiscountCard.revalidate();
pnlDiscountCard.repaint();
}
@Override
public void activatePage(PageGeneric page) throws PageException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public PageGeneric getActivePage() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getPageCount() {
return (panel == null) ? 0 : 1;
}
@Override
public PageGeneric getPage(int index) {
return panel;
}
@Override
public void contentStateChanged() {
super.fireContainerEventStateChanged();
}
@Override
public boolean isDirty() {
boolean res = ((panel == null) ? false : panel.isDirty());
return res;
}
@Override
public void save() throws ClipsException {
if (panel != null) {
panel.save();
}
updateCardList();
}
@Override
public void restore() {
if (panel != null) {
panel.restore();
}
clientLocal.restore();
try {
updateCardList();
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
private void updatePanel(DiscountCardLocal cardLocal) throws ClipsException {
removePage(panel, false);
panel = new PanelDiscountCard(getContainer(), cardLocal);
addNewPage(panel, null);
}
@Override
public void valueChanged(ListSelectionEvent e) {
//if (e.getValueIsAdjusting()) {
Object sel = lstCards.getSelectedValue();
if (panel == null || !panel.isDirty()) {
if (sel != null) {
try {
updatePanel((DiscountCardLocal) sel);
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
lastSelected = lstCards.getSelectedIndex();
} else {
try {
int res = MessageBox.showConfirmYesNoCancel(MessageBox.C_SAVE_CHANGES);
switch (res) {
case MessageBox.ANSWER_YES:
panel.save();
updatePanel((DiscountCardLocal) sel);
break;
case MessageBox.ANSWER_NO:
panel.restore();
updatePanel((DiscountCardLocal) sel);
break;
default:
lstCards.removeListSelectionListener(this);
lstCards.setSelectedIndex(lastSelected);
lstCards.addListSelectionListener(this);
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
//}
}
@Override
public DelegateLine2 getDelegate() {
return clientLocal;
}
}