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 @Nullable
protected abstract String getDescription(@NonNull E entity); 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 void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull E entity, @Nonnull MenuItem.OnMenuItemClickListener listener);
protected abstract boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull E entity); 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) { public void bind(@Nonnull E entity) {
this.entity = entity; this.entity = entity;
textView.setText(entity.getName()); textView.setText(getName(entity));
final String description = getDescription(entity); final String description = getDescription(entity);
if (!Strings.isEmpty(description)) { if (!Strings.isEmpty(description)) {

View File

@ -148,4 +148,10 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
protected String getDescription(@NonNull Function function) { protected String getDescription(@NonNull Function function) {
return registry.getDescription(function.getName()); 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); 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) { public static boolean isValidValue(@Nonnull String value) {
try { try {
final PreparedExpression expression = ToJsclTextProcessor.getInstance().process(value); final PreparedExpression expression = ToJsclTextProcessor.getInstance().process(value);
final List<IConstant> constants = expression.getUndefinedVars(); final List<IConstant> variables = expression.getUndefinedVars();
return constants.isEmpty(); return variables.isEmpty();
} catch (RuntimeException e) { } catch (RuntimeException e) {
return false; return false;
} }
@ -112,44 +112,44 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
} }
@Override @Override
protected Category getCategory(@Nonnull IConstant var) { protected Category getCategory(@Nonnull IConstant variable) {
return registry.getCategory(var); return registry.getCategory(variable);
} }
@Override @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); 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_edit, listener);
addMenu(menu, R.string.c_remove, 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); addMenu(menu, R.string.c_copy_value, listener);
} }
} }
@Override @Override
protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull final IConstant constant) { protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull final IConstant variable) {
FragmentActivity activity = getActivity(); FragmentActivity activity = getActivity();
switch (item.getItemId()) { switch (item.getItemId()) {
case R.string.c_use: case R.string.c_use:
onClick(constant); onClick(variable);
return true; return true;
case R.string.c_edit: case R.string.c_edit:
EditVariableFragment.showDialog(CppVariable.builder(constant).build(), activity); EditVariableFragment.showDialog(CppVariable.builder(variable).build(), activity);
return true; return true;
case R.string.c_remove: case R.string.c_remove:
EntityRemovalDialog.showForVariable(getActivity(), constant.getName(), new DialogInterface.OnClickListener() { EntityRemovalDialog.showForVariable(getActivity(), variable.getName(), new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE); Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
registry.remove(constant); registry.remove(variable);
} }
}); });
return true; return true;
case R.string.c_copy_value: case R.string.c_copy_value:
copyText(constant.getValue()); copyText(variable.getValue());
return true; return true;
} }
return false; return false;
@ -172,8 +172,13 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
@Nullable @Nullable
@Override @Override
protected String getDescription(@NonNull IConstant constant) { protected String getDescription(@NonNull IConstant variable) {
return registry.getDescription(constant.getName()); 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() { public String toString() {
final StringBuilder result = new StringBuilder(); return formatParameter(0) + getName();
/*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();
} }
public final Generic numeric() { public final Generic numeric() {