KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > expert > ExpertTag


1 //Copyright 2005 Improve SA
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package fr.improve.struts.taglib.layout.expert;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21
22 import org.apache.struts.taglib.html.BaseHandlerTag;
23
24 import fr.improve.struts.taglib.layout.field.TextareaFieldTag;
25 import fr.improve.struts.taglib.layout.util.LayoutUtils;
26 import fr.improve.struts.taglib.layout.util.TagUtils;
27 /**
28  * Expert tag, allowing to set the value of specified field with a meta langage.
29  *
30  * @author: Romain Maton
31  */

32 public class ExpertTag extends TextareaFieldTag {
33     
34     public static final String JavaDoc LOADED = "fr.improve.struts.taglib.layout.expert.ExpertTag.LOADED";
35     
36     public static final int DEFAULT_SUGGESTCOUNT = 10;
37     public static final int DEFAULT_MINWORDLENGTH = 1;
38     
39     protected List JavaDoc id = null;
40     protected int minWordLength;
41     protected int suggestCount;
42     
43     /**
44      * Get all the id that expertTag have to manage
45      * @param layoutId
46      */

47     public void doReferencer(String JavaDoc layoutId) {
48         (this.id).add(layoutId);
49     }
50     
51     /**
52      * Write the before value
53      */

54     protected boolean doBeforeValue() throws JspException JavaDoc {
55         super.doBeforeValue();
56         this.id = new ArrayList JavaDoc();
57         this.suggestCount = DEFAULT_SUGGESTCOUNT;
58         this.minWordLength = DEFAULT_MINWORDLENGTH;
59         loadScript();
60         return true;
61     }
62     
63     /**
64      * Load the required Javascript code.
65      */

66     protected void loadScript() throws JspException JavaDoc {
67         if (pageContext.getRequest().getAttribute(LOADED)==null) {
68             TagUtils.write(pageContext, "<script SRC=\"");
69             TagUtils.write(pageContext, LayoutUtils.getSkin(pageContext.getSession()).getConfigDirectory(pageContext.getRequest()));
70             TagUtils.write(pageContext, "/expert.js\"></script>");
71             pageContext.getRequest().setAttribute(LOADED, "");
72         }
73     }
74
75     /**
76      * End of the tag with init and suggest input hidden
77      */

78     public int doEndLayoutTag() throws JspException JavaDoc {
79         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
80         buffer.append("<script language=\"JavaScript\">var arrayId = new Array(");
81         for(int i=0; i<(this.id).size(); i++) {
82             if(i==0)
83                 buffer.append("'"+id.get(i)+"'");
84             else
85                 buffer.append(",'"+id.get(i)+"'");
86         }
87         buffer.append(");StrutsLayoutExpert.init(arrayId);</script>");
88         buffer.append("<br/><div id=\"expertSuggestSuggestionList\" class=\"suggestionList\"></div>");
89         buffer.append("<input type=\"hidden\" id=\"expertSuggestSuggestionList_selectedFieldText" + "\" value=\"0\">");
90         buffer.append("<input type=\"hidden\" id=\"expertSuggestSuggestionList_selectedSuggestionIndex" + "\" value=\"-1\">");
91         buffer.append("<input type=\"hidden\" id=\"expertSuggestSuggestionList_typedWord" + "\" value=\"\">");
92         TagUtils.write(pageContext, buffer.toString());
93         return super.doEndLayoutTag();
94     }
95     
96     /**
97      * Copy properties
98      */

99     protected void copyProperties(BaseHandlerTag in_dest) throws JspException JavaDoc {
100         super.copyProperties(in_dest);
101         if(getAccesskey()==null || getAccesskey().length()==0) {
102             fieldTag.setAccesskey("E");
103         }
104         fieldTag.setOnkeydown((onkeydown != null ? onkeydown : "") +
105                 ";StrutsLayoutExpert.computeKeyDown(this, StrutsLayoutExpert.getKey(event.keyCode, event.which));");
106         fieldTag.setOnkeyup((onkeyup != null ? onkeyup : "") +
107                 ";StrutsLayoutExpert.expertUpdate(this.value,false);" +
108                 "StrutsLayoutExpert.computeKeyUp(this, StrutsLayoutExpert.getKey(event.keyCode, event.which), '" +
109                 this.suggestCount + "', '" + this.minWordLength + "');");
110         fieldTag.setOnkeypress((onkeypress != null ? onkeypress : "") +
111                 ";return StrutsLayoutExpert.computeKeyPress(this, StrutsLayoutExpert.getKey(event.keyCode, event.which));");
112     }
113 }
114
Popular Tags