implémentation de la copie dans le rpesse papier des URL d'article

This commit is contained in:
SeekDaSky 2017-12-20 16:51:52 +01:00
parent f7d68c75e8
commit 92b4b3c420
3 changed files with 85 additions and 9 deletions

63
Classes/Toast.java Normal file
View File

@ -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();
}
}

View File

@ -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<AnchorPane>();
this.observer = observer;
this.root = root_layout;
this.stage = stage;
}
public synchronized void addAllItem(ArrayList<NewsModel> models) throws IOException {
@ -89,12 +97,17 @@ public class Article{
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"));
// }
// });
item.setOnMouseClicked(new EventHandler<MouseEvent>() {
@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());

View File

@ -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);