From e872c556e9d6745c540cbace0e4b863bb45776ad Mon Sep 17 00:00:00 2001 From: David Date: Thu, 13 Apr 2023 18:45:20 -0300 Subject: [PATCH] Release 0.2.1 (#3) --- pom.xml | 2 +- .../blazemeter/jmeter/commons/SwingUtils.java | 104 +++++++++++++++++- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 5655c02..0da633d 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.blazemeter jmeter-bzm-commons jar - 0.2 + 0.2.1 BlazeMeter JMeter commons This project holds common utilities used by BlazeMeter plugin projects. https://github.com/Blazemeter/jmeter-bzm-commons diff --git a/src/main/java/com/blazemeter/jmeter/commons/SwingUtils.java b/src/main/java/com/blazemeter/jmeter/commons/SwingUtils.java index a386f46..3508175 100644 --- a/src/main/java/com/blazemeter/jmeter/commons/SwingUtils.java +++ b/src/main/java/com/blazemeter/jmeter/commons/SwingUtils.java @@ -1,10 +1,25 @@ package com.blazemeter.jmeter.commons; +import java.awt.Color; +import java.awt.Component; +import java.awt.Desktop; +import java.awt.Dimension; import java.awt.event.ActionListener; +import java.io.IOException; +import java.net.URISyntaxException; +import javax.swing.DefaultListModel; +import javax.swing.GroupLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; +import javax.swing.JList; +import javax.swing.JScrollPane; +import javax.swing.JTextField; +import javax.swing.JTextPane; +import javax.swing.ListSelectionModel; +import javax.swing.event.HyperlinkEvent; import org.apache.jmeter.util.JMeterUtils; +import org.slf4j.Logger; public class SwingUtils { public static T createComponent(String name, T component) { @@ -12,12 +27,65 @@ public static T createComponent(String name, T component) return component; } + public static T createComponent(String name, T component, + Dimension minimumSize) { + component.setName(name); + component.setMinimumSize(minimumSize); + component.setPreferredSize(minimumSize); + return component; + } + + public static void setFieldMinimumAndPreferredColumns(JTextField field, int minCols, + int prefCols) { + field.setColumns(minCols); + field.setMinimumSize(field.getPreferredSize()); + field.setColumns(prefCols); + field.setMaximumSize(new Dimension(Short.MAX_VALUE, field.getPreferredSize().height)); + } + + public static JTextPane buildJTextPane(String name, Logger log) { + JTextPane pane = SwingUtils.createComponent(name, new JTextPane()); + pane.setContentType("text/html"); + pane.setEditable(false); + pane.addHyperlinkListener(e -> { + if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED && Desktop.isDesktopSupported()) { + try { + Desktop.getDesktop().browse(e.getURL().toURI()); + } catch (IOException | URISyntaxException ex) { + log.error("There was an issue trying to open the url {}", e.getURL(), ex); + } + } + }); + return pane; + } + + public static JList buildJList(String name, DefaultListModel model, + Dimension preferredSize) { + JList jList = SwingUtils.createComponent(name, new JList<>()); + jList.setPreferredSize(preferredSize); + jList.setModel(model); + jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + return jList; + } + + public static JScrollPane buildScrollPanel(Component list, String name, Dimension preferredSize) { + JScrollPane scroll = new JScrollPane(); + scroll.setName(name); + scroll.setViewportView(list); + scroll.createVerticalScrollBar(); + scroll.createHorizontalScrollBar(); + scroll.setPreferredSize(preferredSize); + return scroll; + } + public static final class ButtonBuilder { private ActionListener actionListener; private String action; private String name; private String iconName = ""; + private String text = ""; + private String toolTip = ""; private ImageIcon icon; private boolean enabled = true; private boolean hasText = true; @@ -61,6 +129,16 @@ public ButtonBuilder hasText(boolean hasText) { return this; } + public ButtonBuilder withText(String text) { + this.text = text; + return this; + } + + public ButtonBuilder withToolTip(String toolTip) { + this.toolTip = toolTip; + return this; + } + public JButton build() { String parsedName = JMeterUtils.getResString(name); @@ -70,10 +148,32 @@ public JButton build() { button.addActionListener(actionListener); button.setEnabled(enabled); button.setIcon(iconName.isEmpty() ? icon : ThemedIcon.fromResourceName(iconName)); - button.setText(!hasText ? "" - : parsedName.contains("res_key") ? StringUtils.capitalize(name) : parsedName); + if (text.isEmpty()) { + button.setText(!hasText ? "" + : parsedName.contains("res_key") ? StringUtils.capitalize(name) : parsedName); + } else { + button.setText(text); + } + + if (!toolTip.isEmpty()) { + button.setToolTipText(toolTip); + } + return button; } } + public static Color getEnabledForegroundColor(boolean enabled) { + JTextField field = new JTextField(); + return enabled ? field.getForeground() : field.getDisabledTextColor(); + } + + public static GroupLayout createGroupLayout(JComponent component) { + GroupLayout layout = new GroupLayout(component); + layout.setAutoCreateGaps(true); + layout.setAutoCreateContainerGaps(true); + component.setLayout(layout); + return layout; + } + }