package application.customviews; import java.io.File; import java.net.MalformedURLException; import application.UICoordinator; import application.helpers.Utils; import application.res.Text; import javafx.application.HostServices; import javafx.event.EventHandler; import javafx.geometry.Point2D; import javafx.scene.control.Label; import javafx.scene.control.Tooltip; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBox; public class FileView extends HBox { /** * Set height of all FileViews */ private static final int HEIGHT = 62; /** * Set width of all FileViews */ public static final int WIDTH = 300; /** * Set border width of all FileViews */ public static final double BORDER_WIDTH = Utils.darkGreyBorderBR.getInsets().getBottom(); /** * Set height of the icon of all FileViews */ private static final int PIC_HEIGHT = HEIGHT - 5; /** * Initializes the view and sets up the clickHandler * @param file File which will be represented with this view * @param desc The user readable name of the file to be displayed to the user. * @param hs The hostServices needed to open the document in host application. */ public FileView(File file, String desc, HostServices hs) { this.setMaxHeight(HEIGHT); this.setMinHeight(HEIGHT); this.setMaxWidth(WIDTH); this.setMinWidth(WIDTH); ImageView image; this.setBorder(Utils.darkGreyBorderBR); if (file.getName().endsWith(".csv")) { image = new ImageView(new Image(UICoordinator.class.getResourceAsStream("csv-grey.png"))); } else { Image i = new Image(UICoordinator.class.getResourceAsStream("txt-grey.png")); image = new ImageView(i); } image.setFitWidth(PIC_HEIGHT); image.setFitHeight(PIC_HEIGHT); Label name = new Label(desc); name.setMinHeight(HEIGHT); name.setMaxHeight(HEIGHT); this.getChildren().add(image); this.getChildren().add(name); setOnMouseClicked(new EventHandler() { // Click on the fileView should open the file in a system native application. // Otherwise a tooltip will appear to inform the user about failing to open the // file. @Override public void handle(MouseEvent event) { try { hs.showDocument(file.toURI().toURL().toExternalForm()); name.setTooltip(null); } catch (MalformedURLException e) { Point2D p = name.localToScreen(name.getLayoutBounds().getCenterX(), name.getLayoutBounds().getCenterY()); Tooltip t = new Tooltip(Text.NO_OPENING_FILES); name.setTooltip(t); t.show(name, p.getX(), p.getY()); t.autoHideProperty().set(true); } } }); } }