values shown on wires, some minor improvements on CircuitComponent

This commit is contained in:
hneemann 2016-03-23 19:36:43 +01:00
parent ea820d8ded
commit 72876b61a9
7 changed files with 60 additions and 40 deletions

View File

@ -51,7 +51,7 @@ public class ObservableValue extends Value {
if (highZ)
return "?";
else
return "0x" + Long.toHexString(value);
return "0x" + Long.toHexString(value).toUpperCase();
}

View File

@ -82,7 +82,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
@Override
public void actionPerformed(ActionEvent e) {
if (ClosingWindowListener.checkForSave(Main.this, Main.this)) {
JFileChooser fc = getjFileChooser();
JFileChooser fc = getjFileChooser(filename);
if (fc.showOpenDialog(Main.this) == JFileChooser.APPROVE_OPTION) {
loadFile(fc.getSelectedFile());
}
@ -93,7 +93,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
ToolTipAction saveas = new ToolTipAction("Save As") {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = getjFileChooser();
JFileChooser fc = getjFileChooser(filename);
if (fc.showSaveDialog(Main.this) == JFileChooser.APPROVE_OPTION) {
saveFile(fc.getSelectedFile());
}
@ -260,7 +260,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
}
}
private JFileChooser getjFileChooser() {
public static JFileChooser getjFileChooser(File filename) {
JFileChooser fileChooser = new JFileChooser(filename == null ? null : filename.getParentFile());
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Circuit", "dig"));
return fileChooser;

View File

@ -209,8 +209,11 @@ public class CircuitComponent extends JComponent implements Observer {
wire = new Wire(startPos, startPos);
repaint();
} else {
wire = null;
repaint();
if (wire != null) {
wire = null;
repaint();
} else
editAttributes(e);
}
}
@ -249,16 +252,15 @@ public class CircuitComponent extends JComponent implements Observer {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
if (partToInsert == null) {
Vector pos = getPosVector(e);
insert = false;
for (VisualElement vp : circuit.getParts())
if (vp.matches(pos)) {
partToInsert = vp;
partToInsert.setHighLight(true);
delta = partToInsert.getPos().sub(pos);
repaint();
break;
}
Vector pos = getPosVector(e);
VisualElement vp = circuit.getElementAt(pos);
if (vp != null) {
partToInsert = vp;
partToInsert.setHighLight(true);
delta = partToInsert.getPos().sub(pos);
repaint();
}
} else {
partToInsert.setPos(raster(partToInsert.getPos()));
if (insert)
@ -269,19 +271,7 @@ public class CircuitComponent extends JComponent implements Observer {
partToInsert = null;
}
} else {
Vector pos = getPosVector(e);
for (VisualElement vp : circuit.getParts())
if (vp.matches(pos)) {
String name = vp.getElementName();
ArrayList<AttributeKey> list = library.getElementType(name).getAttributeList();
if (list.size() > 0) {
Point p = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(p, CircuitComponent.this);
new AttributeDialog(p, list, vp.getElementAttributes()).showDialog();
circuit.modified();
repaint();
}
}
editAttributes(e);
}
}
@ -312,6 +302,23 @@ public class CircuitComponent extends JComponent implements Observer {
}
}
private boolean editAttributes(MouseEvent e) {
VisualElement vp = circuit.getElementAt(getPosVector(e));
if (vp != null) {
String name = vp.getElementName();
ArrayList<AttributeKey> list = library.getElementType(name).getAttributeList();
if (list.size() > 0) {
Point p = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(p, CircuitComponent.this);
new AttributeDialog(p, list, vp.getElementAttributes()).showDialog();
circuit.modified();
repaint();
return true;
}
}
return false;
}
private static enum State {COPY, MOVE}
private class SelectMouseListener extends Mouse {
@ -414,15 +421,14 @@ public class CircuitComponent extends JComponent implements Observer {
@Override
public void mouseClicked(MouseEvent e) {
Vector pos = getPosVector(e);
for (VisualElement vp : circuit.getParts())
if (vp.matches(pos)) {
Point p = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(p, CircuitComponent.this);
vp.clicked(CircuitComponent.this, p);
if (manualChangeObserver != null)
manualChangeObserver.hasChanged();
}
VisualElement ve = circuit.getElementAt(getPosVector(e));
if (ve != null) {
Point p = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(p, CircuitComponent.this);
ve.clicked(CircuitComponent.this, p);
if (manualChangeObserver != null)
manualChangeObserver.hasChanged();
}
}
}
}

View File

@ -63,7 +63,7 @@ public class Circuit implements Drawable {
modified();
}
public ArrayList<VisualElement> getParts() {
public ArrayList<VisualElement> getElements() {
return visualElements;
}
@ -121,6 +121,14 @@ public class Circuit implements Drawable {
modified();
}
public VisualElement getElementAt(Vector pos) {
for (VisualElement element : visualElements) {
if (element.matches(pos))
return element;
}
return null;
}
public void modified() {
modified = true;
}

View File

@ -10,6 +10,7 @@ import de.neemann.digital.gui.draw.shapes.Drawable;
* @author hneemann
*/
public class Wire implements Drawable, Moveable {
private static final int MIN_LABEL_LEN = 50;
public Vector p1;
public Vector p2;
private transient ObservableValue value;
@ -37,6 +38,11 @@ public class Wire implements Drawable, Moveable {
graphic.drawLine(p1, p2, style);
if (value != null && p1.y == p2.y && Math.abs(p1.x - p2.x) > MIN_LABEL_LEN && value.getBits() > 1) {
Vector pos = p1.add(p2).div(2).add(0, -2);
graphic.drawText(pos, pos.add(1, 0), value.getValueString(), de.neemann.digital.gui.draw.graphics.Orientation.CENTERBOTTOM, Style.SHAPE_PIN);
}
if (p1Dot || p2Dot) {
Vector r = new Vector(style.getThickness(), style.getThickness());
if (p1Dot)

View File

@ -31,7 +31,7 @@ public class ModelDescription implements Iterable<ModelEntry> {
public ModelDescription(Circuit circuit, ElementLibrary library) throws PinException {
entries = new ArrayList<>();
netList = new NetList(circuit.getWires());
for (VisualElement vp : circuit.getParts()) {
for (VisualElement vp : circuit.getElements()) {
Pins pins = vp.getPins();
ElementTypeDescription elementType = library.getElementType(vp.getElementName());
Element element = elementType.createElement(vp.getElementAttributes());

View File

@ -56,7 +56,7 @@ public class InputShape implements Shape {
ObservableValue value = ioState.getOutput(0);
style = Style.getWireStyle(value);
if (value.getBits() > 1) {
Vector textPos = new Vector(2 + SIZE, -2 - SIZE);
Vector textPos = new Vector(-2 - SIZE, -2 - SIZE);
graphic.drawText(textPos, textPos.add(1, 0), value.getValueString(), Orientation.CENTERBOTTOM, Style.NORMAL);
}
}