KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > actions > GoToPanel


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.modules.java.actions;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ItemEvent JavaDoc;
26 import java.awt.event.ItemListener JavaDoc;
27 import java.awt.event.KeyEvent JavaDoc;
28 import java.io.IOException JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import javax.swing.InputMap JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JList JavaDoc;
33 import javax.swing.KeyStroke JavaDoc;
34 import javax.swing.ListCellRenderer JavaDoc;
35 import javax.swing.ListModel JavaDoc;
36 import javax.swing.ListSelectionModel JavaDoc;
37 import javax.swing.SwingConstants JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39 import javax.swing.event.DocumentEvent JavaDoc;
40 import javax.swing.event.DocumentListener JavaDoc;
41 import javax.swing.event.ListSelectionEvent JavaDoc;
42 import javax.swing.event.ListSelectionListener JavaDoc;
43 import javax.swing.text.BadLocationException JavaDoc;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.util.NbBundle;
46
47 /**
48  *
49  * @author Petr Hrebejk
50  */

51 public class GoToPanel extends javax.swing.JPanel JavaDoc {
52             
53     private static final int BRIGHTER_COLOR_COMPONENT = 10;
54     private ContentProvider contentProvider;
55     private boolean containsScrollPane;
56     private JLabel JavaDoc messageLabel;
57     
58     
59     /** Creates new form GoToPanel */
60     public GoToPanel( ContentProvider contentProvider ) throws IOException JavaDoc {
61         this.contentProvider = contentProvider;
62         initComponents();
63         containsScrollPane = true;
64                 
65         matchesList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
66         //matchesList.setPrototypeCellValue("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
67
matchesList.addListSelectionListener(null);
68         
69         Color JavaDoc bgColorBrighter = new Color JavaDoc(
70                                     Math.min(getBackground().getRed() + BRIGHTER_COLOR_COMPONENT, 255),
71                                     Math.min(getBackground().getGreen() + BRIGHTER_COLOR_COMPONENT, 255),
72                                     Math.min(getBackground().getBlue() + BRIGHTER_COLOR_COMPONENT, 255)
73                             );
74         
75         messageLabel = new JLabel JavaDoc();
76         messageLabel.setBackground(bgColorBrighter);
77         messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
78         messageLabel.setEnabled(true);
79         messageLabel.setText(NbBundle.getMessage(GoToPanel.class, "TXT_NoTypesFound")); // NOI18N
80
messageLabel.setFont(matchesList.getFont());
81         
82         matchesList.setBackground( bgColorBrighter );
83         // matchesScrollPane1.setBackground( bgColorBrighter );
84
matchesList.setCellRenderer( contentProvider.getListCellRenderer( matchesList ) );
85         contentProvider.setListModel( this, null );
86         
87         PatternListener pl = new PatternListener( this );
88         nameField.getDocument().addDocumentListener(pl);
89         caseSensitive.setSelected(UiOptions.GoToTypeDialog.getCaseSensitive());
90         caseSensitive.addItemListener(pl);
91         matchesList.addListSelectionListener(pl);
92                 
93     }
94     
95     /** Sets the model from different therad
96      */

97     public void setModel( final ListModel JavaDoc model ) {
98         SwingUtilities.invokeLater(new Runnable JavaDoc() {
99            public void run() {
100                if (model.getSize() > 0 || getText() == null || getText().trim().length() == 0 ) {
101                    matchesList.setModel(model);
102                    matchesList.setSelectedIndex(0);
103                    setListPanelContent(null);
104                }
105                else {
106                    setListPanelContent( NbBundle.getMessage(GoToPanel.class, "TXT_NoTypesFound") ); // NOI18N
107
}
108            }
109        });
110     }
111     
112     /** Sets the initial text to find in case the user did not start typing yet. */
113     public void setInitialText( final String JavaDoc text ) {
114         SwingUtilities.invokeLater( new Runnable JavaDoc() {
115             public void run() {
116                 String JavaDoc textInField = nameField.getText();
117                 if ( textInField == null || textInField.trim().length() == 0 ) {
118                     nameField.setText(text);
119                     nameField.setCaretPosition(text.length());
120                     nameField.setSelectionStart(0);
121                     nameField.setSelectionEnd(text.length());
122                 }
123             }
124         });
125     }
126     
127     public void openSelectedItem() {
128         TypeDescription selectedValue = ((TypeDescription) matchesList.getSelectedValue());
129         if ( selectedValue != null ) {
130             ((TypeDescription) matchesList.getSelectedValue()).open();
131         }
132     }
133             
134     /** This method is called from within the constructor to
135      * initialize the form.
136      * WARNING: Do NOT modify this code. The content of this method is
137      * always regenerated by the Form Editor.
138      */

139     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
140
private void initComponents() {
141         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
142
143         jLabelText = new javax.swing.JLabel JavaDoc();
144         nameField = new javax.swing.JTextField JavaDoc();
145         jLabelList = new javax.swing.JLabel JavaDoc();
146         listPanel = new javax.swing.JPanel JavaDoc();
147         matchesScrollPane1 = new javax.swing.JScrollPane JavaDoc();
148         matchesList = new javax.swing.JList JavaDoc();
149         caseSensitive = new javax.swing.JCheckBox JavaDoc();
150         jLabelLocation = new javax.swing.JLabel JavaDoc();
151         jTextFieldLocation = new javax.swing.JTextField JavaDoc();
152
153         setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 8, 8, 8));
154         setFocusable(false);
155         setNextFocusableComponent(nameField);
156         setLayout(new java.awt.GridBagLayout JavaDoc());
157
158         jLabelText.setLabelFor(nameField);
159         org.openide.awt.Mnemonics.setLocalizedText(jLabelText, org.openide.util.NbBundle.getMessage(GoToPanel.class, "TXT_GoToType_TypeName_Label")); // NOI18N
160
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
161         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
162         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
163         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 4, 0);
164         add(jLabelText, gridBagConstraints);
165
166         nameField.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
167         nameField.setBorder(javax.swing.BorderFactory.createEtchedBorder());
168         nameField.addActionListener(new java.awt.event.ActionListener JavaDoc() {
169             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
170                 nameFieldActionPerformed(evt);
171             }
172         });
173         nameField.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
174             public void keyPressed(java.awt.event.KeyEvent JavaDoc evt) {
175                 nameFieldKeyPressed(evt);
176             }
177             public void keyReleased(java.awt.event.KeyEvent JavaDoc evt) {
178                 nameFieldKeyReleased(evt);
179             }
180             public void keyTyped(java.awt.event.KeyEvent JavaDoc evt) {
181                 nameFieldKeyTyped(evt);
182             }
183         });
184         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
185         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
186         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
187         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 8, 0);
188         add(nameField, gridBagConstraints);
189
190         jLabelList.setLabelFor(matchesScrollPane1);
191         jLabelList.setText(org.openide.util.NbBundle.getMessage(GoToPanel.class, "TXT_GoToType_MatchesList_Label")); // NOI18N
192
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
193         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
194         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
195         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 4, 0);
196         add(jLabelList, gridBagConstraints);
197
198         listPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
199         listPanel.setName("dataPanel");
200         listPanel.setLayout(new java.awt.BorderLayout JavaDoc());
201
202         matchesScrollPane1.setBorder(null);
203         matchesScrollPane1.setFocusable(false);
204
205         matchesList.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
206         matchesList.setFocusable(false);
207         matchesList.setVisibleRowCount(15);
208         matchesList.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
209             public void mouseReleased(java.awt.event.MouseEvent JavaDoc evt) {
210                 matchesListMouseReleased(evt);
211             }
212         });
213         matchesScrollPane1.setViewportView(matchesList);
214
215         listPanel.add(matchesScrollPane1, java.awt.BorderLayout.CENTER);
216
217         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
218         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
219         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
220         gridBagConstraints.weightx = 1.0;
221         gridBagConstraints.weighty = 1.0;
222         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 8, 0);
223         add(listPanel, gridBagConstraints);
224
225         org.openide.awt.Mnemonics.setLocalizedText(caseSensitive, org.openide.util.NbBundle.getMessage(GoToPanel.class, "TXT_GoToType_CaseSensitive")); // NOI18N
226
caseSensitive.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
227         caseSensitive.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
228         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
229         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
230         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
231         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 4, 0);
232         add(caseSensitive, gridBagConstraints);
233
234         jLabelLocation.setText(org.openide.util.NbBundle.getMessage(GoToPanel.class, "LBL_GoToType_LocationJLabel")); // NOI18N
235
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
236         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
237         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
238         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 4, 0);
239         add(jLabelLocation, gridBagConstraints);
240
241         jTextFieldLocation.setEditable(false);
242         jTextFieldLocation.setFocusable(false);
243         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
244         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
245         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
246         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
247         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
248         add(jTextFieldLocation, gridBagConstraints);
249     }// </editor-fold>//GEN-END:initComponents
250

