This commit is contained in:
Sergey Solovyev 2012-10-01 00:35:20 +04:00
parent e946b1547c
commit 2e7f4b632d
17 changed files with 1256 additions and 1120 deletions

View File

@ -1,299 +1,301 @@
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.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.history.EditorHistoryState; import org.solovyev.android.calculator.history.EditorHistoryState;
import org.solovyev.common.gui.CursorControl; import org.solovyev.common.gui.CursorControl;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.text.StringUtils;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 21.09.12 * Date: 21.09.12
* Time: 11:53 * Time: 11:53
*/ */
public class CalculatorEditorImpl implements CalculatorEditor { public class CalculatorEditorImpl implements CalculatorEditor {
@Nullable @Nullable
private CalculatorEditorView view; private CalculatorEditorView view;
@NotNull @NotNull
private final Object viewLock = new Object(); private final Object viewLock = new Object();
@NotNull @NotNull
private CalculatorEditorViewState lastViewState = CalculatorEditorViewStateImpl.newDefaultInstance(); private CalculatorEditorViewState lastViewState = CalculatorEditorViewStateImpl.newDefaultInstance();
@NotNull @NotNull
private final Calculator calculator; private final Calculator calculator;
@NotNull @NotNull
private final CursorControlAdapter cursorControlAdapter = new CursorControlAdapter(this); private final CursorControlAdapter cursorControlAdapter = new CursorControlAdapter(this);
public CalculatorEditorImpl(@NotNull Calculator calculator) { public CalculatorEditorImpl(@NotNull Calculator calculator) {
this.calculator = calculator; this.calculator = calculator;
this.calculator.addCalculatorEventListener(this); this.calculator.addCalculatorEventListener(this);
} }
@Override @Override
public void setView(@Nullable CalculatorEditorView view) { public void setView(@Nullable CalculatorEditorView view) {
synchronized (viewLock) { synchronized (viewLock) {
this.view = view; this.view = view;
if ( view != null ) { if ( view != null ) {
view.setState(lastViewState); view.setState(lastViewState);
} }
} }
} }
@NotNull @NotNull
@Override @Override
public CalculatorEditorViewState getViewState() { public CalculatorEditorViewState getViewState() {
return lastViewState; return lastViewState;
} }
@Override @Override
public void updateViewState() { public void updateViewState() {
setViewState(this.lastViewState, false); setViewState(this.lastViewState, false);
} }
@Override @Override
public void setViewState(@NotNull CalculatorEditorViewState newViewState) { public void setViewState(@NotNull CalculatorEditorViewState newViewState) {
setViewState(newViewState, true); setViewState(newViewState, true);
} }
private void setViewState(CalculatorEditorViewState newViewState, boolean fireEvent) { private void setViewState(CalculatorEditorViewState newViewState, boolean fireEvent) {
synchronized (viewLock) { synchronized (viewLock) {
final CalculatorEditorViewState oldViewState = this.lastViewState; final CalculatorEditorViewState oldViewState = this.lastViewState;
this.lastViewState = newViewState; this.lastViewState = newViewState;
if (this.view != null) { if (this.view != null) {
this.view.setState(newViewState); this.view.setState(newViewState);
} }
if (fireEvent) { if (fireEvent) {
calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState)); calculator.fireCalculatorEvent(CalculatorEventType.editor_state_changed, new CalculatorEditorChangeEventDataImpl(oldViewState, newViewState));
} }
} }
} }
@Override @Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData,
@NotNull CalculatorEventType calculatorEventType, @NotNull CalculatorEventType calculatorEventType,
@Nullable Object data) { @Nullable Object data) {
if (calculatorEventType == CalculatorEventType.use_history_state) { switch (calculatorEventType) {
final CalculatorHistoryState calculatorHistoryState = (CalculatorHistoryState)data; case use_history_state:
final EditorHistoryState editorState = calculatorHistoryState.getEditorState(); final CalculatorHistoryState calculatorHistoryState = (CalculatorHistoryState)data;
this.setText(StringUtils.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition()); final EditorHistoryState editorState = calculatorHistoryState.getEditorState();
} this.setText(StringUtils.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition());
} break;
}
/* }
**********************************************************************
* /*
* SELECTION **********************************************************************
* *
********************************************************************** * SELECTION
*/ *
**********************************************************************
@NotNull */
private CalculatorEditorViewState newSelectionViewState(int newSelection) {
if (this.lastViewState.getSelection() != newSelection) { @NotNull
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, newSelection); private CalculatorEditorViewState newSelectionViewState(int newSelection) {
setViewState(result); if (this.lastViewState.getSelection() != newSelection) {
return result; final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, newSelection);
} else { setViewState(result);
return this.lastViewState; return result;
} } else {
} return this.lastViewState;
}
@NotNull }
public CalculatorEditorViewState setCursorOnStart() {
synchronized (viewLock) { @NotNull
return newSelectionViewState(0); public CalculatorEditorViewState setCursorOnStart() {
} synchronized (viewLock) {
} return newSelectionViewState(0);
}
}
@NotNull
public CalculatorEditorViewState setCursorOnEnd() {
synchronized (viewLock) { @NotNull
return newSelectionViewState(this.lastViewState.getText().length()); public CalculatorEditorViewState setCursorOnEnd() {
} synchronized (viewLock) {
} return newSelectionViewState(this.lastViewState.getText().length());
}
@NotNull }
public CalculatorEditorViewState moveCursorLeft() {
synchronized (viewLock) { @NotNull
if (this.lastViewState.getSelection() > 0) { public CalculatorEditorViewState moveCursorLeft() {
return newSelectionViewState(this.lastViewState.getSelection() - 1); synchronized (viewLock) {
} else { if (this.lastViewState.getSelection() > 0) {
return this.lastViewState; return newSelectionViewState(this.lastViewState.getSelection() - 1);
} } else {
} return this.lastViewState;
} }
}
@NotNull }
public CalculatorEditorViewState moveCursorRight() {
synchronized (viewLock) { @NotNull
if (this.lastViewState.getSelection() < this.lastViewState.getText().length()) { public CalculatorEditorViewState moveCursorRight() {
return newSelectionViewState(this.lastViewState.getSelection() + 1); synchronized (viewLock) {
} else { if (this.lastViewState.getSelection() < this.lastViewState.getText().length()) {
return this.lastViewState; return newSelectionViewState(this.lastViewState.getSelection() + 1);
} } else {
} return this.lastViewState;
} }
}
@NotNull }
@Override
public CursorControl asCursorControl() { @NotNull
return cursorControlAdapter; @Override
} public CursorControl asCursorControl() {
return cursorControlAdapter;
/* }
**********************************************************************
* /*
* EDITOR ACTIONS **********************************************************************
* *
********************************************************************** * EDITOR ACTIONS
*/ *
**********************************************************************
@NotNull */
@Override
public CalculatorEditorViewState erase() { @NotNull
synchronized (viewLock) { @Override
int selection = this.lastViewState.getSelection(); public CalculatorEditorViewState erase() {
final String text = this.lastViewState.getText(); synchronized (viewLock) {
if (selection > 0 && text.length() > 0 && selection <= text.length()) { int selection = this.lastViewState.getSelection();
final StringBuilder newText = new StringBuilder(text.length() - 1); final String text = this.lastViewState.getText();
newText.append(text.substring(0, selection - 1)).append(text.substring(selection, text.length())); if (selection > 0 && text.length() > 0 && selection <= text.length()) {
final StringBuilder newText = new StringBuilder(text.length() - 1);
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), selection - 1); newText.append(text.substring(0, selection - 1)).append(text.substring(selection, text.length()));
setViewState(result);
return result; final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), selection - 1);
} else { setViewState(result);
return this.lastViewState; return result;
} } else {
} return this.lastViewState;
} }
}
@NotNull }
@Override
public CalculatorEditorViewState clear() { @NotNull
synchronized (viewLock) { @Override
return setText(""); public CalculatorEditorViewState clear() {
} synchronized (viewLock) {
} return setText("");
}
@NotNull }
@Override
public CalculatorEditorViewState setText(@NotNull String text) { @NotNull
synchronized (viewLock) { @Override
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, text.length()); public CalculatorEditorViewState setText(@NotNull String text) {
setViewState(result); synchronized (viewLock) {
return result; final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, text.length());
} setViewState(result);
} return result;
}
@NotNull }
@Override
public CalculatorEditorViewState setText(@NotNull String text, int selection) { @NotNull
synchronized (viewLock) { @Override
selection = correctSelection(selection, text); public CalculatorEditorViewState setText(@NotNull String text, int selection) {
synchronized (viewLock) {
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, selection); selection = correctSelection(selection, text);
setViewState(result);
return result; final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(text, selection);
} setViewState(result);
} return result;
}
@NotNull }
@Override
public CalculatorEditorViewState insert(@NotNull String text) { @NotNull
synchronized (viewLock) { @Override
return insert(text, 0); public CalculatorEditorViewState insert(@NotNull String text) {
} synchronized (viewLock) {
} return insert(text, 0);
}
@NotNull }
@Override
public CalculatorEditorViewState insert(@NotNull String text, int selectionOffset) { @NotNull
synchronized (viewLock) { @Override
final int selection = this.lastViewState.getSelection(); public CalculatorEditorViewState insert(@NotNull String text, int selectionOffset) {
final String oldText = this.lastViewState.getText(); synchronized (viewLock) {
final int selection = this.lastViewState.getSelection();
int newTextLength = text.length() + oldText.length(); final String oldText = this.lastViewState.getText();
final StringBuilder newText = new StringBuilder(newTextLength);
int newTextLength = text.length() + oldText.length();
newText.append(oldText.substring(0, selection)); final StringBuilder newText = new StringBuilder(newTextLength);
newText.append(text);
newText.append(oldText.substring(selection)); newText.append(oldText.substring(0, selection));
newText.append(text);
int newSelection = correctSelection(text.length() + selection + selectionOffset, newTextLength); newText.append(oldText.substring(selection));
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), newSelection);
setViewState(result); int newSelection = correctSelection(text.length() + selection + selectionOffset, newTextLength);
return result; final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newInstance(newText.toString(), newSelection);
} setViewState(result);
} return result;
}
@NotNull }
@Override
public CalculatorEditorViewState moveSelection(int offset) { @NotNull
synchronized (viewLock) { @Override
int selection = this.lastViewState.getSelection() + offset; public CalculatorEditorViewState moveSelection(int offset) {
synchronized (viewLock) {
return setSelection(selection); int selection = this.lastViewState.getSelection() + offset;
}
} return setSelection(selection);
}
@NotNull }
@Override
public CalculatorEditorViewState setSelection(int selection) { @NotNull
synchronized (viewLock) { @Override
selection = correctSelection(selection, this.lastViewState.getText()); public CalculatorEditorViewState setSelection(int selection) {
synchronized (viewLock) {
final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, selection); selection = correctSelection(selection, this.lastViewState.getText());
setViewState(result);
return result; final CalculatorEditorViewState result = CalculatorEditorViewStateImpl.newSelection(this.lastViewState, selection);
} setViewState(result);
} return result;
}
private int correctSelection(int selection, @NotNull String text) { }
return correctSelection(selection, text.length());
} private int correctSelection(int selection, @NotNull String text) {
return correctSelection(selection, text.length());
private int correctSelection(int selection, int textLength) { }
int result = Math.max(selection, 0);
result = Math.min(result, textLength); private int correctSelection(int selection, int textLength) {
return result; int result = Math.max(selection, 0);
} result = Math.min(result, textLength);
return result;
private static final class CursorControlAdapter implements CursorControl { }
@NotNull private static final class CursorControlAdapter implements CursorControl {
private final CalculatorEditor calculatorEditor;
@NotNull
private CursorControlAdapter(@NotNull CalculatorEditor calculatorEditor) { private final CalculatorEditor calculatorEditor;
this.calculatorEditor = calculatorEditor;
} private CursorControlAdapter(@NotNull CalculatorEditor calculatorEditor) {
this.calculatorEditor = calculatorEditor;
@Override }
public void setCursorOnStart() {
this.calculatorEditor.setCursorOnStart(); @Override
} public void setCursorOnStart() {
this.calculatorEditor.setCursorOnStart();
@Override }
public void setCursorOnEnd() {
this.calculatorEditor.setCursorOnEnd(); @Override
} public void setCursorOnEnd() {
this.calculatorEditor.setCursorOnEnd();
@Override }
public void moveCursorLeft() {
this.calculatorEditor.moveCursorLeft(); @Override
} public void moveCursorLeft() {
this.calculatorEditor.moveCursorLeft();
@Override }
public void moveCursorRight() {
this.calculatorEditor.moveCursorRight(); @Override
} public void moveCursorRight() {
} this.calculatorEditor.moveCursorRight();
} }
}
}

