|
| 1 | +/* |
| 2 | + * Copyright 2008 Ayman Al-Sairafi [email protected] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package org.quiltmc.enigma.gui.dialog; |
| 15 | + |
| 16 | +import org.quiltmc.enigma.gui.config.Config; |
| 17 | +import org.quiltmc.enigma.gui.config.keybind.KeyBinds; |
| 18 | +import org.quiltmc.enigma.gui.util.GuiUtil; |
| 19 | +import org.quiltmc.enigma.gui.util.GuiUtil.FocusCondition; |
| 20 | +import org.quiltmc.enigma.util.I18n; |
| 21 | +import org.quiltmc.syntaxpain.QuickFindToolBar; |
| 22 | + |
| 23 | +import javax.swing.Box; |
| 24 | +import javax.swing.JButton; |
| 25 | +import javax.swing.JCheckBox; |
| 26 | +import javax.swing.SwingConstants; |
| 27 | + |
| 28 | +import static org.quiltmc.enigma.gui.util.GuiUtil.putKeyBindAction; |
| 29 | + |
| 30 | +/** |
| 31 | + * Extension of {@link QuickFindToolBar} to allow using keybindings, and improve UI. |
| 32 | + */ |
| 33 | +public class EnigmaQuickFindToolBar extends QuickFindToolBar { |
| 34 | + protected JCheckBox persistentCheckBox; |
| 35 | + protected JButton closeButton; |
| 36 | + |
| 37 | + public EnigmaQuickFindToolBar() { |
| 38 | + super(); |
| 39 | + // keybinding support |
| 40 | + this.reloadKeyBinds(); |
| 41 | + |
| 42 | + // configure parent components |
| 43 | + this.ignoreCaseCheckBox.setMnemonic(KeyBinds.QUICK_FIND_DIALOG_IGNORE_CASE.getKeyCode()); |
| 44 | + this.regexCheckBox.setMnemonic(KeyBinds.QUICK_FIND_DIALOG_REGEX.getKeyCode()); |
| 45 | + this.wrapCheckBox.setMnemonic(KeyBinds.QUICK_FIND_DIALOG_WRAP.getKeyCode()); |
| 46 | + |
| 47 | + // make buttons icon-only |
| 48 | + this.nextButton.setText(""); |
| 49 | + this.nextButton.setIcon(GuiUtil.getDownChevron()); |
| 50 | + this.prevButton.setText(""); |
| 51 | + this.prevButton.setIcon(GuiUtil.getUpChevron()); |
| 52 | + |
| 53 | + // add custom components |
| 54 | + // push the rest of the components to the right |
| 55 | + this.add(Box.createHorizontalGlue()); |
| 56 | + |
| 57 | + this.addSeparator(); |
| 58 | + |
| 59 | + this.persistentCheckBox = new JCheckBox(); |
| 60 | + this.persistentCheckBox.setFocusable(false); |
| 61 | + this.persistentCheckBox.setOpaque(false); |
| 62 | + this.persistentCheckBox.setVerticalTextPosition(SwingConstants.BOTTOM); |
| 63 | + this.persistentCheckBox.setHorizontalTextPosition(SwingConstants.LEADING); |
| 64 | + this.persistentCheckBox.addActionListener(this); |
| 65 | + this.persistentCheckBox.addItemListener(e -> { |
| 66 | + final boolean selected = this.persistentCheckBox.isSelected(); |
| 67 | + if (selected != Config.main().persistentEditorQuickFind.value()) { |
| 68 | + Config.main().persistentEditorQuickFind.setValue(selected); |
| 69 | + } |
| 70 | + |
| 71 | + // request focus so when it's lost this may be dismissed |
| 72 | + this.requestFocus(); |
| 73 | + }); |
| 74 | + this.persistentCheckBox.setSelected(Config.main().persistentEditorQuickFind.value()); |
| 75 | + Config.main().persistentEditorQuickFind.registerCallback(callback -> { |
| 76 | + final Boolean configured = callback.value(); |
| 77 | + if (this.persistentCheckBox.isSelected() != configured) { |
| 78 | + this.persistentCheckBox.setSelected(configured); |
| 79 | + } |
| 80 | + }); |
| 81 | + this.add(this.persistentCheckBox); |
| 82 | + |
| 83 | + this.addSeparator(); |
| 84 | + |
| 85 | + this.closeButton = new JButton(); |
| 86 | + this.closeButton.setIcon(GuiUtil.getCloseIcon()); |
| 87 | + this.closeButton.setFocusable(false); |
| 88 | + this.closeButton.setOpaque(false); |
| 89 | + this.closeButton.addActionListener(e -> this.dismiss()); |
| 90 | + this.add(this.closeButton); |
| 91 | + |
| 92 | + this.translate(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected boolean dismissOnFocusLost() { |
| 97 | + return !this.persistentCheckBox.isSelected(); |
| 98 | + } |
| 99 | + |
| 100 | + public void translate() { |
| 101 | + this.notFound = I18n.translate("editor.quick_find.not_found"); |
| 102 | + |
| 103 | + this.ignoreCaseCheckBox.setText(I18n.translate("editor.quick_find.ignore_case")); |
| 104 | + this.regexCheckBox.setText(I18n.translate("editor.quick_find.use_regex")); |
| 105 | + this.wrapCheckBox.setText(I18n.translate("editor.quick_find.wrap")); |
| 106 | + |
| 107 | + this.persistentCheckBox.setText(I18n.translate("editor.quick_find.persistent")); |
| 108 | + } |
| 109 | + |
| 110 | + public void reloadKeyBinds() { |
| 111 | + putKeyBindAction(KeyBinds.QUICK_FIND_DIALOG_PREVIOUS, this.searchField, e -> this.prevButton.doClick()); |
| 112 | + putKeyBindAction(KeyBinds.QUICK_FIND_DIALOG_NEXT, this.searchField, e -> this.nextButton.doClick()); |
| 113 | + putKeyBindAction( |
| 114 | + KeyBinds.QUICK_FIND_DIALOG_CLOSE, this, FocusCondition.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, |
| 115 | + e -> this.setVisible(false) |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments