GNU Classpath (0.18) | ||
Frames | No Frames |
1: /* BasicColorChooserUI.java -- 2: Copyright (C) 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package javax.swing.plaf.basic; 40: 41: import java.awt.BorderLayout; 42: import java.awt.Container; 43: import java.beans.PropertyChangeEvent; 44: import java.beans.PropertyChangeListener; 45: 46: import javax.swing.JColorChooser; 47: import javax.swing.JComponent; 48: import javax.swing.JPanel; 49: import javax.swing.JTabbedPane; 50: import javax.swing.UIDefaults; 51: import javax.swing.UIManager; 52: import javax.swing.colorchooser.AbstractColorChooserPanel; 53: import javax.swing.colorchooser.ColorChooserComponentFactory; 54: import javax.swing.event.ChangeEvent; 55: import javax.swing.event.ChangeListener; 56: import javax.swing.plaf.ColorChooserUI; 57: import javax.swing.plaf.ComponentUI; 58: 59: /** 60: * This is the UI Class for the JColorChooser in the Basic Look and Feel. 61: */ 62: public class BasicColorChooserUI extends ColorChooserUI 63: { 64: /** 65: * This helper class handles property changes from the JColorChooser. 66: */ 67: public class PropertyHandler implements PropertyChangeListener 68: { 69: /** 70: * This method is called when any of the properties of the JColorChooser 71: * change. 72: * 73: * @param e The PropertyChangeEvent. 74: */ 75: public void propertyChange(PropertyChangeEvent e) 76: { 77: if (e.getPropertyName() == JColorChooser.CHOOSER_PANELS_PROPERTY) 78: makeTabs(chooser.getChooserPanels()); 79: else if (e.getPropertyName() == JColorChooser.PREVIEW_PANEL_PROPERTY) 80: updatePreviewPanel(chooser.getPreviewPanel()); 81: else if (e.getPropertyName() == JColorChooser.SELECTION_MODEL_PROPERTY) 82: ((AbstractColorChooserPanel) pane.getSelectedComponent()) 83: .updateChooser(); 84: 85: chooser.repaint(); 86: } 87: } 88: 89: /** 90: * This is a helper class that listens to the Model of the JColorChooser for 91: * color change events so it can update the preview panel. 92: */ 93: private class PreviewListener implements ChangeListener 94: { 95: /** 96: * This method is called whenever the JColorChooser's color changes. 97: * 98: * @param e The ChangeEvent. 99: */ 100: public void stateChanged(ChangeEvent e) 101: { 102: if (pane != null) 103: { 104: AbstractColorChooserPanel panel = (AbstractColorChooserPanel) pane 105: .getSelectedComponent(); 106: if (panel != null) 107: panel.updateChooser(); 108: } 109: chooser.repaint(); 110: } 111: } 112: 113: /** 114: * This helper class listens to the JTabbedPane that is used for tab 115: * changes. 116: */ 117: private class TabPaneListener implements ChangeListener 118: { 119: /** 120: * This method is called whenever a different tab is selected in the 121: * JTabbedPane. 122: * 123: * @param e The ChangeEvent. 124: */ 125: public void stateChanged(ChangeEvent e) 126: { 127: // Need to do this because we don't update all the tabs when they're not 128: // visible, so they are not informed of new colors when they're hidden. 129: AbstractColorChooserPanel comp = (AbstractColorChooserPanel) pane 130: .getSelectedComponent(); 131: comp.updateChooser(); 132: } 133: } 134: 135: /** An array of default choosers to use in the JColorChooser. */ 136: protected AbstractColorChooserPanel[] defaultChoosers; 137: 138: /** The listener for the preview panel. */ 139: protected ChangeListener previewListener; 140: 141: /** The PropertyChangeListener for the JColorChooser. */ 142: protected PropertyChangeListener propertyChangeListener; 143: 144: /** 145: * The JColorChooser. 146: * This is package-private to avoid an accessor method. 147: */ 148: JColorChooser chooser; 149: 150: /** The JTabbedPane that is used. */ 151: JTabbedPane pane; 152: 153: /** The Container that holds the preview panel. */ 154: private Container prevContainer; 155: 156: /** 157: * Creates a new BasicColorChooserUI object. 158: */ 159: public BasicColorChooserUI() 160: { 161: super(); 162: } 163: 164: /** 165: * This method creates a new UI Component for the given JComponent. 166: * 167: * @param c The JComponent to create an UI for. 168: * 169: * @return A new BasicColorChooserUI. 170: */ 171: public static ComponentUI createUI(JComponent c) 172: { 173: return new BasicColorChooserUI(); 174: } 175: 176: /** 177: * This method creates the default chooser panels for the JColorChooser. 178: * 179: * @return The default chooser panels. 180: */ 181: protected AbstractColorChooserPanel[] createDefaultChoosers() 182: { 183: return ColorChooserComponentFactory.getDefaultChooserPanels(); 184: } 185: 186: /** 187: * This method installs the UI Component for the given JComponent. 188: * 189: * @param c The JComponent to install this UI for. 190: */ 191: public void installUI(JComponent c) 192: { 193: if (c instanceof JColorChooser) 194: { 195: chooser = (JColorChooser) c; 196: chooser.setLayout(new BorderLayout()); 197: 198: // Do this first, so we avoid doing work for property change events. 199: defaultChoosers = createDefaultChoosers(); 200: chooser.setChooserPanels(defaultChoosers); 201: pane = new JTabbedPane(); 202: 203: pane.addChangeListener(new ChangeListener() 204: { 205: public void stateChanged(ChangeEvent e) 206: { 207: pane.repaint(); 208: } 209: }); 210: 211: makeTabs(defaultChoosers); 212: 213: chooser.add(pane, BorderLayout.NORTH); 214: 215: installPreviewPanel(); 216: 217: installDefaults(); 218: installListeners(); 219: } 220: } 221: 222: /** 223: * This method adds tabs to the JTabbedPane for the chooserPanels defined in 224: * the JColorChooser. 225: * This is package-private to avoid an accessor method. 226: * 227: * @param panels The Panels that need tabs to be made for them. 228: */ 229: void makeTabs(AbstractColorChooserPanel[] panels) 230: { 231: pane.removeAll(); 232: for (int i = 0; i < panels.length; i++) 233: pane.addTab(panels[i].getDisplayName(), panels[i].getSmallDisplayIcon(), 234: panels[i]); 235: } 236: 237: /** 238: * This method uninstalls this UI for the given JComponent. 239: * 240: * @param c The JComponent that will have this UI removed. 241: */ 242: public void uninstallUI(JComponent c) 243: { 244: uninstallListeners(); 245: uninstallDefaults(); 246: 247: pane = null; 248: chooser = null; 249: } 250: 251: /** 252: * This method installs the preview panel for the JColorChooser. 253: */ 254: protected void installPreviewPanel() 255: { 256: updatePreviewPanel(ColorChooserComponentFactory.getPreviewPanel()); 257: } 258: 259: /** 260: * This is a helper method that swaps the existing preview panel with the 261: * given panel. 262: * This is package-private to avoid an accessor method. 263: * 264: * @param preview The new preview panel. 265: */ 266: void updatePreviewPanel(JComponent preview) 267: { 268: if (prevContainer == null) 269: { 270: prevContainer = new JPanel(); 271: prevContainer.setLayout(new BorderLayout()); 272: chooser.add(prevContainer, BorderLayout.CENTER); 273: } 274: prevContainer.removeAll(); 275: prevContainer.add(preview, BorderLayout.CENTER); 276: } 277: 278: /** 279: * This method installs the default properties given by the Basic Look and 280: * Feel. 281: */ 282: protected void installDefaults() 283: { 284: UIDefaults defaults = UIManager.getLookAndFeelDefaults(); 285: 286: chooser.setFont(defaults.getFont("ColorChooser.font")); 287: chooser.setForeground(defaults.getColor("ColorChooser.foreground")); 288: chooser.setBackground(defaults.getColor("ColorChooser.background")); 289: } 290: 291: /** 292: * This method uninstalls the default properties given by the Basic Look and 293: * Feel. 294: */ 295: protected void uninstallDefaults() 296: { 297: chooser.setBackground(null); 298: chooser.setForeground(null); 299: chooser.setFont(null); 300: } 301: 302: /** 303: * This method installs any listeners required for this UI to function. 304: */ 305: protected void installListeners() 306: { 307: propertyChangeListener = createPropertyChangeListener(); 308: previewListener = new PreviewListener(); 309: 310: chooser.addPropertyChangeListener(propertyChangeListener); 311: chooser.getSelectionModel().addChangeListener(previewListener); 312: 313: pane.addChangeListener(new TabPaneListener()); 314: } 315: 316: /** 317: * This method creates the PropertyChangeListener used for listening to the 318: * JColorChooser. 319: * 320: * @return A PropertyChangeListener. 321: */ 322: protected PropertyChangeListener createPropertyChangeListener() 323: { 324: return new PropertyHandler(); 325: } 326: 327: /** 328: * This method uninstalls any listeners that were previously installed by 329: * the UI. 330: */ 331: protected void uninstallListeners() 332: { 333: chooser.removePropertyChangeListener(propertyChangeListener); 334: chooser.getSelectionModel().removeChangeListener(previewListener); 335: 336: previewListener = null; 337: propertyChangeListener = null; 338: } 339: }
GNU Classpath (0.18) |