Changes
This commit is contained in:
		| @@ -68,4 +68,7 @@ public interface Calculator extends CalculatorEventContainer, HistoryControl<Cal | ||||
|  | ||||
|     @NotNull | ||||
|     CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId); | ||||
|  | ||||
| 	@NotNull | ||||
| 	PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException; | ||||
| } | ||||
|   | ||||
| @@ -122,7 +122,7 @@ public enum CalculatorEventType { | ||||
| 	// @NotNull Function | ||||
| 	function_added, | ||||
|  | ||||
| 	// @NotNull Change<Function> | ||||
| 	// @NotNull Change<IFunction> | ||||
| 	function_changed, | ||||
|  | ||||
|     /* | ||||
|   | ||||
| @@ -9,6 +9,7 @@ package org.solovyev.android.calculator; | ||||
| import jscl.CustomFunctionCalculationException; | ||||
| import jscl.math.function.CustomFunction; | ||||
| import jscl.math.function.Function; | ||||
| import jscl.math.function.IFunction; | ||||
| import org.jetbrains.annotations.NotNull; | ||||
| import org.jetbrains.annotations.Nullable; | ||||
| import org.solovyev.android.calculator.model.AFunction; | ||||
| @@ -16,8 +17,8 @@ import org.solovyev.android.calculator.model.Functions; | ||||
| import org.solovyev.android.calculator.model.MathEntityBuilder; | ||||
| import org.solovyev.common.JBuilder; | ||||
| import org.solovyev.common.math.MathRegistry; | ||||
| import org.solovyev.common.text.StringUtils; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| @@ -52,7 +53,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis | ||||
|  | ||||
| 	public static void saveFunction(@NotNull CalculatorMathRegistry<Function> registry, | ||||
| 									@NotNull MathEntityBuilder<? extends Function> builder, | ||||
| 									@Nullable Function editedInstance, | ||||
| 									@Nullable IFunction editedInstance, | ||||
| 									@NotNull Object source, boolean save) throws CustomFunctionCalculationException { | ||||
| 		final Function addedFunction = registry.add(builder); | ||||
|  | ||||
| @@ -84,22 +85,34 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
| 	@Nullable | ||||
| 	@Override | ||||
| 	public String getDescription(@NotNull String functionName) { | ||||
| 		final Function function = get(functionName); | ||||
|  | ||||
| 		String result = null; | ||||
| 		if ( function instanceof CustomFunction ) { | ||||
| 			result = ((CustomFunction) function).getDescription(); | ||||
| 		} | ||||
|  | ||||
| 		if (StringUtils.isEmpty(result) ) { | ||||
| 			result = super.getDescription(functionName); | ||||
| 		} | ||||
|  | ||||
| 		return result; | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	@NotNull | ||||
| 	@Override | ||||
| 	protected JBuilder<? extends Function> createBuilder(@NotNull AFunction entity) { | ||||
| 		CustomFunction.Builder builder = new CustomFunction.Builder(entity.getName(), entity.getParameterNames(), entity.getContent()); | ||||
| 		builder.setDescription(entity.getDescription()); | ||||
| 		return builder; | ||||
| 	protected JBuilder<? extends Function> createBuilder(@NotNull AFunction function) { | ||||
| 		return new CustomFunction.Builder(function); | ||||
| 	} | ||||
|  | ||||
|     @Override | ||||
| 	protected AFunction transform(@NotNull Function entity) { | ||||
| 		if (entity instanceof CustomFunction) { | ||||
| 			final AFunction result = new AFunction(); | ||||
| 			result.setName(entity.getName()); | ||||
| 			result.setContent(((CustomFunction) entity).getContent()); | ||||
| 			result.setParameterNames(new ArrayList<String>(((CustomFunction) entity).getParameterNames())); | ||||
| 			return result; | ||||
| 	protected AFunction transform(@NotNull Function function) { | ||||
| 		if (function instanceof CustomFunction) { | ||||
| 			return AFunction.fromIFunction((CustomFunction) function); | ||||
| 		} else { | ||||
| 			return null; | ||||
| 		} | ||||
|   | ||||
| @@ -169,7 +169,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { | ||||
|             if (StringUtils.isEmpty(expression)) { | ||||
|                 fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, CalculatorOutputImpl.newEmptyOutput(operation)); | ||||
|             } else { | ||||
|                 preparedExpression = preprocessor.process(expression); | ||||
| 				preparedExpression = prepareExpression(expression); | ||||
|  | ||||
| 				final String jsclExpression = preparedExpression.toString(); | ||||
|  | ||||
| @@ -204,6 +204,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { | ||||
|         } | ||||
|     } | ||||
|  | ||||
| 	@NotNull | ||||
| 	@Override | ||||
| 	public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException { | ||||
| 		return preprocessor.process(expression); | ||||
| 	} | ||||
|  | ||||
| 	@NotNull | ||||
|     private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation, | ||||
|                                                         @NotNull String expression, | ||||
|   | ||||
| @@ -13,13 +13,19 @@ import org.simpleframework.xml.Element; | ||||
| import org.simpleframework.xml.ElementList; | ||||
| import org.simpleframework.xml.Root; | ||||
| import org.simpleframework.xml.Transient; | ||||
| import org.solovyev.android.calculator.CalculatorLocatorImpl; | ||||
| import org.solovyev.android.calculator.CalculatorParseException; | ||||
| import org.solovyev.android.calculator.MathPersistenceEntity; | ||||
| import org.solovyev.common.math.MathEntity; | ||||
| import org.solovyev.common.msg.Message; | ||||
| import org.solovyev.common.msg.MessageType; | ||||
| import org.solovyev.common.text.StringUtils; | ||||
|  | ||||
| import java.io.Serializable; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| import java.util.Locale; | ||||
|  | ||||
| /** | ||||
|  * User: serso | ||||
| @@ -28,7 +34,7 @@ import java.util.List; | ||||
|  */ | ||||
|  | ||||
| @Root(name = "function") | ||||
| public class AFunction implements IFunction, MathPersistenceEntity { | ||||
| public class AFunction implements IFunction, MathPersistenceEntity, Serializable { | ||||
|  | ||||
| 	/* | ||||
| 	********************************************************************** | ||||
| @@ -74,6 +80,14 @@ public class AFunction implements IFunction, MathPersistenceEntity { | ||||
| 		this.id = id; | ||||
| 	} | ||||
|  | ||||
| 	public static AFunction fromIFunction(@NotNull IFunction function) { | ||||
| 		final AFunction result = new AFunction(); | ||||
|  | ||||
| 		copy(result, function); | ||||
|  | ||||
| 		return result; | ||||
| 	} | ||||
|  | ||||
| 	/* | ||||
| 	********************************************************************** | ||||
| 	* | ||||
| @@ -85,19 +99,24 @@ public class AFunction implements IFunction, MathPersistenceEntity { | ||||
| 	@Override | ||||
| 	public void copy(@NotNull MathEntity mathEntity) { | ||||
| 		if (mathEntity instanceof IFunction) { | ||||
| 			final IFunction that = ((IFunction) mathEntity); | ||||
| 			this.name = that.getName(); | ||||
| 			this.content = that.getContent(); | ||||
| 			this.description = StringUtils.getNotEmpty(this.getDescription(), ""); | ||||
| 			this.system = that.isSystem(); | ||||
| 			if (that.isIdDefined()) { | ||||
| 				this.id = that.getId(); | ||||
| 			} | ||||
| 			copy(this, (IFunction) mathEntity); | ||||
| 		} else { | ||||
| 			throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + mathEntity.getClass()); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	private static void copy(@NotNull AFunction target, | ||||
| 							 @NotNull IFunction source) { | ||||
| 		target.name = source.getName(); | ||||
| 		target.content = source.getContent(); | ||||
| 		target.description = StringUtils.getNotEmpty(source.getDescription(), ""); | ||||
| 		target.system = source.isSystem(); | ||||
| 		if (source.isIdDefined()) { | ||||
| 			target.id = source.getId(); | ||||
| 		} | ||||
| 		target.parameterNames = new ArrayList<String>(source.getParameterNames()); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toJava() { | ||||
| 		return String.valueOf(this.content); | ||||
| @@ -248,12 +267,57 @@ public class AFunction implements IFunction, MathPersistenceEntity { | ||||
| 			} | ||||
|  | ||||
| 			result.name = name; | ||||
| 			result.content = value; | ||||
| 			try { | ||||
| 				result.content = CalculatorLocatorImpl.getInstance().getCalculator().prepareExpression(value).toString(); | ||||
| 			} catch (CalculatorParseException e) { | ||||
| 				throw new CreationException(e); | ||||
| 			} | ||||
| 			result.system = system; | ||||
| 			result.description = StringUtils.getNotEmpty(description, ""); | ||||
| 			result.parameterNames = new ArrayList<String>(parameterNames); | ||||
|  | ||||
| 			return result; | ||||
| 		} | ||||
|  | ||||
| 		public static class CreationException extends RuntimeException implements Message { | ||||
|  | ||||
| 			@NotNull | ||||
| 			private final CalculatorParseException message; | ||||
|  | ||||
| 			public CreationException(@NotNull CalculatorParseException cause) { | ||||
| 				super(cause); | ||||
| 				message = cause; | ||||
| 			} | ||||
|  | ||||
| 			@NotNull | ||||
| 			@Override | ||||
| 			public String getMessageCode() { | ||||
| 				return message.getMessageCode(); | ||||
| 			} | ||||
|  | ||||
| 			@NotNull | ||||
| 			@Override | ||||
| 			public List<Object> getParameters() { | ||||
| 				return message.getParameters(); | ||||
| 			} | ||||
|  | ||||
| 			@NotNull | ||||
| 			@Override | ||||
| 			public MessageType getMessageType() { | ||||
| 				return message.getMessageType(); | ||||
| 			} | ||||
|  | ||||
| 			@Override | ||||
| 			@Nullable | ||||
| 			public String getLocalizedMessage() { | ||||
| 				return message.getLocalizedMessage(); | ||||
| 			} | ||||
|  | ||||
| 			@NotNull | ||||
| 			@Override | ||||
| 			public String getLocalizedMessage(@NotNull Locale locale) { | ||||
| 				return message.getLocalizedMessage(locale); | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -15,15 +15,15 @@ | ||||
| 			a:id="@+id/function_up_param_button" | ||||
| 			a:layout_height="wrap_content" | ||||
| 			a:layout_width="wrap_content" | ||||
| 			a:text="^" /> | ||||
| 			a:text="↑" /> | ||||
|  | ||||
| 	<Button | ||||
| 			a:id="@+id/function_down_param_button" | ||||
| 			a:layout_height="wrap_content" | ||||
| 			a:layout_width="wrap_content" | ||||
| 			a:text="v" /> | ||||
| 			a:text="↓" /> | ||||
|  | ||||
|     <EditText | ||||
|     <org.solovyev.android.calculator.function.FunctionParamEditText | ||||
| 		a:id="@+id/function_param_edit_text" | ||||
| 		a:layout_height="wrap_content" | ||||
|         a:layout_width="match_parent" | ||||
|   | ||||
| @@ -127,6 +127,12 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S | ||||
|         return calculator.fireCalculatorEvent(calculatorEventType, data, sequenceId); | ||||
|     } | ||||
|  | ||||
| 	@NotNull | ||||
| 	@Override | ||||
| 	public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException { | ||||
| 		return calculator.prepareExpression(expression); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
|     public void init() { | ||||
|         this.calculator.init(); | ||||
|   | ||||
| @@ -1,6 +1,8 @@ | ||||
| package org.solovyev.android.calculator.function; | ||||
|  | ||||
| import android.os.Bundle; | ||||
| import android.os.Parcel; | ||||
| import android.os.Parcelable; | ||||
| import android.support.v4.app.DialogFragment; | ||||
| import android.support.v4.app.FragmentManager; | ||||
| import android.view.LayoutInflater; | ||||
| @@ -13,10 +15,15 @@ import jscl.math.function.IFunction; | ||||
| import org.jetbrains.annotations.NotNull; | ||||
| import org.jetbrains.annotations.Nullable; | ||||
| import org.solovyev.android.AndroidUtils2; | ||||
| import org.solovyev.android.calculator.*; | ||||
| import org.solovyev.android.calculator.CalculatorEventData; | ||||
| import org.solovyev.android.calculator.CalculatorEventListener; | ||||
| import org.solovyev.android.calculator.CalculatorEventType; | ||||
| import org.solovyev.android.calculator.CalculatorLocatorImpl; | ||||
| import org.solovyev.android.calculator.R; | ||||
| import org.solovyev.android.calculator.math.edit.MathEntityRemover; | ||||
| import org.solovyev.android.calculator.model.AFunction; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
| @@ -26,8 +33,10 @@ import java.util.List; | ||||
|  */ | ||||
| public class FunctionEditDialogFragment extends DialogFragment implements CalculatorEventListener { | ||||
|  | ||||
| 	private static final String INPUT = "input"; | ||||
|  | ||||
| 	@NotNull | ||||
|     private final Input input; | ||||
|     private Input input; | ||||
|  | ||||
|     public FunctionEditDialogFragment() { | ||||
|         this(Input.newInstance()); | ||||
| @@ -39,7 +48,16 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul | ||||
|  | ||||
|     @Override | ||||
|     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||||
|         return inflater.inflate(R.layout.function_edit, container, false); | ||||
| 		final View result = inflater.inflate(R.layout.function_edit, container, false); | ||||
|  | ||||
| 		if (savedInstanceState != null) { | ||||
| 			final  Parcelable input = savedInstanceState.getParcelable(INPUT); | ||||
| 			if ( input instanceof Input ) { | ||||
| 				this.input = (Input)input; | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		return result; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -49,7 +67,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul | ||||
|         final FunctionParamsView paramsView = (FunctionParamsView) root.findViewById(R.id.function_params_layout); | ||||
|  | ||||
| 		final AFunction.Builder builder; | ||||
| 		final IFunction function = input.getFunction(); | ||||
| 		final AFunction function = input.getFunction(); | ||||
| 		if (function != null) { | ||||
| 			builder = new AFunction.Builder(function); | ||||
| 		} else { | ||||
| @@ -92,11 +110,18 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul | ||||
| 			// EDIT MODE | ||||
| 			getDialog().setTitle(R.string.function_edit_function); | ||||
|  | ||||
| 			Function customFunction = new CustomFunction.Builder(function).create(); | ||||
| 			final Function customFunction = new CustomFunction.Builder(function).create(); | ||||
| 			root.findViewById(R.id.remove_button).setOnClickListener(MathEntityRemover.newFunctionRemover(customFunction, null, this.getActivity(), FunctionEditDialogFragment.this)); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void onSaveInstanceState(@NotNull Bundle out) { | ||||
| 		super.onSaveInstanceState(out); | ||||
|  | ||||
| 		out.putParcelable(INPUT, FunctionEditorSaver.readInput(input.getFunction(), getView())); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
|     public void onResume() { | ||||
|         super.onResume(); | ||||
| @@ -137,10 +162,50 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul | ||||
|         AndroidUtils2.showDialog(new FunctionEditDialogFragment(input), "function-editor", fm); | ||||
|     } | ||||
|  | ||||
|     public static class Input { | ||||
|     public static class Input implements Parcelable { | ||||
|  | ||||
| 		public static final Parcelable.Creator<Input> CREATOR = new Creator<Input>() { | ||||
| 			@Override | ||||
| 			public Input createFromParcel(@NotNull Parcel in) { | ||||
| 				return Input.fromParcel(in); | ||||
| 			} | ||||
|  | ||||
| 			@Override | ||||
| 			public Input[] newArray(int size) { | ||||
| 				return new Input[size]; | ||||
| 			} | ||||
| 		}; | ||||
|  | ||||
| 		private static final Parcelable.Creator<String> STRING_CREATOR = new Creator<String>() { | ||||
| 			@Override | ||||
| 			public String createFromParcel(@NotNull Parcel in) { | ||||
| 				return in.readString(); | ||||
| 			} | ||||
|  | ||||
| 			@Override | ||||
| 			public String[] newArray(int size) { | ||||
| 				return new String[size]; | ||||
| 			} | ||||
| 		}; | ||||
|  | ||||
| 		@NotNull | ||||
| 		private static Input fromParcel(@NotNull Parcel in) { | ||||
| 			final Input result = new Input(); | ||||
| 			result.name = in.readString(); | ||||
| 			result.content = in.readString(); | ||||
| 			result.description = in.readString(); | ||||
|  | ||||
| 			final List<String> parameterNames = new ArrayList<String>(); | ||||
| 			in.readTypedList(parameterNames, STRING_CREATOR); | ||||
| 			result.parameterNames = parameterNames; | ||||
|  | ||||
| 			result.function = (AFunction) in.readSerializable(); | ||||
|  | ||||
| 			return result; | ||||
| 		} | ||||
|  | ||||
| 		@Nullable | ||||
|         private IFunction function; | ||||
|         private AFunction function; | ||||
|  | ||||
|         @Nullable | ||||
|         private String name; | ||||
| @@ -165,14 +230,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul | ||||
|         @NotNull | ||||
|         public static Input newFromFunction(@NotNull IFunction 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.content = value; | ||||
|             result.function = AFunction.fromIFunction(function); | ||||
|             return result; | ||||
|         } | ||||
|  | ||||
| @@ -180,18 +238,23 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul | ||||
|         public static Input newInstance(@Nullable IFunction function, | ||||
|                                         @Nullable String name, | ||||
|                                         @Nullable String value, | ||||
|                                         @Nullable String description) { | ||||
|                                         @Nullable String description, | ||||
| 										@NotNull List<String> parameterNames) { | ||||
|  | ||||
|             final Input result = new Input(); | ||||
|             result.function = function; | ||||
| 			if (function != null) { | ||||
| 				result.function = AFunction.fromIFunction(function); | ||||
| 			} | ||||
| 			result.name = name; | ||||
|             result.content = value; | ||||
|             result.description = description; | ||||
|             result.parameterNames = new ArrayList<String>(parameterNames); | ||||
|  | ||||
|             return result; | ||||
|         } | ||||
|  | ||||
|         @Nullable | ||||
|         public IFunction getFunction() { | ||||
|         public AFunction getFunction() { | ||||
|             return function; | ||||
|         } | ||||
|  | ||||
| @@ -214,5 +277,19 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul | ||||
|         public List<String> getParameterNames() { | ||||
|             return parameterNames == null ? (function == null ? null : function.getParameterNames()) : parameterNames; | ||||
|         } | ||||
|  | ||||
| 		@Override | ||||
| 		public int describeContents() { | ||||
| 			return 0; | ||||
| 		} | ||||
|  | ||||
| 		@Override | ||||
| 		public void writeToParcel(@NotNull Parcel out, int flags) { | ||||
| 			out.writeString(name); | ||||
| 			out.writeString(content); | ||||
| 			out.writeString(description); | ||||
| 			out.writeList(parameterNames); | ||||
| 			out.writeSerializable(function); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -18,6 +18,7 @@ import org.solovyev.android.calculator.model.MathEntityBuilder; | ||||
| import org.solovyev.common.msg.MessageType; | ||||
| import org.solovyev.common.text.StringUtils; | ||||
|  | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
|  | ||||
| public class FunctionEditorSaver implements View.OnClickListener { | ||||
| @@ -29,7 +30,7 @@ public class FunctionEditorSaver implements View.OnClickListener { | ||||
| 	private final AFunction.Builder builder; | ||||
|  | ||||
| 	@Nullable | ||||
| 	private final Function editedInstance; | ||||
| 	private final IFunction editedInstance; | ||||
|  | ||||
| 	@NotNull | ||||
| 	private final View view; | ||||
| @@ -45,11 +46,7 @@ public class FunctionEditorSaver implements View.OnClickListener { | ||||
| 							   @NotNull Object source) { | ||||
|  | ||||
| 		this.builder = builder; | ||||
| 		if (editedInstance instanceof Function || editedInstance == null) { | ||||
| 			this.editedInstance = (Function)editedInstance; | ||||
| 		} else { | ||||
| 			throw new IllegalArgumentException(); | ||||
| 		} | ||||
| 		this.editedInstance = editedInstance; | ||||
| 		this.view = view; | ||||
| 		this.mathRegistry = registry; | ||||
| 		this.source = source; | ||||
| @@ -59,17 +56,16 @@ public class FunctionEditorSaver implements View.OnClickListener { | ||||
| 	public void onClick(View v) { | ||||
| 		final Integer error; | ||||
|  | ||||
| 		final EditText editName = (EditText) view.findViewById(R.id.function_edit_name); | ||||
| 		String name = editName.getText().toString(); | ||||
| 		final FunctionEditDialogFragment.Input input = readInput(null, view); | ||||
|  | ||||
| 		final EditText editValue = (EditText) view.findViewById(R.id.function_edit_value); | ||||
| 		String content = editValue.getText().toString(); | ||||
| 		final String name = input.getName(); | ||||
| 		final String content = input.getContent(); | ||||
| 		final String description = input.getDescription(); | ||||
|  | ||||
| 		final EditText editDescription = (EditText) view.findViewById(R.id.function_edit_description); | ||||
| 		String description = editDescription.getText().toString(); | ||||
|  | ||||
| 		final FunctionParamsView editParams = (FunctionParamsView) view.findViewById(R.id.function_params_layout); | ||||
| 		List<String> parameterNames = editParams.getParameterNames(); | ||||
| 		List<String> parameterNames = input.getParameterNames(); | ||||
| 		if ( parameterNames == null ) { | ||||
| 			parameterNames = Collections.emptyList(); | ||||
| 		} | ||||
|  | ||||
| 		if (VarEditorSaver.isValidName(name)) { | ||||
|  | ||||
| @@ -111,10 +107,29 @@ public class FunctionEditorSaver implements View.OnClickListener { | ||||
|                 CalculatorFunctionsMathRegistry.saveFunction(mathRegistry, new BuilderAdapter(builder), editedInstance, source, true); | ||||
|             } catch (CustomFunctionCalculationException e) { | ||||
|                 CalculatorLocatorImpl.getInstance().getNotifier().showMessage(e); | ||||
|             } catch (AFunction.Builder.CreationException e) { | ||||
| 				CalculatorLocatorImpl.getInstance().getNotifier().showMessage(e); | ||||
| 			} | ||||
|         } | ||||
| 	} | ||||
|  | ||||
| 	@NotNull | ||||
| 	public static FunctionEditDialogFragment.Input readInput(@Nullable IFunction function, @NotNull View root) { | ||||
| 		final EditText editName = (EditText) root.findViewById(R.id.function_edit_name); | ||||
| 		String name = editName.getText().toString(); | ||||
|  | ||||
| 		final EditText editValue = (EditText) root.findViewById(R.id.function_edit_value); | ||||
| 		String content = editValue.getText().toString(); | ||||
|  | ||||
| 		final EditText editDescription = (EditText) root.findViewById(R.id.function_edit_description); | ||||
| 		String description = editDescription.getText().toString(); | ||||
|  | ||||
| 		final FunctionParamsView editParams = (FunctionParamsView) root.findViewById(R.id.function_params_layout); | ||||
| 		List<String> parameterNames = editParams.getParameterNames(); | ||||
|  | ||||
| 		return FunctionEditDialogFragment.Input.newInstance(function, name, content, description, parameterNames); | ||||
| 	} | ||||
|  | ||||
| 	private boolean validateParameters(@NotNull List<String> parameterNames) { | ||||
| 		for (String parameterName : parameterNames) { | ||||
| 			if ( !VarEditorSaver.isValidName(parameterName) ) { | ||||
|   | ||||
| @@ -0,0 +1,34 @@ | ||||
| package org.solovyev.android.calculator.function; | ||||
|  | ||||
| import android.content.Context; | ||||
| import android.os.Parcelable; | ||||
| import android.util.AttributeSet; | ||||
| import android.view.AbsSavedState; | ||||
| import android.widget.EditText; | ||||
|  | ||||
| public class FunctionParamEditText extends EditText { | ||||
|  | ||||
| 	public FunctionParamEditText(Context context) { | ||||
| 		super(context); | ||||
| 	} | ||||
|  | ||||
| 	public FunctionParamEditText(Context context, AttributeSet attrs) { | ||||
| 		super(context, attrs); | ||||
| 	} | ||||
|  | ||||
| 	public FunctionParamEditText(Context context, AttributeSet attrs, int defStyle) { | ||||
| 		super(context, attrs, defStyle); | ||||
| 	} | ||||
|  | ||||
| 	// we restore state manually outside | ||||
| 	@Override | ||||
| 	public Parcelable onSaveInstanceState() { | ||||
| 		super.onSaveInstanceState(); | ||||
| 		return AbsSavedState.EMPTY_STATE; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public void onRestoreInstanceState(Parcelable state) { | ||||
| 		super.onRestoreInstanceState(null); | ||||
| 	} | ||||
| } | ||||
| @@ -294,6 +294,11 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten | ||||
|         } | ||||
|     } | ||||
|  | ||||
| 	@Nullable | ||||
| 	protected MathEntityArrayAdapter<T> getAdapter() { | ||||
| 		return adapter; | ||||
| 	} | ||||
|  | ||||
| 	@NotNull | ||||
|     protected Handler getUiHandler() { | ||||
|         return uiHandler; | ||||
|   | ||||
| @@ -18,7 +18,12 @@ import jscl.math.function.Function; | ||||
| import jscl.math.function.IFunction; | ||||
| import org.jetbrains.annotations.NotNull; | ||||
| import org.jetbrains.annotations.Nullable; | ||||
| import org.solovyev.android.calculator.*; | ||||
| import org.solovyev.android.calculator.CalculatorEventData; | ||||
| import org.solovyev.android.calculator.CalculatorEventType; | ||||
| import org.solovyev.android.calculator.CalculatorLocatorImpl; | ||||
| import org.solovyev.android.calculator.CalculatorMathRegistry; | ||||
| import org.solovyev.android.calculator.Change; | ||||
| 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; | ||||
| @@ -126,7 +131,7 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment< | ||||
|                 break; | ||||
|  | ||||
|             case function_changed: | ||||
|                 processFunctionChanged((Change<Function>) data); | ||||
|                 processFunctionChanged((Change<IFunction>) data); | ||||
|                 break; | ||||
|  | ||||
|             case function_removed: | ||||
| @@ -148,18 +153,39 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment< | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void processFunctionChanged(@NotNull final Change<Function> change) { | ||||
|         final Function newFunction = change.getNewValue(); | ||||
|         if (this.isInCategory(newFunction)) { | ||||
|     private void processFunctionChanged(@NotNull final Change<IFunction> change) { | ||||
|         final IFunction newFunction = change.getNewValue(); | ||||
|  | ||||
| 		if (newFunction instanceof Function) { | ||||
|  | ||||
| 			if (this.isInCategory((Function)newFunction)) { | ||||
|  | ||||
| 				getUiHandler().post(new Runnable() { | ||||
| 					@Override | ||||
| 					public void run() { | ||||
|                     removeFromAdapter(change.getOldValue()); | ||||
|                     addToAdapter(newFunction); | ||||
| 						IFunction oldValue = change.getOldValue(); | ||||
|  | ||||
| 						if (oldValue.isIdDefined()) { | ||||
| 							final MathEntityArrayAdapter<Function> adapter = getAdapter(); | ||||
| 							if ( adapter != null ) { | ||||
| 								for (int i = 0; i < adapter.getCount(); i++) { | ||||
| 									final Function functionFromAdapter = adapter.getItem(i); | ||||
| 									if ( functionFromAdapter.isIdDefined() && oldValue.getId().equals(functionFromAdapter.getId()) ) { | ||||
| 										adapter.remove(functionFromAdapter); | ||||
| 										break; | ||||
| 									} | ||||
| 								} | ||||
| 							} | ||||
| 						} | ||||
|  | ||||
| 						addToAdapter((Function)newFunction); | ||||
| 						sort(); | ||||
| 					} | ||||
| 				}); | ||||
| 			} | ||||
| 		} else { | ||||
| 			throw new IllegalArgumentException("Function must be instance of jscl.math.function.Function class!"); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|     private void processFunctionAdded(@NotNull final Function function) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Sergey Solovyev
					Sergey Solovyev