implemented undo for test data

This commit is contained in:
hneemann 2017-05-26 14:57:52 +02:00
parent cc69fa0096
commit 096ac11054
6 changed files with 81 additions and 39 deletions

View File

@ -239,4 +239,19 @@ public class ElementAttributes {
attributes = null; attributes = null;
fireValueChanged(); fireValueChanged();
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ElementAttributes that = (ElementAttributes) o;
return attributes != null ? attributes.equals(that.attributes) : that.attributes == null;
}
@Override
public int hashCode() {
return attributes != null ? attributes.hashCode() : 0;
}
} }

View File

@ -2,6 +2,7 @@ package de.neemann.digital.gui.components;
import de.neemann.digital.core.element.ElementAttributes; import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.Key; import de.neemann.digital.core.element.Key;
import de.neemann.digital.draw.elements.VisualElement;
import de.neemann.digital.gui.Main; import de.neemann.digital.gui.Main;
import de.neemann.digital.lang.Lang; import de.neemann.digital.lang.Lang;
import de.neemann.gui.ErrorMessage; import de.neemann.gui.ErrorMessage;
@ -30,6 +31,7 @@ public class AttributeDialog extends JDialog {
private final Point pos; private final Point pos;
private final ElementAttributes elementAttributes; private final ElementAttributes elementAttributes;
private final JPanel buttonPanel; private final JPanel buttonPanel;
private VisualElement visualElement;
private boolean changed = false; private boolean changed = false;
/** /**
@ -174,6 +176,24 @@ public class AttributeDialog extends JDialog {
return null; return null;
} }
/**
* @return the visual element of this dialog, maybe null
*/
public VisualElement getVisualElement() {
return visualElement;
}
/**
* Sets the visual element of this dialog
*
* @param visualElement the visual element which attributes are edited
* @return this for chained calls
*/
public AttributeDialog setVisualElement(VisualElement visualElement) {
this.visualElement = visualElement;
return this;
}
private static final class EditorHolder<T> { private static final class EditorHolder<T> {
private final Editor<T> e; private final Editor<T> e;
private final Key<T> key; private final Key<T> key;

View File

@ -3,10 +3,7 @@ package de.neemann.digital.gui.components;
import de.neemann.digital.core.NodeException; import de.neemann.digital.core.NodeException;
import de.neemann.digital.core.ObservableValue; import de.neemann.digital.core.ObservableValue;
import de.neemann.digital.core.Observer; import de.neemann.digital.core.Observer;
import de.neemann.digital.core.element.ElementTypeDescription; import de.neemann.digital.core.element.*;
import de.neemann.digital.core.element.ImmutableList;
import de.neemann.digital.core.element.Key;
import de.neemann.digital.core.element.Keys;
import de.neemann.digital.draw.elements.*; import de.neemann.digital.draw.elements.*;
import de.neemann.digital.gui.components.modification.*; import de.neemann.digital.gui.components.modification.*;
import de.neemann.digital.draw.graphics.*; import de.neemann.digital.draw.graphics.*;
@ -709,15 +706,15 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
hasChanged(); hasChanged();
} }
private void editAttributes(VisualElement vp, MouseEvent e) { private void editAttributes(VisualElement element, MouseEvent e) {
String name = vp.getElementName(); String name = element.getElementName();
try { try {
ElementTypeDescription elementType = library.getElementType(name); ElementTypeDescription elementType = library.getElementType(name);
ArrayList<Key> list = elementType.getAttributeList(); ArrayList<Key> list = elementType.getAttributeList();
if (list.size() > 0) { if (list.size() > 0) {
Point p = new Point(e.getX(), e.getY()); Point p = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(p, CircuitComponent.this); SwingUtilities.convertPointToScreen(p, CircuitComponent.this);
AttributeDialog attributeDialog = new AttributeDialog(this, p, list, vp.getElementAttributes()); AttributeDialog attributeDialog = new AttributeDialog(this, p, list, element.getElementAttributes()).setVisualElement(element);
if (elementType instanceof ElementLibrary.ElementTypeDescriptionCustom) { if (elementType instanceof ElementLibrary.ElementTypeDescriptionCustom) {
attributeDialog.addButton(Lang.get("attr_openCircuitLabel"), new ToolTipAction(Lang.get("attr_openCircuit")) { attributeDialog.addButton(Lang.get("attr_openCircuitLabel"), new ToolTipAction(Lang.get("attr_openCircuit")) {
@Override @Override
@ -737,14 +734,17 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
@Override @Override
public void actionPerformed(ActionEvent actionEvent) { public void actionPerformed(ActionEvent actionEvent) {
try { try {
new ElementHelpDialog(attributeDialog, elementType, vp.getElementAttributes()).setVisible(true); new ElementHelpDialog(attributeDialog, elementType, element.getElementAttributes()).setVisible(true);
} catch (PinException | NodeException e1) { } catch (PinException | NodeException e1) {
new ErrorMessage(Lang.get("msg_creatingHelp")).addCause(e1).show(CircuitComponent.this); new ErrorMessage(Lang.get("msg_creatingHelp")).addCause(e1).show(CircuitComponent.this);
} }
} }
}.setToolTip(Lang.get("attr_help_tt"))); }.setToolTip(Lang.get("attr_help_tt")));
ElementAttributes oldValues = new ElementAttributes(element.getElementAttributes());
if (attributeDialog.showDialog()) if (attributeDialog.showDialog())
addModificationAlreadyMade(new ModifyAttributes(vp)); if (!oldValues.equals(element.getElementAttributes()))
addModificationAlreadyMade(new ModifyAttributes(element));
} }
} catch (ElementNotFoundException ex) { } catch (ElementNotFoundException ex) {
// do nothing if element not found! // do nothing if element not found!

View File

@ -1,12 +1,13 @@
package de.neemann.digital.gui.components.testing; package de.neemann.digital.gui.components.testing;
import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.Key;
import de.neemann.digital.draw.elements.PinException; import de.neemann.digital.draw.elements.PinException;
import de.neemann.digital.draw.elements.VisualElement;
import de.neemann.digital.gui.Main; import de.neemann.digital.gui.Main;
import de.neemann.digital.gui.components.CircuitComponent; import de.neemann.digital.gui.components.CircuitComponent;
import de.neemann.digital.gui.components.modification.ModifyAttribute;
import de.neemann.digital.gui.components.table.ShowStringDialog; import de.neemann.digital.gui.components.table.ShowStringDialog;
import de.neemann.digital.lang.Lang; import de.neemann.digital.lang.Lang;
import de.neemann.digital.testing.TestCaseElement;
import de.neemann.digital.testing.TestData; import de.neemann.digital.testing.TestData;
import de.neemann.digital.testing.Transitions; import de.neemann.digital.testing.Transitions;
import de.neemann.digital.testing.parser.ParserException; import de.neemann.digital.testing.parser.ParserException;
@ -29,28 +30,19 @@ public class TestDataDialog extends JDialog {
/** /**
* Creates a new data dialog * Creates a new data dialog
* *
* @param parent the parent component * @param parent the parent component
* @param data the data to edit * @param data the data to edit
* @param element the element to be modified
*/ */
public TestDataDialog(Component parent, TestData data) { public TestDataDialog(Component parent, TestData data, VisualElement element) {
this(parent, data, null, null);
}
/**
* Creates a new data dialog
*
* @param parent the parent component
* @param data the data to edit
* @param key the key for the apply button
* @param elementAttributes the attributes to store the values
*/
public TestDataDialog(Component parent, TestData data, Key<TestData> key, ElementAttributes elementAttributes) {
super(SwingUtilities.getWindowAncestor(parent), super(SwingUtilities.getWindowAncestor(parent),
Lang.get("key_Testdata"), Lang.get("key_Testdata"),
key == null ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS); element == null ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);
setDefaultCloseOperation(DISPOSE_ON_CLOSE); setDefaultCloseOperation(DISPOSE_ON_CLOSE);
String initialDataString = data.getDataString();
JTextArea text = new JTextArea(data.getDataString(), 30, 50); JTextArea text = new JTextArea(data.getDataString(), 30, 50);
text.setFont(new Font(Font.MONOSPACED, Font.PLAIN, Screen.getInstance().getFontSize())); text.setFont(new Font(Font.MONOSPACED, Font.PLAIN, Screen.getInstance().getFontSize()));
@ -90,16 +82,15 @@ public class TestDataDialog extends JDialog {
}.setToolTip(Lang.get("btn_addTransitions_tt")).createJButton()); }.setToolTip(Lang.get("btn_addTransitions_tt")).createJButton());
} }
if (key != null && elementAttributes != null) { if (element != null) {
buttons.add(new ToolTipAction(Lang.get("menu_runTests")) { buttons.add(new ToolTipAction(Lang.get("menu_runTests")) {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
data.setDataString(text.getText()); data.setDataString(text.getText());
elementAttributes.set(key, data);
if (parent instanceof CircuitComponent) { if (parent instanceof CircuitComponent) {
element.getElementAttributes().set(TestCaseElement.TESTDATA, data);
CircuitComponent cc = (CircuitComponent) parent; CircuitComponent cc = (CircuitComponent) parent;
cc.getCircuit().modified();
cc.getMain().startTests(); cc.getMain().startTests();
} }
} catch (ParserException | IOException e1) { } catch (ParserException | IOException e1) {
@ -114,12 +105,11 @@ public class TestDataDialog extends JDialog {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
data.setDataString(text.getText()); data.setDataString(text.getText());
if (key != null && elementAttributes != null) { if (element != null
elementAttributes.set(key, data); && !initialDataString.equals(data.getDataString())
if (parent instanceof CircuitComponent) { && parent instanceof CircuitComponent) {
CircuitComponent cc = (CircuitComponent) parent; CircuitComponent cc = (CircuitComponent) parent;
cc.getCircuit().modified(); cc.modify(new ModifyAttribute<>(element, TestCaseElement.TESTDATA, new TestData(data)));
}
} }
dispose(); dispose();
} catch (ParserException | IOException e1) { } catch (ParserException | IOException e1) {

View File

@ -2,6 +2,7 @@ package de.neemann.digital.gui.components.testing;
import de.neemann.digital.core.element.ElementAttributes; import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.Key; import de.neemann.digital.core.element.Key;
import de.neemann.digital.draw.elements.VisualElement;
import de.neemann.digital.gui.Main; import de.neemann.digital.gui.Main;
import de.neemann.digital.gui.components.EditorFactory; import de.neemann.digital.gui.components.EditorFactory;
import de.neemann.digital.lang.Lang; import de.neemann.digital.lang.Lang;
@ -43,7 +44,7 @@ public class TestDataEditor extends EditorFactory.LabelEditor<TestData> {
panel.add(new ToolTipAction(Lang.get("btn_edit")) { panel.add(new ToolTipAction(Lang.get("btn_edit")) {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
new TestDataDialog(panel, data).setVisible(true); new TestDataDialog(panel, data, null).setVisible(true);
} }
}.createJButton()); }.createJButton());
@ -51,7 +52,8 @@ public class TestDataEditor extends EditorFactory.LabelEditor<TestData> {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
getAttributeDialog().fireOk(); getAttributeDialog().fireOk();
TestDataDialog dialog = new TestDataDialog(getAttributeDialog().getDialogParent(), data, key, elementAttributes); VisualElement visualElement = TestDataEditor.this.getAttributeDialog().getVisualElement();
TestDataDialog dialog = new TestDataDialog(getAttributeDialog().getDialogParent(), data, visualElement);
Main main = getAttributeDialog().getMain(); Main main = getAttributeDialog().getMain();
if (main != null) if (main != null)
main.getWindowPosManager().register("testdata", dialog); main.getWindowPosManager().register("testdata", dialog);

View File

@ -88,4 +88,19 @@ public class TestData {
check(); check();
return names; return names;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestData testData = (TestData) o;
return dataString != null ? dataString.equals(testData.dataString) : testData.dataString == null;
}
@Override
public int hashCode() {
return dataString != null ? dataString.hashCode() : 0;
}
} }