Skip to content

Commit

Permalink
#9 WIP, the start of dict:to_list support in erlyberly, i.e. showing …
Browse files Browse the repository at this point in the history
…a dicts internal representation as something readble.
  • Loading branch information
andytill committed Oct 31, 2015
1 parent 9e7a313 commit 0628968
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 62 deletions.
15 changes: 15 additions & 0 deletions src/main/java/erlyberly/ErlyBerly.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,19 @@ public void handle(WindowEvent e) {
public static NodeAPI nodeAPI() {
return nodeAPI;
}


public static void subWindow(String title, Parent parentControl) {
Stage stage = new Stage();
Scene scene = new Scene(parentControl);

CloseWindowOnEscape.apply(scene, stage);

stage.setScene(scene);
stage.setWidth(800);
stage.setHeight(600);
stage.setTitle(title);

stage.show();
}
}
28 changes: 28 additions & 0 deletions src/main/java/erlyberly/TermTreeItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package erlyberly;

import com.ericsson.otp.erlang.OtpErlangObject;

/**
* A wrapper class around a {@link TermTreeView} tree item, with a erlang term
* and the string which gets shown in the tree. The string is not necessarily
* derivable from the term, and depends on its position in the tree so much be
* separated.
*/
class TermTreeItem {
private final String toString;
private final OtpErlangObject object;
public TermTreeItem(OtpErlangObject object, String toString) {
this.object = object;
this.toString = toString;
}
public String getToString() {
return toString;
}
public OtpErlangObject getObject() {
return object;
}
@Override
public String toString() {
return toString;
}
}
122 changes: 70 additions & 52 deletions src/main/java/erlyberly/TermTreeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,83 +17,102 @@
import javafx.scene.input.KeyCombination;

