001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.CmsPushButton; 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.logical.shared.HasValueChangeHandlers; 038import com.google.gwt.event.logical.shared.ValueChangeEvent; 039import com.google.gwt.event.logical.shared.ValueChangeHandler; 040import com.google.gwt.event.shared.HandlerRegistration; 041import com.google.gwt.user.client.ui.Composite; 042 043/** 044 * Tri-state checkbox.<p> 045 */ 046public class CmsTriStateCheckBox extends Composite implements HasValueChangeHandlers<CmsTriStateCheckBox.State> { 047 048 /** 049 * The possible check box states.<p> 050 */ 051 public enum State { 052 /** neither on nor off. */ 053 middle, 054 /** off. **/ 055 off, 056 /** on. **/ 057 on; 058 } 059 060 /** The CSS bundle for this class. */ 061 protected static final I_CmsInputCss CSS = I_CmsInputLayoutBundle.INSTANCE.inputCss(); 062 063 /** The button which provides the actual widget layout. */ 064 private CmsPushButton m_button; 065 066 /** The state to use as a next state if the user clicks on the checkbox while it is in its third state. */ 067 private State m_nextStateAfterMiddle = State.off; 068 069 /** The current state. */ 070 private State m_state; 071 072 /** The check box text. */ 073 private String m_text; 074 075 /** 076 * Creates a new instance.<p> 077 * 078 * @param labelText the label text 079 */ 080 public CmsTriStateCheckBox(String labelText) { 081 082 m_button = new CmsPushButton(); 083 m_text = labelText; 084 initWidget(m_button); 085 m_button.setButtonStyle(ButtonStyle.TRANSPARENT, null); 086 addStyleName(CSS.triState()); 087 m_state = State.middle; 088 updateStyle(); 089 m_button.addClickHandler(new ClickHandler() { 090 091 public void onClick(ClickEvent event) { 092 093 CmsTriStateCheckBox.this.onClick(); 094 } 095 }); 096 097 } 098 099 /** 100 * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler) 101 */ 102 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<State> handler) { 103 104 return addHandler(handler, ValueChangeEvent.getType()); 105 } 106 107 /** 108 * Sets the state which the check box should transition to if the user clicks on it while it is neither on nor off.<p> 109 * 110 * @param state the target state 111 */ 112 public void setNextStateAfterIntermediateState(State state) { 113 114 m_nextStateAfterMiddle = state; 115 } 116 117 /** 118 * Sets the state of the check box and optionally fires an event.<p> 119 * 120 * @param state the new state 121 * @param fireEvent true if a ValueChangeEvent should be fired 122 */ 123 public void setState(State state, boolean fireEvent) { 124 125 boolean changed = m_state != state; 126 m_state = state; 127 if (changed) { 128 updateStyle(); 129 } 130 if (fireEvent) { 131 ValueChangeEvent.fire(this, state); 132 } 133 } 134 135 /** 136 * Sets the check box label text.<p> 137 * 138 * @param text the new label text 139 */ 140 public void setText(String text) { 141 142 m_text = text; 143 updateStyle(); 144 } 145 146 /** 147 * Handles clicks on the check box.<p> 148 */ 149 protected void onClick() { 150 151 State newState = getNextState(m_state); 152 setState(newState, true); 153 } 154 155 /** 156 * Gets the image class to use for the check box.<p> 157 * 158 * @return the image class 159 */ 160 private String getImageClass() { 161 162 switch (m_state) { 163 case on: 164 return CSS.triStateOn(); 165 case off: 166 return CSS.triStateOff(); 167 case middle: 168 default: 169 return CSS.triStateMedium(); 170 } 171 172 } 173 174 /** 175 * Gets the state which the check box would change to if cilcked on in a given state.<p> 176 * 177 * @param state the original state 178 * @return the next state 179 */ 180 private State getNextState(State state) { 181 182 State nextState = null; 183 switch (state) { 184 case off: 185 nextState = State.on; 186 break; 187 case on: 188 nextState = State.off; 189 break; 190 case middle: 191 default: 192 nextState = m_nextStateAfterMiddle; 193 } 194 return nextState; 195 } 196 197 /** 198 * Updates the UI state according to the logical state.<p> 199 */ 200 private void updateStyle() { 201 202 m_button.setUpFace(m_text, getImageClass()); 203 } 204}