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.awt.Dialog;
017: import java.awt.Dimension;
018: import java.awt.Rectangle;
019: import java.awt.Toolkit;
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.awt.event.KeyEvent;
023: import java.awt.event.KeyListener;
024:
025: import javax.swing.Action;
026: import javax.swing.JButton;
027: import javax.swing.text.JTextComponent;
028:
029: import org.netbeans.editor.BaseDocument;
030: import org.netbeans.editor.BaseKit;
031: import org.netbeans.editor.DialogSupport;
032: import org.netbeans.editor.EditorState;
033: import org.netbeans.editor.LocaleSupport;
034: import org.netbeans.editor.Utilities;
035:
036: /**
037: * Support for displaying goto dialog
038: *
039: * @author Miloslav Metelka, Petr Nejedly
040: * @version 1.00
041: */
042:
043: public class GotoDialogSupport implements ActionListener {
044:
045: /** The EditorSettings key storing the last location of the dialog. */
046: private static final String BOUNDS_KEY = "GotoDialogSupport.bounds-goto-line"; // NOI18N
047:
048: private JButton[] gotoButtons;
049: private GotoDialogPanel gotoPanel;
050: private static Dialog gotoDialog;
051:
052: public GotoDialogSupport() {
053: JButton gotoButton = new JButton(LocaleSupport
054: .getString("goto-button-goto")); // NOI18N
055: JButton cancelButton = new JButton(LocaleSupport
056: .getString("goto-button-cancel")); // NOI18N
057: gotoButton.getAccessibleContext().setAccessibleDescription(
058: LocaleSupport.getString("ACSD_goto-button-goto")); // NOI18N
059: cancelButton.getAccessibleContext().setAccessibleDescription(
060: LocaleSupport.getString("ACSD_goto-button-cancel")); // NOI18N
061: /*
062: * gotoButton.setMnemonic(
063: * LocaleSupport.getChar("goto-button-goto-mnemonic", 'G') ); //NOI18N
064: */
065:
066: gotoButtons = new JButton[] { gotoButton, cancelButton };
067: gotoPanel = new GotoDialogPanel();
068:
069: gotoPanel.getGotoCombo().getEditor().getEditorComponent()
070: .addKeyListener(new KeyListener() {
071: public void keyPressed(KeyEvent evt) {
072: }
073:
074: public void keyReleased(KeyEvent evt) {
075: }
076:
077: public void keyTyped(KeyEvent evt) {
078: if (evt.getKeyChar() == '\n') {
079: actionPerformed(new ActionEvent(
080: gotoButtons[0], 0, null));
081: }
082: }
083: });
084:
085: }
086:
087: protected synchronized Dialog createGotoDialog() {
088: if (gotoDialog == null) {
089: gotoDialog = DialogSupport.createDialog(LocaleSupport
090: .getString("goto-title"), // NOI18N
091: gotoPanel, false, // non-modal
092: gotoButtons, false, // sidebuttons,
093: 0, // defaultIndex = 0 => gotoButton
094: 1, // cancelIndex = 1 => cancelButton
095: this // listener
096: );
097:
098: gotoDialog.pack();
099:
100: // Position the dialog according to the history
101: Rectangle lastBounds = (Rectangle) EditorState
102: .get(BOUNDS_KEY);
103: if (lastBounds != null) {
104: gotoDialog.setBounds(lastBounds);
105: } else { // no history, center it on the screen
106: Dimension dim = gotoDialog.getPreferredSize();
107: Dimension screen = Toolkit.getDefaultToolkit()
108: .getScreenSize();
109: int x = Math.max(0, (screen.width - dim.width) / 2);
110: int y = Math.max(0, (screen.height - dim.height) / 2);
111: gotoDialog.setLocation(x, y);
112: }
113:
114: return gotoDialog;
115: } else {
116: gotoDialog.toFront();
117: return null;
118: }
119: }
120:
121: protected synchronized void disposeGotoDialog() {
122: if (gotoDialog != null) {
123: EditorState.put(BOUNDS_KEY, gotoDialog.getBounds());
124: gotoDialog.dispose();
125: Utilities.returnFocus();
126: }
127:
128: gotoDialog = null;
129: }
130:
131: public void showGotoDialog() {
132: Dialog dialog = createGotoDialog();
133: if (dialog == null) { // already visible
134: // TODO:beep()
135: return;
136: }
137: gotoPanel.popupNotify();
138: dialog.setVisible(true);
139: }
140:
141: public void actionPerformed(ActionEvent evt) {
142: Object src = evt.getSource();
143: if (src == gotoButtons[0] || src == gotoPanel) { // Find button
144: if (performGoto()) {
145: gotoPanel.updateHistory(); // A.N.: support for history
146: disposeGotoDialog();
147: }
148: } else { // Cancel button
149: disposeGotoDialog();
150: }
151: }
152:
153: /**
154: * Perform the goto operation.
155: *
156: * @return whether the dialog should be made invisible or not
157: */
158: protected boolean performGoto() {
159: JTextComponent c = Utilities.getLastActiveComponent();
160: if (c != null) {
161: try {
162: int line = Integer.parseInt((String) gotoPanel
163: .getValue());
164:
165: BaseDocument doc = Utilities.getDocument(c);
166: if (doc != null) {
167: // Obtain the offset where to jump
168: int pos = Utilities.getRowStartFromLineOffset(doc,
169: line - 1);
170:
171: BaseKit kit = Utilities.getKit(c);
172: if (kit != null) {
173: Action a = kit
174: .getActionByName(ExtKit.gotoAction);
175: if (a instanceof ExtKit.GotoAction) {
176: pos = ((ExtKit.GotoAction) a)
177: .getOffsetFromLine(doc, line - 1);
178: }
179: }
180:
181: if (pos != -1) {
182: c.getCaret().setDot(pos);
183: } else {
184: c.getToolkit().beep();
185: return false;
186: }
187: }
188: } catch (NumberFormatException e) {
189: c.getToolkit().beep();
190: return false;
191: }
192: }
193: return true;
194: }
195:
196: }
|