Search code examples
javamilton

How do I get Milton to distinguish between child files and child folders?


I have some code that retrieves a custom DriveItem class, where a DriveItem can represent either a file or a folder.

@ChildrenOf // everything is a folder with the annotation, a file without
public List<DriveItem> getNextLevelDriveItems(DriveItem driveItem) {
    // ...
    return foldersAndFiles;
}

DriveItem has isFile() and isDirectory() methods that lets you know what kind of object the drive item is. How can I get Milton to make use of this information? Right now, all of my DriveItems show up as either folders or files, depending on whether or not I us the @ChildrenOf annotation.

enter image description here


Solution

  • I got some help from Andriy Yevsyukov. He suggested:

    You should extend DriveItem to differentiate folders and files - it could be just a marker classes

    While it requires some refactoring of our code, the addition of two new classes enables folders and files to display differently:

    public class MiltonFolder extends DriveItem implements FolderResource { ... }
    
    public class MiltonFile extends DriveItem implements FileResource { ... }
    

    Then we can have a method annotated with @ChildrenOf that handles the MiltonFolder but not the MiltonFile:

    @ChildrenOf
    public List<DriveItem> getNextLevelDriveItems(MiltonFolder driveItem) { ...}