registries

This commit is contained in:
Sergey Solovyev 2012-10-07 19:54:54 +04:00
parent 78a1cc4942
commit 4134204e67
10 changed files with 538 additions and 482 deletions

View File

@ -1,232 +1,236 @@
package org.solovyev.android.calculator;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.calculator.view.NumeralBasesButton;
import org.solovyev.android.calculator.view.OnDragListenerVibrator;
import org.solovyev.android.history.HistoryDragProcessor;
import org.solovyev.android.view.drag.*;
import org.solovyev.common.Announcer;
import org.solovyev.common.math.Point2d;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 9/28/12
* Time: 12:12 AM
*/
public abstract class AbstractCalculatorHelper implements SharedPreferences.OnSharedPreferenceChangeListener {
@NotNull
private CalculatorPreferences.Gui.Layout layout;
@NotNull
private CalculatorPreferences.Gui.Theme theme;
@Nullable
private Vibrator vibrator;
@NotNull
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
@NotNull
private String logTag = "CalculatorActivity";
protected AbstractCalculatorHelper() {
}
protected AbstractCalculatorHelper(@NotNull String logTag) {
this.logTag = logTag;
}
protected void onCreate(@NotNull Activity activity) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
vibrator = (Vibrator) activity.getSystemService(Activity.VIBRATOR_SERVICE);
layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences);
preferences.registerOnSharedPreferenceChangeListener(this);
}
public void logDebug(@NotNull String message) {
Log.d(logTag, message);
}
public void processButtons(@NotNull final Activity activity, @NotNull View root) {
dpclRegister.clear();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, activity);
setOnDragListeners(root, dragPreferences, preferences);
final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(getCalculator()), dragPreferences), vibrator, preferences);
final DragButton historyButton = getButton(root, R.id.historyButton);
if (historyButton != null) {
historyButton.setOnDragListener(historyOnDragListener);
}
final DragButton subtractionButton = getButton(root, R.id.subtractionButton);
if (subtractionButton != null) {
subtractionButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() {
@Override
public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
if (dragDirection == DragDirection.down) {
CalculatorActivity.operatorsButtonClickHandler(activity);
return true;
}
return false;
}
}, dragPreferences), vibrator, preferences));
}
final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences);
final DragButton rightButton = getButton(root, R.id.rightButton);
if (rightButton != null) {
rightButton.setOnDragListener(toPositionOnDragListener);
}
final DragButton leftButton = getButton(root, R.id.leftButton);
if (leftButton != null) {
leftButton.setOnDragListener(toPositionOnDragListener);
}
final DragButton equalsButton = getButton(root, R.id.equalsButton);
if (equalsButton != null) {
equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences));
}
final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) getButton(root, R.id.sixDigitButton);
if (angleUnitsButton != null) {
angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.AngleUnitsChanger(activity), dragPreferences), vibrator, preferences));
}
final NumeralBasesButton clearButton = (NumeralBasesButton) getButton(root, R.id.clearButton);
if (clearButton != null) {
clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.NumeralBasesChanger(activity), dragPreferences), vibrator, preferences));
}
final DragButton varsButton = getButton(root, R.id.varsButton);
if (varsButton != null) {
varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.VarsDragProcessor(activity), dragPreferences), vibrator, preferences));
}
final DragButton roundBracketsButton = getButton(root, R.id.roundBracketsButton);
if (roundBracketsButton != null) {
roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences));
}
if (layout == CalculatorPreferences.Gui.Layout.simple) {
toggleButtonDirectionText(root, R.id.oneDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.twoDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.threeDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.sixDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.sevenDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.eightDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.clearButton, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.fourDigitButton, false, DragDirection.down);
toggleButtonDirectionText(root, R.id.fiveDigitButton, false, DragDirection.down);
toggleButtonDirectionText(root, R.id.nineDigitButton, false, DragDirection.left);
toggleButtonDirectionText(root, R.id.multiplicationButton, false, DragDirection.left);
toggleButtonDirectionText(root, R.id.plusButton, false, DragDirection.down, DragDirection.up);
}
CalculatorButtons.processButtons(true, theme, root);
CalculatorButtons.toggleEqualsButton(preferences, activity);
}
private void toggleButtonDirectionText(@NotNull View root, int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) {
final View v = getButton(root, id);
if (v instanceof DirectionDragButton ) {
final DirectionDragButton button = (DirectionDragButton)v;
for (DragDirection dragDirection : dragDirections) {
button.showDirectionText(showDirectionText, dragDirection);
}
}
}
@NotNull
private Calculator getCalculator() {
return CalculatorLocatorImpl.getInstance().getCalculator();
}
private void setOnDragListeners(@NotNull View root, @NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull SharedPreferences preferences) {
final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new DigitButtonDragProcessor(getKeyboard()), dragPreferences), vibrator, preferences);
final List<Integer> dragButtonIds = new ArrayList<Integer>();
for (Field field : R.id.class.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
try {
int viewId = field.getInt(R.id.class);
final View view = root.findViewById(viewId);
if (view instanceof DragButton) {
dragButtonIds.add(viewId);
}
} catch (IllegalAccessException e) {
Log.e(R.id.class.getName(), e.getMessage());
}
}
}
for (Integer dragButtonId : dragButtonIds) {
final DragButton button = getButton(root, dragButtonId);
if (button != null) {
button.setOnDragListener(onDragListener);
}
}
}
@NotNull
private CalculatorKeyboard getKeyboard() {
return CalculatorLocatorImpl.getInstance().getKeyboard();
}
@Nullable
private <T extends DragButton> T getButton(@NotNull View root, int buttonId) {
return (T) root.findViewById(buttonId);
}
@NotNull
private SimpleOnDragListener newOnDragListener(@NotNull SimpleOnDragListener.DragProcessor dragProcessor,
@NotNull SimpleOnDragListener.Preferences dragPreferences) {
final SimpleOnDragListener onDragListener = new SimpleOnDragListener(dragProcessor, dragPreferences);
dpclRegister.addListener(onDragListener);
return onDragListener;
}
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (key != null && key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, CalculatorApplication.getInstance()));
}
}
public void onDestroy(@NotNull Activity activity) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
preferences.unregisterOnSharedPreferenceChangeListener(this);
}
}
package org.solovyev.android.calculator;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.calculator.view.NumeralBasesButton;
import org.solovyev.android.calculator.view.OnDragListenerVibrator;
import org.solovyev.android.history.HistoryDragProcessor;
import org.solovyev.android.view.drag.*;
import org.solovyev.common.Announcer;
import org.solovyev.common.math.Point2d;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 9/28/12
* Time: 12:12 AM
*/
public abstract class AbstractCalculatorHelper implements SharedPreferences.OnSharedPreferenceChangeListener {
@NotNull
private CalculatorPreferences.Gui.Layout layout;
@NotNull
private CalculatorPreferences.Gui.Theme theme;
@Nullable
private Vibrator vibrator;
@NotNull
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
@NotNull
private String logTag = "CalculatorActivity";
protected AbstractCalculatorHelper() {
}
protected AbstractCalculatorHelper(@NotNull String logTag) {
this.logTag = logTag;
}
protected void onCreate(@NotNull Activity activity) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
vibrator = (Vibrator) activity.getSystemService(Activity.VIBRATOR_SERVICE);
layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences);
preferences.registerOnSharedPreferenceChangeListener(this);
}
public void logDebug(@NotNull String message) {
Log.d(logTag, message);
}
public void logError(@NotNull String message) {
Log.e(logTag, message);
}
public void processButtons(@NotNull final Activity activity, @NotNull View root) {
dpclRegister.clear();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, activity);
setOnDragListeners(root, dragPreferences, preferences);
final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(getCalculator()), dragPreferences), vibrator, preferences);
final DragButton historyButton = getButton(root, R.id.historyButton);
if (historyButton != null) {
historyButton.setOnDragListener(historyOnDragListener);
}
final DragButton subtractionButton = getButton(root, R.id.subtractionButton);
if (subtractionButton != null) {
subtractionButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() {
@Override
public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
if (dragDirection == DragDirection.down) {
CalculatorActivity.operatorsButtonClickHandler(activity);
return true;
}
return false;
}
}, dragPreferences), vibrator, preferences));
}
final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences);
final DragButton rightButton = getButton(root, R.id.rightButton);
if (rightButton != null) {
rightButton.setOnDragListener(toPositionOnDragListener);
}
final DragButton leftButton = getButton(root, R.id.leftButton);
if (leftButton != null) {
leftButton.setOnDragListener(toPositionOnDragListener);
}
final DragButton equalsButton = getButton(root, R.id.equalsButton);
if (equalsButton != null) {
equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences));
}
final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) getButton(root, R.id.sixDigitButton);
if (angleUnitsButton != null) {
angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.AngleUnitsChanger(activity), dragPreferences), vibrator, preferences));
}
final NumeralBasesButton clearButton = (NumeralBasesButton) getButton(root, R.id.clearButton);
if (clearButton != null) {
clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.NumeralBasesChanger(activity), dragPreferences), vibrator, preferences));
}
final DragButton varsButton = getButton(root, R.id.varsButton);
if (varsButton != null) {
varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.VarsDragProcessor(activity), dragPreferences), vibrator, preferences));
}
final DragButton roundBracketsButton = getButton(root, R.id.roundBracketsButton);
if (roundBracketsButton != null) {
roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences));
}
if (layout == CalculatorPreferences.Gui.Layout.simple) {
toggleButtonDirectionText(root, R.id.oneDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.twoDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.threeDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.sixDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.sevenDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.eightDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.clearButton, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.fourDigitButton, false, DragDirection.down);
toggleButtonDirectionText(root, R.id.fiveDigitButton, false, DragDirection.down);
toggleButtonDirectionText(root, R.id.nineDigitButton, false, DragDirection.left);
toggleButtonDirectionText(root, R.id.multiplicationButton, false, DragDirection.left);
toggleButtonDirectionText(root, R.id.plusButton, false, DragDirection.down, DragDirection.up);
}
CalculatorButtons.processButtons(true, theme, root);
CalculatorButtons.toggleEqualsButton(preferences, activity);
}
private void toggleButtonDirectionText(@NotNull View root, int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) {
final View v = getButton(root, id);
if (v instanceof DirectionDragButton ) {
final DirectionDragButton button = (DirectionDragButton)v;
for (DragDirection dragDirection : dragDirections) {
button.showDirectionText(showDirectionText, dragDirection);
}
}
}
@NotNull
private Calculator getCalculator() {
return CalculatorLocatorImpl.getInstance().getCalculator();
}
private void setOnDragListeners(@NotNull View root, @NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull SharedPreferences preferences) {
final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new DigitButtonDragProcessor(getKeyboard()), dragPreferences), vibrator, preferences);
final List<Integer> dragButtonIds = new ArrayList<Integer>();
for (Field field : R.id.class.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
try {
int viewId = field.getInt(R.id.class);
final View view = root.findViewById(viewId);
if (view instanceof DragButton) {
dragButtonIds.add(viewId);
}
} catch (IllegalAccessException e) {
Log.e(R.id.class.getName(), e.getMessage());
}
}
}
for (Integer dragButtonId : dragButtonIds) {
final DragButton button = getButton(root, dragButtonId);
if (button != null) {
button.setOnDragListener(onDragListener);
}
}
}
@NotNull
private CalculatorKeyboard getKeyboard() {
return CalculatorLocatorImpl.getInstance().getKeyboard();
}
@Nullable
private <T extends DragButton> T getButton(@NotNull View root, int buttonId) {
return (T) root.findViewById(buttonId);
}
@NotNull
private SimpleOnDragListener newOnDragListener(@NotNull SimpleOnDragListener.DragProcessor dragProcessor,
@NotNull SimpleOnDragListener.Preferences dragPreferences) {
final SimpleOnDragListener onDragListener = new SimpleOnDragListener(dragProcessor, dragPreferences);
dpclRegister.addListener(onDragListener);
return onDragListener;
}
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (key != null && key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, CalculatorApplication.getInstance()));
}
}
public void onDestroy(@NotNull Activity activity) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
preferences.unregisterOnSharedPreferenceChangeListener(this);
}
}

