usr/src/java/vpanels/app/usermgr/com/oracle/solaris/vp/panels/usermgr/client/swing/ActionString.java
changeset 847 a8e124b894b8
equal deleted inserted replaced
846:0a2af4721353 847:a8e124b894b8
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panels.usermgr.client.swing;
       
    27 
       
    28 import java.util.*;
       
    29 
       
    30 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    31 
       
    32 /**
       
    33  * SMC code adapted for Visual Panels
       
    34  *
       
    35  * ActionString interposes between the application code and it's resource
       
    36  * bundle, but parses the returned string for the mnemonic indicator and
       
    37  * removes it.  The mnemonic character is '&', as used in Windows.  This
       
    38  * class make it easy to localize the text and associated mnemonics in a
       
    39  * resource properties file - mnemonics can be embedded within the string,
       
    40  * obviating the need for seperate resource strings for the mnemonics.
       
    41  *
       
    42  * Note: many Swing components can now take in HTML encoded text in addition
       
    43  * to plain text. To encode a mnemonic in HTML the '&' has to be encoded
       
    44  * in the HTML escaped form '&'. Otherwise the HTML processing within
       
    45  * Swing will remove the a single '&'.
       
    46  *
       
    47  */
       
    48 public class ActionString {
       
    49 
       
    50     private String  string;
       
    51     private int mnemonic = 0;
       
    52 
       
    53     /**
       
    54      * Returns the localized message associated with the specified message key
       
    55      * from the default resource bundle.  Strip out the mnemonic indicator.
       
    56      * Users of this class can then use getString() and getMnemonic().
       
    57      *
       
    58      * @param	key	message lookup key
       
    59      */
       
    60     public ActionString(String key) {
       
    61 
       
    62 	string = Finder.getString(key);
       
    63 	split();
       
    64 
       
    65     } // constructor
       
    66 
       
    67     /*
       
    68      * Returns the text string without the mnemonic indicator
       
    69      *
       
    70      * @return the text string without the mnemonic character
       
    71      */
       
    72     public String getString() {
       
    73 
       
    74 	return string;
       
    75 
       
    76     } // getString
       
    77 
       
    78 
       
    79     /*
       
    80      * Returns the mnemonic character
       
    81      *
       
    82      * @return the mnemonic character
       
    83      */
       
    84     public int getMnemonic() {
       
    85 
       
    86 	return mnemonic;
       
    87 
       
    88     } // getMnemonic
       
    89 
       
    90 
       
    91     /**
       
    92      * Split the retrieved message into its string and mnemonic parts.
       
    93      */
       
    94     private void split() {
       
    95 
       
    96 	/*
       
    97 	 * Simple check to see if the text is HTML or plain text. If the text
       
    98 	 * is HTML the mnemonic indicator will be encoded as '&' and not
       
    99 	 * the plain text '&'. Therefore extraction of the mnemonic itself
       
   100 	 * is a little more interesting. Also the underline tags need to
       
   101 	 * be inserted to ensure that the mnemonic character is correctly
       
   102 	 * displayed underlined when the text is HTML.
       
   103 	 */
       
   104 	boolean isHtml =
       
   105 		(string.startsWith("<html>") && string.endsWith("</html>"));
       
   106 
       
   107 	String ampString = null;
       
   108 
       
   109 	if (isHtml) {
       
   110 	    ampString = "&amp;";
       
   111 	} else {
       
   112 	    ampString = "&";
       
   113 	}
       
   114 
       
   115 	int ampStringLen = ampString.length();
       
   116 	int i = string.indexOf(ampString);
       
   117 
       
   118 	if (i > -1) {
       
   119 	    String sUpper = string.toUpperCase();
       
   120 	    mnemonic = sUpper.charAt(i + ampStringLen);
       
   121 
       
   122 	    StringBuffer s = new StringBuffer();
       
   123 	    s.append(string.substring(0, i));
       
   124 
       
   125 	    if (isHtml) {
       
   126 		s.append("<u>");
       
   127 	    }
       
   128 
       
   129 	    s.append(string.substring(i + ampStringLen, i+ ampStringLen + 1));
       
   130 
       
   131 	    if (isHtml) {
       
   132 		s.append("</u>");
       
   133 	    }
       
   134 
       
   135 	    s.append(string.substring(i + ampStringLen + 1, string.length()));
       
   136 
       
   137 	    string = s.toString();
       
   138 	}
       
   139 
       
   140     } // split
       
   141 
       
   142 } // ActionString