Skip to content

Commit 2f39228

Browse files
committed
fix: handle dynamic font size support to BSBDropdownView with prototype-based sizing
- Add fontSizeListener to track font size changes - Add updateFontAndSize() method to update font and recalculate component size - Add getPrototypeItem() helper to find widest dropdown item for proper sizing - Register/unregister font size listener in addNotify()/removeNotify() - Replace manual size setting with updateFontAndSize() calls - Use setPrototypeDisplayValue() for accurate combo box width calculation
1 parent 5520d5b commit 2f39228

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

blue-ui-core/src/main/java/blue/orchestra/editor/blueSynthBuilder/swing/BSBDropdownView.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import blue.orchestra.blueSynthBuilder.BSBDropdownItem;
2424
import blue.ui.utilities.UiUtilities;
2525
import java.awt.BorderLayout;
26+
import java.awt.Dimension;
2627
import java.awt.event.ActionEvent;
2728
import java.awt.event.ActionListener;
2829
import java.beans.PropertyChangeEvent;
@@ -50,6 +51,8 @@ public class BSBDropdownView extends BSBObjectView<BSBDropdown> implements Prope
5051
private final ActionListener updateIndexListener;
5152

5253
private final ChangeListener<? super Number> indexListener;
54+
55+
private final ChangeListener<? super Number> fontSizeListener;
5356

5457
private boolean updating = false;
5558

@@ -71,13 +74,12 @@ public String getToolTipText() {
7174

7275
this.add(comboBox, BorderLayout.CENTER);
7376

74-
// comboBox.setSize(comboBox.getPreferredSize());
75-
this.setSize(comboBox.getPreferredSize());
76-
7777
int index = dropdown.getSelectedIndex();
7878
if (index < dropdown.dropdownItemsProperty().size()) {
7979
comboBox.setSelectedIndex(index);
8080
}
81+
82+
updateFontAndSize();
8183

8284
this.updateIndexListener = (ActionEvent e) -> {
8385
if (!updating) {
@@ -99,6 +101,10 @@ public String getToolTipText() {
99101

100102
}
101103
};
104+
105+
this.fontSizeListener = (obs, old, newVal) -> {
106+
UiUtilities.invokeOnSwingThread(this::updateFontAndSize);
107+
};
102108

103109
comboBox.addActionListener(updateIndexListener);
104110

@@ -112,7 +118,9 @@ public String getToolTipText() {
112118
public void addNotify() {
113119
super.addNotify();
114120
bsbObj.selectedIndexProperty().addListener(indexListener);
121+
bsbObj.fontSizeProperty().addListener(fontSizeListener);
115122
bsbObj.addPropertyChangeListener(this);
123+
SwingUtilities.invokeLater(this::updateFontAndSize);
116124

117125
ToolTipManager.sharedInstance().registerComponent(comboBox);
118126
}
@@ -122,6 +130,7 @@ public void addNotify() {
122130
public void removeNotify() {
123131
super.removeNotify();
124132
bsbObj.selectedIndexProperty().removeListener(indexListener);
133+
bsbObj.fontSizeProperty().removeListener(fontSizeListener);
125134
bsbObj.removePropertyChangeListener(this);
126135

127136
ToolTipManager.sharedInstance().unregisterComponent(comboBox);
@@ -135,8 +144,7 @@ public void propertyChange(PropertyChangeEvent evt) {
135144
model.refresh();
136145
comboBox.setModel(model);
137146
SwingUtilities.invokeLater(() -> {
138-
setSize(comboBox.getPreferredSize());
139-
revalidate();
147+
updateFontAndSize();
140148
});
141149

142150
bsbObj.setSelectedIndex(comboBox.getSelectedIndex());
@@ -145,6 +153,33 @@ public void propertyChange(PropertyChangeEvent evt) {
145153

146154
}
147155
}
156+
157+
private void updateFontAndSize() {
158+
comboBox.setFont(comboBox.getFont().deriveFont((float) bsbObj.getFontSize()));
159+
comboBox.setPrototypeDisplayValue(getPrototypeItem());
160+
Dimension preferredSize = comboBox.getPreferredSize();
161+
comboBox.setSize(preferredSize);
162+
setSize(preferredSize);
163+
revalidate();
164+
}
165+
166+
private BSBDropdownItem getPrototypeItem() {
167+
BSBDropdownItem prototype = null;
168+
int maxWidth = -1;
169+
var fontMetrics = comboBox.getFontMetrics(comboBox.getFont());
170+
171+
for (BSBDropdownItem item : bsbObj.dropdownItemsProperty()) {
172+
String text = item == null || item.getName() == null ? "" : item.getName();
173+
int width = fontMetrics.stringWidth(text);
174+
if (width > maxWidth) {
175+
maxWidth = width;
176+
prototype = new BSBDropdownItem();
177+
prototype.setName(text);
178+
}
179+
}
180+
181+
return prototype;
182+
}
148183

149184
}
150185

0 commit comments

Comments
 (0)