View File

@ -1,104 +1,121 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 20.09.12 * Date: 20.09.12
* Time: 16:40 * Time: 16:40
*/ */
public enum CalculatorEventType { public enum CalculatorEventType {
/* /*
********************************************************************** **********************************************************************
* *
* CALCULATION * CALCULATION
* org.solovyev.android.calculator.CalculatorEvaluationEventData * org.solovyev.android.calculator.CalculatorEvaluationEventData
* *
********************************************************************** **********************************************************************
*/ */
// @NotNull CalculatorEditorViewState // @NotNull CalculatorEditorViewState
manual_calculation_requested, manual_calculation_requested,
// @NotNull org.solovyev.android.calculator.CalculatorInput // @NotNull org.solovyev.android.calculator.CalculatorInput
calculation_started, calculation_started,
// @NotNull org.solovyev.android.calculator.CalculatorOutput // @NotNull org.solovyev.android.calculator.CalculatorOutput
calculation_result, calculation_result,
calculation_cancelled, calculation_cancelled,
// @NotNull org.solovyev.android.calculator.CalculatorFailure // @NotNull org.solovyev.android.calculator.CalculatorFailure
calculation_failed, calculation_failed,
calculation_finished, calculation_finished,
/* /*
********************************************************************** **********************************************************************
* *
* CONVERSION * CONVERSION
* CalculatorConversionEventData * CalculatorConversionEventData
* *
********************************************************************** **********************************************************************
*/ */
conversion_started, conversion_started,
// @NotNull String conversion result // @NotNull String conversion result
conversion_result, conversion_result,
// @NotNull ConversionFailure // @NotNull ConversionFailure
conversion_failed, conversion_failed,
conversion_finished, conversion_finished,
/* /*
********************************************************************** **********************************************************************
* *
* EDITOR * EDITOR
* *
********************************************************************** **********************************************************************
*/ */
// @NotNull org.solovyev.android.calculator.CalculatorEditorChangeEventData // @NotNull org.solovyev.android.calculator.CalculatorEditorChangeEventData
editor_state_changed, editor_state_changed,
// @NotNull CalculatorDisplayChangeEventData // @NotNull CalculatorDisplayChangeEventData
display_state_changed, display_state_changed,
/* /*
********************************************************************** **********************************************************************
* *
* ENGINE * ENGINE
* *
********************************************************************** **********************************************************************
*/ */
engine_preferences_changed, engine_preferences_changed,
/* /*
********************************************************************** **********************************************************************
* *
* HISTORY * HISTORY
* *
********************************************************************** **********************************************************************
*/ */
// @NotNull CalculatorHistoryState // @NotNull CalculatorHistoryState
history_state_added, history_state_added,
// @NotNull CalculatorHistoryState // @NotNull CalculatorHistoryState
use_history_state; use_history_state,
public boolean isOfType(@NotNull CalculatorEventType... types) { /*
for (CalculatorEventType type : types) { **********************************************************************
if ( this == type ) { *
return true; * MATH ENTITIES
} *
} **********************************************************************
*/
return false;
} // @NotNull IConstant
use_constant,
}
// @NotNull Function
use_function,
// @NotNull Operator
use_operator;
public boolean isOfType(@NotNull CalculatorEventType... types) {
for (CalculatorEventType type : types) {
if ( this == type ) {
return true;
}
}
return false;
}
}

