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 ThemeContainer { // // Fields // private static final QuiltDesignThumbnailProvider s_thumbnailProvider = new QuiltDesignThumbnailProvider(); private RepositoryContainerProvider m_provider; // // Public // public ThemeContainer(RepositoryContainerProvider provider) { m_provider = provider; } public int getCount() throws RepositoryException { return m_provider.getCount(); } public List<ThemeContainerEntry> getEntries(boolean loadThumbnails) throws RepositoryException { ArrayList<ThemeContainerEntry> m_entries = new ArrayList<ThemeContainerEntry>(); for (RepositoryObjectProvider objectProvider : m_provider.getObjects(loadThumbnails)) { ThemeContainerEntry entry = new ThemeContainerEntry(objectProvider.getObjectName(), objectProvider.getLastChangedDate(), objectProvider.getThumbnail()); m_entries.add(entry); } Collections.sort(m_entries); return m_entries; } public Theme loadTheme(String themeName) throws RepositoryException { try { RepositoryObjectProvider objectProvider = m_provider.getObject(themeName, false); Theme theme = Theme.createFromJSONObject(objectProvider.getObject()); return theme; } catch (JSONException ex) { throw new RepositoryException(ex); } } public void saveTheme(String themeName, Theme theme, String saveAsThemeName) throws RepositoryException { try { m_provider.deleteObject(themeName); if (saveAsThemeName != null) { themeName = saveAsThemeName; } JSONObject jsonTheme = theme.createJSONObject(); Bitmap thumbnail = s_thumbnailProvider.getThumbnail(theme); m_provider.createObject(themeName, jsonTheme, thumbnail); } catch (JSONException ex) { throw new RepositoryException(ex); } } public void deleteTheme(String themeName) throws RepositoryException { m_provider.deleteObject(themeName); } public void createStandardThemes(int swatchCount) throws RepositoryException { for (int idx = 0; idx < swatchCount; ++idx) { int angle = 360 * idx / swatchCount; String hueName = getHueName(angle); float hue = ColorFunctions.AngleToHue(angle); { String themeName = hueName + " Shades"; Theme theme = Theme.createShades(swatchCount, hue, 1f); saveTheme(themeName, theme, null); } { String themeName = hueName + " Tints"; Theme theme = Theme.createTints(swatchCount, hue, 1f); saveTheme(themeName, theme, null); } } saveTheme("All Tints", Theme.createColors(swatchCount, 0.6f, 1f), null); saveTheme("All Colors", Theme.createColors(swatchCount, 1f, 1f), null); saveTheme("All Shades", Theme.createColors(swatchCount, 1f, 0.6f), null); } public String getNewThemeName() throws RepositoryException { for (int idx = 1; idx < 1000; ++idx) { String themeName = "Theme " + idx; if (!m_provider.objectExists(themeName)) { return themeName; } } return null; } public boolean themeExists(String themeName) throws RepositoryException { return m_provider.objectExists(themeName); } public void clear() throws RepositoryException { m_provider.clear(); } // public void saveThemeRepository(File themeRepositoryFile) // throws RepositoryException { // saveJSONObject(themeRepositoryFile, createJSONObject()); // } // // // Private // // private JSONObject createJSONObject() throws RepositoryException { // try { // JSONArray jsonEntries = new JSONArray(); // for (ThemeContainerEntry entry : getEntries(false)) { // Theme theme = loadTheme(entry.getThemeName()); // // JSONObject jsonEntry = new JSONObject(); // jsonEntry.put("themeName", entry.getThemeName()); // jsonEntry.put("theme", theme.createJSONObject()); // // jsonEntries.put(jsonEntry); // } // // JSONObject jsonRepository = new JSONObject(); // jsonRepository.put("entries", jsonEntries); // // JSONObject jsonRoot = new JSONObject(); // jsonRoot.put("themeRepository", jsonRepository); // // return jsonRoot; // } catch (JSONException ex) { // throw new RepositoryException(ex); // } // } private String getHueName(int angle) { String hueName = null; switch (angle) { case 0: hueName = "Red"; break; case 30: hueName = "Red-Orange"; break; case 60: hueName = "Orange"; break; case 90: hueName = "Yellow-Orange"; break; case 120: hueName = "Yellow"; break; case 150: hueName = "Yellow-Green"; break; case 180: hueName = "Green"; break; case 210: hueName = "Blue-Green"; break; case 240: hueName = "Blue"; break; case 270: hueName = "Blue-Purple"; break; case 300: hueName = "Purple"; break; case 330: hueName = "Red-Purple"; break; } return hueName; } }