the value dialog uses the format selected in the input

This commit is contained in:
hneemann 2020-07-01 19:20:36 +02:00
parent d51825caf8
commit 0c337407a9
2 changed files with 67 additions and 17 deletions

View File

@ -165,7 +165,8 @@ public class InputShape implements Shape {
} else { } else {
if (dialog == null || !dialog.isVisible()) { if (dialog == null || !dialog.isVisible()) {
Model model = ((In) element).getModel(); Model model = ((In) element).getModel();
dialog = new SingleValueDialog(model.getWindowPosManager().getMainFrame(), pos, label, value, isHighZ, model); dialog = new SingleValueDialog(model.getWindowPosManager().getMainFrame(), pos, label, value, isHighZ, model)
.setSelectedFormat(getFormat(format));
dialog.setVisible(true); dialog.setVisible(true);
} else } else
dialog.requestFocus(); dialog.requestFocus();
@ -199,4 +200,20 @@ public class InputShape implements Shape {
} }
} }
} }
private SingleValueDialog.InMode getFormat(IntFormat format) {
switch (format) {
case decSigned:
case dec:
return SingleValueDialog.InMode.DECIMAL;
case oct:
return SingleValueDialog.InMode.OCTAL;
case bin:
return SingleValueDialog.InMode.BIN;
case ascii:
return SingleValueDialog.InMode.ASCII;
default:
return SingleValueDialog.InMode.HEX;
}
}
} }

View File

@ -29,12 +29,33 @@ public final class SingleValueDialog extends JDialog implements ModelStateObserv
private final ObservableValue value; private final ObservableValue value;
private final SyncAccess syncAccess; private final SyncAccess syncAccess;
private enum InMode { /**
* The format used in the text field
*/
public enum InMode {
/**
* Hex format
*/
HEX(Lang.get("attr_dialogHex")), HEX(Lang.get("attr_dialogHex")),
/**
* decimal format
*/
DECIMAL(Lang.get("attr_dialogDecimal")), DECIMAL(Lang.get("attr_dialogDecimal")),
/**
* binary format
*/
BIN(Lang.get("attr_dialogBinary")),
/**
* octal format
*/
OCTAL(Lang.get("attr_dialogOctal")), OCTAL(Lang.get("attr_dialogOctal")),
/**
* ascii format
*/
ASCII(Lang.get("attr_dialogAscii")), ASCII(Lang.get("attr_dialogAscii")),
// highZ needs to be the last entry!! See InMode#values(boolean) /**
* highZ needs to be the last entry!! See InMode#values(boolean)
*/
HIGHZ(Lang.get("attr_dialogHighz")); HIGHZ(Lang.get("attr_dialogHighz"));
private final String langText; private final String langText;
@ -48,7 +69,7 @@ public final class SingleValueDialog extends JDialog implements ModelStateObserv
return langText; return langText;
} }
public static InMode[] values(boolean supportsHighZ) { private static InMode[] values(boolean supportsHighZ) {
if (supportsHighZ) { if (supportsHighZ) {
return values(); return values();
} else { } else {
@ -62,7 +83,6 @@ public final class SingleValueDialog extends JDialog implements ModelStateObserv
private final JComboBox<InMode> formatComboBox; private final JComboBox<InMode> formatComboBox;
private final long mask; private final long mask;
private JCheckBox[] checkBoxes; private JCheckBox[] checkBoxes;
private boolean programmaticModifyingFormat = false;
private long editValue; private long editValue;
/** /**
@ -90,7 +110,6 @@ public final class SingleValueDialog extends JDialog implements ModelStateObserv
formatComboBox = new JComboBox<>(InMode.values(supportsHighZ)); formatComboBox = new JComboBox<>(InMode.values(supportsHighZ));
formatComboBox.addActionListener(actionEvent -> { formatComboBox.addActionListener(actionEvent -> {
if (!programmaticModifyingFormat)
setLongToDialog(editValue); setLongToDialog(editValue);
}); });
@ -206,36 +225,48 @@ public final class SingleValueDialog extends JDialog implements ModelStateObserv
switch (getSelectedFormat()) { switch (getSelectedFormat()) {
case ASCII: case ASCII:
char val = (char) (editValue); char val = (char) (editValue);
textField.setText("'" + val + "'"); setText("'" + val + "'");
textField.setCaretPosition(1); textField.setCaretPosition(1);
break; break;
case DECIMAL: case DECIMAL:
textField.setText(Long.toString(editValue)); setText(Long.toString(editValue));
break; break;
case HEX: case HEX:
textField.setText("0x" + Long.toHexString(editValue)); setText("0x" + Long.toHexString(editValue));
break;
case BIN:
setText("0b" + Long.toBinaryString(editValue));
break; break;
case OCTAL: case OCTAL:
textField.setText("0" + Long.toOctalString(editValue)); setText("0" + Long.toOctalString(editValue));
break; break;
case HIGHZ: case HIGHZ:
textField.setText("?"); setText("?");
break; break;
default: default:
} }
textField.requestFocus(); textField.requestFocus();
} }
private void setText(String text) {
if (!textField.getText().equals(text))
textField.setText(text);
}
private InMode getSelectedFormat() { private InMode getSelectedFormat() {
return (InMode) formatComboBox.getSelectedItem(); return (InMode) formatComboBox.getSelectedItem();
} }
private void setSelectedFormat(InMode format) { /**
if (!getSelectedFormat().equals(format)) { * Sets the selected format
programmaticModifyingFormat = true; *
* @param format the format
* @return this for chained calls
*/
public SingleValueDialog setSelectedFormat(InMode format) {
if (!getSelectedFormat().equals(format))
formatComboBox.setSelectedItem(format); formatComboBox.setSelectedItem(format);
programmaticModifyingFormat = false; return this;
}
} }
private void setStringToDialog(String text) { private void setStringToDialog(String text) {
@ -254,6 +285,8 @@ public final class SingleValueDialog extends JDialog implements ModelStateObserv
} else { } else {
if (text.startsWith("0x")) if (text.startsWith("0x"))
setSelectedFormat(InMode.HEX); setSelectedFormat(InMode.HEX);
else if (text.startsWith("0b"))
setSelectedFormat(InMode.BIN);
else if (text.startsWith("0") && text.length() > 1) else if (text.startsWith("0") && text.length() > 1)
setSelectedFormat(InMode.OCTAL); setSelectedFormat(InMode.OCTAL);
else else