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;
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();

View File

@ -30,6 +30,7 @@ public class Key<VALUE> {
// 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<VALUE> {
}
/**
* 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<VALUE> {
/**
* 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<VALUE> {
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
*

View File

@ -173,7 +173,7 @@ public final class Keys {
* The value of constants
*/
public static final Key<Long> 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<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

View File

@ -37,7 +37,7 @@ public interface Editor<T> {
* @param elementAttributes the attributes
* @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.

View File

@ -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<?>, Class<? extends Editor>> map = new HashMap<>();
private final HashMap<Class<?>, Class<? extends Editor>> 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<T> 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<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
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<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
public JComponent getComponent(ElementAttributes attr) {
return comboBox;

View File

@ -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)));
}
}