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

Java tutorial

Introduction

Here is the source code for ru.develgame.jflickrorganizer.DataModel.TablePhotosDataModel.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.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import ru.develgame.jflickrorganizer.Common.Authorizer;
import ru.develgame.jflickrorganizer.Common.LocaleMessages;
import static ru.develgame.jflickrorganizer.Threads.BackupRunnable.THUMBS_FOLDER;
import ru.develgame.jflickrorganizer.entities.Photo;
import ru.develgame.jflickrorganizer.repositories.PhotoRepository;

/**
 *
 * @author Ilya Zemskov
 */
@Component
@Scope("prototype")
public class TablePhotosDataModel extends AbstractTableModel {
    @Autowired
    private PhotoRepository photoRepository;

    @Autowired
    private ListFilters listFilters;

    @Autowired
    private Authorizer authorizer;

    private final List<WrapperPhoto> photos = new ArrayList<>();

    public static final int COLUMN_PIC = 0;
    public static final int COLUMN_COUNT_VIEWS = 1;
    public static final int COLUMN_VISIBILITY = 2;
    public static final int COLUMN_DATE_TAKEN = 3;
    public static final int COLUMN_DATE_UPLOADED = 4;
    public static final int COLUMN_DESCRIPTION = 5;

    public static final int COUNT_COLUMNS = 6;

    public static final int COLUMN_PIC_MIN_HEIGHT = 150;

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

    @Override
    public int getRowCount() {
        return photos.size();
    }

    @Override
    public int getColumnCount() {
        return COUNT_COLUMNS;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex >= 0 && rowIndex < photos.size()) {
            WrapperPhoto photo = photos.get(rowIndex);
            switch (columnIndex) {
            case COLUMN_PIC:
                return photo.getImage();
            case COLUMN_COUNT_VIEWS: {
                if (photo.getPhoto().getViews() != null)
                    return photo.getPhoto().getViews();

                return -1;
            }
            case COLUMN_VISIBILITY: {
                if (photo.getPhoto().getVisibility() != null
                        && photo.getPhoto().getVisibility() == VisibilityFilter.VISIBILITY_PUBLIC)
                    return LocaleMessages.getMessage("MainForm.Table.Value.Public");

                return LocaleMessages.getMessage("MainForm.Table.Value.Private");
            }
            case COLUMN_DATE_TAKEN:
                return photo.getPhoto().getDateTaken();
            case COLUMN_DATE_UPLOADED:
                return photo.getPhoto().getDateUploaded();
            case COLUMN_DESCRIPTION:
                return photo.getPhoto().getDescription();
            }
        }

        return "";
    }

    public void loadData() {
        photos.clear();

        for (Photo photo : photoRepository.findAll()) {
            String filename = authorizer.getUser().getSyncFolder() + System.getProperty("file.separator")
                    + THUMBS_FOLDER + System.getProperty("file.separator") + photo.getFileName();
            ImageIcon image = null;
            if (new File(filename).exists())
                image = new ImageIcon(filename);

            WrapperPhoto wrapperPhoto = new WrapperPhoto(photo, image);
            if (listFilters.checkConditions(wrapperPhoto))
                photos.add(wrapperPhoto);
        }

        updateEvent();
    }

    public void updateEvent() {
        fireTableDataChanged();
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
        case COLUMN_PIC:
            return LocaleMessages.getMessage("MainForm.Table.Column.Pic");
        case COLUMN_COUNT_VIEWS:
            return LocaleMessages.getMessage("MainForm.Table.Column.Views");
        case COLUMN_VISIBILITY:
            return LocaleMessages.getMessage("MainForm.Table.Column.Visibility");
        case COLUMN_DATE_TAKEN:
            return LocaleMessages.getMessage("MainForm.Table.Column.DateTaken");
        case COLUMN_DATE_UPLOADED:
            return LocaleMessages.getMessage("MainForm.Table.Column.DateUploaded");
        case COLUMN_DESCRIPTION:
            return LocaleMessages.getMessage("MainForm.Table.Column.Description");
        }

        return "";
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
        case COLUMN_PIC:
            return ImageIcon.class;
        case COLUMN_COUNT_VIEWS:
            return Integer.class;
        case COLUMN_VISIBILITY:
            return String.class;
        case COLUMN_DATE_TAKEN:
            return Date.class;
        case COLUMN_DATE_UPLOADED:
            return Date.class;
        case COLUMN_DESCRIPTION:
            return String.class;
        }

        return String.class;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    }

    public static class WrapperPhoto {
        private Photo photo;
        private ImageIcon image;

        public WrapperPhoto(Photo photo, ImageIcon image) {
            this.photo = photo;
            this.image = image;
        }

        public Photo getPhoto() {
            return photo;
        }

        public ImageIcon getImage() {
            return image;
        }
    }
}