KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > GotoDialogPanel


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.editor.ext;
21
22 import java.awt.event.*;
23 import java.util.ResourceBundle JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.DefaultComboBoxModel JavaDoc;
26 import javax.swing.JTextField JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.netbeans.editor.EditorState;
33 import org.openide.util.NbBundle;
34
35 /**
36  * GotoDialogPanel is an UI object for entering line numbers to move caret to.
37  * It maintains its own history (stored in EditorState).
38  * For proper history functionality, it is needed to call
39  * <CODE>updateHistory()</CODE> for valid inserts.
40  *
41  * @author Miloslav Metelka, Petr Nejedly
42  * @version 2.0
43  */

44 public class GotoDialogPanel extends JPanel JavaDoc implements FocusListener {
45
46     static final long serialVersionUID =-8686958102543713464L;
47     private static final String JavaDoc HISTORY_KEY = "GotoDialogPanel.history-goto-line"; // NOI18N
48
private static final int MAX_ITEMS = 20;
49
50     /** The variable used during updating combo to prevent firing */
51     private boolean dontFire = false;
52     private KeyEventBlocker blocker;
53     private final ResourceBundle JavaDoc bundle = NbBundle.getBundle(org.netbeans.editor.BaseKit.class);
54
55     /** Initializes the UI and fetches the history */
56     public GotoDialogPanel() {
57         initComponents ();
58         getAccessibleContext().setAccessibleName(bundle.getString("goto-title")); // NOI18N
59
getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_goto")); // NOI18N
60
gotoCombo.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_goto-line")); // NOI18N
61
List JavaDoc history = (List JavaDoc)EditorState.get( HISTORY_KEY );
62         if( history == null ) history = new ArrayList JavaDoc();
63         updateCombo( history );
64     }
65
66     /** Set the content of the history combo
67      * @param content The List of items to be shown in the combo
68      */

69     protected void updateCombo( List JavaDoc content ) {
70         dontFire = true;
71         gotoCombo.setModel( new DefaultComboBoxModel JavaDoc( content.toArray() ) );
72         dontFire = false;
73     }
74
75     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
76
private void initComponents() {
77         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
78
79         gotoLabel = new javax.swing.JLabel JavaDoc();
80         gotoCombo = new javax.swing.JComboBox JavaDoc();
81
82         setLayout(new java.awt.GridBagLayout JavaDoc());
83
84         gotoLabel.setLabelFor(gotoCombo);
85         gotoLabel.setText(bundle.getString("goto-line"));
86         gotoLabel.setDisplayedMnemonic(bundle.getString("goto-line-mnemonic").charAt(0));
87         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
88         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
89         gridBagConstraints.weighty = 1.0;
90         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 11);
91         add(gotoLabel, gridBagConstraints);
92
93         gotoCombo.setEditable(true);
94         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
95         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
96         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
97         gridBagConstraints.weightx = 1.0;
98         gridBagConstraints.weighty = 1.0;
99         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 10);
100         add(gotoCombo, gridBagConstraints);
101
102     }// </editor-fold>//GEN-END:initComponents
103

104
105
106     // Variables declaration - do not modify//GEN-BEGIN:variables
107
protected javax.swing.JComboBox JavaDoc gotoCombo;
108     protected javax.swing.JLabel JavaDoc gotoLabel;
109     // End of variables declaration//GEN-END:variables
110

111
112     /** @return the current text from the input field */
113     public String JavaDoc getValue() {
114         return (String JavaDoc)gotoCombo.getEditor().getItem();
115     }
116     
117     /** This method is to be called when caller wishes to add the current
118      * content of the input filed to the history
119      */

120     public void updateHistory() {
121         List JavaDoc history = (List JavaDoc)EditorState.get( HISTORY_KEY );
122         if( history == null ) history = new ArrayList JavaDoc();
123
124         Object JavaDoc value = getValue();
125
126         if( history.contains( value ) ) {
127             // move it to top
128
history.remove( value );
129             history.add( 0, value );
130         } else {
131             // assure it won't hold more than MAX_ITEMS
132
if( history.size() >= MAX_ITEMS )
133                 history = history.subList(0, MAX_ITEMS-1);
134             // add the last entered value to the top
135
history.add( 0, getValue() );
136         }
137         EditorState.put( HISTORY_KEY, history );
138         
139         updateCombo( history );
140     }
141
142     /** the method called to ensure that the input field would be a focused
143      * component with the content selected
144      */

145     public void popupNotify(KeyEventBlocker blocker) {
146         this.blocker = blocker;
147         gotoCombo.getEditor().getEditorComponent().addFocusListener(this);
148         gotoCombo.getEditor().selectAll();
149         gotoCombo.getEditor().getEditorComponent().requestFocus();
150     }
151
152     public javax.swing.JComboBox JavaDoc getGotoCombo()
153     {
154         return gotoCombo;
155     }
156
157     public void focusGained(FocusEvent e) {
158         if (blocker != null)
159             blocker.stopBlocking();
160         ((JComponent JavaDoc)e.getSource()).removeFocusListener(this);
161     }
162
163     public void focusLost(FocusEvent e) {
164     }
165 }
166
Popular Tags