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