/org.adempiere.base/src/org/adempiere/util/GridRowCtx.java

https://bitbucket.org/idempiere/idempiere/ · Java · 239 lines · 191 code · 27 blank · 21 comment · 31 complexity · 30287511060113d6791d22774e557823 MD5 · raw file

  1. /**
  2. *
  3. */
  4. package org.adempiere.util;
  5. import java.util.Collection;
  6. import java.util.Enumeration;
  7. import java.util.Map;
  8. import java.util.Properties;
  9. import java.util.Set;
  10. import org.compiere.model.GridField;
  11. import org.compiere.model.GridTab;
  12. import org.compiere.model.GridTable;
  13. import org.compiere.util.Env;
  14. import org.compiere.util.Evaluatee;
  15. import org.compiere.util.KeyNamePair;
  16. import org.compiere.util.ValueNamePair;
  17. /**
  18. * Context (Properties) wrapper to be able to evaluate grid row context
  19. * @author Teo Sarca, teo.sarca@gmail.com
  20. */
  21. public class GridRowCtx extends Properties
  22. implements Evaluatee
  23. {
  24. /**
  25. *
  26. */
  27. private static final long serialVersionUID = 8163657930039348267L;
  28. private final Properties ctx;
  29. private final GridTab gridTab;
  30. private final GridTable gridTable;
  31. private final int windowNo;
  32. private final int row;
  33. public GridRowCtx(Properties ctx, GridTab tab, int row)
  34. {
  35. super();
  36. this.ctx = ctx;
  37. this.gridTab = tab;
  38. this.gridTable = tab.getTableModel();
  39. this.windowNo = tab.getWindowNo();
  40. this.row = row;
  41. }
  42. public GridRowCtx(Properties ctx, GridTable table, int windowNo, int row)
  43. {
  44. super();
  45. this.ctx = ctx;
  46. this.gridTab = null;
  47. this.gridTable = table;
  48. this.windowNo = windowNo;
  49. this.row = row;
  50. }
  51. private String getColumnName(Object key)
  52. {
  53. if (! (key instanceof String) )
  54. return null;
  55. String windowStr = windowNo+"|";
  56. String keyStr = (String)key;
  57. if (!keyStr.startsWith(windowStr))
  58. {
  59. return null;
  60. }
  61. String columnName = keyStr.substring(windowStr.length()).trim();
  62. //strip tab no too
  63. if (columnName.indexOf("|") > 0) {
  64. columnName = columnName.substring(columnName.indexOf("|")+1);
  65. }
  66. return columnName;
  67. }
  68. @Override
  69. public synchronized Object get(Object key)
  70. {
  71. String columnName = getColumnName(key);
  72. if (columnName == null)
  73. {
  74. return ctx.get(key);
  75. }
  76. int col = gridTable.findColumn(columnName);
  77. if (col == -1)
  78. {
  79. return ctx.get(key);
  80. }
  81. Object value = gridTable.getValueAt(row, col);
  82. if (value == null)
  83. {
  84. value = "";
  85. }
  86. else if (value instanceof KeyNamePair)
  87. {
  88. value = ((KeyNamePair)value).getKey();
  89. }
  90. else if (value instanceof ValueNamePair)
  91. {
  92. value = ((ValueNamePair)value).getID();
  93. }
  94. else if (value instanceof Boolean)
  95. {
  96. value = ((Boolean)value).booleanValue() ? "Y" : "N";
  97. }
  98. return value.toString();
  99. }
  100. @Override
  101. public synchronized void clear() {
  102. ctx.clear();
  103. }
  104. @Override
  105. public synchronized Object clone() {
  106. final GridRowCtx grc;
  107. if (this.gridTab != null)
  108. grc = new GridRowCtx((Properties)this.ctx.clone(), this.gridTab, this.row);
  109. else
  110. grc = new GridRowCtx((Properties)this.ctx.clone(), this.gridTable, this.windowNo, this.row);
  111. return grc;
  112. }
  113. @Override
  114. public synchronized boolean contains(Object value) {
  115. // TODO: check if that value exists in one of our GridFields
  116. return this.ctx.contains(value);
  117. }
  118. @Override
  119. public synchronized boolean containsKey(Object key)
  120. {
  121. String columnName = getColumnName(key);
  122. if (columnName != null && gridTable.findColumn(columnName) != -1)
  123. return true;
  124. return ctx.containsKey(key);
  125. }
  126. @Override
  127. public boolean containsValue(Object value) {
  128. // TODO: check if that value exists in one of our GridFields
  129. return ctx.containsValue(value);
  130. }
  131. @Override
  132. public synchronized Enumeration<Object> elements() {
  133. // TODO: implement for GridField values too
  134. return ctx.elements();
  135. }
  136. @Override
  137. public Set<java.util.Map.Entry<Object, Object>> entrySet() {
  138. // TODO: implement for GridField values too
  139. return ctx.entrySet();
  140. }
  141. @Override
  142. public synchronized boolean isEmpty() {
  143. return false;
  144. }
  145. @Override
  146. public synchronized Enumeration<Object> keys() {
  147. // TODO: implement for GridField values too
  148. return ctx.keys();
  149. }
  150. @Override
  151. public Set<Object> keySet() {
  152. // TODO: implement for GridField values too
  153. return ctx.keySet();
  154. }
  155. @Override
  156. public synchronized Object put(Object key, Object value)
  157. {
  158. if (gridTab == null)
  159. throw new IllegalStateException("Method not supported (gridTab is null)");
  160. if (gridTab.getCurrentRow() != row)
  161. {
  162. return ctx.put(key, value);
  163. }
  164. String columnName = getColumnName(key);
  165. if (columnName == null)
  166. {
  167. return ctx.put(key, value);
  168. }
  169. GridField field = gridTab.getField(columnName);
  170. if (field == null)
  171. {
  172. return ctx.put(key, value);
  173. }
  174. Object valueOld = field.getValue();
  175. field.setValue(value, false);
  176. return valueOld;
  177. }
  178. @Override
  179. public synchronized void putAll(Map<? extends Object, ? extends Object> t) {
  180. for (Map.Entry<? extends Object, ? extends Object> e : t.entrySet())
  181. put(e.getKey(), e.getValue());
  182. }
  183. @Override
  184. public synchronized Object remove(Object key) {
  185. // TODO: implement for GridField values too
  186. return ctx.remove(key);
  187. }
  188. @Override
  189. public synchronized int size() {
  190. // TODO: implement for GridField values too
  191. return ctx.size();
  192. }
  193. @Override
  194. public synchronized String toString() {
  195. // TODO: implement for GridField values too
  196. return ctx.toString();
  197. }
  198. @Override
  199. public Collection<Object> values() {
  200. return ctx.values();
  201. }
  202. @Override
  203. public String getProperty(String key) {
  204. // I need to override this method, because Properties.getProperty method is calling super.get() instead of get()
  205. Object oval = get(key);
  206. return oval == null ? null : oval.toString();
  207. }
  208. public String get_ValueAsString(String variableName)
  209. {
  210. return Env.getContext (this, windowNo, variableName, true);
  211. }
  212. }