1 7 package javax.swing.text.html; 8 9 import java.net.*; 10 import java.io.*; 11 import java.awt.*; 12 import java.awt.event.*; 13 import java.util.*; 14 import javax.swing.*; 15 import javax.swing.event.*; 16 import javax.swing.text.*; 17 18 90 public class FormView extends ComponentView implements ActionListener { 91 92 99 @Deprecated 100 public static final String SUBMIT = new String ("Submit Query"); 101 108 @Deprecated 109 public static final String RESET = new String ("Reset"); 110 111 117 private short maxIsPreferred; 118 119 124 public FormView(Element elem) { 125 super(elem); 126 } 127 128 133 protected Component createComponent() { 134 AttributeSet attr = getElement().getAttributes(); 135 HTML.Tag t = (HTML.Tag ) 136 attr.getAttribute(StyleConstants.NameAttribute); 137 JComponent c = null; 138 Object model = attr.getAttribute(StyleConstants.ModelAttribute); 139 if (t == HTML.Tag.INPUT) { 140 c = createInputComponent(attr, model); 141 } else if (t == HTML.Tag.SELECT) { 142 143 if (model instanceof OptionListModel ) { 144 145 JList list = new JList((ListModel) model); 146 int size = HTML.getIntegerAttributeValue(attr, 147 HTML.Attribute.SIZE, 148 1); 149 list.setVisibleRowCount(size); 150 list.setSelectionModel((ListSelectionModel)model); 151 c = new JScrollPane(list); 152 } else { 153 c = new JComboBox((ComboBoxModel) model); 154 maxIsPreferred = 3; 155 } 156 } else if (t == HTML.Tag.TEXTAREA) { 157 JTextArea area = new JTextArea((Document) model); 158 int rows = HTML.getIntegerAttributeValue(attr, 159 HTML.Attribute.ROWS, 160 1); 161 area.setRows(rows); 162 int cols = HTML.getIntegerAttributeValue(attr, 163 HTML.Attribute.COLS, 164 20); 165 maxIsPreferred = 3; 166 area.setColumns(cols); 167 c = new JScrollPane(area, 168 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 169 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 170 } 171 172 if (c != null) { 173 c.setAlignmentY(1.0f); 174 } 175 return c; 176 } 177 178 179 187 private JComponent createInputComponent(AttributeSet attr, Object model) { 188 JComponent c = null; 189 String type = (String ) attr.getAttribute(HTML.Attribute.TYPE); 190 191 if (type.equals("submit") || type.equals("reset")) { 192 String value = (String ) 193 attr.getAttribute(HTML.Attribute.VALUE); 194 if (value == null) { 195 if (type.equals("submit")) { 196 value = UIManager.getString("FormView.submitButtonText"); 197 } else { 198 value = UIManager.getString("FormView.resetButtonText"); 199 } 200 } 201 JButton button = new JButton(value); 202 if (model != null) { 203 button.setModel((ButtonModel)model); 204 button.addActionListener(this); 205 } 206 c = button; 207 maxIsPreferred = 3; 208 } else if (type.equals("image")) { 209 String srcAtt = (String ) attr.getAttribute(HTML.Attribute.SRC); 210 JButton button; 211 try { 212 URL base = ((HTMLDocument )getElement().getDocument()).getBase(); 213 URL srcURL = new URL(base, srcAtt); 214 Icon icon = new ImageIcon(srcURL); 215 button = new JButton(icon); 216 } catch (MalformedURLException e) { 217 button = new JButton(srcAtt); 218 } 219 if (model != null) { 220 button.setModel((ButtonModel)model); 221 button.addMouseListener(new MouseEventListener()); 222 } 223 c = button; 224 maxIsPreferred = 3; 225 } else if (type.equals("checkbox")) { 226 c = new JCheckBox(); 227 if (model != null) { 228 ((JCheckBox)c).setModel((JToggleButton.ToggleButtonModel) model); 229 } 230 maxIsPreferred = 3; 231 } else if (type.equals("radio")) { 232 c = new JRadioButton(); 233 if (model != null) { 234 ((JRadioButton)c).setModel((JToggleButton.ToggleButtonModel)model); 235 } 236 maxIsPreferred = 3; 237 } else if (type.equals("text")) { 238 int size = HTML.getIntegerAttributeValue(attr, 239 HTML.Attribute.SIZE, 240 -1); 241 JTextField field; 242 if (size > 0) { 243 field = new JTextField(); 244 field.setColumns(size); 245 } 246 else { 247 field = new JTextField(); 248 field.setColumns(20); 249 } 250 c = field; 251 if (model != null) { 252 field.setDocument((Document) model); 253 } 254 field.addActionListener(this); 255 maxIsPreferred = 3; 256 } else if (type.equals("password")) { 257 JPasswordField field = new JPasswordField(); 258 c = field; 259 if (model != null) { 260 field.setDocument((Document) model); 261 } 262 int size = HTML.getIntegerAttributeValue(attr, 263 HTML.Attribute.SIZE, 264 -1); 265 field.setColumns((size > 0) ? size : 20); 266 field.addActionListener(this); 267 maxIsPreferred = 3; 268 } else if (type.equals("file")) { 269 JTextField field = new JTextField(); 270 if (model != null) { 271 field.setDocument((Document)model); 272 } 273 int size = HTML.getIntegerAttributeValue(attr, HTML.Attribute.SIZE, 274 -1); 275 field.setColumns((size > 0) ? size : 20); 276 JButton browseButton = new JButton(UIManager.getString 277 ("FormView.browseFileButtonText")); 278 Box box = Box.createHorizontalBox(); 279 box.add(field); 280 box.add(Box.createHorizontalStrut(5)); 281 box.add(browseButton); 282 browseButton.addActionListener(new BrowseFileAction( 283 attr, (Document)model)); 284 c = box; 285 maxIsPreferred = 3; 286 } 287 return c; 288 } 289 290 291 305 public float getMaximumSpan(int axis) { 306 switch (axis) { 307 case View.X_AXIS: 308 if ((maxIsPreferred & 1) == 1) { 309 super.getMaximumSpan(axis); 310 return getPreferredSpan(axis); 311 } 312 return super.getMaximumSpan(axis); 313 case View.Y_AXIS: 314 if ((maxIsPreferred & 2) == 2) { 315 super.getMaximumSpan(axis); 316 return getPreferredSpan(axis); 317 } 318 return super.getMaximumSpan(axis); 319 default: 320 break; 321 } 322 return super.getMaximumSpan(axis); 323 } 324 325 326 340 public void actionPerformed(ActionEvent evt) { 341 Element element = getElement(); 342 StringBuffer dataBuffer = new StringBuffer (); 343 HTMLDocument doc = (HTMLDocument )getDocument(); 344 AttributeSet attr = element.getAttributes(); 345 346 String type = (String ) attr.getAttribute(HTML.Attribute.TYPE); 347 348 if (type.equals("submit")) { 349 getFormData(dataBuffer); 350 submitData(dataBuffer.toString()); 351 } else if (type.equals("reset")) { 352 resetForm(); 353 } else if (type.equals("text") || type.equals("password")) { 354 if (isLastTextOrPasswordField()) { 355 getFormData(dataBuffer); 356 submitData(dataBuffer.toString()); 357 } else { 358 getComponent().transferFocus(); 359 } 360 } 361 } 362 363 364 368 protected void submitData(String data) { 369 SubmitThread dataThread = new SubmitThread(getElement(), data); 371 dataThread.start(); 372 } 373 374 375 382 class SubmitThread extends Thread { 383 384 String data; 385 HTMLDocument hdoc; 386 AttributeSet formAttr; 387 URL url; 388 String method; 389 String target; 390 URL actionURL; 391 392 public SubmitThread(Element elem, String data) { 393 this.data = data; 394 hdoc = (HTMLDocument )elem.getDocument(); 395 Element formE = getFormElement(); 396 if (formE != null) { 397 formAttr = formE.getAttributes(); 398 } 399 400 method = getMethod(); 401 402 try { 403 404 String action = getAction(); 405 method = getMethod(); 406 target = getTarget(); 407 408 410 URL baseURL = hdoc.getBase(); 411 if (action == null) { 412 413 String file = baseURL.getFile(); 414 actionURL = new URL(baseURL.getProtocol(), 415 baseURL.getHost(), 416 baseURL.getPort(), 417 file); 418 } else { 419 actionURL = new URL(baseURL, action); 420 } 421 } catch (MalformedURLException m) { 422 actionURL = null; 423 } 424 } 425 426 427 438 public void run() { 439 440 if (data.length() > 0) { 441 442 try { 443 444 URLConnection connection; 445 446 JEditorPane c = (JEditorPane)getContainer(); 448 HTMLEditorKit kit = (HTMLEditorKit )c.getEditorKit(); 449 if (kit.isAutoFormSubmission()) { 450 if ("post".equals(method)) { 451 url = actionURL; 452 connection = url.openConnection(); 453 postData(connection, data); 454 } else { 455 456 url = new URL(actionURL+"?"+data); 457 } 458 459 Runnable callLoadDocument = new Runnable () { 460 public void run() { 461 JEditorPane c = (JEditorPane)getContainer(); 462 if (hdoc.isFrameDocument()) { 463 c.fireHyperlinkUpdate(createFormSubmitEvent()); 464 } else { 465 try { 466 c.setPage(url); 467 } catch (IOException e) { 468 } 469 } 470 } 471 }; 472 SwingUtilities.invokeLater(callLoadDocument); 473 } else { 474 c.fireHyperlinkUpdate(createFormSubmitEvent()); 475 } 476 } catch (MalformedURLException m) { 477 } catch (IOException e) { 479 } 481 } 482 } 483 484 487 private FormSubmitEvent createFormSubmitEvent() { 488 FormSubmitEvent.MethodType formMethod = 489 "post".equals(method) ? FormSubmitEvent.MethodType.POST : 490 FormSubmitEvent.MethodType.GET; 491 return new FormSubmitEvent (FormView.this, 492 HyperlinkEvent.EventType.ACTIVATED, 493 actionURL, 494 getElement(), 495 target, 496 formMethod, 497 data); 498 } 499 500 503 private String getTarget() { 504 if (formAttr != null) { 505 String target = (String )formAttr.getAttribute(HTML.Attribute.TARGET); 506 if (target != null) { 507 return target.toLowerCase(); 508 } 509 } 510 return "_self"; 511 } 512 513 516 public String getAction() { 517 if (formAttr == null) { 518 return null; 519 } 520 return (String )formAttr.getAttribute(HTML.Attribute.ACTION); 521 } 522 523 526 String getMethod() { 527 if (formAttr != null) { 528 String method = (String )formAttr.getAttribute(HTML.Attribute.METHOD); 529 if (method != null) { 530 return method.toLowerCase(); 531 } 532 } 533 return null; 534 } 535 536 537 544 public void postData(URLConnection connection, String data) { 545 connection.setDoOutput(true); 546 PrintWriter out = null; 547 try { 548 out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream())); 549 out.print(data); 550 out.flush(); 551 } catch (IOException e) { 552 } finally { 554 if (out != null) { 555 out.close(); 556 } 557 } 558 } 559 } 560 561 568 protected class MouseEventListener extends MouseAdapter { 569 570 public void mouseReleased(MouseEvent evt) { 571 String imageData = getImageData(evt.getPoint()); 572 imageSubmit(imageData); 573 } 574 } 575 576 583 protected void imageSubmit(String imageData) { 584 585 StringBuffer dataBuffer = new StringBuffer (); 586 Element elem = getElement(); 587 HTMLDocument hdoc = (HTMLDocument )elem.getDocument(); 588 getFormData(dataBuffer); 589 if (dataBuffer.length() > 0) { 590 dataBuffer.append('&'); 591 } 592 dataBuffer.append(imageData); 593 submitData(dataBuffer.toString()); 594 return; 595 } 596 597 610 private String getImageData(Point point) { 611 612 String mouseCoords = point.x + ":" + point.y; 613 int sep = mouseCoords.indexOf(':'); 614 String x = mouseCoords.substring(0, sep); 615 String y = mouseCoords.substring(++sep); 616 String name = (String ) getElement().getAttributes().getAttribute(HTML.Attribute.NAME); 617 618 String data; 619 if (name == null || name.equals("")) { 620 data = "x="+ x +"&y="+ y; 621 } else { 622 name = URLEncoder.encode(name); 623 data = name + ".x" +"="+ x +"&"+ name +".y"+"="+ y; 624 } 625 return data; 626 } 627 628 629 637 638 639 642 private Element getFormElement() { 643 Element elem = getElement(); 644 while (elem != null) { 645 if (elem.getAttributes().getAttribute 646 (StyleConstants.NameAttribute) == HTML.Tag.FORM) { 647 return elem; 648 } 649 elem = elem.getParentElement(); 650 } 651 return null; 652 } 653 654 666 void getFormData(StringBuffer buffer) { 667 Element formE = getFormElement(); 668 if (formE != null) { 669 ElementIterator it = new ElementIterator(formE); 670 Element next; 671 672 while ((next = it.next()) != null) { 673 if (isControl(next)) { 674 String type = (String )next.getAttributes().getAttribute 675 (HTML.Attribute.TYPE); 676 677 if (type != null && type.equals("submit") && 678 next != getElement()) { 679 } else if (type == null || !type.equals("image")) { 681 loadElementDataIntoBuffer(next, buffer); 686 } 687 } 688 } 689 } 690 } 691 692 700 private void loadElementDataIntoBuffer(Element elem, StringBuffer buffer) { 701 702 AttributeSet attr = elem.getAttributes(); 703 String name = (String )attr.getAttribute(HTML.Attribute.NAME); 704 if (name == null) { 705 return; 706 } 707 String value = null; 708 HTML.Tag tag = (HTML.Tag )elem.getAttributes().getAttribute 709 (StyleConstants.NameAttribute); 710 711 if (tag == HTML.Tag.INPUT) { 712 value = getInputElementData(attr); 713 } else if (tag == HTML.Tag.TEXTAREA) { 714 value = getTextAreaData(attr); 715 } else if (tag == HTML.Tag.SELECT) { 716 loadSelectData(attr, buffer); 717 } 718 719 if (name != null && value != null) { 720 appendBuffer(buffer, name, value); 721 } 722 } 723 724 725 732 private String getInputElementData(AttributeSet attr) { 733 734 Object model = attr.getAttribute(StyleConstants.ModelAttribute); 735 String type = (String ) attr.getAttribute(HTML.Attribute.TYPE); 736 String value = null; 737 738 if (type.equals("text") || type.equals("password")) { 739 Document doc = (Document)model; 740 try { 741 value = doc.getText(0, doc.getLength()); 742 } catch (BadLocationException e) { 743 value = null; 744 } 745 } else if (type.equals("submit") || type.equals("hidden")) { 746 value = (String ) attr.getAttribute(HTML.Attribute.VALUE); 747 if (value == null) { 748 value = ""; 749 } 750 } else if (type.equals("radio") || type.equals("checkbox")) { 751 ButtonModel m = (ButtonModel)model; 752 if (m.isSelected()) { 753 value = (String ) attr.getAttribute(HTML.Attribute.VALUE); 754 if (value == null) { 755 value = "on"; 756 } 757 } 758 } else if (type.equals("file")) { 759 Document doc = (Document)model; 760 String path; 761 762 try { 763 path = doc.getText(0, doc.getLength()); 764 } catch (BadLocationException e) { 765 path = null; 766 } 767 if (path != null && path.length() > 0) { 768 value = path; 769 792 } 793 } 794 return value; 795 } 796 797 802 private String getTextAreaData(AttributeSet attr) { 803 Document doc = (Document)attr.getAttribute(StyleConstants.ModelAttribute); 804 try { 805 return doc.getText(0, doc.getLength()); 806 } catch (BadLocationException e) { 807 return null; 808 } 809 } 810 811 812 817 private void loadSelectData(AttributeSet attr, StringBuffer buffer) { 818 819 String name = (String )attr.getAttribute(HTML.Attribute.NAME); 820 if (name == null) { 821 return; 822 } 823 Object m = attr.getAttribute(StyleConstants.ModelAttribute); 824 if (m instanceof OptionListModel ) { 825 OptionListModel model = (OptionListModel )m; 826 827 for (int i = 0; i < model.getSize(); i++) { 828 if (model.isSelectedIndex(i)) { 829 Option option = (Option ) model.getElementAt(i); 830 appendBuffer(buffer, name, option.getValue()); 831 } 832 } 833 } else if (m instanceof ComboBoxModel) { 834 ComboBoxModel model = (ComboBoxModel)m; 835 Option option = (Option )model.getSelectedItem(); 836 if (option != null) { 837 appendBuffer(buffer, name, option.getValue()); 838 } 839 } 840 } 841 842 848 private void appendBuffer(StringBuffer buffer, String name, String value) { 849 if (buffer.length() > 0) { 850 buffer.append('&'); 851 } 852 String encodedName = URLEncoder.encode(name); 853 buffer.append(encodedName); 854 buffer.append('='); 855 String encodedValue = URLEncoder.encode(value); 856 buffer.append(encodedValue); 857 } 858 859 862 private boolean isControl(Element elem) { 863 return elem.isLeaf(); 864 } 865 866 872 boolean isLastTextOrPasswordField() { 873 Element parent = getFormElement(); 874 Element elem = getElement(); 875 876 if (parent != null) { 877 ElementIterator it = new ElementIterator(parent); 878 Element next; 879 boolean found = false; 880 881 while ((next = it.next()) != null) { 882 if (next == elem) { 883 found = true; 884 } 885 else if (found && isControl(next)) { 886 AttributeSet elemAttr = next.getAttributes(); 887 888 if (HTMLDocument.matchNameAttribute 889 (elemAttr, HTML.Tag.INPUT)) { 890 String type = (String )elemAttr.getAttribute 891 (HTML.Attribute.TYPE); 892 893 if ("text".equals(type) || "password".equals(type)) { 894 return false; 895 } 896 } 897 } 898 } 899 } 900 return true; 901 } 902 903 911 void resetForm() { 912 Element parent = getFormElement(); 913 914 if (parent != null) { 915 ElementIterator it = new ElementIterator(parent); 916 Element next; 917 918 while((next = it.next()) != null) { 919 if (isControl(next)) { 920 AttributeSet elemAttr = next.getAttributes(); 921 Object m = elemAttr.getAttribute(StyleConstants. 922 ModelAttribute); 923 if (m instanceof TextAreaDocument ) { 924 TextAreaDocument doc = (TextAreaDocument )m; 925 doc.reset(); 926 } else if (m instanceof PlainDocument) { 927 try { 928 PlainDocument doc = (PlainDocument)m; 929 doc.remove(0, doc.getLength()); 930 if (HTMLDocument.matchNameAttribute 931 (elemAttr, HTML.Tag.INPUT)) { 932 String value = (String )elemAttr. 933 getAttribute(HTML.Attribute.VALUE); 934 if (value != null) { 935 doc.insertString(0, value, null); 936 } 937 } 938 } catch (BadLocationException e) { 939 } 940 } else if (m instanceof OptionListModel ) { 941 OptionListModel model = (OptionListModel ) m; 942 int size = model.getSize(); 943 for (int i = 0; i < size; i++) { 944 model.removeIndexInterval(i, i); 945 } 946 BitSet selectionRange = model.getInitialSelection(); 947 for (int i = 0; i < selectionRange.size(); i++) { 948 if (selectionRange.get(i)) { 949 model.addSelectionInterval(i, i); 950 } 951 } 952 } else if (m instanceof OptionComboBoxModel ) { 953 OptionComboBoxModel model = (OptionComboBoxModel ) m; 954 Option option = model.getInitialSelection(); 955 if (option != null) { 956 model.setSelectedItem(option); 957 } 958 } else if (m instanceof JToggleButton.ToggleButtonModel) { 959 boolean checked = ((String )elemAttr.getAttribute 960 (HTML.Attribute.CHECKED) != null); 961 JToggleButton.ToggleButtonModel model = 962 (JToggleButton.ToggleButtonModel)m; 963 model.setSelected(checked); 964 } 965 } 966 } 967 } 968 } 969 970 971 977 private class BrowseFileAction implements ActionListener { 978 private AttributeSet attrs; 979 private Document model; 980 981 BrowseFileAction(AttributeSet attrs, Document model) { 982 this.attrs = attrs; 983 this.model = model; 984 } 985 986 public void actionPerformed(ActionEvent ae) { 987 JFileChooser fc = new JFileChooser(); 990 fc.setMultiSelectionEnabled(false); 991 if (fc.showOpenDialog(getContainer()) == 992 JFileChooser.APPROVE_OPTION) { 993 File selected = fc.getSelectedFile(); 994 995 if (selected != null) { 996 try { 997 if (model.getLength() > 0) { 998 model.remove(0, model.getLength()); 999 } 1000 model.insertString(0, selected.getPath(), null); 1001 } catch (BadLocationException ble) {} 1002 } 1003 } 1004 } 1005 } 1006} 1007 | Popular Tags |