1 19 20 package org.netbeans.modules.java.actions; 21 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ItemEvent ; 26 import java.awt.event.ItemListener ; 27 import java.awt.event.KeyEvent ; 28 import java.io.IOException ; 29 import javax.swing.Action ; 30 import javax.swing.InputMap ; 31 import javax.swing.JLabel ; 32 import javax.swing.JList ; 33 import javax.swing.KeyStroke ; 34 import javax.swing.ListCellRenderer ; 35 import javax.swing.ListModel ; 36 import javax.swing.ListSelectionModel ; 37 import javax.swing.SwingConstants ; 38 import javax.swing.SwingUtilities ; 39 import javax.swing.event.DocumentEvent ; 40 import javax.swing.event.DocumentListener ; 41 import javax.swing.event.ListSelectionEvent ; 42 import javax.swing.event.ListSelectionListener ; 43 import javax.swing.text.BadLocationException ; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.util.NbBundle; 46 47 51 public class GoToPanel extends javax.swing.JPanel { 52 53 private static final int BRIGHTER_COLOR_COMPONENT = 10; 54 private ContentProvider contentProvider; 55 private boolean containsScrollPane; 56 private JLabel messageLabel; 57 58 59 60 public GoToPanel( ContentProvider contentProvider ) throws IOException { 61 this.contentProvider = contentProvider; 62 initComponents(); 63 containsScrollPane = true; 64 65 matchesList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); 66 matchesList.addListSelectionListener(null); 68 69 Color bgColorBrighter = new Color ( 70 Math.min(getBackground().getRed() + BRIGHTER_COLOR_COMPONENT, 255), 71 Math.min(getBackground().getGreen() + BRIGHTER_COLOR_COMPONENT, 255), 72 Math.min(getBackground().getBlue() + BRIGHTER_COLOR_COMPONENT, 255) 73 ); 74 75 messageLabel = new JLabel (); 76 messageLabel.setBackground(bgColorBrighter); 77 messageLabel.setHorizontalAlignment(SwingConstants.CENTER); 78 messageLabel.setEnabled(true); 79 messageLabel.setText(NbBundle.getMessage(GoToPanel.class, "TXT_NoTypesFound")); messageLabel.setFont(matchesList.getFont()); 81 82 matchesList.setBackground( bgColorBrighter ); 83 matchesList.setCellRenderer( contentProvider.getListCellRenderer( matchesList ) ); 85 contentProvider.setListModel( this, null ); 86 87 PatternListener pl = new PatternListener( this ); 88 nameField.getDocument().addDocumentListener(pl); 89 caseSensitive.setSelected(UiOptions.GoToTypeDialog.getCaseSensitive()); 90 caseSensitive.addItemListener(pl); 91 matchesList.addListSelectionListener(pl); 92 93 } 94 95 97 public void setModel( final ListModel model ) { 98 SwingUtilities.invokeLater(new Runnable () { 99 public void run() { 100 if (model.getSize() > 0 || getText() == null || getText().trim().length() == 0 ) { 101 matchesList.setModel(model); 102 matchesList.setSelectedIndex(0); 103 setListPanelContent(null); 104 } 105 else { 106 setListPanelContent( NbBundle.getMessage(GoToPanel.class, "TXT_NoTypesFound") ); } 108 } 109 }); 110 } 111 112 113 public void setInitialText( final String text ) { 114 SwingUtilities.invokeLater( new Runnable () { 115 public void run() { 116 String textInField = nameField.getText(); 117 if ( textInField == null || textInField.trim().length() == 0 ) { 118 nameField.setText(text); 119 nameField.setCaretPosition(text.length()); 120 nameField.setSelectionStart(0); 121 nameField.setSelectionEnd(text.length()); 122 } 123 } 124 }); 125 } 126 127 public void openSelectedItem() { 128 TypeDescription selectedValue = ((TypeDescription) matchesList.getSelectedValue()); 129 if ( selectedValue != null ) { 130 ((TypeDescription) matchesList.getSelectedValue()).open(); 131 } 132 } 133 134 139 private void initComponents() { 141 java.awt.GridBagConstraints gridBagConstraints; 142 143 jLabelText = new javax.swing.JLabel (); 144 nameField = new javax.swing.JTextField (); 145 jLabelList = new javax.swing.JLabel (); 146 listPanel = new javax.swing.JPanel (); 147 matchesScrollPane1 = new javax.swing.JScrollPane (); 148 matchesList = new javax.swing.JList (); 149 caseSensitive = new javax.swing.JCheckBox (); 150 jLabelLocation = new javax.swing.JLabel (); 151 jTextFieldLocation = new javax.swing.JTextField (); 152 153 setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 8, 8, 8)); 154 setFocusable(false); 155 setNextFocusableComponent(nameField); 156 setLayout(new java.awt.GridBagLayout ()); 157 158 jLabelText.setLabelFor(nameField); 159 org.openide.awt.Mnemonics.setLocalizedText(jLabelText, org.openide.util.NbBundle.getMessage(GoToPanel.class, "TXT_GoToType_TypeName_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 161 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 162 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; 163 gridBagConstraints.insets = new java.awt.Insets (0, 0, 4, 0); 164 add(jLabelText, gridBagConstraints); 165 166 nameField.setFont(new java.awt.Font ("Monospaced", 0, 12)); 167 nameField.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 168 nameField.addActionListener(new java.awt.event.ActionListener () { 169 public void actionPerformed(java.awt.event.ActionEvent evt) { 170 nameFieldActionPerformed(evt); 171 } 172 }); 173 nameField.addKeyListener(new java.awt.event.KeyAdapter () { 174 public void keyPressed(java.awt.event.KeyEvent evt) { 175 nameFieldKeyPressed(evt); 176 } 177 public void keyReleased(java.awt.event.KeyEvent evt) { 178 nameFieldKeyReleased(evt); 179 } 180 public void keyTyped(java.awt.event.KeyEvent evt) { 181 nameFieldKeyTyped(evt); 182 } 183 }); 184 gridBagConstraints = new java.awt.GridBagConstraints (); 185 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 186 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 187 gridBagConstraints.insets = new java.awt.Insets (0, 0, 8, 0); 188 add(nameField, gridBagConstraints); 189 190 jLabelList.setLabelFor(matchesScrollPane1); 191 jLabelList.setText(org.openide.util.NbBundle.getMessage(GoToPanel.class, "TXT_GoToType_MatchesList_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 193 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 194 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; 195 gridBagConstraints.insets = new java.awt.Insets (0, 0, 4, 0); 196 add(jLabelList, gridBagConstraints); 197 198 listPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 199 listPanel.setName("dataPanel"); 200 listPanel.setLayout(new java.awt.BorderLayout ()); 201 202 matchesScrollPane1.setBorder(null); 203 matchesScrollPane1.setFocusable(false); 204 205 matchesList.setFont(new java.awt.Font ("Monospaced", 0, 12)); 206 matchesList.setFocusable(false); 207 matchesList.setVisibleRowCount(15); 208 matchesList.addMouseListener(new java.awt.event.MouseAdapter () { 209 public void mouseReleased(java.awt.event.MouseEvent evt) { 210 matchesListMouseReleased(evt); 211 } 212 }); 213 matchesScrollPane1.setViewportView(matchesList); 214 215 listPanel.add(matchesScrollPane1, java.awt.BorderLayout.CENTER); 216 217 gridBagConstraints = new java.awt.GridBagConstraints (); 218 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 219 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 220 gridBagConstraints.weightx = 1.0; 221 gridBagConstraints.weighty = 1.0; 222 gridBagConstraints.insets = new java.awt.Insets (0, 0, 8, 0); 223 add(listPanel, gridBagConstraints); 224 225 org.openide.awt.Mnemonics.setLocalizedText(caseSensitive, org.openide.util.NbBundle.getMessage(GoToPanel.class, "TXT_GoToType_CaseSensitive")); caseSensitive.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 227 caseSensitive.setMargin(new java.awt.Insets (0, 0, 0, 0)); 228 gridBagConstraints = new java.awt.GridBagConstraints (); 229 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 230 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 231 gridBagConstraints.insets = new java.awt.Insets (0, 0, 4, 0); 232 add(caseSensitive, gridBagConstraints); 233 234 jLabelLocation.setText(org.openide.util.NbBundle.getMessage(GoToPanel.class, "LBL_GoToType_LocationJLabel")); gridBagConstraints = new java.awt.GridBagConstraints (); 236 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 237 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 238 gridBagConstraints.insets = new java.awt.Insets (0, 0, 4, 0); 239 add(jLabelLocation, gridBagConstraints); 240 241 jTextFieldLocation.setEditable(false); 242 jTextFieldLocation.setFocusable(false); 243 gridBagConstraints = new java.awt.GridBagConstraints (); 244 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 245 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 246 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 247 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 248 add(jTextFieldLocation, gridBagConstraints); 249 } 251 private void matchesListMouseReleased(java.awt.event.MouseEvent evt) { if ( evt.getClickCount() == 2 ) { 253 nameFieldActionPerformed( null ); 254 } 255 } 257 private void nameFieldKeyTyped(java.awt.event.KeyEvent evt) { if (boundScrollingKey(evt)) { 259 delegateScrollingKey(evt); 260 } 261 } 263 private void nameFieldKeyReleased(java.awt.event.KeyEvent evt) { if (boundScrollingKey(evt)) { 265 delegateScrollingKey(evt); 266 } 267 } 269 private void nameFieldKeyPressed(java.awt.event.KeyEvent evt) { if (boundScrollingKey(evt)) { 271 delegateScrollingKey(evt); 272 } 273 } 275 private void nameFieldActionPerformed(java.awt.event.ActionEvent evt) { if (contentProvider.hasValidContent()) { 277 contentProvider.closeDialog(); 278 openSelectedItem(); 279 } 280 } 282 283 private javax.swing.JCheckBox caseSensitive; 285 private javax.swing.JLabel jLabelList; 286 private javax.swing.JLabel jLabelLocation; 287 private javax.swing.JLabel jLabelText; 288 private javax.swing.JTextField jTextFieldLocation; 289 private javax.swing.JPanel listPanel; 290 private javax.swing.JList matchesList; 291 private javax.swing.JScrollPane matchesScrollPane1; 292 private javax.swing.JTextField nameField; 293 295 296 312 313 private String getText() { 314 try { 315 String text = nameField.getDocument().getText(0, nameField.getDocument().getLength()); 316 return text; 317 } 318 catch( BadLocationException ex ) { 319 return null; 320 } 321 } 322 323 public boolean isCaseSensitive () { 324 return this.caseSensitive.isSelected(); 325 } 326 327 void setListPanelContent( String message ) { 328 329 if ( message == null && !containsScrollPane ) { 330 listPanel.remove( messageLabel ); 331 listPanel.add( matchesScrollPane1 ); 332 containsScrollPane = true; 333 revalidate(); 334 repaint(); 335 } 336 else if ( message != null ) { 337 jTextFieldLocation.setText(""); 338 messageLabel.setText(message); 339 if ( containsScrollPane ) { 340 listPanel.remove( matchesScrollPane1 ); 341 listPanel.add( messageLabel ); 342 containsScrollPane = false; 343 } 344 revalidate(); 345 repaint(); 346 } 347 } 348 349 private String listActionFor(KeyEvent ev) { 350 InputMap map = matchesList.getInputMap(); 351 Object o = map.get(KeyStroke.getKeyStrokeForEvent(ev)); 352 if (o instanceof String ) { 353 return (String )o; 354 } else { 355 return null; 356 } 357 } 358 359 private boolean boundScrollingKey(KeyEvent ev) { 360 String action = listActionFor(ev); 361 return "selectPreviousRow".equals(action) || "selectNextRow".equals(action) || "selectFirstRow".equals(action) || "selectLastRow".equals(action) || "scrollUp".equals(action) || "scrollDown".equals(action); } 369 370 private void delegateScrollingKey(KeyEvent ev) { 371 String action = listActionFor(ev); 372 373 if ( "selectNextRow".equals(action) && 375 matchesList.getSelectedIndex() == matchesList.getModel().getSize() -1 ) { 376 matchesList.setSelectedIndex(0); 377 matchesList.ensureIndexIsVisible(0); 378 return; 379 } 380 else if ( "selectPreviousRow".equals(action) && 381 matchesList.getSelectedIndex() == 0 ) { 382 int last = matchesList.getModel().getSize() - 1; 383 matchesList.setSelectedIndex(last); 384 matchesList.ensureIndexIsVisible(last); 385 return; 386 } 387 388 Action a = matchesList.getActionMap().get(action); 390 if (a != null) { 391 a.actionPerformed(new ActionEvent (matchesList, 0, action)); 392 } 393 } 394 395 private static class PatternListener implements DocumentListener , ItemListener , ListSelectionListener { 396 397 private final GoToPanel dialog; 398 399 400 PatternListener( GoToPanel dialog ) { 401 this.dialog = dialog; 402 } 403 404 PatternListener( DocumentEvent e, GoToPanel dialog ) { 405 this.dialog = dialog; 406 } 407 408 410 public void changedUpdate( DocumentEvent e ) { 411 update(); 412 } 413 414 public void removeUpdate( DocumentEvent e ) { 415 update(); 416 } 417 418 public void insertUpdate( DocumentEvent e ) { 419 update(); 420 } 421 422 424 public void itemStateChanged (final ItemEvent e) { 425 UiOptions.GoToTypeDialog.setCaseSensitive(dialog.isCaseSensitive()); 426 update(); 427 } 428 429 431 public void valueChanged(ListSelectionEvent ev) { 432 TypeDescription selectedValue = ((TypeDescription) dialog.matchesList.getSelectedValue()); 433 if ( selectedValue != null ) { 434 dialog.jTextFieldLocation.setText( FileUtil.getFileDisplayName(selectedValue.getFileObject()) ); 435 } 436 else { 437 dialog.jTextFieldLocation.setText(""); 438 } 439 } 440 441 private void update() { 442 dialog.setListPanelContent(NbBundle.getMessage(GoToPanel.class, "TXT_Searching")); dialog.contentProvider.setListModel(dialog, dialog.getText()); 444 } 445 446 } 447 448 public static interface ContentProvider { 449 450 public ListCellRenderer getListCellRenderer( JList list ); 451 452 public void setListModel( GoToPanel panel, String text ); 453 454 public void closeDialog(); 455 456 public boolean hasValidContent (); 457 458 } 459 460 } 461 | Popular Tags |