diff --git a/diylc/diylc-core/src/main/java/org/diylc/core/IDynamicPropertySource.java b/diylc/diylc-core/src/main/java/org/diylc/core/IDynamicPropertySource.java index 1f48d97c..7ccc80bb 100644 --- a/diylc/diylc-core/src/main/java/org/diylc/core/IDynamicPropertySource.java +++ b/diylc/diylc-core/src/main/java/org/diylc/core/IDynamicPropertySource.java @@ -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 properties); + + void setComponent(IDIYComponent ownerObject); List getAvailableValues(); diff --git a/diylc/diylc-core/src/main/java/org/diylc/core/SwitchPositionPropertyValueSource.java b/diylc/diylc-core/src/main/java/org/diylc/core/SwitchPositionPropertyValueSource.java deleted file mode 100644 index ff94f52f..00000000 --- a/diylc/diylc-core/src/main/java/org/diylc/core/SwitchPositionPropertyValueSource.java +++ /dev/null @@ -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 getAvailableValues() { - if (component == null) { - throw new RuntimeException("Component not set"); - } - if (!(component instanceof ISwitch sw)) { - return List.of(); - } - int positionCount = sw.getPositionCount(); - List 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); - } -} diff --git a/diylc/diylc-library/src/main/java/org/diylc/components/guitar/LeverSwitch.java b/diylc/diylc-library/src/main/java/org/diylc/components/guitar/LeverSwitch.java index 168abc81..57200772 100644 --- a/diylc/diylc-library/src/main/java/org/diylc/components/guitar/LeverSwitch.java +++ b/diylc/diylc-library/src/main/java/org/diylc/components/guitar/LeverSwitch.java @@ -397,20 +397,7 @@ public class LeverSwitch extends AbstractAngledComponent properties; + private IDIYComponent component; + + @Override + public void setProperties(List properties) { + this.properties = properties; + } + + @Override + public void setComponent(IDIYComponent component) { + this.component = component; + } + + @Override + public List getAvailableValues() { + if (properties == null) { + return List.of(); + } + Optional 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 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); + } +} diff --git a/diylc/diylc-swing/src/main/java/org/diylc/swing/gui/editor/DynamicValueEditor.java b/diylc/diylc-swing/src/main/java/org/diylc/swing/gui/editor/DynamicValueEditor.java index df8417a0..501fbbdb 100644 --- a/diylc/diylc-swing/src/main/java/org/diylc/swing/gui/editor/DynamicValueEditor.java +++ b/diylc/diylc-swing/src/main/java/org/diylc/swing/gui/editor/DynamicValueEditor.java @@ -71,29 +71,35 @@ public class DynamicValueEditor extends JComboBox { 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() { diff --git a/diylc/diylc-swing/src/main/java/org/diylc/swing/gui/editor/PropertyEditorDialog.java b/diylc/diylc-swing/src/main/java/org/diylc/swing/gui/editor/PropertyEditorDialog.java index 10114a6c..0cd6a07a 100644 --- a/diylc/diylc-swing/src/main/java/org/diylc/swing/gui/editor/PropertyEditorDialog.java +++ b/diylc/diylc-swing/src/main/java/org/diylc/swing/gui/editor/PropertyEditorDialog.java @@ -76,6 +76,13 @@ public class PropertyEditorDialog extends ButtonDialog { this.properties = properties; this.defaultedProperties = new HashSet(); + // 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();