PageRenderTime 224ms CodeModel.GetById 90ms app.highlight 14ms RepoModel.GetById 117ms app.codeStats 0ms

/src/mpv5/utils/tables/DynamicTableCalculator.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 209 lines | 121 code | 27 blank | 61 comment | 30 complexity | 95c02a26df48f10a9c5f421d90dc98bf MD5 | raw file
  1
  2/*
  3 *  This file is part of YaBS.
  4 *
  5 *      YaBS is free software: you can redistribute it and/or modify
  6 *      it under the terms of the GNU General Public License as published by
  7 *      the Free Software Foundation, either version 3 of the License, or
  8 *      (at your option) any later version.
  9 *
 10 *      YaBS is distributed in the hope that it will be useful,
 11 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 *      GNU General Public License for more details.
 14 *
 15 *      You should have received a copy of the GNU General Public License
 16 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 17 */
 18package mpv5.utils.tables;
 19
 20import mpv5.logging.Log;
 21
 22import mpv5.ui.beans.LabeledTextField;
 23import mpv5.utils.models.MPTableModel;
 24import mpv5.utils.numberformat.FormatNumber;
 25import mpv5.utils.renderer.LazyCellEditor;
 26import java.math.BigDecimal;
 27import java.util.HashMap;
 28import javax.swing.JComponent;
 29import javax.swing.JEditorPane;
 30import javax.swing.JLabel;
 31import javax.swing.JTable;
 32import javax.swing.JTextArea;
 33import javax.swing.JTextField;
 34import javax.swing.table.TableCellEditor;
 35
 36/**
 37 * This class provides auto calculation functions for table cell values (Double.class required!), works only with {@link MPTableModel}s!
 38 */
 39public class DynamicTableCalculator implements Runnable {
 40
 41    private HashMap<Integer, JComponent> sumcols = new HashMap<Integer, JComponent>();
 42    private final JTable table;
 43    private final int[] targetColumns;
 44    private final String term;
 45
 46    /**
 47     *
 48     * @param table the table which we want to calculate
 49     * @param targetColumns the columns which old the result of a row
 50     * @param term the Formula to calculate
 51     */
 52    public DynamicTableCalculator(JTable table, String term, int[] targetColumns) {
 53        super();
 54        this.table = table;
 55        this.targetColumns = targetColumns;
 56        this.term = term;
 57    }
 58
 59    /**
 60     *
 61     * @param row
 62     * @return
 63     */
 64    public synchronized BigDecimal calculate(int row) {
 65        BigDecimal val = BigDecimal.ZERO;
 66        HashMap<Integer, BigDecimal> values = new HashMap<Integer, BigDecimal>();
 67        for (int i=0; i < table.getModel().getColumnCount(); i++) {
 68            if (table.getModel().getValueAt(row, i) != null && 
 69                    table.getModel().getValueAt(row, i).getClass().isInstance(BigDecimal.ZERO)) {
 70                values.put(i, (BigDecimal) table.getModel().getValueAt(row, i));
 71            } else if (table.getModel().getValueAt(row, i) != null && 
 72                    table.getModel().getValueAt(row, i).getClass().isInstance(new Double("0"))) {
 73                values.put(i, BigDecimal.valueOf( (Double) table.getModel().getValueAt(row, i)));
 74            }
 75        }
 76        try {
 77            DynamicArithmetic da = new DynamicArithmetic(term, values);
 78            val = da.result;
 79        } catch (ParseFormatException ex) {
 80            Log.Debug(this, ex);
 81        }
 82
 83        for (int i = 0; i < targetColumns.length; i++) {
 84            int j = targetColumns[i];
 85            ((MPTableModel) table.getModel()).setValueAt(val.doubleValue(), row, j, true);
 86        }
 87        return val;
 88    }
 89
 90    @Override
 91    public void run() {
 92        while (table.isShowing()) {
 93            if (!table.isEditing()) {
 94                calculateOnce();
 95
 96                try {
 97                    Thread.sleep(500);
 98                } catch (InterruptedException ex) {
 99                    Log.Debug(ex);
100                }
101            }
102        }
103    }
104
105    /**
106     * Start calculating, will run always while the table is showing on the screen
107     */
108    public void start() {
109        new Thread(this).start();
110    }
111
112    /**
113     * Calculate once
114     */
115    public void calculateOnce() {
116        for (int i = 0; i < table.getRowCount(); i++) {
117            calculate(i);
118        }
119
120        sumUp();
121    }
122
123    /**
124     * Checks whether the cell is not a target cell for this calculator
125     * @param row
126     * @param column
127     * @return
128     */
129    public boolean isTargetCell(int row, int column) {
130        for (int i = 0; i < targetColumns.length; i++) {
131            int col = targetColumns[i];
132
133            if (col == column) {
134                return true;
135            }
136        }
137
138        return false;
139    }
140
141    /**
142     * Define where the values are displayed
143     * @param value
144     * @param sumColumn
145     */
146    public void addLabel(LabeledTextField value, int sumColumn) {
147        sumcols.put(new Integer(sumColumn), value.getTextField());
148    }
149
150    /**
151     * Define where the values are displayed
152     * @param value
153     * @param sumColumn
154     */
155    public void addLabel(JTextField value, int sumColumn) {
156        sumcols.put(new Integer(sumColumn), value);
157    }
158
159    /**
160     * Define where the values are displayed
161     * @param value
162     * @param sumColumn
163     */
164    public void addLabel(JLabel value, int sumColumn) {
165        sumcols.put(new Integer(sumColumn), value);
166    }
167
168    private void sumUp() {
169        TableCellEditor e = table.getCellEditor();
170
171        if ((e != null) && (e instanceof LazyCellEditor)) {
172            ((LazyCellEditor) e).stopCellEditingSilent();
173        }
174
175        for (int i = 0; i < targetColumns.length; i++) {
176            int k = targetColumns[i];
177
178            if (sumcols.containsKey(new Integer(k))) {
179                Double ovalue = 0d;
180
181                for (int j = 0; j < table.getRowCount(); j++) {
182                    if (table.getModel().getValueAt(j, k) != null) {
183                        ovalue += Double.valueOf(table.getModel().getValueAt(j, k).toString());
184                    }
185                }
186
187                JComponent t = sumcols.get(new Integer(k));
188
189                if (t instanceof JLabel) {
190                    ((JLabel) t).setText(FormatNumber.formatDezimal(ovalue));
191                } else if (t instanceof JTextField) {
192                    ((JTextField) t).setText(FormatNumber.formatDezimal(ovalue));
193                } else if (t instanceof JTextArea) {
194                    ((JTextArea) t).setText(FormatNumber.formatDezimal(ovalue));
195                } else if (t instanceof JEditorPane) {
196                    ((JEditorPane) t).setText(FormatNumber.formatDezimal(ovalue));
197                }
198            }
199        }
200    }
201
202    /**
203     *
204     * @return
205     */
206    public JTable getTable() {
207        return table;
208    }
209}