Changeset 6252


Ignore:
Timestamp:
Mar 14, 2013, 12:46:14 PM (9 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1747: Change the meaning of 'Decimals' setting to 'Precision'

Added AdaptiveNumberFormatter implementation that dynamically calculates the number of decimals to use. The FormatterFactory implementation now uses the new formatter instead of the old one.

Also changed the formatting original and remaining quantity display on a several places to use a Formatter implementation (eg. a better fix for #1746) so now the setting should automatically be applied everywhere numbers are displayed. The only exception is file sizes which now always use 2 decimals and dynamic switching between kb, Mb and Gb (this was not consistent before).

Location:
branches/3.2-stable
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2-stable/doc/src/docbook/user/webclient.xml

    r5971 r6252  
    852852              <listitem>
    853853                <para>
    854                 The number of decimals to display for numerical floating point values.
    855                 The default is 2.
     854                The base number of decimals to display for numerical floating point values.
     855                The default is 2. This setting is used for values between 1 and 10. For higher
     856                or lower values, the number of decimals is adapted in order to not loose
     857                information (eg. 0.0059 instead of 0.01) or give the impression of very
     858                high precision (eg. 135000 instead of 135000.00).
    856859                </para>
    857860              </listitem>
  • branches/3.2-stable/src/clients/web/net/sf/basedb/clients/web/formatter/FormatterFactory.java

    r5440 r6252  
    3333import net.sf.basedb.core.Unit;
    3434import net.sf.basedb.util.ColorGenerator;
     35import net.sf.basedb.util.formatter.AdaptiveNumberFormatter;
    3536import net.sf.basedb.util.formatter.BooleanFormatter;
    3637import net.sf.basedb.util.formatter.DateFormatter;
     
    7172    if (formatter == null)
    7273    {
    73       formatter = new NumberFormatter(FormatterSettings.getNumDecimals(sc), false);
     74      formatter = new AdaptiveNumberFormatter(FormatterSettings.getNumDecimals(sc), false);
    7475      sc.setSessionSetting("formatter.float", formatter);
    7576    }
     
    9495    if (formatter == null)
    9596    {
    96       formatter = new NumberFormatter(FormatterSettings.getNumDecimals(sc), true);
     97      formatter = new AdaptiveNumberFormatter(FormatterSettings.getNumDecimals(sc), true);
    9798      sc.setSessionSetting("formatter.double", formatter);
    9899    }
  • branches/3.2-stable/src/core/net/sf/basedb/util/formatter/PrefixSuffixFormatter.java

    r4544 r6252  
    5151  private final Formatter<T> parent;
    5252  private final String suffix;
     53  private final boolean usePrefixForNull;
     54  private final boolean useSuffixForNull;
    5355 
    5456  /**
    55     Create a new formatter.
     57    Create a new formatter. Prefix and suffix are included for null values.
    5658    @param prefix The prefix, or null to not use any prefix
    5759    @param parent The parent formatter, if null a {@link ToStringFormatter}
     
    6163  public PrefixSuffixFormatter(String prefix, Formatter<T> parent, String suffix)
    6264  {
     65    this(prefix, true, parent, suffix, true);
     66  }
     67 
     68  /**
     69    Create a new formatter.
     70    @param prefix The prefix, or null to not use any prefix
     71    @param usePrefixForNull If set, the prefix is always included, otherwise only for non-null
     72      values
     73    @param parent The parent formatter, if null a {@link ToStringFormatter}
     74      is automatically created
     75    @param suffix The suffix, or null to not use any suffix
     76    @param useSuffixForNull If set, the suffix is always included, otherwise only for non-null
     77      values
     78  */
     79  public PrefixSuffixFormatter(String prefix, boolean usePrefixForNull, Formatter<T> parent, String suffix, boolean useSuffixForNull)
     80  {
    6381    this.prefix = prefix == null ? "" : prefix;
     82    this.usePrefixForNull = usePrefixForNull;
    6483    this.parent = parent == null ? new ToStringFormatter<T>() : parent;
    6584    this.suffix = suffix == null ? "" : suffix;
     85    this.useSuffixForNull = useSuffixForNull;
    6686  }
     87
    6788 
    6889  /*
     
    7293  public String format(T value)
    7394  {
    74     return prefix + parent.format(value) + suffix;
     95    StringBuilder sb = new StringBuilder();
     96    if (value != null || usePrefixForNull) sb.append(prefix);
     97    sb.append(parent.format(value));
     98    if (value != null || useSuffixForNull) sb.append(suffix);
     99    return sb.toString();
    75100  }
    76101  public T parseString(String value)
  • branches/3.2-stable/www/biomaterials/events/view_event.jsp

    r6251 r6252  
    5656  import="net.sf.basedb.util.Values"
    5757  import="net.sf.basedb.util.formatter.Formatter"
     58  import="net.sf.basedb.util.formatter.NumberFormatter"
     59  import="net.sf.basedb.util.formatter.PrefixSuffixFormatter"
    5860  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
    5961  import="net.sf.basedb.clients.web.formatter.FormatterSettings"
     
    8890{
    8991  Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
    90   int numDecimals = FormatterSettings.getNumDecimals(sc);
     92  Formatter<Number> numberFormatter = FormatterFactory.getNumberFormatter(sc);
     93  Formatter<Number> quantityFormatter = new PrefixSuffixFormatter(null, false, numberFormatter, " µg", false);
     94
    9195  Map<Plugin.MainType, Integer> pluginCount = PluginDefinition.countPlugins(dc, guiContext);
    9296
     
    255259      <tr>
    256260        <th>Used quantity</th>
    257         <td><%=Values.formatNumber(event.getUsedQuantity(bioMaterial), numDecimals, " µg")%></td>
     261        <td><%=quantityFormatter.format(event.getUsedQuantity(bioMaterial))%></td>
    258262      </tr>
    259263      <tr>
     
    304308            <%=Base.getLinkedName(ID, source, source == null, true)%>
    305309            <%=sourceType != null ? "(" + sourceType + ")" : "" %>
    306             <%=Values.formatNumber(evtSrc.getUsedQuantity(), numDecimals, " µg")%>
     310            <%=quantityFormatter.format(evtSrc.getUsedQuantity())%>
    307311            <br>
    308312            <%
  • branches/3.2-stable/www/biomaterials/extracts/view_extract.jsp

    r6251 r6252  
    6767  import="net.sf.basedb.util.formatter.Formatter"
    6868  import="net.sf.basedb.util.formatter.WellCoordinateFormatter"
     69  import="net.sf.basedb.util.formatter.PrefixSuffixFormatter"
    6970  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
    7071  import="net.sf.basedb.clients.web.formatter.FormatterSettings"
     
    101102{
    102103  Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
    103   int numDecimals = FormatterSettings.getNumDecimals(sc);
     104  Formatter<Number> numberFormatter = FormatterFactory.getNumberFormatter(sc);
     105  Formatter<Number> quantityFormatter = new PrefixSuffixFormatter(null, false, numberFormatter, " µg", false);
     106
    104107  Map<Plugin.MainType, Integer> pluginCount = PluginDefinition.countPlugins(dc, guiContext);
    105108 
     
    348351        <tr>
    349352          <th>Original quantity</th>
    350           <td><%=Values.formatNumber(extract.getOriginalQuantity(), numDecimals, " µg")%></td>
     353          <td><%=quantityFormatter.format(extract.getOriginalQuantity())%></td>
    351354        </tr>
    352355        <tr>
    353356          <th class="subprompt">- remaining</th>
    354           <td><%=Values.formatNumber(extract.getRemainingQuantity(), numDecimals, " µg")%></td>
     357          <td><%=quantityFormatter.format(extract.getRemainingQuantity())%></td>
    355358        </tr>
    356359        <tr>
     
    475478                  %>
    476479                </tbl:cell>
    477                 <tbl:cell column="quantity"><%=Values.formatNumber(item.getUsedQuantity(), numDecimals)%></tbl:cell>
     480                <tbl:cell column="quantity"><%=numberFormatter.format(item.getUsedQuantity())%></tbl:cell>
    478481                <tbl:cell column="description"><%=HTML.encodeTags(bm == null ? "" : bm.getDescription())%></tbl:cell>
    479482              </tbl:row>
     
    588591                    %>
    589592                  </tbl:cell>
    590                   <tbl:cell column="quantity"><%=Values.formatNumber(item.getUsedQuantity(), numDecimals)%></tbl:cell>
     593                  <tbl:cell column="quantity"><%=numberFormatter.format(item.getUsedQuantity())%></tbl:cell>
    591594                  <tbl:cell column="description"><%=HTML.encodeTags(child == null ? "" : child.getDescription())%></tbl:cell>
    592595                </tbl:row>
  • branches/3.2-stable/www/biomaterials/samples/view_sample.jsp

    r6251 r6252  
    6363  import="net.sf.basedb.util.formatter.Formatter"
    6464  import="net.sf.basedb.util.formatter.WellCoordinateFormatter"
     65  import="net.sf.basedb.util.formatter.PrefixSuffixFormatter"
    6566  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
    6667  import="net.sf.basedb.clients.web.formatter.FormatterSettings"
     
    9798{
    9899  Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
    99   int numDecimals = FormatterSettings.getNumDecimals(sc);
     100  Formatter<Number> numberFormatter = FormatterFactory.getNumberFormatter(sc);
     101  Formatter<Number> quantityFormatter = new PrefixSuffixFormatter(null, false, numberFormatter, " µg", false);
     102
    100103  Map<Plugin.MainType, Integer> pluginCount = PluginDefinition.countPlugins(dc, guiContext);
    101104
     
    333336      <tr>
    334337        <th>Original quantity</th>
    335         <td><%=Values.formatNumber(sample.getOriginalQuantity(), numDecimals, " µg")%></td>
     338        <td><%=quantityFormatter.format(sample.getOriginalQuantity())%></td>
    336339      </tr>
    337340      <tr>
    338341        <th class="subprompt">- remaining</th>
    339         <td><%=Values.formatNumber(sample.getRemainingQuantity(), numDecimals, " µg")%></td>
     342        <td><%=quantityFormatter.format(sample.getRemainingQuantity())%></td>
    340343      </tr>
    341344      <tr>
     
    460463                  %>
    461464                </tbl:cell>
    462                 <tbl:cell column="quantity"><%=Values.formatNumber(item.getUsedQuantity(), numDecimals)%></tbl:cell>
     465                <tbl:cell column="quantity"><%=numberFormatter.format(item.getUsedQuantity())%></tbl:cell>
    463466                <tbl:cell column="description"><%=HTML.encodeTags(bm == null ? "" : bm.getDescription())%></tbl:cell>
    464467              </tbl:row>
     
    561564                    %>
    562565                  </tbl:cell>
    563                   <tbl:cell column="quantity"><%=Values.formatNumber(item.getUsedQuantity(), numDecimals)%></tbl:cell>
     566                  <tbl:cell column="quantity"><%=numberFormatter.format(item.getUsedQuantity())%></tbl:cell>
    564567                  <tbl:cell column="description"><%=HTML.encodeTags(bm == null ? "" : bm.getDescription())%></tbl:cell>
    565568                </tbl:row>
  • branches/3.2-stable/www/filemanager/files/list_files.jsp

    r6158 r6252  
    236236  int numListed = 0;
    237237  Formatter<Date> dateTimeFormatter = FormatterFactory.getDateTimeFormatter(sc);
    238   int numDecimals = FormatterSettings.getNumDecimals(sc);
    239238  JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, guiContext, current);
    240239  ExtensionsInvoker invoker = ToolbarUtil.useExtensions(jspContext);
     
    10151014                icon = "file_compressed.png";
    10161015              }
    1017               String fileSize = Values.formatBytes(item.getSize(), numDecimals);
     1016              String fileSize = Values.formatBytes(item.getSize(), 2);
    10181017              index++;
    10191018              numListed++;
     
    10971096                  /></tbl:cell>
    10981097                <tbl:cell column="size"><span title="<%=item.getSize()%> bytes"><%=fileSize%></span></tbl:cell>
    1099                 <tbl:cell column="compressedSize"><span title="<%=item.getCompressedSize()%> bytes"><%=Values.formatBytes(item.getCompressedSize(), numDecimals)%></span></tbl:cell>
     1098                <tbl:cell column="compressedSize"><span title="<%=item.getCompressedSize()%> bytes"><%=Values.formatBytes(item.getCompressedSize(), 2)%></span></tbl:cell>
    11001099                <tbl:cell column="compressed"><%=item.isCompressed() ? "yes" : "no"%></tbl:cell>
    11011100                <tbl:cell column="writeProtected"><%=item.isWriteProtected() ? "yes" : "no"%></tbl:cell>
  • branches/3.2-stable/www/filemanager/files/view_file.jsp

    r5951 r6252  
    435435        <tr>
    436436          <th class="subprompt">- compressed size</th>
    437           <td title="<%=file.getCompressedSize()%> bytes"><%=Values.formatBytes(file.getCompressedSize())%></td>
     437          <td title="<%=file.getCompressedSize()%> bytes"><%=Values.formatBytes(file.getCompressedSize(), 2)%></td>
    438438        </tr>
    439439        <%
  • branches/3.2-stable/www/views/physicalbioassays/view_bioassay.jsp

    r6251 r6252  
    9898{
    9999  Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
    100   int numDecimals = FormatterSettings.getNumDecimals(sc);
     100  Formatter<Number> numberFormatter = FormatterFactory.getNumberFormatter(sc);
    101101  Map<Plugin.MainType, Integer> pluginCount = PluginDefinition.countPlugins(dc, guiContext);
    102102
     
    433433                  </tbl:cell>
    434434                  <tbl:cell column="tag" visible="<%=bm != null && bm.getType() == Item.EXTRACT%>"><base:propertyvalue item="<%=bm%>" property="tag" /></tbl:cell>
    435                   <tbl:cell column="quantity"><%=Values.formatNumber(item.getUsedQuantity(), numDecimals)%></tbl:cell>
     435                  <tbl:cell column="quantity"><%=numberFormatter.format(item.getUsedQuantity())%></tbl:cell>
    436436                  <tbl:cell column="description"><%=HTML.encodeTags(bm == null ? "" : bm.getDescription())%></tbl:cell>
    437437                </tbl:row>
Note: See TracChangeset for help on using the changeset viewer.