This commit is contained in:
serso 2012-10-03 10:58:43 +04:00
parent 604c61cf29
commit 0b100efcf0
4 changed files with 623 additions and 588 deletions

View File

@ -0,0 +1,35 @@
package org.solovyev.android;
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 org.jetbrains.annotations.NotNull;
/**
* User: Solovyev_S
* Date: 03.10.12
* Time: 10:48
*/
public final class AndroidUtils2 {
private AndroidUtils2() {
throw new AssertionError();
}
public static void showDialog(@NotNull DialogFragment dialogFragment,
@NotNull String fragmentTag,
@NotNull FragmentManager fm) {
final FragmentTransaction ft = fm.beginTransaction();
Fragment prev = fm.findFragmentByTag(fragmentTag);
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
dialogFragment.show(ft, fragmentTag);
}
}

View File

@ -1,85 +1,86 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import com.actionbarsherlock.app.SherlockFragmentActivity; 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;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.about.CalculatorAboutTabActivity; import org.solovyev.android.AndroidUtils2;
import org.solovyev.android.calculator.help.CalculatorHelpTabActivity; import org.solovyev.android.calculator.about.CalculatorAboutTabActivity;
import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity; import org.solovyev.android.calculator.help.CalculatorHelpTabActivity;
import org.solovyev.android.calculator.math.edit.*; import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity;
import org.solovyev.android.calculator.plot.CalculatorPlotActivity; import org.solovyev.android.calculator.math.edit.*;
import org.solovyev.android.calculator.plot.CalculatorPlotFragment; import org.solovyev.android.calculator.plot.CalculatorPlotActivity;
import org.solovyev.common.msg.MessageType; import org.solovyev.android.calculator.plot.CalculatorPlotFragment;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.StringUtils;
/**
* User: serso /**
* Date: 11/2/11 * User: serso
* Time: 2:18 PM * Date: 11/2/11
*/ * Time: 2:18 PM
public class CalculatorActivityLauncher { */
public class CalculatorActivityLauncher {
public static void showHistory(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorHistoryFragmentActivity.class)); public static void showHistory(@NotNull final Context context) {
} context.startActivity(new Intent(context, CalculatorHistoryFragmentActivity.class));
}
public static void showHelp(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorHelpTabActivity.class)); public static void showHelp(@NotNull final Context context) {
} context.startActivity(new Intent(context, CalculatorHelpTabActivity.class));
}
public static void showSettings(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorPreferencesActivity.class)); public static void showSettings(@NotNull final Context context) {
} context.startActivity(new Intent(context, CalculatorPreferencesActivity.class));
}
public static void showAbout(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorAboutTabActivity.class)); public static void showAbout(@NotNull final Context context) {
} context.startActivity(new Intent(context, CalculatorAboutTabActivity.class));
}
public static void showFunctions(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorFunctionsFragmentActivity.class)); public static void showFunctions(@NotNull final Context context) {
} context.startActivity(new Intent(context, CalculatorFunctionsFragmentActivity.class));
}
public static void showOperators(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorOperatorsFragmentActivity.class)); public static void showOperators(@NotNull final Context context) {
} context.startActivity(new Intent(context, CalculatorOperatorsFragmentActivity.class));
}
public static void showVars(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorVarsFragmentActivity.class)); public static void showVars(@NotNull final Context context) {
} context.startActivity(new Intent(context, CalculatorVarsFragmentActivity.class));
}
public static void plotGraph(@NotNull final Context context, @NotNull Generic generic, @NotNull Constant constant){
final Intent intent = new Intent(); public static void plotGraph(@NotNull final Context context, @NotNull Generic generic, @NotNull Constant constant){
intent.putExtra(ChartFactory.TITLE, context.getString(R.string.c_graph)); final Intent intent = new Intent();
intent.putExtra(CalculatorPlotFragment.INPUT, new CalculatorPlotFragment.Input(generic.toString(), constant.getName())); intent.putExtra(ChartFactory.TITLE, context.getString(R.string.c_graph));
intent.setClass(context, CalculatorPlotActivity.class); intent.putExtra(CalculatorPlotFragment.INPUT, new CalculatorPlotFragment.Input(generic.toString(), constant.getName()));
context.startActivity(intent); intent.setClass(context, CalculatorPlotActivity.class);
} context.startActivity(intent);
}
public static void createVar(@NotNull final Context context, @NotNull CalculatorDisplay calculatorDisplay) {
final CalculatorDisplayViewState viewState = calculatorDisplay.getViewState(); public static void createVar(@NotNull final Context context, @NotNull CalculatorDisplay calculatorDisplay) {
if (viewState.isValid() ) { final CalculatorDisplayViewState viewState = calculatorDisplay.getViewState();
final String varValue = viewState.getText(); if (viewState.isValid() ) {
if (!StringUtils.isEmpty(varValue)) { final String varValue = viewState.getText();
if (CalculatorVarsFragment.isValidValue(varValue)) { if (!StringUtils.isEmpty(varValue)) {
if (context instanceof SherlockFragmentActivity) { if (CalculatorVarsFragment.isValidValue(varValue)) {
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newFromValue(varValue), ((SherlockFragmentActivity) context).getSupportFragmentManager()); if (context instanceof SherlockFragmentActivity) {
} else { VarEditDialogFragment.showDialog(VarEditDialogFragment.Input.newFromValue(varValue), ((SherlockFragmentActivity) context).getSupportFragmentManager());
final Intent intent = new Intent(context, CalculatorVarsFragmentActivity.class); } else {
intent.putExtra(CalculatorVarsFragment.CREATE_VAR_EXTRA_STRING, varValue); final Intent intent = new Intent(context, CalculatorVarsFragmentActivity.class);
context.startActivity(intent); intent.putExtra(CalculatorVarsFragment.CREATE_VAR_EXTRA_STRING, varValue);
} context.startActivity(intent);
} else { }
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(R.string.c_not_valid_result, MessageType.error); } else {
} CalculatorLocatorImpl.getInstance().getNotifier().showMessage(R.string.c_not_valid_result, MessageType.error);
} else { }
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(R.string.c_empty_var_error, MessageType.error); } else {
} 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); } else {
} CalculatorLocatorImpl.getInstance().getNotifier().showMessage(R.string.c_not_valid_result, MessageType.error);
} }
} }
}

View File

@ -1,279 +1,280 @@
/* /*
* 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.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import com.actionbarsherlock.app.SherlockFragmentActivity; 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.*; import org.solovyev.android.AndroidUtils2;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.*;
import org.solovyev.android.menu.AMenuItem; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.menu.LabeledMenuItem; import org.solovyev.android.menu.AMenuItem;
import org.solovyev.common.JPredicate; import org.solovyev.android.menu.LabeledMenuItem;
import org.solovyev.common.collections.CollectionsUtils; import org.solovyev.common.JPredicate;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.collections.CollectionsUtils;
import org.solovyev.common.text.StringUtils;
import java.util.ArrayList;
import java.util.Arrays; import java.util.ArrayList;
import java.util.List; import java.util.Arrays;
import java.util.List;
/**
* User: serso /**
* Date: 9/28/11 * User: serso
* Time: 10:55 PM * Date: 9/28/11
*/ * Time: 10:55 PM
public class CalculatorVarsFragment extends AbstractMathEntityListFragment<IConstant> { */
public class CalculatorVarsFragment extends AbstractMathEntityListFragment<IConstant> {
public static final String CREATE_VAR_EXTRA_STRING = "create_var";
public static final String CREATE_VAR_EXTRA_STRING = "create_var";
@Override
protected int getLayoutId() { @Override
return R.layout.vars_fragment; protected int getLayoutId() {
} return R.layout.vars_fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) { @Override
super.onCreate(savedInstanceState); public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Bundle bundle = getArguments();
if (bundle != null) { final Bundle bundle = getArguments();
final String varValue = bundle.getString(CREATE_VAR_EXTRA_STRING); if (bundle != null) {
if (!StringUtils.isEmpty(varValue)) { final String varValue = bundle.getString(CREATE_VAR_EXTRA_STRING);
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newFromValue(varValue), this.getActivity().getSupportFragmentManager()); if (!StringUtils.isEmpty(varValue)) {
VarEditDialogFragment.showDialog(VarEditDialogFragment.Input.newFromValue(varValue), this.getActivity().getSupportFragmentManager());
// in order to stop intent for other tabs
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() { @Override
return R.string.c_vars; protected int getTitleResId() {
} return R.string.c_vars;
}
@Override
protected AMenuItem<IConstant> getOnClickAction() { @Override
return LongClickMenuItem.use; protected AMenuItem<IConstant> getOnClickAction() {
} return LongClickMenuItem.use;
}
@NotNull
@Override @NotNull
protected List<LabeledMenuItem<IConstant>> getMenuItemsOnLongClick(@NotNull IConstant item) { @Override
final List<LabeledMenuItem<IConstant>> result = new ArrayList<LabeledMenuItem<IConstant>>(Arrays.asList(LongClickMenuItem.values())); protected List<LabeledMenuItem<IConstant>> getMenuItemsOnLongClick(@NotNull IConstant item) {
final List<LabeledMenuItem<IConstant>> result = new ArrayList<LabeledMenuItem<IConstant>>(Arrays.asList(LongClickMenuItem.values()));
if (item.isSystem()) {
result.remove(LongClickMenuItem.edit); if (item.isSystem()) {
result.remove(LongClickMenuItem.remove); result.remove(LongClickMenuItem.edit);
} result.remove(LongClickMenuItem.remove);
}
if (StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(item.getName()))) {
result.remove(LongClickMenuItem.copy_description); if (StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(item.getName()))) {
} result.remove(LongClickMenuItem.copy_description);
}
if (StringUtils.isEmpty(item.getValue())) {
result.remove(LongClickMenuItem.copy_value); if (StringUtils.isEmpty(item.getValue())) {
} result.remove(LongClickMenuItem.copy_value);
}
return result;
} return result;
}
@NotNull
@Override @NotNull
protected MathEntityDescriptionGetter getDescriptionGetter() { @Override
return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry()); protected MathEntityDescriptionGetter getDescriptionGetter() {
} return new MathEntityDescriptionGetterImpl(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry());
}
@SuppressWarnings({"UnusedDeclaration"})
public void addVarButtonClickHandler(@NotNull View v) { @SuppressWarnings({"UnusedDeclaration"})
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newInstance(), this.getActivity().getSupportFragmentManager()); public void addVarButtonClickHandler(@NotNull View v) {
} VarEditDialogFragment.showDialog(VarEditDialogFragment.Input.newInstance(), this.getActivity().getSupportFragmentManager());
}
@NotNull
@Override @NotNull
protected List<IConstant> getMathEntities() { @Override
final List<IConstant> result = new ArrayList<IConstant>(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getEntities()); protected List<IConstant> getMathEntities() {
final List<IConstant> result = new ArrayList<IConstant>(CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getEntities());
CollectionsUtils.removeAll(result, new JPredicate<IConstant>() {
@Override CollectionsUtils.removeAll(result, new JPredicate<IConstant>() {
public boolean apply(@Nullable IConstant var) { @Override
return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN); public boolean apply(@Nullable IConstant var) {
} return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN);
}); }
});
return result;
} return result;
}
@Override
protected String getMathEntityCategory(@NotNull IConstant var) { @Override
return CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getCategory(var); protected String getMathEntityCategory(@NotNull IConstant var) {
} return CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getCategory(var);
}
public static boolean isValidValue(@NotNull String value) {
// now every string might be constant public static boolean isValidValue(@NotNull String value) {
return true; // now every string might be constant
} return true;
}
/*
********************************************************************** /*
* **********************************************************************
* MENU *
* * MENU
********************************************************************** *
*/ **********************************************************************
*/
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { @Override
inflater.inflate(R.menu.var_menu, menu); public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
} inflater.inflate(R.menu.var_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { @Override
boolean result; public boolean onOptionsItemSelected(MenuItem item) {
boolean result;
switch (item.getItemId()) {
case R.id.var_menu_add_var: switch (item.getItemId()) {
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newInstance(), this.getActivity().getSupportFragmentManager()); case R.id.var_menu_add_var:
result = true; VarEditDialogFragment.showDialog(VarEditDialogFragment.Input.newInstance(), this.getActivity().getSupportFragmentManager());
break; result = true;
default: break;
result = super.onOptionsItemSelected(item); default:
} result = super.onOptionsItemSelected(item);
}
return result;
} return result;
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { @Override
super.onCalculatorEvent(calculatorEventData, calculatorEventType, data); public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
super.onCalculatorEvent(calculatorEventData, calculatorEventType, data);
switch (calculatorEventType) {
case constant_added: switch (calculatorEventType) {
processConstantAdded((IConstant) data); case constant_added:
break; processConstantAdded((IConstant) data);
break;
case constant_changed:
processConstantChanged((Change<IConstant>) data); case constant_changed:
break; processConstantChanged((Change<IConstant>) data);
break;
case constant_removed:
processConstantRemoved((IConstant) data); case constant_removed:
break; processConstantRemoved((IConstant) data);
} break;
} }
}
private void processConstantRemoved(@NotNull final IConstant constant) {
if (this.isInCategory(constant)) { private void processConstantRemoved(@NotNull final IConstant constant) {
getUiHandler().post(new Runnable() { if (this.isInCategory(constant)) {
@Override getUiHandler().post(new Runnable() {
public void run() { @Override
removeFromAdapter(constant); public void run() {
notifyAdapter(); removeFromAdapter(constant);
} notifyAdapter();
}); }
} });
} }
}
private void processConstantChanged(@NotNull final Change<IConstant> change) {
final IConstant newConstant = change.getNewValue(); private void processConstantChanged(@NotNull final Change<IConstant> change) {
if (this.isInCategory(newConstant)) { final IConstant newConstant = change.getNewValue();
getUiHandler().post(new Runnable() { if (this.isInCategory(newConstant)) {
@Override getUiHandler().post(new Runnable() {
public void run() { @Override
removeFromAdapter(change.getOldValue()); public void run() {
addToAdapter(newConstant); removeFromAdapter(change.getOldValue());
sort(); addToAdapter(newConstant);
} sort();
}); }
} });
} }
}
private void processConstantAdded(@NotNull final IConstant constant) {
if (this.isInCategory(constant)) { private void processConstantAdded(@NotNull final IConstant constant) {
getUiHandler().post(new Runnable() { if (this.isInCategory(constant)) {
@Override getUiHandler().post(new Runnable() {
public void run() { @Override
addToAdapter(constant); public void run() {
sort(); addToAdapter(constant);
} sort();
}); }
} });
} }
}
/*
********************************************************************** /*
* **********************************************************************
* STATIC *
* * STATIC
********************************************************************** *
*/ **********************************************************************
*/
private static enum LongClickMenuItem implements LabeledMenuItem<IConstant> {
use(R.string.c_use) { private static enum LongClickMenuItem implements LabeledMenuItem<IConstant> {
@Override use(R.string.c_use) {
public void onClick(@NotNull IConstant data, @NotNull Context context) { @Override
CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_constant, data); public void onClick(@NotNull IConstant data, @NotNull Context context) {
} CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_constant, data);
}, }
},
edit(R.string.c_edit) {
@Override edit(R.string.c_edit) {
public void onClick(@NotNull IConstant constant, @NotNull Context context) { @Override
VarEditDialogFragment.createEditVariableDialog(VarEditDialogFragment.Input.newFromConstant(constant), ((SherlockFragmentActivity) context).getSupportFragmentManager()); public void onClick(@NotNull IConstant constant, @NotNull Context context) {
} VarEditDialogFragment.showDialog(VarEditDialogFragment.Input.newFromConstant(constant), ((SherlockFragmentActivity) context).getSupportFragmentManager());
}, }
},
remove(R.string.c_remove) {
@Override remove(R.string.c_remove) {
public void onClick(@NotNull IConstant constant, @NotNull Context context) { @Override
new MathEntityRemover<IConstant>(constant, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), context, context).showConfirmationDialog(); public void onClick(@NotNull IConstant constant, @NotNull Context context) {
} new MathEntityRemover<IConstant>(constant, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), context, context).showConfirmationDialog();
}, }
},
copy_value(R.string.c_copy_value) {
@Override copy_value(R.string.c_copy_value) {
public void onClick(@NotNull IConstant data, @NotNull Context context) { @Override
final String text = data.getValue(); public void onClick(@NotNull IConstant data, @NotNull Context context) {
if (!StringUtils.isEmpty(text)) { final String text = data.getValue();
assert text != null; if (!StringUtils.isEmpty(text)) {
CalculatorLocatorImpl.getInstance().getClipboard().setText(text); assert text != null;
} CalculatorLocatorImpl.getInstance().getClipboard().setText(text);
} }
}, }
},
copy_description(R.string.c_copy_description) {
@Override copy_description(R.string.c_copy_description) {
public void onClick(@NotNull IConstant data, @NotNull Context context) { @Override
final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName()); public void onClick(@NotNull IConstant data, @NotNull Context context) {
if (!StringUtils.isEmpty(text)) { final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName());
assert text != null; if (!StringUtils.isEmpty(text)) {
CalculatorLocatorImpl.getInstance().getClipboard().setText(text); assert text != null;
} CalculatorLocatorImpl.getInstance().getClipboard().setText(text);
} }
}; }
private final int captionId; };
private final int captionId;
LongClickMenuItem(int captionId) {
this.captionId = captionId; LongClickMenuItem(int captionId) {
} this.captionId = captionId;
}
@NotNull
@Override @NotNull
public String getCaption(@NotNull Context context) { @Override
return context.getString(captionId); public String getCaption(@NotNull Context context) {
} return context.getString(captionId);
} }
} }
}

