Editor/display changes

This commit is contained in:
Sergey Solovyev 2012-09-24 23:52:21 +04:00
parent 9c20d58678
commit 35102cbcc3

View File

@ -1,216 +1,233 @@
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.*;
import org.solovyev.common.history.HistoryAction; import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.history.HistoryHelper; import org.solovyev.common.history.HistoryHelper;
import org.solovyev.common.history.SimpleHistoryHelper; import org.solovyev.common.history.SimpleHistoryHelper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import static org.solovyev.android.calculator.CalculatorEventType.display_state_changed; import static org.solovyev.android.calculator.CalculatorEventType.display_state_changed;
import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed; import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed;
import static org.solovyev.android.calculator.CalculatorEventType.manual_calculation_requested; import static org.solovyev.android.calculator.CalculatorEventType.manual_calculation_requested;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 20.09.12 * Date: 20.09.12
* Time: 16:12 * Time: 16:12
*/ */
public class CalculatorHistoryImpl implements CalculatorHistory { public class CalculatorHistoryImpl implements CalculatorHistory {
private final AtomicInteger counter = new AtomicInteger(0); private final AtomicInteger counter = new AtomicInteger(0);
@NotNull @NotNull
private final HistoryHelper<CalculatorHistoryState> history = new SimpleHistoryHelper<CalculatorHistoryState>(); private final HistoryHelper<CalculatorHistoryState> history = new SimpleHistoryHelper<CalculatorHistoryState>();
@NotNull @NotNull
private final List<CalculatorHistoryState> savedHistory = new ArrayList<CalculatorHistoryState>(); private final List<CalculatorHistoryState> savedHistory = new ArrayList<CalculatorHistoryState>();
@NotNull @NotNull
private volatile CalculatorEventData lastEventDataId = CalculatorUtils.createFirstEventDataId(); private volatile CalculatorEventData lastEventData = CalculatorUtils.createFirstEventDataId();
@Nullable @NotNull
private volatile CalculatorEditorViewState lastEditorViewState; private final Object lastEventDataLock = new Object();
public CalculatorHistoryImpl(@NotNull Calculator calculator) { @Nullable
calculator.addCalculatorEventListener(this); private volatile CalculatorEditorViewState lastEditorViewState;
}
public CalculatorHistoryImpl(@NotNull Calculator calculator) {
@Override calculator.addCalculatorEventListener(this);
public boolean isEmpty() { }
synchronized (history) {
return this.history.isEmpty(); @Override
} public boolean isEmpty() {
} synchronized (history) {
return this.history.isEmpty();
@Override }
public CalculatorHistoryState getLastHistoryState() { }
synchronized (history) {
return this.history.getLastHistoryState(); @Override
} public CalculatorHistoryState getLastHistoryState() {
} synchronized (history) {
return this.history.getLastHistoryState();
@Override }
public boolean isUndoAvailable() { }
synchronized (history) {
return history.isUndoAvailable(); @Override
} public boolean isUndoAvailable() {
} synchronized (history) {
return history.isUndoAvailable();
@Override }
public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) { }
synchronized (history) {
return history.undo(currentState); @Override
} public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) {
} synchronized (history) {
return history.undo(currentState);
@Override }
public boolean isRedoAvailable() { }
return history.isRedoAvailable();
} @Override
public boolean isRedoAvailable() {
@Override return history.isRedoAvailable();
public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) { }
synchronized (history) {
return history.redo(currentState); @Override
} public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) {
} synchronized (history) {
return history.redo(currentState);
@Override }
public boolean isActionAvailable(@NotNull HistoryAction historyAction) { }
synchronized (history) {
return history.isActionAvailable(historyAction); @Override
} public boolean isActionAvailable(@NotNull HistoryAction historyAction) {
} synchronized (history) {
return history.isActionAvailable(historyAction);
@Override }
public CalculatorHistoryState doAction(@NotNull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) { }
synchronized (history) {
return history.doAction(historyAction, currentState); @Override
} public CalculatorHistoryState doAction(@NotNull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) {
} synchronized (history) {
return history.doAction(historyAction, currentState);
@Override }
public void addState(@Nullable CalculatorHistoryState currentState) { }
synchronized (history) {
history.addState(currentState); @Override
} public void addState(@Nullable CalculatorHistoryState currentState) {
} synchronized (history) {
history.addState(currentState);
@NotNull }
@Override }
public List<CalculatorHistoryState> getStates() {
synchronized (history) { @NotNull
return history.getStates(); @Override
} public List<CalculatorHistoryState> getStates() {
} synchronized (history) {
return history.getStates();
@Override }
public void clear() { }
synchronized (history) {
this.history.clear(); @Override
} public void clear() {
} synchronized (history) {
this.history.clear();
@Override }
@NotNull }
public List<CalculatorHistoryState> getSavedHistory() {
return Collections.unmodifiableList(savedHistory); @Override
} @NotNull
public List<CalculatorHistoryState> getSavedHistory() {
@Override return Collections.unmodifiableList(savedHistory);
@NotNull }
public CalculatorHistoryState addSavedState(@NotNull CalculatorHistoryState historyState) {
if (historyState.isSaved()) { @Override
return historyState; @NotNull
} else { public CalculatorHistoryState addSavedState(@NotNull CalculatorHistoryState historyState) {
final CalculatorHistoryState savedState = historyState.clone(); if (historyState.isSaved()) {
return historyState;
savedState.setId(counter.incrementAndGet()); } else {
savedState.setSaved(true); final CalculatorHistoryState savedState = historyState.clone();
savedHistory.add(savedState); savedState.setId(counter.incrementAndGet());
savedState.setSaved(true);
return savedState;
} savedHistory.add(savedState);
}
return savedState;
@Override }
public void load() { }
// todo serso: create saved/loader class
} @Override
public void load() {
@Override // todo serso: create saved/loader class
public void save() { }
// todo serso: create saved/loader class
} @Override
public void save() {
@Override // todo serso: create saved/loader class
public void fromXml(@NotNull String xml) { }
clearSavedHistory();
@Override
HistoryUtils.fromXml(xml, this.savedHistory); public void fromXml(@NotNull String xml) {
for (CalculatorHistoryState historyState : savedHistory) { clearSavedHistory();
historyState.setSaved(true);
historyState.setId(counter.incrementAndGet()); HistoryUtils.fromXml(xml, this.savedHistory);
} for (CalculatorHistoryState historyState : savedHistory) {
} historyState.setSaved(true);
historyState.setId(counter.incrementAndGet());
@Override }
public String toXml() { }
return HistoryUtils.toXml(this.savedHistory);
} @Override
public String toXml() {
@Override return HistoryUtils.toXml(this.savedHistory);
public void clearSavedHistory() { }
this.savedHistory.clear();
} @Override
public void clearSavedHistory() {
@Override this.savedHistory.clear();
public void removeSavedHistory(@NotNull CalculatorHistoryState historyState) { }
this.savedHistory.remove(historyState);
} @Override
public void removeSavedHistory(@NotNull CalculatorHistoryState historyState) {
@Override this.savedHistory.remove(historyState);
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, }
@NotNull CalculatorEventType calculatorEventType,
@Nullable Object data) { @Override
if (calculatorEventType.isOfType(editor_state_changed, display_state_changed, manual_calculation_requested)) { public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
@NotNull CalculatorEventType calculatorEventType,
if (calculatorEventData.isAfter(this.lastEventDataId)) { @Nullable Object data) {
final boolean sameSequence = calculatorEventData.isSameSequence(this.lastEventDataId); if (calculatorEventType.isOfType(editor_state_changed, display_state_changed, manual_calculation_requested)) {
if (sameSequence || calculatorEventData.isAfterSequence(this.lastEventDataId)) { boolean sameSequence = false;
this.lastEventDataId = calculatorEventData; boolean afterSequence = false;
switch (calculatorEventType) { boolean processEvent = false;
case manual_calculation_requested:
lastEditorViewState = (CalculatorEditorViewState) data; synchronized (this.lastEventDataLock) {
break; if (calculatorEventData.isAfter(this.lastEventData)) {
case editor_state_changed:
final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data; sameSequence = calculatorEventData.isSameSequence(this.lastEventData);
lastEditorViewState = editorChangeData.getNewState(); if (!sameSequence) {
break; afterSequence = calculatorEventData.isAfterSequence(this.lastEventData);
case display_state_changed: processEvent = afterSequence;
if (sameSequence) { } else {
if (lastEditorViewState != null) { processEvent = true;
final CalculatorEditorViewState editorViewState = lastEditorViewState; }
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data; }
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewState(); }
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState));
} if (processEvent) {
} else { this.lastEventData = calculatorEventData;
lastEditorViewState = null;
} switch (calculatorEventType) {
break; case manual_calculation_requested:
} lastEditorViewState = (CalculatorEditorViewState) data;
} break;
} case editor_state_changed:
} final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data;
} lastEditorViewState = editorChangeData.getNewState();
} break;
case display_state_changed:
if (sameSequence) {
if (lastEditorViewState != null) {
final CalculatorEditorViewState editorViewState = lastEditorViewState;
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data;
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewState();
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState));
}
} else {
lastEditorViewState = null;
}
break;
}
}
}
}
}