JavaFX/controller/Article.java

156 lines
3.7 KiB
Java

package controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import Classes.Category;
import Interfaces.EventObserver;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import model.NewsModel;
public class Article{
/* Data */
private ArrayList<AnchorPane> items;
private FlowPane parent;
private EventObserver observer;
/* Constructor */
public Article(FlowPane p_parent, EventObserver observer){
/* (1) Attributes
---------------------------------------------------------*/
this.parent = p_parent;
this.items = new ArrayList<AnchorPane>();
this.observer = observer;
}
public synchronized void addItem(NewsModel model) throws IOException {
/* (1) Get the loaded item*/
FXMLLoader loader = new FXMLLoader( getClass().getResource("/fxml/article_disp.fxml") );
AnchorPane item = (AnchorPane) loader.load();
/* (2) Set content */
this.gsetContent( model.getDescription(), item );
/* (3) Set date */
this.gsetDate( model.getDate(), item );
/* (4) Set title */
HBox headerContainer = (HBox) item.getChildren().get(3);
this.gsetTitle( model.getTitle(), headerContainer );
/* (5) Set tags */
this.gsetTags( model.getTags(), headerContainer );
/* (6) Set image */
this.gsetImage( model.getImageURL(), item );
/* (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());
}
public void display(){
Platform.runLater(new Runnable(){
public void run(){
for( AnchorPane item : items )
Article.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(Date 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.toString());
}
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 gsetImage(String p_uri, AnchorPane p_parent){
/* (1) Get node */
ImageView g_image = (ImageView) p_parent.getChildren().get(0);
/* (2) Update title */
try{
g_image.setImage(new Image(p_uri));
}catch(IllegalArgumentException wrongurlex){
System.out.println("Cannot find image URL: '"+p_uri+"'");
}
}
public void gsetTags(ArrayList<Category> p_tags, HBox p_parent) throws IOException{
/* (1) For each tag -> load a new one */
for( Category tag : p_tags ){
/* (1.1) Load the tag elements */
FXMLLoader loader = new FXMLLoader( getClass().getResource("/fxml/article_tag_disp.fxml") );
FlowPane g_tag = (FlowPane) loader.load();
Text g_tagText = (Text) g_tag.getChildren().get(0);
/* (1.2) Update the tag name */
g_tagText.setText(tag.getLabel());
/* (1.3) Set the custom color */
g_tag.setStyle("-fx-background-color: "+tag.getColor());
/* (1.4) Ajout au parent*/
p_parent.getChildren().add(g_tag);
}
}
}