View File

@ -1,224 +1,222 @@
package org.solovyev.android.calculator.math.edit; package org.solovyev.android.calculator.math.edit;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; 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.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.*; import org.solovyev.android.AndroidUtils2;
import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.model.Var;
/**
* User: Solovyev_S /**
* Date: 01.10.12 * User: Solovyev_S
* Time: 17:41 * Date: 01.10.12
*/ * Time: 17:41
public class VarEditDialogFragment extends DialogFragment implements CalculatorEventListener { */
public class VarEditDialogFragment extends DialogFragment implements CalculatorEventListener {
@NotNull
private final Input input; @NotNull
private final Input input;
public VarEditDialogFragment() {
this(Input.newInstance()); public VarEditDialogFragment() {
} this(Input.newInstance());
}
public VarEditDialogFragment(@NotNull Input input) {
this.input = input; public VarEditDialogFragment(@NotNull Input input) {
} this.input = input;
}
public static void createEditVariableDialog(@NotNull Input input, @NotNull FragmentManager fm) {
final FragmentTransaction ft = fm.beginTransaction(); @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Fragment prev = fm.findFragmentByTag("constant-editor"); return inflater.inflate(R.layout.var_edit, container, false);
if (prev != null) { }
ft.remove(prev);
} @Override
ft.addToBackStack(null); public void onResume() {
super.onResume();
// Create and show the dialog.
final DialogFragment newFragment = new VarEditDialogFragment(input); CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this);
newFragment.show(ft, "constant-editor"); }
} @Override
public void onPause() {
@Override CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.var_edit, container, false); super.onPause();
} }
@Override @Override
public void onResume() { public void onViewCreated(@NotNull View root, Bundle savedInstanceState) {
super.onResume(); super.onViewCreated(root, savedInstanceState);
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); final String errorMsg = this.getString(R.string.c_char_is_not_accepted);
}
final EditText editName = (EditText) root.findViewById(R.id.var_edit_name);
@Override editName.setText(input.getName());
public void onPause() { editName.addTextChangedListener(new TextWatcher() {
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
@Override
super.onPause(); public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} }
@Override @Override
public void onViewCreated(@NotNull View root, Bundle savedInstanceState) { public void onTextChanged(CharSequence s, int start, int before, int count) {
super.onViewCreated(root, savedInstanceState); }
final String errorMsg = this.getString(R.string.c_char_is_not_accepted); @Override
public void afterTextChanged(Editable s) {
final EditText editName = (EditText) root.findViewById(R.id.var_edit_name); for (int i = 0; i < s.length(); i++) {
editName.setText(input.getName()); char c = s.charAt(i);
editName.addTextChangedListener(new TextWatcher() { if (!AbstractMathEntityListFragment.acceptableChars.contains(c)) {
s.delete(i, i + 1);
@Override Toast.makeText(getActivity(), String.format(errorMsg, c), Toast.LENGTH_SHORT).show();
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
} }
}
@Override });
public void onTextChanged(CharSequence s, int start, int before, int count) {
} // show soft keyboard automatically
editName.requestFocus();
@Override getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
public void afterTextChanged(Editable s) {
for (int i = 0; i < s.length(); i++) { final EditText editValue = (EditText) root.findViewById(R.id.var_edit_value);
char c = s.charAt(i); editValue.setText(input.getValue());
if (!AbstractMathEntityListFragment.acceptableChars.contains(c)) {
s.delete(i, i + 1); final EditText editDescription = (EditText) root.findViewById(R.id.var_edit_description);
Toast.makeText(getActivity(), String.format(errorMsg, c), Toast.LENGTH_SHORT).show(); editDescription.setText(input.getDescription());
}
} final Var.Builder varBuilder;
} final IConstant constant = input.getConstant();
}); if (constant != null) {
varBuilder = new Var.Builder(constant);
// show soft keyboard automatically } else {
editName.requestFocus(); varBuilder = new Var.Builder();
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); }
final EditText editValue = (EditText) root.findViewById(R.id.var_edit_value); root.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
editValue.setText(input.getValue()); @Override
public void onClick(View v) {
final EditText editDescription = (EditText) root.findViewById(R.id.var_edit_description); dismiss();
editDescription.setText(input.getDescription()); }
});
final Var.Builder varBuilder;
final IConstant constant = input.getConstant(); root.findViewById(R.id.save_button).setOnClickListener(new VarEditorSaver<IConstant>(varBuilder, constant, root, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), this));
if (constant != null) {
varBuilder = new Var.Builder(constant); if ( constant == null ) {
} else { // CREATE MODE
varBuilder = new Var.Builder(); getDialog().setTitle(R.string.c_var_create_var);
}
root.findViewById(R.id.remove_button).setVisibility(View.GONE);
root.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { } else {
@Override // EDIT MODE
public void onClick(View v) { getDialog().setTitle(R.string.c_var_edit_var);
dismiss();
} root.findViewById(R.id.remove_button).setOnClickListener(new MathEntityRemover<IConstant>(constant, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), getActivity(), this));
}); }
}
root.findViewById(R.id.save_button).setOnClickListener(new VarEditorSaver<IConstant>(varBuilder, constant, root, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), this));
@Override
if ( constant == null ) { public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
// CREATE MODE switch (calculatorEventType) {
getDialog().setTitle(R.string.c_var_create_var); case constant_removed:
case constant_added:
root.findViewById(R.id.remove_button).setVisibility(View.GONE); case constant_changed:
} else { if ( calculatorEventData.getSource() == this ) {
// EDIT MODE dismiss();
getDialog().setTitle(R.string.c_var_edit_var); }
break;
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: * STATIC
case constant_added: *
case constant_changed: **********************************************************************
if ( calculatorEventData.getSource() == this ) { */
dismiss();
} public static void showDialog(@NotNull Input input, @NotNull FragmentManager fm) {
break; AndroidUtils2.showDialog(new VarEditDialogFragment(input), "constant-editor", fm);
}
}
} public static class Input {
public static class Input { @Nullable
private IConstant constant;
@Nullable
private IConstant constant; @Nullable
private String name;
@Nullable
private String name; @Nullable
private String value;
@Nullable
private String value; @Nullable
private String description;
@Nullable
private String description; private Input() {
}
private Input() {
} @NotNull
public static Input newInstance() {
@NotNull return new Input();
public static Input newInstance() { }
return new Input();
} @NotNull
public static Input newFromConstant(@NotNull IConstant constant) {
@NotNull final Input result = new Input();
public static Input newFromConstant(@NotNull IConstant constant) { result.constant = constant;
final Input result = new Input(); return result;
result.constant = constant; }
return result;
} @NotNull
public static Input newFromValue(@Nullable String value) {
@NotNull final Input result = new Input();
public static Input newFromValue(@Nullable String value) { result.value = value;
final Input result = new Input(); return result;
result.value = value; }
return result;
} @NotNull
public static Input newInstance(@Nullable IConstant constant, @Nullable String name, @Nullable String value, @Nullable String description) {
@NotNull final Input result = new Input();
public static Input newInstance(@Nullable IConstant constant, @Nullable String name, @Nullable String value, @Nullable String description) { result.constant = constant;
final Input result = new Input(); result.name = name;
result.constant = constant; result.value = value;
result.name = name; result.description = description;
result.value = value; return result;
result.description = description; }
return result;
} @Nullable
public IConstant getConstant() {
@Nullable return constant;
public IConstant getConstant() { }
return constant;
} @Nullable
public String getName() {
@Nullable return name == null ? (constant == null ? null : constant.getName()) : name;
public String getName() { }
return name == null ? (constant == null ? null : constant.getName()) : name;
} @Nullable
public String getValue() {
@Nullable return value == null ? (constant == null ? null : constant.getValue()) : value;
public String getValue() { }
return value == null ? (constant == null ? null : constant.getValue()) : value;
} @Nullable
public String getDescription() {
@Nullable return description == null ? (constant == null ? null : constant.getDescription()) : description;
public String getDescription() { }
return description == null ? (constant == null ? null : constant.getDescription()) : description; }
} }
}
}