View File

@ -4,6 +4,9 @@ import jscl.AbstractJsclArithmeticException;
import jscl.NumeralBase; import jscl.NumeralBase;
import jscl.NumeralBaseException; import jscl.NumeralBaseException;
import jscl.math.Generic; import jscl.math.Generic;
import jscl.math.function.Function;
import jscl.math.function.IConstant;
import jscl.math.operator.Operator;
import jscl.text.ParseInterruptedException; import jscl.text.ParseInterruptedException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -16,7 +19,8 @@ import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.msg.MessageRegistry; import org.solovyev.common.msg.MessageRegistry;
import org.solovyev.common.msg.MessageType; import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.text.StringUtils;
import org.solovyev.math.units.*; import org.solovyev.math.units.ConversionException;
import org.solovyev.math.units.ConversionUtils;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -381,9 +385,26 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
evaluate(JsclOperation.numeric, changeEventData.getNewState().getText(), calculatorEventData.getSequenceId()); evaluate(JsclOperation.numeric, changeEventData.getNewState().getText(), calculatorEventData.getSequenceId());
} }
break; break;
case engine_preferences_changed: case engine_preferences_changed:
evaluate(calculatorEventData.getSequenceId()); evaluate(calculatorEventData.getSequenceId());
break; break;
case use_constant:
final IConstant constant = (IConstant)data;
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(constant.getName());
break;
case use_operator:
final Operator operator = (Operator)data;
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(operator.getName());
break;
case use_function:
final Function function = (Function)data;
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(function.getName());
break;
} }
} }

View File

@ -41,6 +41,8 @@
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_functions" android:name=".math.edit.CalculatorFunctionsFragmentActivity"/> <activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_functions" android:name=".math.edit.CalculatorFunctionsFragmentActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_operators" android:name=".math.edit.CalculatorOperatorsFragmentActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_vars_and_constants" android:name=".math.edit.CalculatorVarsFragmentActivity"/> <activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_vars_and_constants" android:name=".math.edit.CalculatorVarsFragmentActivity"/>
<activity android:label="@string/c_plot_graph" android:name=".plot.CalculatorPlotActivity"/> <activity android:label="@string/c_plot_graph" android:name=".plot.CalculatorPlotActivity"/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

View File

@ -77,6 +77,10 @@ public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper imple
activity.setContentView(layoutId); activity.setContentView(layoutId);
CalculatorButtons.processButtons(true, theme, activity.getWindow().getDecorView()); CalculatorButtons.processButtons(true, theme, activity.getWindow().getDecorView());
if (savedInstanceState != null) {
navPosition = savedInstanceState.getInt(SELECTED_NAV, 0);
}
} }
@Override @Override
@ -89,10 +93,7 @@ public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper imple
actionBar.setHomeButtonEnabled(false); actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true); actionBar.setDisplayShowTitleEnabled(true);
actionBar.setIcon(R.drawable.icon_action_bar);
if (savedInstanceState != null) {
navPosition = savedInstanceState.getInt(SELECTED_NAV, 0);
}
} }
@Override @Override
@ -150,10 +151,14 @@ public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper imple
final ActionBar.Tab tab = actionBar.newTab(); final ActionBar.Tab tab = actionBar.newTab();
tab.setTag(tag); tab.setTag(tag);
tab.setText(captionResId); tab.setText(captionResId);
tab.setTabListener(new ActionBarFragmentTabListener(activity, tag, fragmentClass, fragmentArgs, parentViewId));
final ActionBarFragmentTabListener listener = new ActionBarFragmentTabListener(activity, tag, fragmentClass, fragmentArgs, parentViewId);
tab.setTabListener(listener);
actionBar.addTab(tab); actionBar.addTab(tab);
fragmentTags.add(tag); fragmentTags.add(tag);
restoreSavedTab(activity);
} }
@Override @Override

View File

