Skip to content

Commit 01b4297

Browse files
committed
Added a number of UI elements
1 parent 1122954 commit 01b4297

File tree

5 files changed

+197
-53
lines changed

5 files changed

+197
-53
lines changed

src/CAS.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import javafx.scene.Parent;
77
import javafx.scene.Scene;
88
import javafx.stage.Stage;
9-
import jmc.cas.Expression;
10-
import jmc.cas.Operable;
11-
import jmc.cas.Operation;
9+
import jmc.cas.*;
10+
11+
import java.util.NoSuchElementException;
1212

1313
/**
1414
* Created by Jiachen on 3/2/18.
@@ -31,16 +31,44 @@ public void start(Stage primaryStage) throws Exception {
3131
expression.bind(controller.input.textProperty());
3232
expression.addListener((observable, oldValue, newValue) -> {
3333
System.out.println(oldValue + " " + newValue);
34-
Operable operable = Expression.interpret(newValue);
35-
controller.addition.setText(operable instanceof Operation ? ((Operation) operable).copy().toAdditionOnly().toString() : operable.toString());
36-
controller.exponential.setText(operable instanceof Operation ? ((Operation) operable).copy().toExponentialForm().toString() : operable.toString());
37-
controller.simplified.setText(operable.copy().simplify().toString()); //should conform to MVC, setText should be in controller.
38-
controller.beautified.setText(operable.copy().simplify().beautify().toString());
34+
try {
35+
Operable operable = Expression.interpret(newValue);
36+
controller.addition.setText(operable instanceof Operation ? ((Operation) operable).copy().toAdditionOnly().toString() : operable.toString());
37+
controller.exponential.setText(operable instanceof Operation ? ((Operation) operable).copy().toExponentialForm().toString() : operable.toString());
38+
39+
int nodes1 = operable.numNodes(), complexity1 = operable.complexity();
40+
controller.nodesBefore.setText(String.valueOf(nodes1));
41+
controller.complexityBefore.setText(String.valueOf(complexity1));
42+
43+
operable = operable.simplify();
44+
controller.simplified.setText(operable.toString()); //should conform to MVC, setText should be in controller.
45+
46+
operable = operable.beautify();
47+
controller.beautified.setText(operable.toString());
48+
49+
try {
50+
controller.vars.setText(Operable.extractVariables(operable)
51+
.stream().map(Variable::getName)
52+
.reduce((a, b) -> a + ", " + b).get());
53+
} catch (NoSuchElementException e) {
54+
controller.vars.setText("None");
55+
}
56+
57+
58+
controller.val.setText(String.valueOf(operable.val()));
59+
60+
controller.complexityAfter.setText(String.valueOf(operable.complexity()));
61+
controller.nodesAfter.setText(String.valueOf(operable.numNodes()));
62+
controller.errMsg.setText("\"\"");
63+
} catch (JMCException e) {
64+
controller.error(e.getMessage());
65+
}
3966
});
4067
primaryStage.setTitle("JMC Computer Algebra System");
41-
primaryStage.setScene(new Scene(root, 400, 200));
42-
primaryStage.setMinHeight(200);
68+
primaryStage.setScene(new Scene(root, 400, 400));
69+
primaryStage.setMinHeight(400);
4370
primaryStage.setMinWidth(400);
4471
primaryStage.show();
4572
}
73+
4674
}

src/CasController.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,41 @@ public class CasController {
1414
public Label beautified;
1515
public Label addition;
1616
public Label exponential;
17+
public Label nodesBefore;
18+
public Label complexityBefore;
19+
public Label complexityAfter;
20+
public Label nodesAfter;
21+
public Label errMsg;
22+
public Label val;
23+
public Label vars;
1724

1825
public void textFieldInput(KeyEvent keyEvent) {
1926
System.out.println(keyEvent.getEventType());
2027
simplified.setVisible(true);
2128
beautified.setVisible(true);
2229
addition.setVisible(true);
2330
exponential.setVisible(true);
31+
nodesBefore.setVisible(true);
32+
complexityBefore.setVisible(true);
33+
complexityAfter.setVisible(true);
34+
nodesAfter.setVisible(true);
35+
errMsg.setVisible(true);
36+
val.setVisible(true);
37+
vars.setVisible(true);
2438
System.out.println(input.textProperty().get());
2539
System.out.println(input.textProperty().get());
2640
}
41+
42+
void error(String msg) {
43+
String str = "...";
44+
this.addition.setText(str);
45+
this.exponential.setText(str);
46+
this.nodesBefore.setText(str);
47+
this.complexityBefore.setText(str);
48+
this.simplified.setText(str);
49+
this.beautified.setText(str);
50+
this.complexityAfter.setText(str);
51+
this.nodesAfter.setText(str);
52+
errMsg.setText(msg);
53+
}
2754
}

src/cas.fxml

Lines changed: 83 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,94 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3+
<?import java.lang.*?>
4+
<?import javafx.geometry.*?>
5+
<?import javafx.scene.control.*?>
6+
<?import javafx.scene.layout.*?>
7+
<?import javafx.scene.text.*?>
38
<?import javafx.geometry.Insets?>
49
<?import javafx.scene.control.Label?>
510
<?import javafx.scene.control.TextField?>
611
<?import javafx.scene.layout.ColumnConstraints?>
712
<?import javafx.scene.layout.GridPane?>
813
<?import javafx.scene.layout.RowConstraints?>
914
<?import javafx.scene.layout.VBox?>
15+
<?import javafx.scene.text.Font?>
1016

11-
<GridPane alignment="CENTER" minHeight="200.0" minWidth="400.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CasController">
12-
<VBox nodeOrientation="LEFT_TO_RIGHT" spacing="10.0" GridPane.hgrow="ALWAYS">
13-
<padding>
14-
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
15-
</padding>
16-
<opaqueInsets>
17-
<Insets />
18-
</opaqueInsets>
19-
<Label text="JMC CAS Experimental" />
20-
<TextField fx:id="input" onKeyTyped="#textFieldInput" promptText="Enter Expression">
21-
<VBox.margin>
22-
<Insets />
23-
</VBox.margin>
24-
</TextField>
25-
<GridPane>
26-
<columnConstraints>
27-
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
28-
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
29-
</columnConstraints>
30-
<rowConstraints>
31-
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
32-
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
33-
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
34-
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
35-
</rowConstraints>
36-
<Label fx:id="simplified" text="Simplified" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" />
37-
<Label fx:id="addition" text="Addition Form" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="1" />
38-
<Label fx:id="exponential" text="Exponential Form" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
39-
<Label fx:id="beautified" text="Beautified" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="3" />
40-
<Label text="Simplified:" />
41-
<Label text="Additional Only Form:" GridPane.rowIndex="1" />
42-
<Label text="Exponential Form:" GridPane.rowIndex="2" />
43-
<Label text="Beautified:" GridPane.rowIndex="3" />
44-
</GridPane>
45-
</VBox>
46-
<columnConstraints>
47-
<ColumnConstraints />
48-
</columnConstraints>
49-
<rowConstraints>
50-
<RowConstraints />
51-
</rowConstraints>
17+
<!--<?import ui.SelectableLabel?>-->
18+
19+
<GridPane alignment="CENTER" minHeight="400.0" minWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CasController">
20+
<columnConstraints>
21+
<ColumnConstraints />
22+
</columnConstraints>
23+
<rowConstraints>
24+
<RowConstraints />
25+
</rowConstraints>
26+
<children>
27+
<VBox nodeOrientation="LEFT_TO_RIGHT" spacing="10.0" GridPane.hgrow="ALWAYS">
28+
<padding>
29+
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
30+
</padding>
31+
<opaqueInsets>
32+
<Insets />
33+
</opaqueInsets>
34+
<children>
35+
<Label text="JMC CAS Experimental" />
36+
<TextField fx:id="input" onKeyTyped="#textFieldInput" promptText="Enter Expression">
37+
<VBox.margin>
38+
<Insets />
39+
</VBox.margin>
40+
</TextField>
41+
<GridPane hgap="5.0">
42+
<columnConstraints>
43+
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
44+
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
45+
</columnConstraints>
46+
<rowConstraints>
47+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
48+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
49+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
50+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
51+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
52+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
53+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
54+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
55+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
56+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
57+
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
58+
</rowConstraints>
59+
<children>
60+
61+
62+
<!--<SelectableLabel text="Selectable"/>-->
63+
<Label fx:id="addition" text="Addition Form" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" />
64+
<Label fx:id="exponential" text="Exponential Form" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="1" />
65+
<Label fx:id="simplified" text="Simplified" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="4" />
66+
<Label fx:id="beautified" text="Beautified" textFill="#20a822" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="5">
67+
<font>
68+
<Font name="System Bold" size="13.0" />
69+
</font>
70+
</Label>
71+
<Label text="Additional Only Form:" />
72+
<Label text="Exponential Form:" GridPane.rowIndex="1" />
73+
<Label text="Simplified:" GridPane.rowIndex="4" />
74+
<Label text="Beautified:" GridPane.rowIndex="5" />
75+
<Label text="# Nodes:" GridPane.rowIndex="2" />
76+
<Label fx:id="nodesBefore" text="# nodes" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
77+
<Label text="Complexity:" GridPane.rowIndex="3" />
78+
<Label fx:id="complexityBefore" text="complexity" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="3" />
79+
<Label text="# Nodes (Simplified):" GridPane.rowIndex="6" />
80+
<Label fx:id="nodesAfter" text="nodes (after)" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="6" />
81+
<Label text="Complexity (Simplified):" GridPane.rowIndex="7" />
82+
<Label fx:id="complexityAfter" text="complexity (after)" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="7" />
83+
<Label text="Error Message:" GridPane.rowIndex="8" />
84+
<Label fx:id="errMsg" text="&quot;&quot;" textFill="#f80909" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="8" />
85+
<Label text="Raw Value:" GridPane.rowIndex="9" />
86+
<Label fx:id="val" text="val" textFill="#0c49ff" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="9" />
87+
<Label text="Variables:" GridPane.rowIndex="10" />
88+
<Label fx:id="vars" text="vars" visible="false" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="10" />
89+
</children>
90+
</GridPane>
91+
</children>
92+
</VBox>
93+
</children>
5294
</GridPane>

src/jmc/cas/RawValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public String toString() {
4242
return "undef";
4343
} else if (isInteger()) {
4444
String s = Integer.toString(intValue());
45-
if (s.length() <= 6) return s;
45+
if (s.length() <= 10) return s;
4646
} else {
4747
String s = Double.toString(doubleValue());
48-
if (s.length() <= 6) return s;
48+
if (s.length() <= 10) return s;
4949
}
5050
double extracted = number.doubleValue();
5151
String formatted = Graph.formatForDisplay(extracted);

src/ui/SelectableLabel.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ui; /**
2+
* Created by Jiachen on 3/12/18.
3+
* JavaFX label is not selectable! Here's workaround found on StackOverflow...
4+
*/
5+
6+
import javafx.scene.control.TextArea;
7+
import javafx.scene.layout.StackPane;
8+
import javafx.scene.text.Text;
9+
10+
public class SelectableLabel extends TextArea {
11+
12+
public SelectableLabel() {
13+
this("", true);
14+
}
15+
16+
public SelectableLabel(String text) {
17+
this(text, true);
18+
}
19+
20+
public SelectableLabel(String text, boolean selectable) {
21+
super(text);
22+
23+
this.setMouseTransparent(!selectable);
24+
this.setEditable(false);
25+
this.setFocusTraversable(false);
26+
this.setWrapText(true);
27+
28+
sizeToText();
29+
30+
textProperty().addListener(e -> sizeToText());
31+
}
32+
33+
public void setSelectable(boolean selectable) {
34+
setMouseTransparent(!selectable);
35+
}
36+
37+
private void sizeToText() {
38+
Text t = new Text(getText());
39+
t.setFont(this.getFont());
40+
StackPane pane = new StackPane(t);
41+
pane.layout();
42+
double width = t.getLayoutBounds().getWidth();
43+
double height = t.getLayoutBounds().getHeight();
44+
this.setMaxSize(width, height);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)