Created
June 7, 2022 20:07
-
-
Save MacoChave/eb23e4217aebd81c8d09e1b67148d27f to your computer and use it in GitHub Desktop.
Editor con JavaFX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.geometry.Insets?> | |
<?import javafx.scene.control.Label?> | |
<?import javafx.scene.control.Menu?> | |
<?import javafx.scene.control.MenuBar?> | |
<?import javafx.scene.control.MenuItem?> | |
<?import javafx.scene.control.TextArea?> | |
<?import javafx.scene.layout.BorderPane?> | |
<?import javafx.scene.layout.ColumnConstraints?> | |
<?import javafx.scene.layout.GridPane?> | |
<?import javafx.scene.layout.HBox?> | |
<?import javafx.scene.layout.RowConstraints?> | |
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="768.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.compi.HelloController"> | |
<top> | |
<MenuBar BorderPane.alignment="CENTER"> | |
<menus> | |
<Menu mnemonicParsing="false" text="Archivo"> | |
<items> | |
<MenuItem mnemonicParsing="false" text="Nuevo" onAction="#handleNew" /> | |
<MenuItem mnemonicParsing="false" text="Abrir" onAction="#handleOpen" /> | |
<MenuItem mnemonicParsing="false" text="Guardar" onAction="#handleSave" /> | |
<MenuItem mnemonicParsing="false" text="Salir" onAction="#handleExit" /> | |
</items> | |
</Menu> | |
</menus> | |
</MenuBar> | |
</top> | |
<bottom> | |
<HBox prefWidth="200.0" spacing="10.0" BorderPane.alignment="CENTER"> | |
<children> | |
<Label text="Posicion del cursor" /> | |
<Label text="x,y" fx:id="cursorPosition" /> | |
</children> | |
<padding> | |
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | |
</padding> | |
</HBox> | |
</bottom> | |
<center> | |
<GridPane hgap="10.0" vgap="10.0" BorderPane.alignment="CENTER"> | |
<columnConstraints> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | |
</columnConstraints> | |
<rowConstraints> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
</rowConstraints> | |
<children> | |
<TextArea prefHeight="200.0" prefWidth="200.0" fx:id="sourceText" promptText="Source code" onKeyPressed="#changeSourceText"> | |
<padding> | |
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | |
</padding> | |
</TextArea> | |
<TextArea prefHeight="200.0" prefWidth="200.0" fx:id="resultText" promptText="Results here" style="-fx-background-color: #1e1e1e; -fx-foreground-color: #ffffff;" editable="false" wrapText="true" GridPane.columnIndex="1"> | |
<padding> | |
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | |
</padding> | |
</TextArea> | |
</children> | |
<padding> | |
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | |
</padding> | |
</GridPane> | |
</center> | |
</BorderPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.compi; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
import java.io.IOException; | |
public class HelloApplication extends Application { | |
@Override | |
public void start(Stage stage) throws IOException { | |
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); | |
Scene scene = new Scene(fxmlLoader.load(), 1024, 768); | |
stage.isMaximized(); | |
stage.isResizable(); | |
stage.setTitle("Compi Editor"); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
public static void main(String[] args) { | |
launch(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.compi; | |
import Grammar.JLexer; | |
import Grammar.JParser; | |
import javafx.fxml.FXML; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TextArea; | |
import javafx.stage.FileChooser; | |
import org.antlr.v4.runtime.CharStream; | |
import org.antlr.v4.runtime.CommonTokenStream; | |
import java.io.*; | |
import static org.antlr.v4.runtime.CharStreams.fromString; | |
public class HelloController { | |
@FXML | |
private TextArea sourceText; | |
@FXML | |
private TextArea resultText; | |
@FXML | |
private Label cursorPosition; | |
@FXML | |
private void handleNew() { | |
sourceText.setText(""); | |
resultText.setText(""); | |
} | |
@FXML | |
private void handleOpen() { | |
FileChooser fileChooser = new FileChooser(); | |
fileChooser.setTitle("Open File"); | |
fileChooser.getExtensionFilters().addAll( | |
new FileChooser.ExtensionFilter("Text Files", "*.txt"), | |
new FileChooser.ExtensionFilter("Java Files", "*.java") | |
); | |
fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); | |
File file = fileChooser.showOpenDialog(null); | |
try { | |
BufferedReader reader = new BufferedReader(new FileReader(file)); | |
String line; | |
StringBuilder sb = new StringBuilder(); | |
while ((line = reader.readLine()) != null) { | |
sb.append(line).append("\n"); | |
} | |
sourceText.setText(sb.toString()); | |
} catch (FileNotFoundException e) { | |
resultText.setText("File not found"); | |
} catch (IOException e) { | |
resultText.setText("Error reading file"); | |
} | |
} | |
@FXML | |
private void handleSave() { | |
FileChooser fileChooser = new FileChooser(); | |
fileChooser.setTitle("Save File"); | |
fileChooser.getExtensionFilters().addAll( | |
new FileChooser.ExtensionFilter("Text Files", "*.txt"), | |
new FileChooser.ExtensionFilter("Java Files", "*.java") | |
); | |
fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); | |
File file = fileChooser.showSaveDialog(null); | |
try { | |
BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | |
writer.write(sourceText.getText()); | |
writer.close(); | |
} catch (IOException e) { | |
resultText.setText("Error writing file"); | |
} | |
} | |
@FXML | |
private void handleExit() { | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment