1 package org.lobobrowser.html.renderer; 2 3 4 import javax.swing.*; 5 import java.util.*; 6 7 import org.lobobrowser.html.domimpl.*; 8 import org.lobobrowser.util.gui.WrapperLayout; 9 import org.w3c.dom.Node ; 10 import org.w3c.dom.html2.*; 11 12 class InputSelectControl extends BaseInputControl { 13 private static final OptionFilter OPTION_FILTER = new OptionFilter(); 14 private final JComboBox comboBox; 15 16 public InputSelectControl(HTMLBaseInputElement modelNode) { 17 super(modelNode); 18 this.setLayout(WrapperLayout.getInstance()); 19 JComboBox comboBox = new JComboBox(); 20 this.add(comboBox); 21 this.comboBox = comboBox; 22 } 23 24 public void reset(int availWidth, int availHeight) { 25 super.reset(availWidth, availHeight); 26 HTMLElementImpl selectElement = this.controlElement; 27 ArrayList list = selectElement.getDescendents(OPTION_FILTER); 28 Iterator i = list.iterator(); 29 JComboBox comboBox = this.comboBox; 30 comboBox.removeAllItems(); 31 while(i.hasNext()) { 32 HTMLOptionElement option = (HTMLOptionElement) i.next(); 33 OptionItem item = new OptionItem(option); 34 comboBox.addItem(item); 35 if(option.getDefaultSelected()) { 36 comboBox.setSelectedItem(item); 37 } 38 } 39 } 40 41 public String getValue() { 42 OptionItem item = (OptionItem) this.comboBox.getSelectedItem(); 43 if(item == null) { 44 return null; 45 } 46 else { 47 return item.getValue(); 48 } 49 } 50 51 public boolean getMultiple() { 52 return false; 53 } 54 55 public void setMultiple(boolean value) { 56 if(value) { 57 throw new UnsupportedOperationException (); 59 } 60 } 61 62 public int getSelectedIndex() { 63 return this.comboBox.getSelectedIndex(); 64 } 65 66 public void setSelectedIndex(int value) { 67 this.comboBox.setSelectedIndex(value); 68 } 69 70 public int getVisibleSize() { 71 return this.comboBox.getMaximumRowCount(); 72 } 73 74 public void setVisibleSize(int value) { 75 this.comboBox.setMaximumRowCount(value); 76 } 77 78 public void resetInput() { 79 this.comboBox.setSelectedIndex(-1); 80 } 81 82 private static class OptionFilter implements NodeFilter { 83 public boolean accept(Node node) { 84 return node instanceof HTMLOptionElement; 85 } 86 } 87 88 private static class OptionItem { 89 private final HTMLOptionElement option; 90 private final String caption; 91 92 public OptionItem(HTMLOptionElement option) { 93 this.option = option; 94 String label = option.getLabel(); 95 if(label == null) { 96 this.caption = option.getText(); 97 } 98 else { 99 this.caption = label; 100 } 101 } 102 103 public String toString() { 104 return this.caption; 105 } 106 107 public String getValue() { 108 String value = this.option.getValue(); 109 if(value == null) { 110 value = this.option.getText(); 111 } 112 return value; 113 } 114 } 115 } 116 | Popular Tags |