001package com.hfg.html.custom; 002 003 004import com.hfg.html.Table; 005import com.hfg.html.Td; 006import com.hfg.html.attribute.Align; 007import com.hfg.util.StringBuilderPlus; 008 009//------------------------------------------------------------------------------ 010/** 011 Widget for expanding / collapsing a textarea. 012 <p> 013 See <a href='doc-files/TextareaExpanderTest.html' target='_parent'>this page</a> for examples. 014 </p> 015 <pre> 016 HTMLDoc doc = new HTMLDoc(); 017 HTML html = new HTML("TextareaExpander Test"); 018 doc.setRootTag(html); 019 020 Body body = html.getBody(); 021 022 Form form = body.addForm(); 023 024 form.br(3); 025 026 // Add the CSS classes needed 027 html.getHead().addStyle(TextareaExpander.generateCSS()); 028 // Add the javascript needed 029 html.getHead().addJavascript(TextareaExpander.generateJavascript()); 030 031 String textareaId = "textarea1"; 032 033 TextareaExpander textareaExpander = new TextareaExpander(textareaId); 034 Table table = form.addTable().setCellSpacing(0); 035 Tr row = table.addRow(); 036 row.addCell(textareaExpander); 037 row.addCell().setVAlign(VAlign.BOTTOM).addSpan("Example textarea"); 038 form.addTextarea("foo1", "").setId(textareaId).setRows(5).setCols(20); 039 </pre> 040 @author J. Alex Taylor, hairyfatguy.com 041 */ 042//------------------------------------------------------------------------------ 043// com.hfg XML/HTML Coding Library 044// 045// This library is free software; you can redistribute it and/or 046// modify it under the terms of the GNU Lesser General Public 047// License as published by the Free Software Foundation; either 048// version 2.1 of the License, or (at your option) any later version. 049// 050// This library is distributed in the hope that it will be useful, 051// but WITHOUT ANY WARRANTY; without even the implied warranty of 052// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 053// Lesser General Public License for more details. 054// 055// You should have received a copy of the GNU Lesser General Public 056// License along with this library; if not, write to the Free Software 057// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 058// 059// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 060// jataylor@hairyfatguy.com 061//------------------------------------------------------------------------------ 062 063public class TextareaExpander extends Table 064{ 065 //########################################################################## 066 // CONSTRUCTORS 067 //########################################################################## 068 069 //-------------------------------------------------------------------------- 070 public TextareaExpander(String inTextareaId) 071 { 072 super(); 073 074 setCellPadding(0); 075 setCellSpacing(0); 076 setClass("textarea_expander"); 077 078 Td cell = addRow().addCell("").setAlign(Align.CENTER); 079 cell.addLink("javascript:void(0);", "/\\").setOnClick("adjustTextareaSize(this, '" + inTextareaId + "', 'shorten');") 080 .setTitle("Shorten textarea") 081 .setClass("textarea_expander"); 082 cell.br(); 083 cell.addLink("javascript:void(0);", "<").setOnClick("adjustTextareaSize(this, '" + inTextareaId + "', 'narrow');") 084 .setTitle("Narrow textarea") 085 .setClass("textarea_expander"); 086 cell.nbsp(1); 087 cell.addLink("javascript:void(0);").setOnClick("adjustTextareaSize(this, '" + inTextareaId + "', 'default');") 088 .setClass("textarea_expander") 089 .setTitle("Default size") 090 .addContentWithoutEscaping("•"); 091 cell.nbsp(1); 092 cell.addLink("javascript:void(0);", ">").setOnClick("adjustTextareaSize(this, '" + inTextareaId + "', 'widen');") 093 .setTitle("Widen textarea") 094 .setClass("textarea_expander"); 095 cell.br(); 096 cell.addLink("javascript:void(0);", "\\/").setOnClick("adjustTextareaSize(this, '" + inTextareaId + "', 'lengthen');") 097 .setTitle("Lengthen textarea") 098 .setClass("textarea_expander"); 099 100 } 101 102 //########################################################################## 103 // PUBLIC METHODS 104 //########################################################################## 105 106 107 //--------------------------------------------------------------------------- 108 @Override 109 public TextareaExpander clone() 110 { 111 return (TextareaExpander) super.clone(); 112 } 113 114 //-------------------------------------------------------------------------- 115 public static String generateCSS() 116 { 117 StringBuilderPlus css = new StringBuilderPlus() 118 .appendln("table.textarea_expander {") 119 .appendln(" background-color: #999999;") 120 .appendln(" font-size: .8em;") 121 .appendln(" line-height: 55%;") 122 .appendln("}") 123 .appendln("a.textarea_expander {") 124 .appendln(" color: #ffffff;") 125 .appendln(" font-weight: bold;") 126 .appendln(" text-decoration: none;") 127 .appendln(" cursor: default;") 128 .appendln("}"); 129 130 return css.toString(); 131 } 132 133 //-------------------------------------------------------------------------- 134 public static String generateJavascript() 135 { 136 StringBuilderPlus js = new StringBuilderPlus() 137 .appendln("var defaultTextareaSizes = new Array();") 138 .appendln("//---------------------------------------------------------------") 139 .appendln("function adjustTextareaSize(inLink, inTextareaId, inAction) {") 140 .appendln(" var textarea = document.getElementById(inTextareaId);") 141 .appendln(" if (null == defaultTextareaSizes[inTextareaId + '.cols']) {") 142 .appendln(" defaultTextareaSizes[inTextareaId + '.cols'] = textarea.cols;") 143 .appendln(" defaultTextareaSizes[inTextareaId + '.rows'] = textarea.rows;") 144 .appendln(" }") 145 .appendln("") 146 .appendln(" if (inAction == 'lengthen') {") 147 .appendln(" textarea.rows += (textarea.rows * 0.25);") 148 .appendln(" }") 149 .appendln(" else if (inAction == 'shorten' && textarea.rows > 5) {") 150 .appendln(" textarea.rows += -(textarea.rows * 0.25);") 151 .appendln(" }") 152 .appendln(" else if (inAction == 'widen') {") 153 .appendln(" textarea.cols += (textarea.cols * 0.25);") 154 .appendln(" }") 155 .appendln(" else if (inAction == 'narrow' && textarea.cols > 5) {") 156 .appendln(" textarea.cols += -(textarea.cols * 0.25);") 157 .appendln(" }") 158 .appendln(" else if (inAction == 'default') {") 159 .appendln(" textarea.cols = defaultTextareaSizes[inTextareaId + '.cols'];") 160 .appendln(" textarea.rows = defaultTextareaSizes[inTextareaId + '.rows'];") 161 .appendln(" }") 162 .appendln("") 163 .appendln(" // This forces Safari to redraw the textarea") 164 .appendln(" textarea.style.display = 'inline';") 165 .appendln(" setTimeout('showTextarea(\\'' + inTextareaId + '\\')', 10);") 166 .appendln("}") 167 .appendln("//---------------------------------------------------------------") 168 .appendln("function showTextarea(inTextareaId) {") 169 .appendln(" var textarea = document.getElementById(inTextareaId);") 170 .appendln(" textarea.style.display = 'block';") 171 .appendln("}"); 172 173 return js.toString(); 174 } 175 176}