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