KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > BToggleButton


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: BToggleButton.java,v 1.15 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp;
21
22 import java.util.*;
23
24 import org.apache.log4j.*;
25 import org.w3c.dom.*;
26 import org.w3c.dom.html.*;
27
28 import org.enhydra.barracuda.core.comp.renderer.*;
29 import org.enhydra.barracuda.core.comp.renderer.html.*;
30 import org.enhydra.barracuda.core.comp.renderer.xml.*;
31 import org.enhydra.barracuda.core.util.dom.DOMUtil;
32 import org.enhydra.barracuda.core.view.*;
33 import org.enhydra.barracuda.plankton.*;
34
35 /**
36  * BToggleButton is used to render Radio or Checkbox buttons in
37  * a DOM template.
38  *
39  * <p>In most cases you will not actually need to bind the component
40  * to a view in order to use it--if you return it from a model, this
41  * will be done for you automatically. If however, you intend to use
42  * the component <em>standalone</em> (ie. manually attaching it to a
43  * specific node in the DOM) or <em>inline</em> (ie. in a toString()),
44  * then you MUST BIND IT TO A VIEW before rendering, or an error will
45  * be generated.
46  */

47 public class BToggleButton extends BInput {
48
49     //public vars
50
protected static final Logger logger = Logger.getLogger(BToggleButton.class.getName());
51
52     //private vars
53
protected boolean selected = false;
54
55     //--------------- Constructors -------------------------------
56
/**
57      * Public noargs constructor
58      */

59     public BToggleButton() {}
60     
61     /**
62      * Public constructor which creates the component and
63      * binds it to a specific view
64      *
65      * @param view the View the component should be bound to
66      */

67 // public BToggleButton(View iview) {
68
// this(null, null, null, false, iview, null);
69
// }
70

71     /**
72      * Public constructor which creates the component and
73      * sets the initial data.
74      *
75      * <p>Null values may be passed in for any parameters,
76      * but if you do so you will need manually provide these
77      * values (via the accessor methods) prior to actually
78      * rendering the component
79      *
80      * @param itype BInput.RADIO, BInput.CHECKBOX, or null (indicating
81      * don't render this attribute)
82      * @param iname the name of the button, or null (indicating
83      * don't render this attribute)
84      * @param ivalue the value of the button, or null (indicating
85      * don't render this attribute)
86      * @param iselected true if the button should be selected
87      */

88     public BToggleButton(String JavaDoc itype, String JavaDoc iname, String JavaDoc ivalue, boolean iselected) {
89         this (itype, iname, ivalue, iselected, null, null);
90     }
91
92     /**
93      * Public constructor which creates the component and
94      * sets the initial data. The component is also
95      * bound to the specified view.
96      *
97      * <p>Null values may be passed in for any parameters,
98      * but if you do so you will need manually provide these
99      * values (via the accessor methods) prior to actually
100      * rendering the component
101      *
102      * @param type BInput.RADIO, BInput.CHECKBOX, or null (indicating
103      * don't render this attribute)
104      * @param name the name of the button, or null (indicating
105      * don't render this attribute)
106      * @param value the value of the button, or null (indicating
107      * don't render this attribute)
108      * @param selected true if the button should be selected
109      * @param view the View the component should be bound to
110      * @param dvc the default ViewContext (opt--its presence allows the
111      * component to be rendered as markup in toString())
112      */

113     BToggleButton(String JavaDoc itype, String JavaDoc iname, String JavaDoc ivalue, boolean iselected, View iview, ViewContext idvc) {
114         if (idvc!=null) setDefaultViewContext(idvc);
115         if (itype!=null) setType(itype);
116         if (iname!=null) setName(iname);
117         if (ivalue!=null) setValue(ivalue);
118         setSelected(iselected);
119         if (iview!=null) setView(iview);
120     }
121     
122
123
124
125     //--------------- Renderer -----------------------------------
126
/**
127      * Default component renderer factory registrations
128      */

129     static {
130         HTMLRendererFactory rfHTML = new HTMLRendererFactory();
131         installRendererFactory(rfHTML, BToggleButton.class, HTMLElement.class);
132         installRendererFactory(rfHTML, BToggleButton.class, HTMLDocument.class);
133
134     }
135
136     /**
137      * HTML RendererFactory
138      */

139     static class HTMLRendererFactory implements RendererFactory {
140         public Renderer getInstance() {return new HTMLToggleRenderer();}
141     }
142
143
144
145
146     //--------------- BToggleButton ------------------------------
147
/**
148      * Set the input type (RADIO, or CHECKBOX). If this value remains null,
149      * the type will default to whatever is specified in the underlying
150      * markup. If you set this value, then the type will be overridden
151      * in all the views associated with this component.
152      *
153      * @param itype an string value representing the size.
154      */

155     public void setType(String JavaDoc itype) {
156         itype = itype.toLowerCase();
157         if (itype!=null && !itype.equals(RADIO) && !itype.equals(CHECKBOX)) {
158             itype = CHECKBOX;
159         }
160         super.setType(itype);
161     }
162     
163     /**
164      * Specify whether or not the button is selected.
165      *
166      * @param iselected true if the button is selected
167      */

168     public void setSelected(boolean iselected) {
169         selected = iselected;
170         invalidate();
171     }
172     
173     /**
174      * Return true if the button is selected.
175      *
176      * @return true if the button is selected.
177      */

178     public boolean isSelected() {
179         return selected;
180     }
181
182     /**
183      * Render a specific view for the component.
184      *
185      * @param view View to be rendered
186      * @param vc ViewContext for the client view
187      * @throws RenderException if the particular View is not supported
188      * @param list a List of all the views for this component
189      */

190 /*
191 //021102.3_csc - removed, because now its in BComponent
192     protected void renderView (View view, ViewContext vc, int depth) throws RenderException {
193         if (logger.isInfoEnabled()) logger.info("rendering comp:"+this.toRef()+" view:"+view);
194
195         //actually render the view according to known interfaces
196         try {
197             Renderer r = getRenderer(view);
198             r.renderComponent(this, view, vc);
199             
200         } catch (DOMException e) {
201             logger.warn("DOM Error:", e);
202             throw new DOMAccessException("Error rendering component in view:"+e, e);
203         }
204     }
205 */

206 }
Popular Tags