grouping separator preference

This commit is contained in:
serso 2011-10-23 16:05:40 +04:00
parent 3e3f822a3e
commit 50f1c6804e
14 changed files with 155 additions and 39 deletions

View File

@ -85,4 +85,11 @@
<string name="c_calc_theme_summary">Устанавливает тему оформления приложения</string> <string name="c_calc_theme_summary">Устанавливает тему оформления приложения</string>
<string name="c_clear_history">Очистить историю</string> <string name="c_clear_history">Очистить историю</string>
<string name="c_simplify_instead_of_numeric">Следующие константы не определены: {0}!</string> <string name="c_simplify_instead_of_numeric">Следующие константы не определены: {0}!</string>
<string name="p_grouping_separator_no">Не использовать разделитель разрядов</string>
<string name="p_grouping_separator_apostrophe">Апостроф (\')</string>
<string name="p_grouping_separator_space">Пробел ( )</string>
<string name="c_calc_grouping_separator">Разделитель разрядов</string>
<string name="c_calc_grouping_separator_summary">Устанавливает разделитель разрядов числа</string>
</resources> </resources>

View File

@ -15,4 +15,16 @@
<item>violet_theme</item> <item>violet_theme</item>
<item>light_blue_theme</item> <item>light_blue_theme</item>
</string-array> </string-array>
<string-array name="p_grouping_separator_names">
<item>@string/p_grouping_separator_no</item>
<item>@string/p_grouping_separator_apostrophe</item>
<item>@string/p_grouping_separator_space</item>
</string-array>
<string-array name="p_grouping_separator_values">
<item>""</item>
<item>\'</item>
<item> </item>
</string-array>
</resources> </resources>

View File

@ -24,4 +24,7 @@
<string name="p_calc_theme_key">org.solovyev.android.calculator.CalculatorActivity_calc_theme</string> <string name="p_calc_theme_key">org.solovyev.android.calculator.CalculatorActivity_calc_theme</string>
<string name="p_calc_theme">default_theme</string> <string name="p_calc_theme">default_theme</string>
<string name="p_calc_grouping_separator_key">org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator</string>
<string name="p_calc_grouping_separator"> </string>
</resources> </resources>

View File

@ -85,4 +85,10 @@
<string name="c_calc_theme_summary">Sets the theme for calculator</string> <string name="c_calc_theme_summary">Sets the theme for calculator</string>
<string name="c_clear_history">Clear history</string> <string name="c_clear_history">Clear history</string>
<string name="c_simplify_instead_of_numeric">Next constants are undefined: {0}!</string> <string name="c_simplify_instead_of_numeric">Next constants are undefined: {0}!</string>
<string name="p_grouping_separator_no">No grouping separator</string>
<string name="p_grouping_separator_apostrophe">Apostrophe (\')</string>
<string name="p_grouping_separator_space">Space ( )</string>
<string name="c_calc_grouping_separator">Grouping separator</string>
<string name="c_calc_grouping_separator_summary">Sets grouping separator</string>
</resources> </resources>

View File

@ -17,13 +17,17 @@
a:title="@string/c_calc_color_display_title" a:title="@string/c_calc_color_display_title"
a:defaultValue="true"/> a:defaultValue="true"/>
<ListPreference a:key="@string/p_calc_grouping_separator_key"
a:title="@string/c_calc_grouping_separator"
a:entries="@array/p_grouping_separator_names"
a:summary="@string/c_calc_grouping_separator_summary"
a:entryValues="@array/p_grouping_separator_values"/>
<ListPreference a:key="@string/p_calc_theme_key" <ListPreference a:key="@string/p_calc_theme_key"
a:title="@string/c_calc_theme" a:title="@string/c_calc_theme"
a:entries="@array/p_theme_names" a:entries="@array/p_theme_names"
a:summary="@string/c_calc_theme_summary" a:summary="@string/c_calc_theme_summary"
a:entryValues="@array/p_theme_values" a:entryValues="@array/p_theme_values"/>
a:persistent="true"
a:shouldDisableView="false"/>
<!-- <org.solovyev.android.view.prefs.FloatRangeSeekBarPreference <!-- <org.solovyev.android.view.prefs.FloatRangeSeekBarPreference

View File

@ -25,7 +25,7 @@ public class CalculatorDisplay extends TextView {
private boolean valid = true; private boolean valid = true;
@NotNull @NotNull
private final static TextProcessor<String> textHighlighter = new TextHighlighter(Color.WHITE); private final static TextProcessor<TextHighlighter.Result> textHighlighter = new TextHighlighter(Color.WHITE);
public CalculatorDisplay(Context context) { public CalculatorDisplay(Context context) {
super(context); super(context);
@ -61,7 +61,8 @@ public class CalculatorDisplay extends TextView {
Log.d(this.getClass().getName(), text); Log.d(this.getClass().getName(), text);
try { try {
text = textHighlighter.process(text); TextHighlighter.Result result = textHighlighter.process(text);
text = result.toString();
} catch (ParseException e) { } catch (ParseException e) {
Log.e(this.getClass().getName(), e.getMessage(), e); Log.e(this.getClass().getName(), e.getMessage(), e);
} }

View File

@ -26,7 +26,7 @@ public class CalculatorEditor extends EditText {
private boolean highlightText = true; private boolean highlightText = true;
@NotNull @NotNull
private final static TextProcessor<String> textHighlighter = new TextHighlighter(Color.WHITE); private final static TextProcessor<TextHighlighter.Result> textHighlighter = new TextHighlighter(Color.WHITE);
public CalculatorEditor(Context context) { public CalculatorEditor(Context context) {
super(context); super(context);
@ -69,7 +69,10 @@ public class CalculatorEditor extends EditText {
Log.d(this.getClass().getName(), text); Log.d(this.getClass().getName(), text);
try { try {
text = textHighlighter.process(text); final TextHighlighter.Result result = textHighlighter.process(text);
selectionStart += result.getOffset();
selectionEnd += result.getOffset();
text = result.toString();
} catch (ParseException e) { } catch (ParseException e) {
Log.e(this.getClass().getName(), e.getMessage(), e); Log.e(this.getClass().getName(), e.getMessage(), e);
} }

View File

@ -11,13 +11,51 @@ import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.NumberBuilder; import org.solovyev.android.calculator.model.NumberBuilder;
import org.solovyev.android.calculator.model.ParseException; import org.solovyev.android.calculator.model.ParseException;
import org.solovyev.android.calculator.model.TextProcessor; import org.solovyev.android.calculator.model.TextProcessor;
import org.solovyev.common.utils.MutableObject;
/** /**
* User: serso * User: serso
* Date: 10/12/11 * Date: 10/12/11
* Time: 9:47 PM * Time: 9:47 PM
*/ */
public class TextHighlighter implements TextProcessor<String> { public class TextHighlighter implements TextProcessor<TextHighlighter.Result> {
public static class Result implements CharSequence {
@NotNull
private final String string;
private final int offset;
public Result(@NotNull String string, int offset) {
this.string = string;
this.offset = offset;
}
@Override
public int length() {
return string.length();
}
@Override
public char charAt(int i) {
return string.charAt(i);
}
@Override
public CharSequence subSequence(int i, int i1) {
return string.subSequence(i, i1);
}
@Override
public String toString() {
return string;
}
public int getOffset() {
return offset;
}
}
private final int color; private final int color;
private final int colorRed; private final int colorRed;
@ -36,7 +74,7 @@ public class TextHighlighter implements TextProcessor<String> {
@NotNull @NotNull
@Override @Override
public String process(@NotNull String text) throws ParseException { public Result process(@NotNull String text) throws ParseException {
final String result; final String result;
int maxNumberOfOpenGroupSymbols = 0; int maxNumberOfOpenGroupSymbols = 0;
@ -44,11 +82,15 @@ public class TextHighlighter implements TextProcessor<String> {
final StringBuilder text1 = new StringBuilder(); final StringBuilder text1 = new StringBuilder();
int numberOffset = 0;
final NumberBuilder numberBuilder = new NumberBuilder(); final NumberBuilder numberBuilder = new NumberBuilder();
for (int i = 0; i < text.length(); i++) { for (int i = 0; i < text.length(); i++) {
final MathType.Result mathType = MathType.getType(text, i); MathType.Result mathType = MathType.getType(text, i);
numberBuilder.process(text1, mathType); final MutableObject<Integer> localNumberOffset = new MutableObject<Integer>(0);
numberBuilder.process(text1, mathType, localNumberOffset);
numberOffset += localNumberOffset.getObject();
switch (mathType.getMathType()) { switch (mathType.getMathType()) {
case open_group_symbol: case open_group_symbol:
@ -71,7 +113,9 @@ public class TextHighlighter implements TextProcessor<String> {
} }
} }
numberBuilder.process(text1); final MutableObject<Integer> localNumberOffset = new MutableObject<Integer>(0);
numberBuilder.process(text1, localNumberOffset);
numberOffset += localNumberOffset.getObject();
if (maxNumberOfOpenGroupSymbols > 0) { if (maxNumberOfOpenGroupSymbols > 0) {
@ -90,7 +134,7 @@ public class TextHighlighter implements TextProcessor<String> {
result = text1.toString(); result = text1.toString();
} }
return result; return new Result(result, numberOffset);
} }
private int processHighlightedText(@NotNull StringBuilder result, int i, @NotNull String functionName, @NotNull String tag) { private int processHighlightedText(@NotNull StringBuilder result, int i, @NotNull String functionName, @NotNull String tag) {

View File

@ -198,13 +198,21 @@ public enum MathType {
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) { public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
final String substitute = getSubstituteToJscl(match); final String substitute = getSubstituteToJscl(match);
result.append(substitute == null ? match : substitute); result.append(substitute == null ? match : substitute);
if (match.length() > 1) {
return i + match.length() - 1; return i + match.length() - 1;
} else {
return i;
}
} }
public int processFromJscl(@NotNull StringBuilder result, int i, @NotNull String match) { public int processFromJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
final String substitute = getSubstituteFromJscl(match); final String substitute = getSubstituteFromJscl(match);
result.append(substitute == null ? match : substitute); result.append(substitute == null ? match : substitute);
if (match.length() > 1) {
return i + match.length() - 1; return i + match.length() - 1;
} else {
return i;
}
} }
@Nullable @Nullable

View File

@ -20,6 +20,7 @@ import org.solovyev.common.msg.MessageType;
import org.solovyev.common.utils.CollectionsUtils; import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Formatter; import org.solovyev.common.utils.Formatter;
import org.solovyev.common.utils.MutableObject; import org.solovyev.common.utils.MutableObject;
import org.solovyev.common.utils.StringUtils;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DecimalFormat; import java.text.DecimalFormat;
@ -40,6 +41,9 @@ public enum CalculatorEngine {
instance; instance;
private static final String GROUPING_SEPARATOR_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator";
private static final String GROUPING_SEPARATOR_DEFAULT = " ";
private static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision"; private static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision";
private static final String RESULT_PRECISION_DEFAULT = "5"; private static final String RESULT_PRECISION_DEFAULT = "5";
@ -62,6 +66,12 @@ public enum CalculatorEngine {
@NotNull @NotNull
private DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); private DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault());
{
decimalGroupSymbols.setDecimalSeparator('.');
decimalGroupSymbols.setGroupingSeparator(GROUPING_SEPARATOR_DEFAULT.charAt(0));
}
private boolean useGroupingSeparator = true;
@NotNull @NotNull
private ThreadKiller threadKiller = new AndroidThreadKiller(); private ThreadKiller threadKiller = new AndroidThreadKiller();
@ -71,7 +81,7 @@ public enum CalculatorEngine {
if (!value.isInfinite() && !value.isNaN()) { if (!value.isInfinite() && !value.isNaN()) {
final DecimalFormat df = new DecimalFormat(); final DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(decimalGroupSymbols); df.setDecimalFormatSymbols(decimalGroupSymbols);
df.setGroupingUsed(true); df.setGroupingUsed(useGroupingSeparator);
df.setMaximumFractionDigits(instance.getPrecision()); df.setMaximumFractionDigits(instance.getPrecision());
return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue()); return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue());
} else { } else {
@ -79,11 +89,6 @@ public enum CalculatorEngine {
} }
} }
{
decimalGroupSymbols.setDecimalSeparator('.');
decimalGroupSymbols.setGroupingSeparator(' ');
}
public String evaluate(@NotNull JsclOperation operation, public String evaluate(@NotNull JsclOperation operation,
@NotNull String expression) throws EvalError, ParseException { @NotNull String expression) throws EvalError, ParseException {
return evaluate(operation, expression, null); return evaluate(operation, expression, null);
@ -201,6 +206,14 @@ public enum CalculatorEngine {
final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class); final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class);
//noinspection ConstantConditions //noinspection ConstantConditions
this.setPrecision(integerNumberMapper.parseValue(preferences.getString(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT))); this.setPrecision(integerNumberMapper.parseValue(preferences.getString(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT)));
final String groupingSeparator = preferences.getString(GROUPING_SEPARATOR_P_KEY, GROUPING_SEPARATOR_DEFAULT);
if (StringUtils.isEmpty(groupingSeparator)) {
this.useGroupingSeparator = false;
} else {
this.useGroupingSeparator = true;
this.decimalGroupSymbols.setGroupingSeparator(groupingSeparator.charAt(0));
}
} }
varsRegister.init(context, preferences); varsRegister.init(context, preferences);
@ -218,7 +231,8 @@ public enum CalculatorEngine {
} }
} }
public void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols) { //for tests only
void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols) {
synchronized (lock) { synchronized (lock) {
this.decimalGroupSymbols = decimalGroupSymbols; this.decimalGroupSymbols = decimalGroupSymbols;
} }

View File

@ -14,7 +14,6 @@ import java.util.List;
*/ */
public class FromJsclSimplifyTextProcessor implements TextProcessor<String> { public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
@NotNull @NotNull
@Override @Override
public String process(@NotNull String s) throws ParseException { public String process(@NotNull String s) throws ParseException {
@ -27,14 +26,14 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
continue; continue;
} }
final MathType.Result mathTypeResult = MathType.getType(s, i); MathType.Result mathTypeResult = MathType.getType(s, i);
numberBuilder.process(sb, mathTypeResult); numberBuilder.process(sb, mathTypeResult, null);
i = mathTypeResult.processFromJscl(sb, i); i = mathTypeResult.processFromJscl(sb, i);
} }
numberBuilder.process(sb); numberBuilder.process(sb, null);
return removeMultiplicationSigns(sb.toString()); return removeMultiplicationSigns(sb.toString());
} }

View File

@ -11,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.utils.CollectionsUtils; import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Finder; import org.solovyev.common.utils.Finder;
import org.solovyev.common.utils.MutableObject;
/** /**
* User: serso * User: serso
@ -24,21 +25,28 @@ public class NumberBuilder {
@Nullable @Nullable
private String number = null; private String number = null;
public void process(@NotNull StringBuilder sb, @NotNull MathType.Result mathTypeResult) { @NotNull
public MathType.Result process(@NotNull StringBuilder sb, @NotNull MathType.Result mathTypeResult, @Nullable MutableObject<Integer> numberOffset) {
number = null; number = null;
final MathType.Result possibleResult;
if (mathTypeResult.getMathType() == MathType.digit || mathTypeResult.getMathType() == MathType.dot || mathTypeResult.getMathType() == MathType.power_10) { if (mathTypeResult.getMathType() == MathType.digit || mathTypeResult.getMathType() == MathType.dot || mathTypeResult.getMathType() == MathType.power_10) {
if (numberBuilder == null) { if (numberBuilder == null) {
numberBuilder = new StringBuilder(); numberBuilder = new StringBuilder();
} }
numberBuilder.append(mathTypeResult.getMatch()); numberBuilder.append(mathTypeResult.getMatch());
replaceSystemVars(sb, number); possibleResult = replaceSystemVars(sb, number, numberOffset);
} else { } else {
process(sb); possibleResult = process(sb, numberOffset);
}
} }
public void process(@NotNull StringBuilder sb) { return possibleResult == null ? mathTypeResult : possibleResult;
}
@Nullable
public MathType.Result process(@NotNull StringBuilder sb, @Nullable MutableObject<Integer> numberOffset) {
if (numberBuilder != null) { if (numberBuilder != null) {
try { try {
number = numberBuilder.toString(); number = numberBuilder.toString();
@ -52,12 +60,12 @@ public class NumberBuilder {
number = null; number = null;
} }
replaceSystemVars(sb, number); return replaceSystemVars(sb, number, numberOffset);
} }
@Nullable @Nullable
private MathType replaceSystemVars(StringBuilder sb, String number) { private MathType.Result replaceSystemVars(StringBuilder sb, String number, @Nullable MutableObject<Integer> numberOffset) {
MathType result = null; MathType.Result result = null;
if (number != null) { if (number != null) {
final String finalNumber = number; final String finalNumber = number;
@ -71,10 +79,14 @@ public class NumberBuilder {
if (var != null) { if (var != null) {
sb.delete(sb.length() - number.length(), sb.length()); sb.delete(sb.length() - number.length(), sb.length());
sb.append(var.getName()); sb.append(var.getName());
result = MathType.constant; result = new MathType.Result(MathType.constant, var.getName());
} else { } else {
sb.delete(sb.length() - number.length(), sb.length()); sb.delete(sb.length() - number.length(), sb.length());
sb.append(CalculatorEngine.instance.format(Double.valueOf(number))); final String formattedNumber = CalculatorEngine.instance.format(Double.valueOf(number));
if ( numberOffset != null ) {
numberOffset.setObject(formattedNumber.length() - number.length());
}
sb.append(formattedNumber);
} }
} }

View File

@ -38,9 +38,9 @@ public class TextHighlighterTest {
} }
} }
Assert.assertEquals("<font color=\"#000000\"></font>)(((())())", textHighlighter.process(")(((())())")); Assert.assertEquals("<font color=\"#000000\"></font>)(((())())", textHighlighter.process(")(((())())").toString());
Assert.assertEquals(")", textHighlighter.process(")")); Assert.assertEquals(")", textHighlighter.process(")").toString());
Assert.assertEquals(")()(", textHighlighter.process(")()(")); Assert.assertEquals(")()(", textHighlighter.process(")()(").toString());
Assert.assertEquals("1 000 000", textHighlighter.process("1000000")); Assert.assertEquals("1 000 000", textHighlighter.process("1000000").toString());
} }
} }

View File

@ -22,6 +22,8 @@ public class FromJsclSimplifyTextProcessorTest {
public void testProcess() throws Exception { public void testProcess() throws Exception {
FromJsclSimplifyTextProcessor tp = new FromJsclSimplifyTextProcessor(); FromJsclSimplifyTextProcessor tp = new FromJsclSimplifyTextProcessor();
Assert.assertEquals("(e)", tp.process("(2.718281828459045)")); Assert.assertEquals("(e)", tp.process("(2.718281828459045)"));
Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045"));
Assert.assertEquals("((e)(e))", tp.process("((2.718281828459045)*(2.718281828459045))"));
DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(); DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols();
decimalGroupSymbols.setGroupingSeparator(' '); decimalGroupSymbols.setGroupingSeparator(' ');
CalculatorEngine.instance.setDecimalGroupSymbols(decimalGroupSymbols); CalculatorEngine.instance.setDecimalGroupSymbols(decimalGroupSymbols);
@ -33,6 +35,7 @@ public class FromJsclSimplifyTextProcessorTest {
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("t2.718281828459045", "2")); CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("t2.718281828459045", "2"));
Assert.assertEquals("t2.718281828459045e", tp.process("t2.718281828459045*2.718281828459045")); Assert.assertEquals("t2.718281828459045e", tp.process("t2.718281828459045*2.718281828459045"));
Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045"));
Assert.assertEquals("t×", tp.process("t*")); Assert.assertEquals("t×", tp.process("t*"));
Assert.assertEquals("×t", tp.process("*t")); Assert.assertEquals("×t", tp.process("*t"));
Assert.assertEquals("t×2", tp.process("t*2")); Assert.assertEquals("t×2", tp.process("t*2"));