1 33 34 package edu.rice.cs.drjava.ui.config; 35 36 import javax.swing.*; 37 import edu.rice.cs.drjava.config.*; 38 import edu.rice.cs.drjava.*; 39 import java.awt.*; 40 import java.awt.event.*; 41 42 45 public class ColorOptionComponent extends OptionComponent<Color> { 46 private JButton _button; 47 private JTextField _colorField; 48 private JPanel _panel; 49 private Color _color; 50 private boolean _isBackgroundColor; 51 private boolean _isBoldText; 52 53 58 public ColorOptionComponent (ColorOption opt, String text, Frame parent) { 59 this(opt, text, parent, false); 60 } 61 62 65 public ColorOptionComponent(ColorOption opt, String text, Frame parent, boolean isBackgroundColor) { 66 this(opt, text, parent, isBackgroundColor, false); 67 } 68 69 public ColorOptionComponent(ColorOption opt, String text, Frame parent, boolean isBackgroundColor, boolean isBoldText) 70 { 71 super(opt, text, parent); 72 _isBackgroundColor = isBackgroundColor; 73 _isBoldText = isBoldText; 74 _button = new JButton(); 75 _button.addActionListener(new ActionListener() { 76 public void actionPerformed(ActionEvent e) { chooseColor(); } 77 }); 78 _button.setText("..."); 79 _button.setMaximumSize(new Dimension(10,10)); 80 _button.setMinimumSize(new Dimension(10,10)); 81 82 _colorField = new JTextField(); 83 _colorField.setEditable(false); 84 _colorField.setHorizontalAlignment(JTextField.CENTER); 85 _panel = new JPanel(new BorderLayout()); 86 _panel.add(_colorField, BorderLayout.CENTER); 87 _panel.add(_button, BorderLayout.EAST); 88 if (_isBackgroundColor) { 89 _colorField.setForeground(DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_NORMAL_COLOR)); 91 DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_NORMAL_COLOR, 92 new OptionListener<Color>() { 93 public void optionChanged(OptionEvent<Color> oe) { 94 _colorField.setForeground(oe.value); 95 } 96 }); 97 } 98 else { 99 _colorField.setBackground(DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_BACKGROUND_COLOR)); 101 DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_BACKGROUND_COLOR, 102 new OptionListener<Color>() { 103 public void optionChanged(OptionEvent<Color> oe) { 104 _colorField.setBackground(oe.value); 105 } 106 }); 107 } 108 if (_isBoldText) { 109 _colorField.setFont(_colorField.getFont().deriveFont(Font.BOLD)); 110 } 111 _color = DrJava.getConfig().getSetting(_option); 112 _updateField(_color); 113 } 114 115 116 public ColorOptionComponent(ColorOption opt, String text, 117 Frame parent, String description) { 118 this(opt, text, parent, description, false); 119 } 120 121 122 public ColorOptionComponent(ColorOption opt, String text, Frame parent, String description, boolean isBackgroundColor) 123 { 124 this(opt, text, parent, isBackgroundColor); 125 setDescription(description); 126 } 127 128 129 public ColorOptionComponent(ColorOption opt, String text, Frame parent, String description, boolean isBackgroundColor, 130 boolean isBoldText) { 131 this(opt, text, parent, isBackgroundColor, isBoldText); 132 setDescription(description); 133 } 134 135 138 public void setDescription(String description) { 139 _panel.setToolTipText(description); 140 _button.setToolTipText(description); 141 _colorField.setToolTipText(description); 142 _label.setToolTipText(description); 143 } 144 145 148 public boolean updateConfig() { 149 if (!_color.equals(DrJava.getConfig().getSetting(_option))) { 150 DrJava.getConfig().setSetting(_option, _color); 151 } 152 153 return true; 154 } 155 156 157 158 public void setValue(Color value) { 159 _color = value; 160 _updateField(value); 161 } 162 163 164 private void _updateField(Color c) { 165 if (_isBackgroundColor) { 166 _colorField.setBackground(c); 167 } 168 else { 169 _colorField.setForeground(c); 170 } 171 _colorField.setText(getLabelText() + " ("+_option.format(c)+")"); 172 } 173 174 177 public JComponent getComponent() { return _panel; } 178 179 180 public void chooseColor() { 181 Color c = JColorChooser.showDialog(_parent, "Choose '" + getLabelText() + "'", _color); 182 if (c != null) { 183 _color = c; 184 notifyChangeListeners(); 185 _updateField(_color); 186 } 187 } 188 189 } | Popular Tags |