Back to project page SpunkyCharts.
The source code is released under:
GNU General Public License
If you think the Android project SpunkyCharts listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.jogden.spunkycharts.traditionalchart; /* /*from ww w . ja v a 2 s. c om*/ Copyright (C) 2014 Jonathon Ogden < jeog.dev@gmail.com > This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses. */ import android.app.Activity; import android.content.DialogInterface; import android.content.DialogInterface.OnDismissListener; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.RadioGroup; import android.widget.SeekBar; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.SeekBar.OnSeekBarChangeListener; import com.jogden.spunkycharts.ApplicationPreferences; import com.jogden.spunkycharts.MainApplication; import com.jogden.spunkycharts.R; import com.jogden.spunkycharts.ApplicationPreferences.ChartPreferencesSetup; import com.jogden.spunkycharts.misc.ColorPaletteDialog; /** This is the preference object for Traditional Charts. It will be used * in all traditional chart fragments on top of GlobalChartPreferences * </p> * You can use this as a template for your own Preference Object. Please * refer to the comments at the top of GlobalChartPreferences.java * and ApplicationPreferences.java for more information */ public class TraditionalChartPreferences implements ApplicationPreferences.ChartPreferencesSetup { public static final String TRADITIONAL_CHART_PREF = "com.jogden.spunkycharts.TRADITIONAL_CHART_PREF"; public static final String TRADITIONAL_SEG_THICKNESS = "com.jogden.spunkycharts.TRADITIONAL_SEG_THICKNESS"; public static final String TRADITIONAL_CHART_TYPE = "com.jogden.spunkycharts.TRADITIONAL_CHART_TYPE"; public static final String TRADITIONAL_CHART_FREQ = "com.jogden.spunkycharts.TRADITIONAL_CHART_FREQ"; public static final String TRADITIONAL_BACK_COLOR = "com.jogden.spunkycharts.TRADITIONAL_BACK_COLOR"; public static final String TRADITIONAL_FORE_COLOR = "com.jogden.spunkycharts.TRADITIONAL_FORE_COLOR"; public static final String TRADITIONAL_LINE_COLOR = "com.jogden.spunkycharts.TRADITIONAL_LINE_COLOR"; public static enum Type { OC, OHLC, CANDLE, POINT, LINE, SILO } public static enum Frequency { ONE_MIN ( 1 ), THREE_MIN ( 3 ), FIVE_MIN ( 5 ), FIFTEEN_MIN ( 15 ), THIRTY_MIN ( 30 ), SIXTY_MIN (60); public int value; private Frequency(int i) {value = i;} } /* our original default values */ static private final Type ORIG_DEF_TYPE = Type.CANDLE; static private final Frequency ORIG_DEF_FREQ = Frequency.FIVE_MIN; static private final float ORIG_DEF_SEG_THICKNESS = .90f; private static final int ORIG_DEF_BACK_COLOR = Color.TRANSPARENT; private static final int ORIG_DEF_FORE_COLOR = Color.DKGRAY; private static final int ORIG_DEF_LINE_COLOR = Color.BLACK; /* getters and setters for our default values */ public static int getDefaultLineColor() { return ApplicationPreferences.loadSharedPref( TRADITIONAL_LINE_COLOR, ORIG_DEF_LINE_COLOR ); } private static void setDefaultLineColor(int colorId) { ApplicationPreferences.saveSharedPref( TRADITIONAL_LINE_COLOR, colorId ); } public static int getDefaultBackgroundColor() { return ApplicationPreferences.loadSharedPref( TRADITIONAL_BACK_COLOR, ORIG_DEF_BACK_COLOR ); } private static void setDefaultBackgroundColor(int colorId) { ApplicationPreferences.saveSharedPref( TRADITIONAL_BACK_COLOR, colorId ); } public static int getDefaultForegroundColor() { return ApplicationPreferences.loadSharedPref( TRADITIONAL_FORE_COLOR, ORIG_DEF_FORE_COLOR ); } private static void setDefaultForegroundColor(int colorId) { ApplicationPreferences.saveSharedPref( TRADITIONAL_FORE_COLOR, colorId ); } public static Type getDefaultType() { return Type.valueOf( ApplicationPreferences.loadSharedPref( TRADITIONAL_CHART_TYPE, ORIG_DEF_TYPE.toString() ) ); } private static void setDefaultType(Type type) { ApplicationPreferences.saveSharedPref( TRADITIONAL_CHART_TYPE, type.toString() ); } public static Frequency getDefaultFrequency() { return Frequency.valueOf( ApplicationPreferences.loadSharedPref( TRADITIONAL_CHART_FREQ, ORIG_DEF_FREQ.toString() ) ); } private static void setDefaultFrequency(Frequency frequency) { ApplicationPreferences.saveSharedPref( TRADITIONAL_CHART_FREQ, frequency.toString() ); } public static float getDefaultSegThickness() { return ApplicationPreferences.loadSharedPref( TRADITIONAL_SEG_THICKNESS, ORIG_DEF_SEG_THICKNESS ); } private static void setDefaultSegThickness(float thickness) { ApplicationPreferences.saveSharedPref( TRADITIONAL_SEG_THICKNESS, thickness ); } /* important views in our layout */ private RadioGroup _rgChartFreq, _rgChartType; private SeekBar _sbSegThickness; private View _vBackColor, _vForeColor, _vLineColor; /* need to find all the views in the layout first */ @Override public boolean findViews(MainApplication.ViewFindable finder) { return( (_rgChartFreq = (RadioGroup)finder.findViewById( R.id.localS_chart_freq_radio_group )) != null && (_rgChartType = (RadioGroup)finder.findViewById( R.id.localS_chart_type_radio_group )) != null && ( _sbSegThickness = (SeekBar)finder.findViewById( R.id.localS_seg_thickness_seek_bar )) != null && ( _vForeColor = finder.findViewById( R.id.localS_color_fore_select )) != null && ( _vBackColor = finder.findViewById( R.id.localS_color_back_select )) != null && ( _vLineColor = finder.findViewById( R.id.localS_color_line_select )) != null ); } /* define behavior when user wants to reset defaults to originals */ @Override public void resetDefaults(Activity parent) { setDefaultType(ORIG_DEF_TYPE); setDefaultFrequency(ORIG_DEF_FREQ); setDefaultSegThickness(ORIG_DEF_SEG_THICKNESS); setDefaultForegroundColor(ORIG_DEF_FORE_COLOR); setDefaultBackgroundColor(ORIG_DEF_BACK_COLOR); setDefaultLineColor(ORIG_DEF_LINE_COLOR); setup( getBundleOfDefaults(), getCallbackForDefaults(), parent ); } /* provide a bundle of current defaults */ @Override public Bundle getBundleOfDefaults() { Bundle bundle = new Bundle(); bundle.putFloat( TRADITIONAL_SEG_THICKNESS, getDefaultSegThickness() ); bundle.putString( TRADITIONAL_CHART_TYPE, getDefaultType().toString() ); bundle.putString( TRADITIONAL_CHART_FREQ, getDefaultFrequency().toString() ); bundle.putInt( TRADITIONAL_FORE_COLOR, getDefaultForegroundColor() ); bundle.putInt( TRADITIONAL_BACK_COLOR, getDefaultBackgroundColor() ); bundle.putInt( TRADITIONAL_LINE_COLOR, getDefaultLineColor() ); return bundle; } /* return a callback that will be called when a default is changed */ @Override public ChartPreferencesSetup.Callback getCallbackForDefaults() { return new ChartPreferencesSetup.Callback(){ @Override public <T> void callback(String pref, T val){ if ( pref.equals(TRADITIONAL_CHART_FREQ) ) setDefaultFrequency(Frequency.valueOf((String)val)); else if ( pref.equals(TRADITIONAL_CHART_TYPE) ) setDefaultType(Type.valueOf((String)val)); else if ( pref.equals(TRADITIONAL_SEG_THICKNESS) ) setDefaultSegThickness( (Float)val); else if (pref.equals(TRADITIONAL_FORE_COLOR)) setDefaultForegroundColor( (Integer)val); else if( pref.equals(TRADITIONAL_BACK_COLOR)) setDefaultBackgroundColor( (Integer)val); else if( pref.equals(TRADITIONAL_LINE_COLOR)) setDefaultLineColor( (Integer)val); } }; } /* define how all the layout views will deal with user interaction */ @Override public void setup( Bundle initVals, final ApplicationPreferences.ChartPreferencesSetup.Callback callback, final Activity parent ){ if( parent == null) throw new IllegalArgumentException( "parent can not be null" ); /* SETUP FREQUENCY RADIO GROUP */ _rgChartFreq.check( freqEnumToIdSwitch( Frequency.valueOf( initVals.getString(TRADITIONAL_CHART_FREQ) ) ) ); _rgChartFreq.setOnCheckedChangeListener( new OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { callback.callback( TRADITIONAL_CHART_FREQ, freqIdToEnumSwitch(checkedId).toString() ); } } ); /* SETUP TYPE RADIO GROUP */ _rgChartType.check( typeEnumToIdSwitch( Type.valueOf( initVals.getString(TRADITIONAL_CHART_TYPE) ) ) ); _rgChartType.setOnCheckedChangeListener( new OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { callback.callback( TRADITIONAL_CHART_TYPE, typeIdToEnumSwitch(checkedId).toString() ); } } ); /* SETUP SEGMENT THICKNESS SEEKBACR */ _sbSegThickness.setMax(100); _sbSegThickness.setProgress( (int)((initVals.getFloat(TRADITIONAL_SEG_THICKNESS) - .50)/.005) ); _sbSegThickness.setOnSeekBarChangeListener( new OnSeekBarChangeListener(){ int _progress; @Override public void onProgressChanged( SeekBar seekBar, int progress, boolean fromUser ){ _progress = progress; } @Override public void onStartTrackingTouch( SeekBar seekBar){} @Override public void onStopTrackingTouch( SeekBar seekBar){ callback.callback( TRADITIONAL_SEG_THICKNESS, (float)(_progress * .005 + .50) ); } } ); /* SETUP FOREGROUND COLOR SELECTOR */ _vForeColor.setBackgroundColor( initVals.getInt(TRADITIONAL_FORE_COLOR) ); _vForeColor.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v){ ColorPaletteDialog cpd = new ColorPaletteDialog(parent,getDefaultForegroundColor()); cpd.setOnDismissListener( new OnDismissListener(){ @Override public void onDismiss(DialogInterface dialog) { int newColor = ((ColorPaletteDialog)dialog).getColorId(); callback.callback( TRADITIONAL_FORE_COLOR, newColor ); _vForeColor.setBackgroundColor( newColor); _vForeColor.invalidate(); } } ); cpd.show(); } } ); /* SETUP BACKGROUND COLOR SELECTOR */ _vBackColor.setBackgroundColor( initVals.getInt(TRADITIONAL_BACK_COLOR) ); _vBackColor.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v){ ColorPaletteDialog cpd = new ColorPaletteDialog(parent,getDefaultBackgroundColor()); cpd.setOnDismissListener( new OnDismissListener(){ @Override public void onDismiss(DialogInterface dialog) { int newColor = ((ColorPaletteDialog)dialog).getColorId(); callback.callback( TRADITIONAL_BACK_COLOR, newColor ); _vBackColor.setBackgroundColor(newColor); _vBackColor.invalidate(); } } ); cpd.show(); } } ); /* SETUP LINE COLOR SELECTOR */ _vLineColor.setBackgroundColor( initVals.getInt(TRADITIONAL_LINE_COLOR) ); _vLineColor.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v){ ColorPaletteDialog cpd = new ColorPaletteDialog(parent, getDefaultLineColor()); cpd.setOnDismissListener( new OnDismissListener(){ @Override public void onDismiss(DialogInterface dialog) { int newColor = ((ColorPaletteDialog)dialog).getColorId(); callback.callback( TRADITIONAL_LINE_COLOR, newColor ); _vLineColor.setBackgroundColor(newColor); _vLineColor.invalidate(); } } ); cpd.show(); } } ); } /*provide convenience view-to-value switches if necessary */ public static int typeEnumToIdSwitch(Type chartType) { switch(chartType) { case OHLC: return R.id.localS_chart_type_radio_OHLC; case OC: return R.id.localS_chart_type_radio_OC; case POINT: return R.id.localS_chart_type_radio_POINT; case LINE: return R.id.localS_chart_type_radio_LINE; case SILO: return R.id.localS_chart_type_radio_SILO; default: return R.id.localS_chart_type_radio_CANDLE; } } public static Type typeIdToEnumSwitch(int id) { switch(id) { case R.id.localS_chart_type_radio_OHLC: return TraditionalChartPreferences.Type.OHLC; case R.id.localS_chart_type_radio_OC: return TraditionalChartPreferences.Type.OC; case R.id.localS_chart_type_radio_POINT: return TraditionalChartPreferences.Type.POINT; case R.id.localS_chart_type_radio_LINE: return TraditionalChartPreferences.Type.LINE; case R.id.localS_chart_type_radio_SILO: return TraditionalChartPreferences.Type.SILO; default: return TraditionalChartPreferences.Type.CANDLE; } } public static int freqEnumToIdSwitch(Frequency frequency) { switch(frequency) { case ONE_MIN: return R.id.localS_chart_freq_radio_1min; case THREE_MIN: return R.id.localS_chart_freq_radio_3min; case FIFTEEN_MIN: return R.id.localS_chart_freq_radio_15min; case THIRTY_MIN: return R.id.localS_chart_freq_radio_30min; case SIXTY_MIN: return R.id.localS_chart_freq_radio_60min; default: return R.id.localS_chart_freq_radio_5min; } } public static Frequency freqIdToEnumSwitch(int id) { switch(id) { case R.id.localS_chart_freq_radio_1min: return TraditionalChartPreferences.Frequency.ONE_MIN; case R.id.localS_chart_freq_radio_3min: return TraditionalChartPreferences.Frequency.THREE_MIN; case R.id.localS_chart_freq_radio_15min: return TraditionalChartPreferences.Frequency.FIFTEEN_MIN; case R.id.localS_chart_freq_radio_30min: return TraditionalChartPreferences.Frequency.THIRTY_MIN; case R.id.localS_chart_freq_radio_60min: return TraditionalChartPreferences.Frequency.SIXTY_MIN; default: return TraditionalChartPreferences.Frequency.FIVE_MIN; } } }