Skip to content

Commit fa0d21b

Browse files
New feature : allow to change Swing look and feel
1 parent ca08478 commit fa0d21b

File tree

2 files changed

+120
-7
lines changed

2 files changed

+120
-7
lines changed

jdbc-perf-logger-gui/src/main/java/ch/sla/jdbcperflogger/console/ui/PerfLoggerGuiMain.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616
package ch.sla.jdbcperflogger.console.ui;
1717

1818
import java.awt.Dimension;
19-
import java.awt.EventQueue;
2019
import java.awt.Frame;
2120
import java.util.HashMap;
2221
import java.util.Iterator;
2322
import java.util.Map;
2423
import java.util.Map.Entry;
24+
import java.util.prefs.BackingStoreException;
25+
import java.util.prefs.Preferences;
2526

27+
import javax.annotation.Nullable;
2628
import javax.swing.JPanel;
29+
import javax.swing.SwingUtilities;
2730
import javax.swing.ToolTipManager;
31+
import javax.swing.UIManager;
32+
import javax.swing.UnsupportedLookAndFeelException;
2833

2934
import org.slf4j.Logger;
3035
import org.slf4j.LoggerFactory;
@@ -38,30 +43,68 @@
3843
import ch.sla.jdbcperflogger.console.net.ServerLogReceiver;
3944

