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.*;
045: import java.awt.event.ActionListener;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.WindowEvent;
048: import java.awt.event.WindowAdapter;
049: import java.awt.event.KeyListener;
050: import java.awt.event.KeyEvent;
051: import java.util.ResourceBundle;
052: import javax.swing.JButton;
053: import javax.swing.SwingUtilities;
054: import javax.swing.Action;
055: import javax.swing.KeyStroke;
056: import javax.swing.text.Caret;
057: import javax.swing.text.JTextComponent;
058: import org.netbeans.editor.BaseDocument;
059: import org.netbeans.editor.BaseKit;
060: import org.netbeans.editor.Utilities;
061: import org.netbeans.editor.DialogSupport;
062: import org.netbeans.editor.EditorState;
063: import org.openide.util.NbBundle;
064:
065: /**
066: * Support for displaying goto dialog
067: *
068: * @author Miloslav Metelka, Petr Nejedly
069: * @version 1.00
070: */
071:
072: public class GotoDialogSupport implements ActionListener {
073:
074: /** The EditorSettings key storing the last location of the dialog. */
075: private static final String BOUNDS_KEY = "GotoDialogSupport.bounds-goto-line"; // NOI18N
076:
077: private JButton[] gotoButtons;
078: private GotoDialogPanel gotoPanel;
079: private static Dialog gotoDialog;
080:
081: public GotoDialogSupport() {
082: ResourceBundle bundle = NbBundle
083: .getBundle(org.netbeans.editor.BaseKit.class);
084: JButton gotoButton = new JButton(bundle
085: .getString("goto-button-goto")); // NOI18N
086: JButton cancelButton = new JButton(bundle
087: .getString("goto-button-cancel")); // NOI18N
088: gotoButton.getAccessibleContext().setAccessibleDescription(
089: bundle.getString("ACSD_goto-button-goto")); // NOI18N
090: cancelButton.getAccessibleContext().setAccessibleDescription(
091: bundle.getString("ACSD_goto-button-cancel")); // NOI18N
092: // gotoButton.setMnemonic( bundle.getString("goto-button-goto-mnemonic").charAt(0)); //NOI18N
093:
094: gotoButtons = new JButton[] { gotoButton, cancelButton };
095: gotoPanel = new GotoDialogPanel();
096:
097: gotoPanel.getGotoCombo().getEditor().getEditorComponent()
098: .addKeyListener(new KeyListener() {
099: public void keyPressed(KeyEvent evt) {
100: }
101:
102: public void keyReleased(KeyEvent evt) {
103: }
104:
105: public void keyTyped(KeyEvent evt) {
106: if (evt.getKeyChar() == '\n') {
107: actionPerformed(new ActionEvent(
108: gotoButtons[0], 0, null));
109: }
110: if (!Character.isDigit(evt.getKeyChar())
111: && !Character.isISOControl(evt
112: .getKeyChar())) {
113: evt.consume();
114: Component c = evt.getComponent();
115: if (c != null) {
116: c.getToolkit().beep();
117: }
118: }
119: }
120: });
121:
122: }
123:
124: protected synchronized Dialog createGotoDialog() {
125: if (gotoDialog == null) {
126: gotoDialog = DialogSupport.createDialog(NbBundle.getBundle(
127: org.netbeans.editor.BaseKit.class).getString(
128: "goto-title"), // NOI18N
129: gotoPanel, false, // non-modal
130: gotoButtons, false, // sidebuttons,
131: 0, // defaultIndex = 0 => gotoButton
132: 1, // cancelIndex = 1 => cancelButton
133: this //listener
134: );
135:
136: gotoDialog.pack();
137:
138: // Position the dialog according to the history
139: Rectangle lastBounds = (Rectangle) EditorState
140: .get(BOUNDS_KEY);
141: if (lastBounds != null) {
142: gotoDialog.setBounds(lastBounds);
143: } else { // no history, center it on the screen
144: Dimension dim = gotoDialog.getPreferredSize();
145: Dimension screen = Toolkit.getDefaultToolkit()
146: .getScreenSize();
147: int x = Math.max(0, (screen.width - dim.width) / 2);
148: int y = Math.max(0, (screen.height - dim.height) / 2);
149: gotoDialog.setLocation(x, y);
150: }
151:
152: return gotoDialog;
153: } else {
154: gotoDialog.setVisible(true);
155: gotoDialog.toFront();
156: return null;
157: }
158: }
159:
160: protected synchronized void disposeGotoDialog() {
161: if (gotoDialog != null) {
162: EditorState.put(BOUNDS_KEY, gotoDialog.getBounds());
163: gotoDialog.dispose();
164: Utilities.returnFocus();
165: }
166:
167: gotoDialog = null;
168: }
169:
170: public void showGotoDialog(final KeyEventBlocker blocker) {
171: Dialog dialog = createGotoDialog();
172: if (dialog == null) { // already visible
173: // TODO:beep()
174: return;
175: }
176:
177: dialog.setVisible(true);
178: gotoPanel.popupNotify(blocker);
179:
180: WindowAdapter winAdapt = new WindowAdapter() {
181: public void windowClosing(WindowEvent evt) {
182: disposeGotoDialog();
183: }
184:
185: public void windowClosed(WindowEvent evt) {
186: SwingUtilities.invokeLater(new Runnable() {
187: public void run() {
188: if (blocker != null) {
189: blocker.stopBlocking(false);
190: }
191: Utilities.returnFocus();
192: }
193: });
194: }
195: };
196: dialog.addWindowListener(winAdapt);
197: }
198:
199: public void actionPerformed(ActionEvent evt) {
200: Object src = evt.getSource();
201: if (src == gotoButtons[0] || src == gotoPanel) { // Find button
202: if (performGoto()) {
203: gotoPanel.updateHistory(); //A.N.: support for history
204: disposeGotoDialog();
205: }
206: } else { // Cancel button
207: disposeGotoDialog();
208: }
209: }
210:
211: /** Perform the goto operation.
212: * @return whether the dialog should be made invisible or not
213: */
214: protected boolean performGoto() {
215: JTextComponent c = Utilities.getLastActiveComponent();
216: if (c != null) {
217: try {
218: int line = Integer.parseInt((String) gotoPanel
219: .getValue());
220:
221: BaseDocument doc = Utilities.getDocument(c);
222: if (doc != null) {
223: int rowCount = Utilities.getRowCount(doc);
224: if (line > rowCount)
225: line = rowCount;
226:
227: // Obtain the offset where to jump
228: int pos = Utilities.getRowStartFromLineOffset(doc,
229: line - 1);
230:
231: BaseKit kit = Utilities.getKit(c);
232: if (kit != null) {
233: Action a = kit
234: .getActionByName(ExtKit.gotoAction);
235: if (a instanceof ExtKit.GotoAction) {
236: pos = ((ExtKit.GotoAction) a)
237: .getOffsetFromLine(doc, line - 1);
238: }
239: }
240:
241: if (pos != -1) {
242: Caret caret = c.getCaret();
243: caret.setDot(pos);
244: } else {
245: c.getToolkit().beep();
246: return false;
247: }
248: }
249: } catch (NumberFormatException e) {
250: c.getToolkit().beep();
251: return false;
252: }
253: }
254: return true;
255: }
256:
257: }
|