This commit is contained in:
serso 2016-01-08 10:09:24 +01:00
parent d1c068edff
commit e3c3abf828
52 changed files with 604 additions and 941 deletions

View File

@ -27,7 +27,7 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import jscl.NumeralBase; import jscl.NumeralBase;
import jscl.math.Generic; import jscl.math.Generic;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.HistoryState;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.history.HistoryAction; import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.msg.Message; import org.solovyev.common.msg.Message;
@ -156,12 +156,12 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S
@Override @Override
@Nonnull @Nonnull
public CalculatorHistoryState getCurrentHistoryState() { public HistoryState getCurrentHistoryState() {
return calculator.getCurrentHistoryState(); return calculator.getCurrentHistoryState();
} }
@Override @Override
public void setCurrentHistoryState(@Nonnull CalculatorHistoryState editorHistoryState) { public void setCurrentHistoryState(@Nonnull HistoryState editorHistoryState) {
calculator.setCurrentHistoryState(editorHistoryState); calculator.setCurrentHistoryState(editorHistoryState);
} }

View File

@ -68,7 +68,7 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
@Nonnull @Nonnull
private final Handler uiHandler = new Handler(); private final Handler uiHandler = new Handler();
@Nonnull @Nonnull
private volatile CalculatorDisplayViewState state = CalculatorDisplayViewStateImpl.newDefaultInstance(); private volatile DisplayState state = DisplayState.empty();
private volatile boolean initialized = false; private volatile boolean initialized = false;
/* /*
@ -109,14 +109,14 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
@Nonnull @Nonnull
@Override @Override
public CalculatorDisplayViewState getState() { public DisplayState getState() {
synchronized (lock) { synchronized (lock) {
return this.state; return this.state;
} }
} }
@Override @Override
public void setState(@Nonnull final CalculatorDisplayViewState state) { public void setState(@Nonnull final DisplayState state) {
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override

View File

@ -40,13 +40,12 @@ import org.solovyev.android.views.dragbutton.DragButton;
import org.solovyev.android.views.dragbutton.DragDirection; import org.solovyev.android.views.dragbutton.DragDirection;
import org.solovyev.android.views.dragbutton.DragListener; import org.solovyev.android.views.dragbutton.DragListener;
import org.solovyev.android.views.dragbutton.SimpleDragListener; import org.solovyev.android.views.dragbutton.SimpleDragListener;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.HistoryState;
import org.solovyev.android.calculator.history.HistoryDragProcessor; import org.solovyev.android.calculator.history.HistoryDragProcessor;
import org.solovyev.android.calculator.view.AngleUnitsButton; import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.calculator.view.LongClickEraser; import org.solovyev.android.calculator.view.LongClickEraser;
import org.solovyev.android.calculator.view.NumeralBasesButton; import org.solovyev.android.calculator.view.NumeralBasesButton;
import org.solovyev.android.calculator.view.ViewsCache; import org.solovyev.android.calculator.view.ViewsCache;
import org.solovyev.common.math.Point2d;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -166,7 +165,7 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan
final ViewsCache views = ViewsCache.forView(root); final ViewsCache views = ViewsCache.forView(root);
setOnDragListeners(views, activity); setOnDragListeners(views, activity);
HistoryDragProcessor<CalculatorHistoryState> historyDragProcessor = new HistoryDragProcessor<>(getCalculator()); HistoryDragProcessor<HistoryState> historyDragProcessor = new HistoryDragProcessor<>(getCalculator());
final DragListener historyDragListener = newDragListener(historyDragProcessor, activity); final DragListener historyDragListener = newDragListener(historyDragProcessor, activity);
final DragButton historyButton = getButton(views, R.id.cpp_button_history); final DragButton historyButton = getButton(views, R.id.cpp_button_history);
if (historyButton != null) { if (historyButton != null) {

View File

@ -22,7 +22,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.HistoryState;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.history.HistoryControl; import org.solovyev.common.history.HistoryControl;
@ -37,7 +37,7 @@ import jscl.math.Generic;
* Date: 20.09.12 * Date: 20.09.12
* Time: 16:38 * Time: 16:38
*/ */
public interface Calculator extends CalculatorEventContainer, HistoryControl<CalculatorHistoryState> { public interface Calculator extends CalculatorEventContainer, HistoryControl<HistoryState> {
void init(); void init();

View File

@ -130,7 +130,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener
public static void tryCreateVar(@Nonnull final Context context) { public static void tryCreateVar(@Nonnull final Context context) {
final CalculatorDisplay display = Locator.getInstance().getDisplay(); final CalculatorDisplay display = Locator.getInstance().getDisplay();
final CalculatorDisplayViewState viewState = display.getViewState(); final DisplayState viewState = display.getViewState();
if (viewState.isValid()) { if (viewState.isValid()) {
final String varValue = viewState.getText(); final String varValue = viewState.getText();
if (!Strings.isEmpty(varValue)) { if (!Strings.isEmpty(varValue)) {
@ -156,7 +156,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener
public static void tryCreateFunction(@Nonnull final Context context) { public static void tryCreateFunction(@Nonnull final Context context) {
final CalculatorDisplay display = Locator.getInstance().getDisplay(); final CalculatorDisplay display = Locator.getInstance().getDisplay();
final CalculatorDisplayViewState viewState = display.getViewState(); final DisplayState viewState = display.getViewState();
if (viewState.isValid()) { if (viewState.isValid()) {
final String functionValue = viewState.getText(); final String functionValue = viewState.getText();
@ -180,7 +180,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener
public static void tryPlot() { public static void tryPlot() {
final CalculatorPlotter plotter = Locator.getInstance().getPlotter(); final CalculatorPlotter plotter = Locator.getInstance().getPlotter();
final CalculatorDisplay display = Locator.getInstance().getDisplay(); final CalculatorDisplay display = Locator.getInstance().getDisplay();
final CalculatorDisplayViewState viewState = display.getViewState(); final DisplayState viewState = display.getViewState();
if (viewState.isValid()) { if (viewState.isValid()) {
final String functionValue = viewState.getText(); final String functionValue = viewState.getText();

View File

@ -36,7 +36,7 @@ public interface CalculatorConversionEventData extends CalculatorEventData {
// display state on the moment of conversion // display state on the moment of conversion
@Nonnull @Nonnull
CalculatorDisplayViewState getDisplayState(); DisplayState getDisplayState();
@Nonnull @Nonnull
NumeralBase getFromNumeralBase(); NumeralBase getFromNumeralBase();

View File

@ -47,7 +47,7 @@ public class CalculatorConversionEventDataImpl implements CalculatorConversionEv
private Generic value; private Generic value;
@Nonnull @Nonnull
private CalculatorDisplayViewState displayState; private DisplayState displayState;
private CalculatorConversionEventDataImpl() { private CalculatorConversionEventDataImpl() {
} }
@ -57,7 +57,7 @@ public class CalculatorConversionEventDataImpl implements CalculatorConversionEv
@Nonnull Generic value, @Nonnull Generic value,
@Nonnull NumeralBase from, @Nonnull NumeralBase from,
@Nonnull NumeralBase to, @Nonnull NumeralBase to,
@Nonnull CalculatorDisplayViewState displayViewState) { @Nonnull DisplayState displayViewState) {
final CalculatorConversionEventDataImpl result = new CalculatorConversionEventDataImpl(); final CalculatorConversionEventDataImpl result = new CalculatorConversionEventDataImpl();
result.calculatorEventData = calculatorEventData; result.calculatorEventData = calculatorEventData;
@ -102,7 +102,7 @@ public class CalculatorConversionEventDataImpl implements CalculatorConversionEv
@Nonnull @Nonnull
@Override @Override
public CalculatorDisplayViewState getDisplayState() { public DisplayState getDisplayState() {
return this.displayState; return this.displayState;
} }

View File

@ -40,9 +40,9 @@ public interface CalculatorDisplay extends CalculatorEventListener {
void setView(@Nonnull CalculatorDisplayView view); void setView(@Nonnull CalculatorDisplayView view);
@Nonnull @Nonnull
CalculatorDisplayViewState getViewState(); DisplayState getViewState();
void setViewState(@Nonnull CalculatorDisplayViewState viewState); void setViewState(@Nonnull DisplayState viewState);
@Nonnull @Nonnull
CalculatorEventData getLastEventData(); CalculatorEventData getLastEventData();

View File

@ -27,5 +27,5 @@ package org.solovyev.android.calculator;
* Date: 9/21/12 * Date: 9/21/12
* Time: 9:49 PM * Time: 9:49 PM
*/ */
public interface CalculatorDisplayChangeEventData extends Change<CalculatorDisplayViewState> { public interface CalculatorDisplayChangeEventData extends Change<DisplayState> {
} }

View File

@ -32,25 +32,25 @@ import javax.annotation.Nonnull;
public class CalculatorDisplayChangeEventDataImpl implements CalculatorDisplayChangeEventData { public class CalculatorDisplayChangeEventDataImpl implements CalculatorDisplayChangeEventData {
@Nonnull @Nonnull
private final CalculatorDisplayViewState oldState; private final DisplayState oldState;
@Nonnull @Nonnull
private final CalculatorDisplayViewState newState; private final DisplayState newState;
public CalculatorDisplayChangeEventDataImpl(@Nonnull CalculatorDisplayViewState oldState, @Nonnull CalculatorDisplayViewState newState) { public CalculatorDisplayChangeEventDataImpl(@Nonnull DisplayState oldState, @Nonnull DisplayState newState) {
this.oldState = oldState; this.oldState = oldState;
this.newState = newState; this.newState = newState;
} }
@Nonnull @Nonnull
@Override @Override
public CalculatorDisplayViewState getOldValue() { public DisplayState getOldValue() {
return this.oldState; return this.oldState;
} }
@Nonnull @Nonnull
@Override @Override
public CalculatorDisplayViewState getNewValue() { public DisplayState getNewValue() {
return this.newState; return this.newState;
} }
} }

View File

@ -32,11 +32,6 @@ import static org.solovyev.android.calculator.CalculatorEventType.conversion_fai
import static org.solovyev.android.calculator.CalculatorEventType.conversion_result; import static org.solovyev.android.calculator.CalculatorEventType.conversion_result;
import static org.solovyev.android.calculator.CalculatorEventType.display_state_changed; import static org.solovyev.android.calculator.CalculatorEventType.display_state_changed;
/**
* User: serso
* Date: 9/20/12
* Time: 8:24 PM
*/
public class CalculatorDisplayImpl implements CalculatorDisplay { public class CalculatorDisplayImpl implements CalculatorDisplay {
@Nonnull @Nonnull
@ -48,7 +43,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
@Nullable @Nullable
private CalculatorDisplayView view; private CalculatorDisplayView view;
@Nonnull @Nonnull
private CalculatorDisplayViewState viewState = CalculatorDisplayViewStateImpl.newDefaultInstance(); private DisplayState viewState = DisplayState.empty();
public CalculatorDisplayImpl(@Nonnull Calculator calculator) { public CalculatorDisplayImpl(@Nonnull Calculator calculator) {
this.calculator = calculator; this.calculator = calculator;
@ -81,22 +76,22 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
@Nonnull @Nonnull
@Override @Override
public CalculatorDisplayViewState getViewState() { public DisplayState getViewState() {
return this.viewState; return this.viewState;
} }
@Override @Override
public void setViewState(@Nonnull CalculatorDisplayViewState newViewState) { public void setViewState(@Nonnull DisplayState newViewState) {
synchronized (viewLock) { synchronized (viewLock) {
final CalculatorDisplayViewState oldViewState = setViewState0(newViewState); final DisplayState oldViewState = setViewState0(newViewState);
this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState)); this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState));
} }
} }
private void setViewStateForSequence(@Nonnull CalculatorDisplayViewState newViewState, @Nonnull Long sequenceId) { private void setViewStateForSequence(@Nonnull DisplayState newViewState, @Nonnull Long sequenceId) {
synchronized (viewLock) { synchronized (viewLock) {
final CalculatorDisplayViewState oldViewState = setViewState0(newViewState); final DisplayState oldViewState = setViewState0(newViewState);
this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState), sequenceId); this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState), sequenceId);
} }
@ -104,8 +99,8 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
// must be synchronized with viewLock // must be synchronized with viewLock
@Nonnull @Nonnull
private CalculatorDisplayViewState setViewState0(@Nonnull CalculatorDisplayViewState newViewState) { private DisplayState setViewState0(@Nonnull DisplayState newViewState) {
final CalculatorDisplayViewState oldViewState = this.viewState; final DisplayState oldViewState = this.viewState;
this.viewState = newViewState; this.viewState = newViewState;
if (this.view != null) { if (this.view != null) {
@ -152,7 +147,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
private void processConversationFailed(@Nonnull CalculatorConversionEventData calculatorEventData, private void processConversationFailed(@Nonnull CalculatorConversionEventData calculatorEventData,
@Nonnull ConversionFailure data) { @Nonnull ConversionFailure data) {
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getDisplayState().getOperation(), CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error)), calculatorEventData.getSequenceId()); this.setViewStateForSequence(DisplayState.createError(calculatorEventData.getDisplayState().getOperation(), CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error)), calculatorEventData.getSequenceId());
} }
@ -172,18 +167,18 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
} }
} }
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId()); this.setViewStateForSequence(DisplayState.createError(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId());
} }
private void processCalculationCancelled(@Nonnull CalculatorEvaluationEventData calculatorEventData) { private void processCalculationCancelled(@Nonnull CalculatorEvaluationEventData calculatorEventData) {
final String errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error); final String errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId()); this.setViewStateForSequence(DisplayState.createError(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId());
} }
private void processCalculationResult(@Nonnull CalculatorEvaluationEventData calculatorEventData, @Nonnull CalculatorOutput data) { private void processCalculationResult(@Nonnull CalculatorEvaluationEventData calculatorEventData, @Nonnull CalculatorOutput data) {
final String stringResult = data.getStringResult(); final String stringResult = data.getStringResult();
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(calculatorEventData.getOperation(), data.getResult(), stringResult, 0), calculatorEventData.getSequenceId()); this.setViewStateForSequence(DisplayState.createValid(calculatorEventData.getOperation(), data.getResult(), stringResult, 0), calculatorEventData.getSequenceId());
} }
private void processConversationResult(@Nonnull CalculatorConversionEventData calculatorEventData, @Nonnull String result) { private void processConversationResult(@Nonnull CalculatorConversionEventData calculatorEventData, @Nonnull String result) {
@ -192,7 +187,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
result = calculatorEventData.getToNumeralBase().getJsclPrefix() + result; result = calculatorEventData.getToNumeralBase().getJsclPrefix() + result;
} }
final CalculatorDisplayViewState displayState = calculatorEventData.getDisplayState(); final DisplayState displayState = calculatorEventData.getDisplayState();
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(displayState.getOperation(), displayState.getResult(), result, 0), calculatorEventData.getSequenceId()); this.setViewStateForSequence(DisplayState.createValid(displayState.getOperation(), displayState.getResult(), result, 0), calculatorEventData.getSequenceId());
} }
} }

View File

@ -38,18 +38,18 @@ import jscl.math.Generic;
* Date: 21.09.12 * Date: 21.09.12
* Time: 10:55 * Time: 10:55
*/ */
public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDisplayViewState> { public enum CalculatorDisplayMenuItem implements LabeledMenuItem<DisplayState> {
copy(R.string.c_copy) { copy(R.string.c_copy) {
@Override @Override
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) { public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
Locator.getInstance().getKeyboard().copyButtonPressed(); Locator.getInstance().getKeyboard().copyButtonPressed();
} }
}, },
convert_to_bin(R.string.convert_to_bin) { convert_to_bin(R.string.convert_to_bin) {
@Override @Override
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) { public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
ConversionMenuItem.convert_to_bin.onClick(data, context); ConversionMenuItem.convert_to_bin.onClick(data, context);
} }
@ -61,7 +61,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDispl
convert_to_dec(R.string.convert_to_dec) { convert_to_dec(R.string.convert_to_dec) {
@Override @Override
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) { public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
ConversionMenuItem.convert_to_dec.onClick(data, context); ConversionMenuItem.convert_to_dec.onClick(data, context);
} }
@ -73,7 +73,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDispl
convert_to_hex(R.string.convert_to_hex) { convert_to_hex(R.string.convert_to_hex) {
@Override @Override
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) { public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
ConversionMenuItem.convert_to_hex.onClick(data, context); ConversionMenuItem.convert_to_hex.onClick(data, context);
} }
@ -85,7 +85,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDispl
convert(R.string.c_convert) { convert(R.string.c_convert) {
@Override @Override
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) { public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
final Generic result = data.getResult(); final Generic result = data.getResult();
if (result != null) { if (result != null) {
new NumeralBaseConverterDialog(result.toString()).show(context); new NumeralBaseConverterDialog(result.toString()).show(context);
@ -100,7 +100,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDispl
plot(R.string.c_plot) { plot(R.string.c_plot) {
@Override @Override
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) { public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
final Generic expression = data.getResult(); final Generic expression = data.getResult();
if (expression == null) throw new AssertionError(); if (expression == null) throw new AssertionError();
@ -121,7 +121,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDispl
this.captionId = captionId; this.captionId = captionId;
} }
public final boolean isItemVisible(@Nonnull CalculatorDisplayViewState displayViewState) { public final boolean isItemVisible(@Nonnull DisplayState displayViewState) {
//noinspection ConstantConditions //noinspection ConstantConditions
return displayViewState.isValid() && displayViewState.getResult() != null && isItemVisibleFor(displayViewState.getResult(), displayViewState.getOperation()); return displayViewState.isValid() && displayViewState.getResult() != null && isItemVisibleFor(displayViewState.getResult(), displayViewState.getOperation());
} }

View File

@ -52,7 +52,7 @@ public class CalculatorDisplayOnClickListener implements View.OnClickListener {
if (v instanceof CalculatorDisplayView) { if (v instanceof CalculatorDisplayView) {
final CalculatorDisplay cd = Locator.getInstance().getDisplay(); final CalculatorDisplay cd = Locator.getInstance().getDisplay();
final CalculatorDisplayViewState displayViewState = cd.getViewState(); final DisplayState displayViewState = cd.getViewState();
if (displayViewState.isValid()) { if (displayViewState.isValid()) {
final List<CalculatorDisplayMenuItem> filteredMenuItems = new ArrayList<CalculatorDisplayMenuItem>(CalculatorDisplayMenuItem.values().length); final List<CalculatorDisplayMenuItem> filteredMenuItems = new ArrayList<CalculatorDisplayMenuItem>(CalculatorDisplayMenuItem.values().length);

View File

@ -32,7 +32,7 @@ import javax.annotation.Nonnull;
public interface CalculatorDisplayView { public interface CalculatorDisplayView {
@Nonnull @Nonnull
CalculatorDisplayViewState getState(); DisplayState getState();
void setState(@Nonnull CalculatorDisplayViewState state); void setState(@Nonnull DisplayState state);
} }

View File

@ -1,59 +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 org.solovyev.android.calculator.jscl.JsclOperation;
import java.io.Serializable;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.math.Generic;
/**
* User: serso
* Date: 9/20/12
* Time: 9:50 PM
*/
public interface CalculatorDisplayViewState extends Serializable {
@Nonnull
String getText();
int getSelection();
@Nullable
Generic getResult();
boolean isValid();
@Nullable
String getErrorMessage();
@Nonnull
JsclOperation getOperation();
@Nullable
String getStringResult();
}

View File

@ -1,117 +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 org.solovyev.common.gui.CursorControl;
import javax.annotation.Nonnull;
/**
* User: Solovyev_S
* Date: 21.09.12
* Time: 11:47
*/
public interface CalculatorEditor extends CalculatorEventListener {
@Nonnull
String TAG = CalculatorEditor.class.getSimpleName();
void setView(@Nonnull EditorView view);
void clearView(@Nonnull EditorView view);
@Nonnull
EditorState getViewState();
void setViewState(@Nonnull EditorState viewState);
// updates state of view (view.setState())
void updateViewState();
/*
**********************************************************************
*
* CURSOR CONTROL
*
**********************************************************************
*/
/**
* Method sets the cursor to the beginning
*/
@Nonnull
EditorState setCursorOnStart();
/**
* Method sets the cursor to the end
*/
@Nonnull
EditorState setCursorOnEnd();
/**
* Method moves cursor to the left of current position
*/
@Nonnull
EditorState moveCursorLeft();
/**
* Method moves cursor to the right of current position
*/
@Nonnull
EditorState moveCursorRight();
@Nonnull
CursorControl asCursorControl();
/*
**********************************************************************
*
* EDITOR OPERATIONS
*
**********************************************************************
*/
@Nonnull
EditorState erase();
@Nonnull
EditorState clear();
@Nonnull
EditorState setText(@Nonnull String text);
@Nonnull
EditorState setText(@Nonnull String text, int selection);
@Nonnull
EditorState insert(@Nonnull String text);
@Nonnull
EditorState insert(@Nonnull String text, int selectionOffset);
@Nonnull
EditorState moveSelection(int offset);
@Nonnull
EditorState setSelection(int selection);
}

View File

@ -1,356 +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 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 static java.lang.Math.min;
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
* Date: 21.09.12
* Time: 11:53
*/
public class CalculatorEditorImpl implements CalculatorEditor {
@Nonnull
private final Object viewLock = new Object();
@Nonnull
private final Calculator calculator;
@Nonnull
private final CalculatorEventHolder lastEventHolder;
@Nonnull
private final CursorControlAdapter cursorControlAdapter = new CursorControlAdapter(this);
@Nullable
private final TextProcessor<TextProcessorEditorResult, String> textProcessor;
@Nullable
private EditorView view;
@Nonnull
private EditorState lastViewState = EditorState.empty();
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());
}
public static int clamp(int selection, @Nonnull CharSequence text) {
return clamp(selection, text.length());
}
public static int clamp(int selection, int max) {
return min(Math.max(selection, 0), max);
}
@Override
public void setView(@Nonnull EditorView view) {
synchronized (viewLock) {
this.view = view;
this.view.setState(lastViewState);
}
}
@Override
public void clearView(@Nonnull EditorView view) {
synchronized (viewLock) {
if (this.view == view) {
this.view = null;
}
}
}
@Nonnull
@Override
public EditorState getViewState() {
return lastViewState;
}
@Override
public void setViewState(@Nonnull EditorState newViewState) {
setViewState(newViewState, true);
}
@Override
public void updateViewState() {
setViewState(this.lastViewState, false);
}
private void setViewState(@Nonnull EditorState newViewState, boolean majorChanges) {
if (textProcessor != null) {
try {
final TextProcessorEditorResult result = textProcessor.process(newViewState.getText());
newViewState = EditorState.create(result.getCharSequence(), newViewState.getSelection() + result.getOffset());
} catch (CalculatorParseException e) {
Locator.getInstance().getLogger().error(TAG, e.getMessage(), e);
}
}
synchronized (viewLock) {
final EditorState oldViewState = this.lastViewState;
this.lastViewState = newViewState;
if (this.view != null) {
this.view.setState(newViewState);
}
fireStateChangedEvent(majorChanges, oldViewState, newViewState);
}
}
/*
**********************************************************************
*
* SELECTION
*
**********************************************************************
*/
private void fireStateChangedEvent(boolean majorChanges, @Nonnull EditorState oldViewState, @Nonnull EditorState newViewState) {
if (!Thread.holdsLock(viewLock)) throw new AssertionError();
if (majorChanges) {
calculator.fireCalculatorEvent(editor_state_changed, newChangeEventData(oldViewState, newViewState));
} else {
calculator.fireCalculatorEvent(editor_state_changed_light, newChangeEventData(oldViewState, newViewState));
}
}
@Override
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData,
@Nonnull CalculatorEventType calculatorEventType,
@Nullable Object data) {
final CalculatorEventHolder.Result result = lastEventHolder.apply(calculatorEventData);
if (result.isNewAfter()) {
switch (calculatorEventType) {
case use_history_state:
final CalculatorHistoryState calculatorHistoryState = (CalculatorHistoryState) data;
final EditorHistoryState editorState = calculatorHistoryState.getEditorState();
this.setText(Strings.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition());
break;
}
}
}
@Nonnull
private EditorState newSelectionViewState(int newSelection) {
if (this.lastViewState.getSelection() != newSelection) {
final EditorState result = EditorState.newSelection(this.lastViewState, newSelection);
setViewState(result, false);
return result;
} else {
return this.lastViewState;
}
}
@Nonnull
public EditorState setCursorOnStart() {
synchronized (viewLock) {
return newSelectionViewState(0);
}
}
@Nonnull
public EditorState setCursorOnEnd() {
synchronized (viewLock) {
return newSelectionViewState(this.lastViewState.getText().length());
}
}
@Nonnull
public EditorState moveCursorLeft() {
synchronized (viewLock) {
if (this.lastViewState.getSelection() > 0) {
return newSelectionViewState(this.lastViewState.getSelection() - 1);
} else {
return this.lastViewState;
}
}
}
/*
**********************************************************************
*
* EDITOR ACTIONS
*
**********************************************************************
*/
@Nonnull
public EditorState moveCursorRight() {
synchronized (viewLock) {
if (this.lastViewState.getSelection() < this.lastViewState.getText().length()) {
return newSelectionViewState(this.lastViewState.getSelection() + 1);
} else {
return this.lastViewState;
}
}
}
@Nonnull
@Override
public CursorControl asCursorControl() {
return cursorControlAdapter;
}
@Nonnull
@Override
public EditorState 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 EditorState result = EditorState.create(newText.toString(), selection - 1);
setViewState(result);
return result;
} else {
return this.lastViewState;
}
}
}
@Nonnull
@Override
public EditorState clear() {
synchronized (viewLock) {
return setText("");
}
}
@Nonnull
@Override
public EditorState setText(@Nonnull String text) {
synchronized (viewLock) {
final EditorState result = EditorState.create(text, text.length());
setViewState(result);
return result;
}
}
@Nonnull
@Override
public EditorState setText(@Nonnull String text, int selection) {
synchronized (viewLock) {
selection = clamp(selection, text);
final EditorState result = EditorState.create(text, selection);
setViewState(result);
return result;
}
}
@Nonnull
@Override
public EditorState insert(@Nonnull String text) {
synchronized (viewLock) {
return insert(text, 0);
}
}
@Nonnull
@Override
public EditorState insert(@Nonnull String text, int selectionOffset) {
synchronized (viewLock) {
final String oldText = lastViewState.getText();
final int selection = clamp(lastViewState.getSelection(), oldText);
int newTextLength = text.length() + oldText.length();
final StringBuilder newText = new StringBuilder(newTextLength);
newText.append(oldText.substring(0, selection));
newText.append(text);
newText.append(oldText.substring(selection));
int newSelection = clamp(text.length() + selection + selectionOffset, newTextLength);
final EditorState result = EditorState.create(newText.toString(), newSelection);
setViewState(result);
return result;
}
}
@Nonnull
@Override
public EditorState moveSelection(int offset) {
synchronized (viewLock) {
int selection = this.lastViewState.getSelection() + offset;
return setSelection(selection);
}
}
@Nonnull
@Override
public EditorState setSelection(int selection) {
synchronized (viewLock) {
selection = clamp(selection, this.lastViewState.getText());
final EditorState result = EditorState.newSelection(this.lastViewState, selection);
setViewState(result, false);
return result;
}
}
private static final class CursorControlAdapter implements CursorControl {
@Nonnull
private final CalculatorEditor calculatorEditor;
private CursorControlAdapter(@Nonnull CalculatorEditor calculatorEditor) {
this.calculatorEditor = calculatorEditor;
}
@Override
public void setCursorOnStart() {
this.calculatorEditor.setCursorOnStart();
}
@Override
public void setCursorOnEnd() {
this.calculatorEditor.setCursorOnEnd();
}
@Override
public void moveCursorLeft() {
this.calculatorEditor.moveCursorLeft();
}
@Override
public void moveCursorRight() {
this.calculatorEditor.moveCursorRight();
}
}
}

View File

@ -31,7 +31,7 @@ import jscl.math.function.IConstant;
import jscl.math.operator.Operator; import jscl.math.operator.Operator;
import jscl.text.ParseInterruptedException; import jscl.text.ParseInterruptedException;
import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistory;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.HistoryState;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.text.TextProcessor;
@ -173,21 +173,21 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Override @Override
public void evaluate() { public void evaluate() {
final EditorState viewState = getEditor().getViewState(); final EditorState viewState = getEditor().getState();
final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState); final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
this.evaluate(JsclOperation.numeric, viewState.getText(), eventData.getSequenceId()); this.evaluate(JsclOperation.numeric, viewState.getText(), eventData.getSequenceId());
} }
@Override @Override
public void evaluate(@Nonnull Long sequenceId) { public void evaluate(@Nonnull Long sequenceId) {
final EditorState viewState = getEditor().getViewState(); final EditorState viewState = getEditor().getState();
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState, sequenceId); fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState, sequenceId);
this.evaluate(JsclOperation.numeric, viewState.getText(), sequenceId); this.evaluate(JsclOperation.numeric, viewState.getText(), sequenceId);
} }
@Override @Override
public void simplify() { public void simplify() {
final EditorState viewState = getEditor().getViewState(); final EditorState viewState = getEditor().getState();
final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState); final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
this.evaluate(JsclOperation.simplify, viewState.getText(), eventData.getSequenceId()); this.evaluate(JsclOperation.simplify, viewState.getText(), eventData.getSequenceId());
} }
@ -250,7 +250,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Nonnull Generic value, @Nonnull Generic value,
@Nonnull NumeralBase from, @Nonnull NumeralBase from,
@Nonnull NumeralBase to, @Nonnull NumeralBase to,
@Nonnull CalculatorDisplayViewState displayViewState) { @Nonnull DisplayState displayViewState) {
return CalculatorConversionEventDataImpl.newInstance(nextEventData(sequenceId), value, from, to, displayViewState); return CalculatorConversionEventDataImpl.newInstance(nextEventData(sequenceId), value, from, to, displayViewState);
} }
@ -393,7 +393,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Nonnull final NumeralBase to) { @Nonnull final NumeralBase to) {
final CalculatorEventData eventDataId = nextEventData(); final CalculatorEventData eventDataId = nextEventData();
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState(); final DisplayState displayViewState = Locator.getInstance().getDisplay().getViewState();
final NumeralBase from = Locator.getInstance().getEngine().getNumeralBase(); final NumeralBase from = Locator.getInstance().getEngine().getNumeralBase();
calculationsExecutor.execute(new Runnable() { calculationsExecutor.execute(new Runnable() {
@ -562,7 +562,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
} }
private void onDisplayStateChanged(@Nonnull CalculatorDisplayChangeEventData displayChangeEventData) { private void onDisplayStateChanged(@Nonnull CalculatorDisplayChangeEventData displayChangeEventData) {
final CalculatorDisplayViewState newState = displayChangeEventData.getNewValue(); final DisplayState newState = displayChangeEventData.getNewValue();
if (newState.isValid()) { if (newState.isValid()) {
final String result = newState.getStringResult(); final String result = newState.getStringResult();
if (!Strings.isEmpty(result)) { if (!Strings.isEmpty(result)) {
@ -597,7 +597,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
public void doHistoryAction(@Nonnull HistoryAction historyAction) { public void doHistoryAction(@Nonnull HistoryAction historyAction) {
final CalculatorHistory history = Locator.getInstance().getHistory(); final CalculatorHistory history = Locator.getInstance().getHistory();
if (history.isActionAvailable(historyAction)) { if (history.isActionAvailable(historyAction)) {
final CalculatorHistoryState newState = history.doAction(historyAction, getCurrentHistoryState()); final HistoryState newState = history.doAction(historyAction, getCurrentHistoryState());
if (newState != null) { if (newState != null) {
setCurrentHistoryState(newState); setCurrentHistoryState(newState);
} }
@ -606,12 +606,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Nonnull @Nonnull
@Override @Override
public CalculatorHistoryState getCurrentHistoryState() { public HistoryState getCurrentHistoryState() {
return CalculatorHistoryState.newInstance(getEditor(), getDisplay()); return HistoryState.create(getEditor(), getDisplay());
} }
@Override @Override
public void setCurrentHistoryState(@Nonnull CalculatorHistoryState editorHistoryState) { public void setCurrentHistoryState(@Nonnull HistoryState editorHistoryState) {
editorHistoryState.setValuesFromHistory(getEditor(), getDisplay()); editorHistoryState.setValuesFromHistory(getEditor(), getDisplay());
} }
@ -624,7 +624,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
*/ */
@Nonnull @Nonnull
private CalculatorEditor getEditor() { private Editor getEditor() {
return Locator.getInstance().getEditor(); return Locator.getInstance().getEditor();
} }

View File

@ -83,7 +83,7 @@ public class CalculatorKeyboardImpl implements CalculatorKeyboard {
} }
} }
final CalculatorEditor editor = Locator.getInstance().getEditor(); final Editor editor = Locator.getInstance().getEditor();
editor.insert(textToBeInserted.toString(), cursorPositionOffset); editor.insert(textToBeInserted.toString(), cursorPositionOffset);
} }
@ -110,8 +110,8 @@ public class CalculatorKeyboardImpl implements CalculatorKeyboard {
@Override @Override
public void roundBracketsButtonPressed() { public void roundBracketsButtonPressed() {
final CalculatorEditor editor = Locator.getInstance().getEditor(); final Editor editor = Locator.getInstance().getEditor();
EditorState viewState = editor.getViewState(); EditorState viewState = editor.getState();
final int cursorPosition = viewState.getSelection(); final int cursorPosition = viewState.getSelection();
final String oldText = viewState.getText(); final String oldText = viewState.getText();
@ -139,7 +139,7 @@ public class CalculatorKeyboardImpl implements CalculatorKeyboard {
@Override @Override
public void copyButtonPressed() { public void copyButtonPressed() {
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState(); final DisplayState displayViewState = Locator.getInstance().getDisplay().getViewState();
if (displayViewState.isValid()) { if (displayViewState.isValid()) {
final CharSequence text = displayViewState.getText(); final CharSequence text = displayViewState.getText();
if (!Strings.isEmpty(text)) { if (!Strings.isEmpty(text)) {

View File

@ -58,7 +58,7 @@ public interface CalculatorLocator {
CalculatorDisplay getDisplay(); CalculatorDisplay getDisplay();
@Nonnull @Nonnull
CalculatorEditor getEditor(); Editor getEditor();
@Nonnull @Nonnull
CalculatorKeyboard getKeyboard(); CalculatorKeyboard getKeyboard();

View File

@ -115,7 +115,7 @@ public enum CalculatorSpecialButton {
return; return;
} }
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState(); final DisplayState displayViewState = Locator.getInstance().getDisplay().getViewState();
if (displayViewState.isValid()) { if (displayViewState.isValid()) {
final CharSequence text = displayViewState.getText(); final CharSequence text = displayViewState.getText();
if (!Strings.isEmpty(text)) { if (!Strings.isEmpty(text)) {

View File

@ -37,7 +37,7 @@ import jscl.math.Generic;
* Date: 9/21/12 * Date: 9/21/12
* Time: 12:11 AM * Time: 12:11 AM
*/ */
enum ConversionMenuItem implements AMenuItem<CalculatorDisplayViewState> { enum ConversionMenuItem implements AMenuItem<DisplayState> {
convert_to_bin(NumeralBase.bin), convert_to_bin(NumeralBase.bin),
convert_to_dec(NumeralBase.dec), convert_to_dec(NumeralBase.dec),
@ -67,7 +67,7 @@ enum ConversionMenuItem implements AMenuItem<CalculatorDisplayViewState> {
} }
@Override @Override
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) { public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
final Generic result = data.getResult(); final Generic result = data.getResult();
if (result != null) { if (result != null) {

View File

@ -25,25 +25,14 @@ package org.solovyev.android.calculator;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
import java.io.Serializable;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import jscl.math.Generic; import jscl.math.Generic;
/** public class DisplayState implements Serializable {
* User: serso
* Date: 9/20/12
* Time: 9:50 PM
*/
public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewState {
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@Nonnull @Nonnull
private JsclOperation operation = JsclOperation.numeric; private JsclOperation operation = JsclOperation.numeric;
@ -61,26 +50,18 @@ public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewStat
private int selection = 0; private int selection = 0;
/* private DisplayState() {
**********************************************************************
*
* CONSTRUCTORS
*
**********************************************************************
*/
private CalculatorDisplayViewStateImpl() {
} }
@Nonnull @Nonnull
public static CalculatorDisplayViewState newDefaultInstance() { public static DisplayState empty() {
return new CalculatorDisplayViewStateImpl(); return new DisplayState();
} }
@Nonnull @Nonnull
public static CalculatorDisplayViewState newErrorState(@Nonnull JsclOperation operation, public static DisplayState createError(@Nonnull JsclOperation operation,
@Nonnull String errorMessage) { @Nonnull String errorMessage) {
final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl(); final DisplayState calculatorDisplayState = new DisplayState();
calculatorDisplayState.valid = false; calculatorDisplayState.valid = false;
calculatorDisplayState.errorMessage = errorMessage; calculatorDisplayState.errorMessage = errorMessage;
calculatorDisplayState.operation = operation; calculatorDisplayState.operation = operation;
@ -88,11 +69,11 @@ public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewStat
} }
@Nonnull @Nonnull
public static CalculatorDisplayViewState newValidState(@Nonnull JsclOperation operation, public static DisplayState createValid(@Nonnull JsclOperation operation,
@Nullable Generic result, @Nullable Generic result,
@Nonnull String stringResult, @Nonnull String stringResult,
int selection) { int selection) {
final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl(); final DisplayState calculatorDisplayState = new DisplayState();
calculatorDisplayState.valid = true; calculatorDisplayState.valid = true;
calculatorDisplayState.result = result; calculatorDisplayState.result = result;
calculatorDisplayState.stringResult = stringResult; calculatorDisplayState.stringResult = stringResult;
@ -102,50 +83,35 @@ public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewStat
return calculatorDisplayState; return calculatorDisplayState;
} }
/*
**********************************************************************
*
* METHODS
*
**********************************************************************
*/
@Nonnull @Nonnull
@Override
public String getText() { public String getText() {
return Strings.getNotEmpty(isValid() ? stringResult : errorMessage, ""); return Strings.getNotEmpty(isValid() ? stringResult : errorMessage, "");
} }
@Override
public int getSelection() { public int getSelection() {
return selection; return selection;
} }
@Nullable @Nullable
@Override
public Generic getResult() { public Generic getResult() {
return this.result; return this.result;
} }
@Override
public boolean isValid() { public boolean isValid() {
return this.valid; return this.valid;
} }
@Nullable @Nullable
@Override
public String getErrorMessage() { public String getErrorMessage() {
return this.errorMessage; return this.errorMessage;
} }
@Override
@Nullable @Nullable
public String getStringResult() { public String getStringResult() {
return stringResult; return stringResult;
} }
@Nonnull @Nonnull
@Override
public JsclOperation getOperation() { public JsclOperation getOperation() {
return this.operation; return this.operation;
} }

View File

@ -0,0 +1,265 @@
/*
* 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 org.solovyev.android.Check;
import org.solovyev.android.calculator.history.HistoryState;
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.text.Strings;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static java.lang.Math.min;
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;
public class Editor implements CalculatorEventListener {
private static final String TAG = App.subTag("Editor");
@Nonnull
private final Calculator calculator;
@Nonnull
private final CalculatorEventHolder lastEventHolder;
@Nullable
private final TextProcessor<TextProcessorEditorResult, String> textProcessor;
@Nullable
private EditorView view;
@Nonnull
private EditorState state = EditorState.empty();
public Editor(@Nonnull Calculator calculator, @Nullable TextProcessor<TextProcessorEditorResult, String> textProcessor) {
this.calculator = calculator;
this.textProcessor = textProcessor;
this.calculator.addCalculatorEventListener(this);
this.lastEventHolder = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
}
public static int clamp(int selection, @Nonnull CharSequence text) {
return clamp(selection, text.length());
}
public static int clamp(int selection, int max) {
return min(Math.max(selection, 0), max);
}
public void setView(@Nonnull EditorView view) {
Check.isMainThread();
this.view = view;
this.view.setState(state);
}
public void clearView(@Nonnull EditorView view) {
Check.isMainThread();
if (this.view == view) {
this.view = null;
}
}
@Nonnull
public EditorState getState() {
return state;
}
public void setState(@Nonnull EditorState state) {
setState(state, true);
}
private void setState(@Nonnull EditorState newState, boolean majorChanges) {
Check.isMainThread();
if (textProcessor != null) {
try {
final TextProcessorEditorResult result = textProcessor.process(newState.getText());
newState = EditorState.create(result.getCharSequence(), newState.getSelection() + result.getOffset());
} catch (CalculatorParseException e) {
Locator.getInstance().getLogger().error(TAG, e.getMessage(), e);
}
}
final EditorState oldState = state;
state = newState;
if (view != null) {
view.setState(newState);
}
fireStateChangedEvent(majorChanges, oldState, newState);
}
private void fireStateChangedEvent(boolean majorChanges, @Nonnull EditorState oldViewState, @Nonnull EditorState newViewState) {
Check.isMainThread();
if (majorChanges) {
calculator.fireCalculatorEvent(editor_state_changed, newChangeEventData(oldViewState, newViewState));
} else {
calculator.fireCalculatorEvent(editor_state_changed_light, newChangeEventData(oldViewState, newViewState));
}
}
@Override
public void onCalculatorEvent(@Nonnull CalculatorEventData evenData,
@Nonnull CalculatorEventType eventType,
@Nullable Object data) {
Check.isMainThread();
final CalculatorEventHolder.Result result = lastEventHolder.apply(evenData);
if (result.isNewAfter()) {
switch (eventType) {
case use_history_state:
final HistoryState historyState = (HistoryState) data;
final EditorHistoryState editorState = historyState.getEditorState();
this.setText(Strings.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition());
break;
}
}
}
@Nonnull
private EditorState newSelectionViewState(int newSelection) {
Check.isMainThread();
if (state.getSelection() != newSelection) {
final EditorState result = EditorState.newSelection(state, newSelection);
setState(result, false);
return result;
} else {
return state;
}
}
@Nonnull
public EditorState setCursorOnStart() {
Check.isMainThread();
return newSelectionViewState(0);
}
@Nonnull
public EditorState setCursorOnEnd() {
Check.isMainThread();
return newSelectionViewState(state.getText().length());
}
@Nonnull
public EditorState moveCursorLeft() {
Check.isMainThread();
if (state.getSelection() > 0) {
return newSelectionViewState(state.getSelection() - 1);
} else {
return state;
}
}
@Nonnull
public EditorState moveCursorRight() {
Check.isMainThread();
if (state.getSelection() < state.getText().length()) {
return newSelectionViewState(state.getSelection() + 1);
} else {
return state;
}
}
@Nonnull
public EditorState erase() {
Check.isMainThread();
int selection = state.getSelection();
final String text = state.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 EditorState result = EditorState.create(newText.toString(), selection - 1);
setState(result);
return result;
} else {
return state;
}
}
@Nonnull
public EditorState clear() {
Check.isMainThread();
return setText("");
}
@Nonnull
public EditorState setText(@Nonnull String text) {
Check.isMainThread();
final EditorState result = EditorState.create(text, text.length());
setState(result);
return result;
}
@Nonnull
public EditorState setText(@Nonnull String text, int selection) {
Check.isMainThread();
selection = clamp(selection, text);
final EditorState result = EditorState.create(text, selection);
setState(result);
return result;
}
@Nonnull
public EditorState insert(@Nonnull String text) {
Check.isMainThread();
return insert(text, 0);
}
@Nonnull
public EditorState insert(@Nonnull String text, int selectionOffset) {
Check.isMainThread();
final String oldText = state.getText();
final int selection = clamp(state.getSelection(), oldText);
int newTextLength = text.length() + oldText.length();
final StringBuilder newText = new StringBuilder(newTextLength);
newText.append(oldText.substring(0, selection));
newText.append(text);
newText.append(oldText.substring(selection));
int newSelection = clamp(text.length() + selection + selectionOffset, newTextLength);
final EditorState result = EditorState.create(newText.toString(), newSelection);
setState(result);
return result;
}
@Nonnull
public EditorState moveSelection(int offset) {
Check.isMainThread();
int selection = state.getSelection() + offset;
return setSelection(selection);
}
@Nonnull
public EditorState setSelection(int selection) {
Check.isMainThread();
selection = clamp(selection, state.getText());
final EditorState result = EditorState.newSelection(state, selection);
setState(result, false);
return result;
}
}

View File

@ -31,22 +31,23 @@ import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.widget.EditText; import android.widget.EditText;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService; import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService;
import java.lang.reflect.Method;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.lang.reflect.Method;
public class EditorView extends EditText { public class EditorView extends EditText {
@Nonnull @Nonnull
private static final String TAG = App.subTag("EditorView"); private static final String TAG = App.subTag("EditorView");
private boolean initialized; private boolean reportChanges;
@Nullable @Nullable
private Method setShowSoftInputOnFocusMethod; private Method setShowSoftInputOnFocusMethod;
private boolean externalChange;
public EditorView(Context context) { public EditorView(Context context) {
super(context); super(context);
@ -70,10 +71,10 @@ public class EditorView extends EditText {
} }
private void init() { private void init() {
Check.isTrue(!initialized);
addTextChangedListener(new MyTextWatcher()); addTextChangedListener(new MyTextWatcher());
setShowSoftInputOnFocusCompat(false); setShowSoftInputOnFocusCompat(false);
initialized = true; // changes should only be reported after the view has been set up completely, i.e. now
reportChanges = true;
} }
@Override @Override
@ -85,24 +86,25 @@ public class EditorView extends EditText {
public void setState(@Nonnull final EditorState state) { public void setState(@Nonnull final EditorState state) {
Check.isMainThread(); Check.isMainThread();
try { try {
externalChange = true; // we don't want to be notified about changes we make ourselves
reportChanges = false;
if (App.getTheme().light && getContext() instanceof CalculatorOnscreenService) { if (App.getTheme().light && getContext() instanceof CalculatorOnscreenService) {
// don't need formatting // don't need formatting
setText(state.getText()); setText(state.getText());
} else { } else {
setText(state.getTextAsCharSequence(), BufferType.EDITABLE); setText(state.getTextAsCharSequence(), BufferType.EDITABLE);
} }
final int selection = CalculatorEditorImpl.clamp(state.getSelection(), length()); final int selection = Editor.clamp(state.getSelection(), length());
setSelection(selection); setSelection(selection);
} finally { } finally {
externalChange = false; reportChanges = true;
} }
} }
@Override @Override
protected void onSelectionChanged(int start, int end) { protected void onSelectionChanged(int start, int end) {
Check.isMainThread(); Check.isMainThread();
if (!initialized || externalChange) { if (!reportChanges) {
return; return;
} }
// external text change => need to notify editor // external text change => need to notify editor
@ -147,7 +149,7 @@ public class EditorView extends EditText {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
if (!initialized || externalChange) { if (!reportChanges) {
return; return;
} }
// external text change => need to notify editor // external text change => need to notify editor

View File

@ -44,7 +44,7 @@ public class Locator implements CalculatorLocator {
@Nonnull @Nonnull
private Calculator calculator; private Calculator calculator;
@Nonnull @Nonnull
private CalculatorEditor calculatorEditor; private Editor editor;
@Nonnull @Nonnull
private CalculatorDisplay calculatorDisplay; private CalculatorDisplay calculatorDisplay;
@Nonnull @Nonnull
@ -92,7 +92,7 @@ public class Locator implements CalculatorLocator {
this.calculatorPreferenceService = preferenceService; this.calculatorPreferenceService = preferenceService;
this.calculatorPlotter = plotter; this.calculatorPlotter = plotter;
calculatorEditor = new CalculatorEditorImpl(this.calculator, editorTextProcessor); editor = new Editor(this.calculator, editorTextProcessor);
calculatorDisplay = new CalculatorDisplayImpl(this.calculator); calculatorDisplay = new CalculatorDisplayImpl(this.calculator);
calculatorKeyboard = keyboard; calculatorKeyboard = keyboard;
} }
@ -117,8 +117,8 @@ public class Locator implements CalculatorLocator {
@Nonnull @Nonnull
@Override @Override
public CalculatorEditor getEditor() { public Editor getEditor() {
return calculatorEditor; return editor;
} }
@Override @Override

View File

@ -36,11 +36,11 @@ import android.view.ViewGroup;
import android.widget.EditText; import android.widget.EditText;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.CalculatorDisplayViewState;
import org.solovyev.android.calculator.CalculatorEventData; import org.solovyev.android.calculator.CalculatorEventData;
import org.solovyev.android.calculator.CalculatorEventListener; import org.solovyev.android.calculator.CalculatorEventListener;
import org.solovyev.android.calculator.CalculatorEventType; import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.CalculatorUtils; import org.solovyev.android.calculator.CalculatorUtils;
import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity;
@ -308,7 +308,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
} }
@Nonnull @Nonnull
public static Input newFromDisplay(@Nonnull CalculatorDisplayViewState viewState) { public static Input newFromDisplay(@Nonnull DisplayState viewState) {
final Input result = new Input(); final Input result = new Input();
result.content = viewState.getText(); result.content = viewState.getText();

View File

@ -80,7 +80,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
save(); save();
} }
public void removeSavedHistory(@Nonnull CalculatorHistoryState historyState) { public void removeSavedHistory(@Nonnull HistoryState historyState) {
historyState.setSaved(false); historyState.setSaved(false);
calculatorHistory.removeSavedHistory(historyState); calculatorHistory.removeSavedHistory(historyState);
save(); save();
@ -92,7 +92,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
} }
@Override @Override
public CalculatorHistoryState getLastHistoryState() { public HistoryState getLastHistoryState() {
return calculatorHistory.getLastHistoryState(); return calculatorHistory.getLastHistoryState();
} }
@ -102,7 +102,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
} }
@Override @Override
public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) { public HistoryState undo(@Nullable HistoryState currentState) {
return calculatorHistory.undo(currentState); return calculatorHistory.undo(currentState);
} }
@ -112,7 +112,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
} }
@Override @Override
public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) { public HistoryState redo(@Nullable HistoryState currentState) {
return calculatorHistory.redo(currentState); return calculatorHistory.redo(currentState);
} }
@ -122,24 +122,24 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
} }
@Override @Override
public CalculatorHistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) { public HistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable HistoryState currentState) {
return calculatorHistory.doAction(historyAction, currentState); return calculatorHistory.doAction(historyAction, currentState);
} }
@Override @Override
public void addState(@Nullable CalculatorHistoryState currentState) { public void addState(@Nullable HistoryState currentState) {
calculatorHistory.addState(currentState); calculatorHistory.addState(currentState);
} }
@Nonnull @Nonnull
@Override @Override
public List<CalculatorHistoryState> getStates() { public List<HistoryState> getStates() {
return calculatorHistory.getStates(); return calculatorHistory.getStates();
} }
@Nonnull @Nonnull
@Override @Override
public List<CalculatorHistoryState> getStates(boolean includeIntermediateStates) { public List<HistoryState> getStates(boolean includeIntermediateStates) {
return calculatorHistory.getStates(includeIntermediateStates); return calculatorHistory.getStates(includeIntermediateStates);
} }
@ -149,12 +149,12 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
} }
@Nonnull @Nonnull
public List<CalculatorHistoryState> getSavedHistory() { public List<HistoryState> getSavedHistory() {
return calculatorHistory.getSavedHistory(); return calculatorHistory.getSavedHistory();
} }
@Nonnull @Nonnull
public CalculatorHistoryState addSavedState(@Nonnull CalculatorHistoryState historyState) { public HistoryState addSavedState(@Nonnull HistoryState historyState) {
return calculatorHistory.addSavedState(historyState); return calculatorHistory.addSavedState(historyState);
} }

View File

@ -88,9 +88,9 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
********************************************************************** **********************************************************************
*/ */
public static final Comparator<CalculatorHistoryState> COMPARATOR = new Comparator<CalculatorHistoryState>() { public static final Comparator<HistoryState> COMPARATOR = new Comparator<HistoryState>() {
@Override @Override
public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) { public int compare(HistoryState state1, HistoryState state2) {
if (state1.isSaved() == state2.isSaved()) { if (state1.isSaved() == state2.isSaved()) {
long l = state2.getTime() - state1.getTime(); long l = state2.getTime() - state1.getTime();
return l > 0l ? 1 : (l < 0l ? -1 : 0); return l > 0l ? 1 : (l < 0l ? -1 : 0);
@ -137,15 +137,15 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
ui = CalculatorApplication.getInstance().createFragmentHelper(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId(), false); ui = CalculatorApplication.getInstance().createFragmentHelper(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId(), false);
} }
public static boolean isAlreadySaved(@Nonnull CalculatorHistoryState historyState) { public static boolean isAlreadySaved(@Nonnull HistoryState historyState) {
if (historyState.isSaved()) throw new AssertionError(); if (historyState.isSaved()) throw new AssertionError();
boolean result = false; boolean result = false;
try { try {
historyState.setSaved(true); historyState.setSaved(true);
if (Collections.contains(historyState, Locator.getInstance().getHistory().getSavedHistory(), new Equalizer<CalculatorHistoryState>() { if (Collections.contains(historyState, Locator.getInstance().getHistory().getSavedHistory(), new Equalizer<HistoryState>() {
@Override @Override
public boolean areEqual(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) { public boolean areEqual(@Nullable HistoryState first, @Nullable HistoryState second) {
return first != null && second != null && return first != null && second != null &&
first.getTime() == second.getTime() && first.getTime() == second.getTime() &&
first.getDisplayState().equals(second.getDisplayState()) && first.getDisplayState().equals(second.getDisplayState()) &&
@ -160,13 +160,13 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
return result; return result;
} }
public static void useHistoryItem(@Nonnull final CalculatorHistoryState historyState) { public static void useHistoryItem(@Nonnull final HistoryState historyState) {
App.getVibrator().vibrate(); App.getVibrator().vibrate();
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, historyState); Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, historyState);
} }
@Nonnull @Nonnull
public static String getHistoryText(@Nonnull CalculatorHistoryState state) { public static String getHistoryText(@Nonnull HistoryState state) {
final StringBuilder result = new StringBuilder(); final StringBuilder result = new StringBuilder();
result.append(state.getEditorState().getText()); result.append(state.getEditorState().getText());
result.append(getIdentitySign(state.getDisplayState().getJsclOperation())); result.append(getIdentitySign(state.getDisplayState().getJsclOperation()));
@ -211,7 +211,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
ui.onViewCreated(this, root); ui.onViewCreated(this, root);
adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), org.solovyev.android.calculator.R.id.history_item, new ArrayList<CalculatorHistoryState>(), showDatetime); adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), org.solovyev.android.calculator.R.id.history_item, new ArrayList<HistoryState>(), showDatetime);
setListAdapter(adapter); setListAdapter(adapter);
final ListView lv = getListView(); final ListView lv = getListView();
@ -232,14 +232,14 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
final int position, final int position,
final long id) { final long id) {
useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position)); useHistoryItem((HistoryState) parent.getItemAtPosition(position));
} }
}); });
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override @Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position); final HistoryState historyState = (HistoryState) parent.getItemAtPosition(position);
final FragmentActivity activity = getActivity(); final FragmentActivity activity = getActivity();
@ -309,13 +309,13 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
protected abstract int getItemLayoutId(); protected abstract int getItemLayoutId();
private void updateAdapter() { private void updateAdapter() {
final List<CalculatorHistoryState> historyList = getHistoryList(); final List<HistoryState> historyList = getHistoryList();
final ArrayAdapter<CalculatorHistoryState> adapter = getAdapter(); final ArrayAdapter<HistoryState> adapter = getAdapter();
try { try {
adapter.setNotifyOnChange(false); adapter.setNotifyOnChange(false);
adapter.clear(); adapter.clear();
for (CalculatorHistoryState historyState : historyList) { for (HistoryState historyState : historyList) {
adapter.add(historyState); adapter.add(historyState);
} }
} finally { } finally {
@ -326,26 +326,26 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
} }
@Nonnull @Nonnull
private List<CalculatorHistoryState> getHistoryList() { private List<HistoryState> getHistoryList() {
final List<CalculatorHistoryState> calculatorHistoryStates = getHistoryItems(); final List<HistoryState> historyStates = getHistoryItems();
java.util.Collections.sort(calculatorHistoryStates, COMPARATOR); java.util.Collections.sort(historyStates, COMPARATOR);
final FilterRulesChain<CalculatorHistoryState> filterRulesChain = new FilterRulesChain<>(); final FilterRulesChain<HistoryState> filterRulesChain = new FilterRulesChain<>();
filterRulesChain.addFilterRule(new JPredicate<CalculatorHistoryState>() { filterRulesChain.addFilterRule(new JPredicate<HistoryState>() {
@Override @Override
public boolean apply(CalculatorHistoryState object) { public boolean apply(HistoryState object) {
return object == null || Strings.isEmpty(object.getEditorState().getText()); return object == null || Strings.isEmpty(object.getEditorState().getText());
} }
}); });
new Filter<>(filterRulesChain).filter(calculatorHistoryStates.iterator()); new Filter<>(filterRulesChain).filter(historyStates.iterator());
return calculatorHistoryStates; return historyStates;
} }
@Nonnull @Nonnull
protected abstract List<CalculatorHistoryState> getHistoryItems(); protected abstract List<HistoryState> getHistoryItems();
protected abstract void clearHistory(); protected abstract void clearHistory();

View File

@ -29,12 +29,7 @@ import java.util.Date;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** public class BaseHistoryState implements Cloneable {
* User: serso
* Date: 10/15/11
* Time: 1:45 PM
*/
public class AbstractHistoryState implements Cloneable {
@Element @Element
private long time = new Date().getTime(); private long time = new Date().getTime();
@ -82,16 +77,13 @@ public class AbstractHistoryState implements Cloneable {
this.saved = saved; this.saved = saved;
} }
@SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException")
@Override @Override
protected AbstractHistoryState clone() { protected BaseHistoryState clone() {
AbstractHistoryState clone;
try { try {
clone = (AbstractHistoryState) super.clone(); return (BaseHistoryState) super.clone();
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
throw new UnsupportedOperationException(e); throw new UnsupportedOperationException(e);
} }
return clone;
} }
} }

View File

@ -34,7 +34,7 @@ import javax.annotation.Nonnull;
* Date: 20.09.12 * Date: 20.09.12
* Time: 16:11 * Time: 16:11
*/ */
public interface CalculatorHistory extends HistoryHelper<CalculatorHistoryState>, CalculatorEventListener { public interface CalculatorHistory extends HistoryHelper<HistoryState>, CalculatorEventListener {
void load(); void load();
@ -46,18 +46,18 @@ public interface CalculatorHistory extends HistoryHelper<CalculatorHistoryState>
void clearSavedHistory(); void clearSavedHistory();
void removeSavedHistory(@Nonnull CalculatorHistoryState historyState); void removeSavedHistory(@Nonnull HistoryState historyState);
@Nonnull @Nonnull
List<CalculatorHistoryState> getSavedHistory(); List<HistoryState> getSavedHistory();
@Nonnull @Nonnull
CalculatorHistoryState addSavedState(@Nonnull CalculatorHistoryState historyState); HistoryState addSavedState(@Nonnull HistoryState historyState);
@Nonnull @Nonnull
List<CalculatorHistoryState> getStates(); List<HistoryState> getStates();
@Nonnull @Nonnull
List<CalculatorHistoryState> getStates(boolean includeIntermediateStates); List<HistoryState> getStates(boolean includeIntermediateStates);
} }

View File

@ -47,10 +47,10 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
private final AtomicInteger counter = new AtomicInteger(0); private final AtomicInteger counter = new AtomicInteger(0);
@Nonnull @Nonnull
private final HistoryHelper<CalculatorHistoryState> history = SimpleHistoryHelper.newInstance(); private final HistoryHelper<HistoryState> history = SimpleHistoryHelper.newInstance();
@Nonnull @Nonnull
private final List<CalculatorHistoryState> savedHistory = new ArrayList<CalculatorHistoryState>(); private final List<HistoryState> savedHistory = new ArrayList<HistoryState>();
@Nonnull @Nonnull
private final CalculatorEventHolder lastEventData = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId()); private final CalculatorEventHolder lastEventData = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
@ -70,7 +70,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
} }
@Override @Override
public CalculatorHistoryState getLastHistoryState() { public HistoryState getLastHistoryState() {
synchronized (history) { synchronized (history) {
return this.history.getLastHistoryState(); return this.history.getLastHistoryState();
} }
@ -84,7 +84,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
} }
@Override @Override
public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) { public HistoryState undo(@Nullable HistoryState currentState) {
synchronized (history) { synchronized (history) {
return history.undo(currentState); return history.undo(currentState);
} }
@ -96,7 +96,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
} }
@Override @Override
public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) { public HistoryState redo(@Nullable HistoryState currentState) {
synchronized (history) { synchronized (history) {
return history.redo(currentState); return history.redo(currentState);
} }
@ -110,14 +110,14 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
} }
@Override @Override
public CalculatorHistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) { public HistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable HistoryState currentState) {
synchronized (history) { synchronized (history) {
return history.doAction(historyAction, currentState); return history.doAction(historyAction, currentState);
} }
} }
@Override @Override
public void addState(@Nullable CalculatorHistoryState currentState) { public void addState(@Nullable HistoryState currentState) {
synchronized (history) { synchronized (history) {
history.addState(currentState); history.addState(currentState);
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, currentState); Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, currentState);
@ -126,7 +126,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
@Nonnull @Nonnull
@Override @Override
public List<CalculatorHistoryState> getStates() { public List<HistoryState> getStates() {
synchronized (history) { synchronized (history) {
return history.getStates(); return history.getStates();
} }
@ -134,17 +134,17 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
@Nonnull @Nonnull
@Override @Override
public List<CalculatorHistoryState> getStates(boolean includeIntermediateStates) { public List<HistoryState> getStates(boolean includeIntermediateStates) {
synchronized (history) { synchronized (history) {
if (includeIntermediateStates) { if (includeIntermediateStates) {
return getStates(); return getStates();
} else { } else {
final List<CalculatorHistoryState> states = getStates(); final List<HistoryState> states = getStates();
final List<CalculatorHistoryState> result = new LinkedList<CalculatorHistoryState>(); final List<HistoryState> result = new LinkedList<HistoryState>();
CalculatorHistoryState laterState = null; HistoryState laterState = null;
for (CalculatorHistoryState state : org.solovyev.common.collections.Collections.reversed(states)) { for (HistoryState state : org.solovyev.common.collections.Collections.reversed(states)) {
if (laterState != null) { if (laterState != null) {
final String laterEditorText = laterState.getEditorState().getText(); final String laterEditorText = laterState.getEditorState().getText();
final String editorText = state.getEditorState().getText(); final String editorText = state.getEditorState().getText();
@ -187,17 +187,17 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
@Override @Override
@Nonnull @Nonnull
public List<CalculatorHistoryState> getSavedHistory() { public List<HistoryState> getSavedHistory() {
return Collections.unmodifiableList(savedHistory); return Collections.unmodifiableList(savedHistory);
} }
@Override @Override
@Nonnull @Nonnull
public CalculatorHistoryState addSavedState(@Nonnull CalculatorHistoryState historyState) { public HistoryState addSavedState(@Nonnull HistoryState historyState) {
if (historyState.isSaved()) { if (historyState.isSaved()) {
return historyState; return historyState;
} else { } else {
final CalculatorHistoryState savedState = historyState.clone(); final HistoryState savedState = historyState.clone();
savedState.setId(counter.incrementAndGet()); savedState.setId(counter.incrementAndGet());
savedState.setSaved(true); savedState.setSaved(true);
@ -223,7 +223,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
clearSavedHistory(); clearSavedHistory();
HistoryUtils.fromXml(xml, this.savedHistory); HistoryUtils.fromXml(xml, this.savedHistory);
for (CalculatorHistoryState historyState : savedHistory) { for (HistoryState historyState : savedHistory) {
historyState.setSaved(true); historyState.setSaved(true);
historyState.setId(counter.incrementAndGet()); historyState.setId(counter.incrementAndGet());
} }
@ -240,7 +240,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
} }
@Override @Override
public void removeSavedHistory(@Nonnull CalculatorHistoryState historyState) { public void removeSavedHistory(@Nonnull HistoryState historyState) {
this.savedHistory.remove(historyState); this.savedHistory.remove(historyState);
} }
@ -266,8 +266,8 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
if (lastEditorViewState != null) { if (lastEditorViewState != null) {
final EditorState editorViewState = lastEditorViewState; final EditorState editorViewState = lastEditorViewState;
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data; final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data;
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewValue(); final DisplayState displayViewState = displayChangeData.getNewValue();
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState)); addState(HistoryState.create(editorViewState, displayViewState));
} }
} else { } else {
lastEditorViewState = null; lastEditorViewState = null;

View File

@ -26,8 +26,7 @@ import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root; import org.simpleframework.xml.Root;
import org.simpleframework.xml.Transient; import org.simpleframework.xml.Transient;
import org.solovyev.android.calculator.CalculatorDisplay; import org.solovyev.android.calculator.CalculatorDisplay;
import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
@ -43,7 +42,7 @@ import jscl.math.Generic;
*/ */
@Root @Root
public class CalculatorDisplayHistoryState implements Cloneable { public class DisplayHistoryState implements Cloneable {
@Transient @Transient
private boolean valid = true; private boolean valid = true;
@ -64,15 +63,15 @@ public class CalculatorDisplayHistoryState implements Cloneable {
@Nullable @Nullable
private Generic genericResult; private Generic genericResult;
private CalculatorDisplayHistoryState() { private DisplayHistoryState() {
// for xml // for xml
} }
@Nonnull @Nonnull
public static CalculatorDisplayHistoryState newInstance(@Nonnull CalculatorDisplayViewState viewState) { public static DisplayHistoryState newInstance(@Nonnull DisplayState viewState) {
final CalculatorDisplayHistoryState result = new CalculatorDisplayHistoryState(); final DisplayHistoryState result = new DisplayHistoryState();
result.editorState = EditorHistoryState.newInstance(viewState); result.editorState = EditorHistoryState.create(viewState);
result.valid = viewState.isValid(); result.valid = viewState.isValid();
result.jsclOperation = viewState.getOperation(); result.jsclOperation = viewState.getOperation();
@ -84,9 +83,9 @@ public class CalculatorDisplayHistoryState implements Cloneable {
public void setValuesFromHistory(@Nonnull CalculatorDisplay display) { public void setValuesFromHistory(@Nonnull CalculatorDisplay display) {
if (this.isValid()) { if (this.isValid()) {
display.setViewState(CalculatorDisplayViewStateImpl.newValidState(this.getJsclOperation(), this.getGenericResult(), Strings.getNotEmpty(this.getEditorState().getText(), ""), this.getEditorState().getCursorPosition())); display.setViewState(DisplayState.createValid(this.getJsclOperation(), this.getGenericResult(), Strings.getNotEmpty(this.getEditorState().getText(), ""), this.getEditorState().getCursorPosition()));
} else { } else {
display.setViewState(CalculatorDisplayViewStateImpl.newErrorState(this.getJsclOperation(), Strings.getNotEmpty(this.getErrorMessage(), ""))); display.setViewState(DisplayState.createError(this.getJsclOperation(), Strings.getNotEmpty(this.getErrorMessage(), "")));
} }
} }
@ -121,7 +120,7 @@ public class CalculatorDisplayHistoryState implements Cloneable {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
CalculatorDisplayHistoryState that = (CalculatorDisplayHistoryState) o; DisplayHistoryState that = (DisplayHistoryState) o;
if (!editorState.equals(that.editorState)) return false; if (!editorState.equals(that.editorState)) return false;
if (jsclOperation != that.jsclOperation) return false; if (jsclOperation != that.jsclOperation) return false;
@ -147,9 +146,9 @@ public class CalculatorDisplayHistoryState implements Cloneable {
} }
@Override @Override
protected CalculatorDisplayHistoryState clone() { protected DisplayHistoryState clone() {
try { try {
final CalculatorDisplayHistoryState clone = (CalculatorDisplayHistoryState) super.clone(); final DisplayHistoryState clone = (DisplayHistoryState) super.clone();
clone.editorState = this.editorState.clone(); clone.editorState = this.editorState.clone();

View File

@ -24,8 +24,8 @@ package org.solovyev.android.calculator.history;
import org.simpleframework.xml.Element; import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root; import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.CalculatorEditor; import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.EditorState;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
@ -47,17 +47,17 @@ public class EditorHistoryState implements Cloneable {
} }
@Nonnull @Nonnull
public static EditorHistoryState newInstance(@Nonnull EditorState viewState) { public static EditorHistoryState create(@Nonnull EditorState state) {
final EditorHistoryState result = new EditorHistoryState(); final EditorHistoryState result = new EditorHistoryState();
result.text = String.valueOf(viewState.getText()); result.text = String.valueOf(state.getText());
result.cursorPosition = viewState.getSelection(); result.cursorPosition = state.getSelection();
return result; return result;
} }
@Nonnull @Nonnull
public static EditorHistoryState newInstance(@Nonnull CalculatorDisplayViewState viewState) { public static EditorHistoryState create(@Nonnull DisplayState viewState) {
final EditorHistoryState result = new EditorHistoryState(); final EditorHistoryState result = new EditorHistoryState();
result.text = viewState.getText(); result.text = viewState.getText();
@ -66,7 +66,7 @@ public class EditorHistoryState implements Cloneable {
return result; return result;
} }
public void setValuesFromHistory(@Nonnull CalculatorEditor editor) { public void setValuesFromHistory(@Nonnull Editor editor) {
editor.setText(Strings.getNotEmpty(this.getText(), "")); editor.setText(Strings.getNotEmpty(this.getText(), ""));
editor.setSelection(this.getCursorPosition()); editor.setSelection(this.getCursorPosition());
} }

View File

@ -37,13 +37,13 @@ import java.util.List;
@Root @Root
public class History { public class History {
@ElementList(type = CalculatorHistoryState.class) @ElementList(type = HistoryState.class)
private List<CalculatorHistoryState> historyItems = new ArrayList<CalculatorHistoryState>(); private List<HistoryState> historyItems = new ArrayList<HistoryState>();
public History() { public History() {
} }
public List<CalculatorHistoryState> getHistoryItems() { public List<HistoryState> getHistoryItems() {
return historyItems; return historyItems;
} }
} }

View File

@ -47,12 +47,12 @@ import static org.solovyev.android.calculator.history.BaseHistoryFragment.isAlre
* Date: 12/18/11 * Date: 12/18/11
* Time: 7:39 PM * Time: 7:39 PM
*/ */
public class HistoryArrayAdapter extends ArrayAdapter<CalculatorHistoryState> { public class HistoryArrayAdapter extends ArrayAdapter<HistoryState> {
private static final int DATETIME_FORMAT = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_ABBREV_TIME; private static final int DATETIME_FORMAT = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_ABBREV_TIME;
private boolean showDatetime; private boolean showDatetime;
HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @Nonnull List<CalculatorHistoryState> historyList, boolean showDatetime) { HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @Nonnull List<HistoryState> historyList, boolean showDatetime) {
super(context, resource, textViewResourceId, historyList); super(context, resource, textViewResourceId, historyList);
this.showDatetime = showDatetime; this.showDatetime = showDatetime;
} }
@ -61,7 +61,7 @@ public class HistoryArrayAdapter extends ArrayAdapter<CalculatorHistoryState> {
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent); final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent);
final CalculatorHistoryState state = getItem(position); final HistoryState state = getItem(position);
final TextView time = (TextView) result.findViewById(R.id.history_time); final TextView time = (TextView) result.findViewById(R.id.history_time);
if (showDatetime) { if (showDatetime) {

View File

@ -48,11 +48,11 @@ public class HistoryFragment extends BaseHistoryFragment {
@Nonnull @Nonnull
@Override @Override
protected List<CalculatorHistoryState> getHistoryItems() { protected List<HistoryState> getHistoryItems() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
final boolean showIntermediateCalculations = Preferences.History.showIntermediateCalculations.getPreference(preferences); final boolean showIntermediateCalculations = Preferences.History.showIntermediateCalculations.getPreference(preferences);
final List<CalculatorHistoryState> historyStates = Locator.getInstance().getHistory().getStates(showIntermediateCalculations); final List<HistoryState> historyStates = Locator.getInstance().getHistory().getStates(showIntermediateCalculations);
return new ArrayList<CalculatorHistoryState>(historyStates); return new ArrayList<HistoryState>(historyStates);
} }
@Override @Override

View File

@ -34,23 +34,23 @@ import javax.annotation.Nonnull;
public class HistoryItemMenuData { public class HistoryItemMenuData {
@Nonnull @Nonnull
private final ArrayAdapter<CalculatorHistoryState> adapter; private final ArrayAdapter<HistoryState> adapter;
@Nonnull @Nonnull
private final CalculatorHistoryState historyState; private final HistoryState historyState;
public HistoryItemMenuData(@Nonnull CalculatorHistoryState historyState, ArrayAdapter<CalculatorHistoryState> adapter) { public HistoryItemMenuData(@Nonnull HistoryState historyState, ArrayAdapter<HistoryState> adapter) {
this.historyState = historyState; this.historyState = historyState;
this.adapter = adapter; this.adapter = adapter;
} }
@Nonnull @Nonnull
public CalculatorHistoryState getHistoryState() { public HistoryState getHistoryState() {
return historyState; return historyState;
} }
@Nonnull @Nonnull
public ArrayAdapter<CalculatorHistoryState> getAdapter() { public ArrayAdapter<HistoryState> getAdapter() {
return adapter; return adapter;
} }
} }

View File

@ -57,8 +57,8 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
copy_expression(R.string.c_copy_expression) { copy_expression(R.string.c_copy_expression) {
@Override @Override
public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) { public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) {
final CalculatorHistoryState calculatorHistoryState = data.getHistoryState(); final HistoryState historyState = data.getHistoryState();
final String text = calculatorHistoryState.getEditorState().getText(); final String text = historyState.getEditorState().getText();
if (!Strings.isEmpty(text)) { if (!Strings.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text); clipboard.setText(text);
@ -70,8 +70,8 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
copy_result(R.string.c_copy_result) { copy_result(R.string.c_copy_result) {
@Override @Override
public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) { public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) {
final CalculatorHistoryState calculatorHistoryState = data.getHistoryState(); final HistoryState historyState = data.getHistoryState();
final String text = calculatorHistoryState.getDisplayState().getEditorState().getText(); final String text = historyState.getDisplayState().getEditorState().getText();
if (!Strings.isEmpty(text)) { if (!Strings.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text); clipboard.setText(text);
@ -83,7 +83,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
save(R.string.c_save) { save(R.string.c_save) {
@Override @Override
public void onClick(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context) { public void onClick(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context) {
final CalculatorHistoryState historyState = data.getHistoryState(); final HistoryState historyState = data.getHistoryState();
if (!historyState.isSaved()) { if (!historyState.isSaved()) {
createEditHistoryDialog(data, context, true); createEditHistoryDialog(data, context, true);
} else { } else {
@ -95,7 +95,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
edit(R.string.c_edit) { edit(R.string.c_edit) {
@Override @Override
public void onClick(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context) { public void onClick(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context) {
final CalculatorHistoryState historyState = data.getHistoryState(); final HistoryState historyState = data.getHistoryState();
if (historyState.isSaved()) { if (historyState.isSaved()) {
createEditHistoryDialog(data, context, false); createEditHistoryDialog(data, context, false);
} else { } else {
@ -107,7 +107,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
remove(R.string.c_remove) { remove(R.string.c_remove) {
@Override @Override
public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) { public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) {
final CalculatorHistoryState historyState = data.getHistoryState(); final HistoryState historyState = data.getHistoryState();
if (historyState.isSaved()) { if (historyState.isSaved()) {
data.getAdapter().remove(historyState); data.getAdapter().remove(historyState);
Locator.getInstance().getHistory().removeSavedHistory(historyState); Locator.getInstance().getHistory().removeSavedHistory(historyState);
@ -124,7 +124,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
} }
private static void createEditHistoryDialog(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context, final boolean save) { private static void createEditHistoryDialog(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context, final boolean save) {
final CalculatorHistoryState historyState = data.getHistoryState(); final HistoryState historyState = data.getHistoryState();
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.history_edit, null); final View editView = layoutInflater.inflate(R.layout.history_edit, null);
@ -142,7 +142,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if (save) { if (save) {
final CalculatorHistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(historyState); final HistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(historyState);
savedHistoryItem.setComment(comment.getText().toString()); savedHistoryItem.setComment(comment.getText().toString());
Locator.getInstance().getHistory().save(); Locator.getInstance().getHistory().save();
// we don't need to add element to the adapter as adapter of another activity must be updated and not this // we don't need to add element to the adapter as adapter of another activity must be updated and not this

View File

@ -25,20 +25,14 @@ package org.solovyev.android.calculator.history;
import org.simpleframework.xml.Element; import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root; import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.CalculatorDisplay; import org.solovyev.android.calculator.CalculatorDisplay;
import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.CalculatorEditor; import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.EditorState;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
/**
* User: serso
* Date: 9/11/11
* Time: 12:16 AM
*/
@Root @Root
public class CalculatorHistoryState extends AbstractHistoryState { public class HistoryState extends BaseHistoryState {
@Element @Element
@Nonnull @Nonnull
@ -46,35 +40,28 @@ public class CalculatorHistoryState extends AbstractHistoryState {
@Element @Element
@Nonnull @Nonnull
private CalculatorDisplayHistoryState displayState; private DisplayHistoryState displayState;
private CalculatorHistoryState() { private HistoryState() {
// for xml // for xml
} }
private CalculatorHistoryState(@Nonnull EditorHistoryState editorState, private HistoryState(@Nonnull EditorHistoryState editorState,
@Nonnull CalculatorDisplayHistoryState displayState) { @Nonnull DisplayHistoryState displayState) {
this.editorState = editorState; this.editorState = editorState;
this.displayState = displayState; this.displayState = displayState;
} }
@Nonnull @Nonnull
public static CalculatorHistoryState newInstance(@Nonnull CalculatorEditor editor, public static HistoryState create(@Nonnull Editor editor,
@Nonnull CalculatorDisplay display) { @Nonnull CalculatorDisplay display) {
final EditorState editorViewState = editor.getViewState(); return create(editor.getState(), display.getViewState());
final CalculatorDisplayViewState displayViewState = display.getViewState();
return newInstance(editorViewState, displayViewState);
} }
@Nonnull @Nonnull
public static CalculatorHistoryState newInstance(@Nonnull EditorState editorViewState, public static HistoryState create(@Nonnull EditorState editorState,
@Nonnull CalculatorDisplayViewState displayViewState) { @Nonnull DisplayState displayState) {
final EditorHistoryState editorHistoryState = EditorHistoryState.newInstance(editorViewState); return new HistoryState(EditorHistoryState.create(editorState), DisplayHistoryState.newInstance(displayState));
final CalculatorDisplayHistoryState displayHistoryState = CalculatorDisplayHistoryState.newInstance(displayViewState);
return new CalculatorHistoryState(editorHistoryState, displayHistoryState);
} }
@Nonnull @Nonnull
@ -82,22 +69,14 @@ public class CalculatorHistoryState extends AbstractHistoryState {
return editorState; return editorState;
} }
public void setEditorState(@Nonnull EditorHistoryState editorState) {
this.editorState = editorState;
}
@Nonnull @Nonnull
public CalculatorDisplayHistoryState getDisplayState() { public DisplayHistoryState getDisplayState() {
return displayState; return displayState;
} }
public void setDisplayState(@Nonnull CalculatorDisplayHistoryState displayState) {
this.displayState = displayState;
}
@Override @Override
public String toString() { public String toString() {
return "CalculatorHistoryState{" + return "HistoryState{" +
"editorState=" + editorState + "editorState=" + editorState +
", displayState=" + displayState + ", displayState=" + displayState +
'}'; '}';
@ -108,7 +87,7 @@ public class CalculatorHistoryState extends AbstractHistoryState {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
CalculatorHistoryState that = (CalculatorHistoryState) o; HistoryState that = (HistoryState) o;
if (this.isSaved() != that.isSaved()) return false; if (this.isSaved() != that.isSaved()) return false;
if (this.getId() != that.getId()) return false; if (this.getId() != that.getId()) return false;
@ -127,18 +106,18 @@ public class CalculatorHistoryState extends AbstractHistoryState {
return result; return result;
} }
public void setValuesFromHistory(@Nonnull CalculatorEditor editor, @Nonnull CalculatorDisplay display) { public void setValuesFromHistory(@Nonnull Editor editor, @Nonnull CalculatorDisplay display) {
this.getEditorState().setValuesFromHistory(editor); this.getEditorState().setValuesFromHistory(editor);
this.getDisplayState().setValuesFromHistory(display); this.getDisplayState().setValuesFromHistory(display);
} }
@Override @Override
protected CalculatorHistoryState clone() { protected HistoryState clone() {
final CalculatorHistoryState clone = (CalculatorHistoryState) super.clone(); final HistoryState that = (HistoryState) super.clone();
clone.editorState = this.editorState.clone(); that.editorState = this.editorState.clone();
clone.displayState = this.displayState.clone(); that.displayState = this.displayState.clone();
return clone; return that;
} }
} }

View File

@ -43,12 +43,12 @@ class HistoryUtils {
throw new AssertionError(); throw new AssertionError();
} }
public static void fromXml(@Nullable String xml, @Nonnull List<CalculatorHistoryState> historyItems) { public static void fromXml(@Nullable String xml, @Nonnull List<HistoryState> historyItems) {
if (xml != null) { if (xml != null) {
final Serializer serializer = new Persister(); final Serializer serializer = new Persister();
try { try {
final History history = serializer.read(History.class, xml); final History history = serializer.read(History.class, xml);
for (CalculatorHistoryState historyItem : history.getHistoryItems()) { for (HistoryState historyItem : history.getHistoryItems()) {
historyItems.add(historyItem); historyItems.add(historyItem);
} }
} catch (Exception e) { } catch (Exception e) {
@ -58,9 +58,9 @@ class HistoryUtils {
} }
@Nonnull @Nonnull
public static String toXml(@Nonnull List<CalculatorHistoryState> historyItems) { public static String toXml(@Nonnull List<HistoryState> historyItems) {
final History history = new History(); final History history = new History();
for (CalculatorHistoryState historyState : historyItems) { for (HistoryState historyState : historyItems) {
if (historyState.isSaved()) { if (historyState.isSaved()) {
history.getHistoryItems().add(historyState); history.getHistoryItems().add(historyState);
} }

View File

@ -44,8 +44,8 @@ public class SavedHistoryFragment extends BaseHistoryFragment {
@Nonnull @Nonnull
@Override @Override
protected List<CalculatorHistoryState> getHistoryItems() { protected List<HistoryState> getHistoryItems() {
return new ArrayList<CalculatorHistoryState>(Locator.getInstance().getHistory().getSavedHistory()); return new ArrayList<HistoryState>(Locator.getInstance().getHistory().getSavedHistory());
} }
@Override @Override

View File

@ -122,7 +122,7 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi
view.show(); view.show();
startCalculatorListening(); startCalculatorListening();
view.updateEditorState(Locator.getInstance().getEditor().getViewState()); view.updateEditorState(Locator.getInstance().getEditor().getState());
view.updateDisplayState(Locator.getInstance().getDisplay().getViewState()); view.updateDisplayState(Locator.getInstance().getDisplay().getViewState());
viewCreated = true; viewCreated = true;

View File

@ -180,7 +180,7 @@ public class CalculatorOnscreenView {
} }
} }
public void updateDisplayState(@Nonnull CalculatorDisplayViewState displayState) { public void updateDisplayState(@Nonnull DisplayState displayState) {
checkInit(); checkInit();
displayView.setState(displayState); displayView.setState(displayState);
} }

View File

@ -5,7 +5,7 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
import org.solovyev.android.calculator.Calculator; import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.CalculatorEditor; import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.EditorState;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
@ -22,7 +22,7 @@ public final class LongClickEraser implements View.OnTouchListener {
private final GestureDetector gestureDetector; private final GestureDetector gestureDetector;
@Nonnull @Nonnull
private final CalculatorEditor editor = Locator.getInstance().getEditor(); private final Editor editor = Locator.getInstance().getEditor();
@Nonnull @Nonnull
private final Calculator calculator = Locator.getInstance().getCalculator(); private final Calculator calculator = Locator.getInstance().getCalculator();

View File

@ -107,8 +107,8 @@ public class CalculatorWidget extends AppWidgetProvider {
private void updateWidget(@Nonnull Context context, private void updateWidget(@Nonnull Context context,
@Nonnull AppWidgetManager manager, @Nonnull AppWidgetManager manager,
@Nonnull int[] widgetIds) { @Nonnull int[] widgetIds) {
final EditorState editorState = Locator.getInstance().getEditor().getViewState(); final EditorState editorState = Locator.getInstance().getEditor().getState();
final CalculatorDisplayViewState displayState = Locator.getInstance().getDisplay().getViewState(); final DisplayState displayState = Locator.getInstance().getDisplay().getViewState();
final Resources resources = context.getResources(); final Resources resources = context.getResources();
final SimpleTheme theme = App.getWidgetTheme().resolveThemeFor(App.getTheme()); final SimpleTheme theme = App.getWidgetTheme().resolveThemeFor(App.getTheme());
@ -195,7 +195,7 @@ public class CalculatorWidget extends AppWidgetProvider {
} }
} }
private void updateDisplayState(@Nonnull Context context, @Nonnull RemoteViews views, @Nonnull CalculatorDisplayViewState displayState, @Nonnull SimpleTheme theme) { private void updateDisplayState(@Nonnull Context context, @Nonnull RemoteViews views, @Nonnull DisplayState displayState, @Nonnull SimpleTheme theme) {
final boolean error = !displayState.isValid(); final boolean error = !displayState.isValid();
if (!error) { if (!error) {
views.setTextViewText(R.id.calculator_display, displayState.getText()); views.setTextViewText(R.id.calculator_display, displayState.getText());

View File

@ -47,7 +47,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(value = RobolectricGradleTestRunner.class) @RunWith(value = RobolectricGradleTestRunner.class)
public class AndroidCalculatorEditorViewTest { public class AndroidEditorViewTest {
@BeforeClass @BeforeClass
public static void staticSetUp() throws Exception { public static void staticSetUp() throws Exception {

View File

@ -36,11 +36,11 @@ public class CalculatorDisplayViewStateImplTest {
@Test @Test
public void testSerializable() throws Exception { public void testSerializable() throws Exception {
CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "test", 3)); CalculatorTestUtils.testSerialization(DisplayState.createValid(JsclOperation.numeric, null, "test", 3));
CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, Expression.valueOf("3"), "test", 3)); CalculatorTestUtils.testSerialization(DisplayState.createValid(JsclOperation.numeric, Expression.valueOf("3"), "test", 3));
CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newValidState(JsclOperation.simplify, Expression.valueOf("3+3"), "test", 3)); CalculatorTestUtils.testSerialization(DisplayState.createValid(JsclOperation.simplify, Expression.valueOf("3+3"), "test", 3));
CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newDefaultInstance()); CalculatorTestUtils.testSerialization(DisplayState.empty());
CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.numeric, "ertert")); CalculatorTestUtils.testSerialization(DisplayState.createError(JsclOperation.numeric, "ertert"));
} }
} }

View File

@ -76,7 +76,6 @@ public class CalculatorTestUtils {
public static void initViews(@Nonnull Context context) { public static void initViews(@Nonnull Context context) {
final EditorView editor = new EditorView(context); final EditorView editor = new EditorView(context);
editor.init();
Locator.getInstance().getEditor().setView(editor); Locator.getInstance().getEditor().setView(editor);
final AndroidCalculatorDisplayView display = new AndroidCalculatorDisplayView(context); final AndroidCalculatorDisplayView display = new AndroidCalculatorDisplayView(context);
@ -105,7 +104,7 @@ public class CalculatorTestUtils {
public static void assertEval(@Nonnull String expected, @Nonnull String expression, @Nonnull JsclOperation operation) { public static void assertEval(@Nonnull String expected, @Nonnull String expression, @Nonnull JsclOperation operation) {
final Calculator calculator = Locator.getInstance().getCalculator(); final Calculator calculator = Locator.getInstance().getCalculator();
Locator.getInstance().getDisplay().setViewState(CalculatorDisplayViewStateImpl.newDefaultInstance()); Locator.getInstance().getDisplay().setViewState(DisplayState.empty());
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final TestCalculatorEventListener calculatorEventListener = new TestCalculatorEventListener(latch); final TestCalculatorEventListener calculatorEventListener = new TestCalculatorEventListener(latch);
@ -161,7 +160,7 @@ public class CalculatorTestUtils {
public static void assertError(@Nonnull String expression, @Nonnull JsclOperation operation) { public static void assertError(@Nonnull String expression, @Nonnull JsclOperation operation) {
final Calculator calculator = Locator.getInstance().getCalculator(); final Calculator calculator = Locator.getInstance().getCalculator();
Locator.getInstance().getDisplay().setViewState(CalculatorDisplayViewStateImpl.newDefaultInstance()); Locator.getInstance().getDisplay().setViewState(DisplayState.empty());
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final TestCalculatorEventListener calculatorEventListener = new TestCalculatorEventListener(latch); final TestCalculatorEventListener calculatorEventListener = new TestCalculatorEventListener(latch);
@ -190,7 +189,7 @@ public class CalculatorTestUtils {
@Nullable @Nullable
private CalculatorEventData calculatorEventData; private CalculatorEventData calculatorEventData;
@Nullable @Nullable
private volatile CalculatorDisplayViewState result = null; private volatile DisplayState result = null;
public TestCalculatorEventListener(@Nonnull CountDownLatch latch) { public TestCalculatorEventListener(@Nonnull CountDownLatch latch) {
this.latch = latch; this.latch = latch;
@ -230,7 +229,7 @@ public class CalculatorTestUtils {
} }
@Nullable @Nullable
public CalculatorDisplayViewState getResult() { public DisplayState getResult() {
return result; return result;
} }
} }

View File

@ -33,198 +33,198 @@ import javax.annotation.Nonnull;
* Date: 21.09.12 * Date: 21.09.12
* Time: 12:44 * Time: 12:44
*/ */
public class CalculatorEditorImplTest extends AbstractCalculatorTest { public class EditorTest extends AbstractCalculatorTest {
@Nonnull @Nonnull
private CalculatorEditor calculatorEditor; private Editor editor;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
this.calculatorEditor = new CalculatorEditorImpl(Locator.getInstance().getCalculator(), null); this.editor = new Editor(Locator.getInstance().getCalculator(), null);
} }
@Test @Test
public void testInsert() throws Exception { public void testInsert() throws Exception {
CalculatorEditorViewState viewState = this.calculatorEditor.getViewState(); EditorState viewState = this.editor.getState();
Assert.assertEquals("", viewState.getText()); Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.insert(""); viewState = this.editor.insert("");
Assert.assertEquals("", viewState.getText()); Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.insert("test"); viewState = this.editor.insert("test");
Assert.assertEquals("test", viewState.getText()); Assert.assertEquals("test", viewState.getText());
Assert.assertEquals(4, viewState.getSelection()); Assert.assertEquals(4, viewState.getSelection());
viewState = this.calculatorEditor.insert("test"); viewState = this.editor.insert("test");
Assert.assertEquals("testtest", viewState.getText()); Assert.assertEquals("testtest", viewState.getText());
Assert.assertEquals(8, viewState.getSelection()); Assert.assertEquals(8, viewState.getSelection());
viewState = this.calculatorEditor.insert(""); viewState = this.editor.insert("");
Assert.assertEquals("testtest", viewState.getText()); Assert.assertEquals("testtest", viewState.getText());
Assert.assertEquals(8, viewState.getSelection()); Assert.assertEquals(8, viewState.getSelection());
viewState = this.calculatorEditor.insert("1234567890"); viewState = this.editor.insert("1234567890");
Assert.assertEquals("testtest1234567890", viewState.getText()); Assert.assertEquals("testtest1234567890", viewState.getText());
Assert.assertEquals(18, viewState.getSelection()); Assert.assertEquals(18, viewState.getSelection());
viewState = this.calculatorEditor.moveCursorLeft(); viewState = this.editor.moveCursorLeft();
viewState = this.calculatorEditor.insert("9"); viewState = this.editor.insert("9");
Assert.assertEquals("testtest12345678990", viewState.getText()); Assert.assertEquals("testtest12345678990", viewState.getText());
Assert.assertEquals(18, viewState.getSelection()); Assert.assertEquals(18, viewState.getSelection());
viewState = this.calculatorEditor.setCursorOnStart(); viewState = this.editor.setCursorOnStart();
viewState = this.calculatorEditor.insert("9"); viewState = this.editor.insert("9");
Assert.assertEquals("9testtest12345678990", viewState.getText()); Assert.assertEquals("9testtest12345678990", viewState.getText());
Assert.assertEquals(1, viewState.getSelection()); Assert.assertEquals(1, viewState.getSelection());
viewState = this.calculatorEditor.erase(); viewState = this.editor.erase();
viewState = this.calculatorEditor.insert("9"); viewState = this.editor.insert("9");
Assert.assertEquals("9testtest12345678990", viewState.getText()); Assert.assertEquals("9testtest12345678990", viewState.getText());
Assert.assertEquals(1, viewState.getSelection()); Assert.assertEquals(1, viewState.getSelection());
viewState = this.calculatorEditor.insert("öäü"); viewState = this.editor.insert("öäü");
Assert.assertEquals("9öäütesttest12345678990", viewState.getText()); Assert.assertEquals("9öäütesttest12345678990", viewState.getText());
this.calculatorEditor.setCursorOnEnd(); this.editor.setCursorOnEnd();
viewState = this.calculatorEditor.insert("öäü"); viewState = this.editor.insert("öäü");
Assert.assertEquals("9öäütesttest12345678990öäü", viewState.getText()); Assert.assertEquals("9öäütesttest12345678990öäü", viewState.getText());
} }
@Test @Test
public void testErase() throws Exception { public void testErase() throws Exception {
this.calculatorEditor.setText(""); this.editor.setText("");
this.calculatorEditor.erase(); this.editor.erase();
Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); Assert.assertEquals("", this.editor.getState().getText());
this.calculatorEditor.setText("test"); this.editor.setText("test");
this.calculatorEditor.erase(); this.editor.erase();
Assert.assertEquals("tes", this.calculatorEditor.getViewState().getText()); Assert.assertEquals("tes", this.editor.getState().getText());
this.calculatorEditor.erase(); this.editor.erase();
Assert.assertEquals("te", this.calculatorEditor.getViewState().getText()); Assert.assertEquals("te", this.editor.getState().getText());
this.calculatorEditor.erase(); this.editor.erase();
Assert.assertEquals("t", this.calculatorEditor.getViewState().getText()); Assert.assertEquals("t", this.editor.getState().getText());
this.calculatorEditor.erase(); this.editor.erase();
Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); Assert.assertEquals("", this.editor.getState().getText());
this.calculatorEditor.erase(); this.editor.erase();
Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); Assert.assertEquals("", this.editor.getState().getText());
this.calculatorEditor.setText("1234"); this.editor.setText("1234");
this.calculatorEditor.moveCursorLeft(); this.editor.moveCursorLeft();
this.calculatorEditor.erase(); this.editor.erase();
Assert.assertEquals("124", this.calculatorEditor.getViewState().getText()); Assert.assertEquals("124", this.editor.getState().getText());
this.calculatorEditor.erase(); this.editor.erase();
Assert.assertEquals("14", this.calculatorEditor.getViewState().getText()); Assert.assertEquals("14", this.editor.getState().getText());
this.calculatorEditor.erase(); this.editor.erase();
Assert.assertEquals("4", this.calculatorEditor.getViewState().getText()); Assert.assertEquals("4", this.editor.getState().getText());
this.calculatorEditor.setText("1"); this.editor.setText("1");
this.calculatorEditor.moveCursorLeft(); this.editor.moveCursorLeft();
this.calculatorEditor.erase(); this.editor.erase();
Assert.assertEquals("1", this.calculatorEditor.getViewState().getText()); Assert.assertEquals("1", this.editor.getState().getText());
} }
@Test @Test
public void testMoveSelection() throws Exception { public void testMoveSelection() throws Exception {
this.calculatorEditor.setText(""); this.editor.setText("");
CalculatorEditorViewState viewState = this.calculatorEditor.moveSelection(0); EditorState viewState = this.editor.moveSelection(0);
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(2); viewState = this.editor.moveSelection(2);
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(100); viewState = this.editor.moveSelection(100);
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-3); viewState = this.editor.moveSelection(-3);
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-100); viewState = this.editor.moveSelection(-100);
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("0123456789"); viewState = this.editor.setText("0123456789");
viewState = this.calculatorEditor.moveSelection(0); viewState = this.editor.moveSelection(0);
Assert.assertEquals(10, viewState.getSelection()); Assert.assertEquals(10, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(1); viewState = this.editor.moveSelection(1);
Assert.assertEquals(10, viewState.getSelection()); Assert.assertEquals(10, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-2); viewState = this.editor.moveSelection(-2);
Assert.assertEquals(8, viewState.getSelection()); Assert.assertEquals(8, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(1); viewState = this.editor.moveSelection(1);
Assert.assertEquals(9, viewState.getSelection()); Assert.assertEquals(9, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-9); viewState = this.editor.moveSelection(-9);
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-10); viewState = this.editor.moveSelection(-10);
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(2); viewState = this.editor.moveSelection(2);
Assert.assertEquals(2, viewState.getSelection()); Assert.assertEquals(2, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(2); viewState = this.editor.moveSelection(2);
Assert.assertEquals(4, viewState.getSelection()); Assert.assertEquals(4, viewState.getSelection());
viewState = this.calculatorEditor.moveSelection(-6); viewState = this.editor.moveSelection(-6);
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
} }
@Test @Test
public void testSetText() throws Exception { public void testSetText() throws Exception {
CalculatorEditorViewState viewState = this.calculatorEditor.setText("test"); EditorState viewState = this.editor.setText("test");
Assert.assertEquals("test", viewState.getText()); Assert.assertEquals("test", viewState.getText());
Assert.assertEquals(4, viewState.getSelection()); Assert.assertEquals(4, viewState.getSelection());
viewState = this.calculatorEditor.setText("testtest"); viewState = this.editor.setText("testtest");
Assert.assertEquals("testtest", viewState.getText()); Assert.assertEquals("testtest", viewState.getText());
Assert.assertEquals(8, viewState.getSelection()); Assert.assertEquals(8, viewState.getSelection());
viewState = this.calculatorEditor.setText(""); viewState = this.editor.setText("");
Assert.assertEquals("", viewState.getText()); Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("testtest", 0); viewState = this.editor.setText("testtest", 0);
Assert.assertEquals("testtest", viewState.getText()); Assert.assertEquals("testtest", viewState.getText());
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("testtest", 2); viewState = this.editor.setText("testtest", 2);
Assert.assertEquals("testtest", viewState.getText()); Assert.assertEquals("testtest", viewState.getText());
Assert.assertEquals(2, viewState.getSelection()); Assert.assertEquals(2, viewState.getSelection());
viewState = this.calculatorEditor.setText("", 0); viewState = this.editor.setText("", 0);
Assert.assertEquals("", viewState.getText()); Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("", 3); viewState = this.editor.setText("", 3);
Assert.assertEquals("", viewState.getText()); Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("", -3); viewState = this.editor.setText("", -3);
Assert.assertEquals("", viewState.getText()); Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
viewState = this.calculatorEditor.setText("test"); viewState = this.editor.setText("test");
Assert.assertEquals("test", viewState.getText()); Assert.assertEquals("test", viewState.getText());
Assert.assertEquals(4, viewState.getSelection()); Assert.assertEquals(4, viewState.getSelection());
viewState = this.calculatorEditor.setText("", 2); viewState = this.editor.setText("", 2);
Assert.assertEquals("", viewState.getText()); Assert.assertEquals("", viewState.getText());
Assert.assertEquals(0, viewState.getSelection()); Assert.assertEquals(0, viewState.getSelection());
} }

View File

@ -25,7 +25,7 @@ package org.solovyev.android.calculator.history;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl; import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.EditorState;
import org.solovyev.android.calculator.CalculatorTestUtils; import org.solovyev.android.calculator.CalculatorTestUtils;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
@ -64,13 +64,13 @@ public class CalculatorHistoryImplTest {
addState(calculatorHistory, "2354"); addState(calculatorHistory, "2354");
addState(calculatorHistory, "23547"); addState(calculatorHistory, "23547");
final List<CalculatorHistoryState> states = calculatorHistory.getStates(false); final List<HistoryState> states = calculatorHistory.getStates(false);
Assert.assertEquals(2, states.size()); Assert.assertEquals(2, states.size());
Assert.assertEquals("23547", states.get(1).getEditorState().getText()); Assert.assertEquals("23547", states.get(1).getEditorState().getText());
Assert.assertEquals("123+3", states.get(0).getEditorState().getText()); Assert.assertEquals("123+3", states.get(0).getEditorState().getText());
} }
private void addState(@Nonnull CalculatorHistory calculatorHistory, @Nonnull String text) { private void addState(@Nonnull CalculatorHistory calculatorHistory, @Nonnull String text) {
calculatorHistory.addState(CalculatorHistoryState.newInstance(EditorState.create(text, 3), CalculatorDisplayViewStateImpl.newDefaultInstance())); calculatorHistory.addState(HistoryState.create(EditorState.create(text, 3), DisplayState.empty()));
} }
} }

View File

@ -24,8 +24,7 @@ package org.solovyev.android.calculator.history;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl;
import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.EditorState;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.Objects; import org.solovyev.common.Objects;
@ -137,13 +136,13 @@ public class HistoryUtilsTest {
public void testToXml() throws Exception { public void testToXml() throws Exception {
final Date date = new Date(100000000); final Date date = new Date(100000000);
HistoryHelper<CalculatorHistoryState> history = SimpleHistoryHelper.newInstance(); HistoryHelper<HistoryState> history = SimpleHistoryHelper.newInstance();
CalculatorDisplayViewState calculatorDisplay = CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.simplify, "Error"); DisplayState calculatorDisplay = DisplayState.createError(JsclOperation.simplify, "Error");
CalculatorEditorViewState calculatorEditor = EditorState.create("1+1", 3); CalculatorEditorViewState calculatorEditor = EditorState.create("1+1", 3);
CalculatorHistoryState state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); HistoryState state = HistoryState.newInstance(calculatorEditor, calculatorDisplay);
state.setTime(date.getTime()); state.setTime(date.getTime());
history.addState(state); history.addState(state);
@ -154,29 +153,29 @@ public class HistoryUtilsTest {
assertEquals(toXml1, HistoryUtils.toXml(history.getStates())); assertEquals(toXml1, HistoryUtils.toXml(history.getStates()));
calculatorDisplay = CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "5/6", 3); calculatorDisplay = DisplayState.createValid(JsclOperation.numeric, null, "5/6", 3);
calculatorEditor = EditorState.create("5/6", 2); calculatorEditor = EditorState.create("5/6", 2);
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); state = HistoryState.newInstance(calculatorEditor, calculatorDisplay);
state.setSaved(true); state.setSaved(true);
state.setTime(date.getTime()); state.setTime(date.getTime());
history.addState(state); history.addState(state);
calculatorDisplay = CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.elementary, "Error"); calculatorDisplay = DisplayState.createError(JsclOperation.elementary, "Error");
calculatorEditor = EditorState.create("", 1); calculatorEditor = EditorState.create("", 1);
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); state = HistoryState.newInstance(calculatorEditor, calculatorDisplay);
state.setSaved(true); state.setSaved(true);
state.setTime(date.getTime()); state.setTime(date.getTime());
history.addState(state); history.addState(state);
calculatorDisplay = CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "4+5/35sin(41)+dfdsfsdfs", 1); calculatorDisplay = DisplayState.createValid(JsclOperation.numeric, null, "4+5/35sin(41)+dfdsfsdfs", 1);
calculatorEditor = EditorState.create("4+5/35sin(41)+dfdsfsdfs", 0); calculatorEditor = EditorState.create("4+5/35sin(41)+dfdsfsdfs", 0);
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); state = HistoryState.newInstance(calculatorEditor, calculatorDisplay);
state.setSaved(true); state.setSaved(true);
state.setTime(date.getTime()); state.setTime(date.getTime());
history.addState(state); history.addState(state);
@ -184,23 +183,23 @@ public class HistoryUtilsTest {
String xml = HistoryUtils.toXml(history.getStates()); String xml = HistoryUtils.toXml(history.getStates());
assertEquals(toXml2, xml); assertEquals(toXml2, xml);
final List<CalculatorHistoryState> fromXml = new ArrayList<CalculatorHistoryState>(); final List<HistoryState> fromXml = new ArrayList<HistoryState>();
final HistoryHelper<CalculatorHistoryState> historyFromXml = SimpleHistoryHelper.newInstance(); final HistoryHelper<HistoryState> historyFromXml = SimpleHistoryHelper.newInstance();
HistoryUtils.fromXml(xml, fromXml); HistoryUtils.fromXml(xml, fromXml);
for (CalculatorHistoryState historyState : fromXml) { for (HistoryState historyState : fromXml) {
historyFromXml.addState(historyState); historyFromXml.addState(historyState);
} }
assertEquals(history.getStates().size(), historyFromXml.getStates().size()); assertEquals(history.getStates().size(), historyFromXml.getStates().size());
for (CalculatorHistoryState historyState : history.getStates()) { for (HistoryState historyState : history.getStates()) {
historyState.setId(0); historyState.setId(0);
historyState.setSaved(true); historyState.setSaved(true);
} }
for (CalculatorHistoryState historyState : historyFromXml.getStates()) { for (HistoryState historyState : historyFromXml.getStates()) {
historyState.setId(0); historyState.setId(0);
historyState.setSaved(true); historyState.setSaved(true);
} }
Assert.assertTrue(Objects.areEqual(history.getStates(), historyFromXml.getStates(), new CollectionEqualizer<CalculatorHistoryState>(null))); Assert.assertTrue(Objects.areEqual(history.getStates(), historyFromXml.getStates(), new CollectionEqualizer<HistoryState>(null)));
} }
} }