number formatting is working now
This commit is contained in:
@@ -35,6 +35,7 @@ import android.util.TypedValue;
|
||||
import org.solovyev.android.calculator.core.R;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.view.TextHighlighter;
|
||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||
import org.solovyev.android.view.AutoResizeTextView;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -59,7 +60,7 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
|
||||
*/
|
||||
|
||||
@Nonnull
|
||||
private final static TextProcessor<TextHighlighter.Result, String> textHighlighter = new TextHighlighter(Color.WHITE, false);
|
||||
private final static TextProcessor<TextProcessorEditorResult, String> textHighlighter = new TextHighlighter(Color.WHITE, false);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
@@ -169,7 +170,7 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
|
||||
//Log.d(this.getClass().getName(), text);
|
||||
|
||||
try {
|
||||
final TextHighlighter.Result processedText = textHighlighter.process(text);
|
||||
final TextProcessorEditorResult processedText = textHighlighter.process(text);
|
||||
text = processedText.toString();
|
||||
result = Html.fromHtml(text);
|
||||
} catch (CalculatorParseException e) {
|
||||
|
@@ -23,44 +23,26 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Color;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.text.Editable;
|
||||
import android.text.Html;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.ContextMenu;
|
||||
import android.widget.EditText;
|
||||
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.view.TextHighlighter;
|
||||
import org.solovyev.android.prefs.BooleanPreference;
|
||||
import org.solovyev.common.collections.Collections;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/17/11
|
||||
* Time: 12:25 AM
|
||||
*/
|
||||
public class AndroidCalculatorEditorView extends EditText implements SharedPreferences.OnSharedPreferenceChangeListener, CalculatorEditorView {
|
||||
|
||||
@Nonnull
|
||||
private static final BooleanPreference colorDisplay = BooleanPreference.of("org.solovyev.android.calculator.CalculatorModel_color_display", true);
|
||||
public class AndroidCalculatorEditorView extends EditText implements CalculatorEditorView {
|
||||
|
||||
private volatile boolean initialized = false;
|
||||
|
||||
private boolean highlightText = true;
|
||||
|
||||
@Nonnull
|
||||
private final static TextProcessor<TextHighlighter.Result, String> textHighlighter = new TextHighlighter(Color.WHITE, false);
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
@Nonnull
|
||||
private volatile CalculatorEditorViewState viewState = CalculatorEditorViewStateImpl.newDefaultInstance();
|
||||
@@ -113,57 +95,15 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe
|
||||
menu.removeItem(android.R.id.selectAll);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private CharSequence prepareText(@Nonnull String text, boolean highlightText) {
|
||||
CharSequence result;
|
||||
|
||||
if (highlightText) {
|
||||
|
||||
try {
|
||||
final TextHighlighter.Result processesText = textHighlighter.process(text);
|
||||
|
||||
assert processesText.getOffset() == 0;
|
||||
|
||||
result = Html.fromHtml(processesText.toString());
|
||||
} catch (CalculatorParseException e) {
|
||||
// set raw text
|
||||
result = text;
|
||||
|
||||
Log.e(this.getClass().getName(), e.getMessage(), e);
|
||||
}
|
||||
} else {
|
||||
result = text;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean isHighlightText() {
|
||||
return highlightText;
|
||||
}
|
||||
|
||||
public void setHighlightText(boolean highlightText) {
|
||||
this.highlightText = highlightText;
|
||||
//this.highlightText = highlightText;
|
||||
Locator.getInstance().getEditor().updateViewState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||
if (colorDisplay.getKey().equals(key)) {
|
||||
this.setHighlightText(colorDisplay.getPreference(preferences));
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void init(@Nonnull Context context) {
|
||||
public synchronized void init() {
|
||||
if (!initialized) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
this.addTextChangedListener(new TextWatcherImpl());
|
||||
|
||||
onSharedPreferenceChanged(preferences, colorDisplay.getKey());
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
@@ -172,8 +112,6 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe
|
||||
public void setState(@Nonnull final CalculatorEditorViewState viewState) {
|
||||
synchronized (this) {
|
||||
|
||||
final CharSequence text = prepareText(viewState.getText(), highlightText);
|
||||
|
||||
uiHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -182,7 +120,7 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe
|
||||
try {
|
||||
editorView.viewStateChange = true;
|
||||
editorView.viewState = viewState;
|
||||
editorView.setText(text, BufferType.EDITABLE);
|
||||
editorView.setText(viewState.getTextAsCharSequence(), BufferType.EDITABLE);
|
||||
final int selection = CalculatorEditorImpl.correctSelection(viewState.getSelection(), editorView.getText());
|
||||
editorView.setSelection(selection);
|
||||
} finally {
|
||||
|
@@ -84,6 +84,7 @@ public final class CalculatorPreferences {
|
||||
public static final Preference<Boolean> autoOrientation = BooleanPreference.of("autoOrientation", true);
|
||||
public static final Preference<Boolean> hideNumeralBaseDigits = BooleanPreference.of("hideNumeralBaseDigits", true);
|
||||
public static final Preference<Boolean> preventScreenFromFading = BooleanPreference.of("preventScreenFromFading", true);
|
||||
public static final Preference<Boolean> colorDisplay = BooleanPreference.of("org.solovyev.android.calculator.CalculatorModel_color_display", true);
|
||||
|
||||
@Nonnull
|
||||
public static Theme getTheme(@Nonnull SharedPreferences preferences) {
|
||||
|
@@ -66,6 +66,12 @@ public final class ParcelableCalculatorEditorViewState implements CalculatorEdit
|
||||
return viewState.getText();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CharSequence getTextAsCharSequence() {
|
||||
return getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelection() {
|
||||
return viewState.getSelection();
|
||||
|
@@ -0,0 +1,75 @@
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.text.Html;
|
||||
import android.util.Log;
|
||||
import org.solovyev.android.calculator.CalculatorParseException;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.colorDisplay;
|
||||
import static org.solovyev.android.calculator.view.TextHighlighter.WHITE;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 6/27/13
|
||||
* Time: 6:11 PM
|
||||
*/
|
||||
public final class EditorTextProcessor implements TextProcessor<TextProcessorEditorResult, String>, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
private boolean highlightText = true;
|
||||
|
||||
private final TextProcessor<TextProcessorEditorResult, String> textHighlighter = new TextHighlighter(WHITE, true);
|
||||
|
||||
public EditorTextProcessor() {
|
||||
}
|
||||
|
||||
public void init(@Nonnull Context context) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
onSharedPreferenceChanged(preferences, colorDisplay.getKey());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public TextProcessorEditorResult process(@Nonnull String text) throws CalculatorParseException {
|
||||
TextProcessorEditorResult result;
|
||||
|
||||
if (highlightText) {
|
||||
|
||||
try {
|
||||
final TextProcessorEditorResult processesText = textHighlighter.process(text);
|
||||
|
||||
result = new TextProcessorEditorResult(Html.fromHtml(processesText.toString()), processesText.getOffset());
|
||||
} catch (CalculatorParseException e) {
|
||||
// set raw text
|
||||
result = new TextProcessorEditorResult(text, 0);
|
||||
|
||||
Log.e(this.getClass().getName(), e.getMessage(), e);
|
||||
}
|
||||
} else {
|
||||
result = new TextProcessorEditorResult(text, 0);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean isHighlightText() {
|
||||
return highlightText;
|
||||
}
|
||||
|
||||
public void setHighlightText(boolean highlightText) {
|
||||
this.highlightText = highlightText;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||
if (colorDisplay.getKey().equals(key)) {
|
||||
this.setHighlightText(colorDisplay.getPreference(preferences));
|
||||
}
|
||||
}
|
||||
}
|
@@ -22,14 +22,14 @@
|
||||
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||
import org.solovyev.common.MutableObject;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -38,7 +38,9 @@ import java.util.Map;
|
||||
* Date: 10/12/11
|
||||
* Time: 9:47 PM
|
||||
*/
|
||||
public class TextHighlighter implements TextProcessor<TextHighlighter.Result, String> {
|
||||
public class TextHighlighter implements TextProcessor<TextProcessorEditorResult, String> {
|
||||
|
||||
public static final int WHITE = -1;
|
||||
|
||||
private static final Map<String, String> nbFontAttributes = new HashMap<String, String>();
|
||||
|
||||
@@ -46,54 +48,6 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
|
||||
nbFontAttributes.put("color", "#008000");
|
||||
}
|
||||
|
||||
public static class Result implements CharSequence {
|
||||
|
||||
@Nonnull
|
||||
private final CharSequence charSequence;
|
||||
|
||||
@Nullable
|
||||
private String string;
|
||||
|
||||
private final int offset;
|
||||
|
||||
public Result(@Nonnull CharSequence charSequence, int offset) {
|
||||
this.charSequence = charSequence;
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return charSequence.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public char charAt(int i) {
|
||||
return charSequence.charAt(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence subSequence(int i, int i1) {
|
||||
return charSequence.subSequence(i, i1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (string == null) {
|
||||
string = charSequence.toString();
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CharSequence getCharSequence() {
|
||||
return charSequence;
|
||||
}
|
||||
|
||||
public int getOffset() {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
|
||||
private final int color;
|
||||
private final int colorRed;
|
||||
private final int colorGreen;
|
||||
@@ -113,7 +67,7 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Result process(@Nonnull String text) throws CalculatorParseException {
|
||||
public TextProcessorEditorResult process(@Nonnull String text) throws CalculatorParseException {
|
||||
final CharSequence result;
|
||||
|
||||
int maxNumberOfOpenGroupSymbols = 0;
|
||||
@@ -186,20 +140,17 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
|
||||
|
||||
final StringBuilder text2 = new StringBuilder(text1.length());
|
||||
|
||||
final CharSequence s = text1;
|
||||
int i = processBracketGroup(text2, s, 0, 0, maxNumberOfOpenGroupSymbols);
|
||||
for (; i < s.length(); i++) {
|
||||
text2.append(s.charAt(i));
|
||||
int i = processBracketGroup(text2, text1, 0, 0, maxNumberOfOpenGroupSymbols);
|
||||
for (; i < text1.length(); i++) {
|
||||
text2.append(text1.charAt(i));
|
||||
}
|
||||
|
||||
//Log.d(AndroidCalculatorEditorView.class.getName(), text2.toString());
|
||||
|
||||
result = text2.toString();
|
||||
} else {
|
||||
result = text1.toString();
|
||||
}
|
||||
|
||||
return new Result(result, resultOffset);
|
||||
return new TextProcessorEditorResult(result, resultOffset);
|
||||
}
|
||||
|
||||
private int processHighlightedText(@Nonnull StringBuilder result, int i, @Nonnull String match, @Nonnull String tag, @Nullable Map<String, String> tagAttributes) {
|
||||
|
Reference in New Issue
Block a user