com.richtodd.android.quiltdesign.block.QuiltContainer.java Source code

Java tutorial

Introduction

Here is the source code for com.richtodd.android.quiltdesign.block.QuiltContainer.java

Source

/* Copyright (c) 2013 Richard G. Todd.
 * Licensed under the terms of the GNU General Public License (GPL) Version 3.0.
 */

package com.richtodd.android.quiltdesign.block;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;

import com.richtodd.android.repository.RepositoryContainerProvider;
import com.richtodd.android.repository.RepositoryException;
import com.richtodd.android.repository.RepositoryObjectProvider;

public class QuiltContainer {

    //
    // Fields
    //

    private final static QuiltDesignThumbnailProvider s_thumbnailProvider = new QuiltDesignThumbnailProvider();

    private RepositoryContainerProvider m_provider;

    //
    // Public
    //

    public QuiltContainer(RepositoryContainerProvider provider) {
        m_provider = provider;
    }

    public int getCount() throws RepositoryException {
        return m_provider.getCount();
    }

    public List<QuiltContainerEntry> getEntries(boolean loadThumbnails) throws RepositoryException {
        ArrayList<QuiltContainerEntry> m_entries = new ArrayList<QuiltContainerEntry>();

        for (RepositoryObjectProvider objectProvider : m_provider.getObjects(loadThumbnails)) {
            QuiltContainerEntry entry = new QuiltContainerEntry(objectProvider.getObjectName(),
                    objectProvider.getLastChangedDate(), objectProvider.getThumbnail());
            m_entries.add(entry);
        }

        Collections.sort(m_entries);

        return m_entries;
    }

    public Quilt loadQuilt(String quiltName) throws RepositoryException {
        try {
            RepositoryObjectProvider objectProvider = m_provider.getObject(quiltName, false);
            Quilt quilt = Quilt.createFromJSONObject(objectProvider.getObject());
            return quilt;
        } catch (JSONException ex) {
            throw new RepositoryException(ex);
        }
    }

    public void saveQuilt(String quiltName, Quilt quilt, String saveAsQuiltName) throws RepositoryException {
        try {
            m_provider.deleteObject(quiltName);

            if (saveAsQuiltName != null) {
                quiltName = saveAsQuiltName;
            }

            JSONObject jsonQuilt = quilt.createJSONObject();
            Bitmap thumbnail = s_thumbnailProvider.getThumbnail(quilt);

            m_provider.createObject(quiltName, jsonQuilt, thumbnail);
        } catch (JSONException ex) {
            throw new RepositoryException(ex);
        }
    }

    public void deleteQuilt(String quiltName) throws RepositoryException {
        m_provider.deleteObject(quiltName);
    }

    public String getNewQuiltName() throws RepositoryException {
        for (int idx = 1; idx < 1000; ++idx) {
            String quiltName = "Quilt " + idx;
            if (!m_provider.objectExists(quiltName)) {
                return quiltName;
            }
        }

        return null;
    }

    public boolean quiltExists(String quiltName) throws RepositoryException {
        return m_provider.objectExists(quiltName);
    }
}