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 @NotNull
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data); CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data);
@NotNull
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Object source);
@NotNull @NotNull
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId); 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(); return calculatorEventData.getSequenceId();
} }
@Override
public Object getSource() {
return calculatorEventData.getSource();
}
@Override @Override
public boolean isAfter(@NotNull CalculatorEventData that) { public boolean isAfter(@NotNull CalculatorEventData that) {
return calculatorEventData.isAfter(that); return calculatorEventData.isAfter(that);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -106,7 +106,16 @@ public enum CalculatorEventType {
use_function, use_function,
// @NotNull Operator // @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) { public boolean isOfType(@NotNull CalculatorEventType... types) {
for (CalculatorEventType type : types) { for (CalculatorEventType type : types) {

View File

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

View File

@ -1,7 +1,11 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.msg.Message; import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageType;
import java.util.List;
/** /**
* User: serso * User: serso
@ -11,4 +15,8 @@ import org.solovyev.common.msg.Message;
public interface CalculatorNotifier { public interface CalculatorNotifier {
void showMessage(@NotNull Message message); 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; package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.msg.Message; import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageType;
import java.util.List;
/** /**
* User: serso * User: serso
@ -13,4 +17,12 @@ public class DummyCalculatorNotifier implements CalculatorNotifier {
@Override @Override
public void showMessage(@NotNull Message message) { 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; break;
case editor_state_changed: case editor_state_changed:
final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data; final CalculatorEditorChangeEventData editorChangeData = (CalculatorEditorChangeEventData) data;
lastEditorViewState = editorChangeData.getNewState(); lastEditorViewState = editorChangeData.getNewValue();
break; break;
case display_state_changed: case display_state_changed:
if (sameSequence) { if (sameSequence) {
if (lastEditorViewState != null) { if (lastEditorViewState != null) {
final CalculatorEditorViewState editorViewState = lastEditorViewState; final CalculatorEditorViewState editorViewState = lastEditorViewState;
final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data; final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data;
final CalculatorDisplayViewState displayViewState = displayChangeData.getNewState(); final CalculatorDisplayViewState displayViewState = displayChangeData.getNewValue();
addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState)); addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState));
} }
} else { } else {

View File

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

View File

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

View File

@ -7,53 +7,75 @@
--> -->
<ScrollView xmlns:a="http://schemas.android.com/apk/res/android" <ScrollView xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent" a:layout_height="wrap_content"
a:layout_height="fill_parent"> a:layout_width="match_parent"
a:minWidth="200dp">
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android" <LinearLayout a:layout_height="wrap_content"
a:id="@+id/var_edit" a:layout_width="match_parent"
a:orientation="vertical" a:orientation="vertical">
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:scrollbars="vertical"
a:scrollbarAlwaysDrawVerticalTrack="true">
<TextView a:layout_height="fill_parent" <TextView a:layout_height="wrap_content"
a:layout_width="wrap_content" a:layout_width="match_parent"
a:padding="6dp" a:padding="6dp"
style="@style/default_text_size" style="@style/default_text_size"
a:text="@string/c_var_name"/> a:text="@string/c_var_name"/>
<EditText a:id="@+id/var_edit_name" <EditText a:id="@+id/var_edit_name"
a:layout_width="fill_parent" a:layout_height="wrap_content"
a:layout_height="wrap_content" a:layout_width="match_parent"
style="@style/default_text_size"> style="@style/default_text_size"
</EditText> a:inputType="text"/>
<TextView a:layout_height="fill_parent" <TextView a:layout_height="wrap_content"
a:layout_width="wrap_content" a:layout_width="match_parent"
style="@style/default_text_size" style="@style/default_text_size"
a:padding="6dp" a:padding="6dp"
a:text="@string/c_var_value"/> a:text="@string/c_var_value"/>
<EditText a:id="@+id/var_edit_value" <EditText a:id="@+id/var_edit_value"
a:layout_width="fill_parent" a:layout_height="wrap_content"
a:layout_height="wrap_content" a:layout_width="match_parent"
style="@style/default_text_size"> style="@style/default_text_size"
</EditText> a:inputType="text"/>
<TextView a:layout_height="fill_parent" <TextView a:layout_height="wrap_content"
a:layout_width="wrap_content" a:layout_width="match_parent"
a:padding="6dp" a:padding="6dp"
style="@style/default_text_size" style="@style/default_text_size"
a:text="@string/c_var_description"/> a:text="@string/c_var_description"/>
<EditText a:id="@+id/var_edit_description" <EditText a:id="@+id/var_edit_description"
a:layout_width="fill_parent" a:layout_height="wrap_content"
a:layout_height="wrap_content" a:layout_width="match_parent"
style="@style/default_text_size"> style="@style/default_text_size"
</EditText> a:inputType="text"/>
</LinearLayout> <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> </ScrollView>

View File

@ -102,6 +102,12 @@ public class AndroidCalculator implements Calculator {
return calculator.fireCalculatorEvent(calculatorEventType, data); 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 @Override
@NotNull @NotNull
public CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId) { public CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId) {

View File

@ -1,10 +1,14 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Application; import android.app.Application;
import android.content.Context;
import android.widget.Toast; import android.widget.Toast;
import org.jetbrains.annotations.NotNull; 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.Message;
import org.solovyev.common.msg.MessageType;
import java.util.List;
/** /**
* User: serso * User: serso
@ -14,14 +18,24 @@ import org.solovyev.common.msg.Message;
public class AndroidCalculatorNotifier implements CalculatorNotifier { public class AndroidCalculatorNotifier implements CalculatorNotifier {
@NotNull @NotNull
private final Context context; private final Application application;
public AndroidCalculatorNotifier(@NotNull Application application) { public AndroidCalculatorNotifier(@NotNull Application application) {
this.context = application; this.application = application;
} }
@Override @Override
public void showMessage(@NotNull Message message) { 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.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -76,7 +78,12 @@ public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper imple
activity.setContentView(layoutId); 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) { if (savedInstanceState != null) {
navPosition = savedInstanceState.getInt(SELECTED_NAV, 0); navPosition = savedInstanceState.getInt(SELECTED_NAV, 0);
@ -92,7 +99,11 @@ public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper imple
actionBar.setDisplayHomeAsUpEnabled(homeIcon); actionBar.setDisplayHomeAsUpEnabled(homeIcon);
actionBar.setHomeButtonEnabled(false); actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true); if (activity instanceof CalculatorActivity) {
actionBar.setDisplayShowTitleEnabled(false);
} else {
actionBar.setDisplayShowTitleEnabled(true);
}
actionBar.setIcon(R.drawable.icon_action_bar); 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.Context;
import android.content.Intent; import android.content.Intent;
import android.widget.Toast; import com.actionbarsherlock.app.SherlockFragmentActivity;
import jscl.math.Generic; import jscl.math.Generic;
import jscl.math.function.Constant; import jscl.math.function.Constant;
import org.achartengine.ChartFactory; 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.math.edit.*;
import org.solovyev.android.calculator.plot.CalculatorPlotActivity; import org.solovyev.android.calculator.plot.CalculatorPlotActivity;
import org.solovyev.android.calculator.plot.CalculatorPlotFragment; import org.solovyev.android.calculator.plot.CalculatorPlotFragment;
import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.text.StringUtils;
/** /**
@ -64,17 +65,21 @@ public class CalculatorActivityLauncher {
final String varValue = viewState.getText(); final String varValue = viewState.getText();
if (!StringUtils.isEmpty(varValue)) { if (!StringUtils.isEmpty(varValue)) {
if (CalculatorVarsFragment.isValidValue(varValue)) { if (CalculatorVarsFragment.isValidValue(varValue)) {
final Intent intent = new Intent(context, CalculatorVarsFragmentActivity.class); if (context instanceof SherlockFragmentActivity) {
intent.putExtra(CalculatorVarsFragment.CREATE_VAR_EXTRA_STRING, varValue); VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newFromValue(varValue), ((SherlockFragmentActivity) context).getSupportFragmentManager());
context.startActivity(intent); } else {
} else { final Intent intent = new Intent(context, CalculatorVarsFragmentActivity.class);
Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show(); intent.putExtra(CalculatorVarsFragment.CREATE_VAR_EXTRA_STRING, varValue);
context.startActivity(intent);
}
} else {
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(R.string.c_not_valid_result, MessageType.error);
} }
} else { } else {
Toast.makeText(context, R.string.c_empty_var_error, Toast.LENGTH_SHORT).show(); CalculatorLocatorImpl.getInstance().getNotifier().showMessage(R.string.c_empty_var_error, MessageType.error);
} }
} else { } else {
Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show(); 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.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -37,7 +38,7 @@ import java.util.List;
* Date: 12/21/11 * Date: 12/21/11
* Time: 9:24 PM * Time: 9:24 PM
*/ */
public abstract class AbstractMathEntityListFragment<T extends MathEntity> extends SherlockListFragment { public abstract class AbstractMathEntityListFragment<T extends MathEntity> extends SherlockListFragment implements CalculatorEventListener {
/* /*
********************************************************************** **********************************************************************
@ -49,7 +50,7 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category"; public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category";
protected final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray())); protected final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray()));
/* /*
@ -69,6 +70,8 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
@NotNull @NotNull
private CalculatorFragmentHelper fragmentHelper; private CalculatorFragmentHelper fragmentHelper;
@NotNull
private final Handler uiHandler = new Handler();
protected int getLayoutId() { protected int getLayoutId() {
return R.layout.math_entities_fragment; return R.layout.math_entities_fragment;
@ -79,13 +82,13 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
final Bundle bundle = getArguments(); final Bundle bundle = getArguments();
if ( bundle != null ) { if (bundle != null) {
category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING); category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING);
} }
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(getLayoutId(), getTitleResId()); fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(getLayoutId(), getTitleResId());
fragmentHelper.onCreate(this); fragmentHelper.onCreate(this);
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -107,7 +110,7 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
final int position, final int position,
final long id) { final long id) {
final AMenuItem<T> onClick = getOnClickAction(); final AMenuItem<T> onClick = getOnClickAction();
if ( onClick != null ) { if (onClick != null) {
onClick.onClick(((T) parent.getItemAtPosition(position)), getActivity()); onClick.onClick(((T) parent.getItemAtPosition(position)), getActivity());
} }
} }
@ -140,10 +143,10 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
fragmentHelper.onDestroy(this); fragmentHelper.onDestroy(this);
super.onDestroy(); super.onDestroy();
} }
@NotNull @NotNull
protected abstract List<LabeledMenuItem<T>> getMenuItemsOnLongClick(@NotNull T item); protected abstract List<LabeledMenuItem<T>> getMenuItemsOnLongClick(@NotNull T item);
@Override @Override
public void onPause() { public void onPause() {
@ -192,29 +195,29 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
abstract String getMathEntityCategory(@NotNull T t); abstract String getMathEntityCategory(@NotNull T t);
protected void sort() { protected void sort() {
final MathEntityArrayAdapter<T> localAdapter = adapter; final MathEntityArrayAdapter<T> localAdapter = adapter;
if (localAdapter != null) { if (localAdapter != null) {
localAdapter.sort(new Comparator<T>() { localAdapter.sort(new Comparator<T>() {
@Override @Override
public int compare(T function1, T function2) { public int compare(T function1, T function2) {
return function1.getName().compareTo(function2.getName()); return function1.getName().compareTo(function2.getName());
} }
}); });
localAdapter.notifyDataSetChanged(); localAdapter.notifyDataSetChanged();
} }
} }
protected static class MathEntityArrayAdapter<T extends MathEntity> extends ArrayAdapter<T> { protected static class MathEntityArrayAdapter<T extends MathEntity> extends ArrayAdapter<T> {
@NotNull @NotNull
private final MathEntityDescriptionGetter descriptionGetter; private final MathEntityDescriptionGetter descriptionGetter;
private MathEntityArrayAdapter(@NotNull MathEntityDescriptionGetter descriptionGetter, private MathEntityArrayAdapter(@NotNull MathEntityDescriptionGetter descriptionGetter,
@NotNull Context context, @NotNull Context context,
int resource, int resource,
int textViewResourceId, int textViewResourceId,
@NotNull List<T> objects) { @NotNull List<T> objects) {
super(context, resource, textViewResourceId, objects); super(context, resource, textViewResourceId, objects);
this.descriptionGetter = descriptionGetter; this.descriptionGetter = descriptionGetter;
@ -243,12 +246,12 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
text.setText(String.valueOf(mathEntity)); text.setText(String.valueOf(mathEntity));
final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName()); final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName());
final TextView description = (TextView) result.findViewById(R.id.math_entity_description);
if (!StringUtils.isEmpty(mathEntityDescription)) { if (!StringUtils.isEmpty(mathEntityDescription)) {
final TextView description = (TextView) result.findViewById(R.id.math_entity_description);
description.setVisibility(View.VISIBLE); description.setVisibility(View.VISIBLE);
description.setText(mathEntityDescription); description.setText(mathEntityDescription);
} else { } else {
final TextView description = (TextView) result.findViewById(R.id.math_entity_description);
description.setVisibility(View.GONE); description.setVisibility(View.GONE);
} }
} }
@ -275,23 +278,28 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
String getDescription(@NotNull Context context, @NotNull String mathEntityName); String getDescription(@NotNull Context context, @NotNull String mathEntityName);
} }
public void addToAdapter(@NotNull T mathEntity) { public void addToAdapter(@NotNull T mathEntity) {
if (this.adapter != null) { if (this.adapter != null) {
this.adapter.add(mathEntity); this.adapter.add(mathEntity);
} }
} }
public void removeFromAdapter(@NotNull T mathEntity) { public void removeFromAdapter(@NotNull T mathEntity) {
if (this.adapter != null) { if (this.adapter != null) {
this.adapter.remove(mathEntity); this.adapter.remove(mathEntity);
} }
} }
public void notifyAdapter() { public void notifyAdapter() {
if (this.adapter != null) { if (this.adapter != null) {
this.adapter.notifyDataSetChanged(); this.adapter.notifyDataSetChanged();
} }
} }
@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) { static void putCategory(@NotNull Bundle bundle, @NotNull String categoryId) {
bundle.putString(MATH_ENTITY_CATEGORY_EXTRA_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; package org.solovyev.android.calculator.math.edit;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View; import android.view.View;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
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.CalculatorEventType; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.menu.AMenuItem; import org.solovyev.android.menu.AMenuItem;
import org.solovyev.android.menu.LabeledMenuItem; import org.solovyev.android.menu.LabeledMenuItem;
@ -38,30 +35,30 @@ import java.util.List;
*/ */
public class CalculatorVarsFragment extends AbstractMathEntityListFragment<IConstant> { 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 @Override
protected int getLayoutId() { protected int getLayoutId() {
return R.layout.vars_fragment; return R.layout.vars_fragment;
} }
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
final Bundle bundle = getArguments(); final Bundle bundle = getArguments();
if (bundle != null) { if (bundle != null) {
final String varValue = bundle.getString(CREATE_VAR_EXTRA_STRING); final String varValue = bundle.getString(CREATE_VAR_EXTRA_STRING);
if (!StringUtils.isEmpty(varValue)) { 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 // in order to stop intent for other tabs
bundle.remove(CREATE_VAR_EXTRA_STRING); bundle.remove(CREATE_VAR_EXTRA_STRING);
} }
} }
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
@Override @Override
protected int getTitleResId() { protected int getTitleResId() {
@ -74,61 +71,61 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
} }
@NotNull @NotNull
@Override @Override
protected List<LabeledMenuItem<IConstant>> getMenuItemsOnLongClick(@NotNull IConstant item) { protected List<LabeledMenuItem<IConstant>> getMenuItemsOnLongClick(@NotNull IConstant item) {
final List<LabeledMenuItem<IConstant>> result = new ArrayList<LabeledMenuItem<IConstant>>(Arrays.asList(LongClickMenuItem.values())); final List<LabeledMenuItem<IConstant>> result = new ArrayList<LabeledMenuItem<IConstant>>(Arrays.asList(LongClickMenuItem.values()));
if ( item.isSystem() ) { if (item.isSystem()) {
result.remove(LongClickMenuItem.edit); result.remove(LongClickMenuItem.edit);
result.remove(LongClickMenuItem.remove); result.remove(LongClickMenuItem.remove);
} }
if ( StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(item.getName())) ) { if (StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(item.getName()))) {
result.remove(LongClickMenuItem.copy_description); result.remove(LongClickMenuItem.copy_description);
} }
if ( StringUtils.isEmpty(item.getValue()) ) { if (StringUtils.isEmpty(item.getValue())) {
result.remove(LongClickMenuItem.copy_value); result.remove(LongClickMenuItem.copy_value);
} }
return result; return result;
} }
@NotNull @NotNull
@Override @Override
protected MathEntityDescriptionGetter getDescriptionGetter() { protected MathEntityDescriptionGetter getDescriptionGetter() {
return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry()); return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry());
} }
@SuppressWarnings({"UnusedDeclaration"}) @SuppressWarnings({"UnusedDeclaration"})
public void addVarButtonClickHandler(@NotNull View v) { public void addVarButtonClickHandler(@NotNull View v) {
VarEditDialogFragment.createEditVariableDialog(this, VarEditDialogFragment.Input.newInstance()); VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newInstance(), this.getActivity().getSupportFragmentManager());
} }
@NotNull @NotNull
@Override @Override
protected List<IConstant> getMathEntities() { protected List<IConstant> getMathEntities() {
final List<IConstant> result = new ArrayList<IConstant>(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getEntities()); final List<IConstant> result = new ArrayList<IConstant>(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getEntities());
CollectionsUtils.removeAll(result, new JPredicate<IConstant>() { CollectionsUtils.removeAll(result, new JPredicate<IConstant>() {
@Override @Override
public boolean apply(@Nullable IConstant var) { public boolean apply(@Nullable IConstant var) {
return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN); return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN);
} }
}); });
return result; return result;
} }
@Override @Override
protected String getMathEntityCategory(@NotNull IConstant var) { protected String getMathEntityCategory(@NotNull IConstant var) {
return CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getCategory(var); return CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getCategory(var);
} }
public static boolean isValidValue(@NotNull String value) { public static boolean isValidValue(@NotNull String value) {
// now every string might be constant // now every string might be constant
return true; return true;
} }
/* /*
********************************************************************** **********************************************************************
@ -144,20 +141,77 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
boolean result; boolean result;
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.var_menu_add_var: case R.id.var_menu_add_var:
VarEditDialogFragment.createEditVariableDialog(this, VarEditDialogFragment.Input.newInstance()); VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newInstance(), this.getActivity().getSupportFragmentManager());
result = true; result = true;
break; break;
default: default:
result = super.onOptionsItemSelected(item); result = super.onOptionsItemSelected(item);
} }
return result; 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();
}
});
}
}
/* /*
********************************************************************** **********************************************************************
@ -167,7 +221,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
********************************************************************** **********************************************************************
*/ */
private static enum LongClickMenuItem implements LabeledMenuItem<IConstant>{ private static enum LongClickMenuItem implements LabeledMenuItem<IConstant> {
use(R.string.c_use) { use(R.string.c_use) {
@Override @Override
public void onClick(@NotNull IConstant data, @NotNull Context context) { public void onClick(@NotNull IConstant data, @NotNull Context context) {
@ -177,19 +231,15 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
edit(R.string.c_edit) { edit(R.string.c_edit) {
@Override @Override
public void onClick(@NotNull IConstant data, @NotNull Context context) { public void onClick(@NotNull IConstant constant, @NotNull Context context) {
/*if (context instanceof AbstractMathEntityListFragment) { VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newFromConstant(constant), ((SherlockFragmentActivity) context).getSupportFragmentManager());
createEditVariableDialog((AbstractMathEntityListFragment<IConstant>)context, data, data.getName(), StringUtils.getNotEmpty(data.getValue(), ""), data.getDescription());
}*/
} }
}, },
remove(R.string.c_remove) { remove(R.string.c_remove) {
@Override @Override
public void onClick(@NotNull IConstant data, @NotNull Context context) { public void onClick(@NotNull IConstant constant, @NotNull Context context) {
/*if (context instanceof AbstractMathEntityListFragment) { new MathEntityRemover<IConstant>(constant, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), context, context).showConfirmationDialog();
new MathEntityRemover<IConstant>(data, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), ((AbstractMathEntityListFragment<IConstant>) context)).showConfirmationDialog();
}*/
} }
}, },
@ -198,8 +248,8 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
public void onClick(@NotNull IConstant data, @NotNull Context context) { public void onClick(@NotNull IConstant data, @NotNull Context context) {
final String text = data.getValue(); final String text = data.getValue();
if (!StringUtils.isEmpty(text)) { if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); assert text != null;
clipboard.setText(text); CalculatorLocatorImpl.getInstance().getClipboard().setText(text);
} }
} }
}, },
@ -209,8 +259,8 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
public void onClick(@NotNull IConstant data, @NotNull Context context) { public void onClick(@NotNull IConstant data, @NotNull Context context) {
final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName()); final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName());
if (!StringUtils.isEmpty(text)) { if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); assert text != null;
clipboard.setText(text); CalculatorLocatorImpl.getInstance().getClipboard().setText(text);
} }
} }
}; };

