Constant save refactored

This commit is contained in:
Sergey Solovyev 2012-10-02 01:07:19 +04:00
parent fb42a3ebe9
commit 9b6e3337c5
30 changed files with 1762 additions and 1581 deletions

View File

@ -63,6 +63,9 @@ public interface Calculator extends CalculatorEventContainer, HistoryControl<Cal
@NotNull
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
@NotNull
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Object source);
@NotNull
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId);
}

View File

@ -57,6 +57,11 @@ public class CalculatorConversionEventDataImpl implements CalculatorConversionEv
return calculatorEventData.getSequenceId();
}
@Override
public Object getSource() {
return calculatorEventData.getSource();
}
@Override
public boolean isAfter(@NotNull CalculatorEventData that) {
return calculatorEventData.isAfter(that);

View File

@ -1,17 +1,9 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 9/21/12
* Time: 9:49 PM
*/
public interface CalculatorDisplayChangeEventData {
@NotNull
CalculatorDisplayViewState getOldState();
@NotNull
CalculatorDisplayViewState getNewState();
public interface CalculatorDisplayChangeEventData extends Change<CalculatorDisplayViewState> {
}

View File

@ -22,13 +22,13 @@ public class CalculatorDisplayChangeEventDataImpl implements CalculatorDisplayCh
@NotNull
@Override
public CalculatorDisplayViewState getOldState() {
public CalculatorDisplayViewState getOldValue() {
return this.oldState;
}
@NotNull
@Override
public CalculatorDisplayViewState getNewState() {
public CalculatorDisplayViewState getNewValue() {
return this.newState;
}
}

View File

@ -1,17 +1,9 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/**
* User: Solovyev_S
* Date: 21.09.12
* Time: 13:46
*/
public interface CalculatorEditorChangeEventData {
@NotNull
CalculatorEditorViewState getOldState();
@NotNull
CalculatorEditorViewState getNewState();
public interface CalculatorEditorChangeEventData extends Change<CalculatorEditorViewState>{
}

View File

@ -23,13 +23,13 @@ public class CalculatorEditorChangeEventDataImpl implements CalculatorEditorChan
@NotNull
@Override
public CalculatorEditorViewState getOldState() {
public CalculatorEditorViewState getOldValue() {
return this.oldState;
}
@NotNull
@Override
public CalculatorEditorViewState getNewState() {
public CalculatorEditorViewState getNewValue() {
return this.newState;
}
}

View File

@ -50,6 +50,11 @@ public class CalculatorEvaluationEventDataImpl implements CalculatorEvaluationEv
return calculatorEventData.getSequenceId();
}
@Override
public Object getSource() {
return calculatorEventData.getSource();
}
@Override
public boolean isAfter(@NotNull CalculatorEventData that) {
return calculatorEventData.isAfter(that);

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: Solovyev_S
@ -16,6 +17,9 @@ public interface CalculatorEventData {
@NotNull
Long getSequenceId();
@Nullable
Object getSource();
boolean isAfter(@NotNull CalculatorEventData that);
boolean isSameSequence(@NotNull CalculatorEventData that);

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: Solovyev_S
@ -16,14 +17,22 @@ class CalculatorEventDataImpl implements CalculatorEventData {
@NotNull
private Long sequenceId = NO_SEQUENCE;
private CalculatorEventDataImpl(long id, @NotNull Long sequenceId) {
private final Object source;
private CalculatorEventDataImpl(long id, @NotNull Long sequenceId, @Nullable Object source) {
this.eventId = id;
this.sequenceId = sequenceId;
this.source = source;
}
@NotNull
static CalculatorEventData newInstance(long id, @NotNull Long sequenceId) {
return new CalculatorEventDataImpl(id, sequenceId);
return new CalculatorEventDataImpl(id, sequenceId, null);
}
@NotNull
static CalculatorEventData newInstance(long id, @NotNull Long sequenceId, @NotNull Object source) {
return new CalculatorEventDataImpl(id, sequenceId, source);
}
@Override
@ -37,6 +46,11 @@ class CalculatorEventDataImpl implements CalculatorEventData {
return this.sequenceId;
}
@Override
public Object getSource() {
return this.source;
}
@Override
public boolean isAfter(@NotNull CalculatorEventData that) {
return this.eventId > that.getEventId();

View File

@ -106,7 +106,16 @@ public enum CalculatorEventType {
use_function,
// @NotNull Operator
use_operator;
use_operator,
// @NotNull IConstant
constant_added,
// @NotNull Change<IConstant>
constant_changed,
// @NotNull IConstant
constant_removed;
public boolean isOfType(@NotNull CalculatorEventType... types) {
for (CalculatorEventType type : types) {

View File

@ -60,6 +60,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
return CalculatorEventDataImpl.newInstance(eventId, eventId);
}
@NotNull
private CalculatorEventData nextEventData(@NotNull Object source) {
long eventId = counter.incrementAndGet();
return CalculatorEventDataImpl.newInstance(eventId, eventId, source);
}
@NotNull
private CalculatorEventData nextEventData(@NotNull Long sequenceId) {
long eventId = counter.incrementAndGet();
@ -353,6 +359,16 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
return eventData;
}
@NotNull
@Override
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data, @NotNull Object source) {
final CalculatorEventData eventData = nextEventData(source);
fireCalculatorEvent(eventData, calculatorEventType, data);
return eventData;
}
@NotNull
@Override
public CalculatorEventData fireCalculatorEvent(@NotNull final CalculatorEventType calculatorEventType, @Nullable final Object data, @NotNull Long sequenceId) {
@ -378,11 +394,11 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
case editor_state_changed:
final CalculatorEditorChangeEventData changeEventData = (CalculatorEditorChangeEventData) data;
final String newText = changeEventData.getNewState().getText();
final String oldText = changeEventData.getOldState().getText();
final String newText = changeEventData.getNewValue().getText();
final String oldText = changeEventData.getOldValue().getText();
if (!newText.equals(oldText)) {
evaluate(JsclOperation.numeric, changeEventData.getNewState().getText(), calculatorEventData.getSequenceId());
evaluate(JsclOperation.numeric, changeEventData.getNewValue().getText(), calculatorEventData.getSequenceId());
}
break;

View File

@ -1,7 +1,11 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageType;
import java.util.List;
/**
* User: serso
@ -11,4 +15,8 @@ import org.solovyev.common.msg.Message;
public interface CalculatorNotifier {
void showMessage(@NotNull Message message);
void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @NotNull List<Object> parameters);
void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @Nullable Object... parameters);
}

View File

@ -0,0 +1,18 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 10/1/12
* Time: 11:16 PM
*/
public interface Change<T> {
@NotNull
T getOldValue();
@NotNull
T getNewValue();
}

View File

@ -0,0 +1,42 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 10/1/12
* Time: 11:18 PM
*/
public class ChangeImpl<T> implements Change<T> {
@NotNull
private T oldValue;
@NotNull
private T newValue;
private ChangeImpl() {
}
@NotNull
public static <T> Change<T> newInstance(@NotNull T oldValue, @NotNull T newValue) {
final ChangeImpl<T> result = new ChangeImpl<T>();
result.oldValue = oldValue;
result.newValue = newValue;
return result;
}
@NotNull
@Override
public T getOldValue() {
return this.oldValue;
}
@NotNull
@Override
public T getNewValue() {
return this.newValue;
}
}

View File

@ -1,7 +1,11 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageType;
import java.util.List;
/**
* User: serso
@ -13,4 +17,12 @@ public class DummyCalculatorNotifier implements CalculatorNotifier {
@Override
public void showMessage(@NotNull Message message) {
}
@Override
public void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @NotNull List<Object> parameters) {
}
@Override
public void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @Nullable Object... parameters) {
}
}

View File

@ -213,14 +213,14 @@ public class CalculatorHistoryImpl implements CalculatorHistory {
break;
case editor_state_changed:
final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data;
lastEditorViewState = editorChangeData.getNewState();
lastEditorViewState = editorChangeData.getNewValue();
break;
case display_state_changed:
if (sameSequence) {
if (lastEditorViewState != null) {
final CalculatorEditorViewState editorViewState = lastEditorViewState;
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data;
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewState();
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewValue();
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState));
}
} else {

View File

@ -5,7 +5,7 @@
xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:id="@+id/ad_parent_view"
a:id="@+id/main_layout"
a:orientation="vertical"
a:layout_gravity="center"
a:background="#ff000000">

View File

@ -3,7 +3,7 @@
xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:id="@+id/ad_parent_view"
a:id="@+id/main_layout"
a:orientation="vertical"
a:layout_gravity="center"
a:background="#ff000000">

View File

@ -7,53 +7,75 @@
-->
<ScrollView xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
a:layout_height="wrap_content"
a:layout_width="match_parent"
a:minWidth="200dp">
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/var_edit"
a:orientation="vertical"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:scrollbars="vertical"
a:scrollbarAlwaysDrawVerticalTrack="true">
<LinearLayout a:layout_height="wrap_content"
a:layout_width="match_parent"
a:orientation="vertical">
<TextView a:layout_height="fill_parent"
a:layout_width="wrap_content"
<TextView a:layout_height="wrap_content"
a:layout_width="match_parent"
a:padding="6dp"
style="@style/default_text_size"
a:text="@string/c_var_name"/>
<EditText a:id="@+id/var_edit_name"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
style="@style/default_text_size">
</EditText>
a:layout_width="match_parent"
style="@style/default_text_size"
a:inputType="text"/>
<TextView a:layout_height="fill_parent"
a:layout_width="wrap_content"
<TextView a:layout_height="wrap_content"
a:layout_width="match_parent"
style="@style/default_text_size"
a:padding="6dp"
a:text="@string/c_var_value"/>
<EditText a:id="@+id/var_edit_value"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
style="@style/default_text_size">
</EditText>
a:layout_width="match_parent"
style="@style/default_text_size"
a:inputType="text"/>
<TextView a:layout_height="fill_parent"
a:layout_width="wrap_content"
<TextView a:layout_height="wrap_content"
a:layout_width="match_parent"
a:padding="6dp"
style="@style/default_text_size"
a:text="@string/c_var_description"/>
<EditText a:id="@+id/var_edit_description"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
style="@style/default_text_size">
</EditText>
a:layout_width="match_parent"
style="@style/default_text_size"
a:inputType="text"/>
<LinearLayout
a:layout_height="wrap_content"
a:layout_width="match_parent">
<Button a:id="@+id/cancel_button"
a:layout_height="wrap_content"
a:layout_width="0dp"
a:layout_weight="1"
a:text="@string/c_cancel"/>
<Button a:id="@+id/remove_button"
a:layout_height="wrap_content"
a:layout_width="0dp"
a:layout_weight="1"
a:text="@string/c_remove"/>
<Button a:id="@+id/save_button"
a:layout_height="wrap_content"
a:layout_width="0dp"
a:layout_weight="1"
a:text="@string/c_save"/>
</LinearLayout>
</LinearLayout>
</ScrollView>

View File

@ -102,6 +102,12 @@ public class AndroidCalculator implements Calculator {
return calculator.fireCalculatorEvent(calculatorEventType, data);
}
@NotNull
@Override
public CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Object source) {
return calculator.fireCalculatorEvent(calculatorEventType, data, source);
}
@Override
@NotNull
public CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId) {

View File

@ -1,10 +1,14 @@
package org.solovyev.android.calculator;
import android.app.Application;
import android.content.Context;
import android.widget.Toast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.msg.AndroidMessage;
import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageType;
import java.util.List;
/**
* User: serso
@ -14,14 +18,24 @@ import org.solovyev.common.msg.Message;
public class AndroidCalculatorNotifier implements CalculatorNotifier {
@NotNull
private final Context context;
private final Application application;
public AndroidCalculatorNotifier(@NotNull Application application) {
this.context = application;
this.application = application;
}
@Override
public void showMessage(@NotNull Message message) {
Toast.makeText(context, message.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
Toast.makeText(application, message.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @NotNull List<Object> parameters) {
showMessage(new AndroidMessage(messageCode, messageType, application, parameters));
}
@Override
public void showMessage(@NotNull Integer messageCode, @NotNull MessageType messageType, @Nullable Object... parameters) {
showMessage(new AndroidMessage(messageCode, messageType, application, parameters));
}
}

View File

@ -5,6 +5,8 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull;
@ -76,7 +78,12 @@ public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper imple
activity.setContentView(layoutId);
CalculatorButtons.processButtons(true, theme, activity.getWindow().getDecorView());
final View root = activity.findViewById(R.id.main_layout);
if (root != null) {
CalculatorButtons.processButtons(true, theme, root);
} else {
Log.e(CalculatorActivityHelperImpl.class.getSimpleName(), "Root is null for " + activity.getClass().getName());
}
if (savedInstanceState != null) {
navPosition = savedInstanceState.getInt(SELECTED_NAV, 0);
@ -92,7 +99,11 @@ public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper imple
actionBar.setDisplayHomeAsUpEnabled(homeIcon);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
if (activity instanceof CalculatorActivity) {
actionBar.setDisplayShowTitleEnabled(false);
} else {
actionBar.setDisplayShowTitleEnabled(true);
}
actionBar.setIcon(R.drawable.icon_action_bar);
}

View File

@ -2,7 +2,7 @@ package org.solovyev.android.calculator;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import jscl.math.Generic;
import jscl.math.function.Constant;
import org.achartengine.ChartFactory;
@ -13,6 +13,7 @@ import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity
import org.solovyev.android.calculator.math.edit.*;
import org.solovyev.android.calculator.plot.CalculatorPlotActivity;
import org.solovyev.android.calculator.plot.CalculatorPlotFragment;
import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.StringUtils;
/**
@ -64,17 +65,21 @@ public class CalculatorActivityLauncher {
final String varValue = viewState.getText();
if (!StringUtils.isEmpty(varValue)) {
if (CalculatorVarsFragment.isValidValue(varValue)) {
if (context instanceof SherlockFragmentActivity) {
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newFromValue(varValue), ((SherlockFragmentActivity) context).getSupportFragmentManager());
} else {
final Intent intent = new Intent(context, CalculatorVarsFragmentActivity.class);
intent.putExtra(CalculatorVarsFragment.CREATE_VAR_EXTRA_STRING, varValue);
context.startActivity(intent);
} else {
Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, R.string.c_empty_var_error, Toast.LENGTH_SHORT).show();
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(R.string.c_not_valid_result, MessageType.error);
}
} else {
Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show();
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(R.string.c_empty_var_error, MessageType.error);
}
} else {
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(R.string.c_not_valid_result, MessageType.error);
}
}
}

View File

@ -10,6 +10,7 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -37,7 +38,7 @@ import java.util.List;
* Date: 12/21/11
* Time: 9:24 PM
*/
public abstract class AbstractMathEntityListFragment<T extends MathEntity> extends SherlockListFragment {
public abstract class AbstractMathEntityListFragment<T extends MathEntity> extends SherlockListFragment implements CalculatorEventListener {
/*
**********************************************************************
@ -69,6 +70,8 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
@NotNull
private CalculatorFragmentHelper fragmentHelper;
@NotNull
private final Handler uiHandler = new Handler();
protected int getLayoutId() {
return R.layout.math_entities_fragment;
@ -243,12 +246,12 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
text.setText(String.valueOf(mathEntity));
final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName());
if (!StringUtils.isEmpty(mathEntityDescription)) {
final TextView description = (TextView) result.findViewById(R.id.math_entity_description);
if (!StringUtils.isEmpty(mathEntityDescription)) {
description.setVisibility(View.VISIBLE);
description.setText(mathEntityDescription);
} else {
final TextView description = (TextView) result.findViewById(R.id.math_entity_description);
description.setVisibility(View.GONE);
}
}
@ -293,6 +296,11 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
}
}
@NotNull
protected Handler getUiHandler() {
return uiHandler;
}
/*
**********************************************************************
*
@ -336,4 +344,8 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
static void putCategory(@NotNull Bundle bundle, @NotNull String categoryId) {
bundle.putString(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId);
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
}
}

View File

@ -6,20 +6,17 @@
package org.solovyev.android.calculator.math.edit;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.menu.AMenuItem;
import org.solovyev.android.menu.LabeledMenuItem;
@ -38,7 +35,7 @@ import java.util.List;
*/
public class CalculatorVarsFragment extends AbstractMathEntityListFragment<IConstant> {
public static final String CREATE_VAR_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity_create_var";
public static final String CREATE_VAR_EXTRA_STRING = "create_var";
@Override
protected int getLayoutId() {
@ -53,7 +50,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
if (bundle != null) {
final String varValue = bundle.getString(CREATE_VAR_EXTRA_STRING);
if (!StringUtils.isEmpty(varValue)) {
VarEditDialogFragment.createEditVariableDialog(this, VarEditDialogFragment.Input.newFromValue(varValue));
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newFromValue(varValue), this.getActivity().getSupportFragmentManager());
// in order to stop intent for other tabs
bundle.remove(CREATE_VAR_EXTRA_STRING);
@ -102,7 +99,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
@SuppressWarnings({"UnusedDeclaration"})
public void addVarButtonClickHandler(@NotNull View v) {
VarEditDialogFragment.createEditVariableDialog(this, VarEditDialogFragment.Input.newInstance());
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newInstance(), this.getActivity().getSupportFragmentManager());
}
@NotNull
@ -149,7 +146,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
switch (item.getItemId()) {
case R.id.var_menu_add_var:
VarEditDialogFragment.createEditVariableDialog(this, VarEditDialogFragment.Input.newInstance());
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newInstance(), this.getActivity().getSupportFragmentManager());
result = true;
break;
default:
@ -159,6 +156,63 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
return result;
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
super.onCalculatorEvent(calculatorEventData, calculatorEventType, data);
switch (calculatorEventType) {
case constant_added:
processConstantAdded((IConstant) data);
break;
case constant_changed:
processConstantChanged((Change<IConstant>) data);
break;
case constant_removed:
processConstantRemoved((IConstant) data);
break;
}
}
private void processConstantRemoved(@NotNull final IConstant constant) {
if (this.isInCategory(constant)) {
getUiHandler().post(new Runnable() {
@Override
public void run() {
removeFromAdapter(constant);
notifyAdapter();
}
});
}
}
private void processConstantChanged(@NotNull final Change<IConstant> change) {
final IConstant newConstant = change.getNewValue();
if (this.isInCategory(newConstant)) {
getUiHandler().post(new Runnable() {
@Override
public void run() {
removeFromAdapter(change.getOldValue());
addToAdapter(newConstant);
sort();
}
});
}
}
private void processConstantAdded(@NotNull final IConstant constant) {
if (this.isInCategory(constant)) {
getUiHandler().post(new Runnable() {
@Override
public void run() {
addToAdapter(constant);
sort();
}
});
}
}
/*
**********************************************************************
*
@ -177,19 +231,15 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
edit(R.string.c_edit) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
/*if (context instanceof AbstractMathEntityListFragment) {
createEditVariableDialog((AbstractMathEntityListFragment<IConstant>)context, data, data.getName(), StringUtils.getNotEmpty(data.getValue(), ""), data.getDescription());
}*/
public void onClick(@NotNull IConstant constant, @NotNull Context context) {
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newFromConstant(constant), ((SherlockFragmentActivity) context).getSupportFragmentManager());
}
},
remove(R.string.c_remove) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
/*if (context instanceof AbstractMathEntityListFragment) {
new MathEntityRemover<IConstant>(data, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), ((AbstractMathEntityListFragment<IConstant>) context)).showConfirmationDialog();
}*/
public void onClick(@NotNull IConstant constant, @NotNull Context context) {
new MathEntityRemover<IConstant>(constant, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), context, context).showConfirmationDialog();
}
},
@ -198,8 +248,8 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
public void onClick(@NotNull IConstant data, @NotNull Context context) {
final String text = data.getValue();
if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
assert text != null;
CalculatorLocatorImpl.getInstance().getClipboard().setText(text);
}
}
},
@ -209,8 +259,8 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
public void onClick(@NotNull IConstant data, @NotNull Context context) {
final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName());
if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
assert text != null;
CalculatorLocatorImpl.getInstance().getClipboard().setText(text);
}
}
};

View File

@ -7,12 +7,16 @@
package org.solovyev.android.calculator.math.edit;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.calculator.R;
import org.solovyev.common.math.MathEntity;
/**
@ -20,7 +24,7 @@ import org.solovyev.common.math.MathEntity;
* Date: 12/22/11
* Time: 9:36 PM
*/
class MathEntityRemover<T extends MathEntity> implements DialogInterface.OnClickListener {
class MathEntityRemover<T extends MathEntity> implements View.OnClickListener, DialogInterface.OnClickListener {
@NotNull
private final T mathEntity;
@ -34,55 +38,63 @@ class MathEntityRemover<T extends MathEntity> implements DialogInterface.OnClick
private final CalculatorMathRegistry<? super T> varsRegistry;
@NotNull
private final AbstractMathEntityListFragment<T> fragment;
private Context context;
@NotNull
private final Object source;
public MathEntityRemover(@NotNull T mathEntity,
@Nullable DialogInterface.OnClickListener callbackOnCancel,
@NotNull CalculatorMathRegistry<? super T> varsRegistry,
@NotNull AbstractMathEntityListFragment<T> fragment) {
this(mathEntity, callbackOnCancel, false, varsRegistry, fragment);
@NotNull Context context,
@NotNull Object source) {
this(mathEntity, callbackOnCancel, false, varsRegistry, context, source);
}
public MathEntityRemover(@NotNull T mathEntity,
@Nullable DialogInterface.OnClickListener callbackOnCancel,
boolean confirmed,
@NotNull CalculatorMathRegistry<? super T> varsRegistry,
@NotNull AbstractMathEntityListFragment<T> fragment) {
@NotNull Context context,
@NotNull Object source) {
this.mathEntity = mathEntity;
this.callbackOnCancel = callbackOnCancel;
this.confirmed = confirmed;
this.varsRegistry = varsRegistry;
this.fragment = fragment;
this.context = context;
this.source = source;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (!confirmed) {
showConfirmationDialog();
} else {
if (fragment.isInCategory(mathEntity)) {
fragment.removeFromAdapter(mathEntity);
}
varsRegistry.remove(mathEntity);
varsRegistry.save();
if (fragment.isInCategory(mathEntity)) {
fragment.notifyAdapter();
}
}
}
public void showConfirmationDialog() {
final TextView question = new TextView(fragment.getActivity());
question.setText(String.format(fragment.getString(R.string.c_var_removal_confirmation_question), mathEntity.getName()));
final TextView question = new TextView(context);
question.setText(String.format(context.getString(R.string.c_var_removal_confirmation_question), mathEntity.getName()));
question.setPadding(6, 6, 6, 6);
final AlertDialog.Builder builder = new AlertDialog.Builder(fragment.getActivity())
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setCancelable(true)
.setView(question)
.setTitle(R.string.c_var_removal_confirmation)
.setNegativeButton(R.string.c_no, callbackOnCancel)
.setPositiveButton(R.string.c_yes, new MathEntityRemover<T>(mathEntity, callbackOnCancel, true, varsRegistry, fragment));
.setPositiveButton(R.string.c_yes, new MathEntityRemover<T>(mathEntity, callbackOnCancel, true, varsRegistry, context, source));
builder.create().show();
}
@Override
public void onClick(@Nullable View v) {
if (!confirmed) {
showConfirmationDialog();
} else {
varsRegistry.remove(mathEntity);
varsRegistry.save();
CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_removed, mathEntity, source);
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
onClick(null);
}
}

View File

@ -1,31 +1,30 @@
package org.solovyev.android.calculator.math.edit;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.*;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;
import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.common.text.StringUtils;
/**
* User: Solovyev_S
* Date: 01.10.12
* Time: 17:41
*/
public class VarEditDialogFragment extends DialogFragment {
public class VarEditDialogFragment extends DialogFragment implements CalculatorEventListener {
@NotNull
private final Input input;
@ -38,10 +37,7 @@ public class VarEditDialogFragment extends DialogFragment {
this.input = input;
}
public static void createEditVariableDialog(@NotNull final AbstractMathEntityListFragment<IConstant> fragment,
@NotNull Input input) {
final FragmentManager fm = fragment.getActivity().getSupportFragmentManager();
public static void createEditVariableDialog(@NotNull Input input, @NotNull FragmentManager fm) {
final FragmentTransaction ft = fm.beginTransaction();
Fragment prev = fm.findFragmentByTag("constant-editor");
@ -56,103 +52,23 @@ public class VarEditDialogFragment extends DialogFragment {
}
public static void createEditVariableDialog0(@NotNull final AbstractMathEntityListFragment<IConstant> fragment,
@Nullable final IConstant var,
@Nullable final String name,
@Nullable final String value,
@Nullable final String description) {
final FragmentActivity activity = fragment.getActivity();
if (var == null || !var.isSystem()) {
final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View result = layoutInflater.inflate(R.layout.var_edit, null);
final String errorMsg = fragment.getString(R.string.c_char_is_not_accepted);
final EditText editName = (EditText) result.findViewById(R.id.var_edit_name);
editName.setText(name);
editName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!AbstractMathEntityListFragment.acceptableChars.contains(c)) {
s.delete(i, i + 1);
Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show();
}
}
}
});
final EditText editValue = (EditText) result.findViewById(R.id.var_edit_value);
if (!StringUtils.isEmpty(value)) {
editValue.setText(value);
}
final EditText editDescription = (EditText) result.findViewById(R.id.var_edit_description);
editDescription.setText(description);
final Var.Builder varBuilder;
if (var != null) {
varBuilder = new Var.Builder(var);
} else {
varBuilder = new Var.Builder();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setCancelable(true)
.setNegativeButton(R.string.c_cancel, null)
.setPositiveButton(R.string.c_save, new VarEditorSaver<IConstant>(varBuilder, var, result, fragment, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), new VarEditorSaver.EditorCreator<IConstant>() {
@Override
public void showEditor(@NotNull AbstractMathEntityListFragment<IConstant> activity, @Nullable IConstant editedInstance, @Nullable String name, @Nullable String value, @Nullable String description) {
createEditVariableDialog(activity, Input.newInstance(editedInstance, name, value, description));
}
}))
.setView(result);
if (var != null) {
// EDIT mode
builder.setTitle(R.string.c_var_edit_var);
builder.setNeutralButton(R.string.c_remove, new MathEntityRemover<IConstant>(var, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createEditVariableDialog(fragment, Input.newInstance(var, name, value, description));
}
}, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), fragment));
} else {
// CREATE mode
builder.setTitle(R.string.c_var_create_var);
}
builder.create().show();
} else {
Toast.makeText(activity, fragment.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View result = inflater.inflate(R.layout.var_edit, container, false);
return inflater.inflate(R.layout.var_edit, container, false);
}
@Override
public void onResume() {
super.onResume();
return result;
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this);
}
@Override
public void onPause() {
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
super.onPause();
}
@Override
@ -185,6 +101,10 @@ public class VarEditDialogFragment extends DialogFragment {
}
});
// show soft keyboard automatically
editName.requestFocus();
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
final EditText editValue = (EditText) root.findViewById(R.id.var_edit_value);
editValue.setText(input.getValue());
@ -199,12 +119,39 @@ public class VarEditDialogFragment extends DialogFragment {
varBuilder = new Var.Builder();
}
root.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
root.findViewById(R.id.save_button).setOnClickListener(new VarEditorSaver<IConstant>(varBuilder, constant, root, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), this));
if ( constant == null ) {
// CREATE MODE
getDialog().setTitle(R.string.c_var_create_var);
root.findViewById(R.id.remove_button).setVisibility(View.GONE);
} else {
// EDIT MODE
getDialog().setTitle(R.string.c_var_edit_var);
root.findViewById(R.id.remove_button).setOnClickListener(new MathEntityRemover<IConstant>(constant, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), getActivity(), this));
}
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
switch (calculatorEventType) {
case constant_removed:
case constant_added:
case constant_changed:
if ( calculatorEventData.getSource() == this ) {
dismiss();
}
break;
}
}

