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.app; import java.security.InvalidParameterException; import android.app.ActionBar; import android.app.Activity; import android.app.DialogFragment; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.view.Menu; import android.view.MenuItem; import com.richtodd.android.quiltdesign.block.Repository; import com.richtodd.android.quiltdesign.block.ThemeContainer; public class ThemeEditActivity extends Activity implements AlertDialogFragment.AlertDialogListener, EditNameDialogFragment.EditNameDialogListener { // // Fields // public static final String ARG_THEME_NAME = "com.richtodd.android.quiltdesign.app.ThemeEditActivity.themeName"; private static final String KEY_CONFIRM_DELETE = "confirmDelete"; private static final String KEY_FILE_EXISTS = "fileExists"; private String m_themeNameArgument; private String m_saveAsThemeName; // // Overrides // @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_theme_edit); getActionBar().setDisplayHomeAsUpEnabled(true); m_themeNameArgument = getIntent().getStringExtra(ARG_THEME_NAME); if (savedInstanceState != null) { m_saveAsThemeName = savedInstanceState.getString("saveAsThemeName"); } ActionBar ab = getActionBar(); ab.setTitle("Edit Theme"); ab.setSubtitle(getCurrentThemeName()); FragmentManager fm = getFragmentManager(); // Create color settings fragment. { Fragment f = fm.findFragmentById(R.id.layout_themeEditFragment); if (f == null) { f = ThemeEditFragment.create(m_themeNameArgument); fm.beginTransaction().add(R.id.layout_themeEditFragment, f).commit(); } } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("saveAsThemeName", m_saveAsThemeName); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_activity_theme_edit, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); switch (itemId) { case android.R.id.home: { NavUtils.navigateUpFromSameTask(this); return true; } case R.id.menu_renameTheme: { showEditNameDialog(); return true; } case R.id.menu_deleteTheme: { AlertDialogFragment dialog = AlertDialogFragment.create(KEY_CONFIRM_DELETE, getString(R.string.alert_message_confirmThemeDelete), getString(R.string.alert_button_yes), getString(R.string.alert_button_no)); dialog.show(getFragmentManager(), null); return true; } case R.id.menu_cancelChanges: { getThemeEditFragment().setSaveSuppressed(true); finish(); return true; } } return super.onOptionsItemSelected(item); } @Override public void onAlertDialogPositiveClick(DialogFragment dialog, String key) throws Exception { if (key.equals(KEY_CONFIRM_DELETE)) { getThemeEditFragment().setSaveSuppressed(true); Repository repository = Repository.getDefaultRepository(this); ThemeContainer themes = repository.getThemes(); themes.deleteTheme(m_themeNameArgument); finish(); } else if (key.equals(KEY_FILE_EXISTS)) { showEditNameDialog(); } else { throw new InvalidParameterException("Unknown key " + key); } } @Override public void onAlertDialogNegativeClick(DialogFragment dialog, String key) { // No action required. } @Override public void onEditNamePositiveClick(DialogFragment dialog, String name) throws Exception { Repository repository = Repository.getDefaultRepository(this); ThemeContainer themes = repository.getThemes(); if (themes.themeExists(name)) { AlertDialogFragment alertDialog = AlertDialogFragment.create(KEY_FILE_EXISTS, getString(R.string.alert_message_themeAlreadyExists), getString(R.string.alert_button_acknowledge), null); alertDialog.show(getFragmentManager(), null); } else { m_saveAsThemeName = name; getThemeEditFragment().setSaveAsThemeName(m_saveAsThemeName); } ActionBar ab = getActionBar(); ab.setSubtitle(getCurrentThemeName()); } @Override public void onEditNameNegativeClick(DialogFragment dialog) { // No action required. } // // Private // private String getCurrentThemeName() { if (m_saveAsThemeName != null) { return m_saveAsThemeName; } else { return m_themeNameArgument; } } private void showEditNameDialog() { String themeName = getCurrentThemeName(); EditNameDialogFragment dialog = EditNameDialogFragment.create("Theme", themeName); dialog.show(getFragmentManager(), null); } private ThemeEditFragment getThemeEditFragment() { return (ThemeEditFragment) getFragmentManager().findFragmentById(R.id.layout_themeEditFragment); } }