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_empty_var_error">Unable to create empty constant!</string>
<string name="c_not_valid_result">Current result is not valid!</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_plot_graph">Graph</string>
<string name="c_min_x_value">From</string> <string name="c_min_x_value">From</string>
<string name="c_max_x_value">To</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.Generic;
import jscl.math.function.Constant; import jscl.math.function.Constant;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
import jscl.math.numeric.Numeric;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.jscl.JsclOperation; 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.CalculatorParseException;
import org.solovyev.android.calculator.model.TextProcessor; import org.solovyev.android.calculator.model.TextProcessor;
import org.solovyev.android.calculator.view.TextHighlighter; import org.solovyev.android.calculator.view.TextHighlighter;
import org.solovyev.android.calculator.view.UnitsConverter;
import org.solovyev.android.menu.AMenuItem; import org.solovyev.android.menu.AMenuItem;
import org.solovyev.android.view.AutoResizeTextView; import org.solovyev.android.view.AutoResizeTextView;
import org.solovyev.common.utils.CollectionsUtils;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -33,24 +36,37 @@ import java.util.Set;
*/ */
public class CalculatorDisplay extends AutoResizeTextView implements ICalculatorDisplay{ public class CalculatorDisplay extends AutoResizeTextView implements ICalculatorDisplay{
public static enum Menu implements AMenuItem<CalculatorDisplay> { public static enum MenuItem implements AMenuItem<CalculatorDisplay> {
to_bin(0) {
copy(R.string.c_copy) {
@Override @Override
public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) { public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) {
//To change body of implemented methods use File | Settings | File Templates. CalculatorModel.copyResult(context, data);
}
},
convert(R.string.c_convert) {
@Override
public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) {
// todo serso: continue
new UnitsConverter();
} }
@Override @Override
protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) { protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
return false; return operation == JsclOperation.numeric && generic.getConstants().isEmpty();
} }
}, },
plot(R.string.c_plot_graph) { plot(R.string.c_plot) {
@Override @Override
public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) { public void doAction(@NotNull CalculatorDisplay data, @NotNull Context context) {
final Generic generic = data.getGenericResult();
assert generic != null;
//To change body of implemented methods use File | Settings | File Templates. final Constant constant = CollectionsUtils.getFirstCollectionElement(getNotSystemConstants(generic));
assert constant != null;
CalculatorActivityLauncher.plotGraph(context, generic, constant);
} }
@Override @Override
@ -58,7 +74,18 @@ public class CalculatorDisplay extends AutoResizeTextView implements ICalculator
boolean result = false; boolean result = false;
if (operation == JsclOperation.simplify) { if (operation == JsclOperation.simplify) {
if (getNotSystemConstants(generic).size() == 1) {
result = true;
}
}
return result;
}
@NotNull
private Set<Constant> getNotSystemConstants(@NotNull Generic generic) {
final Set<Constant> notSystemConstants = new HashSet<Constant>(); final Set<Constant> notSystemConstants = new HashSet<Constant>();
for (Constant constant : generic.getConstants()) { for (Constant constant : generic.getConstants()) {
IConstant var = CalculatorEngine.instance.getVarsRegistry().get(constant.getName()); IConstant var = CalculatorEngine.instance.getVarsRegistry().get(constant.getName());
if (var != null && !var.isSystem() && !var.isDefined()) { if (var != null && !var.isSystem() && !var.isDefined()) {
@ -66,18 +93,13 @@ public class CalculatorDisplay extends AutoResizeTextView implements ICalculator
} }
} }
if (notSystemConstants.size() == 1) { return notSystemConstants;
result = true;
}
}
return result;
} }
}; };
private final int captionId; private final int captionId;
Menu(int captionId) { MenuItem(int captionId) {
this.captionId = captionId; this.captionId = captionId;
} }

View File

@ -17,13 +17,9 @@ import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; 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.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.CursorControl; import org.solovyev.android.CursorControl;
import org.solovyev.android.history.HistoryControl;
import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistory;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.history.TextViewEditorAdapter; 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.CalculatorEngine;
import org.solovyev.android.calculator.model.CalculatorEvalException; import org.solovyev.android.calculator.model.CalculatorEvalException;
import org.solovyev.android.calculator.model.CalculatorParseException; import org.solovyev.android.calculator.model.CalculatorParseException;
import org.solovyev.android.history.HistoryControl;
import org.solovyev.android.menu.AMenuBuilder; 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.msg.Message;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.MutableObject; import org.solovyev.common.utils.MutableObject;
import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.StringUtils;
import org.solovyev.common.utils.history.HistoryAction; import org.solovyev.common.utils.history.HistoryAction;
import java.util.HashSet; import java.util.ArrayList;
import java.util.Set; import java.util.List;
/** /**
* User: serso * User: serso
@ -388,110 +384,23 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (v instanceof CalculatorDisplay) { if (v instanceof CalculatorDisplay) {
final CalculatorDisplay cd = (CalculatorDisplay)v; 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 ( notSystemConstants.size() > 0 ) { if (cd.isValid()) {
if (notSystemConstants.size() > 1) { final List<CalculatorDisplay.MenuItem> filteredMenuItems = new ArrayList<CalculatorDisplay.MenuItem>(CalculatorDisplay.MenuItem.values().length);
copyResult(activity, cd); for (CalculatorDisplay.MenuItem menuItem : CalculatorDisplay.MenuItem.values()) {
} else { if (menuItem.isItemVisible(cd)) {
final AMenuBuilder<CalculatorDisplayMenuItem, CalculatorDisplayMenuData> menuBuilder = AMenuBuilder.newInstance(activity, CalculatorDisplayMenuItem.class); filteredMenuItems.add(menuItem);
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;
} }
AMenuBuilder.newInstance(activity, MenuImpl.newInstance(filteredMenuItems)).create(cd).show();
} else { } else {
final String errorMessage = cd.getErrorMessage(); final String errorMessage = cd.getErrorMessage();
if ( errorMessage != null ) { if (errorMessage != null) {
showEvaluationError(activity, errorMessage); 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);
}
}
} }

View File

@ -8,6 +8,7 @@ package org.solovyev.android.calculator.about;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod; 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 String releaseNotesForTitle = context.getString(R.string.c_release_notes_for_title);
final int version = AndroidUtils.getAppVersionCode(context, CalculatorActivity.class.getPackage().getName()); final int version = AndroidUtils.getAppVersionCode(context, CalculatorActivity.class.getPackage().getName());
final TextHelper textHelper = new TextHelper(context.getResources(), R.class.getPackage().getName());
boolean first = true; boolean first = true;
for ( int i = version; i >= minVersion; i-- ) { 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)){ if (!StringUtils.isEmpty(releaseNotesForVersion)){
assert releaseNotesForVersion != null; assert releaseNotesForVersion != null;
if ( !first ) { 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.Serializer;
import org.simpleframework.xml.core.Persister; import org.simpleframework.xml.core.Persister;
import org.solovyev.android.ResourceCache; 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.definitions.IBuilder;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
@ -56,7 +58,7 @@ public abstract class AbstractAndroidMathRegistry<T extends MathEntity, P extend
stringName = prefix + substitute; 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) { 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 {
}