@ -10,10 +10,7 @@ import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.about.CalculatorAboutTabActivity; import org.solovyev.android.calculator.about.CalculatorAboutTabActivity;
import org.solovyev.android.calculator.help.CalculatorHelpTabActivity; import org.solovyev.android.calculator.help.CalculatorHelpTabActivity;
import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity; import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity;
import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragmentActivity; import org.solovyev.android.calculator.math.edit.*;
import org.solovyev.android.calculator.math.edit.CalculatorOperatorsFragment;
import org.solovyev.android.calculator.math.edit.CalculatorVarsFragment;
import org.solovyev.android.calculator.math.edit.CalculatorVarsFragmentActivity;
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.text.StringUtils; import org.solovyev.common.text.StringUtils;
@ -46,7 +43,7 @@ public class CalculatorActivityLauncher {
} }
public static void showOperators(@NotNull final Context context) { public static void showOperators(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorOperatorsFragment.class)); context.startActivity(new Intent(context, CalculatorOperatorsFragmentActivity.class));
} }
public static void showVars(@NotNull final Context context) { public static void showVars(@NotNull final Context context) {

View File

@ -19,6 +19,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.*;
import org.solovyev.android.menu.AMenuBuilder; import org.solovyev.android.menu.AMenuBuilder;
import org.solovyev.android.menu.AMenuItem;
import org.solovyev.android.menu.LabeledMenuItem; import org.solovyev.android.menu.LabeledMenuItem;
import org.solovyev.android.menu.MenuImpl; import org.solovyev.android.menu.MenuImpl;
import org.solovyev.common.equals.EqualsTool; import org.solovyev.common.equals.EqualsTool;
@ -105,9 +106,10 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
final View view, final View view,
final int position, final int position,
final long id) { final long id) {
final AMenuItem<T> onClick = getOnClickAction();
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(((MathEntity) parent.getItemAtPosition(position)).getName()); if ( onClick != null ) {
getActivity().finish(); onClick.onClick(((T) parent.getItemAtPosition(position)), getActivity());
}
} }
}); });
@ -128,6 +130,9 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
}); });
} }
@Nullable
protected abstract AMenuItem<T> getOnClickAction();
protected abstract int getTitleResId(); protected abstract int getTitleResId();
@Override @Override
@ -216,32 +221,35 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
} }
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, @Nullable View convertView, ViewGroup parent) {
final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent); final ViewGroup result;
final T mathEntity = getItem(position); if (convertView == null) {
result = (ViewGroup) super.getView(position, convertView, parent);
final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName()); fillView(position, result);
if (!StringUtils.isEmpty(mathEntityDescription)) {
TextView description = (TextView) result.findViewById(R.id.math_entity_description);
if (description == null) {
final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final ViewGroup itemView = (ViewGroup) layoutInflater.inflate(R.layout.math_entity, null);
description = (TextView) itemView.findViewById(R.id.math_entity_description);
itemView.removeView(description);
result.addView(description);
}
description.setText(mathEntityDescription);
} else { } else {
TextView description = (TextView) result.findViewById(R.id.math_entity_description); result = (ViewGroup) convertView;
if (description != null) { fillView(position, result);
result.removeView(description);
}
} }
return result; return result;
} }
private void fillView(int position, @NotNull ViewGroup result) {
final T mathEntity = getItem(position);
final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName());
if (!StringUtils.isEmpty(mathEntityDescription)) {
final TextView description = (TextView) result.findViewById(R.id.math_entity_description);
description.setVisibility(View.VISIBLE);
description.setText(mathEntityDescription);
} else {
final TextView description = (TextView) result.findViewById(R.id.math_entity_description);
description.setVisibility(View.GONE);
}
}
} }
protected static class MathEntityDescriptionGetterImpl implements MathEntityDescriptionGetter { protected static class MathEntityDescriptionGetterImpl implements MathEntityDescriptionGetter {

View File

@ -1,234 +1,238 @@
/* /*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev. * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com * For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.calculator.math.edit; package org.solovyev.android.calculator.math.edit;
import android.app.Activity; 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.text.ClipboardManager;
import jscl.math.function.Function; import jscl.math.function.Function;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CalculatorLocatorImpl; import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.menu.LabeledMenuItem; import org.solovyev.android.calculator.R;
import org.solovyev.common.text.StringUtils; import org.solovyev.android.menu.AMenuItem;
import org.solovyev.android.menu.LabeledMenuItem;
import java.util.ArrayList; import org.solovyev.common.text.StringUtils;
import java.util.Arrays;
import java.util.List; import java.util.ArrayList;
import java.util.Arrays;
/** import java.util.List;
* User: serso
* Date: 10/29/11 /**
* Time: 4:55 PM * User: serso
*/ * Date: 10/29/11
public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment<Function> { * Time: 4:55 PM
*/
public static final String CREATE_FUN_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity_create_fun"; public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment<Function> {
@Override public static final String CREATE_FUN_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity_create_fun";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); @Override
public void onCreate(Bundle savedInstanceState) {
/*getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { super.onCreate(savedInstanceState);
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { /*getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
final Function function = (Function) parent.getItemAtPosition(position); @Override
if (function instanceof CustomFunction) { public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
createEditVariableDialog(CalculatorFunctionsTabActivity.this, final Function function = (Function) parent.getItemAtPosition(position);
((CustomFunction) function), if (function instanceof CustomFunction) {
function.getName(), createEditVariableDialog(CalculatorFunctionsTabActivity.this,
((CustomFunction) function).getContent(), ((CustomFunction) function),
((CustomFunction) function).getParameterNames(), function.getName(),
null); ((CustomFunction) function).getContent(),
} ((CustomFunction) function).getParameterNames(),
return true; null);
} }
});*/ return true;
}
/*final Intent intent = getIntent(); });*/
if (intent != null) {
final String varValue = intent.getStringExtra(CREATE_FUN_EXTRA_STRING); /*final Intent intent = getIntent();
if (!StringUtils.isEmpty(varValue)) { if (intent != null) {
createEditVariableDialog(this, null, null, varValue, null, null); final String varValue = intent.getStringExtra(CREATE_FUN_EXTRA_STRING);
if (!StringUtils.isEmpty(varValue)) {
// in order to stop intent for other tabs createEditVariableDialog(this, null, null, varValue, null, null);
intent.removeExtra(CREATE_FUN_EXTRA_STRING);
} // in order to stop intent for other tabs
}*/ intent.removeExtra(CREATE_FUN_EXTRA_STRING);
} }
}*/
@Override }
protected int getTitleResId() {
return R.string.c_functions; @Override
} protected AMenuItem<Function> getOnClickAction() {
return LongClickMenuItem.use;
@NotNull }
@Override
protected List<LabeledMenuItem<Function>> getMenuItemsOnLongClick(@NotNull Function item) { @Override
List<LabeledMenuItem<Function>> result = new ArrayList<LabeledMenuItem<Function>>(Arrays.asList(LongClickMenuItem.values())); protected int getTitleResId() {
return R.string.c_functions;
if ( StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(item.getName())) ) { }
result.remove(LongClickMenuItem.copy_description);
} @NotNull
@Override
return result; protected List<LabeledMenuItem<Function>> getMenuItemsOnLongClick(@NotNull Function item) {
} List<LabeledMenuItem<Function>> result = new ArrayList<LabeledMenuItem<Function>>(Arrays.asList(LongClickMenuItem.values()));
/* private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity<Function> activity, if ( StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(item.getName())) ) {
@Nullable final CustomFunction function, result.remove(LongClickMenuItem.copy_description);
@Nullable final String name, }
@Nullable final String expression,
@Nullable final String[] parameterNames, return result;
@Nullable final String description) { }
if (function == null || !function.isSystem()) {
/* private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity<Function> activity,
final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE); @Nullable final CustomFunction function,
final View editView = layoutInflater.inflate(R.layout.var_edit, null); @Nullable final String name,
@Nullable final String expression,
final String errorMsg = activity.getString(R.string.c_char_is_not_accepted); @Nullable final String[] parameterNames,
@Nullable final String description) {
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name); if (function == null || !function.isSystem()) {
editName.setText(name);
editName.addTextChangedListener(new TextWatcher() { final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.var_edit, null);
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { final String errorMsg = activity.getString(R.string.c_char_is_not_accepted);
}
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
@Override editName.setText(name);
public void onTextChanged(CharSequence s, int start, int before, int count) { editName.addTextChangedListener(new TextWatcher() {
}
@Override
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
public void afterTextChanged(Editable s) { }
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i); @Override
if (!acceptableChars.contains(c)) { public void onTextChanged(CharSequence s, int start, int before, int count) {
s.delete(i, i + 1); }
Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show();
} @Override
} public void afterTextChanged(Editable s) {
} for (int i = 0; i < s.length(); i++) {
}); char c = s.charAt(i);
if (!acceptableChars.contains(c)) {
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value); s.delete(i, i + 1);
if (!StringUtils.isEmpty(expression)) { Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show();
editValue.setText(expression); }
} }
}
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); });
editDescription.setText(description);
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
final CustomFunction.Builder functionBuilder; if (!StringUtils.isEmpty(expression)) {
if (function != null) { editValue.setText(expression);
functionBuilder = new CustomFunction.Builder(function); }
} else {
functionBuilder = new CustomFunction.Builder(); final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
} editDescription.setText(description);
final AlertDialog.Builder builder = new AlertDialog.Builder(activity) final CustomFunction.Builder functionBuilder;
.setCancelable(true) if (function != null) {
.setNegativeButton(R.string.c_cancel, null) functionBuilder = new CustomFunction.Builder(function);
.setPositiveButton(R.string.c_save, new FunctionEditorSaver(functionBuilder, function, editView, activity, CalculatorEngine.instance.getFunctionsRegistry(), new FunctionEditorSaver.EditorCreator<Function>() { } else {
functionBuilder = new CustomFunction.Builder();
@Override }
public void showEditor(@NotNull AbstractMathEntityListActivity<Function> activity, @Nullable CustomFunction editedInstance, @Nullable String name, @Nullable String value, @Nullable String[] parameterNames, @Nullable String description) {
createEditVariableDialog(activity, editedInstance, name, value, parameterNames, description); final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
} .setCancelable(true)
})) .setNegativeButton(R.string.c_cancel, null)
.setView(editView); .setPositiveButton(R.string.c_save, new FunctionEditorSaver(functionBuilder, function, editView, activity, CalculatorEngine.instance.getFunctionsRegistry(), new FunctionEditorSaver.EditorCreator<Function>() {
if (function != null) { @Override
// EDIT mode public void showEditor(@NotNull AbstractMathEntityListActivity<Function> activity, @Nullable CustomFunction editedInstance, @Nullable String name, @Nullable String value, @Nullable String[] parameterNames, @Nullable String description) {
createEditVariableDialog(activity, editedInstance, name, value, parameterNames, description);
builder.setTitle(R.string.c_var_edit_var); }
builder.setNeutralButton(R.string.c_remove, new MathEntityRemover<Function>(function, new DialogInterface.OnClickListener() { }))
@Override .setView(editView);
public void onClick(DialogInterface dialog, int which) {
createEditVariableDialog(activity, function, name, expression, parameterNames, description); if (function != null) {
} // EDIT mode
}, CalculatorEngine.instance.getFunctionsRegistry(), activity));
} else { builder.setTitle(R.string.c_var_edit_var);
// CREATE mode builder.setNeutralButton(R.string.c_remove, new MathEntityRemover<Function>(function, new DialogInterface.OnClickListener() {
@Override
builder.setTitle(R.string.c_var_create_var); public void onClick(DialogInterface dialog, int which) {
} createEditVariableDialog(activity, function, name, expression, parameterNames, description);
}
builder.create().show(); }, CalculatorEngine.instance.getFunctionsRegistry(), activity));
} else { } else {
Toast.makeText(activity, activity.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show(); // CREATE mode
}
}*/ builder.setTitle(R.string.c_var_create_var);
}
@NotNull
@Override builder.create().show();
protected MathEntityDescriptionGetter getDescriptionGetter() { } else {
return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry()); Toast.makeText(activity, activity.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show();
} }
}*/
@NotNull
@Override @NotNull
protected List<Function> getMathEntities() { @Override
return new ArrayList<Function>(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getEntities()); protected MathEntityDescriptionGetter getDescriptionGetter() {
} return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry());
}
@Override
protected String getMathEntityCategory(@NotNull Function function) { @NotNull
return CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getCategory(function); @Override
} protected List<Function> getMathEntities() {
return new ArrayList<Function>(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getEntities());
/* }
**********************************************************************
* @Override
* STATIC protected String getMathEntityCategory(@NotNull Function function) {
* return CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getCategory(function);
********************************************************************** }
*/
/*
private static enum LongClickMenuItem implements LabeledMenuItem<Function> { **********************************************************************
use(R.string.c_use) { *
@Override * STATIC
public void onClick(@NotNull Function data, @NotNull Context context) { *
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName()); **********************************************************************
if (context instanceof Activity) { */
((Activity) context).finish();
} private static enum LongClickMenuItem implements LabeledMenuItem<Function> {
} use(R.string.c_use) {
}, @Override
public void onClick(@NotNull Function data, @NotNull Context context) {
/*edit(R.string.c_edit) { CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_function, data);
@Override }
public void doAction(@NotNull Function data, @NotNull Context context) { },
if (context instanceof AbstractMathEntityListActivity) {
} /*edit(R.string.c_edit) {
} @Override
},*/ public void doAction(@NotNull Function data, @NotNull Context context) {
if (context instanceof AbstractMathEntityListActivity) {
copy_description(R.string.c_copy_description) { }
@Override }
public void onClick(@NotNull Function data, @NotNull Context context) { },*/
final String text = CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(data.getName());
if (!StringUtils.isEmpty(text)) { copy_description(R.string.c_copy_description) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); @Override
clipboard.setText(text); public void onClick(@NotNull Function data, @NotNull Context context) {
} final String text = CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(data.getName());
} if (!StringUtils.isEmpty(text)) {
}; final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
private final int captionId; clipboard.setText(text);
}
LongClickMenuItem(int captionId) { }
this.captionId = captionId; };
} private final int captionId;
@NotNull LongClickMenuItem(int captionId) {
@Override this.captionId = captionId;
public String getCaption(@NotNull Context context) { }
return context.getString(captionId);
} @NotNull
} @Override
} public String getCaption(@NotNull Context context) {
return context.getString(captionId);
}
}
}

