001    package railo.runtime.tag;
002    
003    import java.io.IOException;
004    import java.util.Iterator;
005    import java.util.Map.Entry;
006    
007    import javax.servlet.jsp.tagext.Tag;
008    
009    import railo.commons.lang.HTMLEntities;
010    import railo.commons.lang.StringUtil;
011    import railo.runtime.exp.ApplicationException;
012    import railo.runtime.exp.PageException;
013    import railo.runtime.ext.tag.TagImpl;
014    import railo.runtime.op.Caster;
015    import railo.runtime.op.Decision;
016    import railo.runtime.type.Array;
017    import railo.runtime.type.Collection.Key;
018    import railo.runtime.type.util.ListUtil;
019    import railo.runtime.type.Struct;
020    import railo.runtime.type.StructImpl;
021    
022    /**
023     * slider applet tag
024     */
025    public final class Slider extends TagImpl {
026        
027        private Struct params=new StructImpl();
028        private InputBean input=new InputBean();
029        private int height=40;
030            private int width=0;
031            private int vspace=0;
032            private int hspace=0;
033        
034        @Override
035        public void release() {
036            super.release();
037    
038            input=new InputBean();
039            params.clear();
040            
041            width=0;
042            height=40;
043            hspace=0;
044            vspace=0;
045        }
046        
047        /**
048         * sets the attribut to tag
049         * @param align
050         * @throws ApplicationException
051         */
052        public void setAlign(String align) throws ApplicationException {
053            align=align.trim().toLowerCase();
054            if(ListUtil.listFind("top,left,bottom,baseline,texttop,absbottom,middle,absmiddle,right",align,",")>-1) {
055                params.setEL("align",align);
056            }
057            else
058                throw new ApplicationException("attribute align ["+align+"] is invalid","valid alignments are [top,left,bottom,baseline,texttop,absbottom,middle,absmiddle,right]");
059        }
060        
061        /**
062         * sets the attribut to tag
063         * @param bgcolor
064         */
065        public void setBgcolor(String bgcolor) {
066            params.setEL("bgcolor",bgcolor);
067        }
068        
069        /**
070         * sets the attribut to tag
071         * @param bold
072         */
073        public void setBold(boolean bold) {
074            params.setEL("bold",Caster.toString(bold));
075        }
076        
077        /**
078         * sets the attribut to tag
079         * @param font
080         */
081        public void setFont(String font) {
082            params.setEL("font",font);
083        }
084        
085        /**
086         * sets the attribut to tag
087         * @param fontsize
088         */
089        public void setFontsize(double fontsize) {
090            params.setEL("fontsize",Caster.toString((int)fontsize));
091        }
092        
093        /**
094         * sets the attribut to tag
095         * @param italic
096         */
097        public void setItalic(boolean italic) {
098            params.setEL("italic",Caster.toString(italic));
099        }
100        
101        /**
102         * sets the attribut to tag
103         * @param label
104         */
105        public void setLabel(String label) {
106            params.setEL("label",label);
107        }
108        
109        /**
110         * sets the attribut to tag
111         * @param lookandfeel
112         * @throws ApplicationException
113         */
114        public void setLookandfeel(String lookandfeel) throws ApplicationException {
115            lookandfeel=lookandfeel.trim().toLowerCase();
116            if(lookandfeel.equals("motif"))params.setEL("lookandfeel",lookandfeel);
117            else if(lookandfeel.equals("windows"))params.setEL("lookandfeel",lookandfeel);
118            else if(lookandfeel.equals("metal"))params.setEL("lookandfeel",lookandfeel);
119            else throw new ApplicationException("value of attribute lookAndFeel ["+lookandfeel+"] is invalid","valid values are [motif,windows,metal]");
120            
121        }
122        
123        /**
124         * sets the attribut to tag
125         * @param range
126         * @throws PageException
127         */
128        public void setRange(String range) throws PageException {
129            String errMessage="attribute range has an invalid value ["+range+"], must be string list with numbers";
130            String errDetail="Example: [number_from,number_to], [number_from], [number_from,], [,number_to]";
131            
132            Array arr=ListUtil.listToArray(range,',');
133            
134            if(arr.size()==1) {
135                double from=Caster.toDoubleValue(arr.get(1,null),Double.NaN);
136                if(!Decision.isValid(from))throw new ApplicationException(errMessage,errDetail);
137                input.setRangeMin(from);
138                input.setRangeMax(Double.NaN);
139                if(from<100)params.setEL("minimum",Caster.toString(from));
140            }
141            else if(arr.size()==2) {
142                String strFrom=arr.get(1,"").toString().trim();
143                double from=Caster.toDoubleValue(strFrom,Double.NaN);
144                if(!Decision.isValid(from) && strFrom.length()>0) {
145                    throw new ApplicationException(errMessage,errDetail);
146                }
147                input.setRangeMin(from);
148                
149                String strTo=arr.get(2,"").toString().trim();
150                double to=Caster.toDoubleValue(strTo,Double.NaN);
151                if(!Decision.isValid(to) && strTo.length()>0) {
152                    throw new ApplicationException(errMessage,errDetail);
153                }
154                input.setRangeMax(to);
155                
156                if(from<to) {
157                    params.setEL("minimum",Caster.toString(from));
158                    params.setEL("maximum",Caster.toString(to));
159                }
160                
161            }
162            else throw new ApplicationException(errMessage,errDetail);
163        }
164        
165        /**
166         * sets the attribut to tag
167         * @param message
168         */
169        public void setMessage(String message) {
170            input.setMessage(message);
171        }
172        
173        /**
174         * sets the attribut to tag
175         * @param name
176         */
177        public void setName(String name) {
178            input.setName(name);
179        }
180        
181        /**
182         * sets the attribut to tag
183         * @param notsupported
184         */
185        public void setNotsupported(String notsupported) {
186            params.setEL("notsupported",notsupported);
187        }
188        
189        /**
190         * sets the attribut to tag
191         * @param onerror
192         */
193        public void setOnerror(String onerror) {
194            input.setOnError(onerror);
195        }
196        
197        /**
198         * sets the attribut to tag
199         * @param onvalidate
200         */
201        public void setOnvalidate(String onvalidate) {
202            input.setOnValidate(onvalidate);
203        }
204    
205        /**
206         * sets the attribut to tag
207         * @param refreshlabel
208         */
209        public void setRefreshlabel(boolean refreshlabel) {
210            params.setEL("refreshlabel",Caster.toString(refreshlabel));
211        }
212        
213        /**
214         * sets the attribut to tag
215         * @param scale
216         */
217        public void setScale(double scale) {
218            params.setEL("scale",Caster.toString((int)scale));
219        }
220        
221        /**
222         * sets the attribut to tag
223         * @param textcolor
224         */
225        public void setTextcolor(String textcolor) {
226            params.setEL("textcolor",textcolor);
227        }
228        
229        /**
230         * sets the attribut to tag
231         * @param tickmarkimages
232         */
233        public void setTickmarkimages(String tickmarkimages) {
234            params.setEL("tickmarkimages",tickmarkimages);
235        }
236        
237        /**
238         * sets the attribut to tag
239         * @param tickmarklabels
240         */
241        public void setTickmarklabels(String tickmarklabels) {
242            params.setEL("tickmarklabels",tickmarklabels);
243        }
244        
245        /**
246         * sets the attribut to tag
247         * @param tickmarkmajor
248         */
249        public void setTickmarkmajor(boolean tickmarkmajor) {
250            params.setEL("tickmarkmajor",Caster.toString(tickmarkmajor));
251        }
252        
253        /**
254         * sets the attribut to tag
255         * @param tickmarkminor
256         */
257        public void setTickmarkminor(boolean tickmarkminor) {
258            params.setEL("tickmarkminor",Caster.toString(tickmarkminor));
259        }
260        
261        /**
262         * sets the attribut to tag
263         * @param value
264         */
265        public void setValue(double value) {
266            params.setEL("value",Caster.toString((int)value));
267        }
268        
269        /**
270         * sets the attribut to tag
271         * @param vertical
272         */
273        public void setVertical(boolean vertical) {
274            params.setEL("vertical",Caster.toString(vertical));
275            
276        }
277    
278        /**
279         * sets the attribut to tag
280         * @param height
281         */
282        public void setHeight(double height) {
283            this.height=(int)height;
284        }
285        
286        /**
287         * sets the attribut to tag
288         * @param hspace
289         */
290        public void setHspace(double hspace) {
291            this.hspace = (int)hspace;
292        }
293        
294        /**
295         * sets the attribut to tag
296         * @param vspace
297         */
298        public void setVspace(double vspace) {
299            this.vspace = (int) vspace;
300        }
301        
302        /**
303         * sets the attribut to tag
304         * @param width
305         */
306        public void setWidth(double width) {
307            this.width = (int) width;
308        }
309    
310        @Override
311        public int doStartTag() throws PageException {
312            try {
313                _doStartTag();
314            } catch (IOException e) {
315                throw Caster.toPageException(e);
316            }
317            return SKIP_BODY;
318        }
319    
320        private void _doStartTag() throws PageException, IOException  {
321            Tag parent = getParent();
322            while(parent!=null && !(parent instanceof Form)){
323                            parent=parent.getParent();
324                    }
325                    
326                    if(!(parent instanceof Form)) {
327                      throw new ApplicationException("Tag slider must be inside a form tag");
328                    }
329                    Form form = (Form)parent;
330                form.setInput(input);
331                String codebase;
332            
333            pageContext.forceWrite("<input type=\"hidden\" name=\""+enc(input.getName())+"\" value=\"\">");
334            pageContext.forceWrite("<applet MAYSCRIPT code=\"thinlet.AppletLauncher\"");
335            pageContext.forceWrite(" archive=\""+form.getArchive()+"?version=101\"");
336            pageContext.forceWrite(" width=\""+width+"\"");
337            if(!StringUtil.isEmpty(codebase=form.getCodebase()))
338                    pageContext.forceWrite(" codebase=\""+codebase+"\"");
339            if(height>0)pageContext.forceWrite(" height=\""+height+"\"");
340            if(hspace>0)pageContext.forceWrite(" hspace=\""+hspace+"\"");
341            if(vspace>0)pageContext.forceWrite(" vspace=\""+vspace+"\"");
342            Object align = params.get("align",null);
343            if(align!=null)pageContext.forceWrite(" align=\""+align+"\"");
344            pageContext.forceWrite(">\n");
345            pageContext.forceWrite("<param name=\"class\" value=\"railo.applet.SliderThinlet\"></param>\n");
346            pageContext.forceWrite("<param name=\"form\" value=\""+enc(form.getName())+"\"></param>\n");
347            pageContext.forceWrite("<param name=\"element\" value=\""+enc(input.getName())+"\"></param>\n");
348            
349            Iterator<Entry<Key, Object>> it = params.entryIterator();
350            Entry<Key, Object> e;
351            while(it.hasNext()) {
352                e = it.next();
353                pageContext.forceWrite("<param name=\"");
354                pageContext.forceWrite(e.getKey().getString());
355                pageContext.forceWrite("\" value=\"");
356                pageContext.forceWrite(enc(Caster.toString(e.getValue())));
357                pageContext.forceWrite("\"></param>\n");
358            }
359            pageContext.forceWrite("</applet>");
360        }
361    
362        /**
363         * html encode a string
364         * @param str string to encode
365         * @return encoded string
366         */
367        private String enc(String str) {
368            return HTMLEntities.escapeHTML(str,HTMLEntities.HTMLV20);
369        }
370    }