diff --git a/trick_source/java/src/main/java/trick/common/ui/panels/FindBar.java b/trick_source/java/src/main/java/trick/common/ui/panels/FindBar.java index 2ebab2d1d..28b68be98 100644 --- a/trick_source/java/src/main/java/trick/common/ui/panels/FindBar.java +++ b/trick_source/java/src/main/java/trick/common/ui/panels/FindBar.java @@ -1,4 +1,3 @@ - //======================================== // Package //======================================== @@ -8,190 +7,282 @@ //Imports //======================================== -import java.awt.Color; -import java.awt.Component; -import java.awt.FlowLayout; +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.text.*; + +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.JCheckBox; -import javax.swing.JLabel; -import javax.swing.SwingConstants; - -import org.jdesktop.swingx.AbstractPatternPanel; -import org.jdesktop.swingx.JXFindBar; -import org.jdesktop.swingx.JXFindPanel; -import org.jdesktop.swingx.search.Searchable; - +import java.util.ArrayList; +import java.util.List; +import java.util.regex.MatchResult; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; +import org.jdesktop.swingx.JXEditorPane; /** - * A {@link JXFindBar} that is for allowing users to input search text. - * * @since Trick 10 + * A {@link JXFindBar} that allows users to input search text. + * This class extends JXFindBar, a SwingX component providing search bar functionality for a JXEditorPane. + * + * Notes for not extending JXFindBar: + * JXFindBar is a SwingX component extending JXFindPanel. + * JXFindPanel offers various search options, allowing users to input search text. + * When JXFindPanel performs a search, it calls the search method in the Searchable object. + * The Searchable search method is invoked when findNext or findPrevious is clicked. + * The JXEditorPane's Matcher calls toMatchResult() where the search text is used to find the match. + * The toMatchResult method is called in the Matcher class and creates a new Matcher, copying the state over. + * The toMatchResult method returns a {@link MatchResult} object. + * This works until JDK 9. + * JDK 9 introduces a new, non-public {@link java.util.regex.Matcher$ImmutableMatchResult} class that is the result of toMatchResult(). + * Therefore, a Matcher can't be cloned and the state can't be copied over. The following exception would be seen: + * java.lang.ClassCastException: class java.util.regex.Matcher$ImmutableMatchResult cannot be cast to class java.util.regex.Matcher + * (java.util.regex.Matcher$ImmutableMatchResult and java.util.regex.Matcher are in module java.base of loader 'bootstrap') + * + * @since Trick 19 + * This class no longer extends JXFindBar. It extends {@link JPanel} instead. + * FindBar is a custom JPanel that provides search functionality for a JXEditorPane. + * It allows users to search for text within the editor pane and navigate through the matches. + * + *
Usage example:
+ *+ * {@code + * JXEditorPane editorPane = new JXEditorPane(); + * FindBar findBar = new FindBar(editorPane); + * } + **/ -public class FindBar extends JXFindBar implements ActionListener{ - - //======================================== - // Public data - //======================================== - - - //======================================== - // Protected data - //======================================== - - - //======================================== - // Private Data - //======================================= - - private static final long serialVersionUID = 9092192049485321408L; - - /** The initial search text for this find bar */ - private String initialSearchText = null; - - /** By default, this would not have search options as JXFindPanel, true otherwise. */ - private boolean hasOptions = false; - - // TODO: add pattern support - //private JCheckBox anchorCheck; - - //======================================== - // Constructors - //======================================== - /** - * Default constructor. - */ - public FindBar() { - super(); - setNotFoundForegroundColor(); - } - - /** - * The constructor that specifies total number of popup menus for the panel. - * - * @param searchable An instance of {@link Searchable} from a gui component which - * this search bar is for. - */ - public FindBar(Searchable searchable) { - super(searchable); - setNotFoundForegroundColor(); - } - - //======================================== - // Set/Get methods - //======================================== - /** - * Gets to see if case sensitive is checked. - * @return true or false - */ - public boolean isCaseSensitive() { - return getPatternModel().isCaseSensitive(); - } - /** - * Sets whether to have the options as {@link JXFindPanel}. - * @param b true or false - */ - public void setOptions(boolean b) { - hasOptions = b; - } - - /** - * Updates the searchField that is defined in parent class {@link AbstractPatternPanel} - * if there is initial search text is defined. - * @param searchText searchText - */ - public void updateSearchField(String searchText) { - initialSearchText = searchText; - if (searchField != null) { - searchField.setText(searchText); - } - } - - /** - * Gets the current search text shown in searchField. - * @return the text - */ - public String getSearchText() { - if (searchField != null) { - return searchField.getText(); - } - return null; - } - - /** - * Helper method for changing the forground color when the text is not found. - * Since notFoundForegroundColor is proteced in {@link JXFindBar}, extending - * it is the only way to be able to change it. - * - */ - private void setNotFoundForegroundColor() { - notFoundForegroundColor = Color.red; - } - - - //======================================== - // Methods - //======================================== - @Override - protected void build() { - if (hasOptions) { - buildBarWithOption(); - } else { - buildBar(); - } - if (initialSearchText != null) { - updateSearchField(initialSearchText); - } - } - - private void buildBar() { - setLayout(new FlowLayout(SwingConstants.LEADING)); - add(searchLabel); - add(new JLabel(":")); - add(new JLabel(" ")); +public class FindBar extends JPanel { + + // ======================================== + // Public data + // ======================================== + + // ======================================== + // Protected data + // ======================================== + + // ======================================== + // Private Data + // ======================================= + + /** + * A text field for entering search queries. + */ + private JTextField searchField; + /** + * A button that, when pressed, triggers the action to find the next occurrence + * of the search term in the text or document. + */ + private JButton findNextButton; + /** + * JButton used to trigger the action of finding the previous occurrence + * in a search operation within the UI. + */ + private JButton findPreviousButton; + /** + * The JXEditorPane component used for displaying and editing text content. + */ + private JXEditorPane editorPane; + /** + * A list of integer positions. + * This list stores the positions of all find matches. + */ + private List
+ * The FindBar consists of a JTextField for entering the search text, and two buttons + * for navigating to the next and previous matches. It also highlights the current match + * in the editor pane. + *
+ * + *+ * Features: + *
+ *