1 19 20 package org.netbeans.editor.ext; 21 22 import java.awt.*; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.WindowEvent ; 26 import java.awt.event.WindowAdapter ; 27 import java.awt.event.KeyListener ; 28 import java.awt.event.KeyEvent ; 29 import java.util.ResourceBundle ; 30 import javax.swing.JButton ; 31 import javax.swing.SwingUtilities ; 32 import javax.swing.Action ; 33 import javax.swing.text.Caret ; 34 import javax.swing.text.JTextComponent ; 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 48 49 public class GotoDialogSupport implements ActionListener { 50 51 52 private static final String BOUNDS_KEY = "GotoDialogSupport.bounds-goto-line"; 54 private JButton [] gotoButtons; 55 private GotoDialogPanel gotoPanel; 56 private static Dialog gotoDialog; 57 58 public GotoDialogSupport() { 59 ResourceBundle bundle = NbBundle.getBundle(org.netbeans.editor.BaseKit.class); 60 JButton gotoButton = new JButton (bundle.getString("goto-button-goto") ); JButton cancelButton = new JButton (bundle.getString("goto-button-cancel") ); gotoButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_goto-button-goto")); cancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_goto-button-cancel")); 66 gotoButtons = new JButton [] { gotoButton, cancelButton }; 67 gotoPanel = new GotoDialogPanel(); 68 69 gotoPanel.getGotoCombo().getEditor().getEditorComponent().addKeyListener( new KeyListener () { 70 public void keyPressed(KeyEvent evt) { } 71 public void keyReleased(KeyEvent evt) { } 72 public void keyTyped(KeyEvent evt) { 73 if (evt.getKeyChar() == '\n') { 74 actionPerformed( 75 new ActionEvent (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" ), gotoPanel, false, gotoButtons, false, 0, 1, this ); 92 93 gotoDialog.pack(); 94 95 Rectangle lastBounds = (Rectangle)EditorState.get( BOUNDS_KEY ); 97 if( lastBounds != null ) { 98 gotoDialog.setBounds( lastBounds ); 99 } else { 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 ) { return; 131 } 132 133 dialog.setVisible(true); 134 gotoPanel.popupNotify(blocker); 135 136 WindowAdapter winAdapt = new WindowAdapter (){ 137 public void windowClosing(WindowEvent evt) { 138 disposeGotoDialog(); 139 } 140 141 public void windowClosed(WindowEvent evt) { 142 SwingUtilities.invokeLater(new Runnable (){ 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 evt) { 156 Object src = evt.getSource(); 157 if (src == gotoButtons[0] || src == gotoPanel ) { if (performGoto()) { 159 gotoPanel.updateHistory(); disposeGotoDialog(); 161 } 162 } else { disposeGotoDialog(); 164 } 165 } 166 167 170 protected boolean performGoto() { 171 JTextComponent c = Utilities.getLastActiveComponent(); 172 if (c != null) { 173 try { 174 int line = Integer.parseInt( 175 (String )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 int pos = Utilities.getRowStartFromLineOffset(doc, line - 1); 185 186 BaseKit kit = Utilities.getKit(c); 187 if (kit != null) { 188 Action 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 caret = c.getCaret(); 196 caret.setDot(pos); 197 } else { 198 c.getToolkit().beep(); 199 return false; 200 } 201 } 202 } catch (NumberFormatException e) { 203 c.getToolkit().beep(); 204 return false; 205 } 206 } 207 return true; 208 } 209 210 } 211 | Popular Tags |