Calculator editor

This commit is contained in:
serso 2012-09-21 13:54:35 +04:00
parent 30fbc24f81
commit 0c698a6d39
15 changed files with 906 additions and 442 deletions

View File

@ -27,4 +27,8 @@ public interface Calculator extends CalculatorEventContainer {
@NotNull
CalculatorEventDataId convert(@NotNull Generic generic, @NotNull NumeralBase to);
@NotNull
CalculatorEventDataId fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
}

View File

@ -16,4 +16,58 @@ public interface CalculatorEditor extends CalculatorEventListener/*, CursorContr
CalculatorEditorViewState getViewState();
void setViewState(@NotNull CalculatorEditorViewState viewState);
/*
**********************************************************************
*
* CURSOR CONTROL
*
**********************************************************************
*/
/**
* Method sets the cursor to the beginning
*/
@NotNull
public CalculatorEditorViewState setCursorOnStart();
/**
* Method sets the cursor to the end
*/
@NotNull
public CalculatorEditorViewState setCursorOnEnd();
/**
* Method moves cursor to the left of current position
*/
@NotNull
public CalculatorEditorViewState moveCursorLeft();
/**
* Method moves cursor to the right of current position
*/
@NotNull
public CalculatorEditorViewState moveCursorRight();
/*
**********************************************************************
*
* EDITOR OPERATIONS
*
**********************************************************************
*/
@NotNull
CalculatorEditorViewState erase();
@NotNull
CalculatorEditorViewState setText(@NotNull String text);
@NotNull
CalculatorEditorViewState setText(@NotNull String text, int selection);
@NotNull
CalculatorEditorViewState insert(@NotNull String text);
@NotNull
CalculatorEditorViewState moveSelection(int offset);
}

View File

@ -0,0 +1,17 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/**
* User: Solovyev_S
* Date: 21.09.12
* Time: 13:46
*/
public interface CalculatorEditorChangeEventData {
@NotNull
CalculatorEditorViewState getOldState();
@NotNull
CalculatorEditorViewState getNewState();
}

View File

@ -0,0 +1,34 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/**
* User: Solovyev_S
* Date: 21.09.12
* Time: 13:46
*/
public class CalculatorEditorChangeEventDataImpl implements CalculatorEditorChangeEventData {
@NotNull
private CalculatorEditorViewState oldState;
@NotNull
private CalculatorEditorViewState newState;
public CalculatorEditorChangeEventDataImpl(@NotNull CalculatorEditorViewState oldState, @NotNull CalculatorEditorViewState newState) {
this.oldState = oldState;
this.newState = newState;
}
@NotNull
@Override
public CalculatorEditorViewState getOldState() {
return this.oldState;
}
@NotNull
@Override
public CalculatorEditorViewState getNewState() {
return this.newState;
}
}

View File

