WEB开发网
开发学院软件开发Java ICEfaces 和 Google Translate 阅读

ICEfaces 和 Google Translate

 2009-11-05 00:00:00 来源:WEB开发网   
核心提示: 大多数组件都通过 JSF 表达式语言绑定动态绑定到了 backing JavaBeans,如清单 2 所示,ICEfaces 和 Google Translate(5),清单 2. 绑定到 backing JavaBeans 的组件value="#{translate.outputTe

大多数组件都通过 JSF 表达式语言绑定动态绑定到了 backing JavaBeans,如清单 2 所示。


清单 2. 绑定到 backing JavaBeans 的组件
value="#{translate.outputText}" 
value="#{translate.selectedOriginalLanguage}" 
valueChangeListener="#{translate.originalLangChanged}" 
value="#{translate.ORIGINAL_LANGUAGES}" 
value="#{translate.selectedTranslatedLanguage}" 
valueChangeListener="#{translate.translateLangChanged}" 
value="#{translate.TRANSLATION_LANGUAGES}" 
value="#{translate.outputText}" 
effect="#{translate.textEffect}" 
actionListener="#{translate.resetAll}" 
actionListener="#{translate.updateText}" 

步骤 2. 创建 backing JavaBean (GoogleTranslate.java)

com.ibm.translate.GoogleTranslate 类是 main.jspx 页面的 backing bean。该 bean 存储选择的当前状态以及所有的 Google Translate 信息。

清单 3 显示了 GoogleTranslate.java 类的代码。


清单 3. GoogleTranslate.java
public class GoogleTranslate { 
private Hashtable languages = new Hashtable(); 
private Hashtable langLabels = new Hashtable(); 
private Effect textEffect; 
protected SelectItem[] ORIGINAL_LANGUAGES; 
protected SelectItem[] TRANSLATION_LANGUAGES; 
 
public void updateText(ActionEvent event) throws 
Exception{ 
 if(this.getText().length() > 1 ){ 
 this.setOutputText(translate(getText())); 
 } 
} 
 
public void resetAll(ActionEvent event){ 
 this.setText(""); 
 this.setOutputText(""); 
} 
 
public String translate(String input) throws Exception{ 
 String out = ""; 
 if(input != null){ 
 out = Translate.translate(this.getText(), 
           getSelectedOriginalLanguage(), 
           getSelectedTranslatedLanguage()); 
 invokeTextEffect(); 
 } 
 return out; 
} 
 
public SelectItem[] getORIGINAL_LANGUAGES() { 
 if(ORIGINAL_LANGUAGES == null){ 
 this.setLanguages(); 
 this.setLangLabels(); 
 Enumeration en = this.languages.keys(); 
 Enumeration enLbls = this.langLabels.keys(); 
 
 ORIGINAL_LANGUAGES = new 
               SelectItem[languages.size()]; 
 for (int i = 0; i < languages.size(); i++) { 
 ORIGINAL_LANGUAGES[i] = new SelectItem( 
     (String)this.langLabels.get(enLbls.nextElement()),          
     (String) this.languages.get(en.nextElement())); 
 } 
 } 
 return ORIGINAL_LANGUAGES; 
} 
 
public SelectItem[] getTRANSLATION_LANGUAGES() { 
 if(TRANSLATION_LANGUAGES == null){ 
 this.setLanguages(); 
 this.setLangLabels(); 
 Enumeration en = this.languages.keys(); 
 Enumeration enLbls = this.langLabels.keys(); 
 TRANSLATION_LANGUAGES = new 
               SelectItem[languages.size()]; 
 for (int i = 0; i < languages.size(); i++) { 
    TRANSLATION_LANGUAGES[i] = new SelectItem( 
    (String) this.langLabels.get(enLbls.nextElement()),  
    (String) this.languages.get(en.nextElement())); 
 } 
 } 
 return TRANSLATION_LANGUAGES; 
} 
 
public void setLanguages() { 
 this.languages.put(Language.ARABIC, "ARABIC"); 
 this.languages.put(Language.CHINESE_SIMPLIFIED, 
                "CHINESE_SIMPLIFIED"); 
 this.languages.put(Language.CHINESE_TRADITIONAL, 
                "CHINESE_TRADITIONAL"); 
 this.languages.put(Language.DUTCH, "DUTCH"); 
 this.languages.put(Language.ENGLISH, "ENGLISH"); 
 this.languages.put(Language.FRENCH, "FRENCH"); 
 this.languages.put(Language.GERMAN, "GERMAN"); 
 this.languages.put(Language.GREEK, "GREEK"); 
 this.languages.put(Language.ITALIAN, "ITALIAN"); 
 this.languages.put(Language.JAPANESE, "JAPANESE"); 
 this.languages.put(Language.KOREAN, "KOREAN"); 
 this.languages.put(Language.PORTUGESE, "PORTUGESE"); 
 this.languages.put(Language.RUSSIAN, "RUSSIAN"); 
 this.languages.put(Language.SPANISH, "SPANISH"); 
} 
 
public void setLangLabels() { 
 this.langLabels.put(new Integer(0), 
                   Language.PORTUGESE); 
 this.langLabels.put(new Integer(1),Language.KOREAN); 
 this.langLabels.put(new Integer(2), 
                    Language.JAPANESE); 
 this.langLabels.put(new Integer(3), Language.SPANISH); 
 this.langLabels.put(new Integer(4), Language.GERMAN); 
 this.langLabels.put(new Integer(5), Language.FRENCH); 
 this.langLabels.put(new Integer(6), Language.ITALIAN); 
 this.langLabels.put(new Integer(7), 
               Language.CHINESE_SIMPLIFIED); 
 this.langLabels.put(new Integer(8), Language.ARABIC); 
 this.langLabels.put(new Integer(9), Language.RUSSIAN); 
 this.langLabels.put(new Integer(10),Language.GREEK); 
 this.langLabels.put(new Integer(11), 
                     Language.ENGLISH); 
 this.langLabels.put(new Integer(12), Language.DUTCH); 
 this.langLabels.put(new Integer(13), 
               Language.CHINESE_TRADITIONAL); 
 } 
 } 
} 

上一页  1 2 3 4 5 6 7  下一页

Tags:ICEfaces Google Translate

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接