1 19 20 package org.netbeans.core.output2; 21 22 import org.openide.DialogDescriptor; 23 import org.openide.util.NbBundle; 24 25 import javax.swing.*; 26 import java.awt.*; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.KeyAdapter ; 29 import java.awt.event.KeyEvent ; 30 import java.awt.event.WindowAdapter ; 31 import java.lang.ref.Reference ; 32 import java.lang.ref.SoftReference ; 33 import java.lang.ref.WeakReference ; 34 import java.util.Arrays ; 35 import java.util.Vector ; 36 import org.openide.awt.Mnemonics; 37 38 class FindDialogPanel extends javax.swing.JPanel implements Runnable { 39 40 static final long serialVersionUID =5048678953767663114L; 41 42 private static Reference panel = null; 43 private JButton acceptButton; 44 private static Vector history = new Vector (); 45 46 47 FindDialogPanel() { 48 initComponents (); 49 Mnemonics.setLocalizedText(findWhatLabel, NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_What")); 50 getAccessibleContext().setAccessibleName(NbBundle.getBundle(FindDialogPanel.class).getString("ACSN_Find")); 51 getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FindDialogPanel.class).getString("ACSD_Find")); 52 findWhat.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FindDialogPanel.class).getString("ACSD_Find_What")); 53 findWhat.getEditor().getEditorComponent().addKeyListener( new KeyAdapter () { 54 public void keyPressed(KeyEvent evt) { 55 if ( evt.getKeyCode() == KeyEvent.VK_ESCAPE ) { 56 findWhat.setPopupVisible( false ); 57 ((Dialog)dialogRef.get()).setVisible( false ); 58 } 59 } 60 }); 61 findWhat.getEditor().addActionListener( new java.awt.event.ActionListener () { 62 public void actionPerformed( java.awt.event.ActionEvent evt ) { 63 SwingUtilities.invokeLater (FindDialogPanel.this); 66 } 67 }); 68 69 acceptButton = new JButton(); 70 Mnemonics.setLocalizedText(acceptButton, NbBundle.getBundle(FindDialogPanel.class).getString("BTN_Find")); 71 acceptButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FindDialogPanel.class).getString("ACSD_FindBTN")); 72 73 findWhat.setModel( new DefaultComboBoxModel( history ) ); 74 findWhatLabel.setFocusable(false); 75 76 JComponent[] order = new JComponent[] { 77 findWhat, acceptButton 78 }; 79 80 } 81 82 public void run() { 83 acceptButton.doClick(); 84 } 85 86 public static FindDialogPanel getPanel() { 87 FindDialogPanel result = null; 88 if (panel != null) { 89 result = (FindDialogPanel) panel.get(); 90 } 91 if (result == null) { 92 result = new FindDialogPanel(); 93 panel = new SoftReference (result); 94 } 95 return result; 96 } 97 98 void setFindText(String text) { 99 int end = text.indexOf("\n"); 100 String txt = text; 101 if (end > -1) { 102 txt = text.substring(0, end); 103 } 104 if (!txt.equals(findWhat.getSelectedItem())) { 105 findWhat.insertItemAt(txt, 0); 106 findWhat.setSelectedIndex(0); 107 } 108 selectText(); 109 110 } 111 112 private void selectText() { 113 Component comp = findWhat.getEditor().getEditorComponent(); 114 if (comp instanceof JTextField) { 115 JTextField fld = (JTextField)comp; 116 fld.setSelectionStart(0); 117 fld.setSelectionEnd(fld.getText().length()); 118 } 119 } 120 121 122 127 private void initComponents() { 129 java.awt.GridBagConstraints gridBagConstraints; 130 131 findWhatLabel = new javax.swing.JLabel (); 132 findWhat = new javax.swing.JComboBox (); 133 134 setLayout(new java.awt.GridBagLayout ()); 135 136 findWhatLabel.setLabelFor(findWhat); 137 gridBagConstraints = new java.awt.GridBagConstraints (); 138 gridBagConstraints.gridx = 0; 139 gridBagConstraints.gridy = 1; 140 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 141 gridBagConstraints.insets = new java.awt.Insets (5, 12, 5, 5); 142 add(findWhatLabel, gridBagConstraints); 143 144 findWhat.setEditable(true); 145 gridBagConstraints = new java.awt.GridBagConstraints (); 146 gridBagConstraints.gridx = 1; 147 gridBagConstraints.gridy = 1; 148 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 149 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 150 gridBagConstraints.ipadx = 50; 151 gridBagConstraints.weightx = 1.0; 152 gridBagConstraints.insets = new java.awt.Insets (5, 0, 5, 12); 153 add(findWhat, gridBagConstraints); 154 } 156 157 protected javax.swing.JComboBox findWhat; 159 protected javax.swing.JLabel findWhatLabel; 160 162 163 164 static void showFindDialog(ActionListener al, String selection) { 165 java.awt.Dialog dialog = getDialog(); 166 FindDialogPanel findPanel = getPanel(); 167 findPanel.acceptButton.putClientProperty ("panel", findPanel); 168 if (selection != null) { 169 findPanel.setFindText(selection); 170 } 171 if (!Arrays.asList(findPanel.acceptButton.getActionListeners()).contains(al)) { 172 findPanel.acceptButton.addActionListener(al); 173 } 174 dialog.setVisible(true); 175 176 findPanel.selectText(); 178 179 180 dialog.addWindowListener (new DlgWindowListener(al, findPanel.acceptButton)); 181 } 182 183 private static class DlgWindowListener extends WindowAdapter { 184 private ActionListener al; 185 private JButton acceptButton; 186 DlgWindowListener (ActionListener al, JButton acceptButton) { 187 this.al = al; 188 this.acceptButton = acceptButton; 189 } 190 191 public void windowClosed(java.awt.event.WindowEvent windowEvent) { 192 acceptButton.removeActionListener (al); 193 ((Dialog) windowEvent.getSource()).removeWindowListener(this); 194 ((Dialog) windowEvent.getSource()).dispose(); 195 } 196 } 197 198 String getPattern() { 199 FindDialogPanel findPanel = (FindDialogPanel) panel.get(); 200 return findPanel != null ? (String ) findPanel.findWhat.getSelectedItem() : null; 201 } 202 203 private void updateHistory() { 204 Object pattern = findWhat.getEditor().getItem(); 205 206 history.add( 0, pattern ); 207 for ( int i = history.size() - 1; i > 0; i-- ) { 208 if ( history.get( i ).equals( pattern ) ) { 209 history.remove( i ); 210 break; 211 } 212 } 213 } 214 215 private static Dialog createDialog() { 216 final FindDialogPanel findPanel = getPanel(); 217 218 DialogDescriptor dialogDescriptor = new DialogDescriptor( 219 findPanel, 220 NbBundle.getBundle(FindDialogPanel.class).getString("LBL_Find_Title"), 221 true, new Object [] { findPanel.acceptButton, DialogDescriptor.CANCEL_OPTION }, findPanel.acceptButton, DialogDescriptor.RIGHT_ALIGN, null, new java.awt.event.ActionListener () { 227 public void actionPerformed( java.awt.event.ActionEvent evt ) { 228 if ( evt.getSource() == findPanel.acceptButton ) { 229 getPanel().updateHistory(); 230 } 231 else 232 233 getDialog().setVisible( false ); 234 } 235 }); 236 Dialog dialog = org.openide.DialogDisplayer.getDefault().createDialog( dialogDescriptor ); 237 238 dialog.getAccessibleContext().setAccessibleName( 239 NbBundle.getMessage(FindDialogPanel.class, "ACSN_Find")); dialog.getAccessibleContext().setAccessibleDescription( 241 NbBundle.getMessage(FindDialogPanel.class, "ACSD_Find")); return dialog; 243 } 244 245 private static Reference dialogRef = null; 246 private static Dialog getDialog() { 247 Dialog result = null; 248 if (dialogRef != null) { 249 result = (Dialog) dialogRef.get(); 250 } 251 if (result == null) { 252 result = createDialog(); 253 dialogRef = new WeakReference (result); 254 } 255 return result; 256 } 257 258 } 259 | Popular Tags |