View File

@ -27,7 +27,7 @@ public enum AndroidFunctionCategory {
}
@Nullable
public AndroidFunctionCategory valueOf( @NotNull FunctionCategory functionCategory ) {
public static AndroidFunctionCategory valueOf( @NotNull FunctionCategory functionCategory ) {
for (AndroidFunctionCategory androidFunctionCategory : values()) {
if ( androidFunctionCategory.name().equals(functionCategory.name()) ) {
return androidFunctionCategory;

View File

@ -0,0 +1,38 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 10/7/12
* Time: 7:41 PM
*/
public enum AndroidOperatorCategory {
derivatives(R.string.derivatives),
other(R.string.other),
my(R.string.c_fun_category_my),
common(R.string.c_fun_category_common);
private final int captionId;
AndroidOperatorCategory(int captionId) {
this.captionId = captionId;
}
public int getCaptionId() {
return captionId;
}
@Nullable
public static AndroidOperatorCategory valueOf(@NotNull OperatorCategory operatorCategory) {
for (AndroidOperatorCategory androidOperatorCategory : values()) {
if ( androidOperatorCategory.name().equals(operatorCategory.name()) ) {
return androidOperatorCategory;
}
}
return null;
}
}

View File

@ -0,0 +1,93 @@
/*
* 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;
import jscl.math.operator.*;
import org.jetbrains.annotations.NotNull;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
import java.util.*;
/**
* User: serso
* Date: 11/17/11
* Time: 11:29 PM
*/
public class AndroidOperatorsMathRegistry extends AbstractCalculatorMathRegistry<Operator, MathPersistenceEntity> {
@NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>();
static {
substitutes.put("Σ", "sum");
substitutes.put("", "product");
substitutes.put("", "derivative");
substitutes.put("∫ab", "integral_ab");
substitutes.put("", "integral");
substitutes.put("Σ", "sum");
}
@NotNull
private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_";
public AndroidOperatorsMathRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, mathEntityDao);
}
@NotNull
@Override
protected Map<String, String> getSubstitutes() {
return substitutes;
}
@Override
public String getCategory(@NotNull Operator operator) {
for (OperatorCategory category : OperatorCategory.values()) {
if ( category.isInCategory(operator) ) {
return category.name();
}
}
return null;
}
@Override
public void load() {
// not supported yet
}
@NotNull
@Override
protected JBuilder<? extends Operator> createBuilder(@NotNull MathPersistenceEntity entity) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void save() {
// not supported yet
}
@Override
protected MathPersistenceEntity transform(@NotNull Operator entity) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@NotNull
@Override
protected MathEntityPersistenceContainer<MathPersistenceEntity> createPersistenceContainer() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
}

View File

@ -58,4 +58,5 @@ public interface CalculatorActivityHelper {
void processButtons(@NotNull Activity activity, @NotNull View root);
void logError(@NotNull String message);
}

View File

@ -0,0 +1,78 @@
package org.solovyev.android.calculator;
import jscl.math.operator.*;
import org.jetbrains.annotations.NotNull;
import org.solovyev.common.collections.CollectionsUtils;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* User: serso
* Date: 10/7/12
* Time: 7:40 PM
*/
public enum OperatorCategory {
derivatives(100){
@Override
public boolean isInCategory(@NotNull Operator operator) {
return operator instanceof Derivative || operator instanceof Integral || operator instanceof IndefiniteIntegral;
}
},
other(200) {
@Override
public boolean isInCategory(@NotNull Operator operator) {
return operator instanceof Sum || operator instanceof Product;
}
},
my(0) {
@Override
public boolean isInCategory(@NotNull Operator operator) {
return !operator.isSystem();
}
},
common(50) {
@Override
public boolean isInCategory(@NotNull Operator operator) {
for (OperatorCategory category : values()) {
if ( category != this ) {
if ( category.isInCategory(operator) ) {
return false;
}
}
}
return true;
}
};
private final int tabOrder;
OperatorCategory(int tabOrder) {
this.tabOrder = tabOrder;
}
public abstract boolean isInCategory(@NotNull Operator operator);
@NotNull
public static List<OperatorCategory> getCategoriesByTabOrder() {
final List<OperatorCategory> result = CollectionsUtils.asList(OperatorCategory.values());
Collections.sort(result, new Comparator<OperatorCategory>() {
@Override
public int compare(OperatorCategory category, OperatorCategory category1) {
return category.tabOrder - category1.tabOrder;
}
});
// todo serso: current solution (as creating operators is not implemented yet)
result.remove(my);
return result;
}
}

View File

@ -7,6 +7,7 @@
package org.solovyev.android.calculator.math.edit;
import android.os.Bundle;
import android.util.Log;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -33,7 +34,12 @@ public class CalculatorFunctionsActivity extends SherlockFragmentActivity implem
final CalculatorFragmentType fragmentType = CalculatorFragmentType.functions;
for (FunctionCategory category : FunctionCategory.getCategoriesByTabOrder()) {
activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), category.getCaptionId(), R.id.main_layout);
final AndroidFunctionCategory androidCategory = AndroidFunctionCategory.valueOf(category);
if (androidCategory != null) {
activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), androidCategory.getCaptionId(), R.id.main_layout);
} else {
Log.e(CalculatorFunctionsActivity.class.getSimpleName(), "Unable to find android function category for " + category);
}
}
}

