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.acacia.client.widgets; 029 030import org.opencms.acacia.client.css.I_CmsWidgetsLayoutBundle; 031import org.opencms.gwt.client.ui.input.CmsTextBox; 032 033import com.google.gwt.dom.client.Element; 034import com.google.gwt.event.dom.client.FocusEvent; 035import com.google.gwt.event.dom.client.FocusHandler; 036import com.google.gwt.event.logical.shared.ValueChangeEvent; 037import com.google.gwt.event.logical.shared.ValueChangeHandler; 038import com.google.gwt.event.shared.HandlerRegistration; 039import com.google.gwt.user.client.ui.Composite; 040 041/** 042 * Provides a display only widget, for use on a widget dialog.<br> 043 * If there is no value in the content xml, the value<br> 044 * set in the configuration string of the xsd is shown.<p> 045 * 046 * */ 047public class CmsDisplayWidget extends Composite implements I_CmsEditWidget { 048 049 /** Value of the activation. */ 050 private boolean m_active = true; 051 052 /** Default value set in XSD. */ 053 private String m_default = ""; 054 055 /** The disabled textbox to show the value. */ 056 private CmsTextBox m_textbox = new CmsTextBox(); 057 058 /** 059 * Creates a new display widget.<p> 060 * 061 * @param config The configuration string given from OpenCms XSD. 062 */ 063 public CmsDisplayWidget(String config) { 064 065 m_default = config; 066 // All composites must call initWidget() in their constructors. 067 initWidget(m_textbox); 068 069 m_textbox.getTextBoxContainer().addStyleName( 070 I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().displayTextBoxPanel()); 071 m_textbox.getTextBox().addStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().displayTextBox()); 072 m_textbox.setReadOnly(true); 073 } 074 075 /** 076 * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler) 077 */ 078 public HandlerRegistration addFocusHandler(FocusHandler handler) { 079 080 return addDomHandler(handler, FocusEvent.getType()); 081 } 082 083 /** 084 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler) 085 */ 086 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { 087 088 return addHandler(handler, ValueChangeEvent.getType()); 089 } 090 091 /** 092 * Represents a value change event.<p> 093 * 094 */ 095 public void fireChangeEvent() { 096 097 String result = ""; 098 if (m_textbox.getText() != null) { 099 if (!m_textbox.getText().equals(m_default)) { 100 result = m_textbox.getText(); 101 } 102 } 103 104 ValueChangeEvent.fire(this, result); 105 106 } 107 108 /** 109 * @see com.google.gwt.user.client.ui.HasValue#getValue() 110 */ 111 public String getValue() { 112 113 return m_textbox.getText(); 114 } 115 116 /** 117 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive() 118 */ 119 public boolean isActive() { 120 121 return m_active; 122 } 123 124 /** 125 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget() 126 */ 127 public void onAttachWidget() { 128 129 super.onAttach(); 130 } 131 132 /** 133 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element) 134 */ 135 public boolean owns(Element element) { 136 137 // TODO implement this in case we want the delete behavior for optional fields 138 return false; 139 140 } 141 142 /** 143 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean) 144 */ 145 public void setActive(boolean active) { 146 147 // check if the value has changed. If there is no change do nothing. 148 if (m_active == active) { 149 return; 150 } 151 m_textbox.setEnabled(active); 152 m_active = active; 153 if (active) { 154 fireChangeEvent(); 155 } 156 } 157 158 /** 159 * Sets the color for the input box.<p> 160 * 161 * @param color the color that should be set 162 * */ 163 public void setColor(String color) { 164 165 m_textbox.getTextBox().getElement().getStyle().setColor(color); 166 } 167 168 /** 169 * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String) 170 */ 171 public void setName(String name) { 172 173 // no input field so nothing to do 174 175 } 176 177 /** 178 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object) 179 */ 180 public void setValue(String value) { 181 182 setValue(value, false); 183 } 184 185 /** 186 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean) 187 */ 188 public void setValue(String value, boolean fireEvents) { 189 190 // add the saved value to the display field 191 if (value.equals("")) { 192 m_textbox.setFormValueAsString(m_default); 193 } else { 194 m_textbox.setFormValueAsString(value); 195 } 196 if (fireEvents) { 197 fireChangeEvent(); 198 } 199 } 200 201}