fixed dynamic loading

This commit is contained in:
Bane Stojkovic
2025-03-16 20:37:10 +01:00
parent 473630a5e8
commit f925093c80
6 changed files with 104 additions and 66 deletions
@@ -21,12 +21,16 @@
*/
package org.diylc.core;
import org.diylc.common.PropertyWrapper;
import java.util.List;
import java.util.Map;
public interface IDynamicPropertySource {
void setComponent(IDIYComponent<?> component);
void setProperties(List<PropertyWrapper> properties);
void setComponent(IDIYComponent<?> ownerObject);
List<Object> getAvailableValues();
@@ -1,38 +0,0 @@
package org.diylc.core;
import java.util.*;
public class SwitchPositionPropertyValueSource implements IDynamicPropertySource {
private IDIYComponent<?> component;
@Override
public void setComponent(IDIYComponent<?> component) {
this.component = component;
}
@Override
public List<Object> getAvailableValues() {
if (component == null) {
throw new RuntimeException("Component not set");
}
if (!(component instanceof ISwitch sw)) {
return List.of();
}
int positionCount = sw.getPositionCount();
List<Object> res = new ArrayList<>();
res.add(null);
for (int i = 0; i < positionCount; i++) {
res.add(i);
}
return res;
}
@Override
public String getDisplayValue(Object value) {
if (value == null) {
return "";
}
return "#" + (((Integer)value) + 1);
}
}
@@ -397,20 +397,7 @@ public class LeverSwitch extends AbstractAngledComponent<LeverSwitch.LeverSwitch
@Override
public int getPositionCount() {
switch (type) {
case DP3T:
return 3;
case DP4T:
return 4;
case DP3T_5pos:
case DP3T_5pos_Import:
case DP5T:
case _4P5T:
return 5;
case _6_WAY_OG:
return 6;
}
return 0;
return type.getPositionCount();
}
@Override
@@ -522,7 +509,7 @@ public class LeverSwitch extends AbstractAngledComponent<LeverSwitch.LeverSwitch
) // position 6
);
@DynamicEditableProperty(source = SwitchPositionPropertyValueSource.class)
@DynamicEditableProperty(source = LeverSwitchPositionPropertyValueSource.class)
@EditableProperty(name = "Selected Position")
@Override
public Integer getSelectedPosition() {
@@ -550,15 +537,26 @@ public class LeverSwitch extends AbstractAngledComponent<LeverSwitch.LeverSwitch
public static enum LeverSwitchType {
DP3T("DP3T (Standard 3-Position Strat)"), DP3T_5pos("DP3T (Standard 5-Position Strat)"), DP3T_5pos_Import("DP3T (Import 5-Position Strat)"),
_4P5T("4P5T (Super/Mega)"), DP4T("DP4T (4-Position Tele)"), _6_WAY_OG("DP4T (6-Position Oak Grigsby)"), DP5T("DP5T");
DP3T("DP3T (Standard 3-Position Strat)", 3),
DP3T_5pos("DP3T (Standard 5-Position Strat)", 5),
DP3T_5pos_Import("DP3T (Import 5-Position Strat)", 5),
_4P5T("4P5T (Super/Mega)", 5),
DP4T("DP4T (4-Position Tele)", 4),
_6_WAY_OG("DP4T (6-Position Oak Grigsby)", 6),
DP5T("DP5T", 5);
private final int positionCount;
private String title;
private LeverSwitchType(String title) {
private LeverSwitchType(String title, int positionCount) {
this.positionCount = positionCount;
this.title = title;
}
public int getPositionCount() {
return positionCount;
}
@Override
public String toString() {
return title;
@@ -0,0 +1,61 @@
package org.diylc.components.guitar;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDynamicPropertySource;
import org.diylc.core.ISwitch;
import java.util.*;
public class LeverSwitchPositionPropertyValueSource implements IDynamicPropertySource {
private List<PropertyWrapper> properties;
private IDIYComponent<?> component;
@Override
public void setProperties(List<PropertyWrapper> properties) {
this.properties = properties;
}
@Override
public void setComponent(IDIYComponent<?> component) {
this.component = component;
}
@Override
public List<Object> getAvailableValues() {
if (properties == null) {
return List.of();
}
Optional<PropertyWrapper> optionalPropertyWrapper = this.properties.stream().filter(p -> {
try {
return p.getGetter().getName().equals("getValue");
} catch (NoSuchMethodException e) {
return false;
}
}).findFirst();
if (optionalPropertyWrapper.isEmpty()) {
return List.of();
}
LeverSwitch.LeverSwitchType
switchType = (LeverSwitch.LeverSwitchType) optionalPropertyWrapper.get().getValue();
int positionCount = switchType.getPositionCount();
List<Object> res = new ArrayList<>();
res.add(null);
for (int i = 0; i < positionCount; i++) {
res.add(i);
}
return res;
}
@Override
public String getDisplayValue(Object value) {
if (value == null) {
return "None";
}
if (component instanceof ISwitch sw) {
return sw.getPositionName((Integer)value);
}
return "#" + (((Integer)value) + 1);
}
}
@@ -71,29 +71,35 @@ public class DynamicValueEditor extends JComboBox<Object> {
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
// TODO Auto-generated method stub
return super.getListCellRendererComponent(list, value == null ? null : LangUtil.translate(dynamicPropertySource.getDisplayValue(value)),
return super.getListCellRendererComponent(list, LangUtil.translate(dynamicPropertySource.getDisplayValue(value)),
index, isSelected, cellHasFocus);
}
});
setSelectedItem(property.getValue());
// Create a shared refresh action
final Runnable refreshAction = () -> {
if (getModel() instanceof DynamicComboBoxModel model) {
model.refreshValues();
}
};
addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
// Schedule the update just after the popup becomes visible
SwingUtilities.invokeLater(() -> {
if (getModel() instanceof DynamicComboBoxModel model) {
model.refreshValues();
}
});
refreshAction.run();
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {}
public void popupMenuCanceled(PopupMenuEvent e) {
}
});
addItemListener(new ItemListener() {
@@ -76,6 +76,13 @@ public class PropertyEditorDialog extends ButtonDialog {
this.properties = properties;
this.defaultedProperties = new HashSet<PropertyWrapper>();
// update dynamic property sources
// TODO consider better way to do this
properties.stream()
.map(x -> x.getDynamicPropertySource())
.filter(x -> x != null)
.forEach(source -> source.setProperties(properties));
setMinimumSize(new Dimension(240, 40));
layoutGui();