From 92b4b3c4205d58a4dea11561e876509f4a22ea7f Mon Sep 17 00:00:00 2001 From: SeekDaSky Date: Wed, 20 Dec 2017 16:51:52 +0100 Subject: [PATCH] =?UTF-8?q?impl=C3=A9mentation=20de=20la=20copie=20dans=20?= =?UTF-8?q?le=20rpesse=20papier=20des=20URL=20d'article?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Classes/Toast.java | 63 ++++++++++++++++++++++++++++++++++++++ controller/Article.java | 29 +++++++++++++----- controller/RootLayout.java | 2 +- 3 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 Classes/Toast.java diff --git a/Classes/Toast.java b/Classes/Toast.java new file mode 100644 index 0000000..109941e --- /dev/null +++ b/Classes/Toast.java @@ -0,0 +1,63 @@ +package Classes; + +import javafx.animation.KeyFrame; +import javafx.animation.KeyValue; +import javafx.animation.Timeline; +import javafx.scene.Scene; +import javafx.scene.layout.StackPane; +import javafx.scene.paint.Color; +import javafx.scene.text.Font; +import javafx.scene.text.Text; +import javafx.stage.Stage; +import javafx.stage.StageStyle; +import javafx.util.Duration; + +public final class Toast +{ + public static void makeText(Stage ownerStage, String toastMsg, int toastDelay, int fadeInDelay, int fadeOutDelay) + { + Stage toastStage=new Stage(); + toastStage.setY(ownerStage.getHeight()); + toastStage.initOwner(ownerStage); + toastStage.setResizable(false); + toastStage.initStyle(StageStyle.TRANSPARENT); + + Text text = new Text(toastMsg); + text.setFont(Font.font("Verdana", 15)); + text.setFill(Color.gray(0.1)); + + StackPane root = new StackPane(text); + root.setStyle("-fx-background-radius: 5; -fx-background-color: rgba(200, 200, 200, 0.5); -fx-padding: 20px;"); + root.setOpacity(0); + + + Scene scene = new Scene(root); + scene.setFill(Color.TRANSPARENT); + toastStage.setScene(scene); + toastStage.show(); + + Timeline fadeInTimeline = new Timeline(); + KeyFrame fadeInKey1 = new KeyFrame(Duration.millis(fadeInDelay), new KeyValue (toastStage.getScene().getRoot().opacityProperty(), 1)); + fadeInTimeline.getKeyFrames().add(fadeInKey1); + fadeInTimeline.setOnFinished((ae) -> + { + new Thread(() -> { + try + { + Thread.sleep(toastDelay); + } + catch (InterruptedException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Timeline fadeOutTimeline = new Timeline(); + KeyFrame fadeOutKey1 = new KeyFrame(Duration.millis(fadeOutDelay), new KeyValue (toastStage.getScene().getRoot().opacityProperty(), 0)); + fadeOutTimeline.getKeyFrames().add(fadeOutKey1); + fadeOutTimeline.setOnFinished((aeb) -> toastStage.close()); + fadeOutTimeline.play(); + }).start(); + }); + fadeInTimeline.play(); + } +} \ No newline at end of file diff --git a/controller/Article.java b/controller/Article.java index 637547d..15ed55e 100644 --- a/controller/Article.java +++ b/controller/Article.java @@ -1,23 +1,29 @@ package controller; +import java.awt.Toolkit; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.StringSelection; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; - import Classes.Category; +import Classes.Toast; import Interfaces.EventObserver; import javafx.application.Platform; +import javafx.event.EventHandler; 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.input.MouseEvent; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.FlowPane; import javafx.scene.layout.HBox; import javafx.scene.text.Text; +import javafx.stage.Stage; import model.NewsModel; public class Article{ @@ -28,14 +34,16 @@ public class Article{ private EventObserver observer; private AnchorPane root; public Boolean activated = false; + private Stage stage; /* Constructor */ - public Article(FlowPane p_parent, EventObserver observer, AnchorPane root_layout){ + public Article(FlowPane p_parent, EventObserver observer, AnchorPane root_layout, Stage stage){ this.parent = p_parent; this.items = new ArrayList(); this.observer = observer; this.root = root_layout; + this.stage = stage; } public synchronized void addAllItem(ArrayList models) throws IOException { @@ -89,12 +97,17 @@ public class Article{ this.gsetImage( model.getImageURL(), item ); /* (8) Bind event */ -// item.setOnMousePressed(new EventHandler() { -// @Override -// public void handle(MouseEvent event) { -// Article.this.observer.handleEvent(new Classes.Event(item.getId(),"changeMainLayout")); -// } -// }); + item.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + StringSelection selection = new StringSelection(model.getNewsURL()); + Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + clipboard.setContents(selection, selection); + + Toast.makeText(Article.this.stage, "Adresse de l'article copiƩ", 2000, 200, 200); + + } + }); /* (10) On bind au width du parent */ item.prefWidthProperty().bind(this.parent.widthProperty()); diff --git a/controller/RootLayout.java b/controller/RootLayout.java index 29c6f2d..a83ef30 100644 --- a/controller/RootLayout.java +++ b/controller/RootLayout.java @@ -75,7 +75,7 @@ public class RootLayout extends Application implements EventObserver { /* (2) Create container */ - this.articles = new Article(this.main_container, this, this.root_layout); + this.articles = new Article(this.main_container, this, this.root_layout,this.root_stage); this.dico = new Traduction(this.main_container, this, this.root_layout);