isNewFunction + fix for RuntimeExceptions from registry

This commit is contained in:
serso 2016-01-23 14:03:46 +01:00
parent 4245dc7cc0
commit 2e19256941

View File

@ -142,12 +142,16 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) { protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) {
builder.setNegativeButton(R.string.c_cancel, null); builder.setNegativeButton(R.string.c_cancel, null);
builder.setPositiveButton(R.string.ok, null); builder.setPositiveButton(R.string.ok, null);
builder.setTitle(function == null ? R.string.function_create_function : R.string.function_edit_function); builder.setTitle(isNewFunction() ? R.string.function_create_function : R.string.function_edit_function);
if (function != null) { if (!isNewFunction()) {
builder.setNeutralButton(R.string.c_remove, null); builder.setNeutralButton(R.string.c_remove, null);
} }
} }
private boolean isNewFunction() {
return function == null || function.id == NO_ID;
}
@NonNull @NonNull
@Override @Override
public AlertDialog onCreateDialog(Bundle savedInstanceState) { public AlertDialog onCreateDialog(Bundle savedInstanceState) {
@ -170,7 +174,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
tryClose(); tryClose();
} }
}); });
if (function != null) { if (!isNewFunction()) {
final Button neutral = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); final Button neutral = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
neutral.setOnClickListener(new View.OnClickListener() { neutral.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -276,25 +280,26 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
} }
private void tryClose() { private void tryClose() {
if (validate()) { if (validate() && applyData()) {
applyData();
dismiss(); dismiss();
} }
} }
private void applyData() { private boolean applyData() {
try { try {
final String body = calculator.prepareExpression(bodyView.getText().toString()).getExpression(); final String body = calculator.prepareExpression(bodyView.getText().toString()).getExpression();
final CppFunction newFunction = CppFunction.builder(nameView.getText().toString(), body) final CppFunction newFunction = CppFunction.builder(nameView.getText().toString(), body)
.withId(function == null ? NO_ID : function.id) .withId(isNewFunction() ? NO_ID : function.id)
.withParameters(collectParameters()) .withParameters(collectParameters())
.withDescription(descriptionView.getText().toString()).build(); .withDescription(descriptionView.getText().toString()).build();
final Function oldFunction = (function == null || function.id == NO_ID) ? null : registry.getById(function.id); final Function oldFunction = isNewFunction() ? null : registry.getById(function.id);
registry.add(newFunction.toCustomFunctionBuilder(), oldFunction); registry.add(newFunction.toCustomFunctionBuilder(), oldFunction);
} catch (ParseException e) { return true;
} catch (RuntimeException e) {
setError(bodyLabel, e.getLocalizedMessage()); setError(bodyLabel, e.getLocalizedMessage());
} }
return false;
} }
private boolean validate() { private boolean validate() {
@ -314,7 +319,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
setError(nameLabel, getString(R.string.function_already_exists)); setError(nameLabel, getString(R.string.function_already_exists));
return false; return false;
} }
if (function != null && !existingFunction.getId().equals(function.getId())) { if (!isNewFunction() && !existingFunction.getId().equals(function.getId())) {
setError(nameLabel, getString(R.string.function_already_exists)); setError(nameLabel, getString(R.string.function_already_exists));
return false; return false;
} }