Skip to content
Snippets Groups Projects
Commit 5cdcf44c authored by Simon Jensen's avatar Simon Jensen
Browse files

Dev copy, added upload page files. Ready for merging

parent 6ac802aa
No related branches found
No related tags found
2 merge requests!30Weekly merge to master,!25Dev copy, added upload page files. Ready for merging
Pipeline #75556 passed
/**
* Controls the buttons and changable elements on upload page
* @version 1.0 17.03.2020
* @author Simon Jensen
*/
package NTNU.IDATT1002.controllers;
import NTNU.IDATT1002.App;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class UploadController {
public Button uploadBtn;
public VBox thumbnailsField;
public Pane dragDropField;
public TextField searchField;
/**
* Method that changes stage to Explore page
* @throws IOException
*/
public void switchToExplore() throws IOException {
App.setRoot("explore");
}
/**
* Method that changes stage to Album page
* @throws IOException
*/
public void switchToAlbum() throws IOException {
App.setRoot("album");
}
/**
* Method that changes stage to Map page
* @throws IOException
*/
public void switchToMap() throws IOException {
App.setRoot("map");
}
/**
* Method for searching by tags, ect.
* @param keyEvent
*/
public void search(KeyEvent keyEvent) {
//TODO: Make search method
}
/**
* Method that opens filebrowser with an image filter.
* The user chooses what files to upload
* @throws MalformedURLException
*/
public void chooseFile() throws MalformedURLException {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Choose image files");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Image Files", "*.png","*.jpg", "*.jpeg"));
// Show save file dialog
List<File> list = fileChooser.showOpenMultipleDialog(uploadBtn.getScene().getWindow());
if (list != null){
for (File file : list){
//Made a method to confirm that the images was uploaded correctly
addThumbnail(file);
}
}
}
/**
* Method that finds the extension/filetype
* @param fileName the name of the file (img.jpg ect.)
* @return file extension (jpg, png ect.)
*/
public String getExtension(String fileName){
String extension = "";
int i = fileName.lastIndexOf('.');
//if the name is not empty
if (i > 0 && i < fileName.length() - 1){
return fileName.substring(i + 1).toLowerCase();
}
return extension;
}
/**
* Method that decides whenever the file hoovered over the pane is valid or not.
* Called when something is hoovered over the pane.
* @param event something is dragged over the container
*/
public void acceptDrop(DragEvent event) {
// Extensions that are valid to be drag-n-dropped
//TODO: Choose valid file types
List<String> validExtensions = Arrays.asList("jpg", "png", "jpeg");
//Checks if the event contains files
if(event.getDragboard().hasFiles()){
if (!validExtensions.containsAll(
//Makes a list out of the events file extensions
event.getDragboard().getFiles().stream()
.map(file -> getExtension(file.getName()))
.collect(Collectors.toList()))) {
event.consume();
return;
}
//Makes it possible to transfer the files
event.acceptTransferModes(TransferMode.ANY);
}
}
/**
* Method that stores the dropped files. It is not possible to transfer files
* without them already being accepted in acceptDrop() method.
* @param event something is dropped into the container
* @throws MalformedURLException
*/
public void droppedFiles(DragEvent event) throws MalformedURLException {
List<File> list = event.getDragboard().getFiles();
for (File file : list){
addThumbnail(file);
}
}
/**
* Test-method to confirm that the image drop went well
* @param file an image file
* @throws MalformedURLException
*/
//TODO: Remove method?
public void addThumbnail(File file) throws MalformedURLException {
Image image = new Image(file.toURI().toURL().toString());
ImageView view = new ImageView();
view.setImage(image);
//setting the fit height and width of the image view
view.setFitHeight(30);
view.setFitWidth(30);
thumbnailsField.getChildren().add(view);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="NTNU.IDATT1002.controllers.UploadController">
<top>
<HBox prefHeight="71.0" prefWidth="600.0" style="-fx-background-color: #7b7b7b;" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER" prefHeight="135.0" prefWidth="404.0">
<children>
<Pane onMouseClicked="#switchToExplore" prefHeight="71.0" prefWidth="100.0">
<children>
<Text layoutX="21.0" layoutY="42.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Explore">
<font>
<Font size="18.0" />
</font>
</Text>
</children>
</Pane>
<Pane onMouseClicked="#switchToAlbum" prefHeight="71.0" prefWidth="100.0">
<children>
<Text layoutX="24.0" layoutY="42.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Album">
<font>
<Font size="18.0" />
</font>
</Text>
</children>
</Pane>
<Pane onMouseClicked="#switchToMap" prefHeight="71.0" prefWidth="100.0">
<children>
<Text layoutX="32.0" layoutY="42.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Map">
<font>
<Font size="18.0" />
</font>
</Text>
</children>
</Pane>
<Pane prefHeight="71.0" prefWidth="100.0" style="-fx-background-color: #d6d6d6;">
<children>
<Text layoutX="18.0" layoutY="43.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Upload" wrappingWidth="63.408203125">
<font>
<Font size="18.0" />
</font>
</Text>
</children>
</Pane>
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="74.0" prefWidth="200.0">
<children>
<TextField fx:id="searchField" focusTraversable="false" onKeyTyped="#search" prefHeight="26.0" prefWidth="133.0" promptText="Search" />
</children>
</HBox>
</children>
<BorderPane.margin>
<Insets />
</BorderPane.margin>
</HBox>
</top>
<left>
<Pane prefHeight="282.0" prefWidth="71.0" style="-fx-background-color: #b8b8b8;" BorderPane.alignment="CENTER" />
</left>
<center>
<Pane prefHeight="212.0" prefWidth="338.0" snapToPixel="false" style="-fx-background-color: #d6d6d6;" BorderPane.alignment="BOTTOM_RIGHT">
<children>
<Button fx:id="uploadBtn" alignment="CENTER" contentDisplay="CENTER" layoutX="159.0" layoutY="181.0" mnemonicParsing="false" onMouseClicked="#chooseFile" prefHeight="25.0" prefWidth="152.0" text="Choose files" />
<Pane fx:id="dragDropField" layoutX="111.0" layoutY="58.0" onDragDropped="#droppedFiles" onDragOver="#acceptDrop" prefHeight="106.0" prefWidth="248.0" style="-fx-background-color: lightgrey; -fx-border-color: grey;">
<children>
<Text layoutX="61.0" layoutY="58.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Drag and drop files" wrappingWidth="125.33671569824219">
<font>
<Font size="14.0" />
</font>
</Text>
</children>
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
</Pane>
</children>
</Pane>
</center>
<right>
<Pane prefHeight="282.0" prefWidth="58.0" style="-fx-background-color: #a5a5a5;" BorderPane.alignment="CENTER">
<children>
<VBox fx:id="thumbnailsField" layoutX="1.0" layoutY="14.0" prefHeight="200.0" prefWidth="58.0" />
</children></Pane>
</right>
<bottom>
<Pane prefHeight="47.0" prefWidth="600.0" style="-fx-background-color: #727272;" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment