new plotter

This commit is contained in:
Sergey Solovyev
2013-01-19 21:41:13 +04:00
parent 220952ef9b
commit 8d2a0458e2
9 changed files with 207 additions and 249 deletions

View File

@@ -184,6 +184,7 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
super.onCreateOptionsMenu(menu, inflater);
final List<IdentifiableMenuItem<MenuItem>> menuItems = new ArrayList<IdentifiableMenuItem<MenuItem>>();
menuItems.add(PlotMenu.range);
menuItems.add(PlotMenu.preferences);
menuItems.add(PlotMenu.functions);
@@ -307,6 +308,13 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
private static enum PlotMenu implements IdentifiableMenuItem<MenuItem> {
range(R.id.menu_plot_range) {
@Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) {
context.startActivity(new Intent(context, CalculatorPlotRangeActivity.class));
}
},
functions(R.id.menu_plot_functions) {
@Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) {

View File

@@ -4,12 +4,9 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Spinner;
@@ -24,208 +21,152 @@ import org.solovyev.android.fragments.FragmentUtils;
public class CalculatorPlotFunctionSettingsActivity extends SherlockFragmentActivity {
private static final String INPUT_FUNCTION_ID = "plot-function-id";
private static final String INPUT_FUNCTION_ID = "plot-function-id";
public static void startActivity(@NotNull Context context, @NotNull PlotFunction plotFunction) {
final Intent intent = new Intent(context, CalculatorPlotFunctionSettingsActivity.class);
intent.putExtra(INPUT_FUNCTION_ID, plotFunction.getXyFunction().getId());
context.startActivity(intent);
}
public static void startActivity(@NotNull Context context, @NotNull PlotFunction plotFunction) {
final Intent intent = new Intent(context, CalculatorPlotFunctionSettingsActivity.class);
intent.putExtra(INPUT_FUNCTION_ID, plotFunction.getXyFunction().getId());
context.startActivity(intent);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cpp_plot_function_settings_dialog);
setContentView(R.layout.cpp_plot_function_settings_dialog);
final Intent intent = getIntent();
final Intent intent = getIntent();
if (intent != null) {
final String plotFunctionId = intent.getStringExtra(INPUT_FUNCTION_ID);
if (intent != null) {
final String plotFunctionId = intent.getStringExtra(INPUT_FUNCTION_ID);
if (plotFunctionId != null) {
final Bundle parameters = new Bundle();
parameters.putString(INPUT_FUNCTION_ID, plotFunctionId);
FragmentUtils.createFragment(this, CalculatorPlotFunctionSettingsFragment.class, R.id.dialog_layout, "plot-function-settings", parameters);
} else {
finish();
}
} else {
finish();
}
}
if (plotFunctionId != null) {
final Bundle parameters = new Bundle();
parameters.putString(INPUT_FUNCTION_ID, plotFunctionId);
FragmentUtils.createFragment(this, CalculatorPlotFunctionSettingsFragment.class, R.id.dialog_layout, "plot-function-settings", parameters);
} else {
finish();
}
} else {
finish();
}
}
public static class CalculatorPlotFunctionSettingsFragment extends CalculatorFragment {
public static class CalculatorPlotFunctionSettingsFragment extends CalculatorFragment {
/*
**********************************************************************
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
public static final int LINE_WIDTH_DELAY = 1000;
public static final int MESSAGE_ID = 1;
@Nullable
private PlotFunction plotFunction;
@Nullable
private PlotFunction plotFunction;
@NotNull
private final CalculatorPlotter plotter = Locator.getInstance().getPlotter();
@NotNull
private final CalculatorPlotter plotter = Locator.getInstance().getPlotter();
public CalculatorPlotFunctionSettingsFragment() {
super(CalculatorFragmentType.plotter_function_settings);
}
@NotNull
private final Handler handler = new Handler() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@Override
public void handleMessage(Message msg) {
if (plotFunction != null) {
switch (msg.what) {
case MESSAGE_ID:
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineWidth(plotFunction.getPlotLineDef(), msg.arg1);
final PlotFunction newPlotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
if(plotter.updateFunction(newPlotFunction)) {
plotFunction = newPlotFunction;
}
break;
}
}
}
};
final String functionId = getArguments().getString(INPUT_FUNCTION_ID);
if (functionId != null) {
plotFunction = Locator.getInstance().getPlotter().getFunctionById(functionId);
if (plotFunction != null) {
getActivity().setTitle(plotFunction.getXyFunction().getExpressionString());
}
}
}
public CalculatorPlotFunctionSettingsFragment() {
super(CalculatorFragmentType.plotter_function_settings);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@Override
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
final String functionId = getArguments().getString(INPUT_FUNCTION_ID);
if (functionId != null) {
plotFunction = Locator.getInstance().getPlotter().getFunctionById(functionId);
if ( plotFunction != null ) {
getActivity().setTitle(plotFunction.getXyFunction().getExpressionString());
}
}
}
final Spinner plotLineColorSpinner = (Spinner) root.findViewById(R.id.cpp_plot_function_line_color_spinner);
final Spinner plotLineColorTypeSpinner = (Spinner) root.findViewById(R.id.cpp_plot_function_line_color_type_spinner);
final Spinner plotLineStyleSpinner = (Spinner) root.findViewById(R.id.cpp_plot_function_line_style_spinner);
final SeekBar plotLineWidthSeekBar = (SeekBar) root.findViewById(R.id.cpp_plot_functions_line_width_seekbar);
final Button okButton = (Button) root.findViewById(R.id.cpp_apply_button);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
plotLineWidthSeekBar.setMax(10);
@Override
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
if (plotFunction != null) {
plotLineWidthSeekBar.setProgress((int) plotFunction.getPlotLineDef().getLineWidth());
plotLineColorSpinner.setSelection(PlotLineColor.valueOf(plotFunction.getPlotLineDef().getLineColor()).ordinal());
plotLineColorTypeSpinner.setSelection(plotFunction.getPlotLineDef().getLineColorType().ordinal());
plotLineStyleSpinner.setSelection(plotFunction.getPlotLineDef().getLineStyle().ordinal());
final Spinner plotLineColorSpinner = (Spinner)root.findViewById(R.id.cpp_plot_function_line_color_spinner);
final Spinner plotLineColorTypeSpinner = (Spinner)root.findViewById(R.id.cpp_plot_function_line_color_type_spinner);
final Spinner plotLineStyleSpinner = (Spinner)root.findViewById(R.id.cpp_plot_function_line_style_spinner);
final SeekBar plotLineWidthSeekBar = (SeekBar)root.findViewById(R.id.cpp_plot_functions_line_width_seekbar);
final Button okButton = (Button)root.findViewById(R.id.cpp_ok_button);
plotLineWidthSeekBar.setMax(10);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (plotFunction != null) {
plotLineWidthSeekBar.setProgress((int)plotFunction.getPlotLineDef().getLineWidth());
PlotFunction plotFunction = CalculatorPlotFunctionSettingsFragment.this.plotFunction;
plotLineColorSpinner.setSelection(PlotLineColor.valueOf(plotFunction.getPlotLineDef().getLineColor()).ordinal());
plotLineColorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final PlotLineColor newPlotLineColor = PlotLineColor.values()[position];
int newLineColor = newPlotLineColor.getColor();
if ( newLineColor != plotFunction.getPlotLineDef().getLineColor() ) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineColor(plotFunction.getPlotLineDef(), newLineColor);
final PlotFunction newPlotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
if(plotter.updateFunction(newPlotFunction)) {
plotFunction = newPlotFunction;
}
}
}
// color
final PlotLineColor newPlotLineColor = PlotLineColor.values()[plotLineColorSpinner.getSelectedItemPosition()];
int newLineColor = newPlotLineColor.getColor();
if (newLineColor != plotFunction.getPlotLineDef().getLineColor()) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineColor(plotFunction.getPlotLineDef(), newLineColor);
plotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// color type
final PlotLineColorType newPlotLineColorType = PlotLineColorType.values()[plotLineColorTypeSpinner.getSelectedItemPosition()];
if (newPlotLineColorType != CalculatorPlotFunctionSettingsFragment.this.plotFunction.getPlotLineDef().getLineColorType()) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineColorType(plotFunction.getPlotLineDef(), newPlotLineColorType);
plotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
}
plotLineColorTypeSpinner.setSelection(plotFunction.getPlotLineDef().getLineColorType().ordinal());
plotLineColorTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final PlotLineColorType newPlotLineColorType = PlotLineColorType.values()[position];
if ( newPlotLineColorType != plotFunction.getPlotLineDef().getLineColorType() ) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineColorType(plotFunction.getPlotLineDef(), newPlotLineColorType);
final PlotFunction newPlotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
if(plotter.updateFunction(newPlotFunction)) {
plotFunction = newPlotFunction;
}
}
}
// line style
final PlotLineStyle newPlotLineStyle = PlotLineStyle.values()[plotLineStyleSpinner.getSelectedItemPosition()];
if (newPlotLineStyle != plotFunction.getPlotLineDef().getLineStyle()) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineStyle(plotFunction.getPlotLineDef(), newPlotLineStyle);
plotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// width
plotLineStyleSpinner.setSelection(plotFunction.getPlotLineDef().getLineStyle().ordinal());
plotLineStyleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final PlotLineStyle newPlotLineStyle = PlotLineStyle.values()[position];
if ( newPlotLineStyle != plotFunction.getPlotLineDef().getLineStyle() ) {
final int newPlotLineWidth = plotLineWidthSeekBar.getProgress();
if (((float) newPlotLineWidth) != plotFunction.getPlotLineDef().getLineWidth()) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineWidth(plotFunction.getPlotLineDef(), newPlotLineWidth);
plotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
}
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineStyle(plotFunction.getPlotLineDef(), newPlotLineStyle);
final PlotFunction newPlotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
if (plotFunction != CalculatorPlotFunctionSettingsFragment.this.plotFunction) {
// if has been changed
if(plotter.updateFunction(newPlotFunction)) {
plotFunction = newPlotFunction;
}
}
}
if (plotter.updateFunction(plotFunction)) {
CalculatorPlotFunctionSettingsFragment.this.plotFunction = plotFunction;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
final Activity activity = getActivity();
if (activity != null) {
activity.finish();
}
}
});
plotLineWidthSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// remove old messages
handler.removeMessages(MESSAGE_ID);
// send new message
handler.sendMessageDelayed(Message.obtain(handler, MESSAGE_ID, progress, 0), LINE_WIDTH_DELAY);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Activity activity = getActivity();
if ( activity != null ) {
activity.finish();
}
}
});
} else {
plotLineWidthSeekBar.setEnabled(false);
plotLineColorSpinner.setEnabled(false);
plotLineColorTypeSpinner.setEnabled(false);
plotLineStyleSpinner.setEnabled(false);
okButton.setEnabled(false);
}
}
}
} else {
plotLineWidthSeekBar.setEnabled(false);
plotLineColorSpinner.setEnabled(false);
plotLineColorTypeSpinner.setEnabled(false);
plotLineStyleSpinner.setEnabled(false);
okButton.setEnabled(false);
}
}
}
}

View File

@@ -1,15 +1,13 @@
package org.solovyev.android.calculator.plot;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.CalculatorFragment;
import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.CalculatorListFragment;
import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.R;
import org.solovyev.android.fragments.FragmentUtils;
@@ -31,7 +29,7 @@ public class CalculatorPlotRangeActivity extends SherlockFragmentActivity {
FragmentUtils.createFragment(this, CalculatorPlotRangeFragment.class, R.id.dialog_layout, "plot-range");
}
public static class CalculatorPlotRangeFragment extends CalculatorListFragment {
public static class CalculatorPlotRangeFragment extends CalculatorFragment {
public CalculatorPlotRangeFragment() {
super(CalculatorFragmentType.plotter_range);
@@ -52,53 +50,29 @@ public class CalculatorPlotRangeActivity extends SherlockFragmentActivity {
xMinEditText.setText(String.valueOf(boundaries.getXMin()));
xMaxEditText.setText(String.valueOf(boundaries.getXMax()));
xMinEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
final Float newXMin = Float.valueOf(s.toString());
plotter.setPlotBoundaries(PlotBoundaries.newInstance(newXMin, boundaries.getXMax()));
} catch (NumberFormatException e) {
Locator.getInstance().getNotifier().showMessage(R.string.cpp_invalid_number, MessageType.error);
xMinEditText.setText(String.valueOf(boundaries.getXMin()));
}
}
});
xMaxEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
final Float newXMax = Float.valueOf(s.toString());
plotter.setPlotBoundaries(PlotBoundaries.newInstance(boundaries.getXMin(), newXMax));
} catch (NumberFormatException e) {
Locator.getInstance().getNotifier().showMessage(R.string.cpp_invalid_number, MessageType.error);
xMaxEditText.setText(String.valueOf(boundaries.getXMax()));
}
}
});
root.findViewById(R.id.cpp_ok_button).setOnClickListener(new View.OnClickListener() {
root.findViewById(R.id.cpp_apply_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CalculatorPlotRangeFragment.this.getActivity().finish();
try {
final Float xMin = Float.valueOf(xMinEditText.getText().toString());
final Float xMax = Float.valueOf(xMaxEditText.getText().toString());
if ( xMin.equals(xMax) ) {
throw new IllegalArgumentException();
}
plotter.setPlotBoundaries(PlotBoundaries.newInstance(xMin, xMax));
CalculatorPlotRangeFragment.this.getActivity().finish();
} catch (IllegalArgumentException e) {
if (e instanceof NumberFormatException) {
Locator.getInstance().getNotifier().showMessage(R.string.cpp_invalid_number, MessageType.error);
} else {
Locator.getInstance().getNotifier().showMessage(R.string.cpp_plot_boundaries_should_differ, MessageType.error);
}
}
}
});
}