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