1 7 8 package javax.swing.colorchooser; 9 10 import java.awt.*; 11 import java.io.Serializable ; 12 import javax.swing.*; 13 import javax.swing.event.*; 14 15 33 public abstract class AbstractColorChooserPanel extends JPanel { 34 35 38 private JColorChooser chooser; 39 40 43 private ChangeListener colorListener; 44 45 48 private boolean dirty = true; 49 50 51 57 public abstract void updateChooser(); 58 59 62 protected abstract void buildChooser(); 63 64 68 public abstract String getDisplayName(); 69 70 87 public int getMnemonic() { 88 return 0; 89 } 90 91 113 public int getDisplayedMnemonicIndex() { 114 return -1; 115 } 116 117 121 public abstract Icon getSmallDisplayIcon(); 122 123 127 public abstract Icon getLargeDisplayIcon(); 128 129 136 public void installChooserPanel(JColorChooser enclosingChooser) { 137 if (chooser != null) { 138 throw new RuntimeException ("This chooser panel is already installed"); 139 } 140 chooser = enclosingChooser; 141 buildChooser(); 142 updateChooser(); 143 colorListener = new ModelListener(); 144 getColorSelectionModel().addChangeListener(colorListener); 145 } 146 147 151 public void uninstallChooserPanel(JColorChooser enclosingChooser) { 152 getColorSelectionModel().removeChangeListener(colorListener); 153 chooser = null; 154 } 155 156 161 public ColorSelectionModel getColorSelectionModel() { 162 return chooser.getSelectionModel(); 163 } 164 165 169 protected Color getColorFromModel() { 170 return getColorSelectionModel().getSelectedColor(); 171 } 172 173 177 public void paint(Graphics g) { 178 if (dirty) { 179 updateChooser(); 180 dirty = false; 181 } 182 super.paint(g); 183 } 184 185 195 static int getInt(Object key, int defaultValue) { 196 Object value = UIManager.get(key); 197 198 if (value instanceof Integer ) { 199 return ((Integer )value).intValue(); 200 } 201 if (value instanceof String ) { 202 try { 203 return Integer.parseInt((String )value); 204 } catch (NumberFormatException nfe) {} 205 } 206 return defaultValue; 207 } 208 209 212 class ModelListener implements ChangeListener, Serializable { 213 public void stateChanged(ChangeEvent e) { 214 if (isShowing()) { updateChooser(); 216 dirty = false; 217 } else { 218 dirty = true; 219 } 220 } 221 } 222 } 223 | Popular Tags |