diff --git a/calculatorpp/project.properties b/calculatorpp/project.properties
index febda8cf..7be8ede0 100644
--- a/calculatorpp/project.properties
+++ b/calculatorpp/project.properties
@@ -10,5 +10,5 @@
# Project target.
target=android-15
android.library.reference.1=../calculatorpp-service
-android.library.reference.2=gen-external-apklibs/org.solovyev.android_common_0.1.3
+android.library.reference.2=gen-external-apklibs/org.solovyev.android_common_0.1.4
diff --git a/calculatorpp/res/layout/unit_converter.xml b/calculatorpp/res/layout/unit_converter.xml
index 5c4a3273..878af88a 100644
--- a/calculatorpp/res/layout/unit_converter.xml
+++ b/calculatorpp/res/layout/unit_converter.xml
@@ -51,21 +51,25 @@
diff --git a/calculatorpp/res/values-ru/text_strings.xml b/calculatorpp/res/values-ru/text_strings.xml
index f23c92af..caabd673 100644
--- a/calculatorpp/res/values-ru/text_strings.xml
+++ b/calculatorpp/res/values-ru/text_strings.xml
@@ -199,4 +199,8 @@
Перевести в…
+ Перевести в hex
+ Перевести в bin
+ Перевести в dec
+
\ No newline at end of file
diff --git a/calculatorpp/res/values-uk/text_strings.xml b/calculatorpp/res/values-uk/text_strings.xml
index 37e7329b..48a43eb9 100644
--- a/calculatorpp/res/values-uk/text_strings.xml
+++ b/calculatorpp/res/values-uk/text_strings.xml
@@ -199,4 +199,8 @@
Перевести в…
+ Перевести в hex
+ Перевести в bin
+ Перевести в dec
+
diff --git a/calculatorpp/res/values/text_strings.xml b/calculatorpp/res/values/text_strings.xml
index af8ed715..e3a621ed 100644
--- a/calculatorpp/res/values/text_strings.xml
+++ b/calculatorpp/res/values/text_strings.xml
@@ -199,4 +199,8 @@
Convert to…
+ Convert to hex
+ Convert to bin
+ Convert to dec
+
\ No newline at end of file
diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java
index d7407dec..7e9262fe 100644
--- a/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java
+++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java
@@ -99,6 +99,11 @@ public enum AndroidNumeralBase implements UnitType {
button.setShowText(show);
}
+ @NotNull
+ public NumeralBase getNumeralBase() {
+ return numeralBase;
+ }
+
@NotNull
@Override
public Class getUnitValueClass() {
diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java
index b799e741..8ec44603 100644
--- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java
+++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java
@@ -10,6 +10,7 @@ import android.graphics.Color;
import android.text.Html;
import android.util.AttributeSet;
import android.util.Log;
+import jscl.NumeralBase;
import jscl.math.Generic;
import jscl.math.function.Constant;
import jscl.math.function.IConstant;
@@ -19,11 +20,15 @@ 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.calculator.model.ToJsclTextProcessor;
import org.solovyev.android.calculator.view.NumeralBaseConverterDialog;
import org.solovyev.android.calculator.view.TextHighlighter;
+import org.solovyev.android.calculator.view.UnitConverterViewBuilder;
+import org.solovyev.android.menu.AMenuItem;
import org.solovyev.android.menu.LabeledMenuItem;
import org.solovyev.android.view.AutoResizeTextView;
import org.solovyev.common.utils.CollectionsUtils;
+import org.solovyev.common.utils.StringUtils;
import java.util.HashSet;
import java.util.Set;
@@ -35,7 +40,80 @@ import java.util.Set;
*/
public class CalculatorDisplay extends AutoResizeTextView implements ICalculatorDisplay{
- public static enum MenuItem implements LabeledMenuItem {
+ private static enum ConversionMenuItem implements AMenuItem {
+ convert_to_bin(NumeralBase.bin),
+ convert_to_dec(NumeralBase.dec),
+ convert_to_hex(NumeralBase.hex);
+
+ @NotNull
+ private final NumeralBase toNumeralBase;
+
+ private ConversionMenuItem(@NotNull NumeralBase toNumeralBase) {
+ this.toNumeralBase = toNumeralBase;
+ }
+
+ protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
+ boolean result = false;
+
+ if (operation == JsclOperation.numeric) {
+ if (generic.getConstants().isEmpty()) {
+ try {
+ convert(generic);
+
+ // conversion possible => return true
+ result = true;
+
+ } catch (UnitConverterViewBuilder.ConversionException e) {
+ // conversion is not possible => return false
+ }
+ }
+ }
+
+ return result;
+ }
+
+ @Override
+ public void onClick(@NotNull CalculatorDisplay data, @NotNull Context context) {
+ final NumeralBase fromNumeralBase = CalculatorEngine.instance.getEngine().getNumeralBase();
+
+ String to;
+ try {
+ to = convert(data.getGenericResult());
+
+ // add prefix
+ if (fromNumeralBase != toNumeralBase) {
+ to = toNumeralBase.getJsclPrefix() + to;
+ }
+ } catch (UnitConverterViewBuilder.ConversionException e) {
+ to = context.getString(R.string.c_error);
+ }
+
+ data.setText(to);
+ data.redraw();
+ }
+
+ @NotNull
+ private String convert(@NotNull Generic generic) throws UnitConverterViewBuilder.ConversionException {
+ final NumeralBase fromNumeralBase = CalculatorEngine.instance.getEngine().getNumeralBase();
+
+ if (fromNumeralBase != toNumeralBase) {
+ String from = generic.toString();
+ if (!StringUtils.isEmpty(from)) {
+ try {
+ from = ToJsclTextProcessor.getInstance().process(from).getExpression();
+ } catch (CalculatorParseException e) {
+ // ok, problems while processing occurred
+ }
+ }
+
+ return UnitConverterViewBuilder.doConversion(AndroidNumeralBase.getConverter(), from, AndroidNumeralBase.valueOf(fromNumeralBase), AndroidNumeralBase.valueOf(toNumeralBase));
+ } else {
+ return generic.toString();
+ }
+ }
+ }
+
+ public static enum MenuItem implements LabeledMenuItem {
copy(R.string.c_copy) {
@Override
@@ -44,6 +122,42 @@ public class CalculatorDisplay extends AutoResizeTextView implements ICalculator
}
},
+ convert_to_bin(R.string.convert_to_bin) {
+ @Override
+ public void onClick(@NotNull CalculatorDisplay data, @NotNull Context context) {
+ ConversionMenuItem.convert_to_bin.onClick(data, context);
+ }
+
+ @Override
+ protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
+ return ConversionMenuItem.convert_to_bin.isItemVisibleFor(generic, operation);
+ }
+ },
+
+ convert_to_dec(R.string.convert_to_dec) {
+ @Override
+ public void onClick(@NotNull CalculatorDisplay data, @NotNull Context context) {
+ ConversionMenuItem.convert_to_dec.onClick(data, context);
+ }
+
+ @Override
+ protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
+ return ConversionMenuItem.convert_to_dec.isItemVisibleFor(generic, operation);
+ }
+ },
+
+ convert_to_hex(R.string.convert_to_hex) {
+ @Override
+ public void onClick(@NotNull CalculatorDisplay data, @NotNull Context context) {
+ ConversionMenuItem.convert_to_hex.onClick(data, context);
+ }
+
+ @Override
+ protected boolean isItemVisibleFor(@NotNull Generic generic, @NotNull JsclOperation operation) {
+ return ConversionMenuItem.convert_to_hex.isItemVisibleFor(generic, operation);
+ }
+ },
+
convert(R.string.c_convert) {
@Override
public void onClick(@NotNull CalculatorDisplay data, @NotNull Context context) {
diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java
index 2e3aa43c..bfc9b11c 100644
--- a/calculatorpp/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java
+++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java
@@ -4,12 +4,11 @@ import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
-import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import org.solovyev.android.calculator.AndroidNumeralBase;
import org.solovyev.android.Unit;
import org.solovyev.android.UnitImpl;
+import org.solovyev.android.calculator.AndroidNumeralBase;
import org.solovyev.android.calculator.CalculatorModel;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.CalculatorEngine;
@@ -70,12 +69,7 @@ public class NumeralBaseConverterDialog {
String toUnitsValue = toUnits.getValue();
if (!toUnits.getUnitType().equals(AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) {
- for (NumeralBase nb : NumeralBase.values()) {
- if (AndroidNumeralBase.valueOf(nb).equals(toUnits.getUnitType())) {
- toUnitsValue = nb.getJsclPrefix() + toUnitsValue;
- break;
- }
- }
+ toUnitsValue = ((AndroidNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue;
}
CalculatorModel.instance.processDigitButtonAction(toUnitsValue, false);
diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/view/UnitConverterViewBuilder.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/view/UnitConverterViewBuilder.java
index fc8107c1..c7f3218a 100644
--- a/calculatorpp/src/main/java/org/solovyev/android/calculator/view/UnitConverterViewBuilder.java
+++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/view/UnitConverterViewBuilder.java
@@ -172,11 +172,32 @@ public class UnitConverterViewBuilder implements ViewBuilder {
final EditText toEditText = (EditText) main.findViewById(R.id.units_to);
final String from = fromEditText.getText().toString();
+ try {
+ toEditText.setText(doConversion(converter, from, getFromUnitType(main), getToUnitType(main)));
+ } catch (ConversionException e) {
+ toEditText.setText(context.getString(R.string.c_error));
+ }
+ }
+
+ public static final class ConversionException extends Exception {
+ private ConversionException() {
+ }
+
+ private ConversionException(Throwable throwable) {
+ super(throwable);
+ }
+ }
+
+ @NotNull
+ public static String doConversion(@NotNull UnitConverter converter,
+ @Nullable String from,
+ @NotNull UnitType fromUnitType,
+ @NotNull UnitType toUnitType) throws ConversionException{
+ final String result;
+
if (StringUtils.isEmpty(from)) {
- toEditText.setText("");
+ result = "";
} else {
- final UnitType fromUnitType = getFromUnitType(main);
- final UnitType toUnitType = getToUnitType(main);
String to = null;
try {
@@ -184,11 +205,13 @@ public class UnitConverterViewBuilder implements ViewBuilder {
to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue();
}
} catch (RuntimeException e) {
- to = context.getString(R.string.c_error);
+ throw new ConversionException(e);
}
- toEditText.setText(to);
+ result = to;
}
+
+ return result;
}
@NotNull
diff --git a/pom.xml b/pom.xml
index e8d08254..6318b869 100644
--- a/pom.xml
+++ b/pom.xml
@@ -33,7 +33,7 @@
org.solovyev.android
common
- 0.1.3
+ 0.1.4
apklib