Package reportgen.gui.genepanel.formatpanel.dlg.main

Source Code of reportgen.gui.genepanel.formatpanel.dlg.main.TableFormatDlg$MuListSelectionListener

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* TableFormatDlg.java
*
* Created on 22.06.2009, 10:47:55
*/
package reportgen.gui.genepanel.formatpanel.dlg.main;

import reportgen.gui.genepanel.formatpanel.dlg.*;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.jdom.Element;
import reportgen.gui.genepanel.formatpanel.RangeParser;
import reportgen.ren.report.extendedformat.table.TableFormat;
import reportgen.utils.Message;
import reportgen.utils.ReportDialog;
import reportgen.utils.ReportException;
import simplesheet.model.selection.CellPosition;
import simplesheet.model.selection.SheetSelectionEvent;
import simplesheet.model.selection.SheetSelectionModel;
import simplesheet.SheetTable;
import simplesheet.style.Style;
import simplesheet.model.cell.StyleDefault;
import simplesheet.model.cell.TableCell;
import simplesheet.model.selection.SheetSelectionListener;

/**
*
* @author axe
*/
public class TableFormatDlg extends ReportDialog implements TableListener {

    private final TableFormat format;
    private SheetTable table;
    private SheetModel model;

    /** Creates new form TableFormatDlg */
    public TableFormatDlg(Window parent, TableFormat aFormat) throws ReportException {
        super(parent, "Таблица результатов");
        initComponents();
        this.format = aFormat;

        rebuildTable();

        this.tabletitle.setText(this.format.getTitle());
        this.tabletitle.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                update(e.getDocument());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                update(e.getDocument());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }

