Repository for the HealthTool which enables Apple users to analyse their health data from the Apple health app and prepares the data for contributing it for future studies on wearable data.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

3 years ago
  1. package application.customviews;
  2. import java.io.File;
  3. import java.net.MalformedURLException;
  4. import application.UICoordinator;
  5. import application.helpers.Utils;
  6. import application.res.Text;
  7. import javafx.application.HostServices;
  8. import javafx.event.EventHandler;
  9. import javafx.geometry.Point2D;
  10. import javafx.scene.control.Label;
  11. import javafx.scene.control.Tooltip;
  12. import javafx.scene.image.Image;
  13. import javafx.scene.image.ImageView;
  14. import javafx.scene.input.MouseEvent;
  15. import javafx.scene.layout.HBox;
  16. public class FileView extends HBox
  17. {
  18. /**
  19. * Set height of all FileViews
  20. */
  21. private static final int HEIGHT = 62;
  22. /**
  23. * Set width of all FileViews
  24. */
  25. public static final int WIDTH = 300;
  26. /**
  27. * Set border width of all FileViews
  28. */
  29. public static final double BORDER_WIDTH = Utils.darkGreyBorderBR.getInsets().getBottom();
  30. /**
  31. * Set height of the icon of all FileViews
  32. */
  33. private static final int PIC_HEIGHT = HEIGHT - 5;
  34. /**
  35. * Initializes the view and sets up the clickHandler
  36. * @param file File which will be represented with this view
  37. * @param desc The user readable name of the file to be displayed to the user.
  38. * @param hs The hostServices needed to open the document in host application.
  39. */
  40. public FileView(File file, String desc, HostServices hs)
  41. {
  42. this.setMaxHeight(HEIGHT);
  43. this.setMinHeight(HEIGHT);
  44. this.setMaxWidth(WIDTH);
  45. this.setMinWidth(WIDTH);
  46. ImageView image;
  47. this.setBorder(Utils.darkGreyBorderBR);
  48. if (file.getName().endsWith(".csv"))
  49. {
  50. image = new ImageView(new Image(UICoordinator.class.getResourceAsStream("csv-grey.png")));
  51. } else
  52. {
  53. Image i = new Image(UICoordinator.class.getResourceAsStream("txt-grey.png"));
  54. image = new ImageView(i);
  55. }
  56. image.setFitWidth(PIC_HEIGHT);
  57. image.setFitHeight(PIC_HEIGHT);
  58. Label name = new Label(desc);
  59. name.setMinHeight(HEIGHT);
  60. name.setMaxHeight(HEIGHT);
  61. this.getChildren().add(image);
  62. this.getChildren().add(name);
  63. setOnMouseClicked(new EventHandler<MouseEvent>()
  64. { // Click on the fileView should open the file in a system native application.
  65. // Otherwise a tooltip will appear to inform the user about failing to open the
  66. // file.
  67. @Override
  68. public void handle(MouseEvent event)
  69. {
  70. try
  71. {
  72. hs.showDocument(file.toURI().toURL().toExternalForm());
  73. name.setTooltip(null);
  74. } catch (MalformedURLException e)
  75. {
  76. Point2D p = name.localToScreen(name.getLayoutBounds().getCenterX(),
  77. name.getLayoutBounds().getCenterY());
  78. Tooltip t = new Tooltip(Text.NO_OPENING_FILES);
  79. name.setTooltip(t);
  80. t.show(name, p.getX(), p.getY());
  81. t.autoHideProperty().set(true);
  82. }
  83. }
  84. });
  85. }
  86. }