Skip to content

Commit

Permalink
Release 0.2.1 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
3dgiordano authored Apr 13, 2023
1 parent 6b6e803 commit e872c55
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.blazemeter</groupId>
<artifactId>jmeter-bzm-commons</artifactId>
<packaging>jar</packaging>
<version>0.2</version>
<version>0.2.1</version>
<name>BlazeMeter JMeter commons</name>
<description>This project holds common utilities used by BlazeMeter plugin projects.</description>
<url>https://github.com/Blazemeter/jmeter-bzm-commons</url>
Expand Down
104 changes: 102 additions & 2 deletions src/main/java/com/blazemeter/jmeter/commons/SwingUtils.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,91 @@
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 extends JComponent> T createComponent(String name, T component) {
component.setName(name);
return component;
}

public static <T extends JComponent> 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 <T> JList<T> buildJList(String name, DefaultListModel<T> model,
Dimension preferredSize) {
JList<T> 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;
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}

}

0 comments on commit e872c55

Please sign in to comment.