4045
public class PerfLoggerGuiMain implements IClientConnectionDelegate {
46+
private static final String LOOK_AND_FEEL_CLASS_NAME_PREF_KEY = "lookAndFeelClassName";
47+
4148
private final static Logger LOGGER = LoggerFactory.getLogger(PerfLoggerGuiMain.class);
4249

4350
private final PerfLoggerGuiMainFrame frmJdbcPerformanceLogger;
4451

4552
private final Map<String, PerfLoggerController> connectionsToLogController = new HashMap<>();
53+
private static final Preferences prefs = Preferences.userNodeForPackage(PerfLoggerGuiMain.class);
4654

4755
/**
4856
* Launch the application.
4957
*/
5058
public static void main(final String[] args) {
5159
LOGGER.debug("PerfLoggerGuiMain starting...");
52-
EventQueue.invokeLater(new Runnable() {
60+
SwingUtilities.invokeLater(new Runnable() {
5361
@Override
5462
public void run() {
63+
installLookAndFeel();
64+
5565
try {
5666
final PerfLoggerGuiMain window = new PerfLoggerGuiMain();
5767
window.frmJdbcPerformanceLogger.setVisible(true);
5868
} catch (final Exception e) {
5969
e.printStackTrace();
6070
}
6171
}
72+
6273
});
6374
}
6475

76+
private static void installLookAndFeel() {
77+
final String lfClassName = getPreferredLookAndFeel();
78+
try {
79+
if (lfClassName != null) {
80+
UIManager.setLookAndFeel(lfClassName);
81+
} else {
82+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
83+
}
84+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
85+
| UnsupportedLookAndFeelException e) {
86+
LOGGER.warn("Error setting LookAndFeel", e);
87+
}
88+
}
89+
90+
@Nullable
91+
static String getPreferredLookAndFeel() {
92+
return prefs.get(LOOK_AND_FEEL_CLASS_NAME_PREF_KEY, null);
93+
}
94+
95+
static void savePreferredLookAndFeel(final @Nullable String lfClassName) {
96+
if (lfClassName == null) {
97+
prefs.remove(LOOK_AND_FEEL_CLASS_NAME_PREF_KEY);
98+
} else {
99+
prefs.put(LOOK_AND_FEEL_CLASS_NAME_PREF_KEY, lfClassName);
100+
}
101+
try {
102+
prefs.sync();
103+
} catch (final BackingStoreException e) {
104+
throw new IllegalArgumentException(e);
105+
}
106+
}
107+
65108
/**
66109
* Create the application.
67110
*/

jdbc-perf-logger-gui/src/main/java/ch/sla/jdbcperflogger/console/ui/WelcomePanel.java

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package ch.sla.jdbcperflogger.console.ui;
1717

18+
import java.awt.Component;
1819
import java.awt.Cursor;
1920
import java.awt.GridBagConstraints;
2021
import java.awt.GridBagLayout;
@@ -25,21 +26,27 @@
2526
import java.awt.event.KeyEvent;
2627
import java.awt.event.MouseAdapter;
2728
import java.awt.event.MouseEvent;
29+
import java.beans.Beans;
2830
import java.util.TreeSet;
2931
import java.util.prefs.BackingStoreException;
3032
import java.util.prefs.Preferences;
3133

3234
import javax.annotation.Nullable;
35+
import javax.swing.DefaultComboBoxModel;
36+
import javax.swing.DefaultListCellRenderer;
3337
import javax.swing.ImageIcon;
3438
import javax.swing.JButton;
39+
import javax.swing.JComboBox;
3540
import javax.swing.JLabel;
3641
import javax.swing.JList;
42+
import javax.swing.JOptionPane;
3743
import javax.swing.JPanel;
3844
import javax.swing.JScrollPane;
3945
import javax.swing.JTextField;
4046
import javax.swing.JTextPane;
4147
import javax.swing.ListSelectionModel;
4248
import javax.swing.UIManager;
49+
import javax.swing.UIManager.LookAndFeelInfo;
4350
import javax.swing.border.TitledBorder;
4451
import javax.swing.event.HyperlinkEvent;
4552
import javax.swing.event.HyperlinkListener;
@@ -59,19 +66,82 @@ public class WelcomePanel extends JPanel {
5966
public WelcomePanel(final IClientConnectionDelegate clientConnectionCreator) {
6067
final GridBagLayout gridBagLayout = new GridBagLayout();
6168
gridBagLayout.columnWidths = new int[] { 438, 0 };
62-
gridBagLayout.rowHeights = new int[] { 46, 88, 0, 0 };
69+
gridBagLayout.rowHeights = new int[] { 0, 46, 88, 0, 0 };
6370
gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
64-
gridBagLayout.rowWeights = new double[] { 0.0, 1.0, 1.0, Double.MIN_VALUE };
71+
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE };
6572
setLayout(gridBagLayout);
6673

74+
final JPanel lookAndFeelPanel = new JPanel();
75+
lookAndFeelPanel.setBorder(new TitledBorder(null, "Look and Feel", TitledBorder.LEADING, TitledBorder.TOP,
76+
null, null));
77+
final GridBagConstraints gbc_lookAndFeelPanel = new GridBagConstraints();
78+
gbc_lookAndFeelPanel.insets = new Insets(0, 0, 5, 0);
79+
gbc_lookAndFeelPanel.fill = GridBagConstraints.BOTH;
80+
gbc_lookAndFeelPanel.gridx = 0;
81+
gbc_lookAndFeelPanel.gridy = 0;
82+
add(lookAndFeelPanel, gbc_lookAndFeelPanel);
83+
final GridBagLayout gbl_lookAndFeelPanel = new GridBagLayout();
84+
gbl_lookAndFeelPanel.columnWidths = new int[] { 263, 0, 0 };
85+
gbl_lookAndFeelPanel.rowHeights = new int[] { 0, 0 };
86+
gbl_lookAndFeelPanel.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
87+
gbl_lookAndFeelPanel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
88+
lookAndFeelPanel.setLayout(gbl_lookAndFeelPanel);
89+
90+
final JComboBox<LookAndFeelInfo> lookAndFeelsCombobox = new JComboBox<LookAndFeelInfo>();
91+
final GridBagConstraints gbc_lookAndFeelComboBox = new GridBagConstraints();
92+
gbc_lookAndFeelComboBox.fill = GridBagConstraints.HORIZONTAL;
93+
gbc_lookAndFeelComboBox.gridx = 0;
94+
gbc_lookAndFeelComboBox.gridy = 0;
95+
lookAndFeelPanel.add(lookAndFeelsCombobox, gbc_lookAndFeelComboBox);
96+
if (!Beans.isDesignTime()) {
97+
final LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels();
98+
final DefaultComboBoxModel<LookAndFeelInfo> lookAndFeelsCboxModel = new DefaultComboBoxModel<LookAndFeelInfo>(
99+
lookAndFeels);
100+
lookAndFeelsCboxModel.insertElementAt(null, 0);
101+
lookAndFeelsCombobox.setModel(lookAndFeelsCboxModel);
102+
final String initialPreferredLookAndFeel = PerfLoggerGuiMain.getPreferredLookAndFeel();
103+
if (initialPreferredLookAndFeel == null) {
104+
lookAndFeelsCombobox.setSelectedIndex(0);
105+
} else {
106+
for (int i = 0; i < lookAndFeels.length; i++) {
107+
if (lookAndFeels[i].getClassName().equals(initialPreferredLookAndFeel)) {
108+
lookAndFeelsCombobox.setSelectedIndex(1 + i);
109+
break;
110+
}
111+
}
112+
}
113+
}
114+
lookAndFeelsCombobox.setRenderer(new DefaultListCellRenderer() {
115+
@Override
116+
public Component getListCellRendererComponent(@Nullable final JList<?> list, @Nullable final Object value,
117+
final int index, final boolean isSelected, final boolean cellHasFocus) {
118+
if (value == null) {
119+
return super.getListCellRendererComponent(list, "(default)", index, isSelected, cellHasFocus);
120+
}
121+
final LookAndFeelInfo lfInfo = (LookAndFeelInfo) value;
122+
return super.getListCellRendererComponent(list, lfInfo.getName(), index, isSelected, cellHasFocus);
123+
}
124+
});
125+
lookAndFeelsCombobox.addActionListener(new ActionListener() {
126+
@Override
127+
public void actionPerformed(@Nullable final ActionEvent e) {
128+
@Nullable
129+
final LookAndFeelInfo selectedLf = (LookAndFeelInfo) lookAndFeelsCombobox.getSelectedItem();
130+
PerfLoggerGuiMain.savePreferredLookAndFeel(selectedLf != null ? selectedLf.getClassName() : null);
131+
132+
JOptionPane.showMessageDialog(WelcomePanel.this,
133+
"You need to relaunch the application to apply the new look and feel.");
134+
}
135+
});
136+
67137
final JPanel serverModePanel = new JPanel();
68138
serverModePanel.setBorder(new TitledBorder("Server mode"));
69139
final GridBagConstraints gbc_serverModePanel = new GridBagConstraints();
70140
gbc_serverModePanel.anchor = GridBagConstraints.NORTH;
71141
gbc_serverModePanel.fill = GridBagConstraints.HORIZONTAL;
72142
gbc_serverModePanel.insets = new Insets(0, 0, 5, 0);
73143
gbc_serverModePanel.gridx = 0;
74-
gbc_serverModePanel.gridy = 0;
144+
gbc_serverModePanel.gridy = 1;
75145
add(serverModePanel, gbc_serverModePanel);
76146
final GridBagLayout gbl_serverModePanel = new GridBagLayout();
77147
gbl_serverModePanel.columnWidths = new int[] { 204, 32, 0 };
@@ -101,7 +171,7 @@ public WelcomePanel(final IClientConnectionDelegate clientConnectionCreator) {
101171
gbc_clientModePanel.insets = new Insets(0, 0, 5, 0);
102172
gbc_clientModePanel.fill = GridBagConstraints.BOTH;
103173
gbc_clientModePanel.gridx = 0;
104-
gbc_clientModePanel.gridy = 1;
174+
gbc_clientModePanel.gridy = 2;
105175
add(clientModePanel, gbc_clientModePanel);
106176
final GridBagLayout gbl_clientModePanel = new GridBagLayout();
107177
gbl_clientModePanel.columnWidths = new int[] { 30, 74, 292, 0 };
@@ -229,7 +299,7 @@ public void mouseClicked(@Nullable final MouseEvent e) {
229299
final GridBagConstraints gbc_aboutPanel = new GridBagConstraints();
230300
gbc_aboutPanel.fill = GridBagConstraints.BOTH;
231301
gbc_aboutPanel.gridx = 0;
232-
gbc_aboutPanel.gridy = 2;
302+
gbc_aboutPanel.gridy = 3;
233303
add(aboutPanel, gbc_aboutPanel);
234304
final GridBagLayout gbl_aboutPanel = new GridBagLayout();
235305
gbl_aboutPanel.columnWidths = new int[] { 0, 0, 0 };

0 commit comments

Comments
 (0)