From 745e2e3a9ab64a9ade920a3ca9d433d1b2ba566f Mon Sep 17 00:00:00 2001 From: dragonmacher <48328597+dragonmacher@users.noreply.github.com> Date: Mon, 26 Apr 2021 17:17:49 -0400 Subject: [PATCH] GP-867 - fixed hint text field text misalignment --- .../core/disassembler/AddressTableDialog.java | 32 +------- .../core/disassembler/HintTextField.java | 81 ------------------- .../widgets/textfield/HintTextField.java | 23 +++++- 3 files changed, 23 insertions(+), 113 deletions(-) delete mode 100644 Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/disassembler/HintTextField.java diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/disassembler/AddressTableDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/disassembler/AddressTableDialog.java index a6bfb6ef9e..05d5b5e63c 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/disassembler/AddressTableDialog.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/disassembler/AddressTableDialog.java @@ -29,6 +29,7 @@ import docking.action.DockingAction; import docking.widgets.checkbox.GCheckBox; import docking.widgets.label.GDLabel; import docking.widgets.label.GLabel; +import docking.widgets.textfield.HintTextField; import ghidra.app.events.ProgramSelectionPluginEvent; import ghidra.app.services.GoToService; import ghidra.app.util.HelpTopics; @@ -238,18 +239,11 @@ public class AddressTableDialog extends DialogComponentProvider { JLabel viewOffsetLabel = new GDLabel(" "); viewOffsetLabel.setEnabled(false); - viewOffset = new HintTextField(20); + viewOffset = new HintTextField(""); viewOffset.setName("viewOffset"); viewOffset.setToolTipText("Address of the selected table starting at the given offset"); - viewOffset.setHintText("table start address"); - viewOffset.showHint(); - - // We want the background of this text field to be the same as the panel, so it doesn't - // look like something editable. - Color panelColor = makeOptionsPanel.getBackground(); - viewOffset.setBackground( - new Color(panelColor.getRed(), panelColor.getGreen(), panelColor.getBlue())); - viewOffset.setEditable(false); + viewOffset.setColumns(20); + viewOffset.setEnabled(false); offsetField = new JTextField(2); offsetField.setName("offset"); @@ -555,22 +549,4 @@ public class AddressTableDialog extends DialogComponentProvider { addAction(selectionNavigationAction); addAction(selectAction); } - - private void doMakeSelection() { - Program program = plugin.getProgram(); - AddressSet set = new AddressSet(); - AutoTableDisassemblerModel model = plugin.getModel(); - int[] selectedRows = resultsTable.getSelectedRows(); - for (int selectedRow : selectedRows) { - Address selectedAddress = model.getAddress(selectedRow); - AddressTable addrTab = model.get(selectedAddress); - if (addrTab != null) { - set.addRange(selectedAddress, selectedAddress.add(addrTab.getByteLength() - 1)); - } - } - if (!set.isEmpty()) { - plugin.firePluginEvent(new ProgramSelectionPluginEvent(plugin.getName(), - new ProgramSelection(set), program)); - } - } } diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/disassembler/HintTextField.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/disassembler/HintTextField.java deleted file mode 100644 index d733f35700..0000000000 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/disassembler/HintTextField.java +++ /dev/null @@ -1,81 +0,0 @@ -/* ### - * IP: GHIDRA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package ghidra.app.plugin.core.disassembler; - -import java.awt.Color; -import java.awt.Font; - -import javax.swing.JTextField; - -/** - * Allows users to provide a text hint in a text field, shown only when the text is empty. - * - * Hint text will be shown in light grey, italicised, and in angle brackets. Normal text will - * be plain black. - */ -public class HintTextField extends JTextField { - private String hint; - private Color cForeground; - private Color cHint; - - public HintTextField(int cols) { - super(cols); - } - - public void setHintText(String s) { - this.hint = s; - } - - public void showHint() { - this.setText(""); - } - - @Override - public void setText(String text) { - - if (text != null && text.isEmpty()) { - setHintAttributes(); - super.setText("<" + hint + ">"); - } - else { - setPlainAttributes(); - super.setText(text); - } - } - - /** - * Sets the text attributes to be used when NOT viewing the hint. - */ - private void setPlainAttributes() { - this.setFont(getFont().deriveFont(Font.PLAIN)); - setForeground(Color.BLACK); - } - - /** - * Sets the text attributes to be used when viewing the hint. - */ - private void setHintAttributes() { - cForeground = getForeground(); - - if (cHint == null) { - cHint = new Color(cForeground.getRed(), cForeground.getGreen(), cForeground.getBlue(), - cForeground.getAlpha() / 2); - } - setForeground(cHint); - - this.setFont(getFont().deriveFont(Font.ITALIC)); - } -} diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/HintTextField.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/HintTextField.java index 7ed92cc045..122abd986c 100644 --- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/HintTextField.java +++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/HintTextField.java @@ -41,6 +41,7 @@ public class HintTextField extends JTextField { private Color INVALID_COLOR = new Color(255, 225, 225); private Color VALID_COLOR = Color.WHITE; + private Color defaultBackgroundColor; /** * Constructor @@ -128,10 +129,10 @@ public class HintTextField extends JTextField { g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - Rectangle bounds = getBounds(); + Dimension size = getSize(); + Insets insets = getInsets(); int x = 10; // offset - int y = bounds.height - bounds.y; // baseline of text; bottom of the text field - + int y = size.height - insets.bottom - 1; g2.drawString(hint, x, y); } @@ -145,6 +146,15 @@ public class HintTextField extends JTextField { this.required = required; } + /** + * Allows users to override the background color used by this field when the contents are + * valid. The invalid color is currently set by this class. + * @param color the color + */ + public void setDefaultBackgroundColor(Color color) { + this.defaultBackgroundColor = color; + } + /** * Returns true if the field contains valid input. * @@ -177,6 +187,11 @@ public class HintTextField extends JTextField { * field attributes. */ private void validateField() { - setBackground(isFieldValid() ? VALID_COLOR : INVALID_COLOR); + if (isFieldValid()) { + setBackground(defaultBackgroundColor == null ? VALID_COLOR : defaultBackgroundColor); + } + else { + setBackground(INVALID_COLOR); + } } }