@SuppressWarnings("rawtypes")
public class TermTreeView extends TreeView<OtpErlangObject> {

@SuppressWarnings("unchecked")
public class TermTreeView extends TreeView<TermTreeItem> {

public TermTreeView() {
setRoot(new TreeItem());
setRoot(new TreeItem<TermTreeItem>());

MenuItem copyMenuItem = new MenuItem("Copy");
copyMenuItem.setAccelerator(KeyCombination.keyCombination("shortcut+c"));
copyMenuItem.setOnAction(this::onCopyCalls);
setContextMenu(new ContextMenu(copyMenuItem));
}


MenuItem dictMenuItem = new MenuItem("Dict to List");
dictMenuItem.setOnAction(this::onViewDict);

setContextMenu(new ContextMenu(copyMenuItem, dictMenuItem));
}

public void onViewDict(ActionEvent e) {
TreeItem<TermTreeItem> item = getSelectionModel().getSelectedItem();
if(item == null)
return;
if(item.getValue() == null || !OtpUtil.atom("dict").equals(item.getValue().getObject()))
return;
if(item.getParent() == null || !(item.getParent().getValue().getObject() instanceof OtpErlangTuple))
return;

OtpErlangObject dict = item.getParent().getValue().getObject();

try {
OtpErlangList props = ErlyBerly.nodeAPI().dictToPropslist(dict);
TermTreeView parentControl = new TermTreeView();
parentControl.populateFromTerm(props);
ErlyBerly.subWindow("dict_to_list", parentControl);
}
catch (Exception e1) {
e1.printStackTrace();
}
}

public void populateFromTerm(OtpErlangObject obj) {
setShowRoot(false);

addToTreeItem(getRoot(), obj);
}

@SuppressWarnings("unchecked")
private void addToTreeItem(TreeItem parent, OtpErlangObject obj) {

private void addToTreeItem(TreeItem<TermTreeItem> parent, OtpErlangObject obj) {
if(obj instanceof OtpErlangBinary) {
TreeItem item = new TreeItem(OtpUtil.binaryToString((OtpErlangBinary) obj));
TreeItem<TermTreeItem> item = new TreeItem<>(new TermTreeItem(obj, OtpUtil.binaryToString((OtpErlangBinary) obj)));
parent.getChildren().add(item);
}
else if(obj instanceof OtpErlangTuple) {
OtpErlangObject[] elements = ((OtpErlangTuple) obj).elements();

if(elements.length == 0) {
parent.getChildren().add(new TreeItem("{ }"));
parent.getChildren().add(new TreeItem<>(new TermTreeItem(obj, "{ }")));
}
else {
TreeItem tupleItem;

if(isRecord(obj)) {
TreeItem<TermTreeItem> tupleItem;
if(isRecord(obj)) {
String recordNameText = "#" + OtpUtil.tupleElement(1, obj) + " ";

tupleItem = new TreeItem("{");
tupleItem = new TreeItem<>(new TermTreeItem(obj, "{"));
tupleItem.setGraphic(recordLabel(recordNameText));
parent.getChildren().add(tupleItem);
tupleItem.setExpanded(true);
elements = OtpUtil.iterableElements(OtpUtil.tupleElement(2, obj));// ((OtpErlangList) OtpUtil.tupleElement(2, obj)).elements();
for (OtpErlangObject e : elements) {
addToTreeItem(tupleItem, e);
}
parent.getChildren().add(new TreeItem("}"));
}
else if(isRecordField(obj)) {

tupleItem = new TreeItem(" ");
tupleItem.setGraphic(recordLabel(OtpUtil.tupleElement(1, obj) + " = "));
parent.getChildren().add(tupleItem);
tupleItem.setExpanded(true);

parent.getChildren().add(tupleItem);

OtpErlangObject value = OtpUtil.tupleElement(2, obj);
if(OtpUtil.isLittleTerm(value))
tupleItem.setValue(otpObjectToString(value));
else
addToTreeItem(tupleItem, value);

elements = OtpUtil.iterableElements(OtpUtil.tupleElement(2, obj));
for (OtpErlangObject e : elements) {
addToTreeItem(tupleItem, e);
}
parent.getChildren().add(new TreeItem<>(new TermTreeItem(obj, "}")));
}
else if(isRecordField(obj)) {
tupleItem = new TreeItem<>(new TermTreeItem(obj, " "));
tupleItem.setGraphic(recordLabel(OtpUtil.tupleElement(1, obj) + " = "));
tupleItem.setExpanded(true);

parent.getChildren().add(tupleItem);

OtpErlangObject value = OtpUtil.tupleElement(2, obj);
if(OtpUtil.isLittleTerm(value))
tupleItem.setValue(new TermTreeItem(value, otpObjectToString(value)));
else
addToTreeItem(tupleItem, value);
}
else {
tupleItem = new TreeItem();
tupleItem = new TreeItem<>();
tupleItem.setExpanded(true);

if(OtpUtil.isLittleTerm(obj)) {
tupleItem.setValue(otpObjectToString(obj));
tupleItem.setValue(new TermTreeItem(obj, otpObjectToString(obj)));
parent.getChildren().add(tupleItem);
}
else {
tupleItem.setValue("{");
tupleItem.setValue(new TermTreeItem(obj, "{"));
for (OtpErlangObject e : elements) {
addToTreeItem(tupleItem, e);
}
parent.getChildren().add(tupleItem);
parent.getChildren().add(new TreeItem("}"));
parent.getChildren().add(new TreeItem<>(new TermTreeItem(obj, "}")));
}

}
Expand All @@ -103,31 +122,31 @@ else if(obj instanceof OtpErlangList) {
OtpErlangObject[] elements = ((OtpErlangList) obj).elements();

if(elements.length == 0) {
parent.getChildren().add(new TreeItem("[ ]"));
parent.getChildren().add(new TreeItem<>(new TermTreeItem(obj, "[ ]")));
}
else {
TreeItem listItem;
TreeItem<TermTreeItem> listItem;

listItem = new TreeItem("[");
listItem = new TreeItem<>(new TermTreeItem(obj, "["));
listItem.setExpanded(true);


if(OtpUtil.isLittleTerm(obj)) {
listItem.setValue(otpObjectToString(obj));
listItem.setValue(new TermTreeItem(obj, otpObjectToString(obj)));
parent.getChildren().add(listItem);
}
else {
listItem.setValue("[");
listItem.setValue(new TermTreeItem(obj, "["));
for (OtpErlangObject e : elements) {
addToTreeItem(listItem, e);
}
parent.getChildren().add(listItem);
parent.getChildren().add(new TreeItem("]"));
parent.getChildren().add(new TreeItem<>(new TermTreeItem(obj, "]")));
}
}
}
else {
parent.getChildren().add(new TreeItem(otpObjectToString(obj)));
parent.getChildren().add(new TreeItem<>(new TermTreeItem(obj, otpObjectToString(obj))));
}
}

Expand Down Expand Up @@ -156,16 +175,15 @@ private boolean isRecord(OtpErlangObject obj) {
public void populateFromListContents(OtpErlangList list) {
for (OtpErlangObject a : list) {
populateFromTerm(a);
}
}
}

private void onCopyCalls(ActionEvent e) {
StringBuilder sbuilder = new StringBuilder();

for (TreeItem obj : getSelectionModel().getSelectedItems()) {
sbuilder.append(obj.getValue()).append("\n");
}

}
copyToClipboard(sbuilder);
}

Expand Down
24 changes: 14 additions & 10 deletions src/main/java/erlyberly/node/NodeAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import javafx.application.Platform;
import javafx.beans.Observable;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import com.ericsson.otp.erlang.OtpAuthException;
import com.ericsson.otp.erlang.OtpConn;
import com.ericsson.otp.erlang.OtpErlangAtom;
Expand All @@ -45,6 +35,15 @@
import erlyberly.ProcInfo;
import erlyberly.SeqTraceLog;
import erlyberly.TraceLog;
import javafx.application.Platform;
import javafx.beans.Observable;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class NodeAPI {
private static final OtpErlangAtom ERLYBERLY_XREF_STARTED_ATOM = atom("erlyberly_xref_started");
Expand Down Expand Up @@ -544,4 +543,9 @@ public String returnCode(OtpErlangObject result, String errorResponse){
return errorResponse;
}
}

public OtpErlangList dictToPropslist(OtpErlangObject dict) throws IOException, OtpErlangException {
sendRPC("dict", "to_list", list(dict));
return (OtpErlangList) receiveRPC(5000);
}
}

0 comments on commit 0628968

Please sign in to comment.