Lines 42-60
Link Here
|
42 |
package org.netbeans.modules.search.ui; |
42 |
package org.netbeans.modules.search.ui; |
43 |
|
43 |
|
44 |
import java.awt.Color; |
44 |
import java.awt.Color; |
|
|
45 |
import java.awt.Component; |
45 |
import java.awt.EventQueue; |
46 |
import java.awt.EventQueue; |
|
|
47 |
import java.awt.Window; |
46 |
import java.awt.datatransfer.DataFlavor; |
48 |
import java.awt.datatransfer.DataFlavor; |
47 |
import java.awt.datatransfer.Transferable; |
49 |
import java.awt.datatransfer.Transferable; |
48 |
import java.awt.datatransfer.UnsupportedFlavorException; |
50 |
import java.awt.datatransfer.UnsupportedFlavorException; |
|
|
51 |
import java.awt.event.ActionEvent; |
52 |
import java.awt.event.KeyAdapter; |
53 |
import java.awt.event.KeyEvent; |
54 |
import java.awt.event.KeyListener; |
49 |
import java.io.CharConversionException; |
55 |
import java.io.CharConversionException; |
50 |
import java.io.File; |
56 |
import java.io.File; |
51 |
import java.io.IOException; |
57 |
import java.io.IOException; |
52 |
import javax.swing.AbstractButton; |
58 |
import javax.swing.AbstractButton; |
|
|
59 |
import javax.swing.Action; |
60 |
import javax.swing.ComboBoxEditor; |
61 |
import javax.swing.JButton; |
62 |
import javax.swing.JComboBox; |
63 |
import javax.swing.JComponent; |
64 |
import javax.swing.JDialog; |
53 |
import javax.swing.JLabel; |
65 |
import javax.swing.JLabel; |
|
|
66 |
import javax.swing.JRootPane; |
67 |
import javax.swing.KeyStroke; |
54 |
import javax.swing.UIManager; |
68 |
import javax.swing.UIManager; |
55 |
import org.netbeans.modules.search.BasicSearchProvider; |
69 |
import org.netbeans.modules.search.BasicSearchProvider; |
56 |
import org.openide.awt.Mnemonics; |
70 |
import org.openide.awt.Mnemonics; |
57 |
import org.openide.util.NbBundle; |
71 |
import org.openide.util.NbBundle; |
|
|
72 |
import org.openide.util.Utilities; |
58 |
import org.openide.xml.XMLUtil; |
73 |
import org.openide.xml.XMLUtil; |
59 |
|
74 |
|
60 |
/** |
75 |
/** |
Lines 158-161
Link Here
|
158 |
"BasicSearchForm.cboxFileNamePattern.example"); //NOI18N |
173 |
"BasicSearchForm.cboxFileNamePattern.example"); //NOI18N |
159 |
} |
174 |
} |
160 |
} |
175 |
} |
|
|
176 |
|
177 |
/** |
178 |
* Workaround bugs in checkboxes - they consume some key events on Mac with |
179 |
* JDK8. See bug 239241. |
180 |
* |
181 |
* @param comboBoxes |
182 |
*/ |
183 |
public static void fixComboBoxes(JComboBox... comboBoxes) { |
184 |
|
185 |
int javaVersion = -1; |
186 |
String javaVersionStr = System.getProperty("java.version"); //NOI18N |
187 |
if (javaVersionStr != null) { |
188 |
String[] parts = javaVersionStr.split("\\."); //NOI18N |
189 |
if (parts != null && parts.length > 1) { |
190 |
try { |
191 |
javaVersion = Integer.parseInt(parts[1]); |
192 |
} catch (NumberFormatException e) { |
193 |
} |
194 |
} |
195 |
} |
196 |
|
197 |
if (Utilities.isMac() && (javaVersion < 0 || javaVersion > 7) |
198 |
&& comboBoxes.length > 0) { |
199 |
|
200 |
KeyListener kl = new JComboBoxEscapeAndEnterListener(); |
201 |
|
202 |
for (JComboBox<?> comboBox : comboBoxes) { |
203 |
if (comboBox != null) { |
204 |
comboBox.addKeyListener(kl); |
205 |
ComboBoxEditor editor = comboBox.getEditor(); |
206 |
if (editor != null) { |
207 |
Component editorComponent = editor.getEditorComponent(); |
208 |
if (editorComponent != null) { |
209 |
editorComponent.addKeyListener(kl); |
210 |
} |
211 |
} |
212 |
} |
213 |
} |
214 |
} |
215 |
} |
216 |
|
217 |
/** |
218 |
* Listener for Enter and Escape pressing in JComboBoxes. See |
219 |
* {@link #fixComboBoxes(JComboBox...)}. |
220 |
* |
221 |
*/ |
222 |
private static class JComboBoxEscapeAndEnterListener extends KeyAdapter { |
223 |
|
224 |
/** |
225 |
* Key pressed in a JComboBox or in its editor. |
226 |
* |
227 |
* @param e |
228 |
*/ |
229 |
@Override |
230 |
public void keyPressed(KeyEvent e) { |
231 |
Object source = e.getSource(); |
232 |
if (source instanceof Component) { |
233 |
for (Component a = ((Component) source); a != null; |
234 |
a = a.getParent()) { |
235 |
if (a instanceof JComboBox) { |
236 |
process((JComboBox<?>) a, e); |
237 |
return; |
238 |
} |
239 |
} |
240 |
} |
241 |
} |
242 |
|
243 |
/** |
244 |
* Check whether the event should handled, find the containing dialog |
245 |
* and invoke proper action in its root pane. |
246 |
* |
247 |
* @param comboBox |
248 |
* @param e |
249 |
*/ |
250 |
private void process(JComboBox<?> comboBox, KeyEvent e) { |
251 |
if (!comboBox.isPopupVisible() |
252 |
&& comboBox.getParent() != null |
253 |
&& (e.getKeyCode() == KeyEvent.VK_ESCAPE |
254 |
|| e.getKeyCode() == KeyEvent.VK_ENTER)) { |
255 |
Component parent = comboBox; |
256 |
while (parent != null && !(parent instanceof Window)) { |
257 |
parent = parent.getParent(); |
258 |
} |
259 |
if (parent instanceof JDialog) { |
260 |
JDialog dial = (JDialog) parent; |
261 |
JRootPane rootPane = dial.getRootPane(); |
262 |
invokeProperAction(e, rootPane); |
263 |
} |
264 |
} |
265 |
} |
266 |
|
267 |
/** |
268 |
* Invoke proper action for the event - click the default button or |
269 |
* invoke the action associated with Escape key. |
270 |
* |
271 |
* @param e |
272 |
* @param rootPane |
273 |
*/ |
274 |
private void invokeProperAction(KeyEvent e, JRootPane rootPane) { |
275 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
276 |
JButton button = rootPane.getDefaultButton(); |
277 |
if (button != null) { |
278 |
button.doClick(); |
279 |
} |
280 |
} else { |
281 |
Object key = rootPane.getInputMap( |
282 |
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) |
283 |
.get(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)); |
284 |
if (key != null) { |
285 |
Action action = rootPane.getActionMap().get(key); |
286 |
if (action != null) { |
287 |
action.actionPerformed( |
288 |
new ActionEvent(e.getSource(), |
289 |
e.getID(), "Escape")); //NOI18N |
290 |
} |
291 |
} |
292 |
} |
293 |
} |
294 |
} |
161 |
} |
295 |
} |