KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > ui > InputPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * InputPanel.java
21  *
22  * Created on May 14, 2004, 8:03 PM
23  */

24
25 package org.netbeans.core.output2.ui;
26
27 import java.awt.event.FocusEvent JavaDoc;
28 import java.awt.event.FocusListener JavaDoc;
29 import org.netbeans.core.output2.Controller;
30 import org.openide.awt.Mnemonics;
31 import org.openide.util.NbBundle;
32
33 import javax.swing.*;
34 import javax.swing.border.Border JavaDoc;
35 import java.awt.*;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.event.ActionListener JavaDoc;
38 import org.openide.util.Utilities;
39
40 /**
41  * Panel to enable user entered input.
42  *
43  * @author Tim Boudreau
44  */

45 final class InputPanel extends JPanel implements ActionListener JavaDoc, FocusListener JavaDoc {
46     public static final String JavaDoc ACTION_EOF = "eof"; //NOI18N
47
public static final String JavaDoc ACTION_NEWTEXT = "text"; //NOI18N
48

49     private JTextField field = new JTextField();
50     private JButton eof =
51         new JButton(); //NOI18N
52

53     private ActionListener JavaDoc listener = null;
54     private JLabel lbl =
55         new JLabel(); //NOI18N;
56

57     /** Creates a new instance of InputPanel */
58     public InputPanel() {
59         init();
60         setFocusable(false);
61     }
62     
63     public void requestFocus() {
64         field.requestFocus();
65     }
66     
67     public boolean requestFocusInWindow() {
68         return field.requestFocusInWindow();
69     }
70     
71     private void init() {
72         setLayout (new BorderLayout());
73
74         add (lbl, BorderLayout.WEST);
75         add (field, BorderLayout.CENTER);
76         field.setEditable(true);
77         add (eof, BorderLayout.EAST);
78
79         field.addActionListener(this);
80         eof.addActionListener(this);
81
82         eof.setToolTipText(NbBundle.getMessage(InputPanel.class, "TIP_EOF")); //NOI18N
83
field.setToolTipText(NbBundle.getMessage(InputPanel.class, "TIP_INPUT")); //NOI18N
84

85         field.getAccessibleContext().setAccessibleName(lbl.getText());
86         eof.getAccessibleContext().setAccessibleName(eof.getText());
87         field.getAccessibleContext().setAccessibleDescription(field.getToolTipText());
88         eof.getAccessibleContext().setAccessibleDescription(eof.getToolTipText());
89         
90         Mnemonics.setLocalizedText(lbl, NbBundle.getMessage(InputPanel.class, "LBL_INPUT"));
91         Mnemonics.setLocalizedText(eof, NbBundle.getMessage(InputPanel.class, "LBL_EOF"));
92         
93         Border JavaDoc b = field.getBorder();
94         field.setBorder (BorderFactory.createCompoundBorder (
95             BorderFactory.createMatteBorder(3, 3, 3, 3, getBackground()), b));
96         lbl.setBorder (BorderFactory.createEmptyBorder(0,0,0,5));
97
98         lbl.setLabelFor(field);
99         
100         setBorder (BorderFactory.createCompoundBorder (
101                     BorderFactory.createMatteBorder(
102                         1,0,0,0,UIManager.getColor("controlShadow")),
103                 BorderFactory.createEmptyBorder (4,4,4,4)));
104         
105         field.addFocusListener(this);
106     }
107
108     public void doLayout() {
109         Dimension lblp = lbl.getPreferredSize();
110         Dimension eofp = eof.getPreferredSize();
111         Dimension fp = field.getPreferredSize();
112
113         Insets ins = getInsets();
114
115         lbl.setBounds (ins.left, ins.top, lblp.width, getHeight() - (ins.top + ins.bottom));
116
117         int ftop = (getHeight() / 2) - (fp.height / 2);
118
119         int fright = getWidth() - (ins.right + eofp.width);
120         int fleft = ins.left + lblp.width;
121
122         field.setBounds (fleft, ftop, fright - fleft, fp.height);
123
124         int btop = (getHeight() / 2) - (eofp.height / 2);
125         eof.setBounds (fright, btop, getWidth() - (fright + ins.right), eofp.height);
126     }
127
128     public Dimension getPreferredSize() {
129         Dimension lblp = lbl.getPreferredSize();
130         Dimension eofp = eof.getPreferredSize();
131         Dimension fp = field.getPreferredSize();
132
133         Insets ins = getInsets();
134
135         int h = ins.top + ins.bottom + Math.max (Math.max (lblp.height, eofp.height), fp.height);
136         int w = ins.left + lblp.width + eofp.width + fp.width + ins.right;
137
138         return new Dimension (w, h);
139     }
140
141     public Dimension getMinimumSize() {
142         Dimension lblp = lbl.getMinimumSize();
143         Dimension eofp = eof.getMinimumSize();
144         Dimension fp = field.getMinimumSize();
145
146         Insets ins = getInsets();
147
148         int h = ins.top + ins.bottom + Math.max (Math.max (lblp.height, eofp.height), fp.height);
149         int w = ins.left + lblp.width + eofp.width + fp.width + ins.right;
150
151         return new Dimension (w, h);
152     }
153
154
155     public String JavaDoc getText() {
156         return field.getText();
157     }
158     
159     public void addActionListener (ActionListener JavaDoc listener) {
160         if (this.listener != null) {
161             throw new IllegalStateException JavaDoc (this.listener + " is already " + //NOI18N
162
"listening"); //NOI18N
163
}
164         this.listener = listener;
165     }
166     
167     public void removeActionListener (ActionListener JavaDoc listener) {
168         if (listener != this.listener) {
169             throw new IllegalArgumentException JavaDoc (listener + " is not " + //NOI18N
170
this.listener);
171         }
172         this.listener = null;
173     }
174     
175     public void actionPerformed(ActionEvent JavaDoc ae) {
176         ActionEvent JavaDoc e = new ActionEvent JavaDoc (this, ActionEvent.ACTION_PERFORMED,
177             ae.getSource() == eof ? ACTION_EOF : ACTION_NEWTEXT);
178         if (Controller.LOG) Controller.log ("Got action event from " + ae.getSource()); //NOI18N
179
if (Controller.LOG) Controller.log (" Posting event to listener(tab) " + e); //NOI18N
180

181         if (ae.getSource() == field && field.getText().length() > 0) {
182             field.setSelectionStart(0);
183             field.setSelectionEnd(field.getText().length());
184         }
185
186         listener.actionPerformed(e);
187     }
188     
189     private AbstractOutputTab findOutputTab() {
190         if (getParent() != null) {
191             return (AbstractOutputTab) SwingUtilities.getAncestorOfClass(AbstractOutputTab.class, this);
192         } else {
193             return null;
194         }
195     }
196     
197     public void focusGained (FocusEvent JavaDoc fe) {
198         AbstractOutputTab tab = findOutputTab();
199         if (tab != null) {
200             tab.notifyInputFocusGained();
201         }
202     }
203     
204     public void focusLost (FocusEvent JavaDoc fe) {
205         
206     }
207     
208 }
209
Popular Tags