From d5369f3782b0fad5c65b695d779467ea54edc6b9 Mon Sep 17 00:00:00 2001 From: Dolph Flynn <96876199+DolphFlynn@users.noreply.github.com> Date: Sat, 3 Aug 2024 09:36:40 +0100 Subject: [PATCH] Create RunEDTActionOnFirstRenderHierarchyListener. --- .../jwteditor/view/editor/EditorView.java | 28 ++++-------- .../PercentageBasedColumnWidthTable.java | 18 +------- ...TActionOnFirstRenderHierarchyListener.java | 44 +++++++++++++++++++ 3 files changed, 53 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/blackberry/jwteditor/view/utils/RunEDTActionOnFirstRenderHierarchyListener.java diff --git a/src/main/java/com/blackberry/jwteditor/view/editor/EditorView.java b/src/main/java/com/blackberry/jwteditor/view/editor/EditorView.java index f9ff2d4..0fb3993 100644 --- a/src/main/java/com/blackberry/jwteditor/view/editor/EditorView.java +++ b/src/main/java/com/blackberry/jwteditor/view/editor/EditorView.java @@ -27,6 +27,7 @@ import com.blackberry.jwteditor.view.rsta.RstaFactory; import com.blackberry.jwteditor.view.utils.ErrorLoggingActionListenerFactory; import com.blackberry.jwteditor.view.utils.MaxLengthStringComboBoxModel; +import com.blackberry.jwteditor.view.utils.RunEDTActionOnFirstRenderHierarchyListener; import org.exbin.deltahex.EditationAllowed; import org.exbin.deltahex.swing.CodeArea; import org.exbin.utils.binary_data.ByteArrayEditableData; @@ -38,14 +39,11 @@ import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import java.awt.*; -import java.awt.event.HierarchyEvent; -import java.awt.event.HierarchyListener; import java.util.List; import static java.awt.Color.RED; import static java.awt.EventQueue.invokeLater; import static java.awt.Font.BOLD; -import static java.awt.event.HierarchyEvent.SHOWING_CHANGED; import static org.exbin.deltahex.EditationAllowed.ALLOWED; import static org.exbin.deltahex.EditationAllowed.READ_ONLY; @@ -114,7 +112,13 @@ public abstract class EditorView { this.isProVersion = isProVersion; this.presenter = new EditorPresenter(this, collaboratorPayloadGenerator, actionListenerFactory, presenters); - panel.addHierarchyListener(new ResizeSplitPanesOnFirstRenderHierarchyListener()); + panel.addHierarchyListener(new RunEDTActionOnFirstRenderHierarchyListener( + panel, + () -> { + upperSplitPane.setDividerLocation(0.25); + lowerSplitPane.setDividerLocation(0.75); + } + )); // Event handler for Header / JWS payload change events DocumentListener documentListener = new DocumentListener() { @@ -549,20 +553,4 @@ public void setWarnings(String text) { labelWarnings.setText(text); }); } - - private class ResizeSplitPanesOnFirstRenderHierarchyListener implements HierarchyListener { - @Override - public void hierarchyChanged(HierarchyEvent e) { - if (e.getChangeFlags() != SHOWING_CHANGED || !e.getComponent().isShowing()) { - return; - } - - invokeLater(() -> { - upperSplitPane.setDividerLocation(0.25); - lowerSplitPane.setDividerLocation(0.75); - }); - - panel.removeHierarchyListener(this); - } - } } diff --git a/src/main/java/com/blackberry/jwteditor/view/utils/PercentageBasedColumnWidthTable.java b/src/main/java/com/blackberry/jwteditor/view/utils/PercentageBasedColumnWidthTable.java index 70169ae..0e7b08a 100644 --- a/src/main/java/com/blackberry/jwteditor/view/utils/PercentageBasedColumnWidthTable.java +++ b/src/main/java/com/blackberry/jwteditor/view/utils/PercentageBasedColumnWidthTable.java @@ -20,17 +20,13 @@ import javax.swing.*; import javax.swing.table.TableColumnModel; -import java.awt.event.HierarchyEvent; -import java.awt.event.HierarchyListener; - -import static java.awt.event.HierarchyEvent.SHOWING_CHANGED; public class PercentageBasedColumnWidthTable extends JTable { private final int[] columnWidthPercentages; public PercentageBasedColumnWidthTable(int[] columnWidthPercentages) { this.columnWidthPercentages = columnWidthPercentages; - addHierarchyListener(new ResizeColumnsOnFirstRenderHierarchyListener()); + addHierarchyListener(new RunEDTActionOnFirstRenderHierarchyListener(this, this::resizeColumns)); tableHeader.setReorderingAllowed(false); } @@ -49,16 +45,4 @@ private void resizeColumns() { columnModel.getColumn(i).setPreferredWidth(preferredWidth); } } - - private class ResizeColumnsOnFirstRenderHierarchyListener implements HierarchyListener { - @Override - public void hierarchyChanged(HierarchyEvent e) { - if (e.getChangeFlags() != SHOWING_CHANGED || !e.getComponent().isShowing()) { - return; - } - - resizeColumns(); - removeHierarchyListener(this); - } - } } diff --git a/src/main/java/com/blackberry/jwteditor/view/utils/RunEDTActionOnFirstRenderHierarchyListener.java b/src/main/java/com/blackberry/jwteditor/view/utils/RunEDTActionOnFirstRenderHierarchyListener.java new file mode 100644 index 0000000..91a2022 --- /dev/null +++ b/src/main/java/com/blackberry/jwteditor/view/utils/RunEDTActionOnFirstRenderHierarchyListener.java @@ -0,0 +1,44 @@ +/* +Author : Dolph Flynn + +Copyright 2024 Dolph Flynn + +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 com.blackberry.jwteditor.view.utils; + +import java.awt.*; +import java.awt.event.HierarchyEvent; +import java.awt.event.HierarchyListener; + +import static java.awt.EventQueue.invokeLater; +import static java.awt.event.HierarchyEvent.SHOWING_CHANGED; + +public class RunEDTActionOnFirstRenderHierarchyListener implements HierarchyListener { + private final Component component; + private final Runnable action; + + public RunEDTActionOnFirstRenderHierarchyListener(Component component, Runnable action) { + this.component = component; + this.action = action; + } + + @Override + public void hierarchyChanged(HierarchyEvent e) { + if (e.getChangeFlags() == SHOWING_CHANGED && e.getComponent().isShowing()) { + invokeLater(action); + component.removeHierarchyListener(this); + } + } +}