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