KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > FindDialogPanel


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 package org.netbeans.core.output2;
21
22 import org.openide.DialogDescriptor;
23 import org.openide.util.NbBundle;
24
25 import javax.swing.*;
26 import java.awt.*;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.KeyAdapter JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.awt.event.WindowAdapter JavaDoc;
31 import java.lang.ref.Reference JavaDoc;
32 import java.lang.ref.SoftReference JavaDoc;
33 import java.lang.ref.WeakReference JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.Vector JavaDoc;
36 import org.openide.awt.Mnemonics;
37
38 class FindDialogPanel extends javax.swing.JPanel JavaDoc implements Runnable JavaDoc {
39
40     static final long serialVersionUID =5048678953767663114L;
41
42     private static Reference JavaDoc panel = null;
43     private JButton acceptButton;
44     private static Vector JavaDoc history = new Vector JavaDoc();
45     
46     /** Initializes the Form */
47     FindDialogPanel() {
48         initComponents ();
49         Mnemonics.setLocalizedText(findWhatLabel, NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_What"));
50         getAccessibleContext().setAccessibleName(NbBundle.getBundle(FindDialogPanel.class).getString("ACSN_Find"));
51         getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FindDialogPanel.class).getString("ACSD_Find"));
52         findWhat.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FindDialogPanel.class).getString("ACSD_Find_What"));
53         findWhat.getEditor().getEditorComponent().addKeyListener( new KeyAdapter JavaDoc() {
54             public void keyPressed(KeyEvent JavaDoc evt) {
55                 if ( evt.getKeyCode() == KeyEvent.VK_ESCAPE ) {
56                     findWhat.setPopupVisible( false );
57                     ((Dialog)dialogRef.get()).setVisible( false );
58                 }
59             }
60         });
61         findWhat.getEditor().addActionListener( new java.awt.event.ActionListener JavaDoc() {
62                public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
63                    //Give the component a chance to update its text field, or on
64
//first invocation the text will be null
65
SwingUtilities.invokeLater (FindDialogPanel.this);
66                }
67            });
68
69        acceptButton = new JButton();
70        Mnemonics.setLocalizedText(acceptButton, NbBundle.getBundle(FindDialogPanel.class).getString("BTN_Find"));
71        acceptButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FindDialogPanel.class).getString("ACSD_FindBTN"));
72
73        findWhat.setModel( new DefaultComboBoxModel( history ) );
74        findWhatLabel.setFocusable(false);
75
76        JComponent[] order = new JComponent[] {
77            findWhat, acceptButton
78        };
79
80     }
81
82     public void run() {
83         acceptButton.doClick();
84     }
85
86     public static FindDialogPanel getPanel() {
87         FindDialogPanel result = null;
88         if (panel != null) {
89             result = (FindDialogPanel) panel.get();
90         }
91         if (result == null) {
92             result = new FindDialogPanel();
93             panel = new SoftReference JavaDoc (result);
94         }
95         return result;
96     }
97     
98     void setFindText(String JavaDoc text) {
99         int end = text.indexOf("\n");
100         String JavaDoc txt = text;
101         if (end > -1) {
102             txt = text.substring(0, end);
103         }
104         if (!txt.equals(findWhat.getSelectedItem())) {
105             findWhat.insertItemAt(txt, 0);
106             findWhat.setSelectedIndex(0);
107         }
108         selectText();
109     
110     }
111     
112     private void selectText() {
113         Component comp = findWhat.getEditor().getEditorComponent();
114         if (comp instanceof JTextField) {
115             JTextField fld = (JTextField)comp;
116             fld.setSelectionStart(0);
117             fld.setSelectionEnd(fld.getText().length());
118         }
119     }
120     
121     
122     /** This method is called from within the constructor to
123      * initialize the form.
124      * WARNING: Do NOT modify this code. The content of this method is
125      * always regenerated by the FormEditor.
126      */

127     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
128
private void initComponents() {
129         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
130
131         findWhatLabel = new javax.swing.JLabel JavaDoc();
132         findWhat = new javax.swing.JComboBox JavaDoc();
133
134         setLayout(new java.awt.GridBagLayout JavaDoc());
135
136         findWhatLabel.setLabelFor(findWhat);
137         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
138         gridBagConstraints.gridx = 0;
139         gridBagConstraints.gridy = 1;
140         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
141         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 5, 5);
142         add(findWhatLabel, gridBagConstraints);
143
144         findWhat.setEditable(true);
145         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
146         gridBagConstraints.gridx = 1;
147         gridBagConstraints.gridy = 1;
148         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
149         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
150         gridBagConstraints.ipadx = 50;
151         gridBagConstraints.weightx = 1.0;
152         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 5, 12);
153         add(findWhat, gridBagConstraints);
154     }// </editor-fold>//GEN-END:initComponents
155