251     private void matchesListMouseReleased(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_matchesListMouseReleased
252
if ( evt.getClickCount() == 2 ) {
253             nameFieldActionPerformed( null );
254         }
255     }//GEN-LAST:event_matchesListMouseReleased
256

257     private void nameFieldKeyTyped(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_nameFieldKeyTyped
258
if (boundScrollingKey(evt)) {
259         delegateScrollingKey(evt);
260     }
261     }//GEN-LAST:event_nameFieldKeyTyped
262

263     private void nameFieldKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_nameFieldKeyReleased
264
if (boundScrollingKey(evt)) {
265         delegateScrollingKey(evt);
266     }
267     }//GEN-LAST:event_nameFieldKeyReleased
268

269     private void nameFieldKeyPressed(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_nameFieldKeyPressed
270
if (boundScrollingKey(evt)) {
271         delegateScrollingKey(evt);
272     }
273     }//GEN-LAST:event_nameFieldKeyPressed
274

275     private void nameFieldActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_nameFieldActionPerformed
276
if (contentProvider.hasValidContent()) {
277             contentProvider.closeDialog();
278             openSelectedItem();
279         }
280     }//GEN-LAST:event_nameFieldActionPerformed
281

282     
283     // Variables declaration - do not modify//GEN-BEGIN:variables
284
private javax.swing.JCheckBox JavaDoc caseSensitive;
285     private javax.swing.JLabel JavaDoc jLabelList;
286     private javax.swing.JLabel JavaDoc jLabelLocation;
287     private javax.swing.JLabel JavaDoc jLabelText;
288     private javax.swing.JTextField JavaDoc jTextFieldLocation;
289     private javax.swing.JPanel JavaDoc listPanel;
290     private javax.swing.JList JavaDoc matchesList;
291     private javax.swing.JScrollPane JavaDoc matchesScrollPane1;
292     private javax.swing.JTextField JavaDoc nameField;
293     // End of variables declaration//GEN-END:variables
294

