user exceptions fixed

This commit is contained in:
Sergey Solovyev 2012-12-03 14:38:56 +04:00
parent 061a559778
commit 6e09225366
3 changed files with 29 additions and 26 deletions

View File

@ -144,7 +144,8 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe
editorView.viewStateChange = true;
editorView.viewState = viewState;
editorView.setText(text, BufferType.EDITABLE);
editorView.setSelection(viewState.getSelection());
final int selection = CalculatorEditorImpl.correctSelection(viewState.getSelection(), editorView.getText());
editorView.setSelection(selection);
} finally {
editorView.viewStateChange = false;
}

View File

@ -269,11 +269,11 @@ public class CalculatorEditorImpl implements CalculatorEditor {
}
}
private int correctSelection(int selection, @NotNull String text) {
public static int correctSelection(int selection, @NotNull CharSequence text) {
return correctSelection(selection, text.length());
}
private int correctSelection(int selection, int textLength) {
public static int correctSelection(int selection, int textLength) {
int result = Math.max(selection, 0);
result = Math.min(result, textLength);
return result;

View File

@ -114,33 +114,35 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
@NotNull
@Override
public List<CalculatorHistoryState> getStates(boolean includeIntermediateStates) {
if (includeIntermediateStates) {
return getStates();
} else {
final List<CalculatorHistoryState> states = getStates();
synchronized (history) {
if (includeIntermediateStates) {
return getStates();
} else {
final List<CalculatorHistoryState> states = getStates();
final List<CalculatorHistoryState> result = new LinkedList<CalculatorHistoryState>();
final List<CalculatorHistoryState> result = new LinkedList<CalculatorHistoryState>();
CalculatorHistoryState laterState = null;
for (CalculatorHistoryState state : CollectionsUtils.reversed(states)) {
if ( laterState != null ) {
final String laterEditorText = laterState.getEditorState().getText();
final String editorText = state.getEditorState().getText();
if ( laterEditorText != null && editorText != null && isIntermediate(laterEditorText, editorText)) {
// intermediate result => skip from add
} else {
result.add(0, state);
}
} else {
result.add(0, state);
}
CalculatorHistoryState laterState = null;
for (CalculatorHistoryState state : CollectionsUtils.reversed(states)) {
if ( laterState != null ) {
final String laterEditorText = laterState.getEditorState().getText();
final String editorText = state.getEditorState().getText();
if ( laterEditorText != null && editorText != null && isIntermediate(laterEditorText, editorText)) {
// intermediate result => skip from add
} else {
result.add(0, state);
}
} else {
result.add(0, state);
}
laterState = state;
}
laterState = state;
}
return result;
}
}
return result;
}
}
}
private boolean isIntermediate(@NotNull String laterEditorText,
@NotNull String editorText) {