function description
This commit is contained in:
@@ -47,9 +47,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
@NotNull
|
||||
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
|
||||
|
||||
@NotNull
|
||||
private final static Map<Class<?>, Map<String, Integer>> caches = new HashMap<Class<?>, Map<String, Integer>>(3);
|
||||
|
||||
@NotNull
|
||||
private CalculatorModel calculatorModel;
|
||||
|
||||
@@ -178,7 +175,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
|
||||
|
||||
private synchronized void setLayout(@NotNull SharedPreferences preferences) {
|
||||
final Map<String, Integer> layouts = getCache(R.layout.class);
|
||||
final Map<String, Integer> layouts = RClassUtils.getCache(R.layout.class);
|
||||
|
||||
layoutName = preferences.getString(getString(R.string.p_calc_layout_key), getString(R.string.p_calc_layout));
|
||||
|
||||
@@ -194,7 +191,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
}
|
||||
|
||||
private synchronized void setTheme(@NotNull SharedPreferences preferences) {
|
||||
final Map<String, Integer> styles = getCache(R.style.class);
|
||||
final Map<String, Integer> styles = RClassUtils.getCache(R.style.class);
|
||||
|
||||
themeName = preferences.getString(getString(R.string.p_calc_theme_key), getString(R.string.p_calc_theme));
|
||||
|
||||
@@ -209,30 +206,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
setTheme(styleId);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Map<String, Integer> getCache(@NotNull Class<?> clazz) {
|
||||
Map<String, Integer> result = caches.get(clazz);
|
||||
|
||||
if (result == null) {
|
||||
result = new HashMap<String, Integer>();
|
||||
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
int modifiers = field.getModifiers();
|
||||
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
|
||||
try {
|
||||
result.put(field.getName(), field.getInt(R.style.class));
|
||||
} catch (IllegalAccessException e) {
|
||||
Log.e(CalculatorActivity.class.getName(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
caches.put(clazz, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private synchronized void firstTimeInit(@NotNull SharedPreferences preferences) {
|
||||
if (!initialized) {
|
||||
dragButtonIds = new ArrayList<Integer>();
|
||||
|
@@ -86,7 +86,7 @@ public class CalculatorEditor extends EditText {
|
||||
Log.d(this.getClass().getName(), getText().toString());
|
||||
|
||||
int length = getText().length();
|
||||
setSelection(Math.max(Math.min(length - 1, selectionStart), 0), Math.max(Math.min(length - 1, selectionEnd), 0));
|
||||
setSelection(Math.max(Math.min(length, selectionStart), 0), Math.max(Math.min(length, selectionEnd), 0));
|
||||
}
|
||||
|
||||
public boolean isHighlightText() {
|
||||
|
@@ -43,8 +43,6 @@ public class CalculatorFunctionsActivity extends ListActivity{
|
||||
final ListView lv = getListView();
|
||||
lv.setTextFilterEnabled(true);
|
||||
|
||||
|
||||
|
||||
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
public void onItemClick(final AdapterView<?> parent,
|
||||
final View view,
|
||||
@@ -71,7 +69,8 @@ public class CalculatorFunctionsActivity extends ListActivity{
|
||||
|
||||
final Function function = getItem(position);
|
||||
|
||||
if (!StringUtils.isEmpty(function.getDescription())) {
|
||||
final String functionDescription = CalculatorEngine.instance.getFunctionsRegistry().getDescription(getContext(), function.getName());
|
||||
if (!StringUtils.isEmpty(functionDescription)) {
|
||||
TextView description = (TextView) result.findViewById(R.id.var_description);
|
||||
if (description == null) {
|
||||
final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
|
||||
@@ -80,7 +79,7 @@ public class CalculatorFunctionsActivity extends ListActivity{
|
||||
itemView.removeView(description);
|
||||
result.addView(description);
|
||||
}
|
||||
description.setText(function.getDescription());
|
||||
description.setText(functionDescription);
|
||||
} else {
|
||||
TextView description = (TextView) result.findViewById(R.id.var_description);
|
||||
if (description != null) {
|
||||
|
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 android.util.Log;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/30/11
|
||||
* Time: 1:11 AM
|
||||
*/
|
||||
public class RClassUtils {
|
||||
|
||||
@NotNull
|
||||
private final static Map<Class<?>, Map<String, Integer>> caches = new HashMap<Class<?>, Map<String, Integer>>(3);
|
||||
|
||||
// not intended for instantiation
|
||||
private RClassUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Map<String, Integer> getCache(@NotNull Class<?> clazz) {
|
||||
Map<String, Integer> result = caches.get(clazz);
|
||||
|
||||
if (result == null) {
|
||||
result = new HashMap<String, Integer>();
|
||||
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
int modifiers = field.getModifiers();
|
||||
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
|
||||
try {
|
||||
result.put(field.getName(), field.getInt(R.style.class));
|
||||
} catch (IllegalAccessException e) {
|
||||
Log.e(CalculatorActivity.class.getName(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
caches.put(clazz, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 android.content.Context;
|
||||
import jscl.math.function.Function;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/30/11
|
||||
* Time: 1:02 AM
|
||||
*/
|
||||
public interface AndroidFunctionsRegistry extends MathRegistry<Function> {
|
||||
|
||||
@Nullable
|
||||
String getDescription(@NotNull Context context, @NotNull String functionName);
|
||||
}
|
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* 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 android.content.Context;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.FunctionsRegistry;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.RClassUtils;
|
||||
import org.solovyev.common.definitions.IBuilder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/30/11
|
||||
* Time: 1:03 AM
|
||||
*/
|
||||
public class AndroidFunctionsRegistryImpl implements AndroidFunctionsRegistry {
|
||||
|
||||
@NotNull
|
||||
private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_";
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDescription(@NotNull Context context, @NotNull String functionName) {
|
||||
final String result;
|
||||
|
||||
final Map<String, Integer> stringsCache = RClassUtils.getCache(R.string.class);
|
||||
|
||||
final Integer stringId;
|
||||
if (!functionName.equals("√")) {
|
||||
stringId = stringsCache.get(FUNCTION_DESCRIPTION_PREFIX + functionName);
|
||||
} else {
|
||||
// todo serso: think
|
||||
stringId = stringsCache.get(FUNCTION_DESCRIPTION_PREFIX + "sqrt");
|
||||
}
|
||||
if (stringId != null) {
|
||||
result = context.getString(stringId);
|
||||
} else {
|
||||
result = null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Function> getEntities() {
|
||||
return FunctionsRegistry.getInstance().getEntities();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Function> getSystemEntities() {
|
||||
return FunctionsRegistry.getInstance().getSystemEntities();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function add(@Nullable String name, @NotNull IBuilder<Function> IBuilder) {
|
||||
return FunctionsRegistry.getInstance().add(name, IBuilder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(@NotNull Function var) {
|
||||
FunctionsRegistry.getInstance().remove(var);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> getNames() {
|
||||
return FunctionsRegistry.getInstance().getNames();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull String name) {
|
||||
return FunctionsRegistry.getInstance().contains(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function get(@NotNull String name) {
|
||||
return FunctionsRegistry.getInstance().get(name);
|
||||
}
|
||||
}
|
@@ -8,8 +8,6 @@ package org.solovyev.android.calculator.model;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import jscl.math.Expression;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.FunctionsRegistry;
|
||||
import jscl.text.ParseInterruptedException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -17,7 +15,6 @@ import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.android.msg.AndroidMessage;
|
||||
import org.solovyev.common.NumberMapper;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
import org.solovyev.common.msg.MessageRegistry;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
import org.solovyev.common.utils.CollectionsUtils;
|
||||
@@ -67,7 +64,7 @@ public enum CalculatorEngine {
|
||||
private final AndroidVarsRegistry varsRegister = new AndroidVarsRegistryImpl();
|
||||
|
||||
@NotNull
|
||||
private final MathRegistry<Function> functionsRegistry = FunctionsRegistry.getInstance();
|
||||
private final AndroidFunctionsRegistry functionsRegistry = new AndroidFunctionsRegistryImpl();
|
||||
|
||||
@NotNull
|
||||
private final static Set<String> tooLongExecutionCache = new HashSet<String>();
|
||||
@@ -296,7 +293,7 @@ public enum CalculatorEngine {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public MathRegistry<Function> getFunctionsRegistry() {
|
||||
public AndroidFunctionsRegistry getFunctionsRegistry() {
|
||||
return functionsRegistry;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user