295     
296 // private static boolean isJavaIdentifier( String text ) {
297
// for( int i = 0; i < text.length(); i++ ) {
298
// if ( i == 0 ) {
299
// if ( !Character.isJavaIdentifierStart( text.charAt( i ) ) ) {
300
// return false;
301
// }
302
// }
303
// else {
304
// if ( !Character.isJavaIdentifierPart( text.charAt( i ) ) ) {
305
// return false;
306
// }
307
// }
308
// }
309
// return true;
310
// }
311

312     
313     private String JavaDoc getText() {
314         try {
315             String JavaDoc text = nameField.getDocument().getText(0, nameField.getDocument().getLength());
316             return text;
317         }
318         catch( BadLocationException JavaDoc ex ) {
319             return null;
320         }
321     }
322     
323     public boolean isCaseSensitive () {
324         return this.caseSensitive.isSelected();
325     }
326     
327     void setListPanelContent( String JavaDoc message ) {
328         
329         if ( message == null && !containsScrollPane ) {
330            listPanel.remove( messageLabel );
331            listPanel.add( matchesScrollPane1 );
332            containsScrollPane = true;
333            revalidate();
334            repaint();
335         }
336         else if ( message != null ) {
337            jTextFieldLocation.setText("");
338            messageLabel.setText(message);
339            if ( containsScrollPane ) {
340                listPanel.remove( matchesScrollPane1 );
341                listPanel.add( messageLabel );
342                containsScrollPane = false;
343            }
344            revalidate();
345            repaint();
346        }
347     }
348     
349     private String JavaDoc listActionFor(KeyEvent JavaDoc ev) {
350         InputMap JavaDoc map = matchesList.getInputMap();
351         Object JavaDoc o = map.get(KeyStroke.getKeyStrokeForEvent(ev));
352         if (o instanceof String JavaDoc) {
353             return (String JavaDoc)o;
354         } else {
355             return null;
356         }
357     }
358
359     private boolean boundScrollingKey(KeyEvent JavaDoc ev) {
360         String JavaDoc action = listActionFor(ev);
361         // See BasicListUI, MetalLookAndFeel:
362
return "selectPreviousRow".equals(action) || // NOI18N
363
"selectNextRow".equals(action) || // NOI18N
364
"selectFirstRow".equals(action) || // NOI18N
365
"selectLastRow".equals(action) || // NOI18N
366
"scrollUp".equals(action) || // NOI18N
367
"scrollDown".equals(action); // NOI18N
368
}
369
370     private void delegateScrollingKey(KeyEvent JavaDoc ev) {
371         String JavaDoc action = listActionFor(ev);
372         
373         // Wrap around
374
if ( "selectNextRow".equals(action) &&
375              matchesList.getSelectedIndex() == matchesList.getModel().getSize() -1 ) {
376             matchesList.setSelectedIndex(0);
377             matchesList.ensureIndexIsVisible(0);
378             return;
379         }
380         else if ( "selectPreviousRow".equals(action) &&
381                   matchesList.getSelectedIndex() == 0 ) {
382             int last = matchesList.getModel().getSize() - 1;
383             matchesList.setSelectedIndex(last);
384             matchesList.ensureIndexIsVisible(last);
385             return;
386         }
387         
388         // Plain delegation
389
Action JavaDoc a = matchesList.getActionMap().get(action);
390         if (a != null) {
391             a.actionPerformed(new ActionEvent JavaDoc(matchesList, 0, action));
392         }
393     }
394     
395     private static class PatternListener implements DocumentListener JavaDoc, ItemListener JavaDoc, ListSelectionListener JavaDoc {
396                
397         private final GoToPanel dialog;
398         
399         
400         PatternListener( GoToPanel dialog ) {
401             this.dialog = dialog;
402         }
403         
404         PatternListener( DocumentEvent JavaDoc e, GoToPanel dialog ) {
405             this.dialog = dialog;
406         }
407         
408         // DocumentListener ----------------------------------------------------
409

410         public void changedUpdate( DocumentEvent JavaDoc e ) {
411             update();
412         }
413
414         public void removeUpdate( DocumentEvent JavaDoc e ) {
415             update();
416         }
417
418         public void insertUpdate( DocumentEvent JavaDoc e ) {
419             update();
420         }
421         
422         // Item Listener -------------------------------------------------------
423

424         public void itemStateChanged (final ItemEvent JavaDoc e) {
425             UiOptions.GoToTypeDialog.setCaseSensitive(dialog.isCaseSensitive());
426             update();
427         }
428         
429         // ListSelectionListener -----------------------------------------------
430

431         public void valueChanged(ListSelectionEvent JavaDoc ev) {
432             TypeDescription selectedValue = ((TypeDescription) dialog.matchesList.getSelectedValue());
433             if ( selectedValue != null ) {
434                 dialog.jTextFieldLocation.setText( FileUtil.getFileDisplayName(selectedValue.getFileObject()) );
435             }
436             else {
437                 dialog.jTextFieldLocation.setText("");
438             }
439         }
440         
441         private void update() {
442             dialog.setListPanelContent(NbBundle.getMessage(GoToPanel.class, "TXT_Searching")); // NOI18N
443
dialog.contentProvider.setListModel(dialog, dialog.getText());
444         }
445                                          
446     }
447              
448     public static interface ContentProvider {
449         
450         public ListCellRenderer JavaDoc getListCellRenderer( JList JavaDoc list );
451         
452         public void setListModel( GoToPanel panel, String JavaDoc text );
453         
454         public void closeDialog();
455         
456         public boolean hasValidContent ();
457                 
458     }
459     
460 }
461
Popular Tags