javacommons/utils/javasrc/com/nokia/mj/impl/utils/ResourceLoader.java
changeset 79 2f468c1958d0
parent 76 4ad59aaee882
child 80 d6dafc5d983f
equal deleted inserted replaced
76:4ad59aaee882 79:2f468c1958d0
    28  * <pre>
    28  * <pre>
    29  *   ResourceLoader res = new ResourceLoader("javainstaller", "qtn_java_installer_");
    29  *   ResourceLoader res = new ResourceLoader("javainstaller", "qtn_java_installer_");
    30  *   Label subjectLabel = createLabel(
    30  *   Label subjectLabel = createLabel(
    31  *       res.format("subject").arg(certificate.getSubject()).toString(),
    31  *       res.format("subject").arg(certificate.getSubject()).toString(),
    32  *       horizontalSpan, labelStyle);
    32  *       horizontalSpan, labelStyle);
    33  *
       
    34  *   Label noteLabel = createLabel(
       
    35  *       res.string("note"), horizontalSpan, labelStyle);
       
    36  * </pre>
    33  * </pre>
    37  */
    34  */
    38 public class ResourceLoader
    35 public class ResourceLoader
    39 {
    36 {
    40     /** AVKON UI identifier. */
    37     /** AVKON UI identifier. */
   142      * Creates resource loader, using the current locale of the environment.
   139      * Creates resource loader, using the current locale of the environment.
   143      *
   140      *
   144      * @param resourceName name of the resource
   141      * @param resourceName name of the resource
   145      * @param aPrefix prefix added before each id when retrieving
   142      * @param aPrefix prefix added before each id when retrieving
   146      */
   143      */
   147     public ResourceLoader(String resourceName, String aPrefix)
   144     ResourceLoader(String resourceName, String aPrefix)
   148     {
   145     {
   149         locType = AVKON;
   146         locType = AVKON;
   150         prefix = aPrefix;
   147         prefix = aPrefix;
   151         loadFile(resourceName, true);  // Avkon
   148         loadFile(resourceName, true);  // Avkon
   152     }
   149     }
   164     }
   161     }
   165 
   162 
   166     /**
   163     /**
   167      * Get a string formatter of a given resource id.
   164      * Get a string formatter of a given resource id.
   168      *
   165      *
   169      * @param avkonId Avkon resource id.
       
   170      * @param qtId Qt resource id.
       
   171      * @return formatter instance
       
   172      * @see Formatter
       
   173      */
       
   174     public Formatter format(String avkonId, String qtId)
       
   175     {
       
   176         if (locType == AVKON)
       
   177         {
       
   178             return new Formatter(string(avkonId), locType);
       
   179         }
       
   180         else
       
   181         {
       
   182             return new Formatter(string(qtId), locType);
       
   183         }
       
   184     }
       
   185 
       
   186     /**
       
   187      * Formats localised text with specified parameters from an array.
       
   188      *
       
   189      * @param avkonId Avkon resource id.
       
   190      * @param qtId Qt resource id.
       
   191      * @param textParameters parameters to be filled into the text.
       
   192      * @return localised text formatted with the provided parameters.
       
   193      * @see Formatter
       
   194      */
       
   195     public String format(String avkonId, String qtId, Object[] textParameters)
       
   196     {
       
   197         if (locType == AVKON)
       
   198         {
       
   199             return new Formatter(string(avkonId), locType).format(textParameters);
       
   200         }
       
   201         else
       
   202         {
       
   203             return new Formatter(string(qtId), locType).format(textParameters);
       
   204         }
       
   205     }
       
   206 
       
   207     /**
       
   208      * Get a string formatter of a given resource id.
       
   209      *
       
   210      * @param id resource id
   166      * @param id resource id
   211      * @return formatter instance
   167      * @return formatter instance
   212      * @see Formatter
   168      * @see Formatter
   213      */
   169      */
   214     public Formatter format(Id id)
   170     public Formatter format(Id id)
   215     {
   171     {
   216         return new Formatter(id.getString(locType), locType);
   172         return new Formatter(string(id.getString(locType)), locType);
   217     }
   173     }
   218 
   174 
   219     /**
   175     /**
   220      * Formats localised text with specified parameters from an array.
   176      * Formats localised text with specified parameters from an array.
   221      *
   177      *
   242         return new Formatter(string(id.getString(locType)), locType).format(textParameters);
   198         return new Formatter(string(id.getString(locType)), locType).format(textParameters);
   243     }
   199     }
   244 
   200 
   245 
   201 
   246     /**
   202     /**
       
   203      * Gets the locale ID currently being used on the phone. This can be used
       
   204      * e.g. to load a localized icon file, by adding the locale id as suffix.
       
   205      *
       
   206      * @return Locale ID as provided by the platform
       
   207      */
       
   208     public String getLocaleId()
       
   209     {
       
   210         int localeId = _getLocaleId();
       
   211         if (localeId > 0)
       
   212         {
       
   213             if (localeId < 10)
       
   214             {
       
   215                 // Ensure that the returned locale ID has at least two digits.
       
   216                 return "0" + Integer.toString(localeId);
       
   217             }
       
   218             else
       
   219             {
       
   220                 return Integer.toString(localeId);
       
   221             }
       
   222         }
       
   223         return "sc";
       
   224     }
       
   225 
       
   226     /**
       
   227      * Return locale id string on Qt platform.
       
   228      *
       
   229      * @return Qt Locale Id String, null if not in Qt.
       
   230      */
       
   231     public static String getLocaleIdQt()
       
   232     {
       
   233         return _getLocaleIdQt();
       
   234     }
       
   235 
       
   236 
       
   237     /*** ----------------------------- PRIVATE ---------------------------- */
       
   238 
       
   239     /**
   247      * Get a plain string resource with a given resource id.
   240      * Get a plain string resource with a given resource id.
   248      *
   241      *
   249      * @param id resource id, either with prefix or without
   242      * @param id resource id, either with prefix or without
   250      * @return resource string, or the id if does not exist
   243      * @return resource string, or the id if does not exist
   251      */
   244      */
   252     public String string(String id)
   245     private String string(String id)
   253     {
   246     {
   254         String str = (String)resourceMap.get(id);
   247         String str = (String)resourceMap.get(id);
   255         if (str == null)
   248         if (str == null)
   256         {
   249         {
   257             // Try with prefix
   250             // Try with prefix
   280 
   273 
   281         return str;
   274         return str;
   282     }
   275     }
   283 
   276 
   284     /**
   277     /**
   285      * Gets the locale ID currently being used on the phone. This can be used
       
   286      * e.g. to load a localized icon file, by adding the locale id as suffix.
       
   287      *
       
   288      * @return Locale ID as provided by the platform
       
   289      */
       
   290     public String getLocaleId()
       
   291     {
       
   292         int localeId = _getLocaleId();
       
   293         if (localeId > 0)
       
   294         {
       
   295             if (localeId < 10)
       
   296             {
       
   297                 // Ensure that the returned locale ID has at least two digits.
       
   298                 return "0" + Integer.toString(localeId);
       
   299             }
       
   300             else
       
   301             {
       
   302                 return Integer.toString(localeId);
       
   303             }
       
   304         }
       
   305         return "sc";
       
   306     }
       
   307 
       
   308     /**
       
   309      * Return locale id string on Qt platform.
       
   310      *
       
   311      * @return Qt Locale Id String, null if not in Qt.
       
   312      */
       
   313     public static String getLocaleIdQt()
       
   314     {
       
   315         return _getLocaleIdQt();
       
   316     }
       
   317 
       
   318 
       
   319     /*** ----------------------------- PRIVATE ---------------------------- */
       
   320 
       
   321     /**
       
   322      * Loads the resources from .loc type file.
   278      * Loads the resources from .loc type file.
   323      *
   279      *
   324      * @param resourceName name of the resource file.
   280      * @param resourceName name of the resource file.
   325      * @param aIs InputStream pointing to resource. It will be closed after use.
   281      * @param aIs InputStream pointing to resource. It will be closed after use.
   326      * @param true if operation succeed.
   282      * @param true if operation succeed.