View File

@ -6,22 +6,19 @@
package org.solovyev.android.calculator.math.edit;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import jscl.text.Identifier;
import jscl.text.MutableInt;
import jscl.text.ParseException;
import jscl.text.Parser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.StringUtils;
/**
@ -29,18 +26,7 @@ import org.solovyev.common.text.StringUtils;
* Date: 12/22/11
* Time: 9:52 PM
*/
class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickListener {
public static interface EditorCreator<T extends MathEntity> {
void showEditor(@NotNull AbstractMathEntityListFragment<T> activity,
@Nullable T editedInstance,
@Nullable String name,
@Nullable String value,
@Nullable String description);
}
@NotNull
private final EditorCreator<T> editorCreator;
class VarEditorSaver<T extends MathEntity> implements View.OnClickListener {
@NotNull
private final MathEntityBuilder<? extends T> varBuilder;
@ -52,7 +38,7 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
private final CalculatorMathRegistry<T> mathRegistry;
@NotNull
private final AbstractMathEntityListFragment<T> fragment;
private final Object source;
@NotNull
private View editView;
@ -60,20 +46,17 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
public VarEditorSaver(@NotNull MathEntityBuilder<? extends T> varBuilder,
@Nullable T editedInstance,
@NotNull View editView,
@NotNull AbstractMathEntityListFragment<T> fragment,
@NotNull CalculatorMathRegistry<T> mathRegistry,
@NotNull EditorCreator<T> editorCreator) {
@NotNull Object source) {
this.varBuilder = varBuilder;
this.editedInstance = editedInstance;
this.editView = editView;
this.fragment = fragment;
this.mathRegistry = mathRegistry;
this.editorCreator = editorCreator;
this.source = source;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
public void onClick(View v) {
final Integer error;
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
@ -131,22 +114,15 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
}
if (error != null) {
Toast.makeText(fragment.getActivity(), fragment.getString(error), Toast.LENGTH_LONG).show();
editorCreator.showEditor(fragment, editedInstance, name, value, description);
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(error, MessageType.error);
} else {
final T addedVar = mathRegistry.add(varBuilder);
if (fragment.isInCategory(addedVar)) {
if (editedInstance != null) {
fragment.removeFromAdapter(editedInstance);
}
fragment.addToAdapter(addedVar);
}
mathRegistry.save();
if (fragment.isInCategory(addedVar)) {
fragment.sort();
}
if (editedInstance == null) {
CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_added, addedVar, source);
} else {
CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_changed, ChangeImpl.newInstance(editedInstance, addedVar), source);
}
}
}

View File

@ -9,6 +9,7 @@ package org.solovyev.android.calculator.model;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -16,7 +17,6 @@ import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.about.TextHelper;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry;
@ -67,7 +67,13 @@ public abstract class AbstractAndroidMathRegistry<T extends MathEntity, P extend
stringName = prefix + substitute;
}
return new TextHelper(context.getResources(), R.class.getPackage().getName()).getText(stringName);
final Resources resources = context.getResources();
final int stringId = resources.getIdentifier(stringName, "string", R.class.getPackage().getName());
try {
return resources.getString(stringId);
} catch (Resources.NotFoundException e) {
return null;
}
}
public synchronized void load() {

View File

@ -398,7 +398,7 @@ public class CalculatorPlotFragment extends SherlockFragment implements Calculat
if ( calculatorEventData.isAfter(this.lastCalculatorEventData) ) {
this.lastCalculatorEventData = calculatorEventData;
createInputFromDisplayState(((CalculatorDisplayChangeEventData) data).getNewState());
createInputFromDisplayState(((CalculatorDisplayChangeEventData) data).getNewValue());
uiHandler.post(new Runnable() {
@Override