KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > IsindexView


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
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.net.URLEncoder JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import javax.swing.text.*;
16 import javax.swing.*;
17
18
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.11, 12/19/03
25  */

26
27 class IsindexView extends ComponentView implements ActionListener {
28  
29     JTextField textField;
30
31     /**
32      * Creates an IsindexView
33      */

34     public IsindexView(Element elem) {
35     super(elem);
36     }
37
38     /**
39      * Creates the components necessary to to implement
40      * this view. The component returned is a <code>JPanel</code>,
41      * that contains the PROMPT to the left and <code>JTextField</code>
42      * to the right.
43      */

44     public Component createComponent() {
45     AttributeSet attr = getElement().getAttributes();
46     
47     JPanel panel = new JPanel(new BorderLayout());
48     panel.setBackground(null);
49     
50     String JavaDoc prompt = (String JavaDoc)attr.getAttribute(HTML.Attribute.PROMPT);
51     if (prompt == null) {
52             prompt = UIManager.getString("IsindexView.prompt");
53     }
54     JLabel label = new JLabel(prompt);
55
56     textField = new JTextField();
57     textField.addActionListener(this);
58     panel.add(label, BorderLayout.WEST);
59     panel.add(textField, BorderLayout.CENTER);
60     panel.setAlignmentY(1.0f);
61     panel.setOpaque(false);
62     return panel;
63     }
64
65     /**
66      * Responsible for processing the ActionEvent.
67      * In this case this is hitting enter/return
68      * in the text field. This will construct the
69      * URL from the base URL of the document.
70      * To the URL is appended a '?' followed by the
71      * contents of the JTextField. The search
72      * contents are URLEncoded.
73      */

74     public void actionPerformed(ActionEvent evt) {
75
76     String JavaDoc data = textField.getText();
77     if (data != null) {
78         data = URLEncoder.encode(data);
79     }
80
81
82     AttributeSet attr = getElement().getAttributes();
83     HTMLDocument JavaDoc hdoc = (HTMLDocument JavaDoc)getElement().getDocument();
84
85     String JavaDoc action = (String JavaDoc) attr.getAttribute(HTML.Attribute.ACTION);
86     if (action == null) {
87         action = hdoc.getBase().toString();
88     }
89     try {
90         URL JavaDoc url = new URL JavaDoc(action+"?"+data);
91         JEditorPane pane = (JEditorPane)getContainer();
92         pane.setPage(url);
93     } catch (MalformedURLException JavaDoc e1) {
94     } catch (IOException JavaDoc e2) {
95     }
96     }
97 }
98
Popular Tags