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