View File

@ -1,78 +1,82 @@
/*
* 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.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
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.CalculatorHistoryActivity;
import org.solovyev.android.calculator.model.AndroidOperatorsMathRegistry;
/**
* User: serso
* Date: 12/21/11
* Time: 10:33 PM
*/
public class CalculatorOperatorsActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@NotNull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName());
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState);
final CalculatorFragmentType fragmentType = CalculatorFragmentType.operators;
for (AndroidOperatorsMathRegistry.Category category : AndroidOperatorsMathRegistry.Category.getCategoriesByTabOrder()) {
activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), category.getCaptionId(), 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();
this.activityHelper.onDestroy(this);
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
switch (calculatorEventType) {
case use_operator:
this.finish();
break;
}
}
}
/*
* 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.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
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.CalculatorHistoryActivity;
/**
* User: serso
* Date: 12/21/11
* Time: 10:33 PM
*/
public class CalculatorOperatorsActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@NotNull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName());
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState);
final CalculatorFragmentType fragmentType = CalculatorFragmentType.operators;
for (OperatorCategory category : OperatorCategory.getCategoriesByTabOrder()) {
final AndroidOperatorCategory androidCategory = AndroidOperatorCategory.valueOf(category);
if (androidCategory != null) {
activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), androidCategory.getCaptionId(), R.id.main_layout);
} else {
activityHelper.logError("Unable to find android operator category for " + category);
}
}
}
@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();
this.activityHelper.onDestroy(this);
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
switch (calculatorEventType) {
case use_operator:
this.finish();
break;
}
}
}

