mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-29 16:01:19 -04:00
added some more functions to the event sourcing system
This commit is contained in:
parent
a0cbecfa55
commit
9bd3ae3373
@ -8,8 +8,7 @@ import de.neemann.digital.core.element.ImmutableList;
|
|||||||
import de.neemann.digital.core.element.Key;
|
import de.neemann.digital.core.element.Key;
|
||||||
import de.neemann.digital.core.element.Keys;
|
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.Modification;
|
import de.neemann.digital.gui.components.modification.*;
|
||||||
import de.neemann.digital.gui.components.modification.ModifyAttribute;
|
|
||||||
import de.neemann.digital.draw.graphics.*;
|
import de.neemann.digital.draw.graphics.*;
|
||||||
import de.neemann.digital.draw.library.ElementLibrary;
|
import de.neemann.digital.draw.library.ElementLibrary;
|
||||||
import de.neemann.digital.draw.library.ElementNotFoundException;
|
import de.neemann.digital.draw.library.ElementNotFoundException;
|
||||||
@ -18,7 +17,6 @@ import de.neemann.digital.draw.library.LibraryNode;
|
|||||||
import de.neemann.digital.draw.shapes.Drawable;
|
import de.neemann.digital.draw.shapes.Drawable;
|
||||||
import de.neemann.digital.draw.shapes.ShapeFactory;
|
import de.neemann.digital.draw.shapes.ShapeFactory;
|
||||||
import de.neemann.digital.gui.Main;
|
import de.neemann.digital.gui.Main;
|
||||||
import de.neemann.digital.gui.components.modification.ModifyAttributes;
|
|
||||||
import de.neemann.digital.gui.sync.NoSync;
|
import de.neemann.digital.gui.sync.NoSync;
|
||||||
import de.neemann.digital.gui.sync.Sync;
|
import de.neemann.digital.gui.sync.Sync;
|
||||||
import de.neemann.digital.lang.Lang;
|
import de.neemann.digital.lang.Lang;
|
||||||
@ -122,49 +120,8 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
rotateAction.setEnabled(false);
|
rotateAction.setEnabled(false);
|
||||||
|
|
||||||
|
|
||||||
copyAction = new AbstractAction(Lang.get("menu_copy")) {
|
copyAction = createCopyAction(shapeFactory);
|
||||||
@Override
|
pasteAction = createPasteAction(shapeFactory);
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
ArrayList<Movable> elements = null;
|
|
||||||
if (activeMouseController instanceof MouseControllerSelect) {
|
|
||||||
MouseControllerSelect mcs = ((MouseControllerSelect) activeMouseController);
|
|
||||||
elements = circuit.getElementsToCopy(Vector.min(mcs.corner1, mcs.corner2), Vector.max(mcs.corner1, mcs.corner2), shapeFactory);
|
|
||||||
} else if (activeMouseController instanceof MouseControllerMoveElement) {
|
|
||||||
MouseControllerMoveElement mcme = ((MouseControllerMoveElement) activeMouseController);
|
|
||||||
elements = new ArrayList<>();
|
|
||||||
elements.add(mcme.visualElement);
|
|
||||||
}
|
|
||||||
if (elements != null) {
|
|
||||||
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
||||||
clipboard.setContents(new CircuitTransferable(elements), null);
|
|
||||||
activeMouseController.escapePressed();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
copyAction.setEnabled(false);
|
|
||||||
|
|
||||||
pasteAction = new AbstractAction(Lang.get("menu_paste")) {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (!isLocked()) {
|
|
||||||
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
||||||
try {
|
|
||||||
Object data = clipboard.getData(DataFlavor.stringFlavor);
|
|
||||||
if (data instanceof String) {
|
|
||||||
Vector posVector = getPosVector(lastMousePos.x, lastMousePos.y);
|
|
||||||
ArrayList<Movable> elements = CircuitTransferable.createList(data, shapeFactory, posVector);
|
|
||||||
if (elements != null) {
|
|
||||||
removeHighLighted();
|
|
||||||
mouseInsertList.activate(elements, posVector);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_clipboardContainsNoImportableData")).setComponent(CircuitComponent.this));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
deleteAction = new ToolTipAction(Lang.get("menu_delete"), ICON_DELETE) {
|
deleteAction = new ToolTipAction(Lang.get("menu_delete"), ICON_DELETE) {
|
||||||
@Override
|
@Override
|
||||||
@ -265,6 +222,55 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
setToolTipText("");
|
setToolTipText("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private AbstractAction createPasteAction(ShapeFactory shapeFactory) {
|
||||||
|
return new AbstractAction(Lang.get("menu_paste")) {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (!isLocked()) {
|
||||||
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
||||||
|
try {
|
||||||
|
Object data = clipboard.getData(DataFlavor.stringFlavor);
|
||||||
|
if (data instanceof String) {
|
||||||
|
Vector posVector = getPosVector(lastMousePos.x, lastMousePos.y);
|
||||||
|
ArrayList<Movable> elements = CircuitTransferable.createList(data, shapeFactory, posVector);
|
||||||
|
if (elements != null) {
|
||||||
|
removeHighLighted();
|
||||||
|
mouseInsertList.activate(elements, posVector);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_clipboardContainsNoImportableData")).setComponent(CircuitComponent.this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private AbstractAction createCopyAction(ShapeFactory shapeFactory) {
|
||||||
|
AbstractAction copyAction = new AbstractAction(Lang.get("menu_copy")) {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
ArrayList<Movable> elements = null;
|
||||||
|
if (activeMouseController instanceof MouseControllerSelect) {
|
||||||
|
MouseControllerSelect mcs = ((MouseControllerSelect) activeMouseController);
|
||||||
|
elements = circuit.getElementsToCopy(Vector.min(mcs.corner1, mcs.corner2), Vector.max(mcs.corner1, mcs.corner2), shapeFactory);
|
||||||
|
} else if (activeMouseController instanceof MouseControllerMoveElement) {
|
||||||
|
MouseControllerMoveElement mcme = ((MouseControllerMoveElement) activeMouseController);
|
||||||
|
elements = new ArrayList<>();
|
||||||
|
elements.add(mcme.visualElement);
|
||||||
|
}
|
||||||
|
if (elements != null) {
|
||||||
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
||||||
|
clipboard.setContents(new CircuitTransferable(elements), null);
|
||||||
|
activeMouseController.escapePressed();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
copyAction.setEnabled(false);
|
||||||
|
return copyAction;
|
||||||
|
}
|
||||||
|
|
||||||
private void programElementAt(Vector pos) {
|
private void programElementAt(Vector pos) {
|
||||||
VisualElement ve = circuit.getElementAt(pos);
|
VisualElement ve = circuit.getElementAt(pos);
|
||||||
if (ve != null && library.isProgrammable(ve.getElementName())) {
|
if (ve != null && library.isProgrammable(ve.getElementName())) {
|
||||||
@ -991,10 +997,8 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
void clicked(MouseEvent e) {
|
void clicked(MouseEvent e) {
|
||||||
if (e.getButton() == MouseEvent.BUTTON1 && !isLocked()) {
|
if (e.getButton() == MouseEvent.BUTTON1 && !isLocked())
|
||||||
circuit.add(element);
|
modify(new ModifyInsertElement(element));
|
||||||
hasChanged();
|
|
||||||
}
|
|
||||||
mouseNormal.activate();
|
mouseNormal.activate();
|
||||||
focusWasLost = false;
|
focusWasLost = false;
|
||||||
}
|
}
|
||||||
@ -1016,6 +1020,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
private Vector delta;
|
private Vector delta;
|
||||||
private Vector initialPos;
|
private Vector initialPos;
|
||||||
private int initialRot;
|
private int initialRot;
|
||||||
|
private boolean deleted=false;
|
||||||
|
|
||||||
private MouseControllerMoveElement(Cursor cursor) {
|
private MouseControllerMoveElement(Cursor cursor) {
|
||||||
super(cursor);
|
super(cursor);
|
||||||
@ -1035,8 +1040,10 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
void clicked(MouseEvent e) {
|
void clicked(MouseEvent e) {
|
||||||
if (!isLocked())
|
if (!isLocked()) {
|
||||||
visualElement.setPos(raster(visualElement.getPos()));
|
visualElement.setPos(raster(visualElement.getPos()));
|
||||||
|
addModificationAlreadyMade(new ModifyMoveAndRotElement(visualElement, initialPos));
|
||||||
|
}
|
||||||
mouseNormal.activate();
|
mouseNormal.activate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1059,6 +1066,8 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
public void delete() {
|
public void delete() {
|
||||||
if (!isLocked()) {
|
if (!isLocked()) {
|
||||||
circuit.delete(visualElement);
|
circuit.delete(visualElement);
|
||||||
|
addModificationAlreadyMade(new ModifyDeleteElement(visualElement, initialPos));
|
||||||
|
deleted=true;
|
||||||
mouseNormal.activate();
|
mouseNormal.activate();
|
||||||
isManualScale = true;
|
isManualScale = true;
|
||||||
}
|
}
|
||||||
@ -1087,6 +1096,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
private Wire wire;
|
private Wire wire;
|
||||||
private Vector pos;
|
private Vector pos;
|
||||||
private Vector initialPos;
|
private Vector initialPos;
|
||||||
|
private Vector initialWirePos;
|
||||||
|
|
||||||
private MouseControllerMoveWire(Cursor cursor) {
|
private MouseControllerMoveWire(Cursor cursor) {
|
||||||
super(cursor);
|
super(cursor);
|
||||||
@ -1096,6 +1106,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
super.activate();
|
super.activate();
|
||||||
this.wire = wire;
|
this.wire = wire;
|
||||||
this.pos = raster(pos);
|
this.pos = raster(pos);
|
||||||
|
this.initialWirePos = wire.getPos();
|
||||||
this.initialPos = this.pos;
|
this.initialPos = this.pos;
|
||||||
deleteAction.setActive(true);
|
deleteAction.setActive(true);
|
||||||
removeHighLighted();
|
removeHighLighted();
|
||||||
@ -1105,6 +1116,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
@Override
|
@Override
|
||||||
void clicked(MouseEvent e) {
|
void clicked(MouseEvent e) {
|
||||||
removeHighLighted();
|
removeHighLighted();
|
||||||
|
addModificationAlreadyMade(new ModifyMoveWire(wire, initialWirePos));
|
||||||
circuit.elementsMoved();
|
circuit.elementsMoved();
|
||||||
mouseNormal.activate();
|
mouseNormal.activate();
|
||||||
}
|
}
|
||||||
@ -1126,6 +1138,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
@Override
|
@Override
|
||||||
public void delete() {
|
public void delete() {
|
||||||
circuit.delete(wire);
|
circuit.delete(wire);
|
||||||
|
addModificationAlreadyMade(new ModifyDeleteWire(wire, initialWirePos));
|
||||||
mouseNormal.activate();
|
mouseNormal.activate();
|
||||||
isManualScale = true;
|
isManualScale = true;
|
||||||
}
|
}
|
||||||
@ -1170,7 +1183,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
|
|||||||
if (e.getButton() == MouseEvent.BUTTON3)
|
if (e.getButton() == MouseEvent.BUTTON3)
|
||||||
mouseNormal.activate();
|
mouseNormal.activate();
|
||||||
else {
|
else {
|
||||||
circuit.add(wire);
|
modify(new ModifyAddWire(wire));
|
||||||
if (circuit.isPinPos(wire.p2))
|
if (circuit.isPinPos(wire.p2))
|
||||||
mouseNormal.activate();
|
mouseNormal.activate();
|
||||||
else
|
else
|
||||||
|
@ -13,8 +13,12 @@ public abstract class ModificationOfVisualElement implements Modification {
|
|||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
public ModificationOfVisualElement(VisualElement ve) {
|
public ModificationOfVisualElement(VisualElement ve) {
|
||||||
pos = ve.getPos();
|
this(ve, ve.getPos());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ModificationOfVisualElement(VisualElement ve, Vector initialPos) {
|
||||||
name = ve.getElementName();
|
name = ve.getElementName();
|
||||||
|
pos = initialPos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public VisualElement getVisualElement(Circuit circuit) {
|
public VisualElement getVisualElement(Circuit circuit) {
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
package de.neemann.digital.gui.components.modification;
|
||||||
|
|
||||||
|
import de.neemann.digital.draw.elements.Circuit;
|
||||||
|
import de.neemann.digital.draw.elements.Wire;
|
||||||
|
import de.neemann.digital.draw.graphics.Vector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hneemann on 25.05.17.
|
||||||
|
*/
|
||||||
|
public abstract class ModificationOfWire implements Modification {
|
||||||
|
|
||||||
|
private final Vector p1;
|
||||||
|
private final Vector p2;
|
||||||
|
|
||||||
|
public ModificationOfWire(Wire wire) {
|
||||||
|
this(wire, wire.p1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ModificationOfWire(Wire wire, Vector initialPos) {
|
||||||
|
p1 = initialPos;
|
||||||
|
p2 = initialPos.add(wire.p2.sub(wire.p1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Wire getWire(Circuit circuit) {
|
||||||
|
for (Wire w : circuit.getWires()) {
|
||||||
|
if (w.p1.equals(p1) && w.p2.equals(p2))
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
throw new RuntimeException("internal error: Element not found!");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package de.neemann.digital.gui.components.modification;
|
||||||
|
|
||||||
|
import de.neemann.digital.draw.elements.Circuit;
|
||||||
|
import de.neemann.digital.draw.elements.Wire;
|
||||||
|
import de.neemann.digital.draw.graphics.Vector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hneemann on 26.05.17.
|
||||||
|
*/
|
||||||
|
public class ModifyAddWire implements Modification {
|
||||||
|
private final Vector p1;
|
||||||
|
private final Vector p2;
|
||||||
|
|
||||||
|
public ModifyAddWire(Wire w) {
|
||||||
|
p1 = w.p1;
|
||||||
|
p2 = w.p2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void modify(Circuit circuit) {
|
||||||
|
circuit.add(new Wire(p1, p2));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package de.neemann.digital.gui.components.modification;
|
||||||
|
|
||||||
|
import de.neemann.digital.draw.elements.Circuit;
|
||||||
|
import de.neemann.digital.draw.elements.VisualElement;
|
||||||
|
import de.neemann.digital.draw.graphics.Vector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hneemann on 26.05.17.
|
||||||
|
*/
|
||||||
|
public class ModifyDeleteElement extends ModificationOfVisualElement {
|
||||||
|
public ModifyDeleteElement(VisualElement ve, Vector initialPos) {
|
||||||
|
super(ve, initialPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void modify(Circuit circuit) {
|
||||||
|
circuit.delete(getVisualElement(circuit));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package de.neemann.digital.gui.components.modification;
|
||||||
|
|
||||||
|
import de.neemann.digital.draw.elements.Circuit;
|
||||||
|
import de.neemann.digital.draw.elements.Wire;
|
||||||
|
import de.neemann.digital.draw.graphics.Vector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hneemann on 26.05.17.
|
||||||
|
*/
|
||||||
|
public class ModifyDeleteWire extends ModificationOfWire {
|
||||||
|
|
||||||
|
public ModifyDeleteWire(Wire wire, Vector initialPos) {
|
||||||
|
super(wire, initialPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void modify(Circuit circuit) {
|
||||||
|
circuit.delete(getWire(circuit));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package de.neemann.digital.gui.components.modification;
|
||||||
|
|
||||||
|
import de.neemann.digital.draw.elements.Circuit;
|
||||||
|
import de.neemann.digital.draw.elements.VisualElement;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hneemann on 26.05.17.
|
||||||
|
*/
|
||||||
|
public class ModifyInsertElement implements Modification {
|
||||||
|
private final VisualElement element;
|
||||||
|
|
||||||
|
public ModifyInsertElement(VisualElement element) {
|
||||||
|
this.element = new VisualElement(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void modify(Circuit circuit) {
|
||||||
|
circuit.add(new VisualElement(element));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package de.neemann.digital.gui.components.modification;
|
||||||
|
|
||||||
|
import de.neemann.digital.draw.elements.Circuit;
|
||||||
|
import de.neemann.digital.draw.elements.VisualElement;
|
||||||
|
import de.neemann.digital.draw.graphics.Vector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hneemann on 26.05.17.
|
||||||
|
*/
|
||||||
|
public class ModifyMoveAndRotElement extends ModificationOfVisualElement {
|
||||||
|
private final Vector pos;
|
||||||
|
private final int rotation;
|
||||||
|
|
||||||
|
public ModifyMoveAndRotElement(VisualElement ve, Vector initialPos) {
|
||||||
|
super(ve, initialPos);
|
||||||
|
pos=ve.getPos();
|
||||||
|
rotation = ve.getRotate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void modify(Circuit circuit) {
|
||||||
|
VisualElement ve = getVisualElement(circuit);
|
||||||
|
ve.setPos(pos);
|
||||||
|
ve.setRotation(rotation);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package de.neemann.digital.gui.components.modification;
|
||||||
|
|
||||||
|
import de.neemann.digital.draw.elements.Circuit;
|
||||||
|
import de.neemann.digital.draw.elements.Wire;
|
||||||
|
import de.neemann.digital.draw.graphics.Vector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hneemann on 26.05.17.
|
||||||
|
*/
|
||||||
|
public class ModifyMoveWire extends ModificationOfWire {
|
||||||
|
private final Vector delta;
|
||||||
|
|
||||||
|
public ModifyMoveWire(Wire wire, Vector initialPos) {
|
||||||
|
super(wire, initialPos);
|
||||||
|
delta=wire.getPos().sub(initialPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void modify(Circuit circuit) {
|
||||||
|
getWire(circuit).move(delta);
|
||||||
|
circuit.elementsMoved();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
/**
|
||||||
|
* Classes to implement the events used by the event sourcing system.
|
||||||
|
* Created by hneemann on 26.05.17.
|
||||||
|
*/
|
||||||
|
package de.neemann.digital.gui.components.modification;
|
Loading…
x
Reference in New Issue
Block a user