Display menu

This commit is contained in:
Sergey Solovyev 2012-04-20 16:59:50 +04:00
parent fa0be4bd48
commit 891b8abc50
7 changed files with 126 additions and 143 deletions

View File

@ -131,6 +131,7 @@
<string name="c_empty_var_error">Unable to create empty constant!</string>
<string name="c_not_valid_result">Current result is not valid!</string>
<string name="c_convert">Convert to…</string>
<string name="c_plot_graph">Graph</string>
<string name="c_min_x_value">From</string>
<string name="c_max_x_value">To</string>

View File

@ -13,6 +13,7 @@ import android.util.Log;
import jscl.math.Generic;
import jscl.math.function.Constant;
import jscl.math.function.IConstant;
import jscl.math.numeric.Numeric;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.jscl.JsclOperation;
@ -20,8 +21,10 @@ 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.calculator.view.TextHighlighter;
import org.solovyev.android.calculator.view.UnitsConverter;
import org.solovyev.android.menu.AMenuItem;
import org.solovyev.android.view.AutoResizeTextView;
import org.solovyev.common.utils.CollectionsUtils;
import java.util.HashSet;
import java.util.Set;
@ -33,51 +36,70 @@ import java.util.Set;
*/
public class CalculatorDisplay extends AutoResizeTextView implements ICalculatorDisplay{
public static enum Menu implements AMenuItem<CalculatorDisplay> {
to_bin(0) {
@Override
public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) {
//To change body of implemented methods use File | Settings | File Templates.
}
public static enum MenuItem implements AMenuItem<CalculatorDisplay> {
@Override
protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
return false;
}
},
copy(R.string.c_copy) {
@Override
public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) {
CalculatorModel.copyResult(context, data);
}
},
plot(R.string.c_plot_graph) {
@Override
public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) {
convert(R.string.c_convert) {
@Override
public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) {
// todo serso: continue
new UnitsConverter();
}
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
return operation == JsclOperation.numeric && generic.getConstants().isEmpty();
}
},
plot(R.string.c_plot) {
@Override
public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) {
final Generic generic = data.getGenericResult();
assert generic != null;
final Constant constant = CollectionsUtils.getFirstCollectionElement(getNotSystemConstants(generic));
assert constant != null;
CalculatorActivityLauncher.plotGraph(context, generic, constant);
}
@Override
protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
boolean result = false;
if (operation == JsclOperation.simplify) {
final Set<Constant> notSystemConstants = new HashSet<Constant>();
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) {
if (getNotSystemConstants(generic).size() == 1) {
result = true;
}
}
return result;
}
};
@NotNull
private Set<Constant> getNotSystemConstants(@NotNull Generic generic) {
final Set<Constant> notSystemConstants = new HashSet<Constant>();
for (Constant constant : generic.getConstants()) {
IConstant var = CalculatorEngine.instance.getVarsRegistry().get(constant.getName());
if (var != null && !var.isSystem() && !var.isDefined()) {
notSystemConstants.add(constant);
}
}
return notSystemConstants;
}
};
private final int captionId;
Menu(int captionId) {
MenuItem(int captionId) {
this.captionId = captionId;
}

View File

@ -17,13 +17,9 @@ import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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.CursorControl;
import org.solovyev.android.history.HistoryControl;
import org.solovyev.android.calculator.history.CalculatorHistory;
import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.history.TextViewEditorAdapter;
@ -32,16 +28,16 @@ import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.CalculatorEvalException;
import org.solovyev.android.calculator.model.CalculatorParseException;
import org.solovyev.android.history.HistoryControl;
import org.solovyev.android.menu.AMenuBuilder;
import org.solovyev.android.menu.AMenuItem;
import org.solovyev.android.menu.MenuImpl;
import org.solovyev.common.msg.Message;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.MutableObject;
import org.solovyev.common.utils.StringUtils;
import org.solovyev.common.utils.history.HistoryAction;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
@ -387,111 +383,24 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
@Override
public void onClick(View v) {
if (v instanceof CalculatorDisplay) {
final CalculatorDisplay cd = (CalculatorDisplay)v;
if (cd.isValid()) {
switch (cd.getJsclOperation()) {
case simplify:
final Generic genericResult = cd.getGenericResult();
if ( genericResult != null ) {
final Set<Constant> notSystemConstants = new HashSet<Constant>();
for (Constant constant : genericResult.getConstants()) {
IConstant var = CalculatorEngine.instance.getVarsRegistry().get(constant.getName());
if (var != null && !var.isSystem() && !var.isDefined()) {
notSystemConstants.add(constant);
}
}
if (v instanceof CalculatorDisplay) {
final CalculatorDisplay cd = (CalculatorDisplay) v;
if ( notSystemConstants.size() > 0 ) {
if (notSystemConstants.size() > 1) {
copyResult(activity, cd);
} else {
final AMenuBuilder<CalculatorDisplayMenuItem, CalculatorDisplayMenuData> menuBuilder = AMenuBuilder.newInstance(activity, CalculatorDisplayMenuItem.class);
menuBuilder.create(new CalculatorDisplayMenuData(cd, genericResult, notSystemConstants)).show();
}
} else {
copyResult(activity, cd);
}
} else {
copyResult(activity, cd);
}
break;
case elementary:
copyResult(activity, cd);
break;
case numeric:
copyResult(activity, cd);
break;
}
} else {
final String errorMessage = cd.getErrorMessage();
if ( errorMessage != null ) {
showEvaluationError(activity, errorMessage);
}
}
}
}
}
private static class CalculatorDisplayMenuData {
@NotNull
private final CalculatorDisplay display;
@NotNull
private final Generic result;
@NotNull
private final Set<Constant> notSystemConstants;
private CalculatorDisplayMenuData(@NotNull CalculatorDisplay display, @NotNull Generic result, @NotNull Set<Constant> notSystemConstants) {
this.display = display;
this.result = result;
this.notSystemConstants = notSystemConstants;
}
@NotNull
public CalculatorDisplay getDisplay() {
return display;
}
@NotNull
public Generic getResult() {
return result;
}
@NotNull
public Set<Constant> getNotSystemConstants() {
return notSystemConstants;
}
}
private static enum CalculatorDisplayMenuItem implements AMenuItem<CalculatorDisplayMenuData> {
plot(R.string.c_plot){
@Override
public void doAction(@NotNull CalculatorDisplayMenuData data, @NotNull Context context) {
final Constant constant = CollectionsUtils.getFirstCollectionElement(data.getNotSystemConstants());
assert constant != null;
CalculatorActivityLauncher.plotGraph(context, data.getResult(), constant);
}
},
copy(R.string.c_copy){
@Override
public void doAction(@NotNull CalculatorDisplayMenuData data, @NotNull Context context) {
copyResult(context, data.getDisplay());
}
};
private final int captionId;
CalculatorDisplayMenuItem(@NotNull int captionId) {
this.captionId = captionId;
}
@NotNull
@Override
public String getCaption(@NotNull Context context) {
return context.getString(captionId);
}
}
if (cd.isValid()) {
final List<CalculatorDisplay.MenuItem> filteredMenuItems = new ArrayList<CalculatorDisplay.MenuItem>(CalculatorDisplay.MenuItem.values().length);
for (CalculatorDisplay.MenuItem menuItem : CalculatorDisplay.MenuItem.values()) {
if (menuItem.isItemVisible(cd)) {
filteredMenuItems.add(menuItem);
}
}
AMenuBuilder.newInstance(activity, MenuImpl.newInstance(filteredMenuItems)).create(cd).show();
} else {
final String errorMessage = cd.getErrorMessage();
if (errorMessage != null) {
showEvaluationError(activity, errorMessage);
}
}
}
}
}
}

