simplified the SingleValueDialog

This commit is contained in:
hneemann 2020-07-02 12:44:36 +02:00
parent 6a4741b05a
commit 08865eb529
2 changed files with 50 additions and 72 deletions

View File

@ -166,7 +166,7 @@ public class InputShape implements Shape {
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)); .setSelectedFormat(format);
dialog.setVisible(true); dialog.setVisible(true);
} else } else
dialog.requestFocus(); dialog.requestFocus();
@ -200,20 +200,4 @@ 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,33 +29,12 @@ 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")), 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(Lang.get("attr_dialogHighz")); HIGHZ(Lang.get("attr_dialogHighz"));
private final String langText; private final String langText;
@ -76,6 +55,22 @@ public final class SingleValueDialog extends JDialog implements ModelStateObserv
return Arrays.copyOf(values(), values().length - 1); return Arrays.copyOf(values(), values().length - 1);
} }
} }
private static InMode getByFormat(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;
}
}
} }
private final JTextField textField; private final JTextField textField;
@ -110,9 +105,7 @@ public final class SingleValueDialog extends JDialog implements ModelStateObserv
textField.setHorizontalAlignment(JTextField.RIGHT); textField.setHorizontalAlignment(JTextField.RIGHT);
formatComboBox = new JComboBox<>(InMode.values(supportsHighZ)); formatComboBox = new JComboBox<>(InMode.values(supportsHighZ));
formatComboBox.addActionListener(actionEvent -> { formatComboBox.addActionListener(actionEvent -> setLongToDialog(editValue));
setLongToDialog(editValue);
});
model.modify(() -> model.addObserver(this)); model.modify(() -> model.addObserver(this));
addWindowListener(new WindowAdapter() { addWindowListener(new WindowAdapter() {
@ -223,35 +216,32 @@ public final class SingleValueDialog extends JDialog implements ModelStateObserv
} }
private void setLongToDialog(long editValue) { private void setLongToDialog(long editValue) {
switch (getSelectedFormat()) { if (!textIsModifying) {
case ASCII: switch (getSelectedFormat()) {
char val = (char) (editValue); case ASCII:
setText("'" + val + "'"); char val = (char) (editValue);
textField.setCaretPosition(1); textField.setText("'" + val + "'");
break; textField.setCaretPosition(1);
case DECIMAL: break;
setText(Long.toString(editValue)); case DECIMAL:
break; textField.setText(Long.toString(editValue));
case HEX: break;
setText("0x" + Long.toHexString(editValue)); case HEX:
break; textField.setText("0x" + Long.toHexString(editValue));
case BIN: break;
setText("0b" + Long.toBinaryString(editValue)); case BIN:
break; textField.setText("0b" + Long.toBinaryString(editValue));
case OCTAL: break;
setText("0" + Long.toOctalString(editValue)); case OCTAL:
break; textField.setText("0" + Long.toOctalString(editValue));
case HIGHZ: break;
setText("?"); case HIGHZ:
break; textField.setText("?");
default: break;
default:
}
textField.requestFocus();
} }
textField.requestFocus();
}
private void setText(String text) {
if (!textIsModifying)
textField.setText(text);
} }
private InMode getSelectedFormat() { private InMode getSelectedFormat() {
@ -264,10 +254,14 @@ public final class SingleValueDialog extends JDialog implements ModelStateObserv
* @param format the format * @param format the format
* @return this for chained calls * @return this for chained calls
*/ */
public SingleValueDialog setSelectedFormat(InMode format) { public SingleValueDialog setSelectedFormat(IntFormat format) {
setSelectedFormat(InMode.getByFormat(format));
return this;
}
private void setSelectedFormat(InMode format) {
if (!getSelectedFormat().equals(format)) if (!getSelectedFormat().equals(format))
formatComboBox.setSelectedItem(format); formatComboBox.setSelectedItem(format);
return this;
} }
private void setStringToDialog(String text) { private void setStringToDialog(String text) {