001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor.ext;
015:
016: import java.util.ArrayList;
017: import java.util.List;
018:
019: import javax.swing.DefaultComboBoxModel;
020: import javax.swing.JPanel;
021:
022: import org.netbeans.editor.EditorState;
023: import org.netbeans.editor.LocaleSupport;
024:
025: /**
026: * GotoDialogPanel is an UI object for entering line numbers to move caret to.
027: * It maintains its own history (stored in EditorState). For proper history
028: * functionality, it is needed to call <CODE>updateHistory()</CODE> for valid
029: * inserts.
030: *
031: * @author Miloslav Metelka, Petr Nejedly
032: * @version 2.0
033: */
034: public class GotoDialogPanel extends JPanel {
035:
036: static final long serialVersionUID = -8686958102543713464L;
037: private static final String HISTORY_KEY = "GotoDialogPanel.history-goto-line";
038: private static final int MAX_ITEMS = 20;
039:
040: /** The variable used during updating combo to prevent firing */
041: private boolean dontFire = false;
042:
043: /** Initializes the UI and fetches the history */
044: public GotoDialogPanel() {
045: initComponents();
046: getAccessibleContext().setAccessibleName(
047: LocaleSupport.getString("goto-title")); // NOI18N
048: getAccessibleContext().setAccessibleDescription(
049: LocaleSupport.getString("ACSD_goto")); // NOI18N
050: gotoCombo.getAccessibleContext().setAccessibleDescription(
051: LocaleSupport.getString("ACSD_goto-line")); // NOI18N
052: List history = (List) EditorState.get(HISTORY_KEY);
053: if (history == null)
054: history = new ArrayList();
055: updateCombo(history);
056: }
057:
058: /**
059: * Set the content of the history combo
060: *
061: * @param content
062: * The List of items to be shown in the combo
063: */
064: protected void updateCombo(List content) {
065: dontFire = true;
066: gotoCombo.setModel(new DefaultComboBoxModel(content.toArray()));
067: dontFire = false;
068: }
069:
070: private void initComponents() {// GEN-BEGIN:initComponents
071: gotoLabel = new javax.swing.JLabel();
072: gotoCombo = new javax.swing.JComboBox();
073:
074: setLayout(new java.awt.GridBagLayout());
075: java.awt.GridBagConstraints gridBagConstraints1;
076:
077: gotoLabel.setText(LocaleSupport.getString("goto-line"));
078: gotoLabel.setLabelFor(gotoCombo);
079: gotoLabel.setDisplayedMnemonic(LocaleSupport.getChar(
080: "goto-line-mnemonic", 'l'));
081: gridBagConstraints1 = new java.awt.GridBagConstraints();
082: gridBagConstraints1.insets = new java.awt.Insets(12, 12, 0, 11);
083: gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
084: gridBagConstraints1.weighty = 1.0;
085: add(gotoLabel, gridBagConstraints1);
086:
087: gotoCombo.setEditable(true);
088: gridBagConstraints1 = new java.awt.GridBagConstraints();
089: gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
090: gridBagConstraints1.insets = new java.awt.Insets(12, 0, 0, 10);
091: gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
092: gridBagConstraints1.weightx = 1.0;
093: gridBagConstraints1.weighty = 1.0;
094: add(gotoCombo, gridBagConstraints1);
095:
096: }// GEN-END:initComponents
097:
098: // Variables declaration - do not modify//GEN-BEGIN:variables
099: protected javax.swing.JLabel gotoLabel;
100: protected javax.swing.JComboBox gotoCombo;
101:
102: // End of variables declaration//GEN-END:variables
103:
104: /** @return the current text from the input field */
105: public String getValue() {
106: return (String) gotoCombo.getEditor().getItem();
107: }
108:
109: /**
110: * This method is to be called when caller wishes to add the current content
111: * of the input filed to the history
112: */
113: public void updateHistory() {
114: List history = (List) EditorState.get(HISTORY_KEY);
115: if (history == null)
116: history = new ArrayList();
117:
118: Object value = getValue();
119:
120: if (history.contains(value)) {
121: // move it to top
122: history.remove(value);
123: history.add(0, value);
124: } else {
125: // assure it won't hold more than MAX_ITEMS
126: if (history.size() >= MAX_ITEMS)
127: history = history.subList(0, MAX_ITEMS - 1);
128: // add the last entered value to the top
129: history.add(0, getValue());
130: }
131: EditorState.put(HISTORY_KEY, history);
132:
133: updateCombo(history);
134: }
135:
136: /**
137: * the method called to ensure that the input field would be a focused
138: * component with the content selected
139: */
140: public void popupNotify() {
141: gotoCombo.getEditor().selectAll();
142: gotoCombo.getEditor().getEditorComponent().requestFocus();
143: }
144:
145: public javax.swing.JComboBox getGotoCombo() {
146: return gotoCombo;
147: }
148:
149: }
|