Refactor
This commit is contained in:
parent
d1c068edff
commit
e3c3abf828
@ -27,7 +27,7 @@ import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import jscl.NumeralBase;
|
||||
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.common.history.HistoryAction;
|
||||
import org.solovyev.common.msg.Message;
|
||||
@ -156,12 +156,12 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public CalculatorHistoryState getCurrentHistoryState() {
|
||||
public HistoryState getCurrentHistoryState() {
|
||||
return calculator.getCurrentHistoryState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentHistoryState(@Nonnull CalculatorHistoryState editorHistoryState) {
|
||||
public void setCurrentHistoryState(@Nonnull HistoryState editorHistoryState) {
|
||||
calculator.setCurrentHistoryState(editorHistoryState);
|
||||
}
|
||||
|
||||
|
@ -68,7 +68,7 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
|
||||
@Nonnull
|
||||
private final Handler uiHandler = new Handler();
|
||||
@Nonnull
|
||||
private volatile CalculatorDisplayViewState state = CalculatorDisplayViewStateImpl.newDefaultInstance();
|
||||
private volatile DisplayState state = DisplayState.empty();
|
||||
private volatile boolean initialized = false;
|
||||
|
||||
/*
|
||||
@ -109,14 +109,14 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getState() {
|
||||
public DisplayState getState() {
|
||||
synchronized (lock) {
|
||||
return this.state;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setState(@Nonnull final CalculatorDisplayViewState state) {
|
||||
public void setState(@Nonnull final DisplayState state) {
|
||||
|
||||
uiHandler.post(new Runnable() {
|
||||
@Override
|
||||
|
@ -40,13 +40,12 @@ import org.solovyev.android.views.dragbutton.DragButton;
|
||||
import org.solovyev.android.views.dragbutton.DragDirection;
|
||||
import org.solovyev.android.views.dragbutton.DragListener;
|
||||
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.view.AngleUnitsButton;
|
||||
import org.solovyev.android.calculator.view.LongClickEraser;
|
||||
import org.solovyev.android.calculator.view.NumeralBasesButton;
|
||||
import org.solovyev.android.calculator.view.ViewsCache;
|
||||
import org.solovyev.common.math.Point2d;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -166,7 +165,7 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan
|
||||
final ViewsCache views = ViewsCache.forView(root);
|
||||
setOnDragListeners(views, activity);
|
||||
|
||||
HistoryDragProcessor<CalculatorHistoryState> historyDragProcessor = new HistoryDragProcessor<>(getCalculator());
|
||||
HistoryDragProcessor<HistoryState> historyDragProcessor = new HistoryDragProcessor<>(getCalculator());
|
||||
final DragListener historyDragListener = newDragListener(historyDragProcessor, activity);
|
||||
final DragButton historyButton = getButton(views, R.id.cpp_button_history);
|
||||
if (historyButton != null) {
|
||||
|
@ -22,7 +22,7 @@
|
||||
|
||||
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.common.history.HistoryControl;
|
||||
|
||||
@ -37,7 +37,7 @@ import jscl.math.Generic;
|
||||
* Date: 20.09.12
|
||||
* Time: 16:38
|
||||
*/
|
||||
public interface Calculator extends CalculatorEventContainer, HistoryControl<CalculatorHistoryState> {
|
||||
public interface Calculator extends CalculatorEventContainer, HistoryControl<HistoryState> {
|
||||
|
||||
void init();
|
||||
|
||||
|
@ -130,7 +130,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener
|
||||
|
||||
public static void tryCreateVar(@Nonnull final Context context) {
|
||||
final CalculatorDisplay display = Locator.getInstance().getDisplay();
|
||||
final CalculatorDisplayViewState viewState = display.getViewState();
|
||||
final DisplayState viewState = display.getViewState();
|
||||
if (viewState.isValid()) {
|
||||
final String varValue = viewState.getText();
|
||||
if (!Strings.isEmpty(varValue)) {
|
||||
@ -156,7 +156,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener
|
||||
|
||||
public static void tryCreateFunction(@Nonnull final Context context) {
|
||||
final CalculatorDisplay display = Locator.getInstance().getDisplay();
|
||||
final CalculatorDisplayViewState viewState = display.getViewState();
|
||||
final DisplayState viewState = display.getViewState();
|
||||
|
||||
if (viewState.isValid()) {
|
||||
final String functionValue = viewState.getText();
|
||||
@ -180,7 +180,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener
|
||||
public static void tryPlot() {
|
||||
final CalculatorPlotter plotter = Locator.getInstance().getPlotter();
|
||||
final CalculatorDisplay display = Locator.getInstance().getDisplay();
|
||||
final CalculatorDisplayViewState viewState = display.getViewState();
|
||||
final DisplayState viewState = display.getViewState();
|
||||
|
||||
if (viewState.isValid()) {
|
||||
final String functionValue = viewState.getText();
|
||||
|
@ -36,7 +36,7 @@ public interface CalculatorConversionEventData extends CalculatorEventData {
|
||||
|
||||
// display state on the moment of conversion
|
||||
@Nonnull
|
||||
CalculatorDisplayViewState getDisplayState();
|
||||
DisplayState getDisplayState();
|
||||
|
||||
@Nonnull
|
||||
NumeralBase getFromNumeralBase();
|
||||
|
@ -47,7 +47,7 @@ public class CalculatorConversionEventDataImpl implements CalculatorConversionEv
|
||||
private Generic value;
|
||||
|
||||
@Nonnull
|
||||
private CalculatorDisplayViewState displayState;
|
||||
private DisplayState displayState;
|
||||
|
||||
private CalculatorConversionEventDataImpl() {
|
||||
}
|
||||
@ -57,7 +57,7 @@ public class CalculatorConversionEventDataImpl implements CalculatorConversionEv
|
||||
@Nonnull Generic value,
|
||||
@Nonnull NumeralBase from,
|
||||
@Nonnull NumeralBase to,
|
||||
@Nonnull CalculatorDisplayViewState displayViewState) {
|
||||
@Nonnull DisplayState displayViewState) {
|
||||
final CalculatorConversionEventDataImpl result = new CalculatorConversionEventDataImpl();
|
||||
|
||||
result.calculatorEventData = calculatorEventData;
|
||||
@ -102,7 +102,7 @@ public class CalculatorConversionEventDataImpl implements CalculatorConversionEv
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getDisplayState() {
|
||||
public DisplayState getDisplayState() {
|
||||
return this.displayState;
|
||||
}
|
||||
|
||||
|
@ -40,9 +40,9 @@ public interface CalculatorDisplay extends CalculatorEventListener {
|
||||
void setView(@Nonnull CalculatorDisplayView view);
|
||||
|
||||
@Nonnull
|
||||
CalculatorDisplayViewState getViewState();
|
||||
DisplayState getViewState();
|
||||
|
||||
void setViewState(@Nonnull CalculatorDisplayViewState viewState);
|
||||
void setViewState(@Nonnull DisplayState viewState);
|
||||
|
||||
@Nonnull
|
||||
CalculatorEventData getLastEventData();
|
||||
|
@ -27,5 +27,5 @@ package org.solovyev.android.calculator;
|
||||
* Date: 9/21/12
|
||||
* Time: 9:49 PM
|
||||
*/
|
||||
public interface CalculatorDisplayChangeEventData extends Change<CalculatorDisplayViewState> {
|
||||
public interface CalculatorDisplayChangeEventData extends Change<DisplayState> {
|
||||
}
|
||||
|
@ -32,25 +32,25 @@ import javax.annotation.Nonnull;
|
||||
public class CalculatorDisplayChangeEventDataImpl implements CalculatorDisplayChangeEventData {
|
||||
|
||||
@Nonnull
|
||||
private final CalculatorDisplayViewState oldState;
|
||||
private final DisplayState oldState;
|
||||
|
||||
@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.newState = newState;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getOldValue() {
|
||||
public DisplayState getOldValue() {
|
||||
return this.oldState;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getNewValue() {
|
||||
public DisplayState getNewValue() {
|
||||
return this.newState;
|
||||
}
|
||||
}
|
||||
|
@ -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.display_state_changed;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/12
|
||||
* Time: 8:24 PM
|
||||
*/
|
||||
public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
|
||||
@Nonnull
|
||||
@ -48,7 +43,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
@Nullable
|
||||
private CalculatorDisplayView view;
|
||||
@Nonnull
|
||||
private CalculatorDisplayViewState viewState = CalculatorDisplayViewStateImpl.newDefaultInstance();
|
||||
private DisplayState viewState = DisplayState.empty();
|
||||
|
||||
public CalculatorDisplayImpl(@Nonnull Calculator calculator) {
|
||||
this.calculator = calculator;
|
||||
@ -81,22 +76,22 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorDisplayViewState getViewState() {
|
||||
public DisplayState getViewState() {
|
||||
return this.viewState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setViewState(@Nonnull CalculatorDisplayViewState newViewState) {
|
||||
public void setViewState(@Nonnull DisplayState newViewState) {
|
||||
synchronized (viewLock) {
|
||||
final CalculatorDisplayViewState oldViewState = setViewState0(newViewState);
|
||||
final DisplayState oldViewState = setViewState0(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) {
|
||||
final CalculatorDisplayViewState oldViewState = setViewState0(newViewState);
|
||||
final DisplayState oldViewState = setViewState0(newViewState);
|
||||
|
||||
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
|
||||
@Nonnull
|
||||
private CalculatorDisplayViewState setViewState0(@Nonnull CalculatorDisplayViewState newViewState) {
|
||||
final CalculatorDisplayViewState oldViewState = this.viewState;
|
||||
private DisplayState setViewState0(@Nonnull DisplayState newViewState) {
|
||||
final DisplayState oldViewState = this.viewState;
|
||||
|
||||
this.viewState = newViewState;
|
||||
if (this.view != null) {
|
||||
@ -152,7 +147,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
|
||||
private void processConversationFailed(@Nonnull CalculatorConversionEventData calculatorEventData,
|
||||
@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) {
|
||||
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) {
|
||||
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) {
|
||||
@ -192,7 +187,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay {
|
||||
result = calculatorEventData.getToNumeralBase().getJsclPrefix() + result;
|
||||
}
|
||||
|
||||
final CalculatorDisplayViewState displayState = calculatorEventData.getDisplayState();
|
||||
this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(displayState.getOperation(), displayState.getResult(), result, 0), calculatorEventData.getSequenceId());
|
||||
final DisplayState displayState = calculatorEventData.getDisplayState();
|
||||
this.setViewStateForSequence(DisplayState.createValid(displayState.getOperation(), displayState.getResult(), result, 0), calculatorEventData.getSequenceId());
|
||||
}
|
||||
}
|
||||
|
@ -38,18 +38,18 @@ import jscl.math.Generic;
|
||||
* Date: 21.09.12
|
||||
* Time: 10:55
|
||||
*/
|
||||
public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDisplayViewState> {
|
||||
public enum CalculatorDisplayMenuItem implements LabeledMenuItem<DisplayState> {
|
||||
|
||||
copy(R.string.c_copy) {
|
||||
@Override
|
||||
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) {
|
||||
public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
|
||||
Locator.getInstance().getKeyboard().copyButtonPressed();
|
||||
}
|
||||
},
|
||||
|
||||
convert_to_bin(R.string.convert_to_bin) {
|
||||
@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);
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDispl
|
||||
|
||||
convert_to_dec(R.string.convert_to_dec) {
|
||||
@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);
|
||||
}
|
||||
|
||||
@ -73,7 +73,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDispl
|
||||
|
||||
convert_to_hex(R.string.convert_to_hex) {
|
||||
@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);
|
||||
}
|
||||
|
||||
@ -85,7 +85,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDispl
|
||||
|
||||
convert(R.string.c_convert) {
|
||||
@Override
|
||||
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) {
|
||||
public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
|
||||
final Generic result = data.getResult();
|
||||
if (result != null) {
|
||||
new NumeralBaseConverterDialog(result.toString()).show(context);
|
||||
@ -100,7 +100,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDispl
|
||||
|
||||
plot(R.string.c_plot) {
|
||||
@Override
|
||||
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) {
|
||||
public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
|
||||
final Generic expression = data.getResult();
|
||||
if (expression == null) throw new AssertionError();
|
||||
|
||||
@ -121,7 +121,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem<CalculatorDispl
|
||||
this.captionId = captionId;
|
||||
}
|
||||
|
||||
public final boolean isItemVisible(@Nonnull CalculatorDisplayViewState displayViewState) {
|
||||
public final boolean isItemVisible(@Nonnull DisplayState displayViewState) {
|
||||
//noinspection ConstantConditions
|
||||
return displayViewState.isValid() && displayViewState.getResult() != null && isItemVisibleFor(displayViewState.getResult(), displayViewState.getOperation());
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ public class CalculatorDisplayOnClickListener implements View.OnClickListener {
|
||||
if (v instanceof CalculatorDisplayView) {
|
||||
final CalculatorDisplay cd = Locator.getInstance().getDisplay();
|
||||
|
||||
final CalculatorDisplayViewState displayViewState = cd.getViewState();
|
||||
final DisplayState displayViewState = cd.getViewState();
|
||||
|
||||
if (displayViewState.isValid()) {
|
||||
final List<CalculatorDisplayMenuItem> filteredMenuItems = new ArrayList<CalculatorDisplayMenuItem>(CalculatorDisplayMenuItem.values().length);
|
||||
|
@ -32,7 +32,7 @@ import javax.annotation.Nonnull;
|
||||
public interface CalculatorDisplayView {
|
||||
|
||||
@Nonnull
|
||||
CalculatorDisplayViewState getState();
|
||||
DisplayState getState();
|
||||
|
||||
void setState(@Nonnull CalculatorDisplayViewState state);
|
||||
void setState(@Nonnull DisplayState state);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
@ -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);
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -31,7 +31,7 @@ import jscl.math.function.IConstant;
|
||||
import jscl.math.operator.Operator;
|
||||
import jscl.text.ParseInterruptedException;
|
||||
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.model.Var;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
@ -173,21 +173,21 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
|
||||
@Override
|
||||
public void evaluate() {
|
||||
final EditorState viewState = getEditor().getViewState();
|
||||
final EditorState viewState = getEditor().getState();
|
||||
final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.numeric, viewState.getText(), eventData.getSequenceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate(@Nonnull Long sequenceId) {
|
||||
final EditorState viewState = getEditor().getViewState();
|
||||
final EditorState viewState = getEditor().getState();
|
||||
fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState, sequenceId);
|
||||
this.evaluate(JsclOperation.numeric, viewState.getText(), sequenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void simplify() {
|
||||
final EditorState viewState = getEditor().getViewState();
|
||||
final EditorState viewState = getEditor().getState();
|
||||
final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState);
|
||||
this.evaluate(JsclOperation.simplify, viewState.getText(), eventData.getSequenceId());
|
||||
}
|
||||
@ -250,7 +250,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
@Nonnull Generic value,
|
||||
@Nonnull NumeralBase from,
|
||||
@Nonnull NumeralBase to,
|
||||
@Nonnull CalculatorDisplayViewState displayViewState) {
|
||||
@Nonnull DisplayState displayViewState) {
|
||||
return CalculatorConversionEventDataImpl.newInstance(nextEventData(sequenceId), value, from, to, displayViewState);
|
||||
}
|
||||
|
||||
@ -393,7 +393,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
@Nonnull final NumeralBase to) {
|
||||
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();
|
||||
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
@ -562,7 +562,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
}
|
||||
|
||||
private void onDisplayStateChanged(@Nonnull CalculatorDisplayChangeEventData displayChangeEventData) {
|
||||
final CalculatorDisplayViewState newState = displayChangeEventData.getNewValue();
|
||||
final DisplayState newState = displayChangeEventData.getNewValue();
|
||||
if (newState.isValid()) {
|
||||
final String result = newState.getStringResult();
|
||||
if (!Strings.isEmpty(result)) {
|
||||
@ -597,7 +597,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
public void doHistoryAction(@Nonnull HistoryAction historyAction) {
|
||||
final CalculatorHistory history = Locator.getInstance().getHistory();
|
||||
if (history.isActionAvailable(historyAction)) {
|
||||
final CalculatorHistoryState newState = history.doAction(historyAction, getCurrentHistoryState());
|
||||
final HistoryState newState = history.doAction(historyAction, getCurrentHistoryState());
|
||||
if (newState != null) {
|
||||
setCurrentHistoryState(newState);
|
||||
}
|
||||
@ -606,12 +606,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorHistoryState getCurrentHistoryState() {
|
||||
return CalculatorHistoryState.newInstance(getEditor(), getDisplay());
|
||||
public HistoryState getCurrentHistoryState() {
|
||||
return HistoryState.create(getEditor(), getDisplay());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentHistoryState(@Nonnull CalculatorHistoryState editorHistoryState) {
|
||||
public void setCurrentHistoryState(@Nonnull HistoryState editorHistoryState) {
|
||||
editorHistoryState.setValuesFromHistory(getEditor(), getDisplay());
|
||||
}
|
||||
|
||||
@ -624,7 +624,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
*/
|
||||
|
||||
@Nonnull
|
||||
private CalculatorEditor getEditor() {
|
||||
private Editor getEditor() {
|
||||
return Locator.getInstance().getEditor();
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
@ -110,8 +110,8 @@ public class CalculatorKeyboardImpl implements CalculatorKeyboard {
|
||||
|
||||
@Override
|
||||
public void roundBracketsButtonPressed() {
|
||||
final CalculatorEditor editor = Locator.getInstance().getEditor();
|
||||
EditorState viewState = editor.getViewState();
|
||||
final Editor editor = Locator.getInstance().getEditor();
|
||||
EditorState viewState = editor.getState();
|
||||
|
||||
final int cursorPosition = viewState.getSelection();
|
||||
final String oldText = viewState.getText();
|
||||
@ -139,7 +139,7 @@ public class CalculatorKeyboardImpl implements CalculatorKeyboard {
|
||||
|
||||
@Override
|
||||
public void copyButtonPressed() {
|
||||
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState();
|
||||
final DisplayState displayViewState = Locator.getInstance().getDisplay().getViewState();
|
||||
if (displayViewState.isValid()) {
|
||||
final CharSequence text = displayViewState.getText();
|
||||
if (!Strings.isEmpty(text)) {
|
||||
|
@ -58,7 +58,7 @@ public interface CalculatorLocator {
|
||||
CalculatorDisplay getDisplay();
|
||||
|
||||
@Nonnull
|
||||
CalculatorEditor getEditor();
|
||||
Editor getEditor();
|
||||
|
||||
@Nonnull
|
||||
CalculatorKeyboard getKeyboard();
|
||||
|
@ -115,7 +115,7 @@ public enum CalculatorSpecialButton {
|
||||
return;
|
||||
}
|
||||
|
||||
final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState();
|
||||
final DisplayState displayViewState = Locator.getInstance().getDisplay().getViewState();
|
||||
if (displayViewState.isValid()) {
|
||||
final CharSequence text = displayViewState.getText();
|
||||
if (!Strings.isEmpty(text)) {
|
||||
|
@ -37,7 +37,7 @@ import jscl.math.Generic;
|
||||
* Date: 9/21/12
|
||||
* Time: 12:11 AM
|
||||
*/
|
||||
enum ConversionMenuItem implements AMenuItem<CalculatorDisplayViewState> {
|
||||
enum ConversionMenuItem implements AMenuItem<DisplayState> {
|
||||
|
||||
convert_to_bin(NumeralBase.bin),
|
||||
convert_to_dec(NumeralBase.dec),
|
||||
@ -67,7 +67,7 @@ enum ConversionMenuItem implements AMenuItem<CalculatorDisplayViewState> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) {
|
||||
public void onClick(@Nonnull DisplayState data, @Nonnull Context context) {
|
||||
final Generic result = data.getResult();
|
||||
|
||||
if (result != null) {
|
||||
|
@ -25,25 +25,14 @@ package org.solovyev.android.calculator;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
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 class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewState {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
public class DisplayState implements Serializable {
|
||||
|
||||
@Nonnull
|
||||
private JsclOperation operation = JsclOperation.numeric;
|
||||
@ -61,26 +50,18 @@ public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewStat
|
||||
|
||||
private int selection = 0;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private CalculatorDisplayViewStateImpl() {
|
||||
private DisplayState() {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static CalculatorDisplayViewState newDefaultInstance() {
|
||||
return new CalculatorDisplayViewStateImpl();
|
||||
public static DisplayState empty() {
|
||||
return new DisplayState();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static CalculatorDisplayViewState newErrorState(@Nonnull JsclOperation operation,
|
||||
public static DisplayState createError(@Nonnull JsclOperation operation,
|
||||
@Nonnull String errorMessage) {
|
||||
final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl();
|
||||
final DisplayState calculatorDisplayState = new DisplayState();
|
||||
calculatorDisplayState.valid = false;
|
||||
calculatorDisplayState.errorMessage = errorMessage;
|
||||
calculatorDisplayState.operation = operation;
|
||||
@ -88,11 +69,11 @@ public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewStat
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static CalculatorDisplayViewState newValidState(@Nonnull JsclOperation operation,
|
||||
public static DisplayState createValid(@Nonnull JsclOperation operation,
|
||||
@Nullable Generic result,
|
||||
@Nonnull String stringResult,
|
||||
int selection) {
|
||||
final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl();
|
||||
final DisplayState calculatorDisplayState = new DisplayState();
|
||||
calculatorDisplayState.valid = true;
|
||||
calculatorDisplayState.result = result;
|
||||
calculatorDisplayState.stringResult = stringResult;
|
||||
@ -102,50 +83,35 @@ public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewStat
|
||||
return calculatorDisplayState;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getText() {
|
||||
return Strings.getNotEmpty(isValid() ? stringResult : errorMessage, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelection() {
|
||||
return selection;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Generic getResult() {
|
||||
return this.result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return this.valid;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getErrorMessage() {
|
||||
return this.errorMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getStringResult() {
|
||||
return stringResult;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public JsclOperation getOperation() {
|
||||
return this.operation;
|
||||
}
|
265
app/src/main/java/org/solovyev/android/calculator/Editor.java
Normal file
265
app/src/main/java/org/solovyev/android/calculator/Editor.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -31,22 +31,23 @@ import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.ContextMenu;
|
||||
import android.widget.EditText;
|
||||
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class EditorView extends EditText {
|
||||
|
||||
@Nonnull
|
||||
private static final String TAG = App.subTag("EditorView");
|
||||
|
||||
private boolean initialized;
|
||||
private boolean reportChanges;
|
||||
@Nullable
|
||||
private Method setShowSoftInputOnFocusMethod;
|
||||
private boolean externalChange;
|
||||
|
||||
public EditorView(Context context) {
|
||||
super(context);
|
||||
@ -70,10 +71,10 @@ public class EditorView extends EditText {
|
||||
}
|
||||
|
||||
private void init() {
|
||||
Check.isTrue(!initialized);
|
||||
addTextChangedListener(new MyTextWatcher());
|
||||
setShowSoftInputOnFocusCompat(false);
|
||||
initialized = true;
|
||||
// changes should only be reported after the view has been set up completely, i.e. now
|
||||
reportChanges = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -85,24 +86,25 @@ public class EditorView extends EditText {
|
||||
public void setState(@Nonnull final EditorState state) {
|
||||
Check.isMainThread();
|
||||
try {
|
||||
externalChange = true;
|
||||
// we don't want to be notified about changes we make ourselves
|
||||
reportChanges = false;
|
||||
if (App.getTheme().light && getContext() instanceof CalculatorOnscreenService) {
|
||||
// don't need formatting
|
||||
setText(state.getText());
|
||||
} else {
|
||||
setText(state.getTextAsCharSequence(), BufferType.EDITABLE);
|
||||
}
|
||||
final int selection = CalculatorEditorImpl.clamp(state.getSelection(), length());
|
||||
final int selection = Editor.clamp(state.getSelection(), length());
|
||||
setSelection(selection);
|
||||
} finally {
|
||||
externalChange = false;
|
||||
reportChanges = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSelectionChanged(int start, int end) {
|
||||
Check.isMainThread();
|
||||
if (!initialized || externalChange) {
|
||||
if (!reportChanges) {
|
||||
return;
|
||||
}
|
||||
// external text change => need to notify editor
|
||||
@ -147,7 +149,7 @@ public class EditorView extends EditText {
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
if (!initialized || externalChange) {
|
||||
if (!reportChanges) {
|
||||
return;
|
||||
}
|
||||
// external text change => need to notify editor
|
||||
|
@ -44,7 +44,7 @@ public class Locator implements CalculatorLocator {
|
||||
@Nonnull
|
||||
private Calculator calculator;
|
||||
@Nonnull
|
||||
private CalculatorEditor calculatorEditor;
|
||||
private Editor editor;
|
||||
@Nonnull
|
||||
private CalculatorDisplay calculatorDisplay;
|
||||
@Nonnull
|
||||
@ -92,7 +92,7 @@ public class Locator implements CalculatorLocator {
|
||||
this.calculatorPreferenceService = preferenceService;
|
||||
this.calculatorPlotter = plotter;
|
||||
|
||||
calculatorEditor = new CalculatorEditorImpl(this.calculator, editorTextProcessor);
|
||||
editor = new Editor(this.calculator, editorTextProcessor);
|
||||
calculatorDisplay = new CalculatorDisplayImpl(this.calculator);
|
||||
calculatorKeyboard = keyboard;
|
||||
}
|
||||
@ -117,8 +117,8 @@ public class Locator implements CalculatorLocator {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CalculatorEditor getEditor() {
|
||||
return calculatorEditor;
|
||||
public Editor getEditor() {
|
||||
return editor;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -36,11 +36,11 @@ import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorEventData;
|
||||
import org.solovyev.android.calculator.CalculatorEventListener;
|
||||
import org.solovyev.android.calculator.CalculatorEventType;
|
||||
import org.solovyev.android.calculator.CalculatorUtils;
|
||||
import org.solovyev.android.calculator.DisplayState;
|
||||
import org.solovyev.android.calculator.Locator;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity;
|
||||
@ -308,7 +308,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static Input newFromDisplay(@Nonnull CalculatorDisplayViewState viewState) {
|
||||
public static Input newFromDisplay(@Nonnull DisplayState viewState) {
|
||||
final Input result = new Input();
|
||||
|
||||
result.content = viewState.getText();
|
||||
|
@ -80,7 +80,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
|
||||
save();
|
||||
}
|
||||
|
||||
public void removeSavedHistory(@Nonnull CalculatorHistoryState historyState) {
|
||||
public void removeSavedHistory(@Nonnull HistoryState historyState) {
|
||||
historyState.setSaved(false);
|
||||
calculatorHistory.removeSavedHistory(historyState);
|
||||
save();
|
||||
@ -92,7 +92,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState getLastHistoryState() {
|
||||
public HistoryState getLastHistoryState() {
|
||||
return calculatorHistory.getLastHistoryState();
|
||||
}
|
||||
|
||||
@ -102,7 +102,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) {
|
||||
public HistoryState undo(@Nullable HistoryState currentState) {
|
||||
return calculatorHistory.undo(currentState);
|
||||
}
|
||||
|
||||
@ -112,7 +112,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) {
|
||||
public HistoryState redo(@Nullable HistoryState currentState) {
|
||||
return calculatorHistory.redo(currentState);
|
||||
}
|
||||
|
||||
@ -122,24 +122,24 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) {
|
||||
public HistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable HistoryState currentState) {
|
||||
return calculatorHistory.doAction(historyAction, currentState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addState(@Nullable CalculatorHistoryState currentState) {
|
||||
public void addState(@Nullable HistoryState currentState) {
|
||||
calculatorHistory.addState(currentState);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates() {
|
||||
public List<HistoryState> getStates() {
|
||||
return calculatorHistory.getStates();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates(boolean includeIntermediateStates) {
|
||||
public List<HistoryState> getStates(boolean includeIntermediateStates) {
|
||||
return calculatorHistory.getStates(includeIntermediateStates);
|
||||
}
|
||||
|
||||
@ -149,12 +149,12 @@ public class AndroidCalculatorHistory implements CalculatorHistory {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public List<CalculatorHistoryState> getSavedHistory() {
|
||||
public List<HistoryState> getSavedHistory() {
|
||||
return calculatorHistory.getSavedHistory();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CalculatorHistoryState addSavedState(@Nonnull CalculatorHistoryState historyState) {
|
||||
public HistoryState addSavedState(@Nonnull HistoryState historyState) {
|
||||
return calculatorHistory.addSavedState(historyState);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) {
|
||||
public int compare(HistoryState state1, HistoryState state2) {
|
||||
if (state1.isSaved() == state2.isSaved()) {
|
||||
long l = state2.getTime() - state1.getTime();
|
||||
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);
|
||||
}
|
||||
|
||||
public static boolean isAlreadySaved(@Nonnull CalculatorHistoryState historyState) {
|
||||
public static boolean isAlreadySaved(@Nonnull HistoryState historyState) {
|
||||
if (historyState.isSaved()) throw new AssertionError();
|
||||
|
||||
boolean result = false;
|
||||
try {
|
||||
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
|
||||
public boolean areEqual(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) {
|
||||
public boolean areEqual(@Nullable HistoryState first, @Nullable HistoryState second) {
|
||||
return first != null && second != null &&
|
||||
first.getTime() == second.getTime() &&
|
||||
first.getDisplayState().equals(second.getDisplayState()) &&
|
||||
@ -160,13 +160,13 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void useHistoryItem(@Nonnull final CalculatorHistoryState historyState) {
|
||||
public static void useHistoryItem(@Nonnull final HistoryState historyState) {
|
||||
App.getVibrator().vibrate();
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, historyState);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static String getHistoryText(@Nonnull CalculatorHistoryState state) {
|
||||
public static String getHistoryText(@Nonnull HistoryState state) {
|
||||
final StringBuilder result = new StringBuilder();
|
||||
result.append(state.getEditorState().getText());
|
||||
result.append(getIdentitySign(state.getDisplayState().getJsclOperation()));
|
||||
@ -211,7 +211,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
||||
|
||||
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);
|
||||
|
||||
final ListView lv = getListView();
|
||||
@ -232,14 +232,14 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
||||
final int position,
|
||||
final long id) {
|
||||
|
||||
useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position));
|
||||
useHistoryItem((HistoryState) parent.getItemAtPosition(position));
|
||||
}
|
||||
});
|
||||
|
||||
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
||||
@Override
|
||||
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();
|
||||
|
||||
@ -309,13 +309,13 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
||||
protected abstract int getItemLayoutId();
|
||||
|
||||
private void updateAdapter() {
|
||||
final List<CalculatorHistoryState> historyList = getHistoryList();
|
||||
final List<HistoryState> historyList = getHistoryList();
|
||||
|
||||
final ArrayAdapter<CalculatorHistoryState> adapter = getAdapter();
|
||||
final ArrayAdapter<HistoryState> adapter = getAdapter();
|
||||
try {
|
||||
adapter.setNotifyOnChange(false);
|
||||
adapter.clear();
|
||||
for (CalculatorHistoryState historyState : historyList) {
|
||||
for (HistoryState historyState : historyList) {
|
||||
adapter.add(historyState);
|
||||
}
|
||||
} finally {
|
||||
@ -326,26 +326,26 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private List<CalculatorHistoryState> getHistoryList() {
|
||||
final List<CalculatorHistoryState> calculatorHistoryStates = getHistoryItems();
|
||||
private List<HistoryState> getHistoryList() {
|
||||
final List<HistoryState> historyStates = getHistoryItems();
|
||||
|
||||
java.util.Collections.sort(calculatorHistoryStates, COMPARATOR);
|
||||
java.util.Collections.sort(historyStates, COMPARATOR);
|
||||
|
||||
final FilterRulesChain<CalculatorHistoryState> filterRulesChain = new FilterRulesChain<>();
|
||||
filterRulesChain.addFilterRule(new JPredicate<CalculatorHistoryState>() {
|
||||
final FilterRulesChain<HistoryState> filterRulesChain = new FilterRulesChain<>();
|
||||
filterRulesChain.addFilterRule(new JPredicate<HistoryState>() {
|
||||
@Override
|
||||
public boolean apply(CalculatorHistoryState object) {
|
||||
public boolean apply(HistoryState object) {
|
||||
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
|
||||
protected abstract List<CalculatorHistoryState> getHistoryItems();
|
||||
protected abstract List<HistoryState> getHistoryItems();
|
||||
|
||||
protected abstract void clearHistory();
|
||||
|
||||
|
@ -29,12 +29,7 @@ import java.util.Date;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/15/11
|
||||
* Time: 1:45 PM
|
||||
*/
|
||||
public class AbstractHistoryState implements Cloneable {
|
||||
public class BaseHistoryState implements Cloneable {
|
||||
|
||||
@Element
|
||||
private long time = new Date().getTime();
|
||||
@ -82,16 +77,13 @@ public class AbstractHistoryState implements Cloneable {
|
||||
this.saved = saved;
|
||||
}
|
||||
|
||||
@SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException")
|
||||
@Override
|
||||
protected AbstractHistoryState clone() {
|
||||
AbstractHistoryState clone;
|
||||
|
||||
protected BaseHistoryState clone() {
|
||||
try {
|
||||
clone = (AbstractHistoryState) super.clone();
|
||||
return (BaseHistoryState) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
|
||||
return clone;
|
||||
}
|
||||
}
|
@ -34,7 +34,7 @@ import javax.annotation.Nonnull;
|
||||
* Date: 20.09.12
|
||||
* Time: 16:11
|
||||
*/
|
||||
public interface CalculatorHistory extends HistoryHelper<CalculatorHistoryState>, CalculatorEventListener {
|
||||
public interface CalculatorHistory extends HistoryHelper<HistoryState>, CalculatorEventListener {
|
||||
|
||||
void load();
|
||||
|
||||
@ -46,18 +46,18 @@ public interface CalculatorHistory extends HistoryHelper<CalculatorHistoryState>
|
||||
|
||||
void clearSavedHistory();
|
||||
|
||||
void removeSavedHistory(@Nonnull CalculatorHistoryState historyState);
|
||||
void removeSavedHistory(@Nonnull HistoryState historyState);
|
||||
|
||||
@Nonnull
|
||||
List<CalculatorHistoryState> getSavedHistory();
|
||||
List<HistoryState> getSavedHistory();
|
||||
|
||||
@Nonnull
|
||||
CalculatorHistoryState addSavedState(@Nonnull CalculatorHistoryState historyState);
|
||||
HistoryState addSavedState(@Nonnull HistoryState historyState);
|
||||
|
||||
@Nonnull
|
||||
List<CalculatorHistoryState> getStates();
|
||||
List<HistoryState> getStates();
|
||||
|
||||
@Nonnull
|
||||
List<CalculatorHistoryState> getStates(boolean includeIntermediateStates);
|
||||
List<HistoryState> getStates(boolean includeIntermediateStates);
|
||||
|
||||
}
|
||||
|
@ -47,10 +47,10 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
private final AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
@Nonnull
|
||||
private final HistoryHelper<CalculatorHistoryState> history = SimpleHistoryHelper.newInstance();
|
||||
private final HistoryHelper<HistoryState> history = SimpleHistoryHelper.newInstance();
|
||||
|
||||
@Nonnull
|
||||
private final List<CalculatorHistoryState> savedHistory = new ArrayList<CalculatorHistoryState>();
|
||||
private final List<HistoryState> savedHistory = new ArrayList<HistoryState>();
|
||||
|
||||
@Nonnull
|
||||
private final CalculatorEventHolder lastEventData = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
||||
@ -70,7 +70,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState getLastHistoryState() {
|
||||
public HistoryState getLastHistoryState() {
|
||||
synchronized (history) {
|
||||
return this.history.getLastHistoryState();
|
||||
}
|
||||
@ -84,7 +84,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) {
|
||||
public HistoryState undo(@Nullable HistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.undo(currentState);
|
||||
}
|
||||
@ -96,7 +96,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) {
|
||||
public HistoryState redo(@Nullable HistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.redo(currentState);
|
||||
}
|
||||
@ -110,14 +110,14 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorHistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) {
|
||||
public HistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable HistoryState currentState) {
|
||||
synchronized (history) {
|
||||
return history.doAction(historyAction, currentState);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addState(@Nullable CalculatorHistoryState currentState) {
|
||||
public void addState(@Nullable HistoryState currentState) {
|
||||
synchronized (history) {
|
||||
history.addState(currentState);
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, currentState);
|
||||
@ -126,7 +126,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates() {
|
||||
public List<HistoryState> getStates() {
|
||||
synchronized (history) {
|
||||
return history.getStates();
|
||||
}
|
||||
@ -134,17 +134,17 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<CalculatorHistoryState> getStates(boolean includeIntermediateStates) {
|
||||
public List<HistoryState> getStates(boolean includeIntermediateStates) {
|
||||
synchronized (history) {
|
||||
if (includeIntermediateStates) {
|
||||
return getStates();
|
||||
} 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;
|
||||
for (CalculatorHistoryState state : org.solovyev.common.collections.Collections.reversed(states)) {
|
||||
HistoryState laterState = null;
|
||||
for (HistoryState state : org.solovyev.common.collections.Collections.reversed(states)) {
|
||||
if (laterState != null) {
|
||||
final String laterEditorText = laterState.getEditorState().getText();
|
||||
final String editorText = state.getEditorState().getText();
|
||||
@ -187,17 +187,17 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public List<CalculatorHistoryState> getSavedHistory() {
|
||||
public List<HistoryState> getSavedHistory() {
|
||||
return Collections.unmodifiableList(savedHistory);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public CalculatorHistoryState addSavedState(@Nonnull CalculatorHistoryState historyState) {
|
||||
public HistoryState addSavedState(@Nonnull HistoryState historyState) {
|
||||
if (historyState.isSaved()) {
|
||||
return historyState;
|
||||
} else {
|
||||
final CalculatorHistoryState savedState = historyState.clone();
|
||||
final HistoryState savedState = historyState.clone();
|
||||
|
||||
savedState.setId(counter.incrementAndGet());
|
||||
savedState.setSaved(true);
|
||||
@ -223,7 +223,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
clearSavedHistory();
|
||||
|
||||
HistoryUtils.fromXml(xml, this.savedHistory);
|
||||
for (CalculatorHistoryState historyState : savedHistory) {
|
||||
for (HistoryState historyState : savedHistory) {
|
||||
historyState.setSaved(true);
|
||||
historyState.setId(counter.incrementAndGet());
|
||||
}
|
||||
@ -240,7 +240,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSavedHistory(@Nonnull CalculatorHistoryState historyState) {
|
||||
public void removeSavedHistory(@Nonnull HistoryState historyState) {
|
||||
this.savedHistory.remove(historyState);
|
||||
}
|
||||
|
||||
@ -266,8 +266,8 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
|
||||
if (lastEditorViewState != null) {
|
||||
final EditorState editorViewState = lastEditorViewState;
|
||||
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data;
|
||||
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewValue();
|
||||
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState));
|
||||
final DisplayState displayViewState = displayChangeData.getNewValue();
|
||||
addState(HistoryState.create(editorViewState, displayViewState));
|
||||
}
|
||||
} else {
|
||||
lastEditorViewState = null;
|
||||
|
@ -26,8 +26,7 @@ import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.simpleframework.xml.Transient;
|
||||
import org.solovyev.android.calculator.CalculatorDisplay;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl;
|
||||
import org.solovyev.android.calculator.DisplayState;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
@ -43,7 +42,7 @@ import jscl.math.Generic;
|
||||
*/
|
||||
|
||||
@Root
|
||||
public class CalculatorDisplayHistoryState implements Cloneable {
|
||||
public class DisplayHistoryState implements Cloneable {
|
||||
|
||||
@Transient
|
||||
private boolean valid = true;
|
||||
@ -64,15 +63,15 @@ public class CalculatorDisplayHistoryState implements Cloneable {
|
||||
@Nullable
|
||||
private Generic genericResult;
|
||||
|
||||
private CalculatorDisplayHistoryState() {
|
||||
private DisplayHistoryState() {
|
||||
// for xml
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static CalculatorDisplayHistoryState newInstance(@Nonnull CalculatorDisplayViewState viewState) {
|
||||
final CalculatorDisplayHistoryState result = new CalculatorDisplayHistoryState();
|
||||
public static DisplayHistoryState newInstance(@Nonnull DisplayState viewState) {
|
||||
final DisplayHistoryState result = new DisplayHistoryState();
|
||||
|
||||
result.editorState = EditorHistoryState.newInstance(viewState);
|
||||
result.editorState = EditorHistoryState.create(viewState);
|
||||
|
||||
result.valid = viewState.isValid();
|
||||
result.jsclOperation = viewState.getOperation();
|
||||
@ -84,9 +83,9 @@ public class CalculatorDisplayHistoryState implements Cloneable {
|
||||
|
||||
public void setValuesFromHistory(@Nonnull CalculatorDisplay display) {
|
||||
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 {
|
||||
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 (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
CalculatorDisplayHistoryState that = (CalculatorDisplayHistoryState) o;
|
||||
DisplayHistoryState that = (DisplayHistoryState) o;
|
||||
|
||||
if (!editorState.equals(that.editorState)) return false;
|
||||
if (jsclOperation != that.jsclOperation) return false;
|
||||
@ -147,9 +146,9 @@ public class CalculatorDisplayHistoryState implements Cloneable {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CalculatorDisplayHistoryState clone() {
|
||||
protected DisplayHistoryState clone() {
|
||||
try {
|
||||
final CalculatorDisplayHistoryState clone = (CalculatorDisplayHistoryState) super.clone();
|
||||
final DisplayHistoryState clone = (DisplayHistoryState) super.clone();
|
||||
|
||||
clone.editorState = this.editorState.clone();
|
||||
|
@ -24,8 +24,8 @@ package org.solovyev.android.calculator.history;
|
||||
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorEditor;
|
||||
import org.solovyev.android.calculator.DisplayState;
|
||||
import org.solovyev.android.calculator.Editor;
|
||||
import org.solovyev.android.calculator.EditorState;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
@ -47,17 +47,17 @@ public class EditorHistoryState implements Cloneable {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static EditorHistoryState newInstance(@Nonnull EditorState viewState) {
|
||||
public static EditorHistoryState create(@Nonnull EditorState state) {
|
||||
final EditorHistoryState result = new EditorHistoryState();
|
||||
|
||||
result.text = String.valueOf(viewState.getText());
|
||||
result.cursorPosition = viewState.getSelection();
|
||||
result.text = String.valueOf(state.getText());
|
||||
result.cursorPosition = state.getSelection();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static EditorHistoryState newInstance(@Nonnull CalculatorDisplayViewState viewState) {
|
||||
public static EditorHistoryState create(@Nonnull DisplayState viewState) {
|
||||
final EditorHistoryState result = new EditorHistoryState();
|
||||
|
||||
result.text = viewState.getText();
|
||||
@ -66,7 +66,7 @@ public class EditorHistoryState implements Cloneable {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setValuesFromHistory(@Nonnull CalculatorEditor editor) {
|
||||
public void setValuesFromHistory(@Nonnull Editor editor) {
|
||||
editor.setText(Strings.getNotEmpty(this.getText(), ""));
|
||||
editor.setSelection(this.getCursorPosition());
|
||||
}
|
||||
|
@ -37,13 +37,13 @@ import java.util.List;
|
||||
@Root
|
||||
public class History {
|
||||
|
||||
@ElementList(type = CalculatorHistoryState.class)
|
||||
private List<CalculatorHistoryState> historyItems = new ArrayList<CalculatorHistoryState>();
|
||||
@ElementList(type = HistoryState.class)
|
||||
private List<HistoryState> historyItems = new ArrayList<HistoryState>();
|
||||
|
||||
public History() {
|
||||
}
|
||||
|
||||
public List<CalculatorHistoryState> getHistoryItems() {
|
||||
public List<HistoryState> getHistoryItems() {
|
||||
return historyItems;
|
||||
}
|
||||
}
|
||||
|
@ -47,12 +47,12 @@ import static org.solovyev.android.calculator.history.BaseHistoryFragment.isAlre
|
||||
* Date: 12/18/11
|
||||
* 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 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);
|
||||
this.showDatetime = showDatetime;
|
||||
}
|
||||
@ -61,7 +61,7 @@ public class HistoryArrayAdapter extends ArrayAdapter<CalculatorHistoryState> {
|
||||
public View getView(int position, View convertView, ViewGroup 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);
|
||||
if (showDatetime) {
|
||||
|
@ -48,11 +48,11 @@ public class HistoryFragment extends BaseHistoryFragment {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
protected List<CalculatorHistoryState> getHistoryItems() {
|
||||
protected List<HistoryState> getHistoryItems() {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||
final boolean showIntermediateCalculations = Preferences.History.showIntermediateCalculations.getPreference(preferences);
|
||||
final List<CalculatorHistoryState> historyStates = Locator.getInstance().getHistory().getStates(showIntermediateCalculations);
|
||||
return new ArrayList<CalculatorHistoryState>(historyStates);
|
||||
final List<HistoryState> historyStates = Locator.getInstance().getHistory().getStates(showIntermediateCalculations);
|
||||
return new ArrayList<HistoryState>(historyStates);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -34,23 +34,23 @@ import javax.annotation.Nonnull;
|
||||
public class HistoryItemMenuData {
|
||||
|
||||
@Nonnull
|
||||
private final ArrayAdapter<CalculatorHistoryState> adapter;
|
||||
private final ArrayAdapter<HistoryState> adapter;
|
||||
|
||||
@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.adapter = adapter;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CalculatorHistoryState getHistoryState() {
|
||||
public HistoryState getHistoryState() {
|
||||
return historyState;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public ArrayAdapter<CalculatorHistoryState> getAdapter() {
|
||||
public ArrayAdapter<HistoryState> getAdapter() {
|
||||
return adapter;
|
||||
}
|
||||
}
|
||||
|
@ -57,8 +57,8 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
|
||||
copy_expression(R.string.c_copy_expression) {
|
||||
@Override
|
||||
public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) {
|
||||
final CalculatorHistoryState calculatorHistoryState = data.getHistoryState();
|
||||
final String text = calculatorHistoryState.getEditorState().getText();
|
||||
final HistoryState historyState = data.getHistoryState();
|
||||
final String text = historyState.getEditorState().getText();
|
||||
if (!Strings.isEmpty(text)) {
|
||||
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
|
||||
clipboard.setText(text);
|
||||
@ -70,8 +70,8 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
|
||||
copy_result(R.string.c_copy_result) {
|
||||
@Override
|
||||
public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) {
|
||||
final CalculatorHistoryState calculatorHistoryState = data.getHistoryState();
|
||||
final String text = calculatorHistoryState.getDisplayState().getEditorState().getText();
|
||||
final HistoryState historyState = data.getHistoryState();
|
||||
final String text = historyState.getDisplayState().getEditorState().getText();
|
||||
if (!Strings.isEmpty(text)) {
|
||||
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
|
||||
clipboard.setText(text);
|
||||
@ -83,7 +83,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
|
||||
save(R.string.c_save) {
|
||||
@Override
|
||||
public void onClick(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context) {
|
||||
final CalculatorHistoryState historyState = data.getHistoryState();
|
||||
final HistoryState historyState = data.getHistoryState();
|
||||
if (!historyState.isSaved()) {
|
||||
createEditHistoryDialog(data, context, true);
|
||||
} else {
|
||||
@ -95,7 +95,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
|
||||
edit(R.string.c_edit) {
|
||||
@Override
|
||||
public void onClick(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context) {
|
||||
final CalculatorHistoryState historyState = data.getHistoryState();
|
||||
final HistoryState historyState = data.getHistoryState();
|
||||
if (historyState.isSaved()) {
|
||||
createEditHistoryDialog(data, context, false);
|
||||
} else {
|
||||
@ -107,7 +107,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
|
||||
remove(R.string.c_remove) {
|
||||
@Override
|
||||
public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) {
|
||||
final CalculatorHistoryState historyState = data.getHistoryState();
|
||||
final HistoryState historyState = data.getHistoryState();
|
||||
if (historyState.isSaved()) {
|
||||
data.getAdapter().remove(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) {
|
||||
final CalculatorHistoryState historyState = data.getHistoryState();
|
||||
final HistoryState historyState = data.getHistoryState();
|
||||
|
||||
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
final View editView = layoutInflater.inflate(R.layout.history_edit, null);
|
||||
@ -142,7 +142,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
if (save) {
|
||||
final CalculatorHistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(historyState);
|
||||
final HistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(historyState);
|
||||
savedHistoryItem.setComment(comment.getText().toString());
|
||||
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
|
||||
|
@ -25,20 +25,14 @@ package org.solovyev.android.calculator.history;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.solovyev.android.calculator.CalculatorDisplay;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorEditor;
|
||||
import org.solovyev.android.calculator.DisplayState;
|
||||
import org.solovyev.android.calculator.Editor;
|
||||
import org.solovyev.android.calculator.EditorState;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/11/11
|
||||
* Time: 12:16 AM
|
||||
*/
|
||||
|
||||
@Root
|
||||
public class CalculatorHistoryState extends AbstractHistoryState {
|
||||
public class HistoryState extends BaseHistoryState {
|
||||
|
||||
@Element
|
||||
@Nonnull
|
||||
@ -46,35 +40,28 @@ public class CalculatorHistoryState extends AbstractHistoryState {
|
||||
|
||||
@Element
|
||||
@Nonnull
|
||||
private CalculatorDisplayHistoryState displayState;
|
||||
private DisplayHistoryState displayState;
|
||||
|
||||
private CalculatorHistoryState() {
|
||||
private HistoryState() {
|
||||
// for xml
|
||||
}
|
||||
|
||||
private CalculatorHistoryState(@Nonnull EditorHistoryState editorState,
|
||||
@Nonnull CalculatorDisplayHistoryState displayState) {
|
||||
private HistoryState(@Nonnull EditorHistoryState editorState,
|
||||
@Nonnull DisplayHistoryState displayState) {
|
||||
this.editorState = editorState;
|
||||
this.displayState = displayState;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static CalculatorHistoryState newInstance(@Nonnull CalculatorEditor editor,
|
||||
public static HistoryState create(@Nonnull Editor editor,
|
||||
@Nonnull CalculatorDisplay display) {
|
||||
final EditorState editorViewState = editor.getViewState();
|
||||
final CalculatorDisplayViewState displayViewState = display.getViewState();
|
||||
|
||||
return newInstance(editorViewState, displayViewState);
|
||||
return create(editor.getState(), display.getViewState());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static CalculatorHistoryState newInstance(@Nonnull EditorState editorViewState,
|
||||
@Nonnull CalculatorDisplayViewState displayViewState) {
|
||||
final EditorHistoryState editorHistoryState = EditorHistoryState.newInstance(editorViewState);
|
||||
|
||||
final CalculatorDisplayHistoryState displayHistoryState = CalculatorDisplayHistoryState.newInstance(displayViewState);
|
||||
|
||||
return new CalculatorHistoryState(editorHistoryState, displayHistoryState);
|
||||
public static HistoryState create(@Nonnull EditorState editorState,
|
||||
@Nonnull DisplayState displayState) {
|
||||
return new HistoryState(EditorHistoryState.create(editorState), DisplayHistoryState.newInstance(displayState));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@ -82,22 +69,14 @@ public class CalculatorHistoryState extends AbstractHistoryState {
|
||||
return editorState;
|
||||
}
|
||||
|
||||
public void setEditorState(@Nonnull EditorHistoryState editorState) {
|
||||
this.editorState = editorState;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CalculatorDisplayHistoryState getDisplayState() {
|
||||
public DisplayHistoryState getDisplayState() {
|
||||
return displayState;
|
||||
}
|
||||
|
||||
public void setDisplayState(@Nonnull CalculatorDisplayHistoryState displayState) {
|
||||
this.displayState = displayState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CalculatorHistoryState{" +
|
||||
return "HistoryState{" +
|
||||
"editorState=" + editorState +
|
||||
", displayState=" + displayState +
|
||||
'}';
|
||||
@ -108,7 +87,7 @@ public class CalculatorHistoryState extends AbstractHistoryState {
|
||||
if (this == o) return true;
|
||||
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.getId() != that.getId()) return false;
|
||||
@ -127,18 +106,18 @@ public class CalculatorHistoryState extends AbstractHistoryState {
|
||||
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.getDisplayState().setValuesFromHistory(display);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CalculatorHistoryState clone() {
|
||||
final CalculatorHistoryState clone = (CalculatorHistoryState) super.clone();
|
||||
protected HistoryState clone() {
|
||||
final HistoryState that = (HistoryState) super.clone();
|
||||
|
||||
clone.editorState = this.editorState.clone();
|
||||
clone.displayState = this.displayState.clone();
|
||||
that.editorState = this.editorState.clone();
|
||||
that.displayState = this.displayState.clone();
|
||||
|
||||
return clone;
|
||||
return that;
|
||||
}
|
||||
}
|
@ -43,12 +43,12 @@ class HistoryUtils {
|
||||
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) {
|
||||
final Serializer serializer = new Persister();
|
||||
try {
|
||||
final History history = serializer.read(History.class, xml);
|
||||
for (CalculatorHistoryState historyItem : history.getHistoryItems()) {
|
||||
for (HistoryState historyItem : history.getHistoryItems()) {
|
||||
historyItems.add(historyItem);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -58,9 +58,9 @@ class HistoryUtils {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static String toXml(@Nonnull List<CalculatorHistoryState> historyItems) {
|
||||
public static String toXml(@Nonnull List<HistoryState> historyItems) {
|
||||
final History history = new History();
|
||||
for (CalculatorHistoryState historyState : historyItems) {
|
||||
for (HistoryState historyState : historyItems) {
|
||||
if (historyState.isSaved()) {
|
||||
history.getHistoryItems().add(historyState);
|
||||
}
|
||||
|
@ -44,8 +44,8 @@ public class SavedHistoryFragment extends BaseHistoryFragment {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
protected List<CalculatorHistoryState> getHistoryItems() {
|
||||
return new ArrayList<CalculatorHistoryState>(Locator.getInstance().getHistory().getSavedHistory());
|
||||
protected List<HistoryState> getHistoryItems() {
|
||||
return new ArrayList<HistoryState>(Locator.getInstance().getHistory().getSavedHistory());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -122,7 +122,7 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi
|
||||
view.show();
|
||||
|
||||
startCalculatorListening();
|
||||
view.updateEditorState(Locator.getInstance().getEditor().getViewState());
|
||||
view.updateEditorState(Locator.getInstance().getEditor().getState());
|
||||
view.updateDisplayState(Locator.getInstance().getDisplay().getViewState());
|
||||
|
||||
viewCreated = true;
|
||||
|
@ -180,7 +180,7 @@ public class CalculatorOnscreenView {
|
||||
}
|
||||
}
|
||||
|
||||
public void updateDisplayState(@Nonnull CalculatorDisplayViewState displayState) {
|
||||
public void updateDisplayState(@Nonnull DisplayState displayState) {
|
||||
checkInit();
|
||||
displayView.setState(displayState);
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
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.Locator;
|
||||
|
||||
@ -22,7 +22,7 @@ public final class LongClickEraser implements View.OnTouchListener {
|
||||
private final GestureDetector gestureDetector;
|
||||
|
||||
@Nonnull
|
||||
private final CalculatorEditor editor = Locator.getInstance().getEditor();
|
||||
private final Editor editor = Locator.getInstance().getEditor();
|
||||
|
||||
@Nonnull
|
||||
private final Calculator calculator = Locator.getInstance().getCalculator();
|
||||
|
@ -107,8 +107,8 @@ public class CalculatorWidget extends AppWidgetProvider {
|
||||
private void updateWidget(@Nonnull Context context,
|
||||
@Nonnull AppWidgetManager manager,
|
||||
@Nonnull int[] widgetIds) {
|
||||
final EditorState editorState = Locator.getInstance().getEditor().getViewState();
|
||||
final CalculatorDisplayViewState displayState = Locator.getInstance().getDisplay().getViewState();
|
||||
final EditorState editorState = Locator.getInstance().getEditor().getState();
|
||||
final DisplayState displayState = Locator.getInstance().getDisplay().getViewState();
|
||||
|
||||
final Resources resources = context.getResources();
|
||||
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();
|
||||
if (!error) {
|
||||
views.setTextViewText(R.id.calculator_display, displayState.getText());
|
||||
|
@ -47,7 +47,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
|
||||
@RunWith(value = RobolectricGradleTestRunner.class)
|
||||
public class AndroidCalculatorEditorViewTest {
|
||||
public class AndroidEditorViewTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void staticSetUp() throws Exception {
|
@ -36,11 +36,11 @@ public class CalculatorDisplayViewStateImplTest {
|
||||
|
||||
@Test
|
||||
public void testSerializable() throws Exception {
|
||||
CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "test", 3));
|
||||
CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, Expression.valueOf("3"), "test", 3));
|
||||
CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newValidState(JsclOperation.simplify, Expression.valueOf("3+3"), "test", 3));
|
||||
CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newDefaultInstance());
|
||||
CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.numeric, "ertert"));
|
||||
CalculatorTestUtils.testSerialization(DisplayState.createValid(JsclOperation.numeric, null, "test", 3));
|
||||
CalculatorTestUtils.testSerialization(DisplayState.createValid(JsclOperation.numeric, Expression.valueOf("3"), "test", 3));
|
||||
CalculatorTestUtils.testSerialization(DisplayState.createValid(JsclOperation.simplify, Expression.valueOf("3+3"), "test", 3));
|
||||
CalculatorTestUtils.testSerialization(DisplayState.empty());
|
||||
CalculatorTestUtils.testSerialization(DisplayState.createError(JsclOperation.numeric, "ertert"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -76,7 +76,6 @@ public class CalculatorTestUtils {
|
||||
|
||||
public static void initViews(@Nonnull Context context) {
|
||||
final EditorView editor = new EditorView(context);
|
||||
editor.init();
|
||||
Locator.getInstance().getEditor().setView(editor);
|
||||
|
||||
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) {
|
||||
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 TestCalculatorEventListener calculatorEventListener = new TestCalculatorEventListener(latch);
|
||||
@ -161,7 +160,7 @@ public class CalculatorTestUtils {
|
||||
public static void assertError(@Nonnull String expression, @Nonnull JsclOperation operation) {
|
||||
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 TestCalculatorEventListener calculatorEventListener = new TestCalculatorEventListener(latch);
|
||||
@ -190,7 +189,7 @@ public class CalculatorTestUtils {
|
||||
@Nullable
|
||||
private CalculatorEventData calculatorEventData;
|
||||
@Nullable
|
||||
private volatile CalculatorDisplayViewState result = null;
|
||||
private volatile DisplayState result = null;
|
||||
|
||||
public TestCalculatorEventListener(@Nonnull CountDownLatch latch) {
|
||||
this.latch = latch;
|
||||
@ -230,7 +229,7 @@ public class CalculatorTestUtils {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public CalculatorDisplayViewState getResult() {
|
||||
public DisplayState getResult() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -33,198 +33,198 @@ import javax.annotation.Nonnull;
|
||||
* Date: 21.09.12
|
||||
* Time: 12:44
|
||||
*/
|
||||
public class CalculatorEditorImplTest extends AbstractCalculatorTest {
|
||||
public class EditorTest extends AbstractCalculatorTest {
|
||||
|
||||
@Nonnull
|
||||
private CalculatorEditor calculatorEditor;
|
||||
private Editor editor;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
this.calculatorEditor = new CalculatorEditorImpl(Locator.getInstance().getCalculator(), null);
|
||||
this.editor = new Editor(Locator.getInstance().getCalculator(), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert() throws Exception {
|
||||
CalculatorEditorViewState viewState = this.calculatorEditor.getViewState();
|
||||
EditorState viewState = this.editor.getState();
|
||||
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("");
|
||||
viewState = this.editor.insert("");
|
||||
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("test");
|
||||
viewState = this.editor.insert("test");
|
||||
|
||||
Assert.assertEquals("test", viewState.getText());
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("test");
|
||||
viewState = this.editor.insert("test");
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("");
|
||||
viewState = this.editor.insert("");
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("1234567890");
|
||||
viewState = this.editor.insert("1234567890");
|
||||
Assert.assertEquals("testtest1234567890", viewState.getText());
|
||||
Assert.assertEquals(18, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveCursorLeft();
|
||||
viewState = this.calculatorEditor.insert("9");
|
||||
viewState = this.editor.moveCursorLeft();
|
||||
viewState = this.editor.insert("9");
|
||||
Assert.assertEquals("testtest12345678990", viewState.getText());
|
||||
Assert.assertEquals(18, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setCursorOnStart();
|
||||
viewState = this.calculatorEditor.insert("9");
|
||||
viewState = this.editor.setCursorOnStart();
|
||||
viewState = this.editor.insert("9");
|
||||
Assert.assertEquals("9testtest12345678990", viewState.getText());
|
||||
Assert.assertEquals(1, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.erase();
|
||||
viewState = this.calculatorEditor.insert("9");
|
||||
viewState = this.editor.erase();
|
||||
viewState = this.editor.insert("9");
|
||||
Assert.assertEquals("9testtest12345678990", viewState.getText());
|
||||
Assert.assertEquals(1, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.insert("öäü");
|
||||
viewState = this.editor.insert("öäü");
|
||||
Assert.assertEquals("9öäütesttest12345678990", viewState.getText());
|
||||
|
||||
this.calculatorEditor.setCursorOnEnd();
|
||||
viewState = this.calculatorEditor.insert("öäü");
|
||||
this.editor.setCursorOnEnd();
|
||||
viewState = this.editor.insert("öäü");
|
||||
Assert.assertEquals("9öäütesttest12345678990öäü", viewState.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErase() throws Exception {
|
||||
this.calculatorEditor.setText("");
|
||||
this.calculatorEditor.erase();
|
||||
this.editor.setText("");
|
||||
this.editor.erase();
|
||||
|
||||
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
|
||||
Assert.assertEquals("", this.editor.getState().getText());
|
||||
|
||||
this.calculatorEditor.setText("test");
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("tes", this.calculatorEditor.getViewState().getText());
|
||||
this.editor.setText("test");
|
||||
this.editor.erase();
|
||||
Assert.assertEquals("tes", this.editor.getState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("te", this.calculatorEditor.getViewState().getText());
|
||||
this.editor.erase();
|
||||
Assert.assertEquals("te", this.editor.getState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("t", this.calculatorEditor.getViewState().getText());
|
||||
this.editor.erase();
|
||||
Assert.assertEquals("t", this.editor.getState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
|
||||
this.editor.erase();
|
||||
Assert.assertEquals("", this.editor.getState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("", this.calculatorEditor.getViewState().getText());
|
||||
this.editor.erase();
|
||||
Assert.assertEquals("", this.editor.getState().getText());
|
||||
|
||||
this.calculatorEditor.setText("1234");
|
||||
this.calculatorEditor.moveCursorLeft();
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("124", this.calculatorEditor.getViewState().getText());
|
||||
this.editor.setText("1234");
|
||||
this.editor.moveCursorLeft();
|
||||
this.editor.erase();
|
||||
Assert.assertEquals("124", this.editor.getState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("14", this.calculatorEditor.getViewState().getText());
|
||||
this.editor.erase();
|
||||
Assert.assertEquals("14", this.editor.getState().getText());
|
||||
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("4", this.calculatorEditor.getViewState().getText());
|
||||
this.editor.erase();
|
||||
Assert.assertEquals("4", this.editor.getState().getText());
|
||||
|
||||
this.calculatorEditor.setText("1");
|
||||
this.calculatorEditor.moveCursorLeft();
|
||||
this.calculatorEditor.erase();
|
||||
Assert.assertEquals("1", this.calculatorEditor.getViewState().getText());
|
||||
this.editor.setText("1");
|
||||
this.editor.moveCursorLeft();
|
||||
this.editor.erase();
|
||||
Assert.assertEquals("1", this.editor.getState().getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
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());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(2);
|
||||
viewState = this.editor.moveSelection(2);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(100);
|
||||
viewState = this.editor.moveSelection(100);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-3);
|
||||
viewState = this.editor.moveSelection(-3);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-100);
|
||||
viewState = this.editor.moveSelection(-100);
|
||||
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());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(1);
|
||||
viewState = this.editor.moveSelection(1);
|
||||
Assert.assertEquals(10, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-2);
|
||||
viewState = this.editor.moveSelection(-2);
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(1);
|
||||
viewState = this.editor.moveSelection(1);
|
||||
Assert.assertEquals(9, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-9);
|
||||
viewState = this.editor.moveSelection(-9);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-10);
|
||||
viewState = this.editor.moveSelection(-10);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(2);
|
||||
viewState = this.editor.moveSelection(2);
|
||||
Assert.assertEquals(2, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(2);
|
||||
viewState = this.editor.moveSelection(2);
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.moveSelection(-6);
|
||||
viewState = this.editor.moveSelection(-6);
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetText() throws Exception {
|
||||
CalculatorEditorViewState viewState = this.calculatorEditor.setText("test");
|
||||
EditorState viewState = this.editor.setText("test");
|
||||
|
||||
Assert.assertEquals("test", viewState.getText());
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("testtest");
|
||||
viewState = this.editor.setText("testtest");
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(8, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("");
|
||||
viewState = this.editor.setText("");
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("testtest", 0);
|
||||
viewState = this.editor.setText("testtest", 0);
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("testtest", 2);
|
||||
viewState = this.editor.setText("testtest", 2);
|
||||
Assert.assertEquals("testtest", viewState.getText());
|
||||
Assert.assertEquals(2, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", 0);
|
||||
viewState = this.editor.setText("", 0);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", 3);
|
||||
viewState = this.editor.setText("", 3);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", -3);
|
||||
viewState = this.editor.setText("", -3);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("test");
|
||||
viewState = this.editor.setText("test");
|
||||
Assert.assertEquals("test", viewState.getText());
|
||||
Assert.assertEquals(4, viewState.getSelection());
|
||||
|
||||
viewState = this.calculatorEditor.setText("", 2);
|
||||
viewState = this.editor.setText("", 2);
|
||||
Assert.assertEquals("", viewState.getText());
|
||||
Assert.assertEquals(0, viewState.getSelection());
|
||||
}
|
@ -25,7 +25,7 @@ package org.solovyev.android.calculator.history;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
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.CalculatorTestUtils;
|
||||
import org.solovyev.android.calculator.Locator;
|
||||
@ -64,13 +64,13 @@ public class CalculatorHistoryImplTest {
|
||||
addState(calculatorHistory, "2354");
|
||||
addState(calculatorHistory, "23547");
|
||||
|
||||
final List<CalculatorHistoryState> states = calculatorHistory.getStates(false);
|
||||
final List<HistoryState> states = calculatorHistory.getStates(false);
|
||||
Assert.assertEquals(2, states.size());
|
||||
Assert.assertEquals("23547", states.get(1).getEditorState().getText());
|
||||
Assert.assertEquals("123+3", states.get(0).getEditorState().getText());
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
@ -24,8 +24,7 @@ package org.solovyev.android.calculator.history;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl;
|
||||
import org.solovyev.android.calculator.DisplayState;
|
||||
import org.solovyev.android.calculator.EditorState;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.Objects;
|
||||
@ -137,13 +136,13 @@ public class HistoryUtilsTest {
|
||||
public void testToXml() throws Exception {
|
||||
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);
|
||||
|
||||
CalculatorHistoryState state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
HistoryState state = HistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
|
||||
@ -154,29 +153,29 @@ public class HistoryUtilsTest {
|
||||
|
||||
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);
|
||||
|
||||
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state = HistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setSaved(true);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
|
||||
calculatorDisplay = CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.elementary, "Error");
|
||||
calculatorDisplay = DisplayState.createError(JsclOperation.elementary, "Error");
|
||||
|
||||
calculatorEditor = EditorState.create("", 1);
|
||||
|
||||
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state = HistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setSaved(true);
|
||||
state.setTime(date.getTime());
|
||||
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);
|
||||
|
||||
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state = HistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setSaved(true);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
@ -184,23 +183,23 @@ public class HistoryUtilsTest {
|
||||
String xml = HistoryUtils.toXml(history.getStates());
|
||||
assertEquals(toXml2, xml);
|
||||
|
||||
final List<CalculatorHistoryState> fromXml = new ArrayList<CalculatorHistoryState>();
|
||||
final HistoryHelper<CalculatorHistoryState> historyFromXml = SimpleHistoryHelper.newInstance();
|
||||
final List<HistoryState> fromXml = new ArrayList<HistoryState>();
|
||||
final HistoryHelper<HistoryState> historyFromXml = SimpleHistoryHelper.newInstance();
|
||||
HistoryUtils.fromXml(xml, fromXml);
|
||||
for (CalculatorHistoryState historyState : fromXml) {
|
||||
for (HistoryState historyState : fromXml) {
|
||||
historyFromXml.addState(historyState);
|
||||
}
|
||||
|
||||
assertEquals(history.getStates().size(), historyFromXml.getStates().size());
|
||||
|
||||
for (CalculatorHistoryState historyState : history.getStates()) {
|
||||
for (HistoryState historyState : history.getStates()) {
|
||||
historyState.setId(0);
|
||||
historyState.setSaved(true);
|
||||
}
|
||||
for (CalculatorHistoryState historyState : historyFromXml.getStates()) {
|
||||
for (HistoryState historyState : historyFromXml.getStates()) {
|
||||
historyState.setId(0);
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user