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