diff --git a/calculatorpp/AndroidManifest.xml b/calculatorpp/AndroidManifest.xml index f182a0d4..be0b0f9e 100644 --- a/calculatorpp/AndroidManifest.xml +++ b/calculatorpp/AndroidManifest.xml @@ -22,27 +22,17 @@ - - - + - + - + - + - - - - - - - - - + diff --git a/calculatorpp/res/layout/about.xml b/calculatorpp/res/layout/about_fragment.xml similarity index 96% rename from calculatorpp/res/layout/about.xml rename to calculatorpp/res/layout/about_fragment.xml index d219da3d..602bab8a 100644 --- a/calculatorpp/res/layout/about.xml +++ b/calculatorpp/res/layout/about_fragment.xml @@ -1,47 +1,47 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/layout/history.xml b/calculatorpp/res/layout/history_item.xml similarity index 96% rename from calculatorpp/res/layout/history.xml rename to calculatorpp/res/layout/history_item.xml index 07358118..7d7a1c27 100644 --- a/calculatorpp/res/layout/history.xml +++ b/calculatorpp/res/layout/history_item.xml @@ -1,40 +1,40 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/layout/release_notes.xml b/calculatorpp/res/layout/release_notes_fragment.xml similarity index 96% rename from calculatorpp/res/layout/release_notes.xml rename to calculatorpp/res/layout/release_notes_fragment.xml index 911ff731..e01382f0 100644 --- a/calculatorpp/res/layout/release_notes.xml +++ b/calculatorpp/res/layout/release_notes_fragment.xml @@ -1,28 +1,28 @@ - - - - - - - - - - + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/layout/saved_history.xml b/calculatorpp/res/layout/saved_history_item.xml similarity index 96% rename from calculatorpp/res/layout/saved_history.xml rename to calculatorpp/res/layout/saved_history_item.xml index f7abe10c..138c8fe8 100644 --- a/calculatorpp/res/layout/saved_history.xml +++ b/calculatorpp/res/layout/saved_history_item.xml @@ -1,41 +1,41 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/AndroidUtils2.java b/calculatorpp/src/main/java/org/solovyev/android/AndroidUtils2.java index 1b073d4d..9abe58ae 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/AndroidUtils2.java +++ b/calculatorpp/src/main/java/org/solovyev/android/AndroidUtils2.java @@ -1,6 +1,5 @@ package org.solovyev.android; -import android.content.res.Configuration; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; @@ -33,11 +32,4 @@ public final class AndroidUtils2 { dialogFragment.show(ft, fragmentTag); } - - // copied from API-15 - public static boolean isLayoutSizeAtLeast(int size, @NotNull Configuration configuration) { - int cur = configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; - if (cur == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) return false; - return cur >= size; - } } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index f704d90c..565800ca 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -26,13 +26,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.about.CalculatorFragmentType; -import org.solovyev.android.calculator.about.CalculatorReleaseNotesActivity; -import org.solovyev.android.calculator.history.CalculatorHistoryFragment; -import org.solovyev.android.calculator.history.CalculatorSavedHistoryFragment; -import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment; -import org.solovyev.android.calculator.math.edit.CalculatorOperatorsFragment; -import org.solovyev.android.calculator.math.edit.CalculatorVarsFragment; -import org.solovyev.android.calculator.plot.CalculatorPlotFragment; +import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment; import org.solovyev.android.fragments.FragmentUtils; import org.solovyev.android.prefs.Preference; import org.solovyev.android.view.ColorButton; @@ -78,6 +72,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar activityHelper.addTab(this, CalculatorFragmentType.functions, null, R.id.main_second_pane); activityHelper.addTab(this, CalculatorFragmentType.operators, null, R.id.main_second_pane); activityHelper.addTab(this, CalculatorFragmentType.plotter, null, R.id.main_second_pane); + activityHelper.addTab(this, CalculatorFragmentType.faq, null, R.id.main_second_pane); } else { getSupportActionBar().hide(); } @@ -141,7 +136,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar if (savedVersion < appVersion) { final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences); if (showReleaseNotes) { - final String releaseNotes = CalculatorReleaseNotesActivity.getReleaseNotes(context, savedVersion + 1); + final String releaseNotes = CalculatorReleaseNotesFragment.getReleaseNotes(context, savedVersion + 1); if (!StringUtils.isEmpty(releaseNotes)) { final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes)); builder.setPositiveButton(android.R.string.ok, null); diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityHelperImpl.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityHelperImpl.java index feaecb68..a704b28e 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityHelperImpl.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityHelperImpl.java @@ -16,9 +16,6 @@ import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.about.CalculatorFragmentType; import org.solovyev.android.sherlock.tabs.ActionBarFragmentTabListener; -import java.util.ArrayList; -import java.util.List; - /** * User: serso * Date: 9/25/12 @@ -46,9 +43,6 @@ public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper imple private boolean homeIcon = false; - @NotNull - private final List fragmentTags = new ArrayList(); - @NotNull private CalculatorPreferences.Gui.Theme theme; @@ -196,8 +190,6 @@ public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper imple final ActionBarFragmentTabListener listener = new ActionBarFragmentTabListener(activity, tag, fragmentClass, fragmentArgs, parentViewId); tab.setTabListener(listener); actionBar.addTab(tab); - - fragmentTags.add(tag); } @Override diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java index f27affcb..bbbda88e 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java @@ -7,10 +7,9 @@ import jscl.math.Generic; import jscl.math.function.Constant; import org.achartengine.ChartFactory; import org.jetbrains.annotations.NotNull; -import org.solovyev.android.AndroidUtils2; -import org.solovyev.android.calculator.about.CalculatorAboutTabActivity; -import org.solovyev.android.calculator.help.CalculatorHelpTabActivity; -import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity; +import org.solovyev.android.calculator.about.CalculatorAboutActivity; +import org.solovyev.android.calculator.help.CalculatorHelpActivity; +import org.solovyev.android.calculator.history.CalculatorHistoryActivity; import org.solovyev.android.calculator.math.edit.*; import org.solovyev.android.calculator.plot.CalculatorPlotActivity; import org.solovyev.android.calculator.plot.CalculatorPlotFragment; @@ -25,11 +24,11 @@ import org.solovyev.common.text.StringUtils; public class CalculatorActivityLauncher { public static void showHistory(@NotNull final Context context) { - context.startActivity(new Intent(context, CalculatorHistoryFragmentActivity.class)); + context.startActivity(new Intent(context, CalculatorHistoryActivity.class)); } public static void showHelp(@NotNull final Context context) { - context.startActivity(new Intent(context, CalculatorHelpTabActivity.class)); + context.startActivity(new Intent(context, CalculatorHelpActivity.class)); } public static void showSettings(@NotNull final Context context) { @@ -37,19 +36,19 @@ public class CalculatorActivityLauncher { } public static void showAbout(@NotNull final Context context) { - context.startActivity(new Intent(context, CalculatorAboutTabActivity.class)); + context.startActivity(new Intent(context, CalculatorAboutActivity.class)); } public static void showFunctions(@NotNull final Context context) { - context.startActivity(new Intent(context, CalculatorFunctionsFragmentActivity.class)); + context.startActivity(new Intent(context, CalculatorFunctionsActivity.class)); } public static void showOperators(@NotNull final Context context) { - context.startActivity(new Intent(context, CalculatorOperatorsFragmentActivity.class)); + context.startActivity(new Intent(context, CalculatorOperatorsActivity.class)); } public static void showVars(@NotNull final Context context) { - context.startActivity(new Intent(context, CalculatorVarsFragmentActivity.class)); + context.startActivity(new Intent(context, CalculatorVarsActivity.class)); } public static void plotGraph(@NotNull final Context context, @NotNull Generic generic, @NotNull Constant constant){ @@ -69,7 +68,7 @@ public class CalculatorActivityLauncher { if (context instanceof SherlockFragmentActivity) { VarEditDialogFragment.showDialog(VarEditDialogFragment.Input.newFromValue(varValue), ((SherlockFragmentActivity) context).getSupportFragmentManager()); } else { - final Intent intent = new Intent(context, CalculatorVarsFragmentActivity.class); + final Intent intent = new Intent(context, CalculatorVarsActivity.class); intent.putExtra(CalculatorVarsFragment.CREATE_VAR_EXTRA_STRING, varValue); context.startActivity(intent); } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java index 94478fd2..142814f3 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java @@ -15,7 +15,6 @@ import jscl.NumeralBase; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.AndroidUtils; -import org.solovyev.android.AndroidUtils2; import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.view.AngleUnitsButton; import org.solovyev.android.calculator.view.NumeralBasesButton; @@ -66,7 +65,7 @@ public final class CalculatorButtons { @NotNull Activity activity) { preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(activity) : preferences; - final boolean large = AndroidUtils2.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE, activity.getResources().getConfiguration()); + final boolean large = AndroidUtils.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE, activity.getResources().getConfiguration()); if (!large) { if (AndroidUtils.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java index 68acf973..9ad08131 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java @@ -1,98 +1,64 @@ -package org.solovyev.android.calculator; - -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import com.actionbarsherlock.app.SherlockFragment; -import com.actionbarsherlock.view.Menu; -import com.actionbarsherlock.view.MenuInflater; -import com.actionbarsherlock.view.MenuItem; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.menu.ActivityMenu; -import org.solovyev.android.menu.ListActivityMenu; -import org.solovyev.android.sherlock.menu.SherlockMenuHelper; - -/** - * User: Solovyev_S - * Date: 25.09.12 - * Time: 10:49 - */ -public class CalculatorEditorFragment extends SherlockFragment { - - @NotNull - private ActivityMenu menu = ListActivityMenu.fromList(CalculatorMenu.class, SherlockMenuHelper.getInstance()); - - private CalculatorFragmentHelper fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(R.layout.calc_editor, R.string.editor); - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - fragmentHelper.onCreate(this); - - setHasOptionsMenu(true); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return fragmentHelper.onCreateView(this, inflater, container); - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - fragmentHelper.onViewCreated(this, view); - - ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setEditor(getActivity()); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - } - - @Override - public void onDestroy() { - fragmentHelper.onDestroy(this); - super.onDestroy(); - } - - @Override - public void onResume() { - super.onResume(); - - this.fragmentHelper.onResume(this); - } - - @Override - public void onPause() { - this.fragmentHelper.onPause(this); - - super.onPause(); - } - - /* - ********************************************************************** - * - * MENU - * - ********************************************************************** - */ - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - this.menu.onCreateOptionsMenu(this.getActivity(), menu); - } - - @Override - public void onPrepareOptionsMenu(Menu menu) { - this.menu.onPrepareOptionsMenu(this.getActivity(), menu); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - return this.menu.onOptionsItemSelected(this.getActivity(), item); - } -} +package org.solovyev.android.calculator; + +import android.os.Bundle; +import android.view.View; +import com.actionbarsherlock.view.Menu; +import com.actionbarsherlock.view.MenuInflater; +import com.actionbarsherlock.view.MenuItem; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.about.CalculatorFragmentType; +import org.solovyev.android.menu.ActivityMenu; +import org.solovyev.android.menu.ListActivityMenu; +import org.solovyev.android.sherlock.menu.SherlockMenuHelper; + +/** + * User: Solovyev_S + * Date: 25.09.12 + * Time: 10:49 + */ +public class CalculatorEditorFragment extends CalculatorFragment { + + @NotNull + private ActivityMenu menu = ListActivityMenu.fromList(CalculatorMenu.class, SherlockMenuHelper.getInstance()); + + public CalculatorEditorFragment() { + super(CalculatorFragmentType.editor); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setHasOptionsMenu(true); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setEditor(getActivity()); + } + + /* + ********************************************************************** + * + * MENU + * + ********************************************************************** + */ + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + this.menu.onCreateOptionsMenu(this.getActivity(), menu); + } + + @Override + public void onPrepareOptionsMenu(Menu menu) { + this.menu.onPrepareOptionsMenu(this.getActivity(), menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + return this.menu.onOptionsItemSelected(this.getActivity(), item); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragment.java new file mode 100644 index 00000000..bef05c50 --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragment.java @@ -0,0 +1,67 @@ +package org.solovyev.android.calculator; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import com.actionbarsherlock.app.SherlockFragment; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.about.CalculatorFragmentType; + +/** + * User: Solovyev_S + * Date: 03.10.12 + * Time: 14:18 + */ +public abstract class CalculatorFragment extends SherlockFragment { + + @NotNull + private final CalculatorFragmentHelper fragmentHelper; + + protected CalculatorFragment(int layoutResId, int titleResId) { + fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(layoutResId, titleResId); + } + + protected CalculatorFragment(@NotNull CalculatorFragmentType fragmentType) { + fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId()); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + fragmentHelper.onCreate(this); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return fragmentHelper.onCreateView(this, inflater, container); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + fragmentHelper.onViewCreated(this, view); + } + + @Override + public void onDestroy() { + fragmentHelper.onDestroy(this); + super.onDestroy(); + } + + @Override + public void onResume() { + super.onResume(); + + this.fragmentHelper.onResume(this); + } + + @Override + public void onPause() { + this.fragmentHelper.onPause(this); + + super.onPause(); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentActivity.java new file mode 100644 index 00000000..dbca71c9 --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFragmentActivity.java @@ -0,0 +1,65 @@ +package org.solovyev.android.calculator; + +import android.os.Bundle; +import com.actionbarsherlock.app.SherlockFragmentActivity; +import org.jetbrains.annotations.NotNull; + +/** + * User: Solovyev_S + * Date: 03.10.12 + * Time: 14:07 + */ +public abstract class CalculatorFragmentActivity extends SherlockFragmentActivity { + + @NotNull + private final CalculatorActivityHelper activityHelper; + + protected CalculatorFragmentActivity() { + this(R.layout.main_empty); + } + + protected CalculatorFragmentActivity(int layoutResId) { + this.activityHelper = CalculatorApplication.getInstance().createActivityHelper(layoutResId, getClass().getSimpleName()); + } + + @NotNull + protected CalculatorActivityHelper getActivityHelper() { + return activityHelper; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + activityHelper.onCreate(this, savedInstanceState); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + + activityHelper.onSaveInstanceState(this, outState); + } + + @Override + protected void onResume() { + super.onResume(); + + activityHelper.onResume(this); + } + + @Override + protected void onPause() { + this.activityHelper.onPause(this); + + super.onPause(); + } + + + @Override + protected void onDestroy() { + super.onDestroy(); + + activityHelper.onDestroy(this); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorAboutFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorAboutFragment.java new file mode 100644 index 00000000..1968eedd --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorAboutFragment.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.about; + +import android.os.Bundle; +import android.text.method.LinkMovementMethod; +import android.view.View; +import android.widget.TextView; +import org.solovyev.android.calculator.CalculatorFragment; +import org.solovyev.android.calculator.R; + +/** + * User: serso + * Date: 12/24/11 + * Time: 11:55 PM + */ +public class CalculatorAboutFragment extends CalculatorFragment { + + public CalculatorAboutFragment() { + super(CalculatorFragmentType.about); + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + + final TextView about = (TextView) root.findViewById(R.id.aboutTextView); + about.setMovementMethod(LinkMovementMethod.getInstance()); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorAboutTabActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorAboutTabActivity.java deleted file mode 100644 index 83762c9d..00000000 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorAboutTabActivity.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - */ - -package org.solovyev.android.calculator.about; - -import android.app.TabActivity; -import android.os.Bundle; -import android.widget.TabHost; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.AndroidUtils; -import org.solovyev.android.LastTabSaver; -import org.solovyev.android.calculator.R; - -/** - * User: serso - * Date: 9/16/11 - * Time: 11:52 PM - */ -public class CalculatorAboutTabActivity extends TabActivity { - - @Nullable - private LastTabSaver lastTabSaver; - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.tabs); - - final TabHost tabHost = getTabHost(); - - AndroidUtils.addTab(this, tabHost, "about", R.string.c_about, CalculatorAboutActivity.class); - AndroidUtils.addTab(this, tabHost, "release_notes", R.string.c_release_notes, CalculatorReleaseNotesActivity.class); - - lastTabSaver = new LastTabSaver(this, "about"); - - AndroidUtils.centerAndWrapTabsFor(tabHost); - } - - @Override - protected void onDestroy() { - if ( this.lastTabSaver != null ) { - this.lastTabSaver.destroy(); - } - - super.onDestroy(); - } -} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorFragmentType.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorFragmentType.java index 20aa0084..3643eb96 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorFragmentType.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorFragmentType.java @@ -2,7 +2,11 @@ package org.solovyev.android.calculator.about; import android.support.v4.app.Fragment; import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.CalculatorEditorFragment; import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.help.CalculatorHelpFaqFragment; +import org.solovyev.android.calculator.help.CalculatorHelpHintsFragment; +import org.solovyev.android.calculator.help.CalculatorHelpScreensFragment; import org.solovyev.android.calculator.history.CalculatorHistoryFragment; import org.solovyev.android.calculator.history.CalculatorSavedHistoryFragment; import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment; @@ -17,30 +21,41 @@ import org.solovyev.android.calculator.plot.CalculatorPlotFragment; */ public enum CalculatorFragmentType { - history(CalculatorHistoryFragment.class, "history", R.string.c_history), - saved_history(CalculatorSavedHistoryFragment.class, "saved_history", R.string.c_saved_history), - variables(CalculatorVarsFragment.class, "vars", R.string.c_vars), - functions(CalculatorFunctionsFragment.class, "functions", R.string.c_functions), - operators(CalculatorOperatorsFragment.class, "operators", R.string.c_operators), - plotter(CalculatorPlotFragment.class, "plotter", R.string.c_plot); + editor(CalculatorEditorFragment.class, R.layout.calc_editor, R.string.editor), + //display(CalculatorHistoryFragment.class, "history", R.layout.history_fragment, R.string.c_history), + //keyboard(CalculatorHistoryFragment.class, "history", R.layout.history_fragment, R.string.c_history), + history(CalculatorHistoryFragment.class, R.layout.history_fragment, R.string.c_history), + saved_history(CalculatorSavedHistoryFragment.class, R.layout.history_fragment, R.string.c_saved_history), + variables(CalculatorVarsFragment.class, R.layout.vars_fragment, R.string.c_vars), + functions(CalculatorFunctionsFragment.class, R.layout.math_entities_fragment, R.string.c_functions), + operators(CalculatorOperatorsFragment.class, R.layout.math_entities_fragment, R.string.c_operators), + plotter(CalculatorPlotFragment.class, R.layout.plot_fragment, R.string.c_plot), + about(CalculatorAboutFragment.class, R.layout.about_fragment, R.string.c_about), + + // todo serso: rename and inflate ad + faq(CalculatorHelpFaqFragment.class, R.layout.help_faq, R.string.c_faq), + hints(CalculatorHelpHintsFragment.class, R.layout.help_hints, R.string.c_hints), + screens(CalculatorHelpScreensFragment.class, R.layout.help_screens, R.string.c_screens), + release_notes(CalculatorReleaseNotesFragment.class, R.layout.release_notes_fragment, R.string.c_release_notes); @NotNull private Class fragmentClass; - @NotNull - private final String fragmentTag; + private final int defaultLayoutId; private int defaultTitleResId; - private CalculatorFragmentType(@NotNull Class fragmentClass, @NotNull String fragmentTag, int defaultTitleResId) { + private CalculatorFragmentType(@NotNull Class fragmentClass, + int defaultLayoutId, + int defaultTitleResId) { this.fragmentClass = fragmentClass; - this.fragmentTag = fragmentTag; + this.defaultLayoutId = defaultLayoutId; this.defaultTitleResId = defaultTitleResId; } @NotNull public String getFragmentTag() { - return fragmentTag; + return this.name(); } public int getDefaultTitleResId() { @@ -52,8 +67,12 @@ public enum CalculatorFragmentType { return fragmentClass; } + public int getDefaultLayoutId() { + return defaultLayoutId; + } + @NotNull public String createSubFragmentTag(@NotNull String subFragmentTag) { - return this.fragmentTag + "_" + subFragmentTag; + return this.getFragmentTag() + "_" + subFragmentTag; } } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorReleaseNotesActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorReleaseNotesFragment.java similarity index 71% rename from calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorReleaseNotesActivity.java rename to calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorReleaseNotesFragment.java index 0186cca5..480b92e3 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorReleaseNotesActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorReleaseNotesFragment.java @@ -6,16 +6,16 @@ package org.solovyev.android.calculator.about; -import android.app.Activity; import android.content.Context; -import android.content.res.Resources; import android.os.Bundle; import android.text.Html; import android.text.method.LinkMovementMethod; +import android.view.View; import android.widget.TextView; import org.jetbrains.annotations.NotNull; import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.CalculatorActivity; +import org.solovyev.android.calculator.CalculatorFragment; import org.solovyev.android.calculator.R; import org.solovyev.common.text.StringUtils; @@ -24,23 +24,23 @@ import org.solovyev.common.text.StringUtils; * Date: 12/25/11 * Time: 12:00 AM */ -public class CalculatorReleaseNotesActivity extends Activity { +public class CalculatorReleaseNotesFragment extends CalculatorFragment { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + public CalculatorReleaseNotesFragment() { + super(CalculatorFragmentType.release_notes); + } - setContentView(R.layout.release_notes); + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); - final TextView releaseNotes = (TextView) findViewById(R.id.releaseNotesTextView); - releaseNotes.setMovementMethod(LinkMovementMethod.getInstance()); + final TextView releaseNotes = (TextView) root.findViewById(R.id.releaseNotesTextView); + releaseNotes.setMovementMethod(LinkMovementMethod.getInstance()); - releaseNotes.setText(Html.fromHtml(getReleaseNotes(this))); - + releaseNotes.setText(Html.fromHtml(getReleaseNotes(this.getActivity()))); + } - } - - @NotNull + @NotNull public static String getReleaseNotes(@NotNull Context context) { return getReleaseNotes(context, 0); } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java deleted file mode 100644 index cab1ae6b..00000000 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2009-2012. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.help; - -import android.app.Activity; -import android.os.Bundle; -import com.google.ads.AdView; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.ads.AdsController; - -/** - * User: serso - * Date: 1/4/12 - * Time: 12:34 AM - */ -public class AbstractHelpActivity extends Activity { - - private final int layoutId; - - @Nullable - private AdView adView; - - protected AbstractHelpActivity(int layoutId) { - this.layoutId = layoutId; - } - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(layoutId); - - // do not inflate ad in help (as some problems were encountered dut to ScrollView - no space for ad banner) - adView = AdsController.getInstance().inflateAd(this); - } - - @Override - protected void onDestroy() { - if (this.adView != null) { - this.adView.destroy(); - } - super.onDestroy(); - } -} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpActivity.java new file mode 100644 index 00000000..39e65320 --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpActivity.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.help; + +import android.os.Bundle; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.CalculatorFragmentActivity; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.about.CalculatorFragmentType; + +/** + * User: serso + * Date: 11/19/11 + * Time: 11:35 AM + */ +public class CalculatorHelpActivity extends CalculatorFragmentActivity { + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + getActivityHelper().addTab(this, CalculatorFragmentType.faq, null, R.id.main_layout); + getActivityHelper().addTab(this, CalculatorFragmentType.hints, null, R.id.main_layout); + getActivityHelper().addTab(this, CalculatorFragmentType.screens, null, R.id.main_layout); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpFaqFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpFaqFragment.java new file mode 100644 index 00000000..12d39285 --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpFaqFragment.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.help; + +import org.solovyev.android.calculator.CalculatorFragment; +import org.solovyev.android.calculator.about.CalculatorFragmentType; + +/** + * User: serso + * Date: 11/19/11 + * Time: 11:37 AM + */ +public class CalculatorHelpFaqFragment extends CalculatorFragment { + + public CalculatorHelpFaqFragment() { + super(CalculatorFragmentType.faq); + } +} \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpHintsFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpHintsFragment.java new file mode 100644 index 00000000..5cebfc86 --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpHintsFragment.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.help; + +import org.solovyev.android.calculator.CalculatorFragment; +import org.solovyev.android.calculator.about.CalculatorFragmentType; + +/** + * User: serso + * Date: 11/19/11 + * Time: 11:37 AM + */ +public class CalculatorHelpHintsFragment extends CalculatorFragment { + + public CalculatorHelpHintsFragment() { + super(CalculatorFragmentType.hints); + } +} \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpScreensFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpScreensFragment.java new file mode 100644 index 00000000..d1ebb4de --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpScreensFragment.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.help; + +import org.solovyev.android.calculator.CalculatorFragment; +import org.solovyev.android.calculator.about.CalculatorFragmentType; + +/** + * User: serso + * Date: 11/19/11 + * Time: 11:38 AM + */ +public class CalculatorHelpScreensFragment extends CalculatorFragment { + + public CalculatorHelpScreensFragment() { + super(CalculatorFragmentType.screens); + } + +} \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java deleted file mode 100644 index 2a483261..00000000 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.help; - -import android.app.Activity; -import android.app.TabActivity; -import android.content.Intent; -import android.os.Bundle; -import android.widget.TabHost; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.AndroidUtils; -import org.solovyev.android.LastTabSaver; -import org.solovyev.android.calculator.R; - -/** - * User: serso - * Date: 11/19/11 - * Time: 11:35 AM - */ -public class CalculatorHelpTabActivity extends TabActivity { - - @Nullable - private LastTabSaver lastTabSaver; - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.tabs); - - final TabHost tabHost = getTabHost(); - - createTab(tabHost, "faq", R.string.c_faq, HelpFaqActivity.class); - createTab(tabHost, "hints", R.string.c_hints, HelpHintsActivity.class); - createTab(tabHost, "screens", R.string.c_screens, HelpScreensActivity.class); - - this.lastTabSaver = new LastTabSaver(this, "faq"); - - AndroidUtils.centerAndWrapTabsFor(tabHost); - } - - private void createTab(@NotNull TabHost tabHost, - @NotNull String tabId, - int tabCaptionId, - @NotNull Class activityClass) { - - TabHost.TabSpec spec; - - final Intent intent = new Intent().setClass(this, activityClass); - - // Initialize a TabSpec for each tab and add it to the TabHost - spec = tabHost.newTabSpec(tabId).setIndicator(getString(tabCaptionId)).setContent(intent); - - tabHost.addTab(spec); - } - - @Override - protected void onDestroy() { - if (this.lastTabSaver != null) { - this.lastTabSaver.destroy(); - } - - super.onDestroy(); - } -} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/HelpFaqActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/HelpFaqActivity.java deleted file mode 100644 index bbbc8cc6..00000000 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/HelpFaqActivity.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.help; - -import org.solovyev.android.calculator.R; - -/** - * User: serso - * Date: 11/19/11 - * Time: 11:37 AM - */ -public class HelpFaqActivity extends AbstractHelpActivity { - - public HelpFaqActivity() { - super(R.layout.help_faq); - } -} \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/HelpHintsActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/HelpHintsActivity.java deleted file mode 100644 index 4e2b61b6..00000000 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/HelpHintsActivity.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.help; - -import org.solovyev.android.calculator.R; - -/** - * User: serso - * Date: 11/19/11 - * Time: 11:37 AM - */ -public class HelpHintsActivity extends AbstractHelpActivity { - - public HelpHintsActivity() { - super(R.layout.help_hints); - } -} \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/HelpScreensActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/help/HelpScreensActivity.java deleted file mode 100644 index 6ab7aff8..00000000 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/help/HelpScreensActivity.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.help; - -import org.solovyev.android.calculator.R; - -/** - * User: serso - * Date: 11/19/11 - * Time: 11:38 AM - */ -public class HelpScreensActivity extends AbstractHelpActivity{ - - public HelpScreensActivity() { - super(R.layout.help_screens); - } - -} \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/history/AbstractCalculatorHistoryFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/history/AbstractCalculatorHistoryFragment.java index e4ea8499..fd88c3c9 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/history/AbstractCalculatorHistoryFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/history/AbstractCalculatorHistoryFragment.java @@ -1,318 +1,319 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.history; - -import android.content.Context; -import android.os.Bundle; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.ListView; -import com.actionbarsherlock.app.SherlockListFragment; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.jscl.JsclOperation; -import org.solovyev.android.menu.AMenuBuilder; -import org.solovyev.android.menu.MenuImpl; -import org.solovyev.common.collections.CollectionsUtils; -import org.solovyev.common.equals.Equalizer; -import org.solovyev.common.filter.Filter; -import org.solovyev.common.filter.FilterRule; -import org.solovyev.common.filter.FilterRulesChain; -import org.solovyev.common.text.StringUtils; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * User: serso - * Date: 10/15/11 - * Time: 1:13 PM - */ -public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment implements CalculatorEventListener { - - /* - ********************************************************************** - * - * CONSTANTS - * - ********************************************************************** - */ - - @NotNull - private static final String TAG = "CalculatorHistoryFragment"; - - public static final Comparator COMPARATOR = new Comparator() { - @Override - public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) { - if (state1.isSaved() == state2.isSaved()) { - long l = state2.getTime() - state1.getTime(); - return l > 0l ? 1 : (l < 0l ? -1 : 0); - } else if (state1.isSaved()) { - return -1; - } else if (state2.isSaved()) { - return 1; - } - return 0; - } - }; - - /* - ********************************************************************** - * - * FIELDS - * - ********************************************************************** - */ - - - @NotNull - private ArrayAdapter adapter; - - @NotNull - private CalculatorFragmentHelper fragmentHelper; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(R.layout.history_fragment, getTitleResId(), false); - fragmentHelper.onCreate(this); - - logDebug("onCreate"); - } - - private int logDebug(@NotNull String msg) { - return Log.d(TAG + ": " + getTag(), msg); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return fragmentHelper.onCreateView(this, inflater, container); - } - - @Override - public void onViewCreated(View root, Bundle savedInstanceState) { - super.onViewCreated(root, savedInstanceState); - - logDebug("onViewCreated"); - - fragmentHelper.onViewCreated(this, root); - - adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList()); - setListAdapter(adapter); - - final ListView lv = getListView(); - lv.setTextFilterEnabled(true); - - lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { - public void onItemClick(final AdapterView parent, - final View view, - final int position, - final long id) { - - useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position)); - } - }); - - lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { - @Override - public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { - final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position); - - final Context context = getActivity(); - - final HistoryItemMenuData data = new HistoryItemMenuData(historyState, adapter); - - final List menuItems = CollectionsUtils.asList(HistoryItemMenuItem.values()); - - if (historyState.isSaved()) { - menuItems.remove(HistoryItemMenuItem.save); - } else { - if (isAlreadySaved(historyState)) { - menuItems.remove(HistoryItemMenuItem.save); - } - menuItems.remove(HistoryItemMenuItem.remove); - menuItems.remove(HistoryItemMenuItem.edit); - } - - if (historyState.getDisplayState().isValid() && StringUtils.isEmpty(historyState.getDisplayState().getEditorState().getText())) { - menuItems.remove(HistoryItemMenuItem.copy_result); - } - - final AMenuBuilder menuBuilder = AMenuBuilder.newInstance(context, MenuImpl.newInstance(menuItems)); - menuBuilder.create(data).show(); - - return true; - } - }); - } - - protected abstract int getTitleResId(); - - - @Override - public void onResume() { - super.onResume(); - - this.fragmentHelper.onResume(this); - - updateAdapter(); - } - - @Override - public void onPause() { - this.fragmentHelper.onPause(this); - - super.onPause(); - } - - @Override - public void onDestroy() { - logDebug("onDestroy"); - - fragmentHelper.onDestroy(this); - - super.onDestroy(); - } - - protected abstract int getItemLayoutId(); - - private void updateAdapter() { - final List historyList = getHistoryList(); - - final ArrayAdapter adapter = getAdapter(); - try { - adapter.setNotifyOnChange(false); - adapter.clear(); - for (CalculatorHistoryState historyState : historyList) { - adapter.add(historyState); - } - } finally { - adapter.setNotifyOnChange(true); - } - - adapter.notifyDataSetChanged(); - } - - public static boolean isAlreadySaved(@NotNull CalculatorHistoryState historyState) { - assert !historyState.isSaved(); - - boolean result = false; - try { - historyState.setSaved(true); - if ( CollectionsUtils.contains(historyState, CalculatorLocatorImpl.getInstance().getHistory().getSavedHistory(), new Equalizer() { - @Override - public boolean equals(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) { - return first != null && second != null && - first.getTime() == second.getTime() && - first.getDisplayState().equals(second.getDisplayState()) && - first.getEditorState().equals(second.getEditorState()); - } - }) ) { - result = true; - } - } finally { - historyState.setSaved(false); - } - return result; - } - - public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState) { - CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, historyState); - } - - @NotNull - private List getHistoryList() { - final List calculatorHistoryStates = getHistoryItems(); - - Collections.sort(calculatorHistoryStates, COMPARATOR); - - final FilterRulesChain filterRulesChain = new FilterRulesChain(); - filterRulesChain.addFilterRule(new FilterRule() { - @Override - public boolean isFiltered(CalculatorHistoryState object) { - return object == null || StringUtils.isEmpty(object.getEditorState().getText()); - } - }); - - new Filter(filterRulesChain).filter(calculatorHistoryStates.iterator()); - - return calculatorHistoryStates; - } - - @NotNull - protected abstract List getHistoryItems(); - - @NotNull - public static String getHistoryText(@NotNull CalculatorHistoryState state) { - final StringBuilder result = new StringBuilder(); - result.append(state.getEditorState().getText()); - result.append(getIdentitySign(state.getDisplayState().getJsclOperation())); - final String expressionResult = state.getDisplayState().getEditorState().getText(); - if (expressionResult != null) { - result.append(expressionResult); - } - return result.toString(); - } - - @NotNull - private static String getIdentitySign(@NotNull JsclOperation jsclOperation) { - return jsclOperation == JsclOperation.simplify ? "≡" : "="; - } - - // todo serso: menu -/* @Override - public boolean onCreateOptionsMenu(android.view.Menu menu) { - final MenuInflater menuInflater = getMenuInflater(); - menuInflater.inflate(R.menu.history_menu, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - boolean result; - - switch (item.getItemId()) { - case R.id.history_menu_clear_history: - clearHistory(); - result = true; - break; - default: - result = super.onOptionsItemSelected(item); - } - - return result; - }*/ - - protected abstract void clearHistory(); - - @NotNull - protected ArrayAdapter getAdapter() { - return adapter; - } - - @Override - public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { - if ( calculatorEventType == CalculatorEventType.history_state_added ) { - - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - logDebug("onCalculatorEvent"); - updateAdapter(); - } - }); - } - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.history; + +import android.content.Context; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import com.actionbarsherlock.app.SherlockListFragment; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.about.CalculatorFragmentType; +import org.solovyev.android.calculator.jscl.JsclOperation; +import org.solovyev.android.menu.AMenuBuilder; +import org.solovyev.android.menu.MenuImpl; +import org.solovyev.common.collections.CollectionsUtils; +import org.solovyev.common.equals.Equalizer; +import org.solovyev.common.filter.Filter; +import org.solovyev.common.filter.FilterRule; +import org.solovyev.common.filter.FilterRulesChain; +import org.solovyev.common.text.StringUtils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * User: serso + * Date: 10/15/11 + * Time: 1:13 PM + */ +public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment implements CalculatorEventListener { + + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + + @NotNull + private static final String TAG = "CalculatorHistoryFragment"; + + public static final Comparator COMPARATOR = new Comparator() { + @Override + public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) { + if (state1.isSaved() == state2.isSaved()) { + long l = state2.getTime() - state1.getTime(); + return l > 0l ? 1 : (l < 0l ? -1 : 0); + } else if (state1.isSaved()) { + return -1; + } else if (state2.isSaved()) { + return 1; + } + return 0; + } + }; + + /* + ********************************************************************** + * + * FIELDS + * + ********************************************************************** + */ + + + @NotNull + private ArrayAdapter adapter; + + @NotNull + private CalculatorFragmentHelper fragmentHelper; + + protected AbstractCalculatorHistoryFragment(@NotNull CalculatorFragmentType fragmentType) { + fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId(), false); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + fragmentHelper.onCreate(this); + + logDebug("onCreate"); + } + + private int logDebug(@NotNull String msg) { + return Log.d(TAG + ": " + getTag(), msg); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return fragmentHelper.onCreateView(this, inflater, container); + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + + logDebug("onViewCreated"); + + fragmentHelper.onViewCreated(this, root); + + adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList()); + setListAdapter(adapter); + + final ListView lv = getListView(); + lv.setTextFilterEnabled(true); + + lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + public void onItemClick(final AdapterView parent, + final View view, + final int position, + final long id) { + + useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position)); + } + }); + + lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { + final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position); + + final Context context = getActivity(); + + final HistoryItemMenuData data = new HistoryItemMenuData(historyState, adapter); + + final List menuItems = CollectionsUtils.asList(HistoryItemMenuItem.values()); + + if (historyState.isSaved()) { + menuItems.remove(HistoryItemMenuItem.save); + } else { + if (isAlreadySaved(historyState)) { + menuItems.remove(HistoryItemMenuItem.save); + } + menuItems.remove(HistoryItemMenuItem.remove); + menuItems.remove(HistoryItemMenuItem.edit); + } + + if (historyState.getDisplayState().isValid() && StringUtils.isEmpty(historyState.getDisplayState().getEditorState().getText())) { + menuItems.remove(HistoryItemMenuItem.copy_result); + } + + final AMenuBuilder menuBuilder = AMenuBuilder.newInstance(context, MenuImpl.newInstance(menuItems)); + menuBuilder.create(data).show(); + + return true; + } + }); + } + + @Override + public void onResume() { + super.onResume(); + + this.fragmentHelper.onResume(this); + + updateAdapter(); + } + + @Override + public void onPause() { + this.fragmentHelper.onPause(this); + + super.onPause(); + } + + @Override + public void onDestroy() { + logDebug("onDestroy"); + + fragmentHelper.onDestroy(this); + + super.onDestroy(); + } + + protected abstract int getItemLayoutId(); + + private void updateAdapter() { + final List historyList = getHistoryList(); + + final ArrayAdapter adapter = getAdapter(); + try { + adapter.setNotifyOnChange(false); + adapter.clear(); + for (CalculatorHistoryState historyState : historyList) { + adapter.add(historyState); + } + } finally { + adapter.setNotifyOnChange(true); + } + + adapter.notifyDataSetChanged(); + } + + public static boolean isAlreadySaved(@NotNull CalculatorHistoryState historyState) { + assert !historyState.isSaved(); + + boolean result = false; + try { + historyState.setSaved(true); + if ( CollectionsUtils.contains(historyState, CalculatorLocatorImpl.getInstance().getHistory().getSavedHistory(), new Equalizer() { + @Override + public boolean equals(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) { + return first != null && second != null && + first.getTime() == second.getTime() && + first.getDisplayState().equals(second.getDisplayState()) && + first.getEditorState().equals(second.getEditorState()); + } + }) ) { + result = true; + } + } finally { + historyState.setSaved(false); + } + return result; + } + + public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState) { + CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, historyState); + } + + @NotNull + private List getHistoryList() { + final List calculatorHistoryStates = getHistoryItems(); + + Collections.sort(calculatorHistoryStates, COMPARATOR); + + final FilterRulesChain filterRulesChain = new FilterRulesChain(); + filterRulesChain.addFilterRule(new FilterRule() { + @Override + public boolean isFiltered(CalculatorHistoryState object) { + return object == null || StringUtils.isEmpty(object.getEditorState().getText()); + } + }); + + new Filter(filterRulesChain).filter(calculatorHistoryStates.iterator()); + + return calculatorHistoryStates; + } + + @NotNull + protected abstract List getHistoryItems(); + + @NotNull + public static String getHistoryText(@NotNull CalculatorHistoryState state) { + final StringBuilder result = new StringBuilder(); + result.append(state.getEditorState().getText()); + result.append(getIdentitySign(state.getDisplayState().getJsclOperation())); + final String expressionResult = state.getDisplayState().getEditorState().getText(); + if (expressionResult != null) { + result.append(expressionResult); + } + return result.toString(); + } + + @NotNull + private static String getIdentitySign(@NotNull JsclOperation jsclOperation) { + return jsclOperation == JsclOperation.simplify ? "≡" : "="; + } + + // todo serso: menu +/* @Override + public boolean onCreateOptionsMenu(android.view.Menu menu) { + final MenuInflater menuInflater = getMenuInflater(); + menuInflater.inflate(R.menu.history_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + boolean result; + + switch (item.getItemId()) { + case R.id.history_menu_clear_history: + clearHistory(); + result = true; + break; + default: + result = super.onOptionsItemSelected(item); + } + + return result; + }*/ + + protected abstract void clearHistory(); + + @NotNull + protected ArrayAdapter getAdapter() { + return adapter; + } + + @Override + public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { + if ( calculatorEventType == CalculatorEventType.history_state_added ) { + + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + logDebug("onCalculatorEvent"); + updateAdapter(); + } + }); + } + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryFragmentActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java similarity index 88% rename from calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryFragmentActivity.java rename to calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java index 748c6d89..30896450 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryFragmentActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java @@ -18,10 +18,10 @@ import org.solovyev.android.calculator.about.CalculatorFragmentType; * Date: 12/18/11 * Time: 7:37 PM */ -public class CalculatorHistoryFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { +public class CalculatorHistoryActivity extends SherlockFragmentActivity implements CalculatorEventListener { @NotNull - private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName()); + private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName()); @Override public void onCreate(@Nullable Bundle savedInstanceState) { diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryFragment.java index f80be9a3..e38f3485 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryFragment.java @@ -1,44 +1,44 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.history; - -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.CalculatorLocatorImpl; -import org.solovyev.android.calculator.R; - -import java.util.ArrayList; -import java.util.List; - -/** - * User: serso - * Date: 12/18/11 - * Time: 7:39 PM - */ -public class CalculatorHistoryFragment extends AbstractCalculatorHistoryFragment { - - @Override - protected int getTitleResId() { - return R.string.c_history; - } - - @Override - protected int getItemLayoutId() { - return R.layout.history; - } - - @NotNull - @Override - protected List getHistoryItems() { - return new ArrayList(CalculatorLocatorImpl.getInstance().getHistory().getStates()); - } - - @Override - protected void clearHistory() { - CalculatorLocatorImpl.getInstance().getHistory().clear(); - getAdapter().clear(); - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.history; + +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.about.CalculatorFragmentType; + +import java.util.ArrayList; +import java.util.List; + +/** + * User: serso + * Date: 12/18/11 + * Time: 7:39 PM + */ +public class CalculatorHistoryFragment extends AbstractCalculatorHistoryFragment { + + public CalculatorHistoryFragment() { + super(CalculatorFragmentType.history); + } + + @Override + protected int getItemLayoutId() { + return R.layout.history_item; + } + + @NotNull + @Override + protected List getHistoryItems() { + return new ArrayList(CalculatorLocatorImpl.getInstance().getHistory().getStates()); + } + + @Override + protected void clearHistory() { + CalculatorLocatorImpl.getInstance().getHistory().clear(); + getAdapter().clear(); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorSavedHistoryFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorSavedHistoryFragment.java index 766de564..98fcec90 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorSavedHistoryFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/history/CalculatorSavedHistoryFragment.java @@ -1,44 +1,44 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.history; - -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.CalculatorLocatorImpl; -import org.solovyev.android.calculator.R; - -import java.util.ArrayList; -import java.util.List; - -/** - * User: serso - * Date: 12/18/11 - * Time: 7:40 PM - */ -public class CalculatorSavedHistoryFragment extends AbstractCalculatorHistoryFragment { - - @Override - protected int getTitleResId() { - return R.string.c_saved_history; - } - - @Override - protected int getItemLayoutId() { - return R.layout.saved_history; - } - - @NotNull - @Override - protected List getHistoryItems() { - return new ArrayList(CalculatorLocatorImpl.getInstance().getHistory().getSavedHistory()); - } - - @Override - protected void clearHistory() { - CalculatorLocatorImpl.getInstance().getHistory().clearSavedHistory(); - getAdapter().clear(); - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.history; + +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.about.CalculatorFragmentType; + +import java.util.ArrayList; +import java.util.List; + +/** + * User: serso + * Date: 12/18/11 + * Time: 7:40 PM + */ +public class CalculatorSavedHistoryFragment extends AbstractCalculatorHistoryFragment { + + public CalculatorSavedHistoryFragment() { + super(CalculatorFragmentType.saved_history); + } + + @Override + protected int getItemLayoutId() { + return R.layout.saved_history_item; + } + + @NotNull + @Override + protected List getHistoryItems() { + return new ArrayList(CalculatorLocatorImpl.getInstance().getHistory().getSavedHistory()); + } + + @Override + protected void clearHistory() { + CalculatorLocatorImpl.getInstance().getHistory().clearSavedHistory(); + getAdapter().clear(); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java index 31ff545d..33003230 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/AbstractMathEntityListFragment.java @@ -1,351 +1,349 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.math.edit; - -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.os.Handler; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.*; -import com.actionbarsherlock.app.SherlockListFragment; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.*; -import org.solovyev.android.menu.AMenuBuilder; -import org.solovyev.android.menu.AMenuItem; -import org.solovyev.android.menu.LabeledMenuItem; -import org.solovyev.android.menu.MenuImpl; -import org.solovyev.common.equals.EqualsTool; -import org.solovyev.common.filter.Filter; -import org.solovyev.common.filter.FilterRule; -import org.solovyev.common.math.MathEntity; -import org.solovyev.common.text.StringUtils; - -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; - -/** - * User: serso - * Date: 12/21/11 - * Time: 9:24 PM - */ -public abstract class AbstractMathEntityListFragment extends SherlockListFragment implements CalculatorEventListener { - - /* - ********************************************************************** - * - * CONSTANTS - * - ********************************************************************** - */ - - public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category"; - - protected final static List acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray())); - - - /* - ********************************************************************** - * - * FIELDS - * - ********************************************************************** - */ - - @Nullable - private MathEntityArrayAdapter adapter; - - @Nullable - private String category; - - @NotNull - private CalculatorFragmentHelper fragmentHelper; - - @NotNull - private final Handler uiHandler = new Handler(); - - protected int getLayoutId() { - return R.layout.math_entities_fragment; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - final Bundle bundle = getArguments(); - if (bundle != null) { - category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING); - } - - fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(getLayoutId(), getTitleResId()); - fragmentHelper.onCreate(this); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return fragmentHelper.onCreateView(this, inflater, container); - } - - @Override - public void onViewCreated(View root, Bundle savedInstanceState) { - super.onViewCreated(root, savedInstanceState); - - fragmentHelper.onViewCreated(this, root); - - final ListView lv = getListView(); - lv.setTextFilterEnabled(true); - - lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { - public void onItemClick(final AdapterView parent, - final View view, - final int position, - final long id) { - final AMenuItem onClick = getOnClickAction(); - if (onClick != null) { - onClick.onClick(((T) parent.getItemAtPosition(position)), getActivity()); - } - } - }); - - getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { - @Override - public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { - final T item = (T) parent.getItemAtPosition(position); - - final List> menuItems = getMenuItemsOnLongClick(item); - - if (!menuItems.isEmpty()) { - final AMenuBuilder, T> menuBuilder = AMenuBuilder.newInstance(AbstractMathEntityListFragment.this.getActivity(), MenuImpl.newInstance(menuItems)); - menuBuilder.create(item).show(); - } - - return true; - } - }); - } - - @Nullable - protected abstract AMenuItem getOnClickAction(); - - protected abstract int getTitleResId(); - - @Override - public void onDestroy() { - fragmentHelper.onDestroy(this); - - super.onDestroy(); - } - - @NotNull - protected abstract List> getMenuItemsOnLongClick(@NotNull T item); - - @Override - public void onPause() { - this.fragmentHelper.onPause(this); - - super.onPause(); - } - - @Override - public void onResume() { - super.onResume(); - - this.fragmentHelper.onResume(this); - - adapter = new MathEntityArrayAdapter(getDescriptionGetter(), this.getActivity(), R.layout.math_entity, R.id.math_entity_text, getMathEntitiesByCategory()); - setListAdapter(adapter); - - sort(); - } - - @NotNull - private List getMathEntitiesByCategory() { - final List result = getMathEntities(); - - new Filter(new FilterRule() { - @Override - public boolean isFiltered(T t) { - return !isInCategory(t); - } - }).filter(result.iterator()); - - return result; - } - - protected boolean isInCategory(@Nullable T t) { - return t != null && (category == null || EqualsTool.areEqual(getMathEntityCategory(t), category)); - } - - @NotNull - protected abstract MathEntityDescriptionGetter getDescriptionGetter(); - - @NotNull - protected abstract List getMathEntities(); - - @Nullable - abstract String getMathEntityCategory(@NotNull T t); - - protected void sort() { - final MathEntityArrayAdapter localAdapter = adapter; - if (localAdapter != null) { - localAdapter.sort(new Comparator() { - @Override - public int compare(T function1, T function2) { - return function1.getName().compareTo(function2.getName()); - } - }); - - localAdapter.notifyDataSetChanged(); - } - } - - protected static class MathEntityArrayAdapter extends ArrayAdapter { - - @NotNull - private final MathEntityDescriptionGetter descriptionGetter; - - private MathEntityArrayAdapter(@NotNull MathEntityDescriptionGetter descriptionGetter, - @NotNull Context context, - int resource, - int textViewResourceId, - @NotNull List objects) { - - super(context, resource, textViewResourceId, objects); - this.descriptionGetter = descriptionGetter; - } - - @Override - public View getView(int position, @Nullable View convertView, ViewGroup parent) { - final ViewGroup result; - - if (convertView == null) { - result = (ViewGroup) super.getView(position, convertView, parent); - fillView(position, result); - } else { - result = (ViewGroup) convertView; - fillView(position, result); - } - - - return result; - } - - private void fillView(int position, @NotNull ViewGroup result) { - final T mathEntity = getItem(position); - - final TextView text = (TextView) result.findViewById(R.id.math_entity_text); - text.setText(String.valueOf(mathEntity)); - - final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName()); - - final TextView description = (TextView) result.findViewById(R.id.math_entity_description); - if (!StringUtils.isEmpty(mathEntityDescription)) { - description.setVisibility(View.VISIBLE); - description.setText(mathEntityDescription); - } else { - description.setVisibility(View.GONE); - } - } - } - - protected static class MathEntityDescriptionGetterImpl implements MathEntityDescriptionGetter { - - @NotNull - private final CalculatorMathRegistry mathRegistry; - - public MathEntityDescriptionGetterImpl(@NotNull CalculatorMathRegistry mathRegistry) { - this.mathRegistry = mathRegistry; - } - - @Override - public String getDescription(@NotNull Context context, @NotNull String mathEntityName) { - return this.mathRegistry.getDescription(mathEntityName); - } - } - - protected static interface MathEntityDescriptionGetter { - - @Nullable - String getDescription(@NotNull Context context, @NotNull String mathEntityName); - } - - public void addToAdapter(@NotNull T mathEntity) { - if (this.adapter != null) { - this.adapter.add(mathEntity); - } - } - - public void removeFromAdapter(@NotNull T mathEntity) { - if (this.adapter != null) { - this.adapter.remove(mathEntity); - } - } - - public void notifyAdapter() { - if (this.adapter != null) { - this.adapter.notifyDataSetChanged(); - } - } - - @NotNull - protected Handler getUiHandler() { - return uiHandler; - } - - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - - static void createTab(@NotNull Context context, - @NotNull TabHost tabHost, - @NotNull String tabId, - @NotNull String categoryId, - int tabCaptionId, - @NotNull Class activityClass, - @Nullable Intent parentIntent) { - - TabHost.TabSpec spec; - - final Intent intent; - if (parentIntent != null) { - intent = new Intent(parentIntent); - } else { - intent = new Intent(); - } - intent.setClass(context, activityClass); - intent.putExtra(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId); - - // Initialize a TabSpec for each tab and add it to the TabHost - spec = tabHost.newTabSpec(tabId).setIndicator(context.getString(tabCaptionId)).setContent(intent); - - tabHost.addTab(spec); - } - - @NotNull - public static Bundle createBundleFor(@NotNull String categoryId) { - final Bundle result = new Bundle(1); - putCategory(result, categoryId); - return result; - } - - static void putCategory(@NotNull Bundle bundle, @NotNull String categoryId) { - bundle.putString(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId); - } - - @Override - public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math.edit; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.*; +import com.actionbarsherlock.app.SherlockListFragment; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.about.CalculatorFragmentType; +import org.solovyev.android.menu.AMenuBuilder; +import org.solovyev.android.menu.AMenuItem; +import org.solovyev.android.menu.LabeledMenuItem; +import org.solovyev.android.menu.MenuImpl; +import org.solovyev.common.equals.EqualsTool; +import org.solovyev.common.filter.Filter; +import org.solovyev.common.filter.FilterRule; +import org.solovyev.common.math.MathEntity; +import org.solovyev.common.text.StringUtils; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +/** + * User: serso + * Date: 12/21/11 + * Time: 9:24 PM + */ +public abstract class AbstractMathEntityListFragment extends SherlockListFragment implements CalculatorEventListener { + + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + + public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category"; + + protected final static List acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray())); + + + /* + ********************************************************************** + * + * FIELDS + * + ********************************************************************** + */ + + @Nullable + private MathEntityArrayAdapter adapter; + + @Nullable + private String category; + + @NotNull + private final CalculatorFragmentHelper fragmentHelper; + + @NotNull + private final Handler uiHandler = new Handler(); + + protected AbstractMathEntityListFragment(@NotNull CalculatorFragmentType fragmentType) { + fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(fragmentType.getDefaultLayoutId(),fragmentType.getDefaultTitleResId()); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + final Bundle bundle = getArguments(); + if (bundle != null) { + category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING); + } + + fragmentHelper.onCreate(this); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return fragmentHelper.onCreateView(this, inflater, container); + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + + fragmentHelper.onViewCreated(this, root); + + final ListView lv = getListView(); + lv.setTextFilterEnabled(true); + + lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + public void onItemClick(final AdapterView parent, + final View view, + final int position, + final long id) { + final AMenuItem onClick = getOnClickAction(); + if (onClick != null) { + onClick.onClick(((T) parent.getItemAtPosition(position)), getActivity()); + } + } + }); + + getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { + final T item = (T) parent.getItemAtPosition(position); + + final List> menuItems = getMenuItemsOnLongClick(item); + + if (!menuItems.isEmpty()) { + final AMenuBuilder, T> menuBuilder = AMenuBuilder.newInstance(AbstractMathEntityListFragment.this.getActivity(), MenuImpl.newInstance(menuItems)); + menuBuilder.create(item).show(); + } + + return true; + } + }); + } + + @Nullable + protected abstract AMenuItem getOnClickAction(); + + @Override + public void onDestroy() { + fragmentHelper.onDestroy(this); + + super.onDestroy(); + } + + @NotNull + protected abstract List> getMenuItemsOnLongClick(@NotNull T item); + + @Override + public void onPause() { + this.fragmentHelper.onPause(this); + + super.onPause(); + } + + @Override + public void onResume() { + super.onResume(); + + this.fragmentHelper.onResume(this); + + adapter = new MathEntityArrayAdapter(getDescriptionGetter(), this.getActivity(), R.layout.math_entity, R.id.math_entity_text, getMathEntitiesByCategory()); + setListAdapter(adapter); + + sort(); + } + + @NotNull + private List getMathEntitiesByCategory() { + final List result = getMathEntities(); + + new Filter(new FilterRule() { + @Override + public boolean isFiltered(T t) { + return !isInCategory(t); + } + }).filter(result.iterator()); + + return result; + } + + protected boolean isInCategory(@Nullable T t) { + return t != null && (category == null || EqualsTool.areEqual(getMathEntityCategory(t), category)); + } + + @NotNull + protected abstract MathEntityDescriptionGetter getDescriptionGetter(); + + @NotNull + protected abstract List getMathEntities(); + + @Nullable + abstract String getMathEntityCategory(@NotNull T t); + + protected void sort() { + final MathEntityArrayAdapter localAdapter = adapter; + if (localAdapter != null) { + localAdapter.sort(new Comparator() { + @Override + public int compare(T function1, T function2) { + return function1.getName().compareTo(function2.getName()); + } + }); + + localAdapter.notifyDataSetChanged(); + } + } + + protected static class MathEntityArrayAdapter extends ArrayAdapter { + + @NotNull + private final MathEntityDescriptionGetter descriptionGetter; + + private MathEntityArrayAdapter(@NotNull MathEntityDescriptionGetter descriptionGetter, + @NotNull Context context, + int resource, + int textViewResourceId, + @NotNull List objects) { + + super(context, resource, textViewResourceId, objects); + this.descriptionGetter = descriptionGetter; + } + + @Override + public View getView(int position, @Nullable View convertView, ViewGroup parent) { + final ViewGroup result; + + if (convertView == null) { + result = (ViewGroup) super.getView(position, convertView, parent); + fillView(position, result); + } else { + result = (ViewGroup) convertView; + fillView(position, result); + } + + + return result; + } + + private void fillView(int position, @NotNull ViewGroup result) { + final T mathEntity = getItem(position); + + final TextView text = (TextView) result.findViewById(R.id.math_entity_text); + text.setText(String.valueOf(mathEntity)); + + final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName()); + + final TextView description = (TextView) result.findViewById(R.id.math_entity_description); + if (!StringUtils.isEmpty(mathEntityDescription)) { + description.setVisibility(View.VISIBLE); + description.setText(mathEntityDescription); + } else { + description.setVisibility(View.GONE); + } + } + } + + protected static class MathEntityDescriptionGetterImpl implements MathEntityDescriptionGetter { + + @NotNull + private final CalculatorMathRegistry mathRegistry; + + public MathEntityDescriptionGetterImpl(@NotNull CalculatorMathRegistry mathRegistry) { + this.mathRegistry = mathRegistry; + } + + @Override + public String getDescription(@NotNull Context context, @NotNull String mathEntityName) { + return this.mathRegistry.getDescription(mathEntityName); + } + } + + protected static interface MathEntityDescriptionGetter { + + @Nullable + String getDescription(@NotNull Context context, @NotNull String mathEntityName); + } + + public void addToAdapter(@NotNull T mathEntity) { + if (this.adapter != null) { + this.adapter.add(mathEntity); + } + } + + public void removeFromAdapter(@NotNull T mathEntity) { + if (this.adapter != null) { + this.adapter.remove(mathEntity); + } + } + + public void notifyAdapter() { + if (this.adapter != null) { + this.adapter.notifyDataSetChanged(); + } + } + + @NotNull + protected Handler getUiHandler() { + return uiHandler; + } + + /* + ********************************************************************** + * + * STATIC + * + ********************************************************************** + */ + + static void createTab(@NotNull Context context, + @NotNull TabHost tabHost, + @NotNull String tabId, + @NotNull String categoryId, + int tabCaptionId, + @NotNull Class activityClass, + @Nullable Intent parentIntent) { + + TabHost.TabSpec spec; + + final Intent intent; + if (parentIntent != null) { + intent = new Intent(parentIntent); + } else { + intent = new Intent(); + } + intent.setClass(context, activityClass); + intent.putExtra(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId); + + // Initialize a TabSpec for each tab and add it to the TabHost + spec = tabHost.newTabSpec(tabId).setIndicator(context.getString(tabCaptionId)).setContent(intent); + + tabHost.addTab(spec); + } + + @NotNull + public static Bundle createBundleFor(@NotNull String categoryId) { + final Bundle result = new Bundle(1); + putCategory(result, categoryId); + return result; + } + + static void putCategory(@NotNull Bundle bundle, @NotNull String categoryId) { + bundle.putString(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId); + } + + @Override + public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragmentActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java similarity index 89% rename from calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragmentActivity.java rename to calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java index 55e6469a..76d785b6 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragmentActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java @@ -12,7 +12,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.about.CalculatorFragmentType; -import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity; +import org.solovyev.android.calculator.history.CalculatorHistoryActivity; import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; /** @@ -20,10 +20,10 @@ import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; * Date: 12/21/11 * Time: 10:33 PM */ -public class CalculatorFunctionsFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { +public class CalculatorFunctionsActivity extends SherlockFragmentActivity implements CalculatorEventListener { @NotNull - private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName()); + private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName()); @Override public void onCreate(@Nullable Bundle savedInstanceState) { diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragment.java index c3f8ee8a..e7d524db 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsFragment.java @@ -1,238 +1,238 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.math.edit; - -import android.app.Activity; -import android.content.Context; -import android.os.Bundle; -import android.text.ClipboardManager; -import jscl.math.function.Function; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorLocatorImpl; -import org.solovyev.android.calculator.R; -import org.solovyev.android.menu.AMenuItem; -import org.solovyev.android.menu.LabeledMenuItem; -import org.solovyev.common.text.StringUtils; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * User: serso - * Date: 10/29/11 - * Time: 4:55 PM - */ -public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment { - - public static final String CREATE_FUN_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity_create_fun"; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - /*getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { - @Override - public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { - final Function function = (Function) parent.getItemAtPosition(position); - if (function instanceof CustomFunction) { - createEditVariableDialog(CalculatorFunctionsTabActivity.this, - ((CustomFunction) function), - function.getName(), - ((CustomFunction) function).getContent(), - ((CustomFunction) function).getParameterNames(), - null); - } - return true; - } - });*/ - - /*final Intent intent = getIntent(); - if (intent != null) { - final String varValue = intent.getStringExtra(CREATE_FUN_EXTRA_STRING); - if (!StringUtils.isEmpty(varValue)) { - createEditVariableDialog(this, null, null, varValue, null, null); - - // in order to stop intent for other tabs - intent.removeExtra(CREATE_FUN_EXTRA_STRING); - } - }*/ - } - - @Override - protected AMenuItem getOnClickAction() { - return LongClickMenuItem.use; - } - - @Override - protected int getTitleResId() { - return R.string.c_functions; - } - - @NotNull - @Override - protected List> getMenuItemsOnLongClick(@NotNull Function item) { - List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); - - if ( StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(item.getName())) ) { - result.remove(LongClickMenuItem.copy_description); - } - - return result; - } - -/* private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity activity, - @Nullable final CustomFunction function, - @Nullable final String name, - @Nullable final String expression, - @Nullable final String[] parameterNames, - @Nullable final String description) { - if (function == null || !function.isSystem()) { - - final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE); - final View editView = layoutInflater.inflate(R.layout.var_edit, null); - - final String errorMsg = activity.getString(R.string.c_char_is_not_accepted); - - final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name); - editName.setText(name); - editName.addTextChangedListener(new TextWatcher() { - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (!acceptableChars.contains(c)) { - s.delete(i, i + 1); - Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show(); - } - } - } - }); - - final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value); - if (!StringUtils.isEmpty(expression)) { - editValue.setText(expression); - } - - final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); - editDescription.setText(description); - - final CustomFunction.Builder functionBuilder; - if (function != null) { - functionBuilder = new CustomFunction.Builder(function); - } else { - functionBuilder = new CustomFunction.Builder(); - } - - final AlertDialog.Builder builder = new AlertDialog.Builder(activity) - .setCancelable(true) - .setNegativeButton(R.string.c_cancel, null) - .setPositiveButton(R.string.c_save, new FunctionEditorSaver(functionBuilder, function, editView, activity, CalculatorEngine.instance.getFunctionsRegistry(), new FunctionEditorSaver.EditorCreator() { - - @Override - public void showEditor(@NotNull AbstractMathEntityListActivity activity, @Nullable CustomFunction editedInstance, @Nullable String name, @Nullable String value, @Nullable String[] parameterNames, @Nullable String description) { - createEditVariableDialog(activity, editedInstance, name, value, parameterNames, description); - } - })) - .setView(editView); - - if (function != null) { - // EDIT mode - - builder.setTitle(R.string.c_var_edit_var); - builder.setNeutralButton(R.string.c_remove, new MathEntityRemover(function, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - createEditVariableDialog(activity, function, name, expression, parameterNames, description); - } - }, CalculatorEngine.instance.getFunctionsRegistry(), activity)); - } else { - // CREATE mode - - builder.setTitle(R.string.c_var_create_var); - } - - builder.create().show(); - } else { - Toast.makeText(activity, activity.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show(); - } - }*/ - - @NotNull - @Override - protected MathEntityDescriptionGetter getDescriptionGetter() { - return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry()); - } - - @NotNull - @Override - protected List getMathEntities() { - return new ArrayList(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getEntities()); - } - - @Override - protected String getMathEntityCategory(@NotNull Function function) { - return CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getCategory(function); - } - - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - - private static enum LongClickMenuItem implements LabeledMenuItem { - use(R.string.c_use) { - @Override - public void onClick(@NotNull Function data, @NotNull Context context) { - CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_function, data); - } - }, - - /*edit(R.string.c_edit) { - @Override - public void doAction(@NotNull Function data, @NotNull Context context) { - if (context instanceof AbstractMathEntityListActivity) { - } - } - },*/ - - copy_description(R.string.c_copy_description) { - @Override - public void onClick(@NotNull Function data, @NotNull Context context) { - final String text = CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(data.getName()); - if (!StringUtils.isEmpty(text)) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(text); - } - } - }; - private final int captionId; - - LongClickMenuItem(int captionId) { - this.captionId = captionId; - } - - @NotNull - @Override - public String getCaption(@NotNull Context context) { - return context.getString(captionId); - } - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math.edit; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.text.ClipboardManager; +import jscl.math.function.Function; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.CalculatorEventType; +import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.about.CalculatorFragmentType; +import org.solovyev.android.menu.AMenuItem; +import org.solovyev.android.menu.LabeledMenuItem; +import org.solovyev.common.text.StringUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * User: serso + * Date: 10/29/11 + * Time: 4:55 PM + */ +public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment { + + public static final String CREATE_FUN_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity_create_fun"; + + public CalculatorFunctionsFragment() { + super(CalculatorFragmentType.functions); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + /*getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { + final Function function = (Function) parent.getItemAtPosition(position); + if (function instanceof CustomFunction) { + createEditVariableDialog(CalculatorFunctionsTabActivity.this, + ((CustomFunction) function), + function.getName(), + ((CustomFunction) function).getContent(), + ((CustomFunction) function).getParameterNames(), + null); + } + return true; + } + });*/ + + /*final Intent intent = getIntent(); + if (intent != null) { + final String varValue = intent.getStringExtra(CREATE_FUN_EXTRA_STRING); + if (!StringUtils.isEmpty(varValue)) { + createEditVariableDialog(this, null, null, varValue, null, null); + + // in order to stop intent for other tabs + intent.removeExtra(CREATE_FUN_EXTRA_STRING); + } + }*/ + } + + @Override + protected AMenuItem getOnClickAction() { + return LongClickMenuItem.use; + } + + @NotNull + @Override + protected List> getMenuItemsOnLongClick(@NotNull Function item) { + List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); + + if ( StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(item.getName())) ) { + result.remove(LongClickMenuItem.copy_description); + } + + return result; + } + +/* private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity activity, + @Nullable final CustomFunction function, + @Nullable final String name, + @Nullable final String expression, + @Nullable final String[] parameterNames, + @Nullable final String description) { + if (function == null || !function.isSystem()) { + + final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE); + final View editView = layoutInflater.inflate(R.layout.var_edit, null); + + final String errorMsg = activity.getString(R.string.c_char_is_not_accepted); + + final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name); + editName.setText(name); + editName.addTextChangedListener(new TextWatcher() { + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (!acceptableChars.contains(c)) { + s.delete(i, i + 1); + Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show(); + } + } + } + }); + + final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value); + if (!StringUtils.isEmpty(expression)) { + editValue.setText(expression); + } + + final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); + editDescription.setText(description); + + final CustomFunction.Builder functionBuilder; + if (function != null) { + functionBuilder = new CustomFunction.Builder(function); + } else { + functionBuilder = new CustomFunction.Builder(); + } + + final AlertDialog.Builder builder = new AlertDialog.Builder(activity) + .setCancelable(true) + .setNegativeButton(R.string.c_cancel, null) + .setPositiveButton(R.string.c_save, new FunctionEditorSaver(functionBuilder, function, editView, activity, CalculatorEngine.instance.getFunctionsRegistry(), new FunctionEditorSaver.EditorCreator() { + + @Override + public void showEditor(@NotNull AbstractMathEntityListActivity activity, @Nullable CustomFunction editedInstance, @Nullable String name, @Nullable String value, @Nullable String[] parameterNames, @Nullable String description) { + createEditVariableDialog(activity, editedInstance, name, value, parameterNames, description); + } + })) + .setView(editView); + + if (function != null) { + // EDIT mode + + builder.setTitle(R.string.c_var_edit_var); + builder.setNeutralButton(R.string.c_remove, new MathEntityRemover(function, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + createEditVariableDialog(activity, function, name, expression, parameterNames, description); + } + }, CalculatorEngine.instance.getFunctionsRegistry(), activity)); + } else { + // CREATE mode + + builder.setTitle(R.string.c_var_create_var); + } + + builder.create().show(); + } else { + Toast.makeText(activity, activity.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show(); + } + }*/ + + @NotNull + @Override + protected MathEntityDescriptionGetter getDescriptionGetter() { + return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry()); + } + + @NotNull + @Override + protected List getMathEntities() { + return new ArrayList(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getEntities()); + } + + @Override + protected String getMathEntityCategory(@NotNull Function function) { + return CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getCategory(function); + } + + /* + ********************************************************************** + * + * STATIC + * + ********************************************************************** + */ + + private static enum LongClickMenuItem implements LabeledMenuItem { + use(R.string.c_use) { + @Override + public void onClick(@NotNull Function data, @NotNull Context context) { + CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_function, data); + } + }, + + /*edit(R.string.c_edit) { + @Override + public void doAction(@NotNull Function data, @NotNull Context context) { + if (context instanceof AbstractMathEntityListActivity) { + } + } + },*/ + + copy_description(R.string.c_copy_description) { + @Override + public void onClick(@NotNull Function data, @NotNull Context context) { + final String text = CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(data.getName()); + if (!StringUtils.isEmpty(text)) { + final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); + clipboard.setText(text); + } + } + }; + private final int captionId; + + LongClickMenuItem(int captionId) { + this.captionId = captionId; + } + + @NotNull + @Override + public String getCaption(@NotNull Context context) { + return context.getString(captionId); + } + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragmentActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java similarity index 86% rename from calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragmentActivity.java rename to calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java index 2e3053b0..8884fac4 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragmentActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java @@ -12,8 +12,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.about.CalculatorFragmentType; -import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity; -import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; +import org.solovyev.android.calculator.history.CalculatorHistoryActivity; import org.solovyev.android.calculator.model.AndroidOperatorsMathRegistry; /** @@ -21,10 +20,10 @@ import org.solovyev.android.calculator.model.AndroidOperatorsMathRegistry; * Date: 12/21/11 * Time: 10:33 PM */ -public class CalculatorOperatorsFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { +public class CalculatorOperatorsActivity extends SherlockFragmentActivity implements CalculatorEventListener { @NotNull - private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName()); + private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName()); @Override public void onCreate(@Nullable Bundle savedInstanceState) { diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragment.java index 85de1593..1817515f 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsFragment.java @@ -1,133 +1,133 @@ -package org.solovyev.android.calculator.math.edit; - -import android.app.Activity; -import android.content.Context; -import android.text.ClipboardManager; -import jscl.math.operator.Operator; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorLocatorImpl; -import org.solovyev.android.calculator.R; -import org.solovyev.android.menu.AMenuItem; -import org.solovyev.android.menu.LabeledMenuItem; -import org.solovyev.common.text.StringUtils; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * User: serso - * Date: 11/17/11 - * Time: 1:53 PM - */ - -public class CalculatorOperatorsFragment extends AbstractMathEntityListFragment { - - @Override - protected AMenuItem getOnClickAction() { - return LongClickMenuItem.use; - } - - @Override - protected int getTitleResId() { - return R.string.c_operators; - } - - @NotNull - @Override - protected List> getMenuItemsOnLongClick(@NotNull Operator item) { - final List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); - - if ( StringUtils.isEmpty(OperatorDescriptionGetter.instance.getDescription(this.getActivity(), item.getName())) ) { - result.remove(LongClickMenuItem.copy_description); - } - - return result; - } - - @NotNull - @Override - protected MathEntityDescriptionGetter getDescriptionGetter() { - return OperatorDescriptionGetter.instance; - } - - - @NotNull - @Override - protected List getMathEntities() { - final List result = new ArrayList(); - - result.addAll(CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getEntities()); - result.addAll(CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getEntities()); - - return result; - } - - @Override - protected String getMathEntityCategory(@NotNull Operator operator) { - String result = CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getCategory(operator); - if (result == null) { - result = CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getCategory(operator); - } - - return result; - } - - private static enum OperatorDescriptionGetter implements MathEntityDescriptionGetter { - - instance; - - @Override - public String getDescription(@NotNull Context context, @NotNull String mathEntityName) { - String result = CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getDescription(mathEntityName); - if (StringUtils.isEmpty(result)) { - result = CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getDescription(mathEntityName); - } - - return result; - } - } - - /* - ********************************************************************** - * - * STATIC - * - ********************************************************************** - */ - - private static enum LongClickMenuItem implements LabeledMenuItem { - - use(R.string.c_use) { - @Override - public void onClick(@NotNull Operator data, @NotNull Context context) { - CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, data); - } - }, - - copy_description(R.string.c_copy_description) { - @Override - public void onClick(@NotNull Operator data, @NotNull Context context) { - final String text = OperatorDescriptionGetter.instance.getDescription(context, data.getName()); - if (!StringUtils.isEmpty(text)) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(text); - } - } - }; - private final int captionId; - - LongClickMenuItem(int captionId) { - this.captionId = captionId; - } - - @NotNull - @Override - public String getCaption(@NotNull Context context) { - return context.getString(captionId); - } - } - -} - +package org.solovyev.android.calculator.math.edit; + +import android.app.Activity; +import android.content.Context; +import android.text.ClipboardManager; +import jscl.math.operator.Operator; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.CalculatorEventType; +import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.about.CalculatorFragmentType; +import org.solovyev.android.menu.AMenuItem; +import org.solovyev.android.menu.LabeledMenuItem; +import org.solovyev.common.text.StringUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * User: serso + * Date: 11/17/11 + * Time: 1:53 PM + */ + +public class CalculatorOperatorsFragment extends AbstractMathEntityListFragment { + + public CalculatorOperatorsFragment() { + super(CalculatorFragmentType.operators); + } + + @Override + protected AMenuItem getOnClickAction() { + return LongClickMenuItem.use; + } + + @NotNull + @Override + protected List> getMenuItemsOnLongClick(@NotNull Operator item) { + final List> result = new ArrayList>(Arrays.asList(LongClickMenuItem.values())); + + if ( StringUtils.isEmpty(OperatorDescriptionGetter.instance.getDescription(this.getActivity(), item.getName())) ) { + result.remove(LongClickMenuItem.copy_description); + } + + return result; + } + + @NotNull + @Override + protected MathEntityDescriptionGetter getDescriptionGetter() { + return OperatorDescriptionGetter.instance; + } + + + @NotNull + @Override + protected List getMathEntities() { + final List result = new ArrayList(); + + result.addAll(CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getEntities()); + result.addAll(CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getEntities()); + + return result; + } + + @Override + protected String getMathEntityCategory(@NotNull Operator operator) { + String result = CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getCategory(operator); + if (result == null) { + result = CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getCategory(operator); + } + + return result; + } + + private static enum OperatorDescriptionGetter implements MathEntityDescriptionGetter { + + instance; + + @Override + public String getDescription(@NotNull Context context, @NotNull String mathEntityName) { + String result = CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getDescription(mathEntityName); + if (StringUtils.isEmpty(result)) { + result = CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getDescription(mathEntityName); + } + + return result; + } + } + + /* + ********************************************************************** + * + * STATIC + * + ********************************************************************** + */ + + private static enum LongClickMenuItem implements LabeledMenuItem { + + use(R.string.c_use) { + @Override + public void onClick(@NotNull Operator data, @NotNull Context context) { + CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, data); + } + }, + + copy_description(R.string.c_copy_description) { + @Override + public void onClick(@NotNull Operator data, @NotNull Context context) { + final String text = OperatorDescriptionGetter.instance.getDescription(context, data.getName()); + if (!StringUtils.isEmpty(text)) { + final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); + clipboard.setText(text); + } + } + }; + private final int captionId; + + LongClickMenuItem(int captionId) { + this.captionId = captionId; + } + + @NotNull + @Override + public String getCaption(@NotNull Context context) { + return context.getString(captionId); + } + } + +} + diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragmentActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java similarity index 90% rename from calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragmentActivity.java rename to calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java index 6e48ab31..c14bc860 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragmentActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java @@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.about.CalculatorFragmentType; -import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity; +import org.solovyev.android.calculator.history.CalculatorHistoryActivity; import org.solovyev.android.calculator.model.VarCategory; /** @@ -21,10 +21,10 @@ import org.solovyev.android.calculator.model.VarCategory; * Date: 12/21/11 * Time: 11:05 PM */ -public class CalculatorVarsFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { +public class CalculatorVarsActivity extends SherlockFragmentActivity implements CalculatorEventListener { @NotNull - private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName()); + private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName()); @Override public void onCreate(@Nullable Bundle savedInstanceState) { diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java index 037eea8b..56e5989b 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsFragment.java @@ -16,8 +16,8 @@ import com.actionbarsherlock.view.MenuItem; import jscl.math.function.IConstant; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.solovyev.android.AndroidUtils2; import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.about.CalculatorFragmentType; import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.menu.AMenuItem; import org.solovyev.android.menu.LabeledMenuItem; @@ -38,9 +38,8 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment getOnClickAction() { return LongClickMenuItem.use; diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotActivity.java index e1b24875..078806b0 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotActivity.java @@ -2,11 +2,8 @@ package org.solovyev.android.calculator.plot; import android.content.Intent; import android.os.Bundle; -import com.actionbarsherlock.app.SherlockFragmentActivity; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.CalculatorActivityHelper; -import org.solovyev.android.calculator.CalculatorApplication; +import org.solovyev.android.calculator.CalculatorFragmentActivity; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.about.CalculatorFragmentType; @@ -15,17 +12,12 @@ import org.solovyev.android.calculator.about.CalculatorFragmentType; * Date: 9/30/12 * Time: 4:56 PM */ -public class CalculatorPlotActivity extends SherlockFragmentActivity { - - @NotNull - private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorPlotActivity.class.getSimpleName()); +public class CalculatorPlotActivity extends CalculatorFragmentActivity { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - activityHelper.onCreate(this, savedInstanceState); - final Intent intent = getIntent(); final Bundle arguments; @@ -35,37 +27,6 @@ public class CalculatorPlotActivity extends SherlockFragmentActivity { arguments = null; } - activityHelper.addTab(this, CalculatorFragmentType.plotter, arguments, R.id.main_layout); + getActivityHelper().addTab(this, CalculatorFragmentType.plotter, arguments, R.id.main_layout); } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - - activityHelper.onSaveInstanceState(this, outState); - } - - @Override - protected void onResume() { - super.onResume(); - - activityHelper.onResume(this); - } - - @Override - protected void onPause() { - this.activityHelper.onPause(this); - - super.onPause(); - } - - - @Override - protected void onDestroy() { - super.onDestroy(); - - activityHelper.onDestroy(this); - } - - }