AttributeDialog works on a copy of the attributes

This commit is contained in:
hneemann 2017-05-30 17:54:26 +02:00
parent 63769c8443
commit 5518310bd2
6 changed files with 52 additions and 35 deletions

View File

@ -254,4 +254,16 @@ public class ElementAttributes {
public int hashCode() { public int hashCode() {
return attributes != null ? attributes.hashCode() : 0; return attributes != null ? attributes.hashCode() : 0;
} }
/**
* Checks if the values in both attributes are equal
*
* @param key the key
* @param other the other attribute set
* @param <VALUE> the type og the value
* @return true if both values are equal
*/
public <VALUE> boolean equalsKey(Key<VALUE> key, ElementAttributes other) {
return get(key).equals(other.get(key));
}
} }

View File

@ -43,7 +43,6 @@ import de.neemann.digital.lang.Lang;
import de.neemann.digital.testing.TestCaseElement; import de.neemann.digital.testing.TestCaseElement;
import de.neemann.digital.testing.TestingDataException; import de.neemann.digital.testing.TestingDataException;
import de.neemann.gui.*; import de.neemann.gui.*;
import de.neemann.gui.language.Language;
import javax.swing.*; import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileNameExtensionFilter;
@ -543,16 +542,15 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
ToolTipAction editSettings = new ToolTipAction(Lang.get("menu_editSettings")) { ToolTipAction editSettings = new ToolTipAction(Lang.get("menu_editSettings")) {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
final Language oldLang = Settings.getInstance().get(Keys.SETTINGS_LANGUAGE); ElementAttributes modified = new AttributeDialog(Main.this, Settings.SETTINGS_KEYS, Settings.getInstance().getAttributes()).showDialog();
final boolean oldIeeeShapes = Settings.getInstance().get(Keys.SETTINGS_IEEE_SHAPES); if (modified != null) {
if (new AttributeDialog(Main.this, Settings.SETTINGS_KEYS, Settings.getInstance().getAttributes()).showDialog()) { FormatToExpression.setDefaultFormat(modified.get(Keys.SETTINGS_EXPRESSION_FORMAT));
FormatToExpression.setDefaultFormat(Settings.getInstance().get(Keys.SETTINGS_EXPRESSION_FORMAT)); if (!Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_LANGUAGE, modified)
final Language newLang = Settings.getInstance().getAttributes().get(Keys.SETTINGS_LANGUAGE); || !Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_IEEE_SHAPES, modified)) {
final boolean newIeeeShapes = Settings.getInstance().get(Keys.SETTINGS_IEEE_SHAPES); Lang.setLanguage(modified.get(Keys.SETTINGS_LANGUAGE));
if (!newLang.equals(oldLang) || (oldIeeeShapes != newIeeeShapes)) {
Lang.setLanguage(newLang);
JOptionPane.showMessageDialog(Main.this, Lang.get("msg_restartNeeded")); JOptionPane.showMessageDialog(Main.this, Lang.get("msg_restartNeeded"));
} }
Settings.getInstance().getAttributes().getValuesFrom(modified);
} }
} }
}.setToolTip(Lang.get("menu_editSettings_tt")); }.setToolTip(Lang.get("menu_editSettings_tt"));

View File

