Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1664: Assembler text fields use listing font.

Dan94feaafa5931
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/assembler/AssembleDockingAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/assembler/AssembleDockingAction.java
index 7960b3b381..13821413dd 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/assembler/AssembleDockingAction.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/assembler/AssembleDockingAction.java
@@ -16,6 +16,7 @@
package ghidra.app.plugin.core.assembler;
import java.awt.Color;
+import java.awt.Font;
import java.awt.event.*;
import java.math.BigInteger;
import java.util.HashMap;
@@ -32,6 +33,7 @@ import docking.action.*;
import docking.widgets.autocomplete.*;
import docking.widgets.fieldpanel.*;
import docking.widgets.fieldpanel.support.FieldLocation;
+import ghidra.GhidraOptions;
import ghidra.app.context.ListingActionContext;
import ghidra.app.plugin.assembler.Assembler;
import ghidra.app.plugin.assembler.Assemblers;
@@ -40,6 +42,7 @@ import ghidra.app.plugin.core.codebrowser.CodeViewerProvider;
import ghidra.app.util.viewer.field.ListingField;
import ghidra.app.util.viewer.listingpanel.ListingModelAdapter;
import ghidra.app.util.viewer.listingpanel.ListingPanel;
+import ghidra.framework.options.ToolOptions;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.database.util.ProgramTransaction;
import ghidra.program.model.address.Address;
@@ -77,7 +80,7 @@ public class AssembleDockingAction extends DockingAction {
private Language lang;
private Assembler assembler;
private final MyListener listener = new MyListener();
- //private PluginTool tool;
+ private final PluginTool tool;
// Callback to keep the autocompleter positioned under the fields
private FieldPanelOverLayoutListener autoCompleteMover = (FieldPanelOverLayoutEvent ev) -> {
@@ -154,8 +157,13 @@ public class AssembleDockingAction extends DockingAction {
}
public AssembleDockingAction(PluginTool tool, String name, String owner) {
- this(name, owner);
- //this.tool = tool;
+ super(name, owner);
+ this.tool = tool;
+
+ String group = "Disassembly";
+ setPopupMenuData(new MenuData(new String[] { "Patch Instruction" }, group));
+ setKeyBindingData(new KeyBindingData(KEYBIND_ASSEMBLE));
+ setHelpLocation(new HelpLocation("AssemblerPlugin", "AssembleAction"));
// If I lose focus, cancel the assembly
input.addFocusListener(new FocusListener() {
@@ -215,14 +223,6 @@ public class AssembleDockingAction extends DockingAction {
codepane.requestFocusInWindow();
}
- private AssembleDockingAction(String name, String owner) {
- super(name, owner);
- String group = "Disassembly";
- setPopupMenuData(new MenuData(new String[] { "Patch Instruction" }, group));
- setKeyBindingData(new KeyBindingData(KEYBIND_ASSEMBLE));
- setHelpLocation(new HelpLocation("AssemblerPlugin", "AssembleAction"));
- }
-
/**
* Retrieve the location in the code viewer's {@link FieldPanel} for the field at the given
* address having the given header text
@@ -306,6 +306,12 @@ public class AssembleDockingAction extends DockingAction {
cache.get(lang).get(null);
assembler = Assemblers.getAssembler(prog);
+ ToolOptions displayOptions = tool.getOptions(GhidraOptions.CATEGORY_BROWSER_DISPLAY);
+ Font font = displayOptions.getFont(GhidraOptions.OPTION_BASE_FONT, null);
+ if (font != null) {
+ input.setFont(font);
+ }
+
input.setProgramLocation(cur);
FieldLocation locMnem = findFieldLocation(addr, "Mnemonic");
if (null == locMnem) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/assembler/AssemblyDualTextField.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/assembler/AssemblyDualTextField.java
index 9a636717d3..239b196d1d 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/assembler/AssemblyDualTextField.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/assembler/AssemblyDualTextField.java
@@ -49,6 +49,7 @@ import resources.ResourceManager;
/**
* A pair of text fields suitable for guided assembly
*
+ * <p>
* This object must be updated with program location information, so that it knows the applicable
* language and address. It then provides two text boxes: one for the mnemonic, and one for the
* operands. The two are linked so that the user can intuitively navigate between them as if they
@@ -56,6 +57,7 @@ import resources.ResourceManager;
* based syntax errors returned by the assembler. When a valid instruction is present, it provides
* the resulting instruction bytes.
*
+ * <p>
* To detect when the user has activated an instruction-bytes entry, add an
* {@link AutocompletionListener} and check that the selection is an {@link AssemblyInstruction}.
* Otherwise, the usual autocompletion behavior is applied automatically.
@@ -93,6 +95,7 @@ public class AssemblyDualTextField {
/**
* Get the foreground color for the item
+ *
* @return the color
*/
public Color getColor() {
@@ -101,6 +104,7 @@ public class AssemblyDualTextField {
/**
* Get the (possibly HTML) text to display for the item
+ *
* @return the text
*/
public String getDisplay() {
@@ -109,6 +113,7 @@ public class AssemblyDualTextField {
/**
* Get the text to insert when the item is activated
+ *
* @return the text
*/
public String getText() {
@@ -117,6 +122,7 @@ public class AssemblyDualTextField {
/**
* Override this to permit activation by default, i.e., on CTRL-SPACE
+ *
* @return true to permit defaulting, false to prevent it
*/
public boolean getCanDefault() {
@@ -167,6 +173,7 @@ public class AssemblyDualTextField {
/**
* Represents an encoding for a complete assembly instruction
*
+ * <p>
* These provide no insertion text, since their activation should be handled by a custom
* listener.
*/
@@ -184,6 +191,7 @@ public class AssemblyDualTextField {
/**
* Get the assembled instruction bytes
+ *
* @return the bytes
*/
public byte[] getData() {
@@ -208,7 +216,9 @@ public class AssemblyDualTextField {
/**
* Represents the description of an error encountered during parsing or assembling
- * NOTE: not used until error descriptions improve
+ *
+ * <p>
+ * <b>NOTE:</b> not used until error descriptions improve
*/
static class AssemblyError extends AssemblyCompletion {
private String text;
@@ -237,6 +247,7 @@ public class AssemblyDualTextField {
/**
* A customized autocompleter for assembly
*
+ * <p>
* This positions the list at the bottom left of the field(s), and considers the full text of
* the linked text boxes when retrieving the prefix. It also delegates the item styling to the
* item instances.
@@ -337,8 +348,11 @@ public class AssemblyDualTextField {
/**
* A listener which activates the autocompleter on ENTER (in addition to the default
- * CTRL-SPACE). Because the user must activate an entry to specify the desired assembly, we
- * make ENTER pull of the list, hinting that the user must make a selection.
+ * CTRL-SPACE).
+ *
+ * <p>
+ * Because the user must activate an entry to specify the desired assembly, we make ENTER pull
+ * up the list, hinting that the user must make a selection.
*/
class EnterKeyListener implements KeyListener {
@Override
@@ -404,8 +418,10 @@ public class AssemblyDualTextField {
/**
* Set the current program location
*
+ * <p>
* This may cause the construction of a new assembler, if one suitable for the given program's
* language has not yet been built.
+ *
* @param loc the location
*/
public void setProgramLocation(ProgramLocation loc) {
@@ -418,6 +434,7 @@ public class AssemblyDualTextField {
/**
* Specify the language and address without binding to a program
+ *
* @param lang the language
* @param addr the address
*/
@@ -431,6 +448,7 @@ public class AssemblyDualTextField {
/**
* For dual mode: Get the text field containing the mnemonic portion of the assembly
+ *
* @return the text field
*/
public JTextField getMnemonicField() {
@@ -439,6 +457,7 @@ public class AssemblyDualTextField {
/**
* For dual mode: Get the text field containing the operands portion of the assembly
+ *
* @return the text field
*/
public JTextField getOperandsField() {
@@ -455,8 +474,10 @@ public class AssemblyDualTextField {
/**
* Get a reference to the autocompleter
*
+ * <p>
* This is useful for adding the custom listener needed to detect activation of assembled
* instruction entries.
+ *
* @return the autocompleter
*/
public TextFieldAutocompleter<AssemblyCompletion> getAutocompleter() {
@@ -473,21 +494,26 @@ public class AssemblyDualTextField {
/**
* An enum type to specify which variant of the assembly input is shown.
- *
- * <ul>
- * <li>{@link #INVISIBLE} hides both variants. Nothing is shown.</li>
- * <li>{@link #DUAL_VISIBLE} shows the dual-box linked variant, suitable when the current
- * instruction has operands.</li>
- * <li>{@link #SINGLE_VISIBLE} shows the single-box unlinked variant, suitable when the
- * current instruction does not have operands.</li>
- * </ul>
*/
public enum VisibilityMode {
- INVISIBLE, DUAL_VISIBLE, SINGLE_VISIBLE;
+ /**
+ * Hide both variants. Nothing is shown.
+ */
+ INVISIBLE,
+ /**
+ * Show the dual-box linked variant, suitable when the current instruction has operands.
+ */
+ DUAL_VISIBLE,
+ /**
+ * Show the single-box unlinked variant, suitable when the current instruction has no
+ * operands.
+ */
+ SINGLE_VISIBLE;
}
/**
* Set the visibility of the text box(es)
+ *
* @param visibility the VisibilityMode to set.
*/
public void setVisible(VisibilityMode visibility) {
@@ -507,10 +533,23 @@ public class AssemblyDualTextField {
}
}
+ /**
+ * Set the font for all text fields
+ *
+ * @param font the new font
+ */
+ public void setFont(Font font) {
+ linker.setFont(font);
+ assembly.setFont(font);
+ }
+
/**
* Add a focus listener to the box(es)
*
- * NOTE: The listener will not fire when focus passes among the linked boxes of the dual variant.
+ * <p>
+ * <b>NOTE:</b> The listener will not fire when focus passes among the linked boxes of the dual
+ * variant.
+ *
* @param listener the listener
*/
public void addFocusListener(FocusListener listener) {
@@ -520,6 +559,7 @@ public class AssemblyDualTextField {
/**
* Add a key listener to the box(es)
+ *
* @param listener the listener
*/
public void addKeyListener(KeyListener listener) {
@@ -530,6 +570,7 @@ public class AssemblyDualTextField {
/**
* Get the full assembly text
+ *
* @return the text
*/
public String getText() {
@@ -541,6 +582,7 @@ public class AssemblyDualTextField {
/**
* Set the text of the visible field(s)
+ *
* @param text the text
*/
public void setText(String text) {
@@ -554,6 +596,7 @@ public class AssemblyDualTextField {
/**
* Set the caret position of the visible field(s)
+ *
* @param pos the position
*/
public void setCaretPosition(int pos) {
@@ -572,9 +615,11 @@ public class AssemblyDualTextField {
/**
* Sets the style of the text fields
- * @param field the field to configure
*
+ * <p>
* This is an extension point.
+ *
+ * @param field the field to configure
*/
protected void configureField(JTextField field) {
Font mono = new Font(Font.MONOSPACED, Font.PLAIN, 12); // TODO: Font size from options
@@ -583,15 +628,18 @@ public class AssemblyDualTextField {
/**
* Construct the HTML display for a given suggestion
+ *
+ * <p>
+ * This is an extension point.
+ *
+ * <p>
+ * Currently, this just shows the current prefix in bold, and the text that would be inserted as
+ * normal weight.
+ *
* @param prefix the text currently in the fields
* @param suggestion the text suggested by the assembly syntax analyzer
* @param bufferleft the portion of the prefix that is also part of the suggestion
* @return a formatted string that hints to the effect of selecting this suggestion
- *
- * This is an extension point.
- *
- * Currently, this just shows the current prefix in bold, and the text that would be inserted
- * as normal weight.
*/
protected String formatSuggestion(String prefix, String suggestion, String bufferleft) {
String extra = suggestion.substring(bufferleft.length());
@@ -602,18 +650,22 @@ public class AssemblyDualTextField {
/**
* Provides an ordering for assembled instructions appearing in the list
*
+ * <p>
* The items with the highest preference are positioned at the top of the list
- * @param rc a resolved instruction
- * @param existing the instruction, if any, currently under the user's cursor
- * @return a preference
*
+ * <p>
* This is an extension point.
*
- * Currently, a proposed instruction having the same constructor tree as the existing one is
- * the most preferred. Second, are instructions having a similar tree as the existing one --
- * "similar" is not yet well defined, but at the moment, it means their constructor tree
- * strings have a long common prefix. Third, instructions having the same encoded length as
- * the existing one are preferred. Last, the shortest instructions are preferred.
+ * <p>
+ * Currently, a proposed instruction having the same constructor tree as the existing one is the
+ * most preferred. Second, are instructions having a similar tree as the existing one --
+ * "similar" is not yet well defined, but at the moment, it means their constructor tree strings
+ * have a long common prefix. Third, instructions having the same encoded length as the existing
+ * one are preferred. Last, the shortest instructions are preferred.
+ *
+ * @param rc a resolved instruction
+ * @param existing the instruction, if any, currently under the user's cursor
+ * @return a preference
*/
protected int computePreference(AssemblyResolvedConstructor rc, Instruction existing) {
if (existing == null) {
@@ -632,16 +684,19 @@ public class AssemblyDualTextField {
/**
* Compute valid completions given the prefix
- * @param text the prefix
- * @return the collection of completion items
*
+ * <p>
* This is an extension point.
*
- * If text parses and assembles, then the completion set will include assembled
- * instruction-byte entries. Note that there may still be valid textual completions to continue
- * the instruction. The suggestions yielded by all syntax errors are used to create textual
- * completions. If the suggestion is prefixed by the buffer where the syntax error ocurred,
- * then, the tail of that suggestion is made into a completion entry.
+ * <p>
+ * If text parses and assembles, then the completion set will include assembled instruction-byte
+ * entries. Note that there may still be valid textual completions to continue the instruction.
+ * The suggestions yielded by all syntax errors are used to create textual completions. If the
+ * suggestion is prefixed by the buffer where the syntax error ocurred, then, the tail of that
+ * suggestion is made into a completion entry.
+ *
+ * @param text the prefix
+ * @return the collection of completion items
*/
protected Collection<AssemblyCompletion> computeCompletions(String text) {
final AssemblyPatternBlock ctx = assembler.getContextAt(addr);
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/fieldpanel/FieldPanelOverLayoutManager.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/fieldpanel/FieldPanelOverLayoutManager.java
index 7ea4bd90b9..789bf15fe1 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/fieldpanel/FieldPanelOverLayoutManager.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/fieldpanel/FieldPanelOverLayoutManager.java
@@ -15,19 +15,10 @@
*/
package docking.widgets.fieldpanel;
-import java.awt.Component;
-import java.awt.Container;
-import java.awt.Dimension;
-import java.awt.LayoutManager;
-import java.awt.LayoutManager2;
-import java.awt.Rectangle;
+import java.awt.*;
import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
+import java.util.*;
import java.util.List;
-import java.util.Map;
-import java.util.Set;
import docking.widgets.fieldpanel.field.Field;
import docking.widgets.fieldpanel.listener.LayoutListener;
@@ -35,15 +26,17 @@ import docking.widgets.fieldpanel.support.AnchoredLayout;
import docking.widgets.fieldpanel.support.FieldLocation;
/**
- * A {@link LayoutManager} that can be applied to a {@link FieldPanel}, allowing
- * {@link Component}s to be placed over a given field location.
+ * A {@link LayoutManager} that can be applied to a {@link FieldPanel}, allowing {@link Component}s
+ * to be placed over a given field location.
*
+ * <p>
* To apply it, use {@link Container#setLayout(LayoutManager)} to install it. In this case, the
* {@link Container} must be a {@link FieldPanel}. Then, use
* {@link Container#add(Component, Object)}, passing a {@link FieldLocation} as the constraint.
- * Currently, you must call {@link #layoutContainer(Container)} manually after you add or remove
- * any components.
+ * Currently, you must call {@link #layoutContainer(Container)} manually after you add or remove any
+ * components.
*
+ * <p>
* When this layout manager is removed from the {@link FieldPanel}, you should call
* {@link #unregister()} in order to dispose of internal resources.
*/
@@ -109,6 +102,7 @@ public class FieldPanelOverLayoutManager implements LayoutManager2 {
/**
* Add a component to be position over the given location
+ *
* @param loc the location of the field to occlude
* @param comp the component to be placed over the field
*/
@@ -121,6 +115,7 @@ public class FieldPanelOverLayoutManager implements LayoutManager2 {
/**
* Remove a component by its field location
+ *
* @param loc the location of the field
* @return true if there was a component placed at the given location
*/
@@ -135,6 +130,7 @@ public class FieldPanelOverLayoutManager implements LayoutManager2 {
/**
* Remove a component
+ *
* @param comp the component to remove
* @return true if the component existed
*/
@@ -156,6 +152,7 @@ public class FieldPanelOverLayoutManager implements LayoutManager2 {
/**
* Add a listener for overlay layout events
+ *
* @param listener the listener to add
*/
public void addLayoutListener(FieldPanelOverLayoutListener listener) {
@@ -164,6 +161,7 @@ public class FieldPanelOverLayoutManager implements LayoutManager2 {
/**
* Remove a listener for overlay layout events
+ *
* @param listener the listener to remove
*/
public void removeLayoutListener(FieldPanelOverLayoutListener listener) {
@@ -172,6 +170,7 @@ public class FieldPanelOverLayoutManager implements LayoutManager2 {
/**
* Get the list of register overlay layout event listeners
+ *
* @return the array
*/
public FieldPanelOverLayoutListener[] getLayoutListeners() {
@@ -188,6 +187,7 @@ public class FieldPanelOverLayoutManager implements LayoutManager2 {
/**
* Fire the given overlay layout event to all registered listeners
+ *
* @param ev
*/
protected void fireLayoutListeners(FieldPanelOverLayoutEvent ev) {
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/TextFieldLinker.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/TextFieldLinker.java
index 2e351ecfa3..6bc9da7149 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/TextFieldLinker.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/TextFieldLinker.java
@@ -16,6 +16,7 @@
package docking.widgets.textfield;
import java.awt.Component;
+import java.awt.Font;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
@@ -34,16 +35,17 @@ import org.apache.commons.lang3.StringUtils;
/**
* A class that links text fields into a "formatted text field", separated by expressions.
*
+ * <p>
* This fulfills a similar purpose to formatted text fields, except the individual parts may be
* placed independent of the other components. Granted, they ought to appear in an intuitive order.
* The input string is split among a collection of {@link JTextField}s each according to a given
* pattern -- excluding the final field. Cursor navigation, insertion, deletion, etc. are all
* applied as if the linked text fields were part of a single composite text field.
*
+ * <p>
* The individual text fields must be constructed and added by the user, as in the example:
*
* <pre>
- * {@code
* Box hbox = Box.createHorizontalBox();
* TextFieldLinker linker = new TextFieldLinker();
*
@@ -62,7 +64,6 @@ import org.apache.commons.lang3.StringUtils;
* linker.linkLastField(third);
*
* linker.setVisible(true);
- * }
* </pre>
*/
public class TextFieldLinker {
@@ -188,6 +189,7 @@ public class TextFieldLinker {
/**
* Copy the state
+ *
* @return the copy
*/
public LinkerState copy() {
@@ -202,6 +204,7 @@ public class TextFieldLinker {
/**
* Erase the state
*
+ * <p>
* Blank all the fields, and put the caret at the front of the first field.
*/
public void reset() {
@@ -231,8 +234,10 @@ public class TextFieldLinker {
/**
* Get the composite string, omitting the given separator.
*
+ * <p>
* This is used as a helper to delete the separator when backspace/delete is pressed at a
* boundary.
+ *
* @param omitSep the separator to omit, or -1 to omit nothing
* @return the text
*/
@@ -255,6 +260,7 @@ public class TextFieldLinker {
/**
* Get the composite caret location
+ *
* @return the location (including separators)
*/
public int getGlobalCaret() {
@@ -263,6 +269,7 @@ public class TextFieldLinker {
/**
* Get the composite caret location, omitting the given separator.
+ *
* @param omitSep the separator to omit, or -1 to omit nothing
* @return the location
*/
@@ -284,6 +291,7 @@ public class TextFieldLinker {
/**
* Get the composite text preceding the caret in the given field
+ *
* @param field the field whose caret to use
* @return the text
*/
@@ -310,9 +318,11 @@ public class TextFieldLinker {
/**
* Figure out whether the caret in the given field immediately proceeds a separator.
*
+ * <p>
* In other words, the caret must be to the far left (position 0), and the given field must
* not be the first field. If true, the caret immediately follows separator index
* {@code field - 1}.
+ *
* @param field the field index to check
* @return true if the caret immediately follows a separator.
*/
@@ -323,8 +333,10 @@ public class TextFieldLinker {
/**
* Figure out whether the caret in the given field immediately precedes a separator.
*
+ * <p>
* In other words, the caret must be to the far right, and the given field must not be the
* last field. If true, the caret immediately precedes separator index {@code field}.
+ *
* @param field the field index to check
* @return true if the caret immediately precedes a separator.
*/
@@ -339,8 +351,10 @@ public class TextFieldLinker {
/**
* Change focus to the given field as if navigating left.
*
+ * <p>
* The caret will be moved to the rightmost position, because we're moving left from the
* leftmost position of the field to the right.
+ *
* @param field the field index to be given focus.
*/
public void navigateFieldLeft(int field) {
@@ -352,8 +366,10 @@ public class TextFieldLinker {
/**
* Change focus to the given field as if navigating right.
*
+ * <p>
* The caret will be moved to the leftmost position, because we're moving right from the
* rightmost position of the field to the left.
+ *
* @param field the field index to be given focus.
*/
public void navigateFieldRight(int field) {
@@ -363,6 +379,7 @@ public class TextFieldLinker {
/**
* Remove the given separator from the composite text.
+ *
* @param sep the separator to remove, by index
*/
protected void removeSep(int sep) {
@@ -381,6 +398,7 @@ public class TextFieldLinker {
/**
* Set the composite text
+ *
* @param text the new text
*/
public int setText(String text) {
@@ -413,6 +431,7 @@ public class TextFieldLinker {
/**
* Set the composite caret location
+ *
* @param caret the new caret location
* @throws BadLocationException if the location exceeds the text length
*/
@@ -476,40 +495,45 @@ public class TextFieldLinker {
}
}
+ /**
+ * @see #linkField(JTextField, Pattern, String)
+ */
+ public void linkField(JTextField field, String exp, String sep) {
+ Pattern pat = Pattern.compile(exp);
+ linkField(field, pat, sep);
+ }
+
/**
* Add a new text field to this linker
*
- * Links the given field with the others present in this linker, if any. {@code exp} is a
- * regular expression that dictates where the given field ends, and the next field begins.
- * When {@code exp} matches a part of the text in {@code field}, the text is split and
- * re-flowed so that the second part is removed into the next linked field. The separator is
- * omitted from both fields. The packing of the fields -- and surrounding labels -- ought to
- * imply that the separator is still present, because {@link #getText()} or and
- * {@link #getTextBeforeCursor(JTextField)} insert {@code sep} between the fields.
+ * <p>
+ * Links the given field with the others present in this linker, if any. {@code pat} is a
+ * regular expression that dictates where the given field ends, and the next field begins. When
+ * {@code pat} matches a part of the text in {@code field}, the text is split and re-flowed so
+ * that the second part is moved into the next linked field. The separator is omitted from both
+ * fields. The fields should be positioned in order with labels between that display the
+ * expected separators, so that the user may understand what is happening. The
+ * {@link #getText()} and {@link #getTextBeforeCursor(JTextField)} methods will include
+ * {@code sep} between the fields.
*
- * Any number of fields may be added in this fashion, but the last field -- having no
- * associated pattern or separator -- must be added using
- * {@link #linkLastField(JTextField)}. Thus, before linking is actually activated, at least one
- * field must be present. To be meaningful, at least two fields should be linked.
+ * <p>
+ * Any number of fields may be added in this fashion, but the last field -- having no associated
+ * pattern or separator -- must be added using {@link #linkLastField(JTextField)}. Thus, before
+ * linking is actually activated, at least one field must be present. To be meaningful, at least
+ * two fields should be linked.
*
- * NOTE: {@code exp} must match {@code sep}.
+ * <p>
+ * <b>NOTE:</b> {@code pat} must accept {@code sep}, otherwise calling {@link #setText(String)}
+ * with the results of {@link #getText()} will have unexpected effects.
*
* @param field the field to link
* @param exp the separator following the field
* @param sep the separator that replaces {@code exp} when matched
*/
- public void linkField(JTextField field, String exp, String sep) {
- Pattern pat = Pattern.compile(exp);
- linkField(field, pat, sep);
- }
-
- /**
- * @see #linkField(JTextField, String, String)
- */
public void linkField(JTextField field, Pattern pat, String sep) {
checkLast();
if (!pat.matcher(sep).matches()) {
- throw new IllegalArgumentException(pat + " must match " + sep);
+ throw new IllegalArgumentException(pat + " must accept " + sep);
}
linkedFields.add(new LinkedField(field, pat, sep, linkedFields.size()));
}
@@ -517,8 +541,10 @@ public class TextFieldLinker {
/**
* Add the final field, and actually link the fields
*
+ * <p>
* The fields are not effectively linked until this method is called. Additionally, once this
* method is called, the linker cannot take any additional fields.
+ *
* @param field the final field
*/
public void linkLastField(JTextField field) {
@@ -539,6 +565,7 @@ public class TextFieldLinker {
/**
* Get the index of a field.
+ *
* @param field the field
* @return the index, or -1 if the field does not belong to this composite field
*/
@@ -553,6 +580,7 @@ public class TextFieldLinker {
/**
* Provides an opportunity to compose the field from an extension of {@link JTextField}
+ *
* @param i the index of the field to construct
* @return a newly-constructed text field
*/
@@ -563,8 +591,9 @@ public class TextFieldLinker {
/**
* A listener for all my callbacks
*
- * A separate listener is constructed and installed on each field so that we have a reference
- * to the field in every callback.
+ * <p>
+ * A separate listener is constructed and installed on each field so that we have a reference to
+ * the field in every callback.
*/
private class DualFieldListener extends KeyAdapter
implements CaretListener, FocusListener, DocumentListener {
@@ -725,6 +754,7 @@ public class TextFieldLinker {
/**
* Get the full composite text
+ *
* @return the text, including separators
*/
public String getText() {
@@ -733,6 +763,7 @@ public class TextFieldLinker {
/**
* Set the full composite text
+ *
* @param text the text, including separators
*/
public void setText(String text) {
@@ -743,8 +774,20 @@ public class TextFieldLinker {
}
}
+ /**
+ * Set the font for all linked fields
+ *
+ * @param font the new font
+ */
+ public void setFont(Font font) {
+ for (LinkedField lf : linkedFields) {
+ lf.field.setFont(font);
+ }
+ }
+
/**
* Set the location of the caret among the composite text
+ *
* @param pos the position, including separators
* @throws BadLocationException if the position is larger than the composite text
*/
@@ -758,6 +801,7 @@ public class TextFieldLinker {
/**
* Get the text preceding the caret in the given field
+ *
* @param where the field whose caret to consider
* @return the text
*/
@@ -771,8 +815,9 @@ public class TextFieldLinker {
/**
* Get an individual field in the composite
- * @param i
- * @return
+ *
+ * @param i the index of the field
+ * @return the field
*/
public JTextField getField(int i) {
return linkedFields.get(i).field;
@@ -781,8 +826,10 @@ public class TextFieldLinker {
/**
* Get the individual field last having focus
*
+ * <p>
* Effectively, this gives the field containing the composite caret
- * @return
+ *
+ * @return the last-focused field
*/
public JTextField getFocusedField() {
return getField(state.whichFocus);
@@ -790,6 +837,7 @@ public class TextFieldLinker {
/**
* Get the number of fields in this composite
+ *
* @return the field count
*/
public int getNumFields() {
@@ -798,6 +846,7 @@ public class TextFieldLinker {
/**
* Set the visibility of all the component fields
+ *
* @param visible true to show, false to hide
*/
public void setVisible(boolean visible) {
@@ -809,6 +858,7 @@ public class TextFieldLinker {
/**
* Add a focus listener
*
+ * <p>
* The focus listener will receive a callback only when focus is passed completely outside the
* composite text field. No events are generated when focus passes from one field in the
* composite to another.
@@ -821,6 +871,7 @@ public class TextFieldLinker {
/**
* Remove a focus listener
+ *
* @param listener the focus listener to remove
*/
public void removeFocusListener(FocusListener listener) {
@@ -829,7 +880,8 @@ public class TextFieldLinker {
/**
* Fire the given event on all registered focus listeners
- * @param ev
+ *
+ * @param ev the event
*/
protected void fireFocusListeners(FocusEvent ev) {
switch (ev.getID()) {
@@ -850,6 +902,7 @@ public class TextFieldLinker {
/**
* A convenient factory to build two fields separated by spaces
+ *
* @return the linker containing two new linked {@link JTextField}s
*/
public static TextFieldLinker twoSpacedFields() {
@@ -858,5 +911,4 @@ public class TextFieldLinker {
linker.linkLastField(new JTextField());
return linker;
}
-
}