View File

@ -1,165 +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.model;
import jscl.math.operator.*;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.*;
import org.solovyev.common.JBuilder;
import org.solovyev.common.collections.CollectionsUtils;
import org.solovyev.common.math.MathRegistry;
import java.util.*;
/**
* User: serso
* Date: 11/17/11
* Time: 11:29 PM
*/
public class AndroidOperatorsMathRegistry extends AbstractCalculatorMathRegistry<Operator, MathPersistenceEntity> {
@NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>();
static {
substitutes.put("Σ", "sum");
substitutes.put("", "product");
substitutes.put("", "derivative");
substitutes.put("∫ab", "integral_ab");
substitutes.put("", "integral");
substitutes.put("Σ", "sum");
}
@NotNull
private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_";
protected AndroidOperatorsMathRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, mathEntityDao);
}
@NotNull
@Override
protected Map<String, String> getSubstitutes() {
return substitutes;
}
@Override
public String getCategory(@NotNull Operator operator) {
for (Category category : Category.values()) {
if ( category.isInCategory(operator) ) {
return category.name();
}
}
return null;
}
@Override
public void load() {
// not supported yet
}
@NotNull
@Override
protected JBuilder<? extends Operator> createBuilder(@NotNull MathPersistenceEntity entity) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void save() {
// not supported yet
}
@Override
protected MathPersistenceEntity transform(@NotNull Operator entity) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@NotNull
@Override
protected MathEntityPersistenceContainer<MathPersistenceEntity> createPersistenceContainer() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
public static enum Category {
derivatives(R.string.derivatives, 100){
@Override
boolean isInCategory(@NotNull Operator operator) {
return operator instanceof Derivative || operator instanceof Integral || operator instanceof IndefiniteIntegral;
}
},
other(R.string.other, 200) {
@Override
boolean isInCategory(@NotNull Operator operator) {
return operator instanceof Sum || operator instanceof Product;
}
},
my(R.string.c_fun_category_my, 0) {
@Override
boolean isInCategory(@NotNull Operator operator) {
return !operator.isSystem();
}
},
common(R.string.c_fun_category_common, 50) {
@Override
boolean isInCategory(@NotNull Operator operator) {
for (Category category : values()) {
if ( category != this ) {
if ( category.isInCategory(operator) ) {
return false;
}
}
}
return true;
}
};
private final int captionId;
private final int tabOrder;
Category(int captionId, int tabOrder) {
this.captionId = captionId;
this.tabOrder = tabOrder;
}
public int getCaptionId() {
return captionId;
}
abstract boolean isInCategory(@NotNull Operator operator);
@NotNull
public static List<Category> getCategoriesByTabOrder() {
final List<Category> result = CollectionsUtils.asList(Category.values());
Collections.sort(result, new Comparator<Category>() {
@Override
public int compare(Category category, Category category1) {
return category.tabOrder - category1.tabOrder;
}
});
// todo serso: current solution (as creating operators is not implemented yet)
result.remove(my);
return result;
}
}
}

View File

@ -8,10 +8,7 @@ package org.solovyev.android.calculator.model;
import jscl.math.operator.Operator;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.AbstractCalculatorMathRegistry;
import org.solovyev.android.calculator.MathEntityDao;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.android.calculator.*;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
@ -51,7 +48,7 @@ public class AndroidPostfixFunctionsRegistry extends AbstractCalculatorMathRegis
@Override
public String getCategory(@NotNull Operator operator) {
for (AndroidOperatorsMathRegistry.Category category : AndroidOperatorsMathRegistry.Category.values()) {
for (OperatorCategory category : OperatorCategory.values()) {
if ( category.isInCategory(operator) ) {
return category.name();
}