001package com.ganteater.ae.desktop.ui;
002
003import java.awt.Color;
004import java.awt.event.ItemEvent;
005import java.awt.event.ItemListener;
006
007import javax.swing.JCheckBox;
008
009import org.apache.commons.lang.StringUtils;
010
011import com.ganteater.ae.AEWorkspace;
012import com.ganteater.ae.desktop.InputDialogType;
013
014public class CheckPointBox extends JCheckBox implements ItemListener {
015
016        public static final String CHECKPOINT_PREFIX = "CheckPoint";
017
018        private static final long serialVersionUID = -3670107773808535274L;
019
020        private static final String ALWAYS_SHOW_NOTIFICATIONS = "Very important dialog";
021
022        private String name;
023
024        private InputDialogType type;
025
026        private Color bgColor;
027
028        public CheckPointBox(String propertyName, String description, InputDialogType type) {
029                super(ALWAYS_SHOW_NOTIFICATIONS);
030                this.type = type;
031                name = StringUtils.defaultString(description, propertyName);
032                setEnabled(true);
033                addItemListener(this);
034
035                Boolean valueOf = getCheckPointFlag(type, name);
036                if (valueOf != null) {
037                        setSelected(valueOf);
038                } else {
039                        setSelected(!AEFrame.LOOK_AND_FEEL.equals(propertyName));
040                }
041
042                applyBackgroungColor();
043        }
044
045        public void saveCheckpointFlag() {
046                boolean selected = isSelected();
047                setCheckPointFlag(type, name, selected);
048        }
049
050        private void applyBackgroungColor() {
051                if (this.bgColor == null) {
052                        bgColor = getBackground();
053                }
054                setBackground(isSelected() ? new Color(0xe4696a) : bgColor);
055        }
056
057        public static Boolean getCheckPointFlag(InputDialogType type, String name) {
058                CheckpointsDialog instance = CheckpointsDialog.getInstance();
059                instance.updated(name);
060
061                AEWorkspace aeWorkspace = AEWorkspace.getInstance();
062                String propertyName = CHECKPOINT_PREFIX + "." + type + "." + name;
063                String checkPoint = aeWorkspace.getDefaultUserConfiguration(propertyName, null);
064
065                return checkPoint == null ? null : Boolean.valueOf(checkPoint);
066        }
067
068        @Override
069        public void itemStateChanged(ItemEvent e) {
070                saveCheckpointFlag();
071                applyBackgroungColor();
072        }
073
074        public static void setCheckPointFlag(InputDialogType type, String name, boolean selected) {
075                AEWorkspace instance = AEWorkspace.getInstance();
076                String propertyName = CHECKPOINT_PREFIX + "." + type + "." + name;
077                String value = Boolean.toString(selected);
078                instance.setDefaultUserConfiguration(propertyName, value);
079
080        }
081
082}