Skip to content

Commit

Permalink
Gestion des exemples par fichier json
Browse files Browse the repository at this point in the history
  • Loading branch information
germainsip committed Jul 17, 2020
1 parent f925c19 commit f9590b0
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 54 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ plugins {

group 'org.gerblog'
version '1.0-SNAPSHOT'
sourceSets.main.resources.srcDirs("src/main/java").includes.addAll(["**/*.fxml", "**/*.css","**/*.png"])
compileJava.options.encoding = "UTF-8"
sourceSets.main.resources.srcDirs("src/main/java").includes.addAll(["**/*.fxml", "**/*.css","**/*.png","**/*.json"])
repositories {
mavenCentral()
}
Expand All @@ -15,6 +16,7 @@ dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'

compile 'com.jfoenix:jfoenix:9.0.10'
compile 'com.fasterxml.jackson.core:jackson-databind:2.11.1'
}

javafx {
Expand Down
61 changes: 25 additions & 36 deletions src/main/java/org/gerblog/gui/MainController.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package org.gerblog.gui;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.effects.JFXDepthManager;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.effect.InnerShadow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.gerblog.gui.exemple.ExempleTileController;
import org.gerblog.libexemple.Exemple;
import org.gerblog.tools.Exempliser;
import org.gerblog.tools.GuiTool;

import java.awt.*;
import java.io.IOException;
Expand All @@ -29,6 +23,7 @@ public class MainController implements Initializable {
public Hyperlink lienCours;
public VBox listBox;
final static Exemple ex = new Exemple();
public Hyperlink lienJavadoc;

/**
* La méthode prend le texte du bouton et envoie le fxml correspondant
Expand All @@ -41,7 +36,7 @@ public static void ouvreSceneExemple(ActionEvent actionEvent) throws IOException
String evenement = btn.getText();
String action = switch (evenement) {
//inscrire ici le couple nom du bouton et package/nom du fxml
case "Les boutons" -> "buttons/les_boutons";
case "Les Boutons" -> "buttons/les_boutons";
case "Les champs texte" -> "champs/les_champs";
//case "Les Sliders" -> "sliders/les_sliders";
default -> "default";
Expand All @@ -68,54 +63,48 @@ public static Parent loadFenExemple(String chemin) throws IOException {

@Override
public void initialize(URL location, ResourceBundle resources) {
Exempliser ex1 = new Exempliser();
System.out.println(ex1.exTab[0].getComment());
lienCours.setOnAction(event -> {
try {
Desktop.getDesktop().browse(URI.create("https://germainsip.github.io/post/cours/java/javafxelmts/"));
} catch (IOException e) {
e.printStackTrace();
}
});
lienJavadoc.setOnAction(event -> {
try {
Desktop.getDesktop().browse(URI.create("https://openjfx.io/javadoc/14/"));
} catch (IOException e) {
e.printStackTrace();
}
});

for (String key : ex.elmts.keySet()) {
/* for (String key : ex.elmts.keySet()) {
try {
genExemple(key, ex.elmts.get(key));
GuiTool.genExemple(key, ex.elmts.get(key),listBox);
} catch (IOException e) {
e.printStackTrace();
}
}*/
for (Exemple exT : ex1.exTab)
{
try {
GuiTool.genExemple(exT.getName(), exT.getComment(),listBox);
} catch (IOException e) {
e.printStackTrace();
}
}


}

private void genExemple(String name, String comment) throws IOException {
/*HBox boiteExemple = new HBox();
JFXDepthManager.setDepth(boiteExemple, 1);
JFXButton button = new JFXButton(name);
button.setStyle("-fx-background-color: #88BA3F");
button.setPrefWidth(200);
button.setAlignment(Pos.CENTER_LEFT);
button.setOnAction(actionEvent -> {
try {
ouvreSceneExemple(actionEvent);
} catch (IOException e) {
e.printStackTrace();
}
});
boiteExemple.setSpacing(10);
boiteExemple.setPrefWidth(539);
boiteExemple.setPrefHeight(47);
boiteExemple.setPadding(new Insets(10));
boiteExemple.setAlignment(Pos.CENTER_LEFT);
//InnerShadow ombre = new InnerShadow();
boiteExemple.setStyle("-fx-background-color: white");
//boiteExemple.setEffect(ombre);
/*private void genExemple(String name, String comment) throws IOException {
boiteExemple.getChildren().addAll(button, new Label(comment));*/
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/org/gerblog/gui/exemple/exemple_tile.fxml"));
Parent ex = fxmlLoader.load();
ExempleTileController controller = fxmlLoader.<ExempleTileController>getController();
controller.setValues(name,comment);
// listBox.getChildren().add(boiteExemple);
listBox.getChildren().add(ex);
}
}*/
}
23 changes: 12 additions & 11 deletions src/main/java/org/gerblog/gui/buttons/les_boutons.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.web.WebView?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.gerblog.gui.buttons.LesBoutonsController">
<BorderPane prefHeight="336.0" prefWidth="515.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.gerblog.gui.buttons.LesBoutonsController">
<BorderPane prefHeight="336.0" prefWidth="515.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<HBox alignment="CENTER_LEFT" prefHeight="27.0" prefWidth="308.0" spacing="20.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
<Button fx:id="btn1" mnemonicParsing="false" onAction="#writeOnLabel" text="Bouton 1"/>
<Button fx:id="btn2" mnemonicParsing="false" onAction="#writeOnLabel" text="Bouton 2"/>
<Button disable="true" mnemonicParsing="false" text="Bouton 3"/>
<Button fx:id="btn1" mnemonicParsing="false" onAction="#writeOnLabel" text="Bouton 1" />
<Button fx:id="btn2" mnemonicParsing="false" onAction="#writeOnLabel" text="Bouton 2" />
<Button disable="true" mnemonicParsing="false" text="Bouton 3" />
<Label fx:id="res" text="Résultat">
<HBox.margin>
<Insets left="60.0"/>
<Insets left="60.0" />
</HBox.margin>
</Label>
</HBox>
</top>
<center>
<WebView fx:id="buttonWebview" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"/>
<WebView fx:id="buttonWebview" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<effect>
<InnerShadow />
</effect></WebView>
</center>
</BorderPane>
</AnchorPane>
9 changes: 6 additions & 3 deletions src/main/java/org/gerblog/gui/exemple/exemple_tile.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>

<AnchorPane prefWidth="300.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.gerblog.gui.exemple.ExempleTileController">
<AnchorPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.gerblog.gui.exemple.ExempleTileController">
<children>
<HBox fx:id="boitePrincipale" alignment="CENTER_LEFT" prefHeight="27.0" prefWidth="300.0" spacing="80.0" style="-fx-background-color: white;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<HBox fx:id="boitePrincipale" alignment="CENTER_LEFT" prefHeight="27.0" spacing="20.0" style="-fx-background-color: white;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<JFXButton fx:id="genBtn" prefHeight="27.0" prefWidth="200.0" style="-fx-background-color: #88BA3F;" text="TestButt" textFill="WHITE">
<font>
<Font name="Arial Bold" size="13.0" />
</font></JFXButton>
<Label fx:id="commentLab" text="Label" />
<Label fx:id="commentLab" prefWidth="300.0" text="Label" wrapText="true" />
<ImageView fitHeight="90.0" fitWidth="31.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@simple_logo.png" />
</image>
<effect>
<InnerShadow />
</effect>
<HBox.margin>
<Insets left="20.0" />
</HBox.margin>
</ImageView>
</children>
<padding>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/gerblog/gui/main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</Tab>
</tabs>
</TabPane>
<HBox layoutX="14.0" layoutY="6.0">
<HBox layoutX="14.0" layoutY="6.0" spacing="8.0">
<children>
<Label text="Les éléments de JFX">
<font>
Expand All @@ -61,9 +61,10 @@
</Label>
<Hyperlink fx:id="lienCours" text="le cours">
<HBox.margin>
<Insets left="350.0" />
<Insets left="300.0" />
</HBox.margin>
</Hyperlink>
<Hyperlink fx:id="lienJavadoc" text="doc javaFX" />
</children>
</HBox>
</children>
Expand Down
36 changes: 35 additions & 1 deletion src/main/java/org/gerblog/libexemple/Exemple.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@
import java.util.Map;

public class Exemple {
private int id;
private String name;
private String comment;

public Exemple(int id, String name, String comment) {
this.id = id;
this.name = name;
this.comment = comment;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}

public Map<String, String> elmts = new HashMap<>();

public Exemple() {
Expand All @@ -16,7 +50,7 @@ public Exemple() {
"Les textField et la vérification de ceux ci"
);this.elmts.put(
"Les Sliders",
"Les textField et la vérification de ceux ci"
"Les Slider et quelques exemples d'application"
);
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/gerblog/libexemple/liste_exemples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"id": "1",
"name": "Les Boutons",
"comment": "Exemple de code sur les boutons JavaFX"
},
{
"id": "2",
"name": "Les champs texte",
"comment": "Les textField et la vérification de ceux ci"
},
{
"id" : "3",
"name": "Les Sliders",
"comment": "Les Slider et quelques exemples d'application"
},
{
"id" : "4",
"name": "Le Date picker",
"comment": "test hgkjhgkhjg"
}
]

37 changes: 37 additions & 0 deletions src/main/java/org/gerblog/tools/Exempliser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.gerblog.tools;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.gerblog.libexemple.Exemple;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;

public class Exempliser {
//List<Exemple> exempleList;
public Exemple[] exTab;
public Exempliser() {

// this.exempleList = exempleList;

//FileReader reader = new FileReader(file);
try {
File file = new File(getClass().getResource("/org/gerblog/libexemple/liste_exemples.json").getPath());
FileReader fileReader = new FileReader(file);

ObjectMapper mapper = new ObjectMapper();
this.exTab = mapper.readValue(file,Exemple[].class);
//BufferedReader br = new BufferedReader(fileReader);

// String st;
// while ((st = br.readLine()) != null)
// System.out.println(st);
} catch (IOException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}

}
}
26 changes: 26 additions & 0 deletions src/main/java/org/gerblog/tools/GuiTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.gerblog.tools;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.layout.VBox;
import org.gerblog.gui.exemple.ExempleTileController;

import java.io.IOException;

public class GuiTool {
/**
* Génère une tuile avec bouton et commentaire
* @param name nom du bouton
* @param comment commentaire lié au bouton
* @param listBox VBOX dans laquelle est intégré la tuile
* @throws IOException
*/
public static void genExemple(String name, String comment, VBox listBox) throws IOException {

FXMLLoader fxmlLoader = new FXMLLoader(GuiTool.class.getResource("/org/gerblog/gui/exemple/exemple_tile.fxml"));
Parent ex = fxmlLoader.load();
ExempleTileController controller = fxmlLoader.<ExempleTileController>getController();
controller.setValues(name,comment);
listBox.getChildren().add(ex);
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/gerblog/tools/Verification.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ public static boolean verifNum(String champ){
return num.matcher(champ).matches();
}
}


0 comments on commit f9590b0

Please sign in to comment.