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.ui.client; 029 030import org.opencms.ui.components.extensions.CmsAutoGrowingTextArea; 031import org.opencms.ui.shared.components.CmsAutoGrowingTextAreaState; 032 033import com.google.gwt.core.client.Scheduler; 034import com.google.gwt.core.client.Scheduler.ScheduledCommand; 035import com.google.gwt.dom.client.Element; 036import com.google.gwt.dom.client.TextAreaElement; 037import com.google.gwt.event.dom.client.KeyUpEvent; 038import com.google.gwt.event.dom.client.KeyUpHandler; 039import com.google.gwt.user.client.ui.Widget; 040import com.vaadin.client.ComponentConnector; 041import com.vaadin.client.ServerConnector; 042import com.vaadin.client.extensions.AbstractExtensionConnector; 043import com.vaadin.shared.ui.Connect; 044 045/** 046 * This connector will manipulate the CSS classes of the extended widget depending on the scroll position.<p> 047 */ 048@Connect(CmsAutoGrowingTextArea.class) 049public class CmsAutoGrowingTextAreaConnector extends AbstractExtensionConnector { 050 051 /** The serial version id. */ 052 private static final long serialVersionUID = -9079215265941920364L; 053 054 /** The widget to enhance. */ 055 private Widget m_widget; 056 057 /** The line height. */ 058 private int m_lineHeight; 059 060 /** The padding height. */ 061 private int m_paddingHeight; 062 063 /** The last value. */ 064 private String m_lastValue = ""; 065 066 /** 067 * @see com.vaadin.client.ui.AbstractConnector#getState() 068 */ 069 @Override 070 public CmsAutoGrowingTextAreaState getState() { 071 072 return (CmsAutoGrowingTextAreaState)super.getState(); 073 } 074 075 /** 076 * @see com.vaadin.client.extensions.AbstractExtensionConnector#extend(com.vaadin.client.ServerConnector) 077 */ 078 @Override 079 protected void extend(ServerConnector target) { 080 081 // Get the extended widget 082 m_widget = ((ComponentConnector)target).getWidget(); 083 m_widget.addDomHandler(new KeyUpHandler() { 084 085 public void onKeyUp(KeyUpEvent event) { 086 087 handle(); 088 089 } 090 }, KeyUpEvent.getType()); 091 092 Scheduler.get().scheduleDeferred(new ScheduledCommand() { 093 094 public void execute() { 095 096 handle(); 097 } 098 }); 099 100 } 101 102 /** 103 * Resize the text area.<p> 104 */ 105 protected void handle() { 106 107 Element e = m_widget.getElement(); 108 if (e instanceof TextAreaElement) { 109 TextAreaElement elem = (TextAreaElement)e; 110 int scrollHeight = elem.getScrollHeight(); 111 // allow text area to shrink 112 if (m_lastValue.length() > elem.getValue().length()) { 113 elem.setRows(getState().getMinRows()); 114 } 115 int currentRows = elem.getRows(); 116 if (m_lineHeight == 0) { 117 elem.setRows(2); 118 int heightTwo = elem.getClientHeight(); 119 elem.setRows(1); 120 int heightOne = elem.getClientHeight(); 121 m_lineHeight = heightTwo - heightOne; 122 m_paddingHeight = heightOne - m_lineHeight; 123 elem.setRows(currentRows); 124 } 125 if (m_lineHeight > 0) { 126 int totalHeight = scrollHeight - m_paddingHeight; 127 int requiredRows = ((scrollHeight - m_paddingHeight) / m_lineHeight); 128 if ((totalHeight % m_lineHeight) > 0) { 129 requiredRows++; 130 } 131 int minRows = getState().getMinRows(); 132 int maxRows = getState().getMaxRows(); 133 if ((requiredRows <= minRows) && (currentRows != minRows)) { 134 elem.setRows(minRows); 135 } else if ((requiredRows >= maxRows) && (currentRows != maxRows)) { 136 elem.setRows(maxRows); 137 } else if (requiredRows != currentRows) { 138 elem.setRows(requiredRows); 139 } 140 } 141 } 142 } 143}