PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src-beans/com/openbravo/format/Formats.java

https://gitlab.com/martynbristow/UnicentaPOS
Java | 484 lines | 308 code | 47 blank | 129 comment | 35 complexity | f3221b4f1f558e9fcea4afc930d68ed0 MD5 | raw file
  1. // uniCenta oPOS - Touch Friendly Point Of Sale
  2. // Copyright (c) 2009-2014 uniCenta & previous Openbravo POS works
  3. // http://www.unicenta.com
  4. //
  5. // This file is part of uniCenta oPOS
  6. //
  7. // uniCenta oPOS is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // uniCenta oPOS is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with uniCenta oPOS. If not, see <http://www.gnu.org/licenses/>.
  19. package com.openbravo.format;
  20. import java.text.*;
  21. import java.util.Date;
  22. import com.openbravo.basic.BasicException;
  23. /**
  24. *
  25. * @author JG uniCenta
  26. */
  27. public abstract class Formats {
  28. /**
  29. *
  30. */
  31. public final static Formats NULL = new FormatsNULL();
  32. /**
  33. *
  34. */
  35. public final static Formats INT = new FormatsINT();
  36. /**
  37. *
  38. */
  39. public final static Formats STRING = new FormatsSTRING();
  40. /**
  41. *
  42. */
  43. public final static Formats DOUBLE = new FormatsDOUBLE();
  44. /**
  45. *
  46. */
  47. public final static Formats CURRENCY = new FormatsCURRENCY();
  48. /**
  49. *
  50. */
  51. public final static Formats PERCENT = new FormatsPERCENT();
  52. /**
  53. *
  54. */
  55. public final static Formats BOOLEAN = new FormatsBOOLEAN();
  56. /**
  57. *
  58. */
  59. public final static Formats TIMESTAMP = new FormatsTIMESTAMP();
  60. /**
  61. *
  62. */
  63. public final static Formats DATE = new FormatsDATE();
  64. /**
  65. *
  66. */
  67. public final static Formats TIME = new FormatsTIME();
  68. /**
  69. *
  70. */
  71. public final static Formats BYTEA = new FormatsBYTEA();
  72. /**
  73. *
  74. */
  75. public final static Formats HOURMIN = new FormatsHOURMIN();
  76. /** Added; Thanks TSirwani 3 Mar 11 */
  77. public final static Formats SIMPLEDATE = new FormatsSIMPLEDATE();
  78. private static NumberFormat m_integerformat = NumberFormat.getIntegerInstance();
  79. private static NumberFormat m_doubleformat = NumberFormat.getNumberInstance();
  80. private static NumberFormat m_currencyformat = NumberFormat.getCurrencyInstance();
  81. private static NumberFormat m_percentformat = new DecimalFormat("#,##0.##%");
  82. private static DateFormat m_dateformat = DateFormat.getDateInstance();
  83. private static DateFormat m_timeformat = DateFormat.getTimeInstance();
  84. private static DateFormat m_datetimeformat = DateFormat.getDateTimeInstance();
  85. private static final DateFormat m_hourminformat = new SimpleDateFormat("H:mm:ss");
  86. private static final DateFormat m_simpledate = new SimpleDateFormat("dd-MM-yyyy");
  87. /** Creates a new instance of Formats */
  88. protected Formats() {
  89. }
  90. /**
  91. *
  92. * @return
  93. */
  94. public static int getCurrencyDecimals() {
  95. return m_currencyformat.getMaximumFractionDigits();
  96. }
  97. /**
  98. *
  99. * @param value
  100. * @return
  101. */
  102. public String formatValue(Object value) {
  103. if (value == null) {
  104. return "";
  105. } else {
  106. return formatValueInt(value);
  107. }
  108. }
  109. /**
  110. *
  111. * @param value
  112. * @param defvalue
  113. * @return
  114. * @throws BasicException
  115. */
  116. public Object parseValue(String value, Object defvalue) throws BasicException {
  117. if (value == null || "".equals(value)) {
  118. return defvalue;
  119. } else {
  120. try {
  121. return parseValueInt(value);
  122. } catch (ParseException e) {
  123. throw new BasicException(e.getMessage(), e);
  124. }
  125. }
  126. }
  127. /**
  128. *
  129. * @param value
  130. * @return
  131. * @throws BasicException
  132. */
  133. public Object parseValue(String value) throws BasicException {
  134. return parseValue(value, null);
  135. }
  136. /**
  137. *
  138. * @param pattern
  139. */
  140. public static void setIntegerPattern(String pattern) {
  141. if (pattern == null || pattern.equals("")) {
  142. m_integerformat = NumberFormat.getIntegerInstance();
  143. } else {
  144. m_integerformat = new DecimalFormat(pattern);
  145. }
  146. }
  147. /**
  148. *
  149. * @param pattern
  150. */
  151. public static void setDoublePattern(String pattern) {
  152. if (pattern == null || pattern.equals("")) {
  153. m_doubleformat = NumberFormat.getNumberInstance();
  154. } else {
  155. m_doubleformat = new DecimalFormat(pattern);
  156. }
  157. }
  158. /**
  159. *
  160. * @param pattern
  161. */
  162. public static void setCurrencyPattern(String pattern) {
  163. if (pattern == null || pattern.equals("")) {
  164. m_currencyformat = NumberFormat.getCurrencyInstance();
  165. } else {
  166. m_currencyformat = new DecimalFormat(pattern);
  167. }
  168. }
  169. /**
  170. *
  171. * @param pattern
  172. */
  173. public static void setPercentPattern(String pattern) {
  174. if (pattern == null || pattern.equals("")) {
  175. m_percentformat = new DecimalFormat("#,##0.##%");
  176. } else {
  177. m_percentformat = new DecimalFormat(pattern);
  178. }
  179. }
  180. /**
  181. *
  182. * @param pattern
  183. */
  184. public static void setDatePattern(String pattern) {
  185. if (pattern == null || pattern.equals("")) {
  186. m_dateformat = DateFormat.getDateInstance();
  187. } else {
  188. m_dateformat = new SimpleDateFormat(pattern);
  189. }
  190. }
  191. /**
  192. *
  193. * @param pattern
  194. */
  195. public static void setTimePattern(String pattern) {
  196. if (pattern == null || pattern.equals("")) {
  197. m_timeformat = DateFormat.getTimeInstance();
  198. } else {
  199. m_timeformat = new SimpleDateFormat(pattern);
  200. }
  201. }
  202. /**
  203. *
  204. * @param pattern
  205. */
  206. public static void setDateTimePattern(String pattern) {
  207. if (pattern == null || pattern.equals("")) {
  208. m_datetimeformat = DateFormat.getDateTimeInstance();
  209. } else {
  210. m_datetimeformat = new SimpleDateFormat(pattern);
  211. }
  212. }
  213. /**
  214. *
  215. * @param value
  216. * @return
  217. */
  218. protected abstract String formatValueInt(Object value);
  219. /**
  220. *
  221. * @param value
  222. * @return
  223. * @throws ParseException
  224. */
  225. protected abstract Object parseValueInt(String value) throws ParseException;
  226. /**
  227. *
  228. * @return
  229. */
  230. public abstract int getAlignment();
  231. private static final class FormatsNULL extends Formats {
  232. @Override
  233. protected String formatValueInt(Object value) {
  234. return null;
  235. }
  236. @Override
  237. protected Object parseValueInt(String value) throws ParseException {
  238. return null;
  239. }
  240. @Override
  241. public int getAlignment() {
  242. return javax.swing.SwingConstants.LEFT;
  243. }
  244. }
  245. private static final class FormatsINT extends Formats {
  246. @Override
  247. protected String formatValueInt(Object value) {
  248. return m_integerformat.format(((Number) value).longValue());
  249. }
  250. @Override
  251. protected Object parseValueInt(String value) throws ParseException {
  252. return m_integerformat.parse(value).intValue();
  253. }
  254. @Override
  255. public int getAlignment() {
  256. return javax.swing.SwingConstants.RIGHT;
  257. }
  258. }
  259. private static final class FormatsSTRING extends Formats {
  260. @Override
  261. protected String formatValueInt(Object value) {
  262. return (String) value;
  263. }
  264. @Override
  265. protected Object parseValueInt(String value) throws ParseException {
  266. return value;
  267. }
  268. @Override
  269. public int getAlignment() {
  270. return javax.swing.SwingConstants.LEFT;
  271. }
  272. }
  273. private static final class FormatsDOUBLE extends Formats {
  274. @Override
  275. protected String formatValueInt(Object value) {
  276. return m_doubleformat.format(DoubleUtils.fixDecimals((Number) value)); // quickfix for 3838
  277. }
  278. @Override
  279. protected Object parseValueInt(String value) throws ParseException {
  280. return m_doubleformat.parse(value).doubleValue();
  281. }
  282. @Override
  283. public int getAlignment() {
  284. return javax.swing.SwingConstants.RIGHT;
  285. }
  286. }
  287. private static final class FormatsPERCENT extends Formats {
  288. @Override
  289. protected String formatValueInt(Object value) {
  290. return m_percentformat.format(DoubleUtils.fixDecimals((Number) value)); // quickfix for 3838
  291. }
  292. @Override
  293. protected Object parseValueInt(String value) throws ParseException {
  294. try {
  295. return m_percentformat.parse(value).doubleValue();
  296. } catch (ParseException e) {
  297. // Segunda oportunidad como numero normalito
  298. return m_doubleformat.parse(value).doubleValue() / 100;
  299. }
  300. }
  301. @Override
  302. public int getAlignment() {
  303. return javax.swing.SwingConstants.RIGHT;
  304. }
  305. }
  306. private static final class FormatsCURRENCY extends Formats {
  307. @Override
  308. protected String formatValueInt(Object value) {
  309. return m_currencyformat.format(DoubleUtils.fixDecimals((Number) value)); // quickfix for 3838
  310. }
  311. @Override
  312. protected Object parseValueInt(String value) throws ParseException {
  313. try {
  314. return m_currencyformat.parse(value).doubleValue();
  315. } catch (ParseException e) {
  316. // Segunda oportunidad como numero normalito
  317. return m_doubleformat.parse(value).doubleValue();
  318. }
  319. }
  320. @Override
  321. public int getAlignment() {
  322. return javax.swing.SwingConstants.RIGHT;
  323. }
  324. }
  325. private static final class FormatsBOOLEAN extends Formats {
  326. @Override
  327. protected String formatValueInt(Object value) {
  328. return ((Boolean) value).toString();
  329. }
  330. @Override
  331. protected Object parseValueInt(String value) throws ParseException {
  332. return Boolean.valueOf(value);
  333. }
  334. @Override
  335. public int getAlignment() {
  336. return javax.swing.SwingConstants.CENTER;
  337. }
  338. }
  339. private static final class FormatsTIMESTAMP extends Formats {
  340. @Override
  341. protected String formatValueInt(Object value) {
  342. return m_datetimeformat.format((Date) value);
  343. }
  344. @Override
  345. protected Object parseValueInt(String value) throws ParseException {
  346. try {
  347. return m_datetimeformat.parse(value);
  348. } catch (ParseException e) {
  349. // segunda oportunidad como fecha normalita
  350. return m_dateformat.parse(value);
  351. }
  352. }
  353. @Override
  354. public int getAlignment() {
  355. return javax.swing.SwingConstants.CENTER;
  356. }
  357. }
  358. private static final class FormatsDATE extends Formats {
  359. @Override
  360. protected String formatValueInt(Object value) {
  361. return m_dateformat.format((Date) value);
  362. }
  363. @Override
  364. protected Object parseValueInt(String value) throws ParseException {
  365. return m_dateformat.parse(value);
  366. }
  367. @Override
  368. public int getAlignment() {
  369. return javax.swing.SwingConstants.CENTER;
  370. }
  371. }
  372. private static final class FormatsTIME extends Formats {
  373. @Override
  374. protected String formatValueInt(Object value) {
  375. return m_timeformat.format((Date) value);
  376. }
  377. @Override
  378. protected Object parseValueInt(String value) throws ParseException {
  379. return m_timeformat.parse(value);
  380. }
  381. @Override
  382. public int getAlignment() {
  383. return javax.swing.SwingConstants.CENTER;
  384. }
  385. }
  386. private static final class FormatsBYTEA extends Formats {
  387. @Override
  388. protected String formatValueInt(Object value) {
  389. try {
  390. return new String((byte[]) value, "UTF-8");
  391. } catch (java.io.UnsupportedEncodingException eu) {
  392. return "";
  393. }
  394. }
  395. @Override
  396. protected Object parseValueInt(String value) throws ParseException {
  397. try {
  398. return value.getBytes("UTF-8");
  399. } catch (java.io.UnsupportedEncodingException eu) {
  400. return new byte[0];
  401. }
  402. }
  403. @Override
  404. public int getAlignment() {
  405. return javax.swing.SwingConstants.LEADING;
  406. }
  407. }
  408. private static final class FormatsHOURMIN extends Formats {
  409. @Override
  410. protected String formatValueInt(Object value) {
  411. return m_hourminformat.format(value);
  412. }
  413. @Override
  414. protected Date parseValueInt(String value) throws ParseException {
  415. return m_hourminformat.parse(value);
  416. }
  417. @Override
  418. public int getAlignment() {
  419. return javax.swing.SwingConstants.CENTER;
  420. }
  421. }
  422. /** Added; Thanks TSirwani 3 Mar 11 */
  423. private static final class FormatsSIMPLEDATE extends Formats {
  424. @Override
  425. protected String formatValueInt(Object value) {
  426. return m_simpledate.format(value);
  427. }
  428. @Override
  429. protected Date parseValueInt(String value) throws ParseException {
  430. return m_simpledate.parse(value);
  431. }
  432. @Override
  433. public int getAlignment() {
  434. return javax.swing.SwingConstants.CENTER;
  435. }
  436. }
  437. }