001package com.hfg.html.custom; 002 003import com.hfg.html.Link; 004 005//------------------------------------------------------------------------------ 006/** 007 <p> 008 Widget for expanding / collapsing a multi-select box. 009 </p> 010 <p> 011 See <a href='doc-files/MultiSelectExpanderTest.html' target='_parent'>this page</a> for an example. 012 </p> 013 014 Example: 015 <pre> 016 HTMLDoc doc = new HTMLDoc(); 017 HTML html = new HTML(); 018 doc.setRootTag(html); 019 020 // Add the CSS classes needed 021 html.getHead().addStyle(MultiSelectExpander.generateCSS()); 022 // Add the javascript needed 023 html.getHead().addJavascript(MultiSelectExpander.generateJavascript()); 024 025 Body body = html.getBody(); 026 027 String selectId = "MySelect"; 028 029 MultiSelectExpander expander = new MultiSelectExpander(selectId); 030 031 body.br(3); 032 body.addSubtag(expander); 033 Select select = body.addSelect(); 034 035 </pre> 036 037 @author J. Alex Taylor, hairyfatguy.com 038 */ 039//------------------------------------------------------------------------------ 040// com.hfg XML/HTML Coding Library 041// 042// This library is free software; you can redistribute it and/or 043// modify it under the terms of the GNU Lesser General Public 044// License as published by the Free Software Foundation; either 045// version 2.1 of the License, or (at your option) any later version. 046// 047// This library is distributed in the hope that it will be useful, 048// but WITHOUT ANY WARRANTY; without even the implied warranty of 049// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 050// Lesser General Public License for more details. 051// 052// You should have received a copy of the GNU Lesser General Public 053// License along with this library; if not, write to the Free Software 054// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 055// 056// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 057// jataylor@hairyfatguy.com 058//------------------------------------------------------------------------------ 059 060public class MultiSelectExpander extends Link 061{ 062 //########################################################################## 063 // CONSTRUCTORS 064 //########################################################################## 065 066 //-------------------------------------------------------------------------- 067 public MultiSelectExpander(String inMultiSelectId) 068 { 069 super("javascript:void(0);", "[+]"); 070 setClass("expander"); 071 setTitle("Expand/Contract the multi-select box"); 072 setOnClick("adjustMultiSelectSize(this, '" + inMultiSelectId + "', 0);"); 073 setOnDblClick("adjustMultiSelectSize(this, '" + inMultiSelectId + "', 1);"); 074 } 075 076 //########################################################################## 077 // PUBLIC METHODS 078 //########################################################################## 079 080 //-------------------------------------------------------------------------- 081 public static String generateCSS() 082 { 083 StringBuffer css = new StringBuffer(); 084 085 css.append(".expander {\n" 086 + " background-color: #000000;\n" 087 + " color: #ffffff;\n" 088 + " font-size: 8pt;\n" 089 + " text-decoration: none;\n" 090 + " cursor: default;\n" 091 + " padding: 0px 2px 0px 2px;\n" 092 + " }\n\n"); 093 094 095 return css.toString(); 096 } 097 098 //-------------------------------------------------------------------------- 099 public static String generateJavascript() 100 { 101 StringBuffer js = new StringBuffer(); 102 103 js.append("//---------------------------------------------------------------\n" 104 + "function adjustMultiSelectSize(inLink, inMultiSelectId, inDblClick)\n" 105 + "{\n" 106 // On a double-click, Safari seems to fire a click event followed by a double-click event. 107 + " var multiSelect = document.getElementById(inMultiSelectId);\n" 108 + " var isExpanded = (inLink.firstChild.data == '[-]');\n" 109 + " if (inDblClick) isExpanded = !isExpanded;\n" 110 + " var newSize = 1;\n" 111 + " \n" 112 + " if (isExpanded)\n" 113 + " {\n" 114 + " inLink.firstChild.data = '[+]';\n" 115 + " newSize = multiSelect.size / 2;\n" 116 + " if (newSize < 1) newSize = 1;\n" 117 + " }\n" 118 + " else\n" 119 + " {\n" 120 + " inLink.firstChild.data = '[-]';\n" 121 + " newSize = multiSelect.size * 2;\n" 122 + " if (newSize > multiSelect.options.length) newSize = multiSelect.options.length;\n" 123 + " }\n" 124 + " \n" 125 + " multiSelect.size = newSize;\n" 126 + " // This forces Safari to redraw the select box\n" 127 + " multiSelect.style.display = 'block';\n" 128 + "}\n\n"); 129 130 return js.toString(); 131 } 132 133}