Use engine in TextHighlighter
This commit is contained in:
parent
c324c1408e
commit
b40b82574e
@ -23,20 +23,13 @@
|
|||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import android.text.SpannableStringBuilder;
|
import android.text.SpannableStringBuilder;
|
||||||
|
import jscl.NumeralBase;
|
||||||
import org.solovyev.android.calculator.math.MathType;
|
import org.solovyev.android.calculator.math.MathType;
|
||||||
import org.solovyev.common.text.Strings;
|
import org.solovyev.common.text.Strings;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import jscl.NumeralBase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User: serso
|
|
||||||
* Date: 12/15/11
|
|
||||||
* Time: 9:01 PM
|
|
||||||
*/
|
|
||||||
public abstract class BaseNumberBuilder {
|
public abstract class BaseNumberBuilder {
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
|
@ -52,6 +52,8 @@ public class Display {
|
|||||||
@Inject
|
@Inject
|
||||||
Application application;
|
Application application;
|
||||||
@Inject
|
@Inject
|
||||||
|
Engine engine;
|
||||||
|
@Inject
|
||||||
Lazy<Clipboard> clipboard;
|
Lazy<Clipboard> clipboard;
|
||||||
@Inject
|
@Inject
|
||||||
Lazy<Notifier> notifier;
|
Lazy<Notifier> notifier;
|
||||||
@ -139,6 +141,7 @@ public class Display {
|
|||||||
Check.isMainThread();
|
Check.isMainThread();
|
||||||
this.view = view;
|
this.view = view;
|
||||||
this.view.setState(state);
|
this.view.setState(state);
|
||||||
|
this.view.setEngine(engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
|
@ -22,16 +22,14 @@
|
|||||||
|
|
||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import android.app.Service;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.text.method.ScrollingMovementMethod;
|
import android.text.method.ScrollingMovementMethod;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import org.solovyev.android.Check;
|
import org.solovyev.android.Check;
|
||||||
import org.solovyev.android.calculator.text.TextProcessor;
|
|
||||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
|
||||||
import org.solovyev.android.calculator.view.TextHighlighter;
|
import org.solovyev.android.calculator.view.TextHighlighter;
|
||||||
import org.solovyev.android.views.AutoResizeTextView;
|
import org.solovyev.android.views.AutoResizeTextView;
|
||||||
|
|
||||||
@ -42,8 +40,10 @@ import static android.util.TypedValue.applyDimension;
|
|||||||
|
|
||||||
public class DisplayView extends AutoResizeTextView {
|
public class DisplayView extends AutoResizeTextView {
|
||||||
|
|
||||||
@Nonnull
|
@Nullable
|
||||||
private final TextProcessor<TextProcessorEditorResult, String> textHighlighter = new TextHighlighter(getTextColors().getDefaultColor(), false);
|
private Engine engine;
|
||||||
|
@Nullable
|
||||||
|
private TextHighlighter textHighlighter;
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private DisplayState state = DisplayState.empty();
|
private DisplayState state = DisplayState.empty();
|
||||||
|
|
||||||
@ -62,6 +62,14 @@ public class DisplayView extends AutoResizeTextView {
|
|||||||
init(context);
|
init(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private TextHighlighter getTextHighlighter() {
|
||||||
|
if (textHighlighter == null && engine != null) {
|
||||||
|
textHighlighter = new TextHighlighter(getTextColors().getDefaultColor(), false, engine);
|
||||||
|
}
|
||||||
|
return textHighlighter;
|
||||||
|
}
|
||||||
|
|
||||||
private void init(@Nonnull Context context) {
|
private void init(@Nonnull Context context) {
|
||||||
final Resources resources = getResources();
|
final Resources resources = getResources();
|
||||||
setAddEllipsis(false);
|
setAddEllipsis(false);
|
||||||
@ -95,12 +103,20 @@ public class DisplayView extends AutoResizeTextView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setEngine(@Nullable Engine engine) {
|
||||||
|
this.engine = engine;
|
||||||
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private CharSequence highlightText(@Nonnull DisplayState state) {
|
private CharSequence highlightText(@Nonnull DisplayState state) {
|
||||||
final String text = state.text;
|
final String text = state.text;
|
||||||
if (TextUtils.isEmpty(text)) {
|
if (TextUtils.isEmpty(text)) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
final TextHighlighter textHighlighter = getTextHighlighter();
|
||||||
|
if (textHighlighter == null) {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return textHighlighter.process(text).getCharSequence();
|
return textHighlighter.process(text).getCharSequence();
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
|
@ -22,13 +22,9 @@
|
|||||||
|
|
||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import static java.lang.Math.min;
|
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
import com.squareup.otto.Bus;
|
import com.squareup.otto.Bus;
|
||||||
import com.squareup.otto.Subscribe;
|
import com.squareup.otto.Subscribe;
|
||||||
|
|
||||||
import org.solovyev.android.Check;
|
import org.solovyev.android.Check;
|
||||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||||
import org.solovyev.android.calculator.view.EditorTextProcessor;
|
import org.solovyev.android.calculator.view.EditorTextProcessor;
|
||||||
@ -38,6 +34,8 @@ import javax.annotation.Nullable;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import static java.lang.Math.min;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
public class Editor {
|
public class Editor {
|
||||||
|
|
||||||
@ -51,8 +49,8 @@ public class Editor {
|
|||||||
Bus bus;
|
Bus bus;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public Editor(@Nonnull SharedPreferences preferences) {
|
public Editor(@Nonnull SharedPreferences preferences, @Nonnull Engine engine) {
|
||||||
textProcessor = new EditorTextProcessor(preferences);
|
textProcessor = new EditorTextProcessor(preferences, engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
|
@ -23,24 +23,15 @@
|
|||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import android.text.SpannableStringBuilder;
|
import android.text.SpannableStringBuilder;
|
||||||
|
import jscl.NumeralBase;
|
||||||
import org.solovyev.android.calculator.math.MathType;
|
import org.solovyev.android.calculator.math.MathType;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import jscl.NumeralBase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User: serso
|
|
||||||
* Date: 12/15/11
|
|
||||||
* Time: 8:33 PM
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class LiteNumberBuilder extends BaseNumberBuilder {
|
public class LiteNumberBuilder extends BaseNumberBuilder {
|
||||||
|
|
||||||
public LiteNumberBuilder(@Nonnull Engine engine) {
|
public LiteNumberBuilder(@Nonnull Engine engine) {
|
||||||
super(engine);
|
super(engine);
|
||||||
this.nb = engine.getMathEngine().getNumeralBase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -41,11 +41,6 @@ import jscl.text.JsclIntegerParser;
|
|||||||
import jscl.text.ParseException;
|
import jscl.text.ParseException;
|
||||||
import jscl.text.Parser;
|
import jscl.text.Parser;
|
||||||
|
|
||||||
/**
|
|
||||||
* User: serso
|
|
||||||
* Date: 10/23/11
|
|
||||||
* Time: 2:57 PM
|
|
||||||
*/
|
|
||||||
public class NumberBuilder extends BaseNumberBuilder {
|
public class NumberBuilder extends BaseNumberBuilder {
|
||||||
|
|
||||||
public NumberBuilder(@Nonnull Engine engine) {
|
public NumberBuilder(@Nonnull Engine engine) {
|
||||||
|
@ -30,7 +30,6 @@ import jscl.math.function.IFunction;
|
|||||||
import org.simpleframework.xml.Serializer;
|
import org.simpleframework.xml.Serializer;
|
||||||
import org.simpleframework.xml.core.Persister;
|
import org.simpleframework.xml.core.Persister;
|
||||||
import org.solovyev.android.Check;
|
import org.solovyev.android.Check;
|
||||||
import org.solovyev.android.calculator.Calculator;
|
|
||||||
import org.solovyev.android.calculator.entities.BaseEntitiesRegistry;
|
import org.solovyev.android.calculator.entities.BaseEntitiesRegistry;
|
||||||
import org.solovyev.android.calculator.entities.Category;
|
import org.solovyev.android.calculator.entities.Category;
|
||||||
import org.solovyev.android.calculator.entities.Entities;
|
import org.solovyev.android.calculator.entities.Entities;
|
||||||
@ -59,9 +58,6 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function> {
|
|||||||
substitutes.put("√", "sqrt");
|
substitutes.put("√", "sqrt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject
|
|
||||||
Calculator calculator;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public FunctionsRegistry(@Nonnull JsclMathEngine mathEngine) {
|
public FunctionsRegistry(@Nonnull JsclMathEngine mathEngine) {
|
||||||
super(mathEngine.getFunctionsRegistry(), "c_fun_description_");
|
super(mathEngine.getFunctionsRegistry(), "c_fun_description_");
|
||||||
|
@ -4,6 +4,7 @@ import android.app.Application;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
import org.solovyev.android.calculator.App;
|
import org.solovyev.android.calculator.App;
|
||||||
|
import org.solovyev.android.calculator.Engine;
|
||||||
import org.solovyev.android.calculator.Preferences;
|
import org.solovyev.android.calculator.Preferences;
|
||||||
import org.solovyev.android.calculator.text.TextProcessor;
|
import org.solovyev.android.calculator.text.TextProcessor;
|
||||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||||
@ -20,8 +21,11 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private TextHighlighter textHighlighter;
|
private TextHighlighter textHighlighter;
|
||||||
|
@Nonnull
|
||||||
|
private final Engine engine;
|
||||||
|
|
||||||
public EditorTextProcessor(@Nonnull SharedPreferences preferences) {
|
public EditorTextProcessor(@Nonnull SharedPreferences preferences, @Nonnull Engine engine) {
|
||||||
|
this.engine = engine;
|
||||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||||
onSharedPreferenceChanged(preferences, colorDisplay.getKey());
|
onSharedPreferenceChanged(preferences, colorDisplay.getKey());
|
||||||
}
|
}
|
||||||
@ -44,10 +48,6 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
|
|||||||
return textHighlighter;
|
return textHighlighter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isHighlightText() {
|
|
||||||
return highlightText;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setHighlightText(boolean highlightText) {
|
public void setHighlightText(boolean highlightText) {
|
||||||
this.highlightText = highlightText;
|
this.highlightText = highlightText;
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
|
|||||||
setHighlightText(colorDisplay.getPreference(preferences));
|
setHighlightText(colorDisplay.getPreference(preferences));
|
||||||
} else if (theme.isSameKey(key)) {
|
} else if (theme.isSameKey(key)) {
|
||||||
final int color = getTextColor(preferences);
|
final int color = getTextColor(preferences);
|
||||||
textHighlighter = new TextHighlighter(color, true);
|
textHighlighter = new TextHighlighter(color, true, engine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,9 +44,12 @@ public class TextHighlighter implements TextProcessor<TextProcessorEditorResult,
|
|||||||
private final int blue;
|
private final int blue;
|
||||||
private final boolean formatNumber;
|
private final boolean formatNumber;
|
||||||
private final int dark;
|
private final int dark;
|
||||||
|
@Nonnull
|
||||||
|
private final Engine engine;
|
||||||
|
|
||||||
public TextHighlighter(int color, boolean formatNumber) {
|
public TextHighlighter(int color, boolean formatNumber, @Nonnull Engine engine) {
|
||||||
this.formatNumber = formatNumber;
|
this.formatNumber = formatNumber;
|
||||||
|
this.engine = engine;
|
||||||
red = red(color);
|
red = red(color);
|
||||||
green = green(color);
|
green = green(color);
|
||||||
blue = blue(color);
|
blue = blue(color);
|
||||||
@ -77,7 +80,6 @@ public class TextHighlighter implements TextProcessor<TextProcessorEditorResult,
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public TextProcessorEditorResult process(@Nonnull String text) {
|
public TextProcessorEditorResult process(@Nonnull String text) {
|
||||||
final Engine engine = Locator.getInstance().getEngine();
|
|
||||||
final SpannableStringBuilder sb = new SpannableStringBuilder();
|
final SpannableStringBuilder sb = new SpannableStringBuilder();
|
||||||
final BaseNumberBuilder nb = !formatNumber ? new LiteNumberBuilder(engine) : new NumberBuilder(engine);
|
final BaseNumberBuilder nb = !formatNumber ? new LiteNumberBuilder(engine) : new NumberBuilder(engine);
|
||||||
final MathType.Result result = new MathType.Result();
|
final MathType.Result result = new MathType.Result();
|
||||||
|
@ -49,7 +49,7 @@ public class TextHighlighterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testProcess() throws Exception {
|
public void testProcess() throws Exception {
|
||||||
TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.TRANSPARENT, false);
|
TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.TRANSPARENT, false, engine);
|
||||||
|
|
||||||
final Random random = new Random(new Date().getTime());
|
final Random random = new Random(new Date().getTime());
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
@ -69,7 +69,7 @@ public class TextHighlighterTest {
|
|||||||
assertEquals(")", textHighlighter.process(")").toString());
|
assertEquals(")", textHighlighter.process(")").toString());
|
||||||
assertEquals(")()(", textHighlighter.process(")()(").toString());
|
assertEquals(")()(", textHighlighter.process(")()(").toString());
|
||||||
|
|
||||||
textHighlighter = new TextHighlighter(0, true);
|
textHighlighter = new TextHighlighter(0, true, engine);
|
||||||
assertEquals("1 000 000", textHighlighter.process("1000000").toString());
|
assertEquals("1 000 000", textHighlighter.process("1000000").toString());
|
||||||
assertEquals("1 000 000", textHighlighter.process("1000000").toString());
|
assertEquals("1 000 000", textHighlighter.process("1000000").toString());
|
||||||
assertEquals("0.1E3", textHighlighter.process("0.1E3").toString());
|
assertEquals("0.1E3", textHighlighter.process("0.1E3").toString());
|
||||||
@ -88,7 +88,7 @@ public class TextHighlighterTest {
|
|||||||
assertEquals("-1 000 000E3", textHighlighter.process("-1000000E3").toString());
|
assertEquals("-1 000 000E3", textHighlighter.process("-1000000E3").toString());
|
||||||
assertEquals("-1 000 000E-3", textHighlighter.process("-1000000E-3").toString());
|
assertEquals("-1 000 000E-3", textHighlighter.process("-1000000E-3").toString());
|
||||||
assertEquals("-1 000 000E-30000", textHighlighter.process("-1000000E-30000").toString());
|
assertEquals("-1 000 000E-30000", textHighlighter.process("-1000000E-30000").toString());
|
||||||
textHighlighter = new TextHighlighter(0, false);
|
textHighlighter = new TextHighlighter(0, false, engine);
|
||||||
|
|
||||||
textHighlighter.process("cannot calculate 3^10^10 !!!\n" +
|
textHighlighter.process("cannot calculate 3^10^10 !!!\n" +
|
||||||
" unable to enter 0. FIXED\n" +
|
" unable to enter 0. FIXED\n" +
|
||||||
@ -159,7 +159,7 @@ public class TextHighlighterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTime() throws Exception {
|
public void testTime() throws Exception {
|
||||||
final TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.WHITE, false);
|
final TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.WHITE, false, engine);
|
||||||
|
|
||||||
final int count = 1000;
|
final int count = 1000;
|
||||||
final String subExpression = "cos(acos(t8ln(t5t85tln(8ln(5t55tln(5))))))+tln(88cos(tln(t)))+t√(ln(t))";
|
final String subExpression = "cos(acos(t8ln(t5t85tln(8ln(5t55tln(5))))))+tln(88cos(tln(t)))+t√(ln(t))";
|
||||||
@ -178,7 +178,7 @@ public class TextHighlighterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDarkColor() throws Exception {
|
public void testDarkColor() throws Exception {
|
||||||
final TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.BLACK, false);
|
final TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.BLACK, false, engine);
|
||||||
assertEquals("", textHighlighter.process("sin(2cos(3))"));
|
assertEquals("", textHighlighter.process("sin(2cos(3))"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user