From 98dc38540bcd649fcd2b4ffe2089d2458e841298 Mon Sep 17 00:00:00 2001 From: hneemann Date: Fri, 24 Jul 2020 08:02:23 +0200 Subject: [PATCH] improves the editing of constants by representing the values in the selected number format --- .../java/de/neemann/digital/core/Value.java | 19 +++++++++++++ .../de/neemann/digital/core/element/Key.java | 24 ++++++++++++++++- .../de/neemann/digital/core/element/Keys.java | 4 +-- .../digital/gui/components/Editor.java | 2 +- .../digital/gui/components/EditorFactory.java | 27 ++++++++++++++----- .../de/neemann/digital/core/ValueTest.java | 6 +++++ 6 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/neemann/digital/core/Value.java b/src/main/java/de/neemann/digital/core/Value.java index 0caa3a59f..9dbedca1c 100644 --- a/src/main/java/de/neemann/digital/core/Value.java +++ b/src/main/java/de/neemann/digital/core/Value.java @@ -5,6 +5,8 @@ */ package de.neemann.digital.core; +import de.neemann.digital.core.io.InValue; + import static de.neemann.digital.core.ObservableValue.zMaskString; /** @@ -28,6 +30,23 @@ public class Value { this.highZ = 0; } + /** + * Creates a new Value + * + * @param value the value + * @param bits the number of bits + */ + public Value(InValue value, int bits) { + this.bits = bits; + if (value.isHighZ()) { + this.value = 0; + this.highZ = Bits.mask(bits); + } else { + this.value = value.getValue() & Bits.mask(bits); + this.highZ = 0; + } + } + Value(ObservableValue observableValue) { value = observableValue.getValue(); highZ = observableValue.getHighZ(); diff --git a/src/main/java/de/neemann/digital/core/element/Key.java b/src/main/java/de/neemann/digital/core/element/Key.java index 008095485..13f5ae676 100644 --- a/src/main/java/de/neemann/digital/core/element/Key.java +++ b/src/main/java/de/neemann/digital/core/element/Key.java @@ -30,6 +30,7 @@ public class Key { // Both are only used within a custom implemented component. private String name; private String description; + private boolean adaptiveIntFormat; /** * Creates a new Key. @@ -220,7 +221,7 @@ public class Key { } /** - * Called if this setting needs a restart. + * Called if the modification of this setting needs a restart. * * @return this for chained calls */ @@ -238,6 +239,8 @@ public class Key { /** * Called if this setting needs a repaint. + * This means, that the circuit graphics became invalid + * if this setting has changed. * * @return this for chained calls */ @@ -253,6 +256,25 @@ public class Key { return requiresRepaint; } + /** + * Enables an adaptive int format in number editors. + * This means that the string representation of the number is controlled + * by the IntFormat stored in the elements attributes. + * + * @return this for chained calls + */ + public Key setAdaptiveIntFormat() { + adaptiveIntFormat = true; + return this; + } + + /** + * @return true if adaptive int format is required + */ + public boolean isAdaptiveIntFormat() { + return adaptiveIntFormat; + } + /** * Moves this key to the panel with the given id * diff --git a/src/main/java/de/neemann/digital/core/element/Keys.java b/src/main/java/de/neemann/digital/core/element/Keys.java index 3a26c7b52..a9555d89b 100644 --- a/src/main/java/de/neemann/digital/core/element/Keys.java +++ b/src/main/java/de/neemann/digital/core/element/Keys.java @@ -173,7 +173,7 @@ public final class Keys { * The value of constants */ public static final Key VALUE - = new Key<>("Value", 1L).allowGroupEdit(); + = new Key<>("Value", 1L).setAdaptiveIntFormat().allowGroupEdit(); /** * The default value of elements @@ -185,7 +185,7 @@ public final class Keys { * The default value of inputs */ public static final Key INPUT_DEFAULT - = new Key<>("InDefault", new InValue(0)).allowGroupEdit().setSecondary(); + = new Key<>("InDefault", new InValue(0)).setAdaptiveIntFormat().allowGroupEdit().setSecondary(); /** * The default value of the dip switch diff --git a/src/main/java/de/neemann/digital/gui/components/Editor.java b/src/main/java/de/neemann/digital/gui/components/Editor.java index de6932ead..e3813fbed 100644 --- a/src/main/java/de/neemann/digital/gui/components/Editor.java +++ b/src/main/java/de/neemann/digital/gui/components/Editor.java @@ -37,7 +37,7 @@ public interface Editor { * @param elementAttributes the attributes * @param dialog the containing dialog */ - void addToPanel(EditorPanel panel, Key key, ElementAttributes elementAttributes, AttributeDialog dialog); + void addToPanel(EditorPanel panel, Key key, ElementAttributes elementAttributes, AttributeDialog dialog); /** * Used to enable/disable the component. diff --git a/src/main/java/de/neemann/digital/gui/components/EditorFactory.java b/src/main/java/de/neemann/digital/gui/components/EditorFactory.java index c892637ad..b9d51b3b4 100644 --- a/src/main/java/de/neemann/digital/gui/components/EditorFactory.java +++ b/src/main/java/de/neemann/digital/gui/components/EditorFactory.java @@ -6,10 +6,7 @@ package de.neemann.digital.gui.components; import de.neemann.digital.analyse.expression.format.FormatToExpression; -import de.neemann.digital.core.Bits; -import de.neemann.digital.core.Model; -import de.neemann.digital.core.NodeException; -import de.neemann.digital.core.SyncAccess; +import de.neemann.digital.core.*; import de.neemann.digital.core.element.*; import de.neemann.digital.core.extern.Application; import de.neemann.digital.core.extern.PortDefinition; @@ -58,7 +55,7 @@ public final class EditorFactory { * The single EditorFactory instance. */ static final EditorFactory INSTANCE = new EditorFactory(); - private HashMap, Class> map = new HashMap<>(); + private final HashMap, Class> map = new HashMap<>(); private EditorFactory() { add(String.class, StringEditor.class); @@ -124,7 +121,7 @@ public final class EditorFactory { private JLabel label; @Override - public void addToPanel(EditorPanel panel, Key key, ElementAttributes elementAttributes, AttributeDialog attributeDialog) { + public void addToPanel(EditorPanel panel, Key key, ElementAttributes elementAttributes, AttributeDialog attributeDialog) { this.attributeDialog = attributeDialog; label = new JLabel(key.getName() + ": "); final String description = new LineBreaker().toHTML().breakLines(key.getDescription()); @@ -400,6 +397,15 @@ public final class EditorFactory { return comboBox; } + @Override + public void addToPanel(EditorPanel panel, Key key, ElementAttributes attr, AttributeDialog attributeDialog) { + if (key.isAdaptiveIntFormat()) { + Value value = new Value(attr.get(key), attr.getBits()); + comboBox.setSelectedItem(attr.get(Keys.INT_FORMAT).formatToEdit(value)); + } + super.addToPanel(panel, key, attr, attributeDialog); + } + @Override public Long getValue() throws EditorParseException { Object item = comboBox.getSelectedItem(); @@ -432,6 +438,15 @@ public final class EditorFactory { comboBox.setSelectedItem(value.toString()); } + @Override + public void addToPanel(EditorPanel panel, Key key, ElementAttributes attr, AttributeDialog attributeDialog) { + if (key.isAdaptiveIntFormat()) { + Value value = new Value(attr.get(key), attr.getBits()); + comboBox.setSelectedItem(attr.get(Keys.INT_FORMAT).formatToEdit(value)); + } + super.addToPanel(panel, key, attr, attributeDialog); + } + @Override public JComponent getComponent(ElementAttributes attr) { return comboBox; diff --git a/src/test/java/de/neemann/digital/core/ValueTest.java b/src/test/java/de/neemann/digital/core/ValueTest.java index 7aa78769d..02a5c85a2 100644 --- a/src/test/java/de/neemann/digital/core/ValueTest.java +++ b/src/test/java/de/neemann/digital/core/ValueTest.java @@ -5,6 +5,7 @@ */ package de.neemann.digital.core; +import de.neemann.digital.core.io.InValue; import junit.framework.TestCase; public class ValueTest extends TestCase { @@ -16,4 +17,9 @@ public class ValueTest extends TestCase { assertEquals(-1, new Value(3, 2).getValueSigned()); } + public void testFromInValue() throws Bits.NumberFormatException { + assertEquals("5", new Value(new InValue("5"), 4).toString()); + assertEquals("Z", new Value(new InValue("z"), 4).toString()); + assertEquals("?", IntFormat.hex.formatToEdit(new Value(new InValue("z"), 4))); + } }