001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.editor.ext;
043:
044: import java.awt.event.*;
045: import java.util.ResourceBundle;
046: import javax.swing.JPanel;
047: import javax.swing.DefaultComboBoxModel;
048: import javax.swing.JComponent;
049: import java.util.List;
050: import java.util.ArrayList;
051:
052: import org.netbeans.editor.EditorState;
053: import org.openide.util.NbBundle;
054:
055: /**
056: * GotoDialogPanel is an UI object for entering line numbers to move caret to.
057: * It maintains its own history (stored in EditorState).
058: * For proper history functionality, it is needed to call
059: * <CODE>updateHistory()</CODE> for valid inserts.
060: *
061: * @author Miloslav Metelka, Petr Nejedly
062: * @version 2.0
063: */
064: public class GotoDialogPanel extends JPanel implements FocusListener {
065:
066: static final long serialVersionUID = -8686958102543713464L;
067: private static final String HISTORY_KEY = "GotoDialogPanel.history-goto-line"; // NOI18N
068: private static final int MAX_ITEMS = 20;
069:
070: /** The variable used during updating combo to prevent firing */
071: private boolean dontFire = false;
072: private KeyEventBlocker blocker;
073: private final ResourceBundle bundle = NbBundle
074: .getBundle(org.netbeans.editor.BaseKit.class);
075:
076: /** Initializes the UI and fetches the history */
077: public GotoDialogPanel() {
078: initComponents();
079: getAccessibleContext().setAccessibleName(
080: bundle.getString("goto-title")); // NOI18N
081: getAccessibleContext().setAccessibleDescription(
082: bundle.getString("ACSD_goto")); // NOI18N
083: gotoCombo.getAccessibleContext().setAccessibleDescription(
084: bundle.getString("ACSD_goto-line")); // NOI18N
085: List history = (List) EditorState.get(HISTORY_KEY);
086: if (history == null)
087: history = new ArrayList();
088: updateCombo(history);
089: }
090:
091: /** Set the content of the history combo
092: * @param content The List of items to be shown in the combo
093: */
094: protected void updateCombo(List content) {
095: dontFire = true;
096: gotoCombo.setModel(new DefaultComboBoxModel(content.toArray()));
097: dontFire = false;
098: }
099:
100: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
101: private void initComponents() {
102: java.awt.GridBagConstraints gridBagConstraints;
103:
104: gotoLabel = new javax.swing.JLabel();
105: gotoCombo = new javax.swing.JComboBox();
106:
107: setLayout(new java.awt.GridBagLayout());
108:
109: gotoLabel.setLabelFor(gotoCombo);
110: org.openide.awt.Mnemonics.setLocalizedText(gotoLabel, bundle
111: .getString("goto-line")); // NOI18N
112: gridBagConstraints = new java.awt.GridBagConstraints();
113: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
114: gridBagConstraints.weighty = 1.0;
115: gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 11);
116: add(gotoLabel, gridBagConstraints);
117:
118: gotoCombo.setEditable(true);
119: gridBagConstraints = new java.awt.GridBagConstraints();
120: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
121: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
122: gridBagConstraints.weightx = 1.0;
123: gridBagConstraints.weighty = 1.0;
124: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 10);
125: add(gotoCombo, gridBagConstraints);
126: }// </editor-fold>//GEN-END:initComponents
127:
128: // Variables declaration - do not modify//GEN-BEGIN:variables
129: protected javax.swing.JComboBox gotoCombo;
130: protected javax.swing.JLabel gotoLabel;
131:
132: // End of variables declaration//GEN-END:variables
133:
134: /** @return the current text from the input field */
135: public String getValue() {
136: return (String) gotoCombo.getEditor().getItem();
137: }
138:
139: /** This method is to be called when caller wishes to add the current
140: * content of the input filed to the history
141: */
142: public void updateHistory() {
143: List history = (List) EditorState.get(HISTORY_KEY);
144: if (history == null)
145: history = new ArrayList();
146:
147: Object value = getValue();
148:
149: if (history.contains(value)) {
150: // move it to top
151: history.remove(value);
152: history.add(0, value);
153: } else {
154: // assure it won't hold more than MAX_ITEMS
155: if (history.size() >= MAX_ITEMS)
156: history = history.subList(0, MAX_ITEMS - 1);
157: // add the last entered value to the top
158: history.add(0, getValue());
159: }
160: EditorState.put(HISTORY_KEY, history);
161:
162: updateCombo(history);
163: }
164:
165: /** the method called to ensure that the input field would be a focused
166: * component with the content selected
167: */
168: public void popupNotify(KeyEventBlocker blocker) {
169: this .blocker = blocker;
170: gotoCombo.getEditor().getEditorComponent().addFocusListener(
171: this );
172: gotoCombo.getEditor().selectAll();
173: gotoCombo.getEditor().getEditorComponent().requestFocus();
174: }
175:
176: public javax.swing.JComboBox getGotoCombo() {
177: return gotoCombo;
178: }
179:
180: public void focusGained(FocusEvent e) {
181: if (blocker != null)
182: blocker.stopBlocking();
183: ((JComponent) e.getSource()).removeFocusListener(this );
184: }
185:
186: public void focusLost(FocusEvent e) {
187: }
188: }
|