1. /*
  2. * @(#)IsindexView.java 1.11 03/12/19
  3. *
  4. * Copyright 2004 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.11, 12/19/03
  22. */
  23. class IsindexView extends ComponentView implements ActionListener {
  24. JTextField textField;
  25. /**
  26. * Creates an IsindexView
  27. */
  28. public IsindexView(Element elem) {
  29. super(elem);
  30. }
  31. /**
  32. * Creates the components necessary to to implement
  33. * this view. The component returned is a <code>JPanel</code>,
  34. * that contains the PROMPT to the left and <code>JTextField</code>
  35. * to the right.
  36. */
  37. public Component createComponent() {
  38. AttributeSet attr = getElement().getAttributes();
  39. JPanel panel = new JPanel(new BorderLayout());
  40. panel.setBackground(null);
  41. String prompt = (String)attr.getAttribute(HTML.Attribute.PROMPT);
  42. if (prompt == null) {
  43. prompt = UIManager.getString("IsindexView.prompt");
  44. }
  45. JLabel label = new JLabel(prompt);
  46. textField = new JTextField();
  47. textField.addActionListener(this);
  48. panel.add(label, BorderLayout.WEST);
  49. panel.add(textField, BorderLayout.CENTER);
  50. panel.setAlignmentY(1.0f);
  51. panel.setOpaque(false);
  52. return panel;
  53. }
  54. /**
  55. * Responsible for processing the ActionEvent.
  56. * In this case this is hitting enter/return
  57. * in the text field. This will construct the
  58. * URL from the base URL of the document.
  59. * To the URL is appended a '?' followed by the
  60. * contents of the JTextField. The search
  61. * contents are URLEncoded.
  62. */
  63. public void actionPerformed(ActionEvent evt) {
  64. String data = textField.getText();
  65. if (data != null) {
  66. data = URLEncoder.encode(data);
  67. }
  68. AttributeSet attr = getElement().getAttributes();
  69. HTMLDocument hdoc = (HTMLDocument)getElement().getDocument();
  70. String action = (String) attr.getAttribute(HTML.Attribute.ACTION);
  71. if (action == null) {
  72. action = hdoc.getBase().toString();
  73. }
  74. try {
  75. URL url = new URL(action+"?"+data);
  76. JEditorPane pane = (JEditorPane)getContainer();
  77. pane.setPage(url);
  78. } catch (MalformedURLException e1) {
  79. } catch (IOException e2) {
  80. }
  81. }
  82. }