javacommons/utils/javasrc/com/nokia/mj/impl/utils/FormatterQt.java
changeset 80 d6dafc5d983f
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.impl.utils;
       
    20 
       
    21 import java.util.Calendar;
       
    22 import java.util.Date;
       
    23 import java.util.Vector;
       
    24 
       
    25 /**
       
    26  * Formatter for Qt based platform.
       
    27  * @see Formatter
       
    28  */
       
    29 class FormatterQt extends Formatter
       
    30 {
       
    31     /** ResourceLoader to be used with this Formatter. */
       
    32     private ResourceLoaderQt iResourceLoader = null;
       
    33 
       
    34     /** Original text id. */
       
    35     private String iTextId = null;
       
    36 
       
    37     /** Number indicating plurality set with argn() method. */
       
    38     private int iArgN = -1;
       
    39 
       
    40     /** Text parameters set with arg() methods. */
       
    41     private Vector iArgs = new Vector();
       
    42 
       
    43     /*** ----------------------------- PUBLIC ------------------------------ */
       
    44 
       
    45     /**
       
    46      * Set the plurality for this Formatter.
       
    47      *
       
    48      * @param n number indicating plurality
       
    49      * @return same formatter
       
    50      */
       
    51     public Formatter argn(int n)
       
    52     {
       
    53         iArgN = n;
       
    54         return this;
       
    55     }
       
    56 
       
    57     /**
       
    58      * Replace the lowest numbered parameter in the string, which is not yet
       
    59      * replaced.
       
    60      *
       
    61      * @param string string to replace at the argument
       
    62      * @return same formatter
       
    63      */
       
    64     public Formatter arg(String string)
       
    65     {
       
    66         iArgs.addElement(string);
       
    67         return this;
       
    68     }
       
    69 
       
    70     /**
       
    71      * Replace the least numbered parameter in the string, which is not yet
       
    72      * replaced.
       
    73      *
       
    74      * @param number number to replace at the argument
       
    75      * @return same formatter
       
    76      */
       
    77     public Formatter arg(int number)
       
    78     {
       
    79         iArgs.addElement(new Integer(number));
       
    80         return this;
       
    81     }
       
    82 
       
    83     /**
       
    84      * Replace the least numbered parameter in the string, which is not yet
       
    85      * replaced.
       
    86      *
       
    87      * @param ch character to replace at the argument
       
    88      * @return same formatter
       
    89      */
       
    90     public Formatter arg(char ch)
       
    91     {
       
    92         iArgs.addElement(new Character(ch));
       
    93         return this;
       
    94     }
       
    95 
       
    96     /**
       
    97      * Replace the least numbered parameter in the string, which is not yet
       
    98      * replaced. Date is formatted according to current device date format.
       
    99      *
       
   100      * @param date date to replace at the argument
       
   101      * @return same formatter
       
   102      */
       
   103     public Formatter arg(Date date)
       
   104     {
       
   105         Calendar cal = Calendar.getInstance();
       
   106         cal.setTime(date);
       
   107         iArgs.addElement(cal);
       
   108         return this;
       
   109     }
       
   110 
       
   111     /**
       
   112      * Convert the current pattern to string, along with parameter
       
   113      * replacements.
       
   114      *
       
   115      * @return string where parameters are replaced
       
   116      */
       
   117     public String toString()
       
   118     {
       
   119         // Fetch localized text from iResourceLoader.
       
   120         String result = iResourceLoader.string(iTextId, iArgN);
       
   121         // Replace text parameters.
       
   122         Object[] args = new Object[iArgs.size()];
       
   123         for (int i = 0; i < iArgs.size(); i++)
       
   124         {
       
   125             args[i] = iArgs.elementAt(i);
       
   126         }
       
   127         result = _formatParameters(result, args);
       
   128         // Reset for next usage.
       
   129         iArgN = -1;
       
   130         iArgs.removeAllElements();
       
   131         return result;
       
   132     }
       
   133 
       
   134     /**
       
   135      * Applies conversion from european digits into arabic-indic digits
       
   136      * based on existing language settings
       
   137      *
       
   138      * @param str String which might contain european digits
       
   139      * @return A string identical with the provided string but with the
       
   140      *         european digits (if any) converted to arabic-indic digits
       
   141      */
       
   142     public static String formatDigits(String str)
       
   143     {
       
   144         return str;
       
   145     }
       
   146 
       
   147     /*** ----------------------------- PROTECTED -------------------------- */
       
   148 
       
   149     /**
       
   150      * Default constructor.
       
   151      */
       
   152     protected FormatterQt()
       
   153     {
       
   154         this(null, "");
       
   155     }
       
   156 
       
   157     /**
       
   158      * Create a new formatter
       
   159      *
       
   160      * @param aResourceLoader ResourceLoader to be used with this Formatter
       
   161      * @param aTextId text id
       
   162      */
       
   163     protected FormatterQt(ResourceLoaderQt aResourceLoader, String aTextId)
       
   164     {
       
   165         iResourceLoader = aResourceLoader;
       
   166         iTextId = aTextId;
       
   167     }
       
   168 
       
   169     /*** ----------------------------- PRIVATE ---------------------------- */
       
   170 
       
   171     /*** ----------------------------- NATIVE ----------------------------- */
       
   172 
       
   173     /**
       
   174      * Formats given parameters to given text.
       
   175      *
       
   176      * @param aText text to be formatted
       
   177      * @param aArgs text parameters
       
   178      * @return formatted text
       
   179      */
       
   180     private static native String _formatParameters(String aText, Object[] aArgs);
       
   181 
       
   182     /**
       
   183      * Applies conversion from european digits into arabic-indic digits
       
   184      * based on existing language settings
       
   185      *
       
   186      * @param str String which might contain european digits
       
   187      * @return A string identical with the provided string but with the
       
   188      *         european digits (if any) converted to arabic-indic digits
       
   189      */
       
   190     private static native String _formatDigits(String str);
       
   191 }