From c3d1a877e706939298db2e4dd015b99b4cc8adb9 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Tue, 10 Jan 2012 18:50:43 +0400 Subject: [PATCH] context menu for display --- .../android/calculator/CalculatorDisplay.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java b/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java index c9d48ac6..6a48dfeb 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java @@ -11,14 +11,21 @@ import android.text.Html; import android.util.AttributeSet; import android.util.Log; import jscl.math.Generic; +import jscl.math.function.Constant; +import jscl.math.function.IConstant; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.model.CalculatorEngine; import org.solovyev.android.calculator.model.CalculatorParseException; import org.solovyev.android.calculator.model.TextProcessor; +import org.solovyev.android.view.AMenuBuilder; +import org.solovyev.android.view.AMenuItem; import org.solovyev.android.view.AutoResizeTextView; +import java.util.HashSet; +import java.util.Set; + /** * User: serso * Date: 9/17/11 @@ -26,6 +33,69 @@ import org.solovyev.android.view.AutoResizeTextView; */ public class CalculatorDisplay extends AutoResizeTextView implements ICalculatorDisplay{ + public static enum Menu implements AMenuItem { + to_bin(0) { + @Override + public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) { + //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) { + return false; + } + }, + + plot(R.string.c_plot_graph) { + @Override + public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) { + //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) { + boolean result = false; + + if (operation == JsclOperation.simplify) { + final Set notSystemConstants = new HashSet(); + for (Constant constant : generic.getConstants()) { + IConstant var = CalculatorEngine.instance.getVarsRegistry().get(constant.getName()); + if (var != null && !var.isSystem() && !var.isDefined()) { + notSystemConstants.add(constant); + } + } + + if (notSystemConstants.size() == 1) { + result = true; + } + } + + return result; + } + }; + + private final int captionId; + + Menu(int captionId) { + this.captionId = captionId; + } + + public final boolean isItemVisible(@NotNull CalculatorDisplay display) { + //noinspection ConstantConditions + return display.isValid() && display.getGenericResult() != null && isItemVisibleFor(display.getGenericResult(), display.getJsclOperation()); + } + + protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) { + return true; + } + + @NotNull + @Override + public String getCaption(@NotNull Context context) { + return context.getString(captionId); + } + } + private boolean valid = true; @Nullable