1. /*
  2. * @(#)IsindexView.java 1.6 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.text.html;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import java.net.URLEncoder;
  14. import java.net.MalformedURLException;
  15. import java.io.IOException;
  16. import java.net.URL;
  17. import javax.swing.text.*;
  18. import javax.swing.*;
  19. /**
  20. * A view that supports the <ISINDEX< tag. This is implemented
  21. * as a JPanel that contains
  22. *
  23. * @author Sunita Mani
  24. * @version 1.6, 02/02/00
  25. */
  26. class IsindexView extends ComponentView implements ActionListener {
  27. public static final String DEFAULT_PROMPT = "This is a searchable index. Enter search keywords:";
  28. JTextField textField;
  29. /**
  30. * Creates an IsindexView
  31. */
  32. public IsindexView(Element elem) {
  33. super(elem);
  34. }
  35. /**
  36. * Creates the components necessary to to implement
  37. * this view. THe component returned is a JPanel,
  38. * that contains the PROMPT to the left and JTextField
  39. * to the right.
  40. */
  41. public Component createComponent() {
  42. AttributeSet attr = getElement().getAttributes();
  43. JPanel panel = new JPanel(new BorderLayout());
  44. panel.setBackground(null);
  45. String prompt = (String)attr.getAttribute(HTML.Attribute.PROMPT);
  46. if (prompt == null) {
  47. prompt = DEFAULT_PROMPT;
  48. }
  49. JLabel label = new JLabel(prompt);
  50. textField = new JTextField();
  51. textField.addActionListener(this);
  52. panel.add(label, BorderLayout.WEST);
  53. panel.add(textField, BorderLayout.CENTER);
  54. panel.setAlignmentY(1.0f);
  55. panel.setOpaque(false);
  56. return panel;
  57. }
  58. /**
  59. * Responsible for processing the ActionEvent.
  60. * In this case this is hitting enter/return
  61. * in the text field. This will construct the
  62. * URL from the base URL of the document.
  63. * To the URL is appended a '?' followed by the
  64. * contents of the JTextField. The search
  65. * contents are URLEncoded.
  66. */
  67. public void actionPerformed(ActionEvent evt) {
  68. String data = textField.getText();
  69. if (data != null) {
  70. data = URLEncoder.encode(data);
  71. }
  72. AttributeSet attr = getElement().getAttributes();
  73. HTMLDocument hdoc = (HTMLDocument)getElement().getDocument();
  74. String action = (String) attr.getAttribute(HTML.Attribute.ACTION);
  75. if (action == null) {
  76. action = hdoc.getBase().toString();
  77. }
  78. try {
  79. URL url = new URL(action+"?"+data);
  80. JEditorPane pane = (JEditorPane)getContainer();
  81. pane.setPage(url);
  82. } catch (MalformedURLException e1) {
  83. } catch (IOException e2) {
  84. }
  85. }
  86. }