View File

@ -58,6 +58,10 @@ public class CalculatorFunctionsFragmentActivity extends SherlockFragmentActivit
@Override @Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
//To change body of implemented methods use File | Settings | File Templates. switch (calculatorEventType) {
case use_function:
this.finish();
break;
}
} }
} }

View File

@ -1,129 +1,133 @@
package org.solovyev.android.calculator.math.edit; package org.solovyev.android.calculator.math.edit;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.text.ClipboardManager; import android.text.ClipboardManager;
import jscl.math.operator.Operator; import jscl.math.operator.Operator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CalculatorLocatorImpl; import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.menu.LabeledMenuItem; import org.solovyev.android.calculator.R;
import org.solovyev.common.text.StringUtils; import org.solovyev.android.menu.AMenuItem;
import org.solovyev.android.menu.LabeledMenuItem;
import java.util.ArrayList; import org.solovyev.common.text.StringUtils;
import java.util.Arrays;
import java.util.List; import java.util.ArrayList;
import java.util.Arrays;
/** import java.util.List;
* User: serso
* Date: 11/17/11 /**
* Time: 1:53 PM * User: serso
*/ * Date: 11/17/11
* Time: 1:53 PM
public class CalculatorOperatorsFragment extends AbstractMathEntityListFragment<Operator> { */
@Override public class CalculatorOperatorsFragment extends AbstractMathEntityListFragment<Operator> {
protected int getTitleResId() {
return R.string.c_operators; @Override
} protected AMenuItem<Operator> getOnClickAction() {
return LongClickMenuItem.use;
@NotNull }
@Override
protected List<LabeledMenuItem<Operator>> getMenuItemsOnLongClick(@NotNull Operator item) { @Override
final List<LabeledMenuItem<Operator>> result = new ArrayList<LabeledMenuItem<Operator>>(Arrays.asList(LongClickMenuItem.values())); protected int getTitleResId() {
return R.string.c_operators;
if ( StringUtils.isEmpty(OperatorDescriptionGetter.instance.getDescription(this.getActivity(), item.getName())) ) { }
result.remove(LongClickMenuItem.copy_description);
} @NotNull
@Override
return result; protected List<LabeledMenuItem<Operator>> getMenuItemsOnLongClick(@NotNull Operator item) {
} final List<LabeledMenuItem<Operator>> result = new ArrayList<LabeledMenuItem<Operator>>(Arrays.asList(LongClickMenuItem.values()));
@NotNull if ( StringUtils.isEmpty(OperatorDescriptionGetter.instance.getDescription(this.getActivity(), item.getName())) ) {
@Override result.remove(LongClickMenuItem.copy_description);
protected MathEntityDescriptionGetter getDescriptionGetter() { }
return OperatorDescriptionGetter.instance;
} return result;
}
@NotNull @NotNull
@Override @Override
protected List<Operator> getMathEntities() { protected MathEntityDescriptionGetter getDescriptionGetter() {
final List<Operator> result = new ArrayList<Operator>(); return OperatorDescriptionGetter.instance;
}
result.addAll(CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getEntities());
result.addAll(CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getEntities());
@NotNull
return result; @Override
} protected List<Operator> getMathEntities() {
final List<Operator> result = new ArrayList<Operator>();
@Override
protected String getMathEntityCategory(@NotNull Operator operator) { result.addAll(CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getEntities());
String result = CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getCategory(operator); result.addAll(CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getEntities());
if (result == null) {
result = CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getCategory(operator); return result;
} }
return result; @Override
} protected String getMathEntityCategory(@NotNull Operator operator) {
String result = CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getCategory(operator);
private static enum OperatorDescriptionGetter implements MathEntityDescriptionGetter { if (result == null) {
result = CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getCategory(operator);
instance; }
@Override return result;
public String getDescription(@NotNull Context context, @NotNull String mathEntityName) { }
String result = CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getDescription(mathEntityName);
if (StringUtils.isEmpty(result)) { private static enum OperatorDescriptionGetter implements MathEntityDescriptionGetter {
result = CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getDescription(mathEntityName);
} instance;
return result; @Override
} public String getDescription(@NotNull Context context, @NotNull String mathEntityName) {
} String result = CalculatorLocatorImpl.getInstance().getEngine().getOperatorsRegistry().getDescription(mathEntityName);
if (StringUtils.isEmpty(result)) {
/* result = CalculatorLocatorImpl.getInstance().getEngine().getPostfixFunctionsRegistry().getDescription(mathEntityName);
********************************************************************** }
*
* STATIC return result;
* }
********************************************************************** }
*/
/*
private static enum LongClickMenuItem implements LabeledMenuItem<Operator> { **********************************************************************
*
use(R.string.c_use) { * STATIC
@Override *
public void onClick(@NotNull Operator data, @NotNull Context context) { **********************************************************************
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName()); */
if (context instanceof Activity) {
((Activity) context).finish(); private static enum LongClickMenuItem implements LabeledMenuItem<Operator> {
}
} use(R.string.c_use) {
}, @Override
public void onClick(@NotNull Operator data, @NotNull Context context) {
copy_description(R.string.c_copy_description) { CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_operator, data);
@Override }
public void onClick(@NotNull Operator data, @NotNull Context context) { },
final String text = OperatorDescriptionGetter.instance.getDescription(context, data.getName());
if (!StringUtils.isEmpty(text)) { copy_description(R.string.c_copy_description) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); @Override
clipboard.setText(text); public void onClick(@NotNull Operator data, @NotNull Context context) {
} final String text = OperatorDescriptionGetter.instance.getDescription(context, data.getName());
} if (!StringUtils.isEmpty(text)) {
}; final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
private final int captionId; clipboard.setText(text);
}
LongClickMenuItem(int captionId) { }
this.captionId = captionId; };
} private final int captionId;
@NotNull LongClickMenuItem(int captionId) {
@Override this.captionId = captionId;
public String getCaption(@NotNull Context context) { }
return context.getString(captionId);
} @NotNull
} @Override
public String getCaption(@NotNull Context context) {
} return context.getString(captionId);
}
}
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.math.edit;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity;
/**
* User: serso
* Date: 12/21/11
* Time: 10:33 PM
*/
public class CalculatorOperatorsFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@NotNull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName());
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState);
activityHelper.addTab(this, "operators", CalculatorOperatorsFragment.class, null, R.string.c_operators, R.id.main_layout);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState);
}
@Override
protected void onResume() {
super.onResume();
activityHelper.onResume(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
this.activityHelper.onDestroy(this);
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
switch (calculatorEventType) {
case use_operator:
this.finish();
break;
}
}
}

