PageRenderTime 19ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

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