            private void update(Document doc) {
                try {
                    format.setTitle(doc.getText(0, doc.getLength()));
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                    //do nothing
                }
            }
        });
       
        if(model.getRowCount() == 0 || model.getColumnCount() == 0) {
            new CornerCell(parent, format, this).actionPerformed(null);
        }       
    }

    private void rebuildTable() {
        mainPanel.removeAll();
        try {
            model = new SheetModel(format, this, this);
            //corner
            table = new SheetTable(model);
            mainPanel.add(table);
            mainPanel.revalidate();

            table.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() != 2) {
                        return;
                    }
                    SheetSelectionModel selModel = table.getSelectionModel();
                    CellPosition lead = selModel.getLead();
                    if(lead == null) {
                        return;
                    }
                    CellPosition pos = model.getOrigin(lead);
                    TableCell cell = model.getValueAt(pos);
                    if (cell instanceof ActionListener) {
                        ActionListener al = (ActionListener) cell;
                        al.actionPerformed(null);
                    }
                }
            });

            table.getSelectionModel().addSheetSelectionListener(new MuListSelectionListener());
        } catch (ReportException reportException) {
            Message.error(table, reportException);
        }
    }

    private class MuListSelectionListener implements SheetSelectionListener {

        @Override
        public void valueChanged(SheetSelectionEvent e) {
            if (e.isAdjusting) {
                //only finished selection
                return;
            }
            SheetSelectionModel smod = table.getSelectionModel();
            if(smod.isSelectionEmpty()
                    || smod.getAnchor().equals(smod.getLead())) {
                //empty selection or single cell
                return;
            }

            //without head
            boolean dataAvailiable = true;
            int headHeight = RangeParser.getDimensions(format.getCols().getList()).height;
            if (smod.getAnchor().row < headHeight
                    || smod.getLead().row < headHeight) {
                dataAvailiable = false;
            }
            //without left head
            int leftHeadHeight = RangeParser.getDimensions(format.getRows().getList()).height;
            if (smod.getAnchor().col < leftHeadHeight
                    || smod.getLead().col < leftHeadHeight) {
                dataAvailiable = false;
            }
            checkIsFillRange(smod.getAnchor(), smod.getLead(), dataAvailiable);
        }

        /**
         *
         */
        private void checkIsFillRange(CellPosition anchor, CellPosition lead, boolean dataAvailiable) {
            AutoFillDlg autoFill = new AutoFillDlg(TableFormatDlg.this, dataAvailiable);
            autoFill.setVisible(true);
            if (!autoFill.isOK()) {
                return;
            }

            //save SRC value to stream
            TableCell tableCell = model.getValueAt(anchor);
            Element savedStream = null;
            if(tableCell instanceof SheetCell) {
                SheetCell sc = (SheetCell) tableCell;
                savedStream = new Element("save");
                sc.getCellFormat().saveState(savedStream);
            }
            Style style = tableCell.getStyle();

            //iterate dest
            int dif = (anchor.row < lead.row) ? 1 : -1;
            int dif2 = (anchor.col < lead.col) ? 1 : -1;
            int failed = 0;
            for (int iRow = anchor.row; iRow != lead.row+dif; iRow += dif) {
                for (int iCol=anchor.col; iCol != lead.col+dif2; iCol += dif2) {
                    if (iCol == anchor.col && iRow == anchor.row) {
                        continue;
                    }
                    try {
                        fillCell(iRow, iCol, autoFill, savedStream, style);
                    } catch (ReportException ex) {
                        failed++;
                    }
                }
            }
            if (failed > 0) {
                Message.warning(TableFormatDlg.this,
                        "Не удалось назначить значение " + failed + " ячейкам.");
            }
        }

        private void fillCell(int iRow, int iCol,
                AutoFillDlg autoFill, Element savedStream, Style style) throws ReportException {
            TableCell iCell = model.getValueAt(new CellPosition(iRow, iCol));
            if (iCell instanceof SheetCell) {
                SheetCell sc = (SheetCell) iCell;
                if (autoFill.isCopyValue() && savedStream != null) {
                    sc.getCellFormat().restoreState(savedStream);
                }
            }
            if (style == null) {
                return;
            }
            Style newStyle = iCell.getStyle();
            if (autoFill.isCopyStyle() && newStyle == null) {
                newStyle = new StyleDefault();
            }
            if (autoFill.isCopyColor()) {
                newStyle.setForeground(style.getForeground());
                newStyle.setBackground(style.getBackground());
            }
            if (autoFill.isCopyFont()) {
                newStyle.setFont(style.getFont());
            }
            if (autoFill.isCopyMargin()) {
                newStyle.setMargin(style.getMargin());
            }
            if (autoFill.isCopyBorder()) {
                newStyle.setBorder(style.getBorder());
            }
            if (autoFill.isCopyAlign()) {
                newStyle.setTextAlign(style.getTextAlign());
                newStyle.setTextVAlign(style.getTextVAlign());
            }
            iCell.setStyle(newStyle);
        }
    }

    @Override
    public void dataChanged() {
        rebuildTable();
    }

    @Override
    public void cellChanged(int row, int col) {
        model.fireSheetCellUpdated(new CellPosition(row, col));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabletitle = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        mainPanel = new javax.swing.JPanel();
        toolBar = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        okBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new java.awt.BorderLayout(5, 5));

        jPanel3.setPreferredSize(new java.awt.Dimension(597, 50));
        jPanel3.setLayout(new java.awt.BorderLayout(5, 5));

        jLabel1.setText("Название таблицы");
        jPanel3.add(jLabel1, java.awt.BorderLayout.WEST);

        jScrollPane2.setPreferredSize(new java.awt.Dimension(50, 98));

        tabletitle.setColumns(20);
        tabletitle.setRows(5);
        jScrollPane2.setViewportView(tabletitle);

        jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel3, java.awt.BorderLayout.NORTH);

        mainPanel.setLayout(new java.awt.GridLayout(1, 0));
        jScrollPane1.setViewportView(mainPanel);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        toolBar.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        okBtn.setText("    ОК    ");
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBtnActionPerformed(evt);
            }
        });
        jPanel2.add(okBtn);

        cancelBtn.setText("Отмена");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });
        jPanel2.add(cancelBtn);

        toolBar.add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel1.add(toolBar, java.awt.BorderLayout.SOUTH);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 594, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 399, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
        setDlgResult(true);
        setVisible(false);
    }//GEN-LAST:event_okBtnActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancelBtnActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton okBtn;
    private javax.swing.JTextArea tabletitle;
    private javax.swing.JPanel toolBar;
    // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of reportgen.gui.genepanel.formatpanel.dlg.main.TableFormatDlg$MuListSelectionListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.