PageRenderTime 21ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/utils/models/MPTableModelRow.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 133 lines | 69 code | 12 blank | 52 comment | 6 complexity | 548eab48bdb6f4b312ca210570603aff 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.models;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import mpv5.db.common.Context;
  22. import mpv5.db.common.DatabaseObject;
  23. import mpv5.db.common.NodataFoundException;
  24. import mpv5.logging.Log;
  25. /**
  26. *This class represents rows in a {@link MPTableModel}
  27. */
  28. public class MPTableModelRow {
  29. /**
  30. * Converts the given list into MPTableModelRows.
  31. * @param list
  32. * @return
  33. */
  34. public static MPTableModelRow[] toRows(List<DatabaseObject> list) {
  35. MPTableModelRow[] rows = new MPTableModelRow[list.size()];
  36. for (int i = 0; i < list.size(); i++) {
  37. DatabaseObject databaseObject = list.get(i);
  38. rows[i] =new MPTableModelRow(databaseObject);
  39. List<Object[]> sdata = databaseObject.getValues2();
  40. for (int j = 0; j < sdata.size(); j++) {
  41. Object[] strings = sdata.get(j);
  42. rows[i].setValueAt(strings[1], j);
  43. }
  44. }
  45. return rows;
  46. }
  47. /**
  48. * Converts the given array into MPTableModelRows. The first column of the array must be an int value as it is used for the IDS field of the row.
  49. * @param context
  50. * @param object
  51. * @return
  52. */
  53. public static MPTableModelRow[] toRows(Context context, Object[][] object) {
  54. MPTableModelRow[] rows = new MPTableModelRow[object.length];
  55. for (int i = 0; i < rows.length; i++) {
  56. if (Integer.valueOf(object[i][0].toString()).intValue() > 0) {
  57. try {
  58. rows[i] = new MPTableModelRow(DatabaseObject.getObject(context, Integer.valueOf(object[i][0].toString()).intValue()));
  59. } catch (NodataFoundException ex) {
  60. Log.Debug(ex);
  61. rows[i] = new MPTableModelRow(DatabaseObject.getObject(context));
  62. }
  63. } else {
  64. rows[i] = new MPTableModelRow(DatabaseObject.getObject(context));
  65. }
  66. for (int j = 1; j < object[i].length; j++) {
  67. rows[i].setValueAt(object[i][j], j - 1);
  68. }
  69. }
  70. return rows;
  71. }
  72. private Context context;
  73. private int ids;
  74. private HashMap<Integer, Object> values = new HashMap<Integer, Object>();
  75. private final DatabaseObject obj;
  76. public MPTableModelRow(final DatabaseObject obj) {
  77. this.obj = obj;
  78. this.context = obj.getContext();
  79. this.ids = obj.__getIDS();
  80. }
  81. /**
  82. * @return the context
  83. */
  84. public Context getContext() {
  85. return context;
  86. }
  87. /**
  88. * @return the ids
  89. */
  90. public int getIds() {
  91. return ids;
  92. }
  93. /**
  94. * @return the obj
  95. */
  96. public DatabaseObject getObj() {
  97. return obj;
  98. }
  99. /**
  100. * @return the values
  101. */
  102. public HashMap<Integer, Object> getValues() {
  103. return values;
  104. }
  105. /**
  106. * Returns the value at the given column
  107. * @param column
  108. * @return
  109. */
  110. public Object getValueAt(int column) {
  111. return values.get(new Integer(column));
  112. }
  113. /**
  114. * Set or replace the value at the given column
  115. * @param value
  116. * @param column
  117. */
  118. public void setValueAt(Object value, int column) {
  119. values.put(new Integer(column), value);
  120. }
  121. }