syntax highlighting
This commit is contained in:
parent
f930f3e495
commit
941867855f
@ -7,9 +7,13 @@ package org.solovyev.android.calculator;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.ColorStateList;
|
import android.content.res.ColorStateList;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.text.Html;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Log;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import org.solovyev.common.exceptions.SersoException;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.solovyev.android.calculator.math.MathEntityType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
@ -36,36 +40,76 @@ public class CalculatorEditText extends EditText {
|
|||||||
super.setTextColor(colors); //To change body of overridden methods use File | Settings | File Templates.
|
super.setTextColor(colors); //To change body of overridden methods use File | Settings | File Templates.
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void redraw() {
|
||||||
public void setText(CharSequence text, BufferType type) {
|
String text = getText().toString();
|
||||||
/*try {
|
int selectionStart = getSelectionStart();
|
||||||
final List<Pair<Integer, Integer>> groupSymbols = new ArrayList<Pair<Integer, Integer>>();
|
int selectionEnd = getSelectionEnd();
|
||||||
|
|
||||||
final Stack<Pair<Integer, Integer>> groupSymbolsStack = new Stack<Pair<Integer, Integer>>();
|
int maxNumberOfOpenGroupSymbols = 0;
|
||||||
|
int numberOfOpenGroupSymbols = 0;
|
||||||
for (int i = 0; i < text.length(); i++) {
|
for (int i = 0; i < text.length(); i++) {
|
||||||
char ch = text.charAt(i);
|
char ch = text.charAt(i);
|
||||||
if (MathEntityType.openGroupSymbols.contains(ch)) {
|
if (MathEntityType.openGroupSymbols.contains(ch)) {
|
||||||
groupSymbolsStack.push(new Pair<Integer, Integer>(i, null));
|
numberOfOpenGroupSymbols++;
|
||||||
|
maxNumberOfOpenGroupSymbols = Math.max(maxNumberOfOpenGroupSymbols, numberOfOpenGroupSymbols);
|
||||||
} else if (MathEntityType.closeGroupSymbols.contains(ch)) {
|
} else if (MathEntityType.closeGroupSymbols.contains(ch)) {
|
||||||
final Pair<Integer, Integer> pair = groupSymbolsStack.pop();
|
numberOfOpenGroupSymbols--;
|
||||||
if (pair != null) {
|
}
|
||||||
pair.setSecond(i);
|
}
|
||||||
groupSymbols.add(0, pair);
|
|
||||||
|
if (maxNumberOfOpenGroupSymbols > 0) {
|
||||||
|
|
||||||
|
final StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
processGroup(sb, text, 0, 0, maxNumberOfOpenGroupSymbols);
|
||||||
|
|
||||||
|
Log.d(CalculatorEditText.class.getName(), sb.toString());
|
||||||
|
|
||||||
|
super.setText(Html.fromHtml(sb.toString()), BufferType.EDITABLE);
|
||||||
} else {
|
} else {
|
||||||
throw new NoPairGroupSymbolException();
|
super.setText(text, BufferType.EDITABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setSelection(selectionStart, selectionEnd);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int processGroup(@NotNull StringBuilder result, @NotNull String s, int i, int numberOfOpenings, int maxNumberOfGroups) {
|
||||||
|
|
||||||
|
result.append("<font color=\"").append(getColor(maxNumberOfGroups, numberOfOpenings)).append("\">");
|
||||||
|
|
||||||
|
for (; i < s.length(); i++) {
|
||||||
|
char ch = s.charAt(i);
|
||||||
|
|
||||||
|
if (MathEntityType.openGroupSymbols.contains(ch)) {
|
||||||
|
result.append(ch);
|
||||||
|
result.append("</font>");
|
||||||
|
i = processGroup(result, s, i + 1, numberOfOpenings + 1, maxNumberOfGroups);
|
||||||
|
result.append("<font color=\"").append(getColor(maxNumberOfGroups, numberOfOpenings)).append("\">");
|
||||||
|
if (i < s.length() && MathEntityType.closeGroupSymbols.contains(s.charAt(i))) {
|
||||||
|
result.append(s.charAt(i));
|
||||||
|
}
|
||||||
|
} else if (MathEntityType.closeGroupSymbols.contains(ch)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
result.append(ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
text = insertHtml(text, groupSymbols);
|
result.append("</font>");
|
||||||
} catch (NoPairGroupSymbolException e) {
|
|
||||||
// do nothing
|
|
||||||
}*/
|
|
||||||
|
|
||||||
super.setText(text, type);
|
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class NoPairGroupSymbolException extends SersoException {
|
private String getColor(int numberOfOpenGroupSymbols, int numberOfOpenings) {
|
||||||
|
final int baseColor = Color.WHITE;
|
||||||
|
|
||||||
|
double c = 1;
|
||||||
|
|
||||||
|
int i = ((int)(255 * c)) * numberOfOpenings / (numberOfOpenGroupSymbols + 1);
|
||||||
|
|
||||||
|
int result = Color.rgb( Color.red(baseColor) - i, Color.green(baseColor) - i, Color.blue(baseColor) - i);
|
||||||
|
|
||||||
|
return "#" + Integer.toHexString(result).substring(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,6 +118,8 @@ public class CalculatorView implements CursorControl, HistoryControl<CalculatorH
|
|||||||
final String editorStateAfter = this.editor.getText().toString();
|
final String editorStateAfter = this.editor.getText().toString();
|
||||||
if (!editorStateBefore.equals(editorStateAfter)) {
|
if (!editorStateBefore.equals(editorStateAfter)) {
|
||||||
|
|
||||||
|
editor.redraw();
|
||||||
|
|
||||||
currentRunner.setObject(new Runnable() {
|
currentRunner.setObject(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -240,6 +242,8 @@ public class CalculatorView implements CursorControl, HistoryControl<CalculatorH
|
|||||||
synchronized (history) {
|
synchronized (history) {
|
||||||
setValuesFromHistory(this.editor, editorHistoryState.getEditorState());
|
setValuesFromHistory(this.editor, editorHistoryState.getEditorState());
|
||||||
setValuesFromHistory(this.display, editorHistoryState.getDisplayState());
|
setValuesFromHistory(this.display, editorHistoryState.getDisplayState());
|
||||||
|
|
||||||
|
editor.redraw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user