diff options
| author | 2019-05-12 11:04:22 +0200 | |
|---|---|---|
| committer | 2019-05-12 11:04:22 +0200 | |
| commit | 06efc97fa238cadf70c04a494b166d69358e9b0e (patch) | |
| tree | 17cc60c571314f206ce9815746370196bc1f153b /src/main/java/cuchaz/enigma/gui/EnigmaQuickFindDialog.java | |
| parent | Add forward and backward reference history with mouse navigation (#132) (diff) | |
| download | enigma-fork-06efc97fa238cadf70c04a494b166d69358e9b0e.tar.gz enigma-fork-06efc97fa238cadf70c04a494b166d69358e9b0e.tar.xz enigma-fork-06efc97fa238cadf70c04a494b166d69358e9b0e.zip | |
Quick find fixes (#133)
* Correctly offset quick find dialog, select all text on Ctrl+F, and set text to highlighted on open
* Support quick find navigation with enter & shift+enter
Diffstat (limited to 'src/main/java/cuchaz/enigma/gui/EnigmaQuickFindDialog.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/gui/EnigmaQuickFindDialog.java | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/EnigmaQuickFindDialog.java b/src/main/java/cuchaz/enigma/gui/EnigmaQuickFindDialog.java new file mode 100644 index 0000000..0c1d4cd --- /dev/null +++ b/src/main/java/cuchaz/enigma/gui/EnigmaQuickFindDialog.java | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | package cuchaz.enigma.gui; | ||
| 2 | |||
| 3 | import de.sciss.syntaxpane.actions.DocumentSearchData; | ||
| 4 | import de.sciss.syntaxpane.actions.gui.QuickFindDialog; | ||
| 5 | |||
| 6 | import javax.swing.*; | ||
| 7 | import javax.swing.text.JTextComponent; | ||
| 8 | import java.awt.*; | ||
| 9 | import java.awt.event.KeyAdapter; | ||
| 10 | import java.awt.event.KeyEvent; | ||
| 11 | import java.util.stream.IntStream; | ||
| 12 | import java.util.stream.Stream; | ||
| 13 | |||
| 14 | public class EnigmaQuickFindDialog extends QuickFindDialog { | ||
| 15 | public EnigmaQuickFindDialog(JTextComponent target) { | ||
| 16 | super(target, DocumentSearchData.getFromEditor(target)); | ||
| 17 | |||
| 18 | JToolBar toolBar = getToolBar(); | ||
| 19 | JTextField textField = getTextField(toolBar); | ||
| 20 | |||
| 21 | textField.addKeyListener(new KeyAdapter() { | ||
| 22 | @Override | ||
| 23 | public void keyPressed(KeyEvent e) { | ||
| 24 | super.keyPressed(e); | ||
| 25 | if (e.getKeyCode() == KeyEvent.VK_ENTER) { | ||
| 26 | JToolBar toolBar = getToolBar(); | ||
| 27 | boolean next = !e.isShiftDown(); | ||
| 28 | JButton button = next ? getNextButton(toolBar) : getPrevButton(toolBar); | ||
| 29 | button.doClick(); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | }); | ||
| 33 | } | ||
| 34 | |||
| 35 | @Override | ||
| 36 | public void showFor(JTextComponent target) { | ||
| 37 | String selectedText = target.getSelectedText(); | ||
| 38 | |||
| 39 | super.showFor(target); | ||
| 40 | |||
| 41 | Container view = target.getParent(); | ||
| 42 | Point loc = new Point(0, view.getHeight() - getSize().height); | ||
| 43 | setLocationRelativeTo(view); | ||
| 44 | SwingUtilities.convertPointToScreen(loc, view); | ||
| 45 | setLocation(loc); | ||
| 46 | |||
| 47 | JToolBar toolBar = getToolBar(); | ||
| 48 | JTextField textField = getTextField(toolBar); | ||
| 49 | |||
| 50 | if (selectedText != null) { | ||
| 51 | textField.setText(selectedText); | ||
| 52 | } | ||
| 53 | |||
| 54 | textField.selectAll(); | ||
| 55 | } | ||
| 56 | |||
| 57 | private JToolBar getToolBar() { | ||
| 58 | return components(getContentPane(), JToolBar.class).findFirst().orElse(null); | ||
| 59 | } | ||
| 60 | |||
| 61 | private JTextField getTextField(JToolBar toolBar) { | ||
| 62 | return components(toolBar, JTextField.class).findFirst().orElse(null); | ||
| 63 | } | ||
| 64 | |||
| 65 | private JButton getNextButton(JToolBar toolBar) { | ||
| 66 | Stream<JButton> buttons = components(toolBar, JButton.class); | ||
| 67 | return buttons.skip(1).findFirst().orElse(null); | ||
| 68 | } | ||
| 69 | |||
| 70 | private JButton getPrevButton(JToolBar toolBar) { | ||
| 71 | Stream<JButton> buttons = components(toolBar, JButton.class); | ||
| 72 | return buttons.findFirst().orElse(null); | ||
| 73 | } | ||
| 74 | |||
| 75 | private static Stream<Component> components(Container container) { | ||
| 76 | return IntStream.range(0, container.getComponentCount()) | ||
| 77 | .mapToObj(container::getComponent); | ||
| 78 | } | ||
| 79 | |||
| 80 | private static <T extends Component> Stream<T> components(Container container, Class<T> type) { | ||
| 81 | return components(container) | ||
| 82 | .filter(type::isInstance) | ||
| 83 | .map(type::cast); | ||
| 84 | } | ||
| 85 | } | ||