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"
style="@style/default_text_size"> a:layout_width="match_parent"
</EditText> style="@style/default_text_size"
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"
style="@style/default_text_size"> a:layout_width="match_parent"
</EditText> style="@style/default_text_size"
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"
style="@style/default_text_size"> a:layout_width="match_parent"
</EditText> 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> </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);
if (activity instanceof CalculatorActivity) {
actionBar.setDisplayShowTitleEnabled(false);
} else {
actionBar.setDisplayShowTitleEnabled(true); 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)) {
if (context instanceof SherlockFragmentActivity) {
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newFromValue(varValue), ((SherlockFragmentActivity) context).getSupportFragmentManager());
} else {
final Intent intent = new Intent(context, CalculatorVarsFragmentActivity.class); final Intent intent = new Intent(context, CalculatorVarsFragmentActivity.class);
intent.putExtra(CalculatorVarsFragment.CREATE_VAR_EXTRA_STRING, varValue); intent.putExtra(CalculatorVarsFragment.CREATE_VAR_EXTRA_STRING, varValue);
context.startActivity(intent); context.startActivity(intent);
} else {
Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show();
} }
} else { } 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 { } 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.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 {
/* /*
********************************************************************** **********************************************************************
@ -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,7 +82,7 @@ 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);
} }
@ -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());
} }
} }
@ -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());
if (!StringUtils.isEmpty(mathEntityDescription)) {
final TextView description = (TextView) result.findViewById(R.id.math_entity_description); final TextView description = (TextView) result.findViewById(R.id.math_entity_description);
if (!StringUtils.isEmpty(mathEntityDescription)) {
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);
} }
} }
@ -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) { 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,7 +35,7 @@ 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() {
@ -53,7 +50,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
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);
@ -78,16 +75,16 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
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);
} }
@ -102,7 +99,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
@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
@ -149,7 +146,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
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:
@ -159,6 +156,63 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
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;
@ -34,55 +38,63 @@ class MathEntityRemover<T extends MathEntity> implements DialogInterface.OnClick
private final CalculatorMathRegistry<? super T> varsRegistry; private final CalculatorMathRegistry<? super T> varsRegistry;
@NotNull @NotNull
private final AbstractMathEntityListFragment<T> fragment; private Context context;
@NotNull
private final Object source;
public MathEntityRemover(@NotNull T mathEntity, public MathEntityRemover(@NotNull T mathEntity,
@Nullable DialogInterface.OnClickListener callbackOnCancel, @Nullable DialogInterface.OnClickListener callbackOnCancel,
@NotNull CalculatorMathRegistry<? super T> varsRegistry, @NotNull CalculatorMathRegistry<? super T> varsRegistry,
@NotNull AbstractMathEntityListFragment<T> fragment) { @NotNull Context context,
this(mathEntity, callbackOnCancel, false, varsRegistry, fragment); @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;
@ -52,7 +38,7 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
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;
@ -60,20 +46,17 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
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 EditorCreator<T> editorCreator) { @NotNull Object source) {
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);
@ -131,22 +114,15 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
} }
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);
if (fragment.isInCategory(addedVar)) {
if (editedInstance != null) {
fragment.removeFromAdapter(editedInstance);
}
fragment.addToAdapter(addedVar);
}
mathRegistry.save(); mathRegistry.save();
if (fragment.isInCategory(addedVar)) { if (editedInstance == null) {
fragment.sort(); 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.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