Merge remote-tracking branch

'origin/GP-867-dragonmacher-hint-text-field'
This commit is contained in:
ghidra1 2021-04-28 19:11:20 -04:00
commit 5234839b24
3 changed files with 23 additions and 113 deletions

View File

@ -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("<table start address>");
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));
}
}
}

View File

@ -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));
}
}

View File

@ -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);
}
}
}