You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gr...@apache.org on 2023/10/01 10:49:14 UTC

[logging-chainsaw] 01/03: replaced complex logic for next button with factory/builder

This is an automated email from the ASF dual-hosted git repository.

grobmeier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-chainsaw.git

commit 1623493ae87b99d8a9b8ce304d0c79a931c254f3
Author: Christian Grobmeier <cg...@grobmeier.de>
AuthorDate: Sun Oct 1 12:48:30 2023 +0200

    replaced complex logic for next button with factory/builder
---
 .../java/org/apache/log4j/chainsaw/LogPanel.java   | 44 +++++++++-------------
 .../org/apache/log4j/chainsaw/SmallButton.java     | 30 ++++++++++++++-
 2 files changed, 46 insertions(+), 28 deletions(-)

diff --git a/src/main/java/org/apache/log4j/chainsaw/LogPanel.java b/src/main/java/org/apache/log4j/chainsaw/LogPanel.java
index 9fbc947..8539cf6 100644
--- a/src/main/java/org/apache/log4j/chainsaw/LogPanel.java
+++ b/src/main/java/org/apache/log4j/chainsaw/LogPanel.java
@@ -1043,16 +1043,8 @@ public class LogPanel extends DockablePanel implements ChainsawEventBatchListene
         upperPanel.add(findCombo);
         upperPanel.add(Box.createHorizontalStrut(3));
 
-        Action findNextAction = getFindNextAction();
         Action findPreviousAction = getFindPreviousAction();
         //add up & down search
-        JButton findNextButton = new SmallButton(findNextAction);
-        findNextButton.setText("");
-        findNextButton.getActionMap().put(
-            findNextAction.getValue(Action.NAME), findNextAction);
-        findNextButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
-            (KeyStroke) findNextAction.getValue(Action.ACCELERATOR_KEY),
-            findNextAction.getValue(Action.NAME));
 
         JButton findPreviousButton = new SmallButton(findPreviousAction);
         findPreviousButton.setText("");
@@ -1062,7 +1054,7 @@ public class LogPanel extends DockablePanel implements ChainsawEventBatchListene
             (KeyStroke) findPreviousAction.getValue(Action.ACCELERATOR_KEY),
             findPreviousAction.getValue(Action.NAME));
 
-        upperPanel.add(findNextButton);
+        upperPanel.add(createFindNextButton());
 
         upperPanel.add(findPreviousButton);
         upperPanel.add(Box.createHorizontalStrut(3));
@@ -1816,6 +1808,22 @@ public class LogPanel extends DockablePanel implements ChainsawEventBatchListene
         loadSettings();
     }
 
+    private SmallButton createFindNextButton() {
+        SmallButton findNextButton =new SmallButton.Builder()
+            .action(this::findNext)
+            .name("Find next")
+            .text("")
+            .smallIconUrl(ChainsawIcons.DOWN)
+            .shortDescription("Find the next occurrence of the rule from the current row")
+            .keyStroke(KeyStroke.getKeyStroke("F3"))
+            .build();
+        findNextButton.getActionMap()
+            .put(findNextButton.getActionName(), findNextButton.getAction());
+        findNextButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
+            .put(findNextButton.getActionAcceleratorKey(), findNextButton.getActionName());
+        return findNextButton;
+    }
+
     private String getValueOf(int row, int column) {
         if (currentTable == null) {
             return "";
@@ -1851,24 +1859,6 @@ public class LogPanel extends DockablePanel implements ChainsawEventBatchListene
         return "";
     }
 
-    private Action getFindNextAction() {
-        final Action action =
-            new AbstractAction("Find next") {
-                public void actionPerformed(ActionEvent e) {
-                    findNext();
-                }
-            };
-
-        //    action.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_F));
-        action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("F3"));
-        action.putValue(
-            Action.SHORT_DESCRIPTION,
-            "Find the next occurrence of the rule from the current row");
-        action.putValue(Action.SMALL_ICON, new ImageIcon(ChainsawIcons.DOWN));
-
-        return action;
-    }
-
     private Action getFindPreviousAction() {
         final Action action =
             new AbstractAction("Find previous") {
diff --git a/src/main/java/org/apache/log4j/chainsaw/SmallButton.java b/src/main/java/org/apache/log4j/chainsaw/SmallButton.java
index 4c79c19..8024d4d 100644
--- a/src/main/java/org/apache/log4j/chainsaw/SmallButton.java
+++ b/src/main/java/org/apache/log4j/chainsaw/SmallButton.java
@@ -62,6 +62,18 @@ public class SmallButton extends JButton implements MouseListener {
         addMouseListener(this);
     }
 
+    public Action getAction() {
+        return super.getAction();
+    }
+
+    public Object getActionName() {
+        return getAction().getValue(Action.NAME);
+    }
+
+    public KeyStroke getActionAcceleratorKey() {
+        return (KeyStroke)getAction().getValue(Action.ACCELERATOR_KEY);
+    }
+
     /**
      * {@inheritDoc}
      * Set to 0.5
@@ -142,6 +154,8 @@ public class SmallButton extends JButton implements MouseListener {
         Runnable action;
         String name = "";
 
+        String text = "";
+
         boolean enabled = true;
         Icon icon;
         URL iconUrl;
@@ -163,6 +177,12 @@ public class SmallButton extends JButton implements MouseListener {
             this.name = name;
             return this;
         }
+
+        Builder text(String text) {
+            this.text = text;
+            return this;
+        }
+
         Builder enabled() {
             this.enabled = true;
             return this;
@@ -209,7 +229,12 @@ public class SmallButton extends JButton implements MouseListener {
                 }
             });
 
-            button.setText(name);
+            if (text != null) {
+                button.setText(text);
+            } else if (name != null) {
+                button.setText(name);
+            }
+
             if (keyStroke != null) {
                 button.getAction().putValue(Action.ACCELERATOR_KEY, keyStroke);
             }
@@ -218,6 +243,9 @@ public class SmallButton extends JButton implements MouseListener {
             } else  if (iconUrl != null) {
                 button.setIcon(new ImageIcon(iconUrl));
             }
+            if (name != null) {
+                button.getAction().putValue(Action.NAME, name);
+            }
             if (smallIconUrl != null) {
                 button.getAction().putValue(Action.SMALL_ICON, new ImageIcon(smallIconUrl));
             }