1 /*
   2  * Copyright (c) 1998, 2000, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.text.html;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.net.URLEncoder;
  30 import java.net.MalformedURLException;
  31 import java.io.IOException;
  32 import java.net.URL;
  33 import javax.swing.text.*;
  34 import javax.swing.*;
  35 
  36 
  37 /**
  38  * A view that supports the <ISINDEX< tag.  This is implemented
  39  * as a JPanel that contains
  40  *
  41  * @author Sunita Mani
  42  */
  43 
  44 class IsindexView extends ComponentView implements ActionListener {
  45 
  46     JTextField textField;
  47 
  48     /**
  49      * Creates an IsindexView
  50      */
  51     public IsindexView(Element elem) {
  52         super(elem);
  53     }
  54 
  55     /**
  56      * Creates the components necessary to to implement
  57      * this view.  The component returned is a <code>JPanel</code>,
  58      * that contains the PROMPT to the left and <code>JTextField</code>
  59      * to the right.
  60      */
  61     public Component createComponent() {
  62         AttributeSet attr = getElement().getAttributes();
  63 
  64         JPanel panel = new JPanel(new BorderLayout());
  65         panel.setBackground(null);
  66 
  67         String prompt = (String)attr.getAttribute(HTML.Attribute.PROMPT);
  68         if (prompt == null) {
  69             prompt = UIManager.getString("IsindexView.prompt");
  70         }
  71         JLabel label = new JLabel(prompt);
  72 
  73         textField = new JTextField();
  74         textField.addActionListener(this);
  75         panel.add(label, BorderLayout.WEST);
  76         panel.add(textField, BorderLayout.CENTER);
  77         panel.setAlignmentY(1.0f);
  78         panel.setOpaque(false);
  79         return panel;
  80     }
  81 
  82     /**
  83      * Responsible for processing the ActionEvent.
  84      * In this case this is hitting enter/return
  85      * in the text field.  This will construct the
  86      * URL from the base URL of the document.
  87      * To the URL is appended a '?' followed by the
  88      * contents of the JTextField.  The search
  89      * contents are URLEncoded.
  90      */
  91     public void actionPerformed(ActionEvent evt) {
  92 
  93         String data = textField.getText();
  94         if (data != null) {
  95             data = URLEncoder.encode(data);
  96         }
  97 
  98 
  99         AttributeSet attr = getElement().getAttributes();
 100         HTMLDocument hdoc = (HTMLDocument)getElement().getDocument();
 101 
 102         String action = (String) attr.getAttribute(HTML.Attribute.ACTION);
 103         if (action == null) {
 104             action = hdoc.getBase().toString();
 105         }
 106         try {
 107             URL url = new URL(action+"?"+data);
 108             JEditorPane pane = (JEditorPane)getContainer();
 109             pane.setPage(url);
 110         } catch (MalformedURLException e1) {
 111         } catch (IOException e2) {
 112         }
 113     }
 114 }