@ -29,11 +29,12 @@ public class AttributeDialog extends JDialog {
private final JPanel panel; private final JPanel panel;
private final Component parent; private final Component parent;
private final Point pos; private final Point pos;
private final ElementAttributes elementAttributes; private final ElementAttributes originalAttributes;
private final ElementAttributes modifiedAttributes;
private final JPanel buttonPanel; private final JPanel buttonPanel;
private JComponent topMostTextComponent; private JComponent topMostTextComponent;
private VisualElement visualElement; private VisualElement visualElement;
private boolean changed = false; private boolean okPressed = false;
/** /**
* Creates a new instance * Creates a new instance
@ -58,7 +59,8 @@ public class AttributeDialog extends JDialog {
super(SwingUtilities.getWindowAncestor(parent), Lang.get("attr_dialogTitle"), ModalityType.APPLICATION_MODAL); super(SwingUtilities.getWindowAncestor(parent), Lang.get("attr_dialogTitle"), ModalityType.APPLICATION_MODAL);
this.parent = parent; this.parent = parent;
this.pos = pos; this.pos = pos;
this.elementAttributes = elementAttributes; this.originalAttributes = elementAttributes;
this.modifiedAttributes = new ElementAttributes(elementAttributes);
setDefaultCloseOperation(DISPOSE_ON_CLOSE); setDefaultCloseOperation(DISPOSE_ON_CLOSE);
panel = new JPanel(new DialogLayout()); panel = new JPanel(new DialogLayout());
@ -112,8 +114,8 @@ public class AttributeDialog extends JDialog {
* Closes the dialog and stores modified values * Closes the dialog and stores modified values
*/ */
public void fireOk() { public void fireOk() {
setEditedValues(elementAttributes); storeEditedValues();
changed = true; okPressed = true;
dispose(); dispose();
} }
@ -141,17 +143,20 @@ public class AttributeDialog extends JDialog {
return this; return this;
} }
private void setEditedValues(ElementAttributes attr) { /**
* store gui fields to attributes
*/
public void storeEditedValues() {
for (EditorHolder e : editors) for (EditorHolder e : editors)
e.setTo(attr); e.setTo(modifiedAttributes);
} }
/** /**
* shows the dialog * Shows the dialog
* *
* @return true if data was changed * @return the new attributes of null if nothing has changed
*/ */
public boolean showDialog() { public ElementAttributes showDialog() {
pack(); pack();
if (pos == null) if (pos == null)
@ -163,7 +168,10 @@ public class AttributeDialog extends JDialog {
SwingUtilities.invokeLater(() -> topMostTextComponent.requestFocusInWindow()); SwingUtilities.invokeLater(() -> topMostTextComponent.requestFocusInWindow());
setVisible(true); setVisible(true);
return changed; if (okPressed && !originalAttributes.equals(modifiedAttributes))
return modifiedAttributes;
else
return null;
} }
/** /**

View File

@ -324,11 +324,9 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
* @param parent the parent component * @param parent the parent component
*/ */
public void editCircuitAttributes(Component parent) { public void editCircuitAttributes(Component parent) {
ElementAttributes edited = new ElementAttributes(circuit.getAttributes()); ElementAttributes modifiedAttributes = new AttributeDialog(parent, ATTR_LIST, circuit.getAttributes()).showDialog();
if (new AttributeDialog(parent, ATTR_LIST, edited).showDialog()) { if (modifiedAttributes != null)
if (!edited.equals(circuit.getAttributes())) modify(circuit -> circuit.getAttributes().getValuesFrom(modifiedAttributes));
modify(circuit -> circuit.getAttributes().getValuesFrom(edited));
}
} }
/** /**
@ -823,10 +821,9 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
} }
}.setToolTip(Lang.get("attr_help_tt"))); }.setToolTip(Lang.get("attr_help_tt")));
ElementAttributes oldValues = new ElementAttributes(element.getElementAttributes()); ElementAttributes modified = attributeDialog.showDialog();
if (attributeDialog.showDialog()) if (modified != null)
if (!oldValues.equals(element.getElementAttributes())) modify(new ModifyAttributes(element, modified));
addModificationAlreadyMade(new ModifyAttributes(element));
} }
} catch (ElementNotFoundException ex) { } catch (ElementNotFoundException ex) {
// do nothing if element not found! // do nothing if element not found!

View File

@ -15,11 +15,12 @@ public class ModifyAttributes extends ModificationOfVisualElement {
/** /**
* Creates a new instance * Creates a new instance
* *
* @param ve the already modified element * @param ve the element to modify
* @param modified the new attributes
*/ */
public ModifyAttributes(VisualElement ve) { public ModifyAttributes(VisualElement ve, ElementAttributes modified) {
super(ve); super(ve);
attributes = new ElementAttributes(ve.getElementAttributes()); attributes = modified;
} }
@Override @Override

View File

@ -227,10 +227,11 @@ public class TableDialog extends JDialog {
final TreeMap<String, Integer> pins = model.getTable().getPins(); final TreeMap<String, Integer> pins = model.getTable().getPins();
if (pins.containsKey(name)) if (pins.containsKey(name))
attr.set(Keys.PIN, pins.get(name).toString()); attr.set(Keys.PIN, pins.get(name).toString());
if (new AttributeDialog(this, pos, LIST, attr).showDialog()) { ElementAttributes modified = new AttributeDialog(this, pos, LIST, attr).showDialog();
if (modified!=null) {
pins.remove(name); pins.remove(name);
final String newName = attr.get(Keys.LABEL).trim().replace(' ', '_'); final String newName = modified.get(Keys.LABEL).trim().replace(' ', '_');
final String pinStr = attr.get(Keys.PIN).trim(); final String pinStr = modified.get(Keys.PIN).trim();
if (pinStr.length() > 0) { if (pinStr.length() > 0) {
try { try {
int p = Integer.parseInt(pinStr); int p = Integer.parseInt(pinStr);