Verified Commit d6726b41 authored by Jonas Sulzer's avatar Jonas Sulzer 馃帗
Browse files

馃摝 NEW: correctly set up project



Signed-off-by: Jonas Sulzer's avatarJonas Sulzer <jonas@violoncello.ch>
parent f511dc6b
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="089645f1-f143-408a-be57-e098b4773895" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/.gitignore" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/08-tableur.iml" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="28NbeKv4EqmvhULIUnAW8lneCVa" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="089645f1-f143-408a-be57-e098b4773895" name="Changes" comment="" />
<created>1651060086144</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1651060086144</updated>
</task>
<servers />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="OpenJFX 18" level="application" />
</component>
</module>
\ No newline at end of file
package cs108;
import java.util.List;
import java.util.function.IntBinaryOperator;
public final class Cell {
private final int column, row;
private String contentString;
private List<Cell> arguments;
private IntBinaryOperator operator;
private int value;
public Cell(int column, int row, int initialValue) {
this.column = column;
this.row = row;
this.contentString = String.valueOf(initialValue);
this.arguments = List.of();
this.operator = (x, y) -> initialValue;
this.value = initialValue;
}
public int getColumn() {
return column;
}
public int getRow() {
return row;
}
public String getName() {
return String.format("%c%d", 'A' + column, row + 1);
}
public int getValue() {
return value;
}
public void setValue(int newValue) {
value = newValue;
}
public String getFormulaString() {
return contentString;
}
public void setFormula(String newContentString, List<Cell> newArguments, IntBinaryOperator newOperator) {
contentString = newContentString;
arguments = newArguments;
operator = newOperator;
}
}
package cs108;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.IntBinaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class Spreadsheet extends Application {
private static final int SIZE = 9;
private static final Pattern valuePat =
Pattern.compile("\\d+");
private static final Pattern formulaPat =
Pattern.compile("=([A-Ia-i]\\d)([/*+-])([A-Ia-i]\\d)");
private final List<Cell> cells;
private final ObjectProperty<Cell> selectedCellProperty;
public static void main(String[] args) {
launch(args);
}
public Spreadsheet() {
List<Cell> cells = new ArrayList<>(SIZE * SIZE);
for (int r = 0; r < SIZE; ++r) {
for (int c = 0; c < SIZE; ++c) {
cells.add(new Cell(c, r, 0));
}
}
this.cells = cells;
this.selectedCellProperty = new SimpleObjectProperty<>(cells.get(0));
}
@Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
gridPane.setHgap(1);
gridPane.setVgap(1);
gridPane.setPadding(new Insets(5));
// Add column/row headers
for (int r = 1; r <= SIZE; ++r)
gridPane.add(new Label(String.valueOf(r)), 0, r);
for (int c = 1; c <= SIZE; ++c)
gridPane.add(new Label(String.valueOf((char) ('A' + c - 1))), c, 0);
// Add cell buttons
for (Cell cell : cells) {
Button cellButton = new Button(String.valueOf(cell.getValue()));
cellButton.setPrefWidth(70);
cellButton.setOnAction(e -> selectedCellProperty.set(cell));
gridPane.add(cellButton, cell.getColumn() + 1, cell.getRow() + 1);
// TODO: add observer to the cell, and update button's text with new value
}
// Add selected cell name and edit field
Label selectedCellName = new Label();
selectedCellName.textProperty().bind(Bindings.select(selectedCellProperty, "name"));
gridPane.add(selectedCellName, 0, SIZE + 1);
TextField formulaField = new TextField(selectedCellProperty.get().getFormulaString());
formulaField.setPrefColumnCount(3);
formulaField.setOnAction(e -> setFormula(selectedCellProperty.get(), formulaField.getText()));
selectedCellProperty.addListener((p, o, n) -> formulaField.setText(n.getFormulaString()));
gridPane.add(formulaField, 1, SIZE + 1, SIZE, 1);
Scene scene = new Scene(gridPane);
primaryStage.setScene(scene);
primaryStage.show();
}
private void setFormula(Cell cell, String newContent) {
Matcher valueM = valuePat.matcher(newContent);
Matcher formulaM = formulaPat.matcher(newContent);
if (valueM.matches()) {
// Constant value
int value = Integer.parseInt(newContent);
cell.setFormula(newContent, List.of(), (x, y) -> value);
} else if (formulaM.matches()) {
// Formula
Cell c1 = parseCell(formulaM.group(1));
Cell c2 = parseCell(formulaM.group(3));
IntBinaryOperator fun = parseFun(formulaM.group(2));
cell.setFormula(newContent, Arrays.asList(c1, c2), fun);
} else
// Nothing valid
java.awt.Toolkit.getDefaultToolkit().beep();
}
private Cell parseCell(String cellName) {
int c = Character.toUpperCase(cellName.charAt(0)) - 'A';
int r = Integer.parseInt(cellName.substring(1)) - 1;
return cells.get(r * SIZE + c);
}
private IntBinaryOperator parseFun(String op) {
return switch (op.charAt(0)) {
case '+' -> (x, y) -> x + y;
case '-' -> (x, y) -> x - y;
case '*' -> (x, y) -> x * y;
case '/' -> (x, y) -> x / y;
default -> throw new Error();
};
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment