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() {
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.TestingDataException;
import de.neemann.gui.*;
import de.neemann.gui.language.Language;
import javax.swing.*;
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")) {
@Override
public void actionPerformed(ActionEvent e) {
final Language oldLang = Settings.getInstance().get(Keys.SETTINGS_LANGUAGE);
final boolean oldIeeeShapes = Settings.getInstance().get(Keys.SETTINGS_IEEE_SHAPES);
if (new AttributeDialog(Main.this, Settings.SETTINGS_KEYS, Settings.getInstance().getAttributes()).showDialog()) {
FormatToExpression.setDefaultFormat(Settings.getInstance().get(Keys.SETTINGS_EXPRESSION_FORMAT));
final Language newLang = Settings.getInstance().getAttributes().get(Keys.SETTINGS_LANGUAGE);
final boolean newIeeeShapes = Settings.getInstance().get(Keys.SETTINGS_IEEE_SHAPES);
if (!newLang.equals(oldLang) || (oldIeeeShapes != newIeeeShapes)) {
Lang.setLanguage(newLang);
ElementAttributes modified = new AttributeDialog(Main.this, Settings.SETTINGS_KEYS, Settings.getInstance().getAttributes()).showDialog();
if (modified != null) {
FormatToExpression.setDefaultFormat(modified.get(Keys.SETTINGS_EXPRESSION_FORMAT));
if (!Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_LANGUAGE, modified)
|| !Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_IEEE_SHAPES, modified)) {
Lang.setLanguage(modified.get(Keys.SETTINGS_LANGUAGE));
JOptionPane.showMessageDialog(Main.this, Lang.get("msg_restartNeeded"));
}
Settings.getInstance().getAttributes().getValuesFrom(modified);
}
}
}.setToolTip(Lang.get("menu_editSettings_tt"));

View File

@ -29,11 +29,12 @@ public class AttributeDialog extends JDialog {
private final JPanel panel;
private final Component parent;
private final Point pos;
private final ElementAttributes elementAttributes;
private final ElementAttributes originalAttributes;
private final ElementAttributes modifiedAttributes;
private final JPanel buttonPanel;
private JComponent topMostTextComponent;
private VisualElement visualElement;
private boolean changed = false;
private boolean okPressed = false;
/**
* Creates a new instance
@ -58,7 +59,8 @@ public class AttributeDialog extends JDialog {
super(SwingUtilities.getWindowAncestor(parent), Lang.get("attr_dialogTitle"), ModalityType.APPLICATION_MODAL);
this.parent = parent;
this.pos = pos;
this.elementAttributes = elementAttributes;
this.originalAttributes = elementAttributes;
this.modifiedAttributes = new ElementAttributes(elementAttributes);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
panel = new JPanel(new DialogLayout());
@ -112,8 +114,8 @@ public class AttributeDialog extends JDialog {
* Closes the dialog and stores modified values
*/
public void fireOk() {
setEditedValues(elementAttributes);
changed = true;
storeEditedValues();
okPressed = true;
dispose();
}
@ -141,17 +143,20 @@ public class AttributeDialog extends JDialog {
return this;
}
private void setEditedValues(ElementAttributes attr) {
/**
* store gui fields to attributes
*/
public void storeEditedValues() {
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();
if (pos == null)
@ -163,7 +168,10 @@ public class AttributeDialog extends JDialog {
SwingUtilities.invokeLater(() -> topMostTextComponent.requestFocusInWindow());
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
*/
public void editCircuitAttributes(Component parent) {
ElementAttributes edited = new ElementAttributes(circuit.getAttributes());
if (new AttributeDialog(parent, ATTR_LIST, edited).showDialog()) {
if (!edited.equals(circuit.getAttributes()))
modify(circuit -> circuit.getAttributes().getValuesFrom(edited));
}
ElementAttributes modifiedAttributes = new AttributeDialog(parent, ATTR_LIST, circuit.getAttributes()).showDialog();
if (modifiedAttributes != null)
modify(circuit -> circuit.getAttributes().getValuesFrom(modifiedAttributes));
}
/**
@ -823,10 +821,9 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
}
}.setToolTip(Lang.get("attr_help_tt")));
ElementAttributes oldValues = new ElementAttributes(element.getElementAttributes());
if (attributeDialog.showDialog())
if (!oldValues.equals(element.getElementAttributes()))
addModificationAlreadyMade(new ModifyAttributes(element));
ElementAttributes modified = attributeDialog.showDialog();
if (modified != null)
modify(new ModifyAttributes(element, modified));
}
} catch (ElementNotFoundException ex) {
// do nothing if element not found!

View File

@ -15,11 +15,12 @@ public class ModifyAttributes extends ModificationOfVisualElement {
/**
* 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);
attributes = new ElementAttributes(ve.getElementAttributes());
attributes = modified;
}
@Override

View File

@ -227,10 +227,11 @@ public class TableDialog extends JDialog {
final TreeMap<String, Integer> pins = model.getTable().getPins();
if (pins.containsKey(name))
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);
final String newName = attr.get(Keys.LABEL).trim().replace(' ', '_');
final String pinStr = attr.get(Keys.PIN).trim();
final String newName = modified.get(Keys.LABEL).trim().replace(' ', '_');
final String pinStr = modified.get(Keys.PIN).trim();
if (pinStr.length() > 0) {
try {
int p = Integer.parseInt(pinStr);