ru.develgame.jflickrorganizer.DataModel.TreeAlbumsDataModel.java Source code

Java tutorial

Introduction

Here is the source code for ru.develgame.jflickrorganizer.DataModel.TreeAlbumsDataModel.java

Source

/* This Source Code Form is subject to the terms of the Mozilla
 * Public License, v. 2.0. If a copy of the MPL was not distributed
 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * Copyright 2017 Ilya Zemskov */

package ru.develgame.jflickrorganizer.DataModel;

import java.util.List;
import java.util.Vector;
import javax.annotation.PostConstruct;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import ru.develgame.jflickrorganizer.Common.LocaleMessages;
import ru.develgame.jflickrorganizer.entities.Album;
import ru.develgame.jflickrorganizer.repositories.AlbumRepository;

/**
 *
 * @author Ilya Zemskov
 */
public class TreeAlbumsDataModel extends DefaultTreeModel {
    @Autowired
    private AlbumRepository albumRepository;

    public TreeAlbumsDataModel(TreeNode tn) {
        super(createAlbumRoot());
    }

    @PostConstruct
    public void init() {
        loadData();
    }

    private static DefaultMutableTreeNode createAlbumRoot() {
        return new AlbumNode(LocaleMessages.getMessage("TreeAlbumsDataModel.AllPhotos"));
    }

    public void loadData() {
        AlbumNode parent = (AlbumNode) getRoot();

        parent.getChildrens().clear();

        for (Album album : albumRepository.findAll()) {
            new AlbumNode(album, parent);
        }

        nodeStructureChanged((TreeNode) getRoot());
    }

    public static class AlbumNode extends DefaultMutableTreeNode {
        private Album album = null;

        public Album getAlbum() {
            return album;
        }

        public void setAlbum(Album album) {
            this.album = album;
            publicName = album.getName();
        }

        public String getPublicName() {
            return publicName;
        }

        public void setPublicName(String publicName) {
            this.publicName = publicName;
        }

        public List<AlbumNode> getChildrens() {
            return children;
        }

        private String publicName;

        public AlbumNode(String publicName) {
            children = new Vector();

            this.publicName = publicName;
            parent = null;
        }

        public AlbumNode(Album album, AlbumNode parent) {
            children = new Vector();

            setAlbum(album);
            if (parent != null)
                parent.insert(this, 0);
        }

        @Override
        public String toString() {
            return publicName;
        }
    }

}