Skip to content

Commit

Permalink
Merge pull request #42 from EdNutting/master
Browse files Browse the repository at this point in the history
Clipboard
  • Loading branch information
EdNutting authored Mar 21, 2020
2 parents 605cdc8 + c7def69 commit 55f88fd
Show file tree
Hide file tree
Showing 11 changed files with 504 additions and 271 deletions.
22 changes: 20 additions & 2 deletions src/com/modsim/modules/BaseModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -704,15 +704,33 @@ public void propagateDirectionality(BidirPort root) {
* Called by XMLReader and copy routines. Default behaviour is no-op.
* @param data Structure containing state to load (module-defined elements)
*/
public void dataIn(HashMap<String, String> data) {}
public void dataIn(HashMap<String, String> data) {
if (data.containsKey("label")) {
label = data.get("label");
}
if (data.containsKey("label_size")) {
String sizeStr = data.get("label_size");
try {
labelSize = Integer.parseInt(sizeStr);
} catch (NumberFormatException e) {
System.err.println("Warning: unable to parse label_size:");
e.printStackTrace();
}
}
}

/**
* Fill a string-string hash map with module-specific data for retrieval with dataIn.
* Called by XMLWriter and the copy routines. Default behaviour is to return null, indicating that no relevant
* data is contained in the module.
* @return A filled hash map structure, or null if no state is stored
*/
public HashMap<String, String> dataOut() { return null; }
public HashMap<String, String> dataOut() {
HashMap<String, String> dataMap = new HashMap<>();
dataMap.put("label", label);
dataMap.put("label_size", Integer.toString(labelSize));
return dataMap;
}

public enum AvailableModules {
// Enum members should not be renamed!
Expand Down
4 changes: 3 additions & 1 deletion src/com/modsim/modules/Clock.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public void propagate() {

@Override
public void dataIn(HashMap<String, String> data) {
super.dataIn(data);

if (data.containsKey("clock_phase")) {
String phaseStr = data.get("clock_phase");
try {
Expand All @@ -131,7 +133,7 @@ public void dataIn(HashMap<String, String> data) {

@Override
public HashMap<String, String> dataOut() {
HashMap<String, String> dataMap = new HashMap<>();
HashMap<String, String> dataMap = super.dataOut();
dataMap.put("clock_phase", String.valueOf(step));
return dataMap;
}
Expand Down
6 changes: 4 additions & 2 deletions src/com/modsim/modules/LEDMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,18 @@ public boolean isPersistEnabled(){

@Override
public HashMap<String, String> dataOut() {
if (!isPersistEnabled()) return null;
if (!isPersistEnabled()) return super.dataOut();

HashMap<String, String> data = new HashMap<>();
HashMap<String, String> data = super.dataOut();
data.put("persist", "1");

return data;
}

@Override
public void dataIn(HashMap<String, String> data) {
super.dataIn(data);

if (data.containsKey("persist")) {
String storeStr = data.get("persist");
try{
Expand Down
6 changes: 4 additions & 2 deletions src/com/modsim/modules/NRAM.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ public void clear() {

@Override
public void dataIn(HashMap<String, String> data) {
super.dataIn(data);

if (data.containsKey("memory_store")) {
String storeStr = data.get("memory_store");
HexReader.readString(storeStr, this);
Expand All @@ -192,9 +194,9 @@ public void dataIn(HashMap<String, String> data) {
@Override
public HashMap<String, String> dataOut() {
String storeStr = HexWriter.hexString(this, false);
if (storeStr.isEmpty()) return null;
if (storeStr.isEmpty()) return super.dataOut();

HashMap<String, String> data = new HashMap<>();
HashMap<String, String> data = super.dataOut();
data.put("memory_store", storeStr);
data.put("write_jumper", (writeJumper.getEnabled()) ? "1" : "0");

Expand Down
4 changes: 3 additions & 1 deletion src/com/modsim/modules/Register.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ public void clear() {

@Override
public HashMap<String, String> dataOut() {
HashMap<String, String> dataMap = new HashMap<>();
HashMap<String, String> dataMap = super.dataOut();
String latched = myData.toString();
dataMap.put("latched_value", latched);
return dataMap;
}

@Override
public void dataIn(HashMap<String, String> data) {
super.dataIn(data);

if (data.containsKey("latched_value")) {
// Parse latched value
String str = data.get("latched_value");
Expand Down
4 changes: 3 additions & 1 deletion src/com/modsim/modules/SwitchInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public void propagate() {

@Override
public void dataIn(HashMap<String, String> data) {
super.dataIn(data);

if (data.containsKey("switch_set")) {
// Parse switch setting
String str = data.get("switch_set");
Expand All @@ -121,7 +123,7 @@ public void dataIn(HashMap<String, String> data) {

@Override
public HashMap<String, String> dataOut() {
HashMap<String, String> data = new HashMap<>();
HashMap<String, String> data = super.dataOut();
String setting;
setting = s1.getEnabled() ? "1" : "0";
setting += s2.getEnabled() ? "1" : "0";
Expand Down
8 changes: 7 additions & 1 deletion src/com/modsim/operations/Ops.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,13 @@ public static void fileNew() {
Main.ui.view.cancelTool();

if (!Main.clipboard.isEmpty()) {
Main.ui.view.setTool(new PlaceTool(Main.clipboard));
try {
Main.ui.view.setTool(new PlaceTool(Main.clipboard));
}
catch (Exception e) {
// Clipboard contents may not be valid
System.out.println(e.getMessage());
}
}
else {
System.out.println("Nothing to paste");
Expand Down
4 changes: 3 additions & 1 deletion src/com/modsim/tools/PlaceTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ public PlaceTool(PickableEntity e) {

/**
* Acts as a 'pasteInto' tool for the given clipboard
*
* @param clipboard Clipboard containing com.modsim.modules to 'pasteInto'
* @throws Exception
*/
public PlaceTool(ModuleClipboard clipboard) {
public PlaceTool(ModuleClipboard clipboard) throws Exception {
Main.opStack.beginCompoundOp();
entities = clipboard.paste();

Expand Down
Loading

0 comments on commit 55f88fd

Please sign in to comment.