001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt.client.ui.input; 029 030import org.opencms.gwt.client.ui.CmsToggleButton; 031import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle; 032import org.opencms.gwt.client.ui.css.I_CmsInputCss; 033import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle; 034 035import com.google.gwt.event.dom.client.ClickEvent; 036import com.google.gwt.event.dom.client.ClickHandler; 037import com.google.gwt.event.dom.client.HasClickHandlers; 038import com.google.gwt.event.shared.HandlerRegistration; 039import com.google.gwt.user.client.ui.Composite; 040import com.google.gwt.user.client.ui.HasHorizontalAlignment; 041 042/** 043 * Class representing a single radio button.<p> 044 * 045 * In most cases, you will need to set the group of a radio button, which is a Java object, 046 * not just a string as in HTML radio buttons. Clicking on a radio button in a group will result 047 * in the radio button being selected, and none of the other buttons in the group being selected.<p> 048 * 049 * @since 8.0.0 050 * 051 */ 052public class CmsRadioButton extends Composite implements HasHorizontalAlignment, HasClickHandlers { 053 054 /** The CSS bundle instance used for this widget. */ 055 private static final I_CmsInputCss CSS = I_CmsInputLayoutBundle.INSTANCE.inputCss(); 056 057 /** The wigdet used to implement the actual radio button. */ 058 protected CmsToggleButton m_button = new CmsToggleButton(); 059 060 /** The radio button group. */ 061 protected CmsRadioButtonGroup m_group; 062 063 /** The current horizontal alignment. */ 064 private HorizontalAlignmentConstant m_align; 065 066 /** The value associated with this radio button. */ 067 private String m_name; 068 069 /** 070 * Creates a new radio button without setting the name and label text.<p> 071 */ 072 public CmsRadioButton() { 073 074 m_button.setUseMinWidth(false); 075 m_button.setButtonStyle(ButtonStyle.TRANSPARENT, null); 076 m_button.setImageClass(CSS.radioButtonImage()); 077 setHorizontalAlignment(ALIGN_RIGHT); 078 079 initWidget(m_button); 080 addStyleName(CSS.radioButton()); 081 addStyleName(CSS.inlineBlock()); 082 m_button.addClickHandler(new ClickHandler() { 083 084 /** 085 * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent) 086 */ 087 public void onClick(ClickEvent event) { 088 089 m_button.setDown(true); 090 if (m_group != null) { 091 m_group.selectButton(CmsRadioButton.this); 092 } 093 } 094 }); 095 096 } 097 098 /** 099 * Creates a new radio button.<p> 100 * 101 * @param name the value associated with this radio button 102 * @param labelText the label text of the radio button 103 */ 104 public CmsRadioButton(String name, String labelText) { 105 106 this(); 107 m_name = name; 108 if (labelText != null) { 109 m_button.setText(labelText); 110 } 111 } 112 113 /** 114 * Adds a click handler to the radio button.<p> 115 * 116 * @see com.google.gwt.event.dom.client.HasClickHandlers#addClickHandler(com.google.gwt.event.dom.client.ClickHandler) 117 */ 118 public HandlerRegistration addClickHandler(ClickHandler handler) { 119 120 return addDomHandler(handler, ClickEvent.getType()); 121 } 122 123 /** 124 * This is the alignment of the text in reference to the checkbox, possible values are left or right.<p> 125 * 126 * @see com.google.gwt.user.client.ui.HasHorizontalAlignment#getHorizontalAlignment() 127 */ 128 public HorizontalAlignmentConstant getHorizontalAlignment() { 129 130 return m_align; 131 } 132 133 /** 134 * Returns the value associated with this radio button.<p> 135 * 136 * @return the value associated with this radio button 137 */ 138 public String getName() { 139 140 return m_name; 141 } 142 143 /** 144 * Returns the radio button.<p> 145 * 146 * @return the radio button 147 */ 148 public CmsToggleButton getRadioButton() { 149 150 return m_button; 151 } 152 153 /** 154 * Returns true if the radio button is checked.<p> 155 * 156 * @return true if the radio button is checked 157 */ 158 public boolean isChecked() { 159 160 return m_button.isDown(); 161 } 162 163 /** 164 * Returns <code>true</code> if this widget is enabled.<p> 165 * 166 * @return <code>true</code> if this widget is enabled 167 */ 168 public boolean isEnabled() { 169 170 return m_button.isEnabled(); 171 } 172 173 /** 174 * Sets the 'checked' status of the radio button.<p> 175 * 176 * @param checked if true, check the radio button, else uncheck it 177 */ 178 public void setChecked(boolean checked) { 179 180 m_button.setDown(checked); 181 if (checked) { 182 m_group.selectButton(this); 183 } else { 184 m_group.deselectButton(); 185 } 186 } 187 188 /** 189 * Enables or disables the radio button.<p> 190 * 191 * @param enabled if true, the radio button is enabled, else disabled 192 */ 193 public void setEnabled(boolean enabled) { 194 195 m_button.setEnabled(enabled); 196 } 197 198 /** 199 * Sets the group for this radio button.<p> 200 * 201 * @param group the radio button group 202 */ 203 public void setGroup(CmsRadioButtonGroup group) { 204 205 m_group = group; 206 } 207 208 /** 209 * This is the alignment of the text in reference to the checkbox, possible values are left or right.<p> 210 * 211 * @see com.google.gwt.user.client.ui.HasHorizontalAlignment#setHorizontalAlignment(com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant) 212 */ 213 public void setHorizontalAlignment(HorizontalAlignmentConstant align) { 214 215 if (align.equals(HasHorizontalAlignment.ALIGN_CENTER)) { 216 // ignore center alignment 217 return; 218 } 219 m_button.setHorizontalAlignment(align); 220 m_align = align; 221 } 222 223 /** 224 * Sets the name of this radio button.<p> 225 * 226 * @param name the new name 227 */ 228 public void setName(String name) { 229 230 m_name = name; 231 } 232 233 /** 234 * Sets the text which is displayed next to the radio button.<p> 235 * 236 * @param text the new text 237 */ 238 public void setText(String text) { 239 240 m_button.setText(text); 241 } 242 243}