improves the editing of constants by representing the values in the selected number format

This commit is contained in:
hneemann 2020-07-24 08:02:23 +02:00
parent 5ebd7e9dc4
commit 98dc38540b
6 changed files with 72 additions and 10 deletions

View File

@ -5,6 +5,8 @@
*/ */
package de.neemann.digital.core; package de.neemann.digital.core;
import de.neemann.digital.core.io.InValue;
import static de.neemann.digital.core.ObservableValue.zMaskString; import static de.neemann.digital.core.ObservableValue.zMaskString;
/** /**
@ -28,6 +30,23 @@ public class Value {
this.highZ = 0; 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 observableValue) {
value = observableValue.getValue(); value = observableValue.getValue();
highZ = observableValue.getHighZ(); highZ = observableValue.getHighZ();

View File

@ -30,6 +30,7 @@ public class Key<VALUE> {
// Both are only used within a custom implemented component. // Both are only used within a custom implemented component.
private String name; private String name;
private String description; private String description;
private boolean adaptiveIntFormat;
/** /**
* Creates a new Key. * Creates a new Key.
@ -220,7 +221,7 @@ public class Key<VALUE> {
} }
/** /**
* Called if this setting needs a restart. * Called if the modification of this setting needs a restart.
* *
* @return this for chained calls * @return this for chained calls
*/ */
@ -238,6 +239,8 @@ public class Key<VALUE> {
/** /**
* Called if this setting needs a repaint. * 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 * @return this for chained calls
*/ */
@ -253,6 +256,25 @@ public class Key<VALUE> {
return requiresRepaint; 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<VALUE> 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 * Moves this key to the panel with the given id
* *

View File

@ -173,7 +173,7 @@ public final class Keys {
* The value of constants * The value of constants
*/ */
public static final Key<Long> VALUE public static final Key<Long> VALUE
= new Key<>("Value", 1L).allowGroupEdit(); = new Key<>("Value", 1L).setAdaptiveIntFormat().allowGroupEdit();
/** /**
* The default value of elements * The default value of elements
@ -185,7 +185,7 @@ public final class Keys {
* The default value of inputs * The default value of inputs
*/ */
public static final Key<InValue> INPUT_DEFAULT public static final Key<InValue> 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 * The default value of the dip switch

View File

@ -37,7 +37,7 @@ public interface Editor<T> {
* @param elementAttributes the attributes * @param elementAttributes the attributes
* @param dialog the containing dialog * @param dialog the containing dialog
*/ */
void addToPanel(EditorPanel panel, Key key, ElementAttributes elementAttributes, AttributeDialog dialog); void addToPanel(EditorPanel panel, Key<T> key, ElementAttributes elementAttributes, AttributeDialog dialog);
/** /**
* Used to enable/disable the component. * Used to enable/disable the component.

View File

@ -6,10 +6,7 @@
package de.neemann.digital.gui.components; package de.neemann.digital.gui.components;
import de.neemann.digital.analyse.expression.format.FormatToExpression; import de.neemann.digital.analyse.expression.format.FormatToExpression;
import de.neemann.digital.core.Bits; import de.neemann.digital.core.*;
import de.neemann.digital.core.Model;
import de.neemann.digital.core.NodeException;
import de.neemann.digital.core.SyncAccess;
import de.neemann.digital.core.element.*; import de.neemann.digital.core.element.*;
import de.neemann.digital.core.extern.Application; import de.neemann.digital.core.extern.Application;
import de.neemann.digital.core.extern.PortDefinition; import de.neemann.digital.core.extern.PortDefinition;
@ -58,7 +55,7 @@ public final class EditorFactory {
* The single EditorFactory instance. * The single EditorFactory instance.
*/ */
static final EditorFactory INSTANCE = new EditorFactory(); static final EditorFactory INSTANCE = new EditorFactory();
private HashMap<Class<?>, Class<? extends Editor>> map = new HashMap<>(); private final HashMap<Class<?>, Class<? extends Editor>> map = new HashMap<>();
private EditorFactory() { private EditorFactory() {
add(String.class, StringEditor.class); add(String.class, StringEditor.class);
@ -124,7 +121,7 @@ public final class EditorFactory {
private JLabel label; private JLabel label;
@Override @Override
public void addToPanel(EditorPanel panel, Key key, ElementAttributes elementAttributes, AttributeDialog attributeDialog) { public void addToPanel(EditorPanel panel, Key<T> key, ElementAttributes elementAttributes, AttributeDialog attributeDialog) {
this.attributeDialog = attributeDialog; this.attributeDialog = attributeDialog;
label = new JLabel(key.getName() + ": "); label = new JLabel(key.getName() + ": ");
final String description = new LineBreaker().toHTML().breakLines(key.getDescription()); final String description = new LineBreaker().toHTML().breakLines(key.getDescription());
@ -400,6 +397,15 @@ public final class EditorFactory {
return comboBox; return comboBox;
} }
@Override
public void addToPanel(EditorPanel panel, Key<Long> 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 @Override
public Long getValue() throws EditorParseException { public Long getValue() throws EditorParseException {
Object item = comboBox.getSelectedItem(); Object item = comboBox.getSelectedItem();
@ -432,6 +438,15 @@ public final class EditorFactory {
comboBox.setSelectedItem(value.toString()); comboBox.setSelectedItem(value.toString());
} }
@Override
public void addToPanel(EditorPanel panel, Key<InValue> 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 @Override
public JComponent getComponent(ElementAttributes attr) { public JComponent getComponent(ElementAttributes attr) {
return comboBox; return comboBox;

View File

@ -5,6 +5,7 @@
*/ */
package de.neemann.digital.core; package de.neemann.digital.core;
import de.neemann.digital.core.io.InValue;
import junit.framework.TestCase; import junit.framework.TestCase;
public class ValueTest extends TestCase { public class ValueTest extends TestCase {
@ -16,4 +17,9 @@ public class ValueTest extends TestCase {
assertEquals(-1, new Value(3, 2).getValueSigned()); 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)));
}
} }