Skip to content

Commit

Permalink
Added possibility to change look&feel
Browse files Browse the repository at this point in the history
  • Loading branch information
mvmn committed Oct 8, 2020
1 parent 0070f5c commit 5fe12d8
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/main/java/x/mvmn/kafkagui/gui/ConnectionsManagerWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.FileOutputStream;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
Expand All @@ -14,6 +15,7 @@

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
Expand All @@ -22,7 +24,11 @@
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import x.mvmn.kafkagui.gui.util.JMenuBarBuilder;
import x.mvmn.kafkagui.gui.util.JMenuBarBuilder.JMenuBuilder;
import x.mvmn.kafkagui.gui.util.SwingUtil;
import x.mvmn.kafkagui.lang.Tuple;
import x.mvmn.kafkagui.model.KafkaConfigModel;
Expand All @@ -49,6 +55,17 @@ public ConnectionsManagerWindow(File appHomeFolder, SortedSet<String> existingCo
super("MVMn Kafka Client GUI");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JMenuBuilder menuBuilder = new JMenuBarBuilder().menu("Look&Feel");
String currentLnF = UIManager.getLookAndFeel().getName();
List<JCheckBoxMenuItem> lnfOptions = new ArrayList<>();
Arrays.stream(UIManager.getInstalledLookAndFeels()).map(LookAndFeelInfo::getName)
.forEach(lnf -> menuBuilder.item(lnf).checkbox().checked(currentLnF.equals(lnf)).actr(e -> {
SwingUtil.setLookAndFeel(lnf);
lnfOptions.forEach(mi -> mi.setState(lnf.equals(mi.getText())));
}).process(mi -> lnfOptions.add((JCheckBoxMenuItem) mi)).build());

this.setJMenuBar(menuBuilder.build().build());

this.appHomeFolder = appHomeFolder;

configs.add("New connection...");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/x/mvmn/kafkagui/gui/KafkaAdminGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public class KafkaAdminGui extends JFrame {
protected final DefaultMutableTreeNode topicsRootNode = new DefaultMutableTreeNode("Topics", true);
protected final DefaultTreeModel treeModel = new DefaultTreeModel(topicsRootNode);
protected final JTree topicsTree = new JTree(treeModel);
protected final JButton btnCreateTopic = new JButton("Create new topic");
protected final JButton btnDeleteTopic = new JButton("Delete topic");
protected final JButton btnCreateTopic = new JButton("Create");
protected final JButton btnDeleteTopic = new JButton("Delete");
protected final JPanel contentPanel = new JPanel(new BorderLayout());
protected final DefaultTableModel msgTableModel = new DefaultTableModel(new String[] { "Partition", "Offset", "Key", "Content" }, 0) {
private static final long serialVersionUID = -4104977444040382766L;
Expand Down
117 changes: 117 additions & 0 deletions src/main/java/x/mvmn/kafkagui/gui/util/JMenuBarBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package x.mvmn.kafkagui.gui.util;

import java.awt.event.ActionListener;
import java.util.function.Consumer;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

import lombok.Setter;
import lombok.experimental.Accessors;

public class JMenuBarBuilder {

private final JMenuBar menuBar = new JMenuBar();

public JMenuBuilder menu(String text) {
return new JMenuBuilder().text(text);
}

public JMenuBar build() {
return menuBar;
}

@Setter
@Accessors(fluent = true)
public class JMenuBuilder {
private final JMenu menu = new JMenu();

private String text;
private Action action;
private ActionListener actr;

public JMenuBarBuilder build() {
if (text != null) {
menu.setText(text);
}
if (action != null) {
menu.setAction(action);
}
if (actr != null) {
menu.addActionListener(actr);
}

menuBar.add(menu);
return JMenuBarBuilder.this;
}

public JMenuItemBuilder item(String text) {
return new JMenuItemBuilder().text(text);
}

public JMenuBuilder separator() {
menu.add(new JSeparator());
return this;
}

public JMenuBuilder vseparator() {
menu.add(new JSeparator(JSeparator.VERTICAL));
return this;
}

@Setter
@Accessors(fluent = true)
public class JMenuItemBuilder {
private String text;
private Action action;
private ActionListener actr;
private Icon icon;
private Integer mnemonic;
private boolean asCheckbox;
private boolean checked = false;
private Consumer<JMenuItem> process;

public JMenuItemBuilder checkbox() {
asCheckbox = true;
return this;
}

public JMenuBuilder build() {
JMenuItem mi;
if (asCheckbox) {
JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem();
cbmi.setState(checked);
mi = cbmi;
} else {
mi = new JMenuItem();
}
if (text != null) {
mi.setText(text);
}
if (action != null) {
mi.setAction(action);
}
if (actr != null) {
mi.addActionListener(actr);
}
if (icon != null) {
mi.setIcon(icon);
}
if (mnemonic != null) {
mi.setMnemonic(mnemonic);
}
if (process != null) {
process.accept(mi);
}

JMenuBuilder.this.menu.add(mi);
return JMenuBuilder.this;
}
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/x/mvmn/kafkagui/gui/util/SwingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.NumberFormat;
import java.util.Arrays;

import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.NumberFormatter;

import x.mvmn.kafkagui.lang.StackTraceUtil;
Expand Down Expand Up @@ -97,4 +100,21 @@ public static JTextField numericOnlyTextField(Long initialValue, Long min, Long

return txf;
}

public static void setLookAndFeel(String lookAndFeelName) {
Arrays.stream(UIManager.getInstalledLookAndFeels()).filter(lnf -> lnf.getName().equals(lookAndFeelName)).findAny()
.ifPresent(lnf -> {
try {
if (!UIManager.getLookAndFeel().getName().equals(lnf.getName())) {
UIManager.setLookAndFeel(lnf.getClassName());
Arrays.stream(Frame.getFrames()).forEach(frame -> {
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
});
}
} catch (Exception error) {
showError("Error setting look&feel to " + lookAndFeelName, error);
}
});
}
}

0 comments on commit 5fe12d8

Please sign in to comment.