JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
ParamBars.java
Go to the documentation of this file.
1 package jarmos.app;
2 
3 import jarmos.Parameters;
4 
5 import java.text.DecimalFormat;
6 import java.util.ArrayList;
7 import java.util.List;
8 
9 import android.app.Activity;
10 import android.app.AlertDialog;
11 import android.app.AlertDialog.Builder;
12 import android.app.Dialog;
13 import android.content.DialogInterface;
14 import android.text.Html;
15 import android.text.InputType;
16 import android.util.Log;
17 import android.view.View;
18 import android.widget.Button;
19 import android.widget.EditText;
20 import android.widget.LinearLayout;
21 import android.widget.SeekBar;
22 import android.widget.TableLayout;
23 import android.widget.TableRow;
24 import android.widget.TableRow.LayoutParams;
25 import android.widget.TextView;
26 import android.widget.Toast;
27 
37 public class ParamBars {
38 
39  private Activity activity;
40 
41  private List<Button> buttons;
42  private List<SeekBar> bars;
43  private List<TextView> labels;
44 
45  private Parameters p;
46 
47  private int mSweepIndex = -1;
48 
49  public ParamBars(Activity activity, Parameters p) {
50  this.activity = activity;
51  this.p = p;
52  }
53 
54  public int getSweepIndex() {
55  return mSweepIndex;
56  }
57 
58  public void createBars(TableLayout parent) {
59  int np = p.getNumParams();
60 
61  // Create String array of parameters to store in the ListView
62  try {
63 
64  // Clear the paramLayout in case we're doing a new problem
65  parent.removeAllViews();
66 
67  buttons = new ArrayList<Button>(np);
68  bars = new ArrayList<SeekBar>(np);
69  labels = new ArrayList<TextView>(np);
70 
71  for (int i = 0; i < np; i++) {
72  TableRow row = new TableRow(activity);
73  row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
74 
75  // First add the text label
76  TextView t = new TextView(activity);
77  t.setTextSize(15); // Size is in scaled pixels
78  t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
79  TableRow.LayoutParams.WRAP_CONTENT));
80  t.setPadding(0, 0, 4, 0);
81  labels.add(t);
82  row.addView(t);
83 
84  // Next add the SeekBar
85  SeekBar b = new SeekBar(activity);
86  b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
87  b.setPadding(10, 10, 10, 0); // Set 10px padding on
88  // Also set param bars to match current param
89  int prog = (int) Math.round(100 * p.getCurrent()[i] / (p.getMaxValue(i) - p.getMinValue(i)));
90  b.setProgress(prog);
91  // left and right
92  bars.add(b);
93  row.addView(b);
94 
95  // Finally add the parameter value text
96  Button btn = new Button(activity);
97  btn.setOnClickListener(new View.OnClickListener() {
98  public void onClick(View v) {
99  showParamValueSetDialog(buttons.indexOf(v));
100  }
101  });
102  buttons.add(btn);
103  row.addView(btn);
104 
105  displayParamValue(i, p.getCurrent()[i]);
106 
107  parent.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
108  }
109  } catch (Exception e) {
110  Log.e("RBActivity", "Failed init param bars", e);
111  e.printStackTrace();
112  }
113 
114  addParamBarListeners();
115  }
116 
117  public void createSweepButton(LinearLayout parent) {
118  parent.removeAllViews();
119  Button sweepButton = new Button(activity);
120  sweepButton.setText("\u00B5 Sweep");
121  sweepButton.setTextSize(22);
122  sweepButton.setOnClickListener(new View.OnClickListener() {
123  public void onClick(View view) {
124  showParamSweepDialog();
125  }
126  });
127  parent.addView(sweepButton, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
128  }
129 
130  private void displayParamValue(int index, double current_param) {
131  String current_param_str;
132  double abs = Math.abs(current_param);
133  if ((abs < 0.1) && (current_param != 0.)) {
134  DecimalFormat decimal_format = new DecimalFormat("0.###E0");
135  current_param_str = decimal_format.format(current_param);
136  } else if ((abs < 1) && (abs >= 0)) {
137  DecimalFormat decimal_format = new DecimalFormat("@@@");
138  current_param_str = decimal_format.format(current_param);
139  } else {
140  DecimalFormat decimal_format = new DecimalFormat("@@@@");
141  current_param_str = decimal_format.format(current_param);
142  }
143 
144  labels.get(index).setText(Html.fromHtml(p.getLabel(index)));
145  buttons.get(index).setText(Html.fromHtml(current_param_str));
146  }
147 
148  private void addParamBarListeners() {
149  // Add a listener to each SeekBar
150  for (int i = 0; i < p.getNumParams(); i++) {
151  bars.get(i).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
152 
153  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
154  if (fromUser) {
155  int index = bars.indexOf(seekBar);
156 
157  double param_range = p.getMaxValue(index) - p.getMinValue(index);
158 
159  double current_param = p.getMinValue(index) + param_range * progress / seekBar.getMax();
160 
161  p.setCurrent(index, current_param);
162 
163  displayParamValue(index, current_param);
164  }
165  }
166 
167  public void onStartTrackingTouch(SeekBar seekBar) {
168  }
169 
170  public void onStopTrackingTouch(SeekBar seekBar) {
171  }
172  });
173  }
174 
175  }
176 
177  private void showParamValueSetDialog(final int index) {
178  final Dialog dialog = new Dialog(activity);
179  dialog.setContentView(R.layout.rb_param_dialog);
180  dialog.setTitle("Minimum: " + p.getMinValue(index) + " Maximum: " + p.getMaxValue(index));
181  dialog.setCancelable(false);
182 
183  final EditText paramInputField = (EditText) dialog.findViewById(R.id.param_input_textview);
184 
185  // field should accept signed doubles only
186  paramInputField.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL
187  | InputType.TYPE_NUMBER_FLAG_SIGNED);
188 
189  // user-submitted parameter value will be handled when the ok button
190  // is pressed
191  Button okButton2 = (Button) dialog.findViewById(R.id.param_okButton);
192  okButton2.setOnClickListener(new View.OnClickListener() {
193 
194  public void onClick(View view) {
195  // determine if value in input field is within acceptable
196  // range
197  String userParamString = paramInputField.getText().toString();
198  double userParam;
199  try {
200  userParam = Double.parseDouble(userParamString);
201  } catch (NumberFormatException e) {
202  // if user submits non-double, default value is out of
203  // bounds to trigger toast
204  userParam = p.getMinValue(index) - 1;
205  }
206 
207  if (userParam <= p.getMaxValue(index) && userParam >= p.getMinValue(index)) {
208  // update parameter bars
209  double slopeVal = (100 / (p.getMaxValue(index) - p.getMinValue(index)));
210  Double progressVal = Double.valueOf((slopeVal * userParam) - (p.getMinValue(index) * slopeVal));
211  bars.get(index).setProgress(progressVal.intValue());
212 
213  // call displayParamValue to update parameter value
214  displayParamValue(index, userParam);
215  } else {
216  Toast.makeText(activity.getApplicationContext(), "Invalid Value", Toast.LENGTH_SHORT).show();
217  }
218 
219  dialog.dismiss();
220  }
221 
222  });
223  dialog.show();
224  }
225 
226  private void showParamSweepDialog() {
227  final int np = p.getNumParams();
228  try {
229  final String[] paramStrings = new String[np + 1];
230 
231  paramStrings[0] = "No Sweep";
232  for (int i = 0; i < paramStrings.length; i++) {
233  if (i > 0) {
234  paramStrings[i] = "Parameter " + i;
235  }
236  }
237 
238  Builder builder = new AlertDialog.Builder(activity);
239  builder.setTitle("Pick sweep parameter");
240  builder.setItems(paramStrings, new DialogInterface.OnClickListener() {
241 
242  public void onClick(DialogInterface dialog, int item) {
243  // Show a Toast for the selected item
244  Toast.makeText(activity.getApplicationContext(), paramStrings[item], Toast.LENGTH_SHORT).show();
245  mSweepIndex = item - 1;
246 
247  // disable selected slider, enable all others
248  // set disabled slider's progress to 0, all
249  // others to old values
250  for (int i = 0; i < np; i++) {
251  bars.get(i).setEnabled(true);
252  double slopeVal = (100 / (p.getMaxValue(i) - p.getMinValue(i)));
253  Double progressVal = Double.valueOf((slopeVal * p.getCurrent()[i])
254  - (p.getMinValue(i) * slopeVal));
255  bars.get(i).setProgress(progressVal.intValue());
256  }
257  if (mSweepIndex > -1) {
258  bars.get(mSweepIndex).setProgress(0);
259  bars.get(mSweepIndex).setEnabled(false);
260  }
261  for (int i = 0; i < np; i++) {
262  displayParamValue(i, p.getCurrent()[i]);
263  buttons.get(i).setEnabled(true);
264  }
265  if (mSweepIndex > -1) {
266  buttons.get(mSweepIndex).setText("Sweep");
267  buttons.get(mSweepIndex).setEnabled(false);
268  }
269  dialog.dismiss();
270  }
271  });
272 
273  final Dialog dialog = builder.create();
274  dialog.show();
275  } catch (Exception e) {
276  Log.e("ParamBars", "Exception thrown during creation of Sweep dialog");
277  }
278  }
279 }
String getLabel(int i)
Returns the label for the i-th parameter.
A helper class for a collection of UI elements regarding model parameter display. ...
Definition: ParamBars.java:37
double[] getCurrent()
Gets the current parameter.
Definition: Parameters.java:62
void createSweepButton(LinearLayout parent)
Definition: ParamBars.java:117
ParamBars(Activity activity, Parameters p)
Definition: ParamBars.java:49
double getMinValue(int i)
A class for model parameters.
Definition: Parameters.java:17
void createBars(TableLayout parent)
Definition: ParamBars.java:58
double getMaxValue(int i)