Functions and operators should use toString in EntitiesFragment

This commit is contained in:
serso 2016-01-31 10:23:57 +01:00
parent 6d7e75d94b
commit 1fe67e9183
5 changed files with 37 additions and 35 deletions

View File

@ -188,6 +188,9 @@ public abstract class BaseEntitiesFragment<E extends MathEntity> extends BaseFra
@Nullable
protected abstract String getDescription(@NonNull E entity);
@NonNull
protected abstract String getName(@Nonnull E entity);
protected abstract void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull E entity, @Nonnull MenuItem.OnMenuItemClickListener listener);
protected abstract boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull E entity);
@ -209,7 +212,7 @@ public abstract class BaseEntitiesFragment<E extends MathEntity> extends BaseFra
public void bind(@Nonnull E entity) {
this.entity = entity;
textView.setText(entity.getName());
textView.setText(getName(entity));
final String description = getDescription(entity);
if (!Strings.isEmpty(description)) {

View File

@ -148,4 +148,10 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
protected String getDescription(@NonNull Function function) {
return registry.getDescription(function.getName());
}
@NonNull
@Override
protected String getName(@Nonnull Function function) {
return function.toString();
}
}

View File

@ -100,5 +100,11 @@ public class OperatorsFragment extends BaseEntitiesFragment<Operator> {
}
return postfixFunctionsRegistry.getDescription(name);
}
@NonNull
@Override
protected String getName(@Nonnull Operator operator) {
return operator.toString();
}
}

View File

@ -62,8 +62,8 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
public static boolean isValidValue(@Nonnull String value) {
try {
final PreparedExpression expression = ToJsclTextProcessor.getInstance().process(value);
final List<IConstant> constants = expression.getUndefinedVars();
return constants.isEmpty();
final List<IConstant> variables = expression.getUndefinedVars();
return variables.isEmpty();
} catch (RuntimeException e) {
return false;
}
@ -112,44 +112,44 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
}
@Override
protected Category getCategory(@Nonnull IConstant var) {
return registry.getCategory(var);
protected Category getCategory(@Nonnull IConstant variable) {
return registry.getCategory(variable);
}
@Override
protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull IConstant constant, @Nonnull MenuItem.OnMenuItemClickListener listener) {
protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull IConstant variable, @Nonnull MenuItem.OnMenuItemClickListener listener) {
addMenu(menu, R.string.c_use, listener);
if (!constant.isSystem()) {
if (!variable.isSystem()) {
addMenu(menu, R.string.c_edit, listener);
addMenu(menu, R.string.c_remove, listener);
}
if (!Strings.isEmpty(constant.getValue())) {
if (!Strings.isEmpty(variable.getValue())) {
addMenu(menu, R.string.c_copy_value, listener);
}
}
@Override
protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull final IConstant constant) {
protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull final IConstant variable) {
FragmentActivity activity = getActivity();
switch (item.getItemId()) {
case R.string.c_use:
onClick(constant);
onClick(variable);
return true;
case R.string.c_edit:
EditVariableFragment.showDialog(CppVariable.builder(constant).build(), activity);
EditVariableFragment.showDialog(CppVariable.builder(variable).build(), activity);
return true;
case R.string.c_remove:
EntityRemovalDialog.showForVariable(getActivity(), constant.getName(), new DialogInterface.OnClickListener() {
EntityRemovalDialog.showForVariable(getActivity(), variable.getName(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
registry.remove(constant);
registry.remove(variable);
}
});
return true;
case R.string.c_copy_value:
copyText(constant.getValue());
copyText(variable.getValue());
return true;
}
return false;
@ -172,8 +172,13 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
@Nullable
@Override
protected String getDescription(@NonNull IConstant constant) {
return registry.getDescription(constant.getName());
protected String getDescription(@NonNull IConstant variable) {
return registry.getDescription(variable.getName());
}
@NonNull
@Override
protected String getName(@Nonnull IConstant variable) {
return variable.getName();
}
}

View File

@ -15,25 +15,7 @@ abstract class PostfixFunction extends Operator {
}
public String toString() {
final StringBuilder result = new StringBuilder();
/*try {*/
result.append(formatParameter(0));
/*} catch (NotIntegerException e) {
try {
final Variable v = parameters[0].variableValue();
if (v instanceof Frac || v instanceof Pow) {
result.append(GenericVariable.valueOf(parameters[0]));
} else {
result.append(v);
}
} catch (NotVariableException e2) {
result.append(GenericVariable.valueOf(parameters[0]));
}
}*/
result.append(getName());
return result.toString();
return formatParameter(0) + getName();
}
public final Generic numeric() {