View File

@ -1,320 +1,324 @@
/* /*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev. * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com * For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.calculator.math.edit; package org.solovyev.android.calculator.math.edit;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.text.ClipboardManager; import android.text.ClipboardManager;
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.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
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.CalculatorLocatorImpl; import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.menu.LabeledMenuItem; import org.solovyev.android.calculator.model.Var;
import org.solovyev.common.JPredicate; import org.solovyev.android.menu.AMenuItem;
import org.solovyev.common.collections.CollectionsUtils; import org.solovyev.android.menu.LabeledMenuItem;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.JPredicate;
import org.solovyev.common.collections.CollectionsUtils;
import java.util.ArrayList; import org.solovyev.common.text.StringUtils;
import java.util.Arrays;
import java.util.List; import java.util.ArrayList;
import java.util.Arrays;
/** import java.util.List;
* User: serso
* Date: 9/28/11 /**
* Time: 10:55 PM * User: serso
*/ * Date: 9/28/11
public class CalculatorVarsFragment extends AbstractMathEntityListFragment<IConstant> { * Time: 10:55 PM
*/
public static final String CREATE_VAR_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity_create_var"; public class CalculatorVarsFragment extends AbstractMathEntityListFragment<IConstant> {
@Override public static final String CREATE_VAR_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity_create_var";
protected int getLayoutId() {
return R.layout.vars_fragment; @Override
} protected int getLayoutId() {
return R.layout.vars_fragment;
@Override }
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); @Override
public void onCreate(Bundle savedInstanceState) {
final Bundle bundle = getArguments(); super.onCreate(savedInstanceState);
if (bundle != null) {
final String varValue = bundle.getString(CREATE_VAR_EXTRA_STRING); final Bundle bundle = getArguments();
if (!StringUtils.isEmpty(varValue)) { if (bundle != null) {
createEditVariableDialog(this, null, null, varValue, null); final String varValue = bundle.getString(CREATE_VAR_EXTRA_STRING);
if (!StringUtils.isEmpty(varValue)) {
// in order to stop intent for other tabs createEditVariableDialog(this, null, null, varValue, null);
bundle.remove(CREATE_VAR_EXTRA_STRING);
} // in order to stop intent for other tabs
} bundle.remove(CREATE_VAR_EXTRA_STRING);
}
setHasOptionsMenu(true); }
}
setHasOptionsMenu(true);
@Override }
protected int getTitleResId() {
return R.string.c_vars; @Override
} protected int getTitleResId() {
return R.string.c_vars;
@NotNull }
@Override
protected List<LabeledMenuItem<IConstant>> getMenuItemsOnLongClick(@NotNull IConstant item) { @Override
final List<LabeledMenuItem<IConstant>> result = new ArrayList<LabeledMenuItem<IConstant>>(Arrays.asList(LongClickMenuItem.values())); protected AMenuItem<IConstant> getOnClickAction() {
return LongClickMenuItem.use;
if ( item.isSystem() ) { }
result.remove(LongClickMenuItem.edit);
result.remove(LongClickMenuItem.remove); @NotNull
} @Override
protected List<LabeledMenuItem<IConstant>> getMenuItemsOnLongClick(@NotNull IConstant item) {
if ( StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(item.getName())) ) { final List<LabeledMenuItem<IConstant>> result = new ArrayList<LabeledMenuItem<IConstant>>(Arrays.asList(LongClickMenuItem.values()));
result.remove(LongClickMenuItem.copy_description);
} if ( item.isSystem() ) {
result.remove(LongClickMenuItem.edit);
if ( StringUtils.isEmpty(item.getValue()) ) { result.remove(LongClickMenuItem.remove);
result.remove(LongClickMenuItem.copy_value); }
}
if ( StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(item.getName())) ) {
return result; result.remove(LongClickMenuItem.copy_description);
} }
@NotNull if ( StringUtils.isEmpty(item.getValue()) ) {
@Override result.remove(LongClickMenuItem.copy_value);
protected MathEntityDescriptionGetter getDescriptionGetter() { }
return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry());
} return result;
}
@SuppressWarnings({"UnusedDeclaration"})
public void addVarButtonClickHandler(@NotNull View v) { @NotNull
createEditVariableDialog(this, null, null, null, null); @Override
} protected MathEntityDescriptionGetter getDescriptionGetter() {
return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry());
@NotNull }
@Override
protected List<IConstant> getMathEntities() { @SuppressWarnings({"UnusedDeclaration"})
final List<IConstant> result = new ArrayList<IConstant>(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getEntities()); public void addVarButtonClickHandler(@NotNull View v) {
createEditVariableDialog(this, null, null, null, null);
CollectionsUtils.removeAll(result, new JPredicate<IConstant>() { }
@Override
public boolean apply(@Nullable IConstant var) { @NotNull
return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN); @Override
} protected List<IConstant> getMathEntities() {
}); final List<IConstant> result = new ArrayList<IConstant>(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getEntities());
return result; CollectionsUtils.removeAll(result, new JPredicate<IConstant>() {
} @Override
public boolean apply(@Nullable IConstant var) {
@Override return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN);
protected String getMathEntityCategory(@NotNull IConstant var) { }
return CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getCategory(var); });
}
return result;
private static void createEditVariableDialog(@NotNull final AbstractMathEntityListFragment<IConstant> fragment, }
@Nullable final IConstant var,
@Nullable final String name, @Override
@Nullable final String value, protected String getMathEntityCategory(@NotNull IConstant var) {
@Nullable final String description) { return CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getCategory(var);
final FragmentActivity activity = fragment.getActivity(); }
if (var == null || !var.isSystem()) { private static void createEditVariableDialog(@NotNull final AbstractMathEntityListFragment<IConstant> fragment,
@Nullable final IConstant var,
final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); @Nullable final String name,
final View editView = layoutInflater.inflate(R.layout.var_edit, null); @Nullable final String value,
@Nullable final String description) {
final String errorMsg = fragment.getString(R.string.c_char_is_not_accepted); final FragmentActivity activity = fragment.getActivity();
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name); if (var == null || !var.isSystem()) {
editName.setText(name);
editName.addTextChangedListener(new TextWatcher() { final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.var_edit, null);
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { final String errorMsg = fragment.getString(R.string.c_char_is_not_accepted);
}
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
@Override editName.setText(name);
public void onTextChanged(CharSequence s, int start, int before, int count) { editName.addTextChangedListener(new TextWatcher() {
}
@Override
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
public void afterTextChanged(Editable s) { }
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i); @Override
if (!acceptableChars.contains(c)) { public void onTextChanged(CharSequence s, int start, int before, int count) {
s.delete(i, i + 1); }
Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show();
} @Override
} public void afterTextChanged(Editable s) {
} for (int i = 0; i < s.length(); i++) {
}); char c = s.charAt(i);
if (!acceptableChars.contains(c)) {
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value); s.delete(i, i + 1);
if (!StringUtils.isEmpty(value)) { Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show();
editValue.setText(value); }
} }
}
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); });
editDescription.setText(description);
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
final Var.Builder varBuilder; if (!StringUtils.isEmpty(value)) {
if (var != null) { editValue.setText(value);
varBuilder = new Var.Builder(var); }
} else {
varBuilder = new Var.Builder(); final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
} editDescription.setText(description);
final AlertDialog.Builder builder = new AlertDialog.Builder(activity) final Var.Builder varBuilder;
.setCancelable(true) if (var != null) {
.setNegativeButton(R.string.c_cancel, null) varBuilder = new Var.Builder(var);
.setPositiveButton(R.string.c_save, new VarEditorSaver<IConstant>(varBuilder, var, editView, fragment, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), new VarEditorSaver.EditorCreator<IConstant>() { } else {
@Override varBuilder = new Var.Builder();
public void showEditor(@NotNull AbstractMathEntityListFragment<IConstant> activity, @Nullable IConstant editedInstance, @Nullable String name, @Nullable String value, @Nullable String description) { }
createEditVariableDialog(activity, editedInstance, name, value, description);
} final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
})) .setCancelable(true)
.setView(editView); .setNegativeButton(R.string.c_cancel, null)
.setPositiveButton(R.string.c_save, new VarEditorSaver<IConstant>(varBuilder, var, editView, fragment, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), new VarEditorSaver.EditorCreator<IConstant>() {
if (var != null) { @Override
// EDIT mode public void showEditor(@NotNull AbstractMathEntityListFragment<IConstant> activity, @Nullable IConstant editedInstance, @Nullable String name, @Nullable String value, @Nullable String description) {
createEditVariableDialog(activity, editedInstance, name, value, description);
builder.setTitle(R.string.c_var_edit_var); }
builder.setNeutralButton(R.string.c_remove, new MathEntityRemover<IConstant>(var, new DialogInterface.OnClickListener() { }))
@Override .setView(editView);
public void onClick(DialogInterface dialog, int which) {
createEditVariableDialog(fragment, var, name, value, description); if (var != null) {
} // EDIT mode
}, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), fragment));
} else { builder.setTitle(R.string.c_var_edit_var);
// CREATE mode builder.setNeutralButton(R.string.c_remove, new MathEntityRemover<IConstant>(var, new DialogInterface.OnClickListener() {
@Override
builder.setTitle(R.string.c_var_create_var); public void onClick(DialogInterface dialog, int which) {
} createEditVariableDialog(fragment, var, name, value, description);
}
builder.create().show(); }, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), fragment));
} else { } else {
Toast.makeText(activity, fragment.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show(); // CREATE mode
}
} builder.setTitle(R.string.c_var_create_var);
}
public static boolean isValidValue(@NotNull String value) {
// now every string might be constant builder.create().show();
return true; } else {
} Toast.makeText(activity, fragment.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show();
}
/* }
**********************************************************************
* public static boolean isValidValue(@NotNull String value) {
* MENU // now every string might be constant
* return true;
********************************************************************** }
*/
/*
@Override **********************************************************************
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { *
inflater.inflate(R.menu.var_menu, menu); * MENU
} *
**********************************************************************
@Override */
public boolean onOptionsItemSelected(MenuItem item) {
boolean result; @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
switch (item.getItemId()) { inflater.inflate(R.menu.var_menu, menu);
case R.id.var_menu_add_var: }
createEditVariableDialog(this, null, null, null, null);
result = true; @Override
break; public boolean onOptionsItemSelected(MenuItem item) {
default: boolean result;
result = super.onOptionsItemSelected(item);
} switch (item.getItemId()) {
case R.id.var_menu_add_var:
return result; createEditVariableDialog(this, null, null, null, null);
} result = true;
break;
/* default:
********************************************************************** result = super.onOptionsItemSelected(item);
* }
* STATIC
* return result;
********************************************************************** }
*/
/*
private static enum LongClickMenuItem implements LabeledMenuItem<IConstant>{ **********************************************************************
use(R.string.c_use) { *
@Override * STATIC
public void onClick(@NotNull IConstant data, @NotNull Context context) { *
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName()); **********************************************************************
if (context instanceof Activity) { */
((Activity) context).finish();
} private static enum LongClickMenuItem implements LabeledMenuItem<IConstant>{
} use(R.string.c_use) {
}, @Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
edit(R.string.c_edit) { CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_constant, data);
@Override }
public void onClick(@NotNull IConstant data, @NotNull Context context) { },
/*if (context instanceof AbstractMathEntityListFragment) {
createEditVariableDialog((AbstractMathEntityListFragment<IConstant>)context, data, data.getName(), StringUtils.getNotEmpty(data.getValue(), ""), data.getDescription()); edit(R.string.c_edit) {
}*/ @Override
} public void onClick(@NotNull IConstant data, @NotNull Context context) {
}, /*if (context instanceof AbstractMathEntityListFragment) {
createEditVariableDialog((AbstractMathEntityListFragment<IConstant>)context, data, data.getName(), StringUtils.getNotEmpty(data.getValue(), ""), data.getDescription());
remove(R.string.c_remove) { }*/
@Override }
public void onClick(@NotNull IConstant data, @NotNull Context context) { },
/*if (context instanceof AbstractMathEntityListFragment) {
new MathEntityRemover<IConstant>(data, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), ((AbstractMathEntityListFragment<IConstant>) context)).showConfirmationDialog(); remove(R.string.c_remove) {
}*/ @Override
} public void onClick(@NotNull IConstant data, @NotNull Context context) {
}, /*if (context instanceof AbstractMathEntityListFragment) {
new MathEntityRemover<IConstant>(data, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), ((AbstractMathEntityListFragment<IConstant>) context)).showConfirmationDialog();
copy_value(R.string.c_copy_value) { }*/
@Override }
public void onClick(@NotNull IConstant data, @NotNull Context context) { },
final String text = data.getValue();
if (!StringUtils.isEmpty(text)) { copy_value(R.string.c_copy_value) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); @Override
clipboard.setText(text); public void onClick(@NotNull IConstant data, @NotNull Context context) {
} final String text = data.getValue();
} if (!StringUtils.isEmpty(text)) {
}, final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
copy_description(R.string.c_copy_description) { }
@Override }
public void onClick(@NotNull IConstant data, @NotNull Context context) { },
final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName());
if (!StringUtils.isEmpty(text)) { copy_description(R.string.c_copy_description) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); @Override
clipboard.setText(text); public void onClick(@NotNull IConstant data, @NotNull Context context) {
} final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName());
} if (!StringUtils.isEmpty(text)) {
}; final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
private final int captionId; clipboard.setText(text);
}
LongClickMenuItem(int captionId) { }
this.captionId = captionId; };
} private final int captionId;
@NotNull LongClickMenuItem(int captionId) {
@Override this.captionId = captionId;
public String getCaption(@NotNull Context context) { }
return context.getString(captionId);
} @NotNull
} @Override
} public String getCaption(@NotNull Context context) {
return context.getString(captionId);
}
}
}

View File

@ -80,6 +80,10 @@ public class CalculatorVarsFragmentActivity extends SherlockFragmentActivity imp
@Override @Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
//To change body of implemented methods use File | Settings | File Templates. switch (calculatorEventType) {
case use_constant:
this.finish();
break;
}
} }
} }