PageRenderTime 165ms CodeModel.GetById 81ms app.highlight 47ms RepoModel.GetById 33ms app.codeStats 0ms

/src/mpv5/utils/tables/TableCalculator.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 365 lines | 215 code | 62 blank | 88 comment | 61 complexity | 6b66e2633c47f48f0486a554f677fb1c 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
 20//~--- non-JDK imports --------------------------------------------------------
 21
 22import mpv5.logging.Log;
 23
 24import mpv5.ui.beans.LabeledTextField;
 25
 26import mpv5.utils.models.MPTableModel;
 27import mpv5.utils.numberformat.FormatNumber;
 28import mpv5.utils.renderer.LazyCellEditor;
 29
 30//~--- JDK imports ------------------------------------------------------------
 31
 32import java.math.BigDecimal;
 33
 34
 35import java.util.HashMap;
 36
 37import javax.swing.JComponent;
 38import javax.swing.JEditorPane;
 39import javax.swing.JLabel;
 40import javax.swing.JTable;
 41import javax.swing.JTextArea;
 42import javax.swing.JTextField;
 43import javax.swing.table.TableCellEditor;
 44
 45/**
 46 * This class provides auto calculation functions for table cell values (Double.class required!), works only with {@link MPTableModel}s!
 47 * @Deprecated 
 48 * use <link>DynamicTableCalculator</link> DynamicArithmetic for being more flexible
 49 */
 50@Deprecated
 51public class TableCalculator implements Runnable {
 52
 53    /**
 54     * <b>/</b>
 55     */
 56    public static final int ACTION_DIVIDE = 1;
 57
 58    /**
 59     * <b>*</b>
 60     */
 61    public static final int ACTION_MULTIPLY = 3;
 62
 63    /**
 64     * <b>-</b>
 65     */
 66    public static final int ACTION_SUBSTRACT = 2;
 67
 68    /**
 69     * <b>+</b>
 70     */
 71    public static final int              ACTION_SUM = 0;
 72    private boolean                      onScreen   = true;
 73    private HashMap<Integer, JComponent> sumcols    = new HashMap<Integer, JComponent>();
 74    private final int                    action;
 75    private final int[]                  columnsToCalculate;
 76    private final int[]                  percentageColumns;
 77    private int[]                        sumColumn;
 78
 79//  public static NumberFormat DECIMALFORMAT = FormatNumber.getDefaultDecimalFormat();
 80    private final JTable table;
 81    private final int[]  targetColumns;
 82
 83    /**
 84     *
 85     * @param table the table which we want to calculate
 86     * @param columnsToCalculate the columns to calculate
 87     * @param targetColumns the columns which old the result of a row
 88     * @param percentageColumns The columsn which shall be treatened as percent values
 89     * @param action The action to take
 90     * @param sumColumn The columns to sum up vertically (+)
 91     */
 92    public TableCalculator(JTable table, int[] columnsToCalculate, int[] targetColumns, int[] percentageColumns,
 93                           int action, int[] sumColumn) {
 94        super();
 95        this.table              = table;
 96        this.columnsToCalculate = columnsToCalculate;
 97        this.targetColumns      = targetColumns;
 98        this.percentageColumns  = percentageColumns;
 99        this.action             = action;
100        this.sumColumn          = sumColumn;
101    }
102
103    /**
104     *
105     * @param row
106     * @return
107     */
108    public synchronized BigDecimal calculate(int row) {
109        BigDecimal val = BigDecimal.ZERO;
110
111        try {
112            switch (action) {
113            case ACTION_SUM :
114                for (int i = 0; i < columnsToCalculate.length; i++) {
115                    int j = columnsToCalculate[i];
116
117                    if (table.getModel().getValueAt(row, j) != null) {
118                        val.add(BigDecimal.valueOf(Double.valueOf(table.getModel().getValueAt(row, j).toString())));
119                    }
120                }
121
122                break;
123
124            case ACTION_SUBSTRACT :
125                for (int i = 0; i < columnsToCalculate.length; i++) {
126                    int j = columnsToCalculate[i];
127
128                    if (table.getModel().getValueAt(row, j) != null) {
129                        if (i == 0) {
130                            val = BigDecimal.valueOf(Double.valueOf(table.getModel().getValueAt(row, j).toString()));
131                        } else {
132                            val = val.subtract(BigDecimal.valueOf(Double.valueOf(table.getModel().getValueAt(row,
133                                    j).toString())));
134                        }
135                    }
136                }
137
138                break;
139
140            case ACTION_DIVIDE :
141                for (int i = 0; i < columnsToCalculate.length; i++) {
142                    int j = columnsToCalculate[i];
143
144                    if (table.getModel().getValueAt(row, j) != null) {
145                        boolean percentagec = false;
146
147                        for (int k = 0; k < percentageColumns.length; k++) {
148                            int l = percentageColumns[k];
149
150                            if (l == j) {
151                                percentagec = true;
152                            }
153                        }
154
155                        if (!percentagec) {
156                            if (i == 0) {
157                                val = BigDecimal.valueOf(Double.valueOf(table.getModel().getValueAt(row,
158                                        j).toString()));
159                            } else {
160                                val = val.divide(BigDecimal.valueOf(Double.valueOf(table.getModel().getValueAt(row,
161                                        j).toString())), 2, BigDecimal.ROUND_HALF_UP);
162                            }
163                        } else {
164                            if (i == 0) {
165                                val = BigDecimal.valueOf(((Double.valueOf(table.getModel().getValueAt(row,
166                                        j).toString()) / 100) + 1d));
167                            } else {
168                                val = val.divide(BigDecimal.valueOf((Double.valueOf(table.getModel().getValueAt(row,
169                                        j).toString()) / 100) + 1), 2, BigDecimal.ROUND_HALF_UP);
170                            }
171                        }
172                    }
173                }
174
175                break;
176
177            case ACTION_MULTIPLY :
178                for (int i = 0; i < columnsToCalculate.length; i++) {
179                    int j = columnsToCalculate[i];
180
181                    if (table.getModel().getValueAt(row, j) != null) {
182                        boolean percentagec = false;
183
184                        for (int k = 0; k < percentageColumns.length; k++) {
185                            int l = percentageColumns[k];
186
187                            if (l == j) {
188                                percentagec = true;
189                            }
190                        }
191
192                        if (!percentagec) {
193                            if (i == 0) {
194                                val = BigDecimal.valueOf(Double.valueOf(table.getModel().getValueAt(row,
195                                        j).toString()));
196                            } else {
197                                val = val.multiply(BigDecimal.valueOf(Double.valueOf(table.getModel().getValueAt(row,
198                                        j).toString())));
199                            }
200                        } else {
201                            if (i == 0) {
202                                val = BigDecimal.valueOf(((Double.valueOf(table.getModel().getValueAt(row,
203                                        j).toString()) / 100) + 1));
204                            } else {
205                                val = val.multiply(BigDecimal.valueOf((Double.valueOf(table.getModel().getValueAt(row,
206                                        j).toString()) / 100) + 1));
207                            }
208                        }
209                    }
210                }
211
212                break;
213            }
214
215            for (int i = 0; i < targetColumns.length; i++) {
216                int j = targetColumns[i];
217
218                ((MPTableModel) table.getModel()).setValueAt(val.doubleValue(), row, j, true);
219            }
220        } catch (NumberFormatException numberFormatException) {
221            Log.Debug(this, numberFormatException.getMessage());
222        }
223
224        return val;
225    }
226
227    @Override
228    public void run() {
229        while (isUsed()) {
230            while (table.isShowing()) {
231                if (!table.isEditing()) {
232                    calculateOnce();
233
234                    try {
235                        Thread.sleep(500);
236                    } catch (InterruptedException ex) {
237                        Log.Debug(ex);
238                    }
239                }
240            }
241        }
242    }
243
244    /**
245     * Start calculating, will run always while the table is showing on the screen
246     */
247    public void start() {
248        new Thread(this).start();
249    }
250
251    /**
252     * Calculate once
253     */
254    public void calculateOnce() {
255        for (int i = 0; i < table.getRowCount(); i++) {
256            calculate(i);
257        }
258
259        sumUp();
260    }
261
262    /**
263     * @return the onScreen
264     */
265    public boolean isUsed() {
266        return onScreen;
267    }
268
269    /**
270     * @param onScreen the onScreen to set
271     */
272    public void setOnScreen(boolean onScreen) {
273        this.onScreen = onScreen;
274    }
275
276    /**
277     * Checks whether the cell is not a target cell for this calculator
278     * @param row
279     * @param column
280     * @return
281     */
282    public boolean isTargetCell(int row, int column) {
283        for (int i = 0; i < targetColumns.length; i++) {
284            int col = targetColumns[i];
285
286            if (col == column) {
287                return true;
288            }
289        }
290
291        return false;
292    }
293
294    /**
295     * Define where the values are displayed
296     * @param value
297     * @param sumColumn
298     */
299    public void addLabel(LabeledTextField value, int sumColumn) {
300        sumcols.put(new Integer(sumColumn), value.getTextField());
301    }
302
303    /**
304     * Define where the values are displayed
305     * @param value
306     * @param sumColumn
307     */
308    public void addLabel(JTextField value, int sumColumn) {
309        sumcols.put(new Integer(sumColumn), value);
310    }
311
312    /**
313     * Define where the values are displayed
314     * @param value
315     * @param sumColumn
316     */
317    public void addLabel(JLabel value, int sumColumn) {
318        sumcols.put(new Integer(sumColumn), value);
319    }
320
321    private void sumUp() {
322        TableCellEditor e = table.getCellEditor();
323
324        if ((e != null) && (e instanceof LazyCellEditor)) {
325            ((LazyCellEditor) e).stopCellEditingSilent();
326        }
327
328        for (int i = 0; i < sumColumn.length; i++) {
329            int k = sumColumn[i];
330
331            if (sumcols.containsKey(new Integer(k))) {
332                Double ovalue = 0d;
333
334                for (int j = 0; j < table.getRowCount(); j++) {
335                    if (table.getModel().getValueAt(j, k) != null) {
336                        ovalue += Double.valueOf(table.getModel().getValueAt(j, k).toString());
337                    }
338                }
339
340                JComponent t = sumcols.get(new Integer(k));
341
342                if (t instanceof JLabel) {
343                    ((JLabel) t).setText(FormatNumber.formatDezimal(ovalue));
344                } else if (t instanceof JTextField) {
345                    ((JTextField) t).setText(FormatNumber.formatDezimal(ovalue));
346                } else if (t instanceof JTextArea) {
347                    ((JTextArea) t).setText(FormatNumber.formatDezimal(ovalue));
348                } else if (t instanceof JEditorPane) {
349                    ((JEditorPane) t).setText(FormatNumber.formatDezimal(ovalue));
350                }
351            }
352        }
353    }
354
355    /**
356     *
357     * @return
358     */
359    public JTable getTable() {
360        return table;
361    }
362}
363
364
365//~ Formatted by Jindent --- http://www.jindent.com