View File

@ -7,12 +7,16 @@
package org.solovyev.android.calculator.math.edit; package org.solovyev.android.calculator.math.edit;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView; import android.widget.TextView;
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.R; import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.CalculatorMathRegistry; import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.calculator.R;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
/** /**
@ -20,7 +24,7 @@ import org.solovyev.common.math.MathEntity;
* Date: 12/22/11 * Date: 12/22/11
* Time: 9:36 PM * Time: 9:36 PM
*/ */
class MathEntityRemover<T extends MathEntity> implements DialogInterface.OnClickListener { class MathEntityRemover<T extends MathEntity> implements View.OnClickListener, DialogInterface.OnClickListener {
@NotNull @NotNull
private final T mathEntity; private final T mathEntity;
@ -33,56 +37,64 @@ class MathEntityRemover<T extends MathEntity> implements DialogInterface.OnClick
@NotNull @NotNull
private final CalculatorMathRegistry<? super T> varsRegistry; private final CalculatorMathRegistry<? super T> varsRegistry;
@NotNull @NotNull
private final AbstractMathEntityListFragment<T> fragment; private Context context;
public MathEntityRemover(@NotNull T mathEntity, @NotNull
@Nullable DialogInterface.OnClickListener callbackOnCancel, private final Object source;
@NotNull CalculatorMathRegistry<? super T> varsRegistry,
@NotNull AbstractMathEntityListFragment<T> fragment) { public MathEntityRemover(@NotNull T mathEntity,
this(mathEntity, callbackOnCancel, false, varsRegistry, fragment); @Nullable DialogInterface.OnClickListener callbackOnCancel,
@NotNull CalculatorMathRegistry<? super T> varsRegistry,
@NotNull Context context,
@NotNull Object source) {
this(mathEntity, callbackOnCancel, false, varsRegistry, context, source);
} }
public MathEntityRemover(@NotNull T mathEntity, public MathEntityRemover(@NotNull T mathEntity,
@Nullable DialogInterface.OnClickListener callbackOnCancel, @Nullable DialogInterface.OnClickListener callbackOnCancel,
boolean confirmed, boolean confirmed,
@NotNull CalculatorMathRegistry<? super T> varsRegistry, @NotNull CalculatorMathRegistry<? super T> varsRegistry,
@NotNull AbstractMathEntityListFragment<T> fragment) { @NotNull Context context,
@NotNull Object source) {
this.mathEntity = mathEntity; this.mathEntity = mathEntity;
this.callbackOnCancel = callbackOnCancel; this.callbackOnCancel = callbackOnCancel;
this.confirmed = confirmed; this.confirmed = confirmed;
this.varsRegistry = varsRegistry; 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() { public void showConfirmationDialog() {
final TextView question = new TextView(fragment.getActivity()); final TextView question = new TextView(context);
question.setText(String.format(fragment.getString(R.string.c_var_removal_confirmation_question), mathEntity.getName())); question.setText(String.format(context.getString(R.string.c_var_removal_confirmation_question), mathEntity.getName()));
question.setPadding(6, 6, 6, 6); 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) .setCancelable(true)
.setView(question) .setView(question)
.setTitle(R.string.c_var_removal_confirmation) .setTitle(R.string.c_var_removal_confirmation)
.setNegativeButton(R.string.c_no, callbackOnCancel) .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(); 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; 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.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.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
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.CalculatorLocatorImpl; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.model.Var;
import org.solovyev.common.text.StringUtils;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 01.10.12 * Date: 01.10.12
* Time: 17:41 * Time: 17:41
*/ */
public class VarEditDialogFragment extends DialogFragment { public class VarEditDialogFragment extends DialogFragment implements CalculatorEventListener {
@NotNull @NotNull
private final Input input; private final Input input;
@ -38,10 +37,7 @@ public class VarEditDialogFragment extends DialogFragment {
this.input = input; this.input = input;
} }
public static void createEditVariableDialog(@NotNull final AbstractMathEntityListFragment<IConstant> fragment, public static void createEditVariableDialog(@NotNull Input input, @NotNull FragmentManager fm) {
@NotNull Input input) {
final FragmentManager fm = fragment.getActivity().getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction(); final FragmentTransaction ft = fm.beginTransaction();
Fragment prev = fm.findFragmentByTag("constant-editor"); 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 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 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 @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); final EditText editValue = (EditText) root.findViewById(R.id.var_edit_value);
editValue.setText(input.getValue()); editValue.setText(input.getValue());
@ -199,12 +119,39 @@ public class VarEditDialogFragment extends DialogFragment {
varBuilder = new Var.Builder(); 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 ) { if ( constant == null ) {
// CREATE MODE // CREATE MODE
getDialog().setTitle(R.string.c_var_create_var); getDialog().setTitle(R.string.c_var_create_var);
root.findViewById(R.id.remove_button).setVisibility(View.GONE);
} else { } else {
// EDIT MODE // EDIT MODE
getDialog().setTitle(R.string.c_var_edit_var); 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; package org.solovyev.android.calculator.math.edit;
import android.content.DialogInterface;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast;
import jscl.text.Identifier; import jscl.text.Identifier;
import jscl.text.MutableInt; import jscl.text.MutableInt;
import jscl.text.ParseException; import jscl.text.ParseException;
import jscl.text.Parser; import jscl.text.Parser;
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.CalculatorLocatorImpl; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.MathEntityBuilder; import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.text.StringUtils;
/** /**
@ -29,18 +26,7 @@ import org.solovyev.common.text.StringUtils;
* Date: 12/22/11 * Date: 12/22/11
* Time: 9:52 PM * Time: 9:52 PM
*/ */
class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickListener { class VarEditorSaver<T extends MathEntity> implements View.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;
@NotNull @NotNull
private final MathEntityBuilder<? extends T> varBuilder; private final MathEntityBuilder<? extends T> varBuilder;
@ -51,105 +37,95 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
@NotNull @NotNull
private final CalculatorMathRegistry<T> mathRegistry; private final CalculatorMathRegistry<T> mathRegistry;
@NotNull @NotNull
private final AbstractMathEntityListFragment<T> fragment; private final Object source;
@NotNull @NotNull
private View editView; private View editView;
public VarEditorSaver(@NotNull MathEntityBuilder<? extends T> varBuilder, public VarEditorSaver(@NotNull MathEntityBuilder<? extends T> varBuilder,
@Nullable T editedInstance, @Nullable T editedInstance,
@NotNull View editView, @NotNull View editView,
@NotNull AbstractMathEntityListFragment<T> fragment, @NotNull CalculatorMathRegistry<T> mathRegistry,
@NotNull CalculatorMathRegistry<T> mathRegistry, @NotNull Object source) {
@NotNull EditorCreator<T> editorCreator) {
this.varBuilder = varBuilder; this.varBuilder = varBuilder;
this.editedInstance = editedInstance; this.editedInstance = editedInstance;
this.editView = editView; this.editView = editView;
this.fragment = fragment;
this.mathRegistry = mathRegistry; this.mathRegistry = mathRegistry;
this.editorCreator = editorCreator; this.source = source;
} }
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(View v) {
if (which == DialogInterface.BUTTON_POSITIVE) { final Integer error;
final Integer error;
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name); final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
String name = editName.getText().toString(); String name = editName.getText().toString();
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value); final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
String value = editValue.getText().toString(); String value = editValue.getText().toString();
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
String description = editDescription.getText().toString(); String description = editDescription.getText().toString();
if (isValidName(name)) { if (isValidName(name)) {
boolean canBeSaved = false; boolean canBeSaved = false;
final T entityFromRegistry = mathRegistry.get(name); final T entityFromRegistry = mathRegistry.get(name);
if (entityFromRegistry == null) { if (entityFromRegistry == null) {
canBeSaved = true; canBeSaved = true;
} else if (editedInstance != null && entityFromRegistry.getId().equals(editedInstance.getId())) { } else if (editedInstance != null && entityFromRegistry.getId().equals(editedInstance.getId())) {
canBeSaved = true; canBeSaved = true;
} }
if (canBeSaved) { if (canBeSaved) {
final MathType.Result mathType = MathType.getType(name, 0, false); final MathType.Result mathType = MathType.getType(name, 0, false);
if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) { if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) {
if (StringUtils.isEmpty(value)) { if (StringUtils.isEmpty(value)) {
// value is empty => undefined variable // value is empty => undefined variable
varBuilder.setName(name); varBuilder.setName(name);
varBuilder.setDescription(description); varBuilder.setDescription(description);
varBuilder.setValue(null); varBuilder.setValue(null);
error = null; error = null;
} else { } else {
// value is not empty => must be a number // value is not empty => must be a number
boolean valid = CalculatorVarsFragment.isValidValue(value); boolean valid = CalculatorVarsFragment.isValidValue(value);
if (valid) { if (valid) {
varBuilder.setName(name); varBuilder.setName(name);
varBuilder.setDescription(description); varBuilder.setDescription(description);
varBuilder.setValue(value); varBuilder.setValue(value);
error = null; error = null;
} else { } else {
error = R.string.c_value_is_not_a_number; error = R.string.c_value_is_not_a_number;
} }
} }
} else { } else {
error = R.string.c_var_name_clashes; error = R.string.c_var_name_clashes;
} }
} else { } else {
error = R.string.c_var_already_exists; error = R.string.c_var_already_exists;
} }
} else { } else {
error = R.string.c_name_is_not_valid; error = R.string.c_name_is_not_valid;
} }
if (error != null) { if (error != null) {
Toast.makeText(fragment.getActivity(), fragment.getString(error), Toast.LENGTH_LONG).show(); CalculatorLocatorImpl.getInstance().getNotifier().showMessage(error, MessageType.error);
editorCreator.showEditor(fragment, editedInstance, name, value, description); } else {
} else { final T addedVar = mathRegistry.add(varBuilder);
final T addedVar = mathRegistry.add(varBuilder); mathRegistry.save();
if (fragment.isInCategory(addedVar)) {
if (editedInstance != null) {
fragment.removeFromAdapter(editedInstance);
}
fragment.addToAdapter(addedVar);
}
mathRegistry.save(); if (editedInstance == null) {
CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_added, addedVar, source);
if (fragment.isInCategory(addedVar)) { } else {
fragment.sort(); CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_changed, ChangeImpl.newInstance(editedInstance, addedVar), source);
} }
} }
} }
}
boolean isValidName(@Nullable String name) { boolean isValidName(@Nullable String name) {
boolean result = false; boolean result = false;

View File

@ -9,6 +9,7 @@ package org.solovyev.android.calculator.model;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -16,7 +17,6 @@ import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister; import org.simpleframework.xml.core.Persister;
import org.solovyev.android.calculator.CalculatorMathRegistry; import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.about.TextHelper;
import org.solovyev.common.JBuilder; import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
@ -67,7 +67,13 @@ public abstract class AbstractAndroidMathRegistry<T extends MathEntity, P extend
stringName = prefix + substitute; 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() { public synchronized void load() {

View File

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