/*
* TableEditPanel.java
*
* Created on October 7, 2008, 6:28 PM
*/
package clips.report.panels;
import beans.report.selectors.baseSelectors.AbstractReportTable;
import beans.report.selectors.baseSelectors.AbstractReportTable.TableChangeTypes;
import beans.report.selectors.disease.DiseaseTable;
import beans.report.SelectorIndefiner;
import beans.report.selectors.ReportTableFactory;
import cli_fmw.delegate.DelegateLine2;
import beans.report.selectors.baseSelectors.TableContainer;
import cli_fmw.main.ClipsException;
import cli_fmw.main.PageContainer;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.chooser.ChoosePanelData;
import cli_fmw.utils.table_config_states.StateSaver;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.event.ChangeEvent;
import framework.utils.events.TypedChangeEvent;
import framework.utils.events.TypedChangeListener;
/**
*
* @author finder
*/
public class PanelTableEdit extends PanelWinthSubTabs implements TypedChangeListener<AbstractReportTable.TableChangeTypes> {
static final long serialVersionUID = 1L;
TableContainer<AbstractReportTable<?>> tableContainer;
AbstractReportTable<?> curTable;
DefaultListModel tables;
PanelFilterEdit filterEdit;
// DiseaseLinesPanel diseasePanel;
/** Creates new form TableEditPanel
* @param container
* @param tablec
* @throws ClipsException
*/
public PanelTableEdit(PageContainer container, TableContainer<AbstractReportTable<?>> tablec) throws ClipsException {
super(container);
if (tablec == null) {
throw new IllegalArgumentException("tablec can not be null");
}
tableContainer = tablec;
//tables = tableContainer.getTables();
initComponents();
filterEdit = new PanelFilterEdit(null);
pSubContainer.add(filterEdit);
initModels();
StateSaver.attachTo(this);
}
void initModels() throws ClipsException{
SelectorIndefiner[] selectors = tableContainer.getSelectorList();
ChoosePanelData[] data = new ChoosePanelData[selectors.length];
for (int i = 0; i < selectors.length; i++) {
data[i] = new ChoosePanelData(selectors[i].getId(), selectors[i].toString());
}
cpNewTable.setData(data, data[0], "Тип таблицы");
tables = new DefaultListModel();
List<AbstractReportTable<?>> tl;
try{
tl = tableContainer.getTables();
}
catch (Exception ex){
if (ex instanceof ClipsException) {
throw (ClipsException) ex;
}
throw new RuntimeException(ex);
}
for (AbstractReportTable ctable: tl) {
tables.addElement(ctable);
}
lsTableList.setModel(tables);
}
/** 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();
jPanel1 = new javax.swing.JPanel();
jPanel5 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
lsTableList = new javax.swing.JList();
jPanel4 = new javax.swing.JPanel();
cpNewTable = new cli_fmw.utils.chooser.ChoosePanel();
btAdd = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
btMoveUp = new javax.swing.JButton();
btMoveDown = new javax.swing.JButton();
btRemove = new javax.swing.JButton();
pSubContainer = new javax.swing.JPanel();
setLayout(new java.awt.BorderLayout());
jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel5.setLayout(new java.awt.BorderLayout());
lsTableList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
lsTableList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
lsTableList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
lsTableListValueChanged(evt);
}
});
jScrollPane1.setViewportView(lsTableList);
jPanel5.add(jScrollPane1, java.awt.BorderLayout.CENTER);
jPanel4.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
jPanel4.add(cpNewTable);
btAdd.setText("Добавить");
btAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAddActionPerformed(evt);
}
});
jPanel4.add(btAdd);
jPanel5.add(jPanel4, java.awt.BorderLayout.PAGE_START);
jPanel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
btMoveUp.setText("Вверх");
btMoveUp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btMoveUpActionPerformed(evt);
}
});
btMoveDown.setText("Вниз");
btMoveDown.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btMoveDownActionPerformed(evt);
}
});
btRemove.setText("Удалить");
btRemove.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btRemoveActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btRemove)
.addComponent(btMoveDown, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
.addComponent(btMoveUp, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(btRemove)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btMoveUp)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btMoveDown)
.addContainerGap(80, Short.MAX_VALUE))
);
jPanel5.add(jPanel3, java.awt.BorderLayout.EAST);
jPanel1.add(jPanel5, java.awt.BorderLayout.CENTER);
jSplitPane1.setTopComponent(jPanel1);
pSubContainer.setLayout(new javax.swing.BoxLayout(pSubContainer, javax.swing.BoxLayout.LINE_AXIS));
jSplitPane1.setRightComponent(pSubContainer);
add(jSplitPane1, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
ReportTableFactory fctr = new ReportTableFactory();
AbstractReportTable table = fctr.createByID(cpNewTable.getCurrent().value);
tables.addElement(table);
lsTableList.setSelectedValue(table, true);
stateChanged(new TypedChangeEvent<AbstractReportTable.TableChangeTypes>(table, AbstractReportTable.TableChangeTypes.lines));
}//GEN-LAST:event_btAddActionPerformed
private void lsTableListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lsTableListValueChanged
setCurTable((AbstractReportTable) lsTableList.getSelectedValue());
}//GEN-LAST:event_lsTableListValueChanged
private void btRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoveActionPerformed
AbstractReportTable delTable = (AbstractReportTable) lsTableList.getSelectedValue();
tables.removeElement(delTable);
stateChanged(new TypedChangeEvent<AbstractReportTable.TableChangeTypes>(delTable, AbstractReportTable.TableChangeTypes.lines));
}//GEN-LAST:event_btRemoveActionPerformed
private void btMoveUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMoveUpActionPerformed
AbstractReportTable moveTable = (AbstractReportTable) lsTableList.getSelectedValue();
int idx = tables.indexOf(moveTable);
if (idx > 0){
tables.setElementAt(tables.getElementAt(idx - 1), idx);
tables.setElementAt(moveTable, idx - 1);
lsTableList.setSelectedValue(moveTable, true);
}
stateChanged(new TypedChangeEvent<AbstractReportTable.TableChangeTypes>(moveTable, AbstractReportTable.TableChangeTypes.lines));
}//GEN-LAST:event_btMoveUpActionPerformed
private void btMoveDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMoveDownActionPerformed
AbstractReportTable moveTable = (AbstractReportTable) lsTableList.getSelectedValue();
int idx = tables.indexOf(moveTable);
if (idx >= 0 && idx < tables.size() - 1){
tables.setElementAt(tables.getElementAt(idx + 1), idx);
tables.setElementAt(moveTable, idx + 1);
lsTableList.setSelectedValue(moveTable, true);
}
stateChanged(new TypedChangeEvent<AbstractReportTable.TableChangeTypes>(moveTable, AbstractReportTable.TableChangeTypes.lines));
}//GEN-LAST:event_btMoveDownActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btAdd;
private javax.swing.JButton btMoveDown;
private javax.swing.JButton btMoveUp;
private javax.swing.JButton btRemove;
private cli_fmw.utils.chooser.ChoosePanel cpNewTable;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JList lsTableList;
private javax.swing.JPanel pSubContainer;
// End of variables declaration//GEN-END:variables
@SuppressWarnings({"unchecked", "unchecked"})
public void setCurTable(AbstractReportTable newCurTable) {
if (this.curTable != newCurTable){
if (curTable != null) {
curTable.removeChangeListener(this);
}
clearAllSubPage();
if (newCurTable != null){
newCurTable.addChangeListener(this);
try {
if (tableContainer.getMode() != TableContainer.Mode.onlyList){
if (newCurTable instanceof TableContainer){
addSubTab(new PanelTableEdit(getContainer(), (TableContainer)newCurTable));
}
if (newCurTable instanceof DiseaseTable){
addSubTab(new DiseaseLinesPanel(getContainer(), (DiseaseTable)newCurTable));
}
}
}
catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
filterEdit.setCurTable(newCurTable);
}
this.curTable = newCurTable;
updateButtons();
}
void updateButtons(){
btRemove.setEnabled(curTable != null);
btMoveUp.setEnabled(curTable != null);
btMoveDown.setEnabled(curTable != null);
}
@Override
public String getPageTitle() {
return "Таблицы";
}
@Override
public DelegateLine2 getDelegate() {
return null;
}
public void onTableChange() {
ArrayList<AbstractReportTable<?>> buf = new ArrayList<AbstractReportTable<?>>(tables.size());
for (int i = 0; i < tables.size(); i++) {
buf.add((AbstractReportTable) tables.get(i));
}
try {
tableContainer.setTables(buf);
} catch (Exception ex) {
MessageBox.showException(ex);
}
}
@Override
public TableChangeTypes getType() {
return null;
}
@Override
public void stateChanged(ChangeEvent e) {
onTableChange();
}
@Override
public void beforeClose() {
super.beforeClose();
setCurTable(null);
}
}