View File

@ -8,6 +8,7 @@ package org.solovyev.android.calculator.about;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
@ -52,9 +53,11 @@ public class CalculatorReleaseNotesActivity extends Activity {
final String releaseNotesForTitle = context.getString(R.string.c_release_notes_for_title);
final int version = AndroidUtils.getAppVersionCode(context, CalculatorActivity.class.getPackage().getName());
final TextHelper textHelper = new TextHelper(context.getResources(), R.class.getPackage().getName());
boolean first = true;
for ( int i = version; i >= minVersion; i-- ) {
String releaseNotesForVersion = ResourceCache.instance.getCaption("c_release_notes_for_" + i);
String releaseNotesForVersion = textHelper.getText("c_release_notes_for_" + i);
if (!StringUtils.isEmpty(releaseNotesForVersion)){
assert releaseNotesForVersion != null;
if ( !first ) {

View File

@ -0,0 +1,37 @@
package org.solovyev.android.calculator.about;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 4/20/12
* Time: 3:31 PM
*/
public class TextHelper {
@NotNull
public String packageName;
@NotNull
public Resources resources;
public TextHelper(@NotNull Resources resources, @NotNull String packageName) {
this.packageName = packageName;
this.resources = resources;
}
@Nullable
public String getText(@NotNull String stringName) {
final int stringId = this.resources.getIdentifier(stringName, "string", this.packageName);
try {
return resources.getString(stringId);
} catch (Resources.NotFoundException e) {
return null;
}
}
}

View File

@ -14,6 +14,8 @@ import org.jetbrains.annotations.Nullable;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.solovyev.android.ResourceCache;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.about.TextHelper;
import org.solovyev.common.definitions.IBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry;
@ -56,7 +58,7 @@ public abstract class AbstractAndroidMathRegistry<T extends MathEntity, P extend
stringName = prefix + substitute;
}
return ResourceCache.instance.getCaption(stringName);
return new TextHelper(context.getResources(), R.class.getPackage().getName()).getText(stringName);
}
public synchronized void load(@Nullable Context context, @Nullable SharedPreferences preferences) {

View File

@ -0,0 +1,9 @@
package org.solovyev.android.calculator.view;
/**
* User: serso
* Date: 4/20/12
* Time: 4:50 PM
*/
public class UnitsConverter {
}