JavaFX/controller/Traduction.java

185 lines
4.7 KiB
Java

package controller;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import Classes.Category;
import Interfaces.EventObserver;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.TextField;
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 javafx.util.Pair;
import model.NewsModel;
import model.WordTraductionModel;
public class Traduction{
/* Data */
private ArrayList<AnchorPane> items;
private FlowPane parent;
private EventObserver observer;
private AnchorPane root;
public Boolean activated = false;
/* Constructor */
public Traduction(FlowPane p_parent, EventObserver observer, AnchorPane root_layout){
this.parent = p_parent;
this.items = new ArrayList<AnchorPane>();
this.observer = observer;
this.root = root_layout;
}
public synchronized void addAll(ArrayList<Pair<WordTraductionModel, WordTraductionModel>> models) throws IOException {
/* (9) Add to the controller local */
TextField tx = (TextField) this.root.lookup("#mag_searchbar");
int i = 0;
for(Pair<WordTraductionModel, WordTraductionModel> news : models) {
tx.textProperty().set("Chargement... "+i+"/"+models.size());
this.items.add(this.buildItem(news.getKey()));
i++;
}
/* (11) Add to parent (graphics) */
Platform.runLater(new Runnable(){
public void run(){
if(Traduction.this.activated) {
Traduction.this.parent.getChildren().addAll(Traduction.this.items);
tx.textProperty().set("");
}
}
});
}
public AnchorPane buildItem(WordTraductionModel model) 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( model.getSense(), item );
/* (4) Set date */
//this.gsetDate( model.getDate(), item );
/* (5) Set title */
HBox headerContainer = (HBox) item.getChildren().get(3);
this.gsetTitle( model.getUsage(), headerContainer );
/* (6) Set tags */
this.gsetTags( model.getPOS(), headerContainer );
/* (7) Set image */
//this.gsetImage( model.getImageURL(), item );
/* (8) Bind event */
// item.setOnMousePressed(new EventHandler<MouseEvent>() {
// @Override
// public void handle(MouseEvent event) {
// Article.this.observer.handleEvent(new Classes.Event(item.getId(),"changeMainLayout"));
// }
// });
/* (10) On bind au width du parent */
item.prefWidthProperty().bind(this.parent.widthProperty());
item.maxWidthProperty().bind(this.parent.widthProperty());
return item;
}
public void clearContent() {
this.items.clear();
Platform.runLater(new Runnable(){
public void run(){
Iterator<Node> it = Traduction.this.parent.getChildren().iterator();
while(it.hasNext()) {
Node n = it.next();
if(n.getId() != "mag_searchbar") {
it.remove();
}
}
}
});
}
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(new SimpleDateFormat("dd-MM-yyyy HH:mm").format(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 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(Exception e) {
g_image.setImage(new Image("http://lorempicsum.com/futurama/255/200/2"));
}
}
public void gsetTags(String p_tags, HBox p_parent) throws IOException{
/* (1.1) Create the container */
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/article_tag_disp.fxml"));
/* (1.2) Load the tag elements */
FlowPane g_tag = (FlowPane) loader.load();
Text g_tagText = (Text) g_tag.getChildren().get(0);
/* (1.3) Update the tag name */
g_tagText.setText(p_tags);
/* (1.5) Ajout au parent*/
p_parent.getChildren().add(g_tag);
}
}