Digits are numeral base dependant now (show/hide digit buttons)
This commit is contained in:
@@ -1,83 +0,0 @@
|
||||
package org.solovyev.android;
|
||||
|
||||
import jscl.NumeralBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 4/21/12
|
||||
* Time: 8:00 PM
|
||||
*/
|
||||
public enum NumeralBaseUnitType implements UnitType<String> {
|
||||
|
||||
bin(NumeralBase.bin),
|
||||
oct(NumeralBase.oct),
|
||||
dec(NumeralBase.dec),
|
||||
hex(NumeralBase.hex);
|
||||
|
||||
@NotNull
|
||||
private final NumeralBase numeralBase;
|
||||
|
||||
private NumeralBaseUnitType(@NotNull NumeralBase numeralBase) {
|
||||
this.numeralBase = numeralBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Unit<String> createUnit(@NotNull String value) {
|
||||
return UnitImpl.newInstance(value, this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Class<String> getUnitValueClass() {
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final Converter converter = new Converter();
|
||||
|
||||
@NotNull
|
||||
public static Converter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
public static class Converter implements UnitConverter<String> {
|
||||
|
||||
private Converter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<String> to) {
|
||||
return NumeralBaseUnitType.class.isAssignableFrom(from.getClass()) && NumeralBaseUnitType.class.isAssignableFrom(to.getClass());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Unit<String> convert(@NotNull Unit<?> from, @NotNull UnitType<String> toType) {
|
||||
if (!isSupported(from.getUnitType(), toType)) {
|
||||
throw new IllegalArgumentException("Types are not supported!");
|
||||
}
|
||||
|
||||
final NumeralBaseUnitType fromType = (NumeralBaseUnitType) from.getUnitType();
|
||||
final NumeralBase fromNumeralBase = fromType.numeralBase;
|
||||
final NumeralBase toNumeralBase = ((NumeralBaseUnitType) toType).numeralBase;
|
||||
final String fromValue = (String) from.getValue();
|
||||
|
||||
final BigInteger decBigInteger = fromNumeralBase.toBigInteger(fromValue);
|
||||
return UnitImpl.newInstance(toNumeralBase.toString(decBigInteger), (NumeralBaseUnitType) toType);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static NumeralBaseUnitType valueOf(@NotNull NumeralBase nb ) {
|
||||
for (NumeralBaseUnitType numeralBaseUnitType : values()) {
|
||||
if ( numeralBaseUnitType.numeralBase == nb ) {
|
||||
return numeralBaseUnitType;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(nb + " is not supported numeral base!");
|
||||
}
|
||||
}
|
@@ -0,0 +1,153 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.app.Activity;
|
||||
import jscl.NumeralBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.Unit;
|
||||
import org.solovyev.android.UnitConverter;
|
||||
import org.solovyev.android.UnitImpl;
|
||||
import org.solovyev.android.UnitType;
|
||||
import org.solovyev.android.view.drag.DirectionDragButton;
|
||||
import org.solovyev.android.view.drag.DragDirection;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 4/21/12
|
||||
* Time: 8:00 PM
|
||||
*/
|
||||
public enum AndroidNumeralBase implements UnitType<String> {
|
||||
|
||||
bin(NumeralBase.bin) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Integer> getButtonIds() {
|
||||
return Arrays.asList(R.id.zeroDigitButton, R.id.oneDigitButton);
|
||||
}
|
||||
},
|
||||
|
||||
oct(NumeralBase.oct) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Integer> getButtonIds() {
|
||||
final List<Integer> result = new ArrayList<Integer>(bin.getButtonIds());
|
||||
result.addAll(Arrays.asList(R.id.twoDigitButton, R.id.threeDigitButton, R.id.fourDigitButton, R.id.fiveDigitButton, R.id.sixDigitButton, R.id.sevenDigitButton));
|
||||
return result;
|
||||
}
|
||||
},
|
||||
|
||||
dec(NumeralBase.dec) {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Integer> getButtonIds() {
|
||||
final List<Integer> result = new ArrayList<Integer>(oct.getButtonIds());
|
||||
result.addAll(Arrays.asList(R.id.eightDigitButton, R.id.nineDigitButton));
|
||||
return result;
|
||||
}
|
||||
},
|
||||
|
||||
hex(NumeralBase.hex) {
|
||||
|
||||
@NotNull
|
||||
private List<Integer> specialHexButtonIds = Arrays.asList(R.id.oneDigitButton, R.id.twoDigitButton, R.id.threeDigitButton, R.id.fourDigitButton, R.id.fiveDigitButton, R.id.sixDigitButton);
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Integer> getButtonIds() {
|
||||
return dec.getButtonIds();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void toggleButton(boolean show, @NotNull DirectionDragButton button) {
|
||||
super.toggleButton(show, button);
|
||||
if (specialHexButtonIds.contains(button.getId())) {
|
||||
button.showDirectionText(show, DragDirection.left);
|
||||
button.invalidate();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@NotNull
|
||||
private final NumeralBase numeralBase;
|
||||
|
||||
private AndroidNumeralBase(@NotNull NumeralBase numeralBase) {
|
||||
this.numeralBase = numeralBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Unit<String> createUnit(@NotNull String value) {
|
||||
return UnitImpl.newInstance(value, this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public abstract List<Integer> getButtonIds();
|
||||
|
||||
public void toggleButtons(boolean show, @NotNull Activity activity) {
|
||||
for (Integer buttonId : getButtonIds()) {
|
||||
final DirectionDragButton button = (DirectionDragButton) activity.findViewById(buttonId);
|
||||
if (button != null) {
|
||||
toggleButton(show, button);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void toggleButton(boolean show, @NotNull DirectionDragButton button) {
|
||||
button.setShowText(show);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Class<String> getUnitValueClass() {
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final Converter converter = new Converter();
|
||||
|
||||
@NotNull
|
||||
public static Converter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
public static class Converter implements UnitConverter<String> {
|
||||
|
||||
private Converter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<String> to) {
|
||||
return AndroidNumeralBase.class.isAssignableFrom(from.getClass()) && AndroidNumeralBase.class.isAssignableFrom(to.getClass());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Unit<String> convert(@NotNull Unit<?> from, @NotNull UnitType<String> toType) {
|
||||
if (!isSupported(from.getUnitType(), toType)) {
|
||||
throw new IllegalArgumentException("Types are not supported!");
|
||||
}
|
||||
|
||||
final AndroidNumeralBase fromTypeAndroid = (AndroidNumeralBase) from.getUnitType();
|
||||
final NumeralBase fromNumeralBase = fromTypeAndroid.numeralBase;
|
||||
final NumeralBase toNumeralBase = ((AndroidNumeralBase) toType).numeralBase;
|
||||
final String fromValue = (String) from.getValue();
|
||||
|
||||
final BigInteger decBigInteger = fromNumeralBase.toBigInteger(fromValue);
|
||||
return UnitImpl.newInstance(toNumeralBase.toString(decBigInteger), (AndroidNumeralBase) toType);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AndroidNumeralBase valueOf(@NotNull NumeralBase nb) {
|
||||
for (AndroidNumeralBase androidNumeralBase : values()) {
|
||||
if (androidNumeralBase.numeralBase == nb) {
|
||||
return androidNumeralBase;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(nb + " is not supported numeral base!");
|
||||
}
|
||||
}
|
@@ -202,13 +202,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
toggleButtonDirectionText(R.id.plusButton, false, DragDirection.down, DragDirection.up);
|
||||
}
|
||||
|
||||
numeralBaseButtons.clear();
|
||||
numeralBaseButtons.addButtonId(R.id.oneDigitButton);
|
||||
numeralBaseButtons.addButtonId(R.id.twoDigitButton);
|
||||
numeralBaseButtons.addButtonId(R.id.threeDigitButton);
|
||||
numeralBaseButtons.addButtonId(R.id.fourDigitButton);
|
||||
numeralBaseButtons.addButtonId(R.id.fiveDigitButton);
|
||||
numeralBaseButtons.addButtonId(R.id.sixDigitButton);
|
||||
numeralBaseButtons.toggleNumericDigits(this, preferences);
|
||||
|
||||
toggleEqualsButton(preferences);
|
||||
@@ -549,8 +542,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
@SuppressWarnings({"UnusedDeclaration"})
|
||||
public void digitButtonClickHandler(@NotNull View v) {
|
||||
Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed());
|
||||
calculatorModel.processDigitButtonAction(((DirectionDragButton) v).getText().toString());
|
||||
}
|
||||
if (((ColorButton) v).isShowText()) {
|
||||
calculatorModel.processDigitButtonAction(((ColorButton) v).getText().toString());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({"UnusedDeclaration"})
|
||||
public void functionsButtonClickHandler(@NotNull View v) {
|
||||
|
@@ -5,11 +5,6 @@ import android.content.SharedPreferences;
|
||||
import jscl.NumeralBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||
import org.solovyev.android.view.drag.DirectionDragButton;
|
||||
import org.solovyev.android.view.drag.DragDirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
@@ -18,46 +13,18 @@ import java.util.List;
|
||||
*/
|
||||
public class NumeralBaseButtons {
|
||||
|
||||
@NotNull
|
||||
private final List<Integer> buttonIds = new ArrayList<Integer>();
|
||||
|
||||
public void addButton(@NotNull DirectionDragButton button) {
|
||||
buttonIds.add(button.getId());
|
||||
}
|
||||
|
||||
public void addButtonId(@NotNull Integer buttonId) {
|
||||
buttonIds.add(buttonId);
|
||||
}
|
||||
|
||||
public synchronized void removeNumeralDigits(@NotNull Activity activity) {
|
||||
for (Integer id : buttonIds) {
|
||||
final DirectionDragButton button = (DirectionDragButton) activity.findViewById(id);
|
||||
if (button != null) {
|
||||
button.showDirectionText(false, DragDirection.left);
|
||||
button.invalidate();
|
||||
public synchronized void toggleNumericDigits(@NotNull Activity activity, @NotNull NumeralBase currentNumeralBase) {
|
||||
for (NumeralBase numeralBase : NumeralBase.values()) {
|
||||
if ( currentNumeralBase != numeralBase ) {
|
||||
AndroidNumeralBase.valueOf(numeralBase).toggleButtons(false, activity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void showNumeralDigits(@NotNull Activity activity) {
|
||||
for (Integer id : buttonIds) {
|
||||
final DirectionDragButton button = (DirectionDragButton) activity.findViewById(id);
|
||||
if (button != null) {
|
||||
button.showDirectionText(true, DragDirection.left);
|
||||
button.invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
buttonIds.clear();
|
||||
AndroidNumeralBase.valueOf(currentNumeralBase).toggleButtons(true, activity);
|
||||
}
|
||||
|
||||
public synchronized void toggleNumericDigits(@NotNull Activity activity, @NotNull SharedPreferences preferences) {
|
||||
if (CalculatorEngine.Preferences.numeralBase.getPreference(preferences) != NumeralBase.hex) {
|
||||
this.removeNumeralDigits(activity);
|
||||
} else {
|
||||
this.showNumeralDigits(activity);
|
||||
}
|
||||
final NumeralBase nb = CalculatorEngine.Preferences.numeralBase.getPreference(preferences);
|
||||
this.toggleNumericDigits(activity, nb);
|
||||
}
|
||||
}
|
||||
|
@@ -7,7 +7,7 @@ import android.view.WindowManager;
|
||||
import jscl.NumeralBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.NumeralBaseUnitType;
|
||||
import org.solovyev.android.calculator.AndroidNumeralBase;
|
||||
import org.solovyev.android.Unit;
|
||||
import org.solovyev.android.UnitImpl;
|
||||
import org.solovyev.android.calculator.CalculatorModel;
|
||||
@@ -36,22 +36,22 @@ public class NumeralBaseConverterDialog {
|
||||
|
||||
public void show(@NotNull Context context) {
|
||||
final UnitConverterViewBuilder b = new UnitConverterViewBuilder();
|
||||
b.setFromUnitTypes(Arrays.asList(NumeralBaseUnitType.values()));
|
||||
b.setToUnitTypes(Arrays.asList(NumeralBaseUnitType.values()));
|
||||
b.setFromUnitTypes(Arrays.asList(AndroidNumeralBase.values()));
|
||||
b.setToUnitTypes(Arrays.asList(AndroidNumeralBase.values()));
|
||||
|
||||
if (!StringUtils.isEmpty(initialFromValue)) {
|
||||
String value = initialFromValue;
|
||||
try {
|
||||
value = ToJsclTextProcessor.getInstance().process(value).getExpression();
|
||||
b.setFromValue(UnitImpl.newInstance(value, NumeralBaseUnitType.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
||||
b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
||||
} catch (CalculatorParseException e) {
|
||||
b.setFromValue(UnitImpl.newInstance(value, NumeralBaseUnitType.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
||||
b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
||||
}
|
||||
} else {
|
||||
b.setFromValue(UnitImpl.newInstance("", NumeralBaseUnitType.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
||||
b.setFromValue(UnitImpl.newInstance("", AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
||||
}
|
||||
|
||||
b.setConverter(NumeralBaseUnitType.getConverter());
|
||||
b.setConverter(AndroidNumeralBase.getConverter());
|
||||
|
||||
final MutableObject<AlertDialog> alertDialogHolder = new MutableObject<AlertDialog>();
|
||||
b.setOkButtonOnClickListener(new View.OnClickListener() {
|
||||
@@ -69,9 +69,9 @@ public class NumeralBaseConverterDialog {
|
||||
public void onClick(@NotNull Unit<String> fromUnits, @NotNull Unit<String> toUnits) {
|
||||
String toUnitsValue = toUnits.getValue();
|
||||
|
||||
if (!toUnits.getUnitType().equals(NumeralBaseUnitType.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) {
|
||||
if (!toUnits.getUnitType().equals(AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) {
|
||||
for (NumeralBase nb : NumeralBase.values()) {
|
||||
if (NumeralBaseUnitType.valueOf(nb).equals(toUnits.getUnitType())) {
|
||||
if (AndroidNumeralBase.valueOf(nb).equals(toUnits.getUnitType())) {
|
||||
toUnitsValue = nb.getJsclPrefix() + toUnitsValue;
|
||||
break;
|
||||
}
|
||||
|
Reference in New Issue
Block a user