@ -19,6 +19,13 @@ public class CalculatorEditorImpl implements CalculatorEditor {
@NotNull
private CalculatorEditorViewState lastViewState = CalculatorEditorViewStateImpl.newDefaultInstance();
@NotNull
private final Calculator calculator;
public CalculatorEditorImpl(@NotNull Calculator calculator) {
this.calculator = calculator;
}
@Override
public void setView(@Nullable CalculatorEditorView view) {
synchronized (viewLock) {
@ -37,12 +44,16 @@ public class CalculatorEditorImpl implements CalculatorEditor {
}
@Override
public void setViewState(@NotNull CalculatorEditorViewState viewState) {
public void setViewState(@NotNull CalculatorEditorViewState newViewState) {
synchronized (viewLock) {
this.lastViewState = viewState;
final CalculatorEditorViewState oldViewState = this.lastViewState;
this.lastViewState = newViewState;
if (this.view != null) {
this.view.setState(viewState);
this.view.setState(newViewState);
}
calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState));
}
}
@ -53,35 +64,130 @@ public class CalculatorEditorImpl implements CalculatorEditor {
//To change body of implemented methods use File | Settings | File Templates.
}
public void setCursorOnStart() {
@NotNull
public CalculatorEditorViewState setCursorOnStart() {
synchronized (viewLock) {
newSelectionViewState(0);
return newSelectionViewState(0);
}
}
private void newSelectionViewState(int newSelection) {
setViewState(CalculatorEditorViewStateImpl.newSelection(this.lastViewState, newSelection));
@NotNull
private CalculatorEditorViewState newSelectionViewState(int newSelection) {
if (this.lastViewState.getSelection() != newSelection) {
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, newSelection);
setViewState(result);
return result;
} else {
return this.lastViewState;
}
}
public void setCursorOnEnd() {
@NotNull
public CalculatorEditorViewState setCursorOnEnd() {
synchronized (viewLock) {
newSelectionViewState(this.lastViewState.getText().length());
return newSelectionViewState(this.lastViewState.getText().length());
}
}
public void moveCursorLeft() {
@NotNull
public CalculatorEditorViewState moveCursorLeft() {
synchronized (viewLock) {
if (this.lastViewState.getSelection() > 0) {
newSelectionViewState(this.lastViewState.getSelection() - 1);
return newSelectionViewState(this.lastViewState.getSelection() - 1);
} else {
return this.lastViewState;
}
}
}
public void moveCursorRight() {
@NotNull
public CalculatorEditorViewState moveCursorRight() {
synchronized (viewLock) {
if (this.lastViewState.getSelection() < this.lastViewState.getText().length()) {
newSelectionViewState(this.lastViewState.getSelection() + 1);
return newSelectionViewState(this.lastViewState.getSelection() + 1);
} else {
return this.lastViewState;
}
}
}
@NotNull
@Override
public CalculatorEditorViewState erase() {
synchronized (viewLock) {
int selection = this.lastViewState.getSelection();
final String text = this.lastViewState.getText();
if (selection > 0 && text.length() > 0 && selection <= text.length()) {
final StringBuilder newText = new StringBuilder(text.length() - 1);
newText.append(text.substring(0, selection - 1)).append(text.substring(selection, text.length()));
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), selection - 1);
setViewState(result);
return result;
} else {
return this.lastViewState;
}
}
}
@NotNull
@Override
public CalculatorEditorViewState setText(@NotNull String text) {
synchronized (viewLock) {
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, text.length());
setViewState(result);
return result;
}
}
@NotNull
@Override
public CalculatorEditorViewState setText(@NotNull String text, int selection) {
synchronized (viewLock) {
selection = correctSelection(selection, text);
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, selection);
setViewState(result);
return result;
}
}
@NotNull
@Override
public CalculatorEditorViewState insert(@NotNull String text) {
synchronized (viewLock) {
final int selection = this.lastViewState.getSelection();
final String oldText = this.lastViewState.getText();
final StringBuilder newText = new StringBuilder(text.length() + oldText.length());
newText.append(oldText.substring(0, selection));
newText.append(text);
newText.append(oldText.substring(selection));
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), text.length() + selection);
setViewState(result);
return result;
}
}
@NotNull
@Override
public CalculatorEditorViewState moveSelection(int offset) {
synchronized (viewLock) {
int selection = this.lastViewState.getSelection() + offset;
selection = correctSelection(selection, this.lastViewState.getText());
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, selection);
setViewState(result);
return result;
}
}
private int correctSelection(int selection, @NotNull String text) {
int result = Math.max(selection, 0);
result = Math.min(result, text.length());
return result;
}
}

View File

@ -46,4 +46,12 @@ public class CalculatorEditorViewStateImpl implements CalculatorEditorViewState
return result;
}
@NotNull
public static CalculatorEditorViewState newInstance(@NotNull String text, int selection) {
final CalculatorEditorViewStateImpl result = new CalculatorEditorViewStateImpl();
result.text = text;
result.selection = selection;
return result;
}
}

View File

