Java tutorial
/* 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 BlockContainer { // // Fields // private final static QuiltDesignThumbnailProvider s_thumbnailProvider = new QuiltDesignThumbnailProvider(); private RepositoryContainerProvider m_provider; // // Public // public BlockContainer(RepositoryContainerProvider provider) { m_provider = provider; } public int getCount() throws RepositoryException { return m_provider.getCount(); } public List<BlockContainerEntry> getEntries(boolean loadThumbnails) throws RepositoryException { ArrayList<BlockContainerEntry> m_entries = new ArrayList<BlockContainerEntry>(); for (RepositoryObjectProvider objectProvider : m_provider.getObjects(loadThumbnails)) { BlockContainerEntry entry = new BlockContainerEntry(objectProvider.getObjectName(), objectProvider.getLastChangedDate(), objectProvider.getThumbnail()); m_entries.add(entry); } Collections.sort(m_entries); return m_entries; } public PaperPiecedBlock loadPaperPiecedBlock(String blockName) throws RepositoryException { try { RepositoryObjectProvider objectProvider = m_provider.getObject(blockName, false); PaperPiecedBlock block = PaperPiecedBlock.createFromJSONObject(objectProvider.getObject()); return block; } catch (JSONException ex) { throw new RepositoryException(ex); } } public void savePaperPiecedBlock(String blockName, PaperPiecedBlock block, String saveAsBlockName) throws RepositoryException { try { m_provider.deleteObject(blockName); if (saveAsBlockName != null) { blockName = saveAsBlockName; } JSONObject jsonBlock = block.createJSONObject(); Bitmap thumbnail = s_thumbnailProvider.getThumbnail(block); m_provider.createObject(blockName, jsonBlock, thumbnail); } catch (JSONException ex) { throw new RepositoryException(ex); } } public void deleteBlock(String blockName) throws RepositoryException { m_provider.deleteObject(blockName); } public boolean blockExists(String blockName) throws RepositoryException { return m_provider.objectExists(blockName); } public String getNewBlockName() throws RepositoryException { for (int idx = 1; idx < 1000; ++idx) { String blockName = "Block " + idx; if (!m_provider.objectExists(blockName)) { return blockName; } } return null; } }