Skip to content

Commit

Permalink
testdata in non modal dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
hneemann committed Jul 7, 2016
1 parent 8849ff9 commit 34c0091
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 33 deletions.
45 changes: 26 additions & 19 deletions src/main/java/de/neemann/digital/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ private Main(Component parent, File fileToOpen, SavedListener savedListener, Cir
final boolean normalMode = savedListener == null;

if (circuit != null) {
circuitComponent = new CircuitComponent(library, shapeFactory, savedListener);
circuitComponent = new CircuitComponent(this, library, shapeFactory, savedListener);
SwingUtilities.invokeLater(() -> circuitComponent.setCircuit(circuit));
} else {
circuitComponent = new CircuitComponent(library, shapeFactory, savedListener);
circuitComponent = new CircuitComponent(this, library, shapeFactory, savedListener);
if (fileToOpen != null) {
SwingUtilities.invokeLater(() -> loadFile(fileToOpen, false));
} else {
Expand Down Expand Up @@ -470,23 +470,7 @@ public void actionPerformed(ActionEvent e) {
ToolTipAction runTests = new ToolTipAction(Lang.get("menu_runTests"), ICON_TEST) {
@Override
public void actionPerformed(ActionEvent e) {
try {
ArrayList<TestResultDialog.TestSet> tsl = new ArrayList<>();
for (VisualElement el : circuitComponent.getCircuit().getElements())
if (el.equalsDescription(TestCaseElement.TESTCASEDESCRIPTION))
tsl.add(new TestResultDialog.TestSet(
el.getElementAttributes().get(TestCaseElement.TESTDATA),
el.getElementAttributes().getCleanLabel()));

if (tsl.isEmpty())
throw new DataException(Lang.get("err_noTestData"));

windowPosManager.register("testcase", new TestResultDialog(Main.this, tsl, circuitComponent.getCircuit(), library)).setVisible(true);

circuitComponent.getCircuit().clearState();
} catch (Exception e1) {
new ErrorMessage(Lang.get("msg_runningTestError")).addCause(e1).show();
}
startTests();
}
}.setToolTip(Lang.get("menu_runTests_tt"));

Expand Down Expand Up @@ -535,6 +519,29 @@ public void actionPerformed(ActionEvent e) {
toolBar.add(runTests.createJButtonNoText());
}

/**
* starts the tests
*/
public void startTests() {
try {
ArrayList<TestResultDialog.TestSet> tsl = new ArrayList<>();
for (VisualElement el : circuitComponent.getCircuit().getElements())
if (el.equalsDescription(TestCaseElement.TESTCASEDESCRIPTION))
tsl.add(new TestResultDialog.TestSet(
el.getElementAttributes().get(TestCaseElement.TESTDATA),
el.getElementAttributes().getCleanLabel()));

if (tsl.isEmpty())
throw new DataException(Lang.get("err_noTestData"));

windowPosManager.register("testcase", new TestResultDialog(Main.this, tsl, circuitComponent.getCircuit(), library)).setVisible(true);

circuitComponent.getCircuit().clearState();
} catch (Exception e1) {
new ErrorMessage(Lang.get("msg_runningTestError")).addCause(e1).show();
}
}

/**
* Creates the analyse menu
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public AttributeDialog(Component parent, Point pos, java.util.List<Key> list, El
for (Key key : list) {
Editor e = EditorFactory.INSTANCE.create(key, elementAttributes.get(key));
editors.add(new EditorHolder(e, key));
e.addToPanel(panel, key, elementAttributes);
e.addToPanel(panel, key, elementAttributes, this);
}

JButton okButton = new JButton(new AbstractAction(Lang.get("ok")) {
Expand Down Expand Up @@ -123,6 +123,13 @@ public boolean showDialog() {
return changed;
}

/**
* @return the dialogs parent
*/
public Component getDialogParent() {
return parent;
}

private static final class EditorHolder<T> {
private final Editor<T> e;
private final Key<T> key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class CircuitComponent extends JComponent {
public static final Icon ICON_DELETE = IconCreator.create("delete.png");
private static final String DEL_ACTION = "myDelAction";

private final Main parent;
private final ElementLibrary library;
private final SavedListener parentsSavedListener;
private final HashSet<Drawable> highLighted;
Expand Down Expand Up @@ -80,7 +81,8 @@ public class CircuitComponent extends JComponent {
* @param library the library used to edit the attributes of the elements
* @param shapeFactory the shapeFactory used for copied elements
*/
public CircuitComponent(ElementLibrary library, ShapeFactory shapeFactory, SavedListener parentsSavedListener) {
public CircuitComponent(Main parent, ElementLibrary library, ShapeFactory shapeFactory, SavedListener parentsSavedListener) {
this.parent = parent;
this.library = library;
this.parentsSavedListener = parentsSavedListener;
highLighted = new HashSet<>();
Expand Down Expand Up @@ -176,6 +178,13 @@ public void actionPerformed(ActionEvent e) {
setToolTipText("");
}

/**
* @return the main frame
*/
public Main getMain() {
return parent;
}

@Override
public String getToolTipText(MouseEvent event) {
Vector pos = getPosVector(event);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/de/neemann/digital/gui/components/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public interface Editor<T> {
* @param panel the panel to add the components to
* @param key the key which is to edit
* @param elementAttributes the attributes
* @param dialog the containing dialog
*/
void addToPanel(JPanel panel, Key key, ElementAttributes elementAttributes);
void addToPanel(JPanel panel, Key key, ElementAttributes elementAttributes, AttributeDialog dialog);
}
14 changes: 12 additions & 2 deletions src/main/java/de/neemann/digital/gui/components/EditorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ public <T> Editor<T> create(Key<T> key, T value) {
* @param <T> the type to edit
*/
public static abstract class LabelEditor<T> implements Editor<T> {
private AttributeDialog attributeDialog;

@Override
public void addToPanel(JPanel panel, Key key, ElementAttributes elementAttributes) {
public void addToPanel(JPanel panel, Key key, ElementAttributes elementAttributes, AttributeDialog attributeDialog) {
this.attributeDialog = attributeDialog;
JLabel label = new JLabel(key.getName() + ": ");
label.setToolTipText(key.getDescription());
panel.add(label, DialogLayout.LABEL);
Expand All @@ -86,6 +89,13 @@ public void addToPanel(JPanel panel, Key key, ElementAttributes elementAttribute
panel.add(component, DialogLayout.INPUT);
}

/**
* @return the containing dialog
*/
public AttributeDialog getAttributeDialog() {
return attributeDialog;
}

/**
* returns the editor component
*
Expand Down Expand Up @@ -174,7 +184,7 @@ public Boolean getValue() {
}

@Override
public void addToPanel(JPanel panel, Key key, ElementAttributes elementAttributes) {
public void addToPanel(JPanel panel, Key key, ElementAttributes elementAttributes, AttributeDialog attributeDialog) {
panel.add(bool, DialogLayout.BOTH);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package de.neemann.digital.gui.components.test;

import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.Key;
import de.neemann.digital.gui.components.CircuitComponent;
import de.neemann.digital.gui.components.table.ShowStringDialog;
import de.neemann.digital.lang.Lang;
import de.neemann.gui.ErrorMessage;
Expand All @@ -22,8 +25,23 @@ public class TestDataDialog extends JDialog {
* @param parent the parent component
* @param data the data to edit
*/
public TestDataDialog(JComponent parent, TestData data) {
super(SwingUtilities.getWindowAncestor(parent), Lang.get("key_Testdata"), ModalityType.APPLICATION_MODAL);
public TestDataDialog(Component parent, TestData data) {
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),
Lang.get("key_Testdata"),
key == null ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);

JTextArea text = new JTextArea(data.getDataString(), 30, 50);
Expand All @@ -46,6 +64,25 @@ public void actionPerformed(ActionEvent e) {
}
}.createJButton());

if (key!=null && elementAttributes!=null) {
buttons.add(new ToolTipAction(Lang.get("menu_runTests")) {
@Override
public void actionPerformed(ActionEvent e) {
try {
data.setDataString(text.getText());
elementAttributes.set(key, data);
if (parent instanceof CircuitComponent) {
CircuitComponent cc = (CircuitComponent) parent;
cc.getMain().startTests();
}
} catch (DataException e1) {
new ErrorMessage(e1.getMessage()).show(TestDataDialog.this);
}
}
}.createJButton());

}

buttons.add(new ToolTipAction(Lang.get("ok")) {
@Override
public void actionPerformed(ActionEvent e) {
Expand All @@ -63,4 +100,5 @@ public void actionPerformed(ActionEvent e) {
pack();
setLocationRelativeTo(parent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.Key;
import de.neemann.digital.core.memory.DataField;
import de.neemann.digital.gui.components.EditorFactory;
import de.neemann.digital.lang.Lang;
import de.neemann.gui.ToolTipAction;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
Expand All @@ -16,16 +16,17 @@
public class TestDataEditor extends EditorFactory.LabelEditor<TestData> {

private final TestData data;
private JButton editButton;
private final Key<TestData> key;

/**
* Creates a new editor
*
* @param data the data to edit
* @param key the data key
*/
public TestDataEditor(TestData data, Key<DataField> key) {
public TestDataEditor(TestData data, Key<TestData> key) {
this.data = new TestData(data);
this.key = key;
}

@Override
Expand All @@ -35,12 +36,24 @@ public TestData getValue() {

@Override
protected JComponent getComponent(ElementAttributes elementAttributes) {
editButton = new ToolTipAction(Lang.get("btn_edit")) {
JPanel panel = new JPanel(new FlowLayout());

panel.add(new ToolTipAction(Lang.get("btn_edit")) {
@Override
public void actionPerformed(ActionEvent e) {
new TestDataDialog(panel, data).setVisible(true);
}
}.createJButton());

panel.add(new ToolTipAction(Lang.get("btn_editDetached")) {
@Override
public void actionPerformed(ActionEvent e) {
new TestDataDialog(editButton, data).setVisible(true);
getAttributeDialog().dispose();
new TestDataDialog(getAttributeDialog().getDialogParent(), data, key, elementAttributes).setVisible(true);
}
}.createJButton();
return editButton;
}.setToolTip(Lang.get("btn_editDetached_tt"))
.createJButton());

return panel;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/lang/lang_de.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<string name="btn_save">Speichern</string>
<string name="btn_create">Erzeugen</string>
<string name="btn_create_tt">Erzeugt eine Schaltung in einem eigenen Fenster.</string>
<string name="btn_editDetached">Permanent Bearbeiten</string>
<string name="btn_editDetached_tt">Öffnet den Bearbeitendialog nicht modal.</string>
<string name="apply">Übernehmen</string>
<string name="cancel">Abbrechen</string>
<string name="digital">Digital</string>
<string name="expression">Ausdruck</string>
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/lang/lang_en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<string name="btn_save">Save</string>
<string name="btn_create">Create</string>
<string name="btn_create_tt">Create a circuit in a seperate window</string>
<string name="btn_editDetached">Edit detached</string>
<string name="btn_editDetached_tt">Opens the dialog as a non modal dialog</string>
<string name="apply">apply</string>
<string name="cancel">Cancel</string>
<string name="digital">Digital</string>
<string name="expression">Expression</string>
Expand Down

0 comments on commit 34c0091

Please sign in to comment.