@ -40,7 +40,18 @@ public enum CalculatorEventType {
conversion_started,
// @NotNull String conversion result
conversion_finished;
conversion_finished,
/*
**********************************************************************
*
* EDITOR
*
**********************************************************************
*/
// @NotNull org.solovyev.android.calculator.CalculatorEditorChangeEventData
editor_state_changed;
public boolean isOfType(@NotNull CalculatorEventType... types) {
for (CalculatorEventType type : types) {

View File

@ -26,7 +26,7 @@ import java.util.concurrent.atomic.AtomicLong;
* Date: 20.09.12
* Time: 16:42
*/
public class CalculatorImpl implements Calculator {
public class CalculatorImpl implements Calculator, CalculatorEventListener {
private static final long FIRST_ID = 0;
@ -46,6 +46,7 @@ public class CalculatorImpl implements Calculator {
private final Executor threadPoolExecutor = Executors.newFixedThreadPool(10);
public CalculatorImpl() {
this.addCalculatorEventListener(this);
}
@NotNull
@ -161,6 +162,21 @@ public class CalculatorImpl implements Calculator {
return eventDataId;
}
@NotNull
@Override
public CalculatorEventDataId fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
final CalculatorEventDataId eventDataId = nextCalculatorEventDataId();
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
fireCalculatorEvent(CalculatorEventDataImpl.newInstance(eventDataId), calculatorEventType, data);
}
});
return eventDataId;
}
@NotNull
private CalculatorEventData newConversionEventData(@NotNull Long sequenceId) {
return CalculatorEventDataImpl.newInstance(nextEventDataId(sequenceId));
@ -280,6 +296,15 @@ public class CalculatorImpl implements Calculator {
calculatorEventContainer.fireCalculatorEvents(calculatorEvents);
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
if ( calculatorEventType == CalculatorEventType.editor_state_changed ) {
final CalculatorEditorChangeEventData changeEventData = (CalculatorEditorChangeEventData) data;
evaluate(JsclOperation.numeric, changeEventData.getNewState().getText());
}
}
public static final class ConversionException extends Exception {
private ConversionException() {
}

View File

@ -16,10 +16,10 @@ public class CalculatorLocatorImpl implements CalculatorLocator {
private final CalculatorDisplay calculatorDisplay = new CalculatorDisplayImpl();
@NotNull
private final CalculatorEditor calculatorEditor = new CalculatorEditorImpl();
private final Calculator calculator = new CalculatorImpl();
@NotNull
private final Calculator calculator = new CalculatorImpl();
private final CalculatorEditor calculatorEditor = new CalculatorEditorImpl(calculator);
@NotNull
private static final CalculatorLocator instance = new CalculatorLocatorImpl();

View File

@ -8,12 +8,7 @@ package org.solovyev.android.calculator;
import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CalculatorApplication;
import org.solovyev.android.calculator.CalculatorParseException;
import org.solovyev.android.calculator.PreparedExpression;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.text.TextProcessor;
import org.solovyev.android.msg.AndroidMessage;
import org.solovyev.common.StartsWithFinder;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.collections.CollectionsUtils;
@ -57,7 +52,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
MathType.Result mathTypeResult = null;
MathType.Result mathTypeBefore;
final LiteNumberBuilder nb = new LiteNumberBuilder(CalculatorEngine.instance.getEngine());
final LiteNumberBuilder nb = new LiteNumberBuilder(CalculatorLocatorImpl.getInstance().getCalculatorEngine().getEngine());
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') continue;
startsWithFinder.setI(i);
@ -80,8 +75,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
if (mathTypeBefore != null &&
(mathTypeBefore.getMathType() == MathType.function || mathTypeBefore.getMathType() == MathType.operator) &&
CollectionsUtils.find(MathType.openGroupSymbols, startsWithFinder) != null) {
final AndroidMessage androidMessage = new AndroidMessage(R.string.msg_5, MessageType.error, CalculatorApplication.getInstance(), mathTypeBefore.getMatch());
throw new CalculatorParseException(i, s, androidMessage);
throw new CalculatorParseException(i, s, new CalculatorMessage(CalculatorMessages.msg_005, MessageType.error, mathTypeBefore.getMatch()));
}
i = mathTypeResult.processToJscl(result, i);
@ -92,8 +86,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
@NotNull
private static PreparedExpression replaceVariables(@NotNull final String s, int depth, @NotNull List<IConstant> undefinedVars) throws CalculatorParseException {
if (depth >= MAX_DEPTH) {
final AndroidMessage androidMessage = new AndroidMessage(R.string.msg_6, MessageType.error, CalculatorApplication.getInstance());
throw new CalculatorParseException(s, androidMessage);
throw new CalculatorParseException(s, new CalculatorMessage(CalculatorMessages.msg_006, MessageType.error));
} else {
depth++;
}
@ -109,9 +102,9 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
if (functionName == null) {
String operatorName = CollectionsUtils.find(MathType.operator.getTokens(), startsWithFinder);
if (operatorName == null) {
String varName = CollectionsUtils.find(CalculatorEngine.instance.getVarsRegistry().getNames(), startsWithFinder);
String varName = CollectionsUtils.find(CalculatorLocatorImpl.getInstance().getCalculatorEngine().getVarsRegistry().getNames(), startsWithFinder);
if (varName != null) {
final IConstant var = CalculatorEngine.instance.getVarsRegistry().get(varName);
final IConstant var = CalculatorLocatorImpl.getInstance().getCalculatorEngine().getVarsRegistry().get(varName);
if (var != null) {
if (!var.isDefined()) {
undefinedVars.add(var);

View File

@ -140,6 +140,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
if (calculatorEventType.isOfType(CalculatorEventType.calculation_started, CalculatorEventType.calculation_result, CalculatorEventType.calculation_failed)) {
if ( calculatorEventData.isAfter(this.lastEventDataId) ) {
/*
switch (calculatorEventType) {
case calculation_started:
@ -149,6 +150,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
CalculatorLocatorImpl.getInstance().getCalculatorDisplay().get
CalculatorHistoryState.newInstance(new TextViewEditorAdapter(this.editor), display);
*/
this.lastEventDataId = calculatorEventData;
}

View File

@ -0,0 +1,200 @@
package org.solovyev.android.calculator;
import junit.framework.Assert;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Test;
/**
* User: Solovyev_S
* Date: 21.09.12
* Time: 12:44
*/
public class CalculatorEditorImplTest {
@NotNull
private CalculatorEditor calculatorEditor;
@Before
public void setUp() throws Exception {
this.calculatorEditor = new CalculatorEditorImpl(new CalculatorImpl());
}
@Test
public void testInsert() throws Exception {
CalculatorEditorViewState viewState = this.calculatorEditor.getViewState();
Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.insert("");
Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.insert("test");
Assert.assertEquals("test", viewState.getText());
Assert.assertEquals(4, viewState.getSelection());
viewState = this.calculatorEditor.insert("test");
Assert.assertEquals("testtest", viewState.getText());
Assert.assertEquals(8, viewState.getSelection());
viewState = this.calculatorEditor.insert("");
Assert.assertEquals("testtest", viewState.getText());
Assert.assertEquals(8, viewState.getSelection());
viewState = this.calculatorEditor.insert("1234567890");
Assert.assertEquals("testtest1234567890", viewState.getText());
Assert.assertEquals(18, viewState.getSelection());
viewState = this.calculatorEditor.moveCursorLeft();
viewState = this.calculatorEditor.insert("9");
Assert.assertEquals("testtest12345678990", viewState.getText());
Assert.assertEquals(18, viewState.getSelection());
viewState = this.calculatorEditor.setCursorOnStart();
viewState = this.calculatorEditor.insert("9");
Assert.assertEquals("9testtest12345678990", viewState.getText());
Assert.assertEquals(1, viewState.getSelection());
viewState = this.calculatorEditor.erase();
viewState = this.calculatorEditor.insert("9");
Assert.assertEquals("9testtest12345678990", viewState.getText());
Assert.assertEquals(1, viewState.getSelection());
}
@Test
public void testErase() throws Exception {
this.calculatorEditor.setText("");
this.calculatorEditor.erase();
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
this.calculatorEditor.setText("test");
this.calculatorEditor.erase();
Assert.assertEquals("tes", this.calculatorEditor.getViewState().getText());
this.calculatorEditor.erase();
Assert.assertEquals("te", this.calculatorEditor.getViewState().getText());
this.calculatorEditor.erase();
Assert.assertEquals("t", this.calculatorEditor.getViewState().getText());
this.calculatorEditor.erase();
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
this.calculatorEditor.erase();
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
this.calculatorEditor.setText("1234");
this.calculatorEditor.moveCursorLeft();
this.calculatorEditor.erase();
Assert.assertEquals("124", this.calculatorEditor.getViewState().getText());
this.calculatorEditor.erase();
Assert.assertEquals("14", this.calculatorEditor.getViewState().getText());
this.calculatorEditor.erase();
Assert.assertEquals("4", this.calculatorEditor.getViewState().getText());
this.calculatorEditor.setText("1");
this.calculatorEditor.moveCursorLeft();
this.calculatorEditor.erase();
Assert.assertEquals("1", this.calculatorEditor.getViewState().getText());
}
@Test
public void testMoveSelection() throws Exception {
this.calculatorEditor.setText("");
CalculatorEditorViewState viewState = this.calculatorEditor.moveSelection(0);
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(2);
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(100);
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-3);
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-100);
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("0123456789");
viewState = this.calculatorEditor.moveSelection(0);
Assert.assertEquals(10, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(1);
Assert.assertEquals(10, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-2);
Assert.assertEquals(8, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(1);
Assert.assertEquals(9, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-9);
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-10);
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(2);
Assert.assertEquals(2, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(2);
Assert.assertEquals(4, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-6);
Assert.assertEquals(0, viewState.getSelection());
}
@Test
public void testSetText() throws Exception {
CalculatorEditorViewState viewState = this.calculatorEditor.setText("test");
Assert.assertEquals("test", viewState.getText());
Assert.assertEquals(4, viewState.getSelection());
viewState = this.calculatorEditor.setText("testtest");
Assert.assertEquals("testtest", viewState.getText());
Assert.assertEquals(8, viewState.getSelection());
viewState = this.calculatorEditor.setText("");
Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("testtest", 0);
Assert.assertEquals("testtest", viewState.getText());
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("testtest", 2);
Assert.assertEquals("testtest", viewState.getText());
Assert.assertEquals(2, viewState.getSelection());
viewState = this.calculatorEditor.setText("", 0);
Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("", 3);
Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("", -3);
Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("test");
Assert.assertEquals("test", viewState.getText());
Assert.assertEquals(4, viewState.getSelection());
viewState = this.calculatorEditor.setText("", 2);
Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection());
}
}

View File

@ -537,10 +537,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
public void eraseButtonClickHandler(@NotNull View v) {
calculatorModel.doTextOperation(new CalculatorModel.TextOperation() {
@Override
public void doOperation(@NotNull EditText editor) {
if (editor.getSelectionStart() > 0) {
editor.getText().delete(editor.getSelectionStart() - 1, editor.getSelectionStart());
}
public void doOperation(@NotNull CalculatorEditor editor) {
editor.erase();
}
});
}
@ -564,10 +562,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
public void pasteButtonClickHandler(@NotNull View v) {
calculatorModel.doTextOperation(new CalculatorModel.TextOperation() {
@Override
public void doOperation(@NotNull EditText editor) {
public void doOperation(@NotNull CalculatorEditor editor) {
final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (clipboard.hasText()) {
editor.getText().insert(editor.getSelectionStart(), clipboard.getText());
editor.insert(String.valueOf(clipboard.getText()));
}
}
});
@ -754,15 +752,18 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
if ( dragDirection == DragDirection.left ) {
CalculatorModel.instance.doTextOperation(new CalculatorModel.TextOperation() {
@Override
public void doOperation(@NotNull EditText editor) {
final int cursorPosition = editor.getSelectionStart();
final StringBuilder text = new StringBuilder("(");
final String oldText = editor.getText().toString();
text.append(oldText.substring(0, cursorPosition));
text.append(")");
text.append(oldText.substring(cursorPosition));
editor.setText(text);
editor.setSelection(cursorPosition + 2);
public void doOperation(@NotNull CalculatorEditor editor) {
CalculatorEditorViewState viewState = editor.getViewState();
final int cursorPosition = viewState.getSelection();
final String oldText = viewState.getText();
final StringBuilder newText = new StringBuilder(oldText.length() + 2);
newText.append("(");
newText.append(oldText.substring(0, cursorPosition));
newText.append(")");
newText.append(oldText.substring(cursorPosition));
editor.setText(newText.toString(), cursorPosition + 2);
}
});
result = true;

View File

@ -14,7 +14,6 @@ import android.text.ClipboardManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.jetbrains.annotations.NotNull;
@ -37,7 +36,7 @@ import org.solovyev.common.text.StringUtils;
* Date: 9/12/11
* Time: 11:15 PM
*/
public enum CalculatorModel implements HistoryControl<CalculatorHistoryState>, CalculatorEngineControl {
public enum CalculatorModel implements HistoryControl<CalculatorHistoryState>, CalculatorEngineControl, CursorControl {
instance;
@ -271,7 +270,7 @@ public enum CalculatorModel implements HistoryControl<CalculatorHistoryState>, C
doTextOperation(new CalculatorModel.TextOperation() {
@Override
public void doOperation(@NotNull EditText editor) {
public void doOperation(@NotNull CalculatorEditor editor) {
int cursorPositionOffset = 0;
final StringBuilder textToBeInserted = new StringBuilder(text);
@ -296,16 +295,36 @@ public enum CalculatorModel implements HistoryControl<CalculatorHistoryState>, C
}
}
editor.getText().insert(editor.getSelectionStart(), textToBeInserted.toString());
editor.setSelection(editor.getSelectionStart() + cursorPositionOffset, editor.getSelectionEnd() + cursorPositionOffset);
editor.insert(textToBeInserted.toString());
editor.moveSelection(cursorPositionOffset);
}
}, delayEvaluate);
}
}
@Override
public void setCursorOnStart() {
this.editor.setCursorOnStart();
}
@Override
public void setCursorOnEnd() {
this.editor.setCursorOnEnd();
}
@Override
public void moveCursorLeft() {
this.editor.moveCursorLeft();
}
@Override
public void moveCursorRight() {
this.editor.moveCursorRight();
}
public static interface TextOperation {
void doOperation(@NotNull EditText editor);
void doOperation(@NotNull CalculatorEditor editor);
}

View File

@ -14,12 +14,13 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import com.google.ads.AdView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.ads.AdsController;
import org.solovyev.android.calculator.CalculatorEditor;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.CalculatorModel;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.jscl.JsclOperation;
@ -175,19 +176,8 @@ public abstract class AbstractHistoryActivity extends ListActivity {
}
public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState, @NotNull AbstractHistoryActivity activity) {
// before evaluating history item - clear display (in order to get Error message in display if evaluation fail)
CalculatorModel.instance.getDisplay().setText("");
CalculatorModel.instance.doTextOperation(new CalculatorModel.TextOperation() {
@Override
public void doOperation(@NotNull EditText editor) {
final EditorHistoryState editorState = historyState.getEditorState();
editor.setText(editorState.getText());
editor.setSelection(editorState.getCursorPosition());
}
}, false, historyState.getDisplayState().getJsclOperation(), true);
CalculatorModel.instance.setCursorOnEnd();
CalculatorLocatorImpl.getInstance().getCalculatorEditor().setText(StringUtils.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition())
activity.finish();
}