KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.WindowEvent JavaDoc;
26 import java.awt.event.WindowAdapter JavaDoc;
27 import java.awt.event.KeyListener JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.text.Caret JavaDoc;
34 import javax.swing.text.JTextComponent JavaDoc;
35 import org.netbeans.editor.BaseDocument;
36 import org.netbeans.editor.BaseKit;
37 import org.netbeans.editor.Utilities;
38 import org.netbeans.editor.DialogSupport;
39 import org.netbeans.editor.EditorState;
40 import org.openide.util.NbBundle;
41
42 /**
43 * Support for displaying goto dialog
44 *
45 * @author Miloslav Metelka, Petr Nejedly
46 * @version 1.00
47 */

48
49 public class GotoDialogSupport implements ActionListener JavaDoc {
50
51     /** The EditorSettings key storing the last location of the dialog. */
52     private static final String JavaDoc BOUNDS_KEY = "GotoDialogSupport.bounds-goto-line"; // NOI18N
53

54     private JButton JavaDoc[] gotoButtons;
55     private GotoDialogPanel gotoPanel;
56     private static Dialog gotoDialog;
57
58     public GotoDialogSupport() {
59         ResourceBundle JavaDoc bundle = NbBundle.getBundle(org.netbeans.editor.BaseKit.class);
60         JButton JavaDoc gotoButton = new JButton JavaDoc(bundle.getString("goto-button-goto") ); // NOI18N
61
JButton JavaDoc cancelButton = new JButton JavaDoc(bundle.getString("goto-button-cancel") ); // NOI18N
62
gotoButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_goto-button-goto")); // NOI18N
63
cancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_goto-button-cancel")); // NOI18N
64
// gotoButton.setMnemonic( bundle.getString("goto-button-goto-mnemonic").charAt(0)); //NOI18N
65

66         gotoButtons = new JButton JavaDoc[] { gotoButton, cancelButton };
67         gotoPanel = new GotoDialogPanel();
68         
69         gotoPanel.getGotoCombo().getEditor().getEditorComponent().addKeyListener( new KeyListener JavaDoc() {
70                 public void keyPressed(KeyEvent JavaDoc evt) { }
71                 public void keyReleased(KeyEvent JavaDoc evt) { }
72                 public void keyTyped(KeyEvent JavaDoc evt) {
73                     if (evt.getKeyChar() == '\n') {
74                         actionPerformed(
75                             new ActionEvent JavaDoc(gotoButtons[0], 0, null));
76                     }
77                 }
78             });
79         
80     }
81     
82     protected synchronized Dialog createGotoDialog() {
83         if( gotoDialog == null ) {
84             gotoDialog = DialogSupport.createDialog(
85                 NbBundle.getBundle(org.netbeans.editor.BaseKit.class).getString( "goto-title" ), // NOI18N
86
gotoPanel, false, // non-modal
87
gotoButtons, false, // sidebuttons,
88
0, // defaultIndex = 0 => gotoButton
89
1, // cancelIndex = 1 => cancelButton
90
this //listener
91
);
92             
93             gotoDialog.pack();
94             
95             // Position the dialog according to the history
96
Rectangle lastBounds = (Rectangle)EditorState.get( BOUNDS_KEY );
97             if( lastBounds != null ) {
98                 gotoDialog.setBounds( lastBounds );
99             } else { // no history, center it on the screen
100
Dimension dim = gotoDialog.getPreferredSize();
101                 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
102                 int x = Math.max( 0, (screen.width - dim.width)/2 );
103                 int y = Math.max( 0, (screen.height - dim.height)/2 );
104                 gotoDialog.setLocation( x, y );
105             }
106             
107             return gotoDialog;
108         } else {
109             gotoDialog.setVisible(true);
110             gotoDialog.toFront();
111             return null;
112         }
113     }
114     
115     protected synchronized void disposeGotoDialog() {
116         if( gotoDialog != null ) {
117             EditorState.put( BOUNDS_KEY, gotoDialog.getBounds() );
118             gotoDialog.dispose();
119             Utilities.returnFocus();
120         }
121         
122         gotoDialog = null;
123     }
124     
125     
126     public void showGotoDialog(final KeyEventBlocker blocker) {
127         Dialog dialog = createGotoDialog();
128         if( dialog == null ) { // already visible
129
// TODO:beep()
130
return;
131         }
132         
133         dialog.setVisible(true);
134         gotoPanel.popupNotify(blocker);
135         
136         WindowAdapter JavaDoc winAdapt = new WindowAdapter JavaDoc(){
137             public void windowClosing(WindowEvent JavaDoc evt) {
138                 disposeGotoDialog();
139             }
140             
141             public void windowClosed(WindowEvent JavaDoc evt) {
142                 SwingUtilities.invokeLater(new Runnable JavaDoc(){
143                     public void run(){
144                         if (blocker!=null){
145                             blocker.stopBlocking(false);
146                         }
147                         Utilities.returnFocus();
148                     }
149                 });
150             }
151         };
152         dialog.addWindowListener(winAdapt);
153     }
154
155     public void actionPerformed(ActionEvent JavaDoc evt) {
156         Object JavaDoc src = evt.getSource();
157         if (src == gotoButtons[0] || src == gotoPanel ) { // Find button
158
if (performGoto()) {
159                 gotoPanel.updateHistory(); //A.N.: support for history
160
disposeGotoDialog();
161             }
162         } else { // Cancel button
163
disposeGotoDialog();
164         }
165     }
166
167     /** Perform the goto operation.
168     * @return whether the dialog should be made invisible or not
169     */

170     protected boolean performGoto() {
171         JTextComponent JavaDoc c = Utilities.getLastActiveComponent();
172         if (c != null) {
173             try {
174                 int line = Integer.parseInt(
175                                (String JavaDoc)gotoPanel.getValue());
176
177                 BaseDocument doc = Utilities.getDocument(c);
178                 if (doc != null) {
179                     int rowCount = Utilities.getRowCount(doc);
180                     if (line > rowCount)
181                         line = rowCount;
182                     
183                     // Obtain the offset where to jump
184
int pos = Utilities.getRowStartFromLineOffset(doc, line - 1);
185
186                     BaseKit kit = Utilities.getKit(c);
187                     if (kit != null) {
188                         Action JavaDoc a = kit.getActionByName(ExtKit.gotoAction);
189                         if (a instanceof ExtKit.GotoAction) {
190                             pos = ((ExtKit.GotoAction)a).getOffsetFromLine(doc, line - 1);
191                         }
192                     }
193
194                     if (pos != -1) {
195                         Caret JavaDoc caret = c.getCaret();
196                         caret.setDot(pos);
197                     } else {
198                         c.getToolkit().beep();
199                         return false;
200                     }
201                 }
202             } catch (NumberFormatException JavaDoc e) {
203                 c.getToolkit().beep();
204                 return false;
205             }
206         }
207         return true;
208     }
209    
210 }
211
Popular Tags