156
157     // Variables declaration - do not modify//GEN-BEGIN:variables
158
protected javax.swing.JComboBox JavaDoc findWhat;
159     protected javax.swing.JLabel JavaDoc findWhatLabel;
160     // End of variables declaration//GEN-END:variables
161

162
163
164     static void showFindDialog(ActionListener JavaDoc al, String JavaDoc selection) {
165         java.awt.Dialog JavaDoc dialog = getDialog();
166         FindDialogPanel findPanel = getPanel();
167         findPanel.acceptButton.putClientProperty ("panel", findPanel);
168     if (selection != null) {
169         findPanel.setFindText(selection);
170     }
171         if (!Arrays.asList(findPanel.acceptButton.getActionListeners()).contains(al)) {
172             findPanel.acceptButton.addActionListener(al);
173         }
174         dialog.setVisible(true);
175         
176         // always select the text.
177
findPanel.selectText();
178
179
180         dialog.addWindowListener (new DlgWindowListener(al, findPanel.acceptButton));
181     }
182     
183     private static class DlgWindowListener extends WindowAdapter JavaDoc {
184         private ActionListener JavaDoc al;
185         private JButton acceptButton;
186         DlgWindowListener (ActionListener JavaDoc al, JButton acceptButton) {
187             this.al = al;
188             this.acceptButton = acceptButton;
189         }
190         
191         public void windowClosed(java.awt.event.WindowEvent JavaDoc windowEvent) {
192             acceptButton.removeActionListener (al);
193             ((Dialog) windowEvent.getSource()).removeWindowListener(this);
194             ((Dialog) windowEvent.getSource()).dispose();
195         }
196     }
197
198     String JavaDoc getPattern() {
199         FindDialogPanel findPanel = (FindDialogPanel) panel.get();
200         return findPanel != null ? (String JavaDoc) findPanel.findWhat.getSelectedItem() : null;
201     }
202     
203     private void updateHistory() {
204         Object JavaDoc pattern = findWhat.getEditor().getItem();
205
206         history.add( 0, pattern );
207         for ( int i = history.size() - 1; i > 0; i-- ) {
208             if ( history.get( i ).equals( pattern ) ) {
209                 history.remove( i );
210                 break;
211             }
212         }
213     }
214
215     private static Dialog createDialog() {
216         final FindDialogPanel findPanel = getPanel();
217
218         DialogDescriptor dialogDescriptor = new DialogDescriptor(
219                                findPanel,
220                                NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_Title"),
221                                true, // Modal
222
new Object JavaDoc[] { findPanel.acceptButton, DialogDescriptor.CANCEL_OPTION }, // Option lineStartList
223
findPanel.acceptButton, // Default
224
DialogDescriptor.RIGHT_ALIGN, // Align
225
null, // Help
226
new java.awt.event.ActionListener JavaDoc() {
227                                    public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
228                                        if ( evt.getSource() == findPanel.acceptButton ) {
229                                            getPanel().updateHistory();
230                                        }
231                                        else
232
233                                        getDialog().setVisible( false );
234                                    }
235                                });
236         Dialog dialog = org.openide.DialogDisplayer.getDefault().createDialog( dialogDescriptor );
237
238         dialog.getAccessibleContext().setAccessibleName(
239             NbBundle.getMessage(FindDialogPanel.class, "ACSN_Find")); //NOI18N
240
dialog.getAccessibleContext().setAccessibleDescription(
241             NbBundle.getMessage(FindDialogPanel.class, "ACSD_Find")); //NOI18N
242
return dialog;
243     }
244
245     private static Reference JavaDoc dialogRef = null;
246     private static Dialog getDialog() {
247         Dialog result = null;
248         if (dialogRef != null) {
249             result = (Dialog) dialogRef.get();
250         }
251         if (result == null) {
252             result = createDialog();
253             dialogRef = new WeakReference JavaDoc(result);
254         }
255         return result;
256     }
257
258 }
259
Popular Tags