Copyright (c) 2008-2011 Vrije Universiteit, The Netherlands
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the follo...
If you think the Android project interdroid-swan listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package interdroid.swan.ui;
//fromwww.java2s.comimport interdroid.swan.R;
import interdroid.swan.swansong.Expression;
import interdroid.swan.swansong.ExpressionFactory;
import interdroid.swan.swansong.ExpressionParseException;
import interdroid.swan.swansong.HistoryReductionMode;
import interdroid.swan.swansong.MathOperator;
import interdroid.swan.swansong.MathValueExpression;
import interdroid.swan.swansong.ValueExpression;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
publicclass NewMathExpressionDialog extends Activity {
publicstaticfinalint LEFT = 1;
publicstaticfinalint RIGHT = 2;
publicstaticfinalint OPERATOR = 3;
boolean leftActive = false;
boolean rightActive = false;
boolean operatorActive = false;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
setContentView(R.layout.expression_builder_new_math_dialog);
findViewById(R.id.left).setOnClickListener(new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
startActivityForResult(new Intent(getApplicationContext(),
SelectTypedValueDialog.class), LEFT);
}
});
findViewById(R.id.right).setOnClickListener(new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
startActivityForResult(new Intent(getApplicationContext(),
SelectTypedValueDialog.class), RIGHT);
}
});
findViewById(R.id.operator).setOnClickListener(
new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
startActivityForResult(new Intent(
getApplicationContext(),
SelectOperatorDialog.class), OPERATOR);
}
});
findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
// check whether we have a left and a right
// value and an operator, create a new
// comparator expression out of it, then add
// it to the list of expressions.
try {
Expression left = ExpressionFactory
.parse(((Button) findViewById(R.id.left)).getText()
.toString());
Expression right = ExpressionFactory
.parse(((Button) findViewById(R.id.right))
.getText().toString());
MathOperator operator = MathOperator
.parse(((Button) findViewById(R.id.operator))
.getText().toString());
// TODO get a configurable history reduction mode instead of
// default
Expression newExpression = new MathValueExpression(
Expression.LOCATION_SELF, (ValueExpression) left,
operator, (ValueExpression) right,
HistoryReductionMode.DEFAULT_MODE);
Intent result = new Intent();
result.putExtra("Expression", newExpression.toParseString());
// if (name != null && !name.equals("")) {
// result.putExtra("name", name);
// }
setResult(RESULT_OK, result);
finish();
} catch (ExpressionParseException e) {
// TODO: improve this
Toast.makeText(getApplicationContext(), "Failed!",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case LEFT:
((Button) findViewById(R.id.left)).setText(data
.getStringExtra("Expression"));
leftActive = true;
break;
case RIGHT:
((Button) findViewById(R.id.right)).setText(data
.getStringExtra("Expression"));
rightActive = true;
break;
case OPERATOR:
((Button) findViewById(R.id.operator)).setText(data
.getStringExtra("Operator"));
operatorActive = true;
break;
default:
break;
}
findViewById(R.id.ok).setEnabled(
leftActive && rightActive && operatorActive);
}
}
}