Back to project page lamp.
The source code is released under:
GNU General Public License
If you think the Android project lamp 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 cz.tomsuch.lampicka.pallete; /*from w w w . ja va2 s .c o m*/ import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.TextView; import cz.tomsuch.lampicka.R; import cz.tomsuch.lampicka.interfaces.PalleteDialogColorListener; import cz.tomsuch.lampicka.util.Preferences; public class PalleteDialog extends DialogFragment { private TextView t1, t2, t3, t4, t5, t6, t7, t8, t9; private LinearLayout l1, l2, l3, l4, l5, l6, l7, l8, l9; private CheckBox hide_checkbox; private Button cancel; private Preferences prefs = Preferences.getInstance(); private FragmentManager fm; private PALLETE_CONTROL colorChooserFor = null; private PalleteDialogColorListener listener = new PalleteDialogColorListener() { @Override public void colorSelected(int color) { } }; private PalleteDialogColorListener colorChooserListener = new PalleteDialogColorListener() { @Override public void colorSelected(int color) { if (colorChooserFor != null) { prefs.saveCurrentColor(color, colorChooserFor); setPalleteColors(); } } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().setTitle(R.string.title_dialog_pallete); View v = inflater.inflate(R.layout.dialog_pallete, container, false); setupUiControls(v); startUiControlListeners(v); setPalleteColors(); return v; } @Override public void dismiss() { dismiss(false); } private void dismiss(boolean force) { if (force || hide_checkbox.isChecked()) { super.dismiss(); } } private void setPalleteColors() { for (LinearLayout l : new LinearLayout[] { l1, l2, l3, l4, l5, l6, l7, l8, l9 }) { if (l != null) setSavedBackground(l); } } private void startUiControlListeners(View v) { for (TextView t : new TextView[] { t1, t2, t3, t4, t5, t6, t7, t8, t9 }) { t.setOnClickListener(onCLickListener); } for (LinearLayout l : new LinearLayout[] { l1, l2, l3, l4, l5, l6, l7, l8, l9 }) { l.setOnClickListener(onCLickListener); } cancel.setOnClickListener(onCLickListener); } private void setSavedBackground(LinearLayout l) { PALLETE_CONTROL e = getViewEnum(l); l.setBackgroundColor(prefs.getSavedColor(e, Color.parseColor("#555555"))); } public static enum PALLETE_CONTROL { A, B, C, D, E, F, G, H, I, WRONG } private PALLETE_CONTROL getViewEnum(View l) { if (l instanceof LinearLayout) { switch (l.getId()) { default: return PALLETE_CONTROL.WRONG; case R.id.pallete_1: return PALLETE_CONTROL.A; case R.id.pallete_2: return PALLETE_CONTROL.B; case R.id.pallete_3: return PALLETE_CONTROL.C; case R.id.pallete_4: return PALLETE_CONTROL.D; case R.id.pallete_5: return PALLETE_CONTROL.E; case R.id.pallete_6: return PALLETE_CONTROL.F; case R.id.pallete_7: return PALLETE_CONTROL.G; case R.id.pallete_8: return PALLETE_CONTROL.H; case R.id.pallete_9: return PALLETE_CONTROL.I; } } return PALLETE_CONTROL.WRONG; } private void setupUiControls(View parent) { t1 = (TextView) parent.findViewById(R.id.pallete_1_clear); t2 = (TextView) parent.findViewById(R.id.pallete_2_clear); t3 = (TextView) parent.findViewById(R.id.pallete_3_clear); t4 = (TextView) parent.findViewById(R.id.pallete_4_clear); t5 = (TextView) parent.findViewById(R.id.pallete_5_clear); t6 = (TextView) parent.findViewById(R.id.pallete_6_clear); t7 = (TextView) parent.findViewById(R.id.pallete_7_clear); t8 = (TextView) parent.findViewById(R.id.pallete_8_clear); t9 = (TextView) parent.findViewById(R.id.pallete_9_clear); l1 = (LinearLayout) parent.findViewById(R.id.pallete_1); l2 = (LinearLayout) parent.findViewById(R.id.pallete_2); l3 = (LinearLayout) parent.findViewById(R.id.pallete_3); l4 = (LinearLayout) parent.findViewById(R.id.pallete_4); l5 = (LinearLayout) parent.findViewById(R.id.pallete_5); l6 = (LinearLayout) parent.findViewById(R.id.pallete_6); l7 = (LinearLayout) parent.findViewById(R.id.pallete_7); l8 = (LinearLayout) parent.findViewById(R.id.pallete_8); l9 = (LinearLayout) parent.findViewById(R.id.pallete_9); cancel = (Button) parent.findViewById(R.id.dialog_pallete_cancel); hide_checkbox = (CheckBox) parent.findViewById(R.id.dialog_pallete_hide_checkbox); hide_checkbox.setChecked(true); } private View.OnClickListener onCLickListener = new View.OnClickListener() { @Override public void onClick(View v) { if (v instanceof Button) { dismiss(true); } else if (v instanceof LinearLayout) { LinearLayout layout = (LinearLayout) v; PALLETE_CONTROL e = getViewEnum(layout); int color = prefs.getSavedColor(e, 0); if (color == 0) { displayColorChoosedDialog(e); } else { listener.colorSelected(color); dismiss(); } } else if (v instanceof TextView) { TextView t = (TextView) v; prefs.clearSavedColor(getViewEnum((View) t.getParent())); setPalleteColors(); } } }; private void displayColorChoosedDialog(PALLETE_CONTROL e) { colorChooserFor = e; new ColorChooserDialog().setColorChooserListener(colorChooserListener) .show(fm, "color-chooser-dialog"); } public static void attach(FragmentManager fm, PalleteDialogColorListener palleteListener) { new PalleteDialog().addListener(palleteListener).setFragmentManager(fm) .show(fm, "pallete-dialog"); } public PalleteDialog addListener(PalleteDialogColorListener palleteListener) { this.listener = palleteListener; return this; } public PalleteDialog setFragmentManager(FragmentManager fm) { this.fm = fm; return this; } }