number formatting is working now
This commit is contained in:
@@ -22,11 +22,11 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.solovyev.common.gui.CursorControl;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.solovyev.common.gui.CursorControl;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
@@ -34,6 +34,9 @@ import org.solovyev.common.gui.CursorControl;
|
||||
*/
|
||||
public interface CalculatorEditor extends CalculatorEventListener {
|
||||
|
||||
@Nonnull
|
||||
String TAG = CalculatorEditor.class.getSimpleName();
|
||||
|
||||
void setView(@Nullable CalculatorEditorView view);
|
||||
|
||||
@Nonnull
|
||||
|
@@ -22,10 +22,41 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 13:46
|
||||
*/
|
||||
public interface CalculatorEditorChangeEventData extends Change<CalculatorEditorViewState> {
|
||||
public final class CalculatorEditorChangeEventData implements Change<CalculatorEditorViewState> {
|
||||
|
||||
@Nonnull
|
||||
private CalculatorEditorViewState oldState;
|
||||
|
||||
@Nonnull
|
||||
private CalculatorEditorViewState newState;
|
||||
|
||||
private CalculatorEditorChangeEventData(@Nonnull CalculatorEditorViewState oldState,
|
||||
@Nonnull CalculatorEditorViewState newState) {
|
||||
this.oldState = oldState;
|
||||
this.newState = newState;
|
||||
}
|
||||
|
||||
public static CalculatorEditorChangeEventData newChangeEventData(@Nonnull CalculatorEditorViewState oldState,
|
||||
@Nonnull CalculatorEditorViewState newState) {
|
||||
return new CalculatorEditorChangeEventData(oldState, newState);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorEditorViewState getOldValue() {
|
||||
return this.oldState;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorEditorViewState getNewValue() {
|
||||
return this.newState;
|
||||
}
|
||||
}
|
||||
|
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 21.09.12
|
||||
* Time: 13:46
|
||||
*/
|
||||
public class CalculatorEditorChangeEventDataImpl implements CalculatorEditorChangeEventData {
|
||||
|
||||
@Nonnull
|
||||
private CalculatorEditorViewState oldState;
|
||||
|
||||
@Nonnull
|
||||
private CalculatorEditorViewState newState;
|
||||
|
||||
public CalculatorEditorChangeEventDataImpl(@Nonnull CalculatorEditorViewState oldState,
|
||||
@Nonnull CalculatorEditorViewState newState) {
|
||||
this.oldState = oldState;
|
||||
this.newState = newState;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorEditorViewState getOldValue() {
|
||||
return this.oldState;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorEditorViewState getNewValue() {
|
||||
return this.newState;
|
||||
}
|
||||
}
|
@@ -22,13 +22,19 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.solovyev.android.calculator.history.CalculatorHistoryState;
|
||||
import org.solovyev.android.calculator.history.EditorHistoryState;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||
import org.solovyev.common.gui.CursorControl;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.solovyev.android.calculator.history.CalculatorHistoryState;
|
||||
import org.solovyev.android.calculator.history.EditorHistoryState;
|
||||
import org.solovyev.common.gui.CursorControl;
|
||||
import org.solovyev.common.text.Strings;
|
||||
import static org.solovyev.android.calculator.CalculatorEditorChangeEventData.newChangeEventData;
|
||||
import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed;
|
||||
import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed_light;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
@@ -55,8 +61,12 @@ public class CalculatorEditorImpl implements CalculatorEditor {
|
||||
@Nonnull
|
||||
private final CursorControlAdapter cursorControlAdapter = new CursorControlAdapter(this);
|
||||
|
||||
public CalculatorEditorImpl(@Nonnull Calculator calculator) {
|
||||
@Nullable
|
||||
private final TextProcessor<TextProcessorEditorResult, String> textProcessor;
|
||||
|
||||
public CalculatorEditorImpl(@Nonnull Calculator calculator, @Nullable TextProcessor<TextProcessorEditorResult, String> textProcessor) {
|
||||
this.calculator = calculator;
|
||||
this.textProcessor = textProcessor;
|
||||
this.calculator.addCalculatorEventListener(this);
|
||||
this.lastEventHolder = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
}
|
||||
@@ -89,6 +99,14 @@ public class CalculatorEditorImpl implements CalculatorEditor {
|
||||
}
|
||||
|
||||
private void setViewState(@Nonnull CalculatorEditorViewState newViewState, boolean majorChanges) {
|
||||
if (textProcessor != null) {
|
||||
try {
|
||||
final TextProcessorEditorResult result = textProcessor.process(newViewState.getText());
|
||||
newViewState = CalculatorEditorViewStateImpl.newInstance(result.getCharSequence(), newViewState.getSelection() + result.getOffset());
|
||||
} catch (CalculatorParseException e) {
|
||||
Locator.getInstance().getLogger().error(TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
synchronized (viewLock) {
|
||||
final CalculatorEditorViewState oldViewState = this.lastViewState;
|
||||
|
||||
@@ -97,11 +115,17 @@ public class CalculatorEditorImpl implements CalculatorEditor {
|
||||
this.view.setState(newViewState);
|
||||
}
|
||||
|
||||
if (majorChanges) {
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState));
|
||||
} else {
|
||||
calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed_light, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState));
|
||||
}
|
||||
fireStateChangedEvent(majorChanges, oldViewState, newViewState);
|
||||
}
|
||||
}
|
||||
|
||||
private void fireStateChangedEvent(boolean majorChanges, @Nonnull CalculatorEditorViewState oldViewState, @Nonnull CalculatorEditorViewState newViewState) {
|
||||
assert Thread.holdsLock(viewLock);
|
||||
|
||||
if (majorChanges) {
|
||||
calculator.fireCalculatorEvent(editor_state_changed, newChangeEventData(oldViewState, newViewState));
|
||||
} else {
|
||||
calculator.fireCalculatorEvent(editor_state_changed_light, newChangeEventData(oldViewState, newViewState));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -36,5 +36,8 @@ public interface CalculatorEditorViewState extends Serializable {
|
||||
@Nonnull
|
||||
String getText();
|
||||
|
||||
@Nonnull
|
||||
CharSequence getTextAsCharSequence();
|
||||
|
||||
int getSelection();
|
||||
}
|
||||
|
@@ -32,7 +32,7 @@ import javax.annotation.Nonnull;
|
||||
public class CalculatorEditorViewStateImpl implements CalculatorEditorViewState {
|
||||
|
||||
@Nonnull
|
||||
private String text = "";
|
||||
private CharSequence text = "";
|
||||
|
||||
private int selection = 0;
|
||||
|
||||
@@ -47,6 +47,12 @@ public class CalculatorEditorViewStateImpl implements CalculatorEditorViewState
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getText() {
|
||||
return this.text.toString();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CharSequence getTextAsCharSequence() {
|
||||
return this.text;
|
||||
}
|
||||
|
||||
@@ -70,7 +76,7 @@ public class CalculatorEditorViewStateImpl implements CalculatorEditorViewState
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static CalculatorEditorViewState newInstance(@Nonnull String text, int selection) {
|
||||
public static CalculatorEditorViewState newInstance(@Nonnull CharSequence text, int selection) {
|
||||
final CalculatorEditorViewStateImpl result = new CalculatorEditorViewStateImpl();
|
||||
result.text = text;
|
||||
result.selection = selection;
|
||||
|
@@ -22,11 +22,14 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistory;
|
||||
import org.solovyev.android.calculator.plot.CalculatorPlotter;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
@@ -44,7 +47,8 @@ public interface CalculatorLocator {
|
||||
@Nonnull CalculatorPreferenceService preferenceService,
|
||||
@Nonnull CalculatorKeyboard keyboard,
|
||||
@Nonnull CalculatorExternalListenersContainer externalListenersContainer,
|
||||
@Nonnull CalculatorPlotter plotter);
|
||||
@Nonnull CalculatorPlotter plotter,
|
||||
@Nullable TextProcessor<TextProcessorEditorResult, String> editorTextProcessor);
|
||||
|
||||
@Nonnull
|
||||
Calculator getCalculator();
|
||||
|
@@ -25,8 +25,11 @@ package org.solovyev.android.calculator;
|
||||
import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistory;
|
||||
import org.solovyev.android.calculator.plot.CalculatorPlotter;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
@@ -87,7 +90,8 @@ public class Locator implements CalculatorLocator {
|
||||
@Nonnull CalculatorPreferenceService preferenceService,
|
||||
@Nonnull CalculatorKeyboard keyboard,
|
||||
@Nonnull CalculatorExternalListenersContainer externalListenersContainer,
|
||||
@Nonnull CalculatorPlotter plotter) {
|
||||
@Nonnull CalculatorPlotter plotter,
|
||||
@Nullable TextProcessor<TextProcessorEditorResult, String> editorTextProcessor) {
|
||||
|
||||
this.calculator = calculator;
|
||||
this.calculatorEngine = engine;
|
||||
@@ -99,7 +103,7 @@ public class Locator implements CalculatorLocator {
|
||||
this.calculatorExternalListenersContainer = externalListenersContainer;
|
||||
this.calculatorPlotter = plotter;
|
||||
|
||||
calculatorEditor = new CalculatorEditorImpl(this.calculator);
|
||||
calculatorEditor = new CalculatorEditorImpl(this.calculator, editorTextProcessor);
|
||||
calculatorDisplay = new CalculatorDisplayImpl(this.calculator);
|
||||
calculatorKeyboard = keyboard;
|
||||
}
|
||||
|
@@ -0,0 +1,58 @@
|
||||
package org.solovyev.android.calculator.text;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 6/27/13
|
||||
* Time: 8:07 PM
|
||||
*/
|
||||
public final class TextProcessorEditorResult implements CharSequence {
|
||||
|
||||
@Nonnull
|
||||
private final CharSequence charSequence;
|
||||
|
||||
@Nullable
|
||||
private String string;
|
||||
|
||||
private final int offset;
|
||||
|
||||
public TextProcessorEditorResult(@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);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String toString() {
|
||||
if (string == null) {
|
||||
string = charSequence.toString();
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CharSequence getCharSequence() {
|
||||
return charSequence;
|
||||
}
|
||||
|
||||
public int getOffset() {
|
||||
return offset;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user