com.hfg.html.custom.TextareaExpander Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.html.custom;
import com.hfg.html.Table;
import com.hfg.html.Td;
import com.hfg.html.attribute.Align;
import com.hfg.util.StringBuilderPlus;
//------------------------------------------------------------------------------
/**
Widget for expanding / collapsing a textarea.
See this page for examples.
HTMLDoc doc = new HTMLDoc();
HTML html = new HTML("TextareaExpander Test");
doc.setRootTag(html);
Body body = html.getBody();
Form form = body.addForm();
form.br(3);
// Add the CSS classes needed
html.getHead().addStyle(TextareaExpander.generateCSS());
// Add the javascript needed
html.getHead().addJavascript(TextareaExpander.generateJavascript());
String textareaId = "textarea1";
TextareaExpander textareaExpander = new TextareaExpander(textareaId);
Table table = form.addTable().setCellSpacing(0);
Tr row = table.addRow();
row.addCell(textareaExpander);
row.addCell().setVAlign(VAlign.BOTTOM).addSpan("Example textarea");
form.addTextarea("foo1", "").setId(textareaId).setRows(5).setCols(20);
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class TextareaExpander extends Table
{
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public TextareaExpander(String inTextareaId)
{
super();
setCellPadding(0);
setCellSpacing(0);
setClass("textarea_expander");
Td cell = addRow().addCell("").setAlign(Align.CENTER);
cell.addLink("javascript:void(0);", "/\\").setOnClick("adjustTextareaSize(this, '" + inTextareaId + "', 'shorten');")
.setTitle("Shorten textarea")
.setClass("textarea_expander");
cell.br();
cell.addLink("javascript:void(0);", "<").setOnClick("adjustTextareaSize(this, '" + inTextareaId + "', 'narrow');")
.setTitle("Narrow textarea")
.setClass("textarea_expander");
cell.nbsp(1);
cell.addLink("javascript:void(0);").setOnClick("adjustTextareaSize(this, '" + inTextareaId + "', 'default');")
.setClass("textarea_expander")
.setTitle("Default size")
.addContentWithoutEscaping("•");
cell.nbsp(1);
cell.addLink("javascript:void(0);", ">").setOnClick("adjustTextareaSize(this, '" + inTextareaId + "', 'widen');")
.setTitle("Widen textarea")
.setClass("textarea_expander");
cell.br();
cell.addLink("javascript:void(0);", "\\/").setOnClick("adjustTextareaSize(this, '" + inTextareaId + "', 'lengthen');")
.setTitle("Lengthen textarea")
.setClass("textarea_expander");
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
@Override
public TextareaExpander clone()
{
return (TextareaExpander) super.clone();
}
//--------------------------------------------------------------------------
public static String generateCSS()
{
StringBuilderPlus css = new StringBuilderPlus()
.appendln("table.textarea_expander {")
.appendln(" background-color: #999999;")
.appendln(" font-size: .8em;")
.appendln(" line-height: 55%;")
.appendln("}")
.appendln("a.textarea_expander {")
.appendln(" color: #ffffff;")
.appendln(" font-weight: bold;")
.appendln(" text-decoration: none;")
.appendln(" cursor: default;")
.appendln("}");
return css.toString();
}
//--------------------------------------------------------------------------
public static String generateJavascript()
{
StringBuilderPlus js = new StringBuilderPlus()
.appendln("var defaultTextareaSizes = new Array();")
.appendln("//---------------------------------------------------------------")
.appendln("function adjustTextareaSize(inLink, inTextareaId, inAction) {")
.appendln(" var textarea = document.getElementById(inTextareaId);")
.appendln(" if (null == defaultTextareaSizes[inTextareaId + '.cols']) {")
.appendln(" defaultTextareaSizes[inTextareaId + '.cols'] = textarea.cols;")
.appendln(" defaultTextareaSizes[inTextareaId + '.rows'] = textarea.rows;")
.appendln(" }")
.appendln("")
.appendln(" if (inAction == 'lengthen') {")
.appendln(" textarea.rows += (textarea.rows * 0.25);")
.appendln(" }")
.appendln(" else if (inAction == 'shorten' && textarea.rows > 5) {")
.appendln(" textarea.rows += -(textarea.rows * 0.25);")
.appendln(" }")
.appendln(" else if (inAction == 'widen') {")
.appendln(" textarea.cols += (textarea.cols * 0.25);")
.appendln(" }")
.appendln(" else if (inAction == 'narrow' && textarea.cols > 5) {")
.appendln(" textarea.cols += -(textarea.cols * 0.25);")
.appendln(" }")
.appendln(" else if (inAction == 'default') {")
.appendln(" textarea.cols = defaultTextareaSizes[inTextareaId + '.cols'];")
.appendln(" textarea.rows = defaultTextareaSizes[inTextareaId + '.rows'];")
.appendln(" }")
.appendln("")
.appendln(" // This forces Safari to redraw the textarea")
.appendln(" textarea.style.display = 'inline';")
.appendln(" setTimeout('showTextarea(\\'' + inTextareaId + '\\')', 10);")
.appendln("}")
.appendln("//---------------------------------------------------------------")
.appendln("function showTextarea(inTextareaId) {")
.appendln(" var textarea = document.getElementById(inTextareaId);")
.appendln(" textarea.style.display = 'block';")
.appendln("}");
return js.toString();
}
}