1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.admin.datamodel.ui;
22  
23  import java.text.DecimalFormat;
24  import java.text.NumberFormat;
25  
26  import org.efaps.admin.dbproperty.DBProperties;
27  import org.efaps.db.Context;
28  import org.efaps.util.EFapsException;
29  
30  /**
31   * Class to represent a String for the user interface.
32   *
33   * @author The eFaps Team
34   * @version $Id$
35   *
36   */
37  public class NumberWithUoMUI
38      extends StringWithUoMUI
39  {
40  
41      /**
42       * Needed for serialization.
43       */
44      private static final long serialVersionUID = 1L;
45  
46      /**
47       * {@inheritDoc}
48       */
49      @Override
50      public String validateValue(final UIValue _value)
51      {
52          String ret = null;
53          try {
54              if (_value.getDbValue() != null) {
55                  Long.valueOf(String.valueOf(_value.getDbValue()));
56              }
57          } catch (final NumberFormatException e) {
58              ret = DBProperties.getProperty(NumberWithUoMUI.class.getName() + ".InvalidValue");
59          }
60          return ret;
61      }
62  
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public Object format(final Object _object,
69                           final String _pattern)
70          throws EFapsException
71      {
72          final Object ret;
73          final DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Context.getThreadContext()
74                          .getLocale());
75          formatter.applyPattern(_pattern);
76          if (_object instanceof Object[]) {
77              final String tmp = formatter.format(((Object[]) _object)[0]);
78              ((Object[]) _object)[0] = tmp;
79              ret = _object;
80          } else {
81              ret = formatter.format(_object);
82          }
83          return ret;
84      }
85  }