JavaFX Photo Management Application: Add and Delete Tags for Photos
package photo.project.controller;
import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Alert; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import photo.project.Photos; import photo.project.Config.projectConfig; import photo.project.model.Album; import photo.project.model.Tag; import photo.project.model.User; import photo.project.util.AlertUtil; import photo.project.util.ObjectToFileUtil; import photo.project.util.WindowUtil;
import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle;
/**
-
The DisplayController class is responsible for controlling the display screen of the application,
-
which displays the selected photo's details and allows the user to add or delete tags. */ public class DisplayController implements Initializable {
@FXML private Label captionTxt; // The label displaying the selected photo's caption.
@FXML private ImageView imageView; // The image view displaying the selected photo.
@FXML private ListView
tagList; // The list view displaying the tags associated with the selected photo. @FXML private TextField tagTxt; // The text field where the user can enter a new tag.
@FXML private Label timeTxt; // The label displaying the selected photo's time.
@FXML private Label userTxt; // The label displaying the name of the logged in user.
ObservableList observableList; // The observable list used to populate the tag list.
Tag selectedTag; // The tag that is currently selected in the tag list.
/**
-
This method is called when the user clicks on the 'Add Tag' button. It reads the tag entered by the user
-
and adds it to the selected photo's tag list. If no photo is selected, it adds the tag to the first photo that
-
matches the selected tag's caption. If the tag already exists for the selected photo, an error message is displayed.
-
@param event The event triggered by the user clicking on the 'Add Tag' button. */ @FXML void addTag(ActionEvent event) {
// Check if the tag entered by the user is valid. if (tagTxt.getText().equals("")) { AlertUtil.informationDialog(Alert.AlertType.ERROR, "please input valid !"); return; } String[] tagStrngs = tagTxt.getText().split("="); if (tagStrngs.length < 2) { AlertUtil.informationDialog(Alert.AlertType.ERROR, "please input valid !"); return; }
// Create a new tag object from the tag entered by the user. Tag tag = new Tag(tagStrngs[0].trim(), tagStrngs[1].trim());
// If no album is selected, add the tag to the selected photo in the user's collection. if(projectConfig.chooseAlbum==null){ List
userList = ObjectToFileUtil.loadContent(); User user = null; for (int i = 0; i < userList.size(); i++) { User newUser = (User) userList.get(i); if (newUser.getName().equals(projectConfig.loginUser.getName())) { user = userList.get(i); } } // Find the selected photo in the user's collection. boolean isExist =false; for (int i = 0; i < user.getAlbumList().size(); i++) { for (int i1 = 0; i1 < user.getAlbumList().get(i).getAllPhotos().size(); i1++) { if(user.getAlbumList().get(i).getAllPhotos().get(i1).getCaption().equals(projectConfig.choosePhotoButton.getPhoto().getCaption())){ if(isExist==false){ // Add the new tag to the selected photo's tag list. user.getAlbumList().get(i).getAllPhotos().get(i1).getTagList().add(tag); user.getAlbumList().get(i).getAllPhotos().get(i1).updateTime(); projectConfig.choosePhotoButton.setPhoto( user.getAlbumList().get(i).getAllPhotos().get(i1)); isExist=true; } } } } // Save the modified user data to file. projectConfig.loginUser = user; ObjectToFileUtil.saveContent(userList); AlertUtil.informationDialog(Alert.AlertType.INFORMATION, "add tag success!"); tagList.setItems(FXCollections.observableList(projectConfig.choosePhotoButton.getPhoto().getTagList())); tagList.refresh(); return;}
// If an album is selected, add the tag to the selected photo in the selected album. List
userList = ObjectToFileUtil.loadContent(); User user = null; for (int i = 0; i < userList.size(); i++) { User newUser = (User) userList.get(i); if (newUser.getName().equals(projectConfig.loginUser.getName())) { user = userList.get(i); } } // Find the selected photo in the selected album. Album album = null; for (int i = 0; i < user.getAlbumList().size(); i++) { if (user.getAlbumList().get(i).getAlbumName().equals(projectConfig.chooseAlbum.getAlbumName())) { album = user.getAlbumList().get(i); for (int i1 = 0; i1 < album.getAllPhotos().size(); i1++) { if (album.getAllPhotos().get(i1).getCaption().equals(projectConfig.choosePhotoButton.getPhoto().getCaption())) { boolean isExist =false; for (int i2 = 0; i2 < album.getAllPhotos().get(i1).getTagList().size(); i2++) { if(album.getAllPhotos().get(i1).getTagList().get(i2).getName().equals(tag.getName())&& album.getAllPhotos().get(i1).getTagList().get(i2).getValue().equals(tag.getValue()) ){ isExist=true; } } if(isExist==true){ AlertUtil.informationDialog(Alert.AlertType.ERROR,"tag is exist!"); return; }else{ album.getAllPhotos().get(i1).getTagList().add(tag); album.getAllPhotos().get(i1).updateTime(); }
} } }}
// Save the modified user data to file. projectConfig.loginUser = user; projectConfig.chooseAlbum = ObjectToFileUtil.findAlbum(userList,projectConfig.loginUser,projectConfig.chooseAlbum); projectConfig.choosePhotoButton.setPhoto(ObjectToFileUtil.findPhoto(userList,projectConfig.loginUser,projectConfig.chooseAlbum,projectConfig.choosePhotoButton.getPhoto())); ObjectToFileUtil.saveContent(userList); AlertUtil.informationDialog
-
原文地址: https://www.cveoy.top/t/topic/njwM 著作权归作者所有。请勿转载和采集!