JavaFX/controller/Article.java

135 lines
3.4 KiB
Java

package controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import Classes.TagType;
import Interfaces.EventObserver;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
public class Article{
/* Data */
private ArrayList<AnchorPane> items;
private FlowPane parent;
private EventObserver observer;
/* Constructor */
public Article(FlowPane p_parent, EventObserver observer){
this.parent = p_parent;
this.items = new ArrayList<AnchorPane>();
this.observer = observer;
}
public void addItem(String p_title, String p_content, Date p_publ, ArrayList<TagType> p_tags) throws IOException {
/* (1) Load the article_disp.fxml */
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/article_disp.fxml"));
/* (2) Get the loaded item*/
AnchorPane item = (AnchorPane) loader.load();
/* (3) Set content */
this.gsetContent( p_content, item );
/* (4) Set date */
this.gsetDate( p_publ.toString(), item );
/* (5) Set title */
HBox headerContainer = (HBox) item.getChildren().get(3);
this.gsetTitle( p_title, headerContainer );
/* (6) Set tags */
this.gsetTags( p_tags, headerContainer );
/* (7) Bind event */
item.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Article.this.observer.handleEvent(new Classes.Event(item.getId(),"changeMainLayout"));
}
});
/* (8) Add to the controller local */
this.items.add(item);
/* (9) On bind au width du parent */
item.prefWidthProperty().bind(this.parent.widthProperty());
item.maxWidthProperty().bind(this.parent.widthProperty());
/* (10) Add to parent (graphics) */
this.parent.getChildren().add(item);
}
public void gsetContent(String p_content, AnchorPane p_parent){
/* (1) Get node */
Text g_content = (Text) p_parent.getChildren().get(1);
/* (2) Update content */
g_content.setText(p_content);
}
public void gsetDate(String p_date, AnchorPane p_parent){
/* (1) Get node */
Text g_date = (Text) p_parent.getChildren().get(2);
/* (2) Update content */
g_date.setText(p_date);
}
public void gsetTitle(String p_title, HBox p_parent){
/* (1) Get node */
Text g_title = (Text) p_parent.getChildren().get(0);
/* (2) Update title */
g_title.setText(p_title);
}
public void gsetTags(ArrayList<TagType> p_tags, HBox p_parent) throws IOException{
/* (1) For each tag -> load a new one */
for( TagType tag : p_tags ){
/* (1.1) Create the container */
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/article_tag_disp.fxml"));
/* (1.2) Load the tag elements */
Pane g_tag = (Pane) loader.load();
Text g_tagText = (Text) g_tag.getChildren().get(0);
/* (1.3) Update the tag name */
g_tagText.setText(tag.getLabel());
/* (1.4) Set the custom color */
g_tag.setStyle("-fx-background-color: "+tag.getColor());
/* (1.5) Ajout au parent*/
p_parent.getChildren().add(g_tag);
}
}
}