Add function dialog added
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
package org.solovyev.android.calculator.function;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import jscl.math.function.CustomFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.AndroidUtils2;
|
||||
import org.solovyev.android.calculator.*;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/13/12
|
||||
* Time: 11:34 PM
|
||||
*/
|
||||
public class FunctionEditDialogFragment extends DialogFragment implements CalculatorEventListener {
|
||||
|
||||
@NotNull
|
||||
private final Input input;
|
||||
|
||||
public FunctionEditDialogFragment() {
|
||||
this(Input.newInstance());
|
||||
}
|
||||
|
||||
public FunctionEditDialogFragment(@NotNull Input input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.function_edit, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NotNull View root, Bundle savedInstanceState) {
|
||||
super.onViewCreated(root, savedInstanceState);
|
||||
|
||||
final View addParamButton = root.findViewById(R.id.function_add_param_button);
|
||||
|
||||
final ViewGroup functionParamsLayout = (ViewGroup) root.findViewById(R.id.function_params_layout);
|
||||
addParamButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
final View functionEditParamLayout = inflater.inflate(R.layout.function_edit_param, null);
|
||||
|
||||
final View addParamButton = functionEditParamLayout.findViewById(R.id.function_add_param_button);
|
||||
addParamButton.setVisibility(View.GONE);
|
||||
|
||||
final View removeParamButton = functionEditParamLayout.findViewById(R.id.function_remove_param_button);
|
||||
removeParamButton.setVisibility(View.VISIBLE);
|
||||
|
||||
functionParamsLayout.addView(functionEditParamLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
|
||||
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public static void showDialog(@NotNull Input input, @NotNull FragmentManager fm) {
|
||||
AndroidUtils2.showDialog(new FunctionEditDialogFragment(input), "function-editor", fm);
|
||||
}
|
||||
|
||||
public static class Input {
|
||||
|
||||
@Nullable
|
||||
private CustomFunction function;
|
||||
|
||||
@Nullable
|
||||
private String name;
|
||||
|
||||
@Nullable
|
||||
private String value;
|
||||
|
||||
@Nullable
|
||||
private String description;
|
||||
|
||||
private Input() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Input newInstance() {
|
||||
return new Input();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Input newFromFunction(@NotNull CustomFunction function) {
|
||||
final Input result = new Input();
|
||||
result.function = function;
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Input newFromValue(@Nullable String value) {
|
||||
final Input result = new Input();
|
||||
result.value = value;
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Input newInstance(@Nullable CustomFunction function, @Nullable String name, @Nullable String value, @Nullable String description) {
|
||||
final Input result = new Input();
|
||||
result.function = function;
|
||||
result.name = name;
|
||||
result.value = value;
|
||||
result.description = description;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public CustomFunction getFunction() {
|
||||
return function;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getName() {
|
||||
return name == null ? (function == null ? null : function.getName()) : name;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getValue() {
|
||||
return value == null ? (function == null ? null : function.getContent()) : value;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getDescription() {
|
||||
return description == null ? (function == null ? null : function.getContent()) : description;
|
||||
}
|
||||
}
|
||||
}
|
@@ -10,12 +10,19 @@ import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.text.ClipboardManager;
|
||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||
import com.actionbarsherlock.view.Menu;
|
||||
import com.actionbarsherlock.view.MenuInflater;
|
||||
import com.actionbarsherlock.view.MenuItem;
|
||||
import jscl.math.function.CustomFunction;
|
||||
import jscl.math.function.Function;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.CalculatorEventType;
|
||||
import org.solovyev.android.calculator.CalculatorLocatorImpl;
|
||||
import org.solovyev.android.calculator.CalculatorMathRegistry;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.about.CalculatorFragmentType;
|
||||
import org.solovyev.android.calculator.function.FunctionEditDialogFragment;
|
||||
import org.solovyev.android.menu.AMenuItem;
|
||||
import org.solovyev.android.menu.LabeledMenuItem;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
@@ -31,7 +38,7 @@ import java.util.List;
|
||||
*/
|
||||
public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment<Function> {
|
||||
|
||||
public static final String CREATE_FUN_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity_create_fun";
|
||||
public static final String CREATE_FUNCTION_EXTRA_STRING = "create_function";
|
||||
|
||||
public CalculatorFunctionsFragment() {
|
||||
super(CalculatorFragmentType.functions);
|
||||
@@ -67,6 +74,8 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment<
|
||||
intent.removeExtra(CREATE_FUN_EXTRA_STRING);
|
||||
}
|
||||
}*/
|
||||
setHasOptionsMenu(true);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -78,10 +87,16 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment<
|
||||
@Override
|
||||
protected List<LabeledMenuItem<Function>> getMenuItemsOnLongClick(@NotNull Function item) {
|
||||
List<LabeledMenuItem<Function>> result = new ArrayList<LabeledMenuItem<Function>>(Arrays.asList(LongClickMenuItem.values()));
|
||||
|
||||
if ( StringUtils.isEmpty(CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(item.getName())) ) {
|
||||
|
||||
final CalculatorMathRegistry<Function> functionsRegistry = CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry();
|
||||
if ( StringUtils.isEmpty(functionsRegistry.getDescription(item.getName())) ) {
|
||||
result.remove(LongClickMenuItem.copy_description);
|
||||
}
|
||||
|
||||
final Function function = functionsRegistry.get(item.getName());
|
||||
if (!(function instanceof CustomFunction)) {
|
||||
result.remove(LongClickMenuItem.edit);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -189,6 +204,35 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment<
|
||||
return CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getCategory(function);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* MENU
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.functions_menu, menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
boolean result;
|
||||
|
||||
switch (item.getItemId()) {
|
||||
case R.id.functions_menu_add_function:
|
||||
FunctionEditDialogFragment.showDialog(FunctionEditDialogFragment.Input.newInstance(), this.getActivity().getSupportFragmentManager());
|
||||
result = true;
|
||||
break;
|
||||
default:
|
||||
result = super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
@@ -205,13 +249,14 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment<
|
||||
}
|
||||
},
|
||||
|
||||
/*edit(R.string.c_edit) {
|
||||
@Override
|
||||
public void doAction(@NotNull Function data, @NotNull Context context) {
|
||||
if (context instanceof AbstractMathEntityListActivity) {
|
||||
}
|
||||
}
|
||||
},*/
|
||||
edit(R.string.c_edit) {
|
||||
@Override
|
||||
public void onClick(@NotNull Function function, @NotNull Context context) {
|
||||
if (function instanceof CustomFunction) {
|
||||
FunctionEditDialogFragment.showDialog(FunctionEditDialogFragment.Input.newFromFunction((CustomFunction) function), ((SherlockFragmentActivity) context).getSupportFragmentManager());
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
copy_description(R.string.c_copy_description) {
|
||||
@Override
|
||||
|
@@ -132,7 +132,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.var_menu, menu);
|
||||
inflater.inflate(R.menu.vars_menu, menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user