GNU Classpath (0.20) | |
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.LookAndFeel; 51: import javax.swing.colorchooser.AbstractColorChooserPanel; 52: import javax.swing.colorchooser.ColorChooserComponentFactory; 53: import javax.swing.event.ChangeEvent; 54: import javax.swing.event.ChangeListener; 55: import javax.swing.plaf.ColorChooserUI; 56: import javax.swing.plaf.ComponentUI; 57: 58: /** 59: * This is the UI Class for the JColorChooser in the Basic Look and Feel. 60: */ 61: public class BasicColorChooserUI extends ColorChooserUI 62: { 63: /** 64: * This helper class handles property changes from the JColorChooser. 65: */ 66: public class PropertyHandler implements PropertyChangeListener 67: { 68: /** 69: * This method is called when any of the properties of the JColorChooser 70: * change. 71: * 72: * @param e The PropertyChangeEvent. 73: */ 74: public void propertyChange(PropertyChangeEvent e) 75: { 76: if (e.getPropertyName() == JColorChooser.CHOOSER_PANELS_PROPERTY) 77: makeTabs(chooser.getChooserPanels()); 78: else if (e.getPropertyName() == JColorChooser.PREVIEW_PANEL_PROPERTY) 79: updatePreviewPanel(chooser.getPreviewPanel()); 80: else if (e.getPropertyName() == JColorChooser.SELECTION_MODEL_PROPERTY) 81: ((AbstractColorChooserPanel) pane.getSelectedComponent()) 82: .updateChooser(); 83: 84: chooser.repaint(); 85: } 86: } 87: 88: /** 89: * This is a helper class that listens to the Model of the JColorChooser for 90: * color change events so it can update the preview panel. 91: */ 92: private class PreviewListener implements ChangeListener 93: { 94: /** 95: * This method is called whenever the JColorChooser's color changes. 96: * 97: * @param e The ChangeEvent. 98: */ 99: public void stateChanged(ChangeEvent e) 100: { 101: if (pane != null) 102: { 103: AbstractColorChooserPanel panel = (AbstractColorChooserPanel) pane 104: .getSelectedComponent(); 105: if (panel != null) 106: panel.updateChooser(); 107: } 108: chooser.repaint(); 109: } 110: } 111: 112: /** 113: * This helper class listens to the JTabbedPane that is used for tab 114: * changes. 115: */ 116: private class TabPaneListener implements ChangeListener 117: { 118: /** 119: * This method is called whenever a different tab is selected in the 120: * JTabbedPane. 121: * 122: * @param e The ChangeEvent. 123: */ 124: public void stateChanged(ChangeEvent e) 125: { 126: // Need to do this because we don't update all the tabs when they're not 127: // visible, so they are not informed of new colors when they're hidden. 128: AbstractColorChooserPanel comp = (AbstractColorChooserPanel) pane 129: .getSelectedComponent(); 130: comp.updateChooser(); 131: } 132: } 133: 134: /** An array of default choosers to use in the JColorChooser. */ 135: protected AbstractColorChooserPanel[] defaultChoosers; 136: 137: /** The listener for the preview panel. */ 138: protected ChangeListener previewListener; 139: 140: /** The PropertyChangeListener for the JColorChooser. */ 141: protected PropertyChangeListener propertyChangeListener; 142: 143: /** 144: * The JColorChooser. 145: * This is package-private to avoid an accessor method. 146: */ 147: JColorChooser chooser; 148: 149: /** The JTabbedPane that is used. */ 150: JTabbedPane pane; 151: 152: /** The Container that holds the preview panel. */ 153: private Container prevContainer; 154: 155: /** 156: * Creates a new BasicColorChooserUI object. 157: */ 158: public BasicColorChooserUI() 159: { 160: super(); 161: } 162: 163: /** 164: * This method creates a new UI Component for the given JComponent. 165: * 166: * @param c The JComponent to create an UI for. 167: * 168: * @return A new BasicColorChooserUI. 169: */ 170: public static ComponentUI createUI(JComponent c) 171: { 172: return new BasicColorChooserUI(); 173: } 174: 175: /** 176: * This method creates the default chooser panels for the JColorChooser. 177: * 178: * @return The default chooser panels. 179: */ 180: protected AbstractColorChooserPanel[] createDefaultChoosers() 181: { 182: return ColorChooserComponentFactory.getDefaultChooserPanels(); 183: } 184: 185: /** 186: * This method installs the UI Component for the given JComponent. 187: * 188: * @param c The JComponent to install this UI for. 189: */ 190: public void installUI(JComponent c) 191: { 192: if (c instanceof JColorChooser) 193: { 194: chooser = (JColorChooser) c; 195: chooser.setLayout(new BorderLayout()); 196: 197: // Do this first, so we avoid doing work for property change events. 198: defaultChoosers = createDefaultChoosers(); 199: chooser.setChooserPanels(defaultChoosers); 200: pane = new JTabbedPane(); 201: 202: pane.addChangeListener(new ChangeListener() 203: { 204: public void stateChanged(ChangeEvent e) 205: { 206: pane.repaint(); 207: } 208: }); 209: 210: makeTabs(defaultChoosers); 211: 212: chooser.add(pane, BorderLayout.NORTH); 213: 214: installPreviewPanel(); 215: 216: installDefaults(); 217: installListeners(); 218: } 219: } 220: 221: /** 222: * This method adds tabs to the JTabbedPane for the chooserPanels defined in 223: * the JColorChooser. 224: * This is package-private to avoid an accessor method. 225: * 226: * @param panels The Panels that need tabs to be made for them. 227: */ 228: void makeTabs(AbstractColorChooserPanel[] panels) 229: { 230: pane.removeAll(); 231: for (int i = 0; i < panels.length; i++) 232: pane.addTab(panels[i].getDisplayName(), panels[i].getSmallDisplayIcon(), 233: panels[i]); 234: } 235: 236: /** 237: * This method uninstalls this UI for the given JComponent. 238: * 239: * @param c The JComponent that will have this UI removed. 240: */ 241: public void uninstallUI(JComponent c) 242: { 243: uninstallListeners(); 244: uninstallDefaults(); 245: uninstallDefaultChoosers(); 246: 247: pane = null; 248: chooser = null; 249: } 250: 251: /** 252: * Uninstalls the default color choosers that have been installed by this UI. 253: */ 254: protected void uninstallDefaultChoosers() 255: { 256: defaultChoosers = null; 257: } 258: 259: /** 260: * This method installs the preview panel for the JColorChooser. 261: */ 262: protected void installPreviewPanel() 263: { 264: updatePreviewPanel(ColorChooserComponentFactory.getPreviewPanel()); 265: } 266: 267: /** 268: * This is a helper method that swaps the existing preview panel with the 269: * given panel. 270: * This is package-private to avoid an accessor method. 271: * 272: * @param preview The new preview panel. 273: */ 274: void updatePreviewPanel(JComponent preview) 275: { 276: if (prevContainer == null) 277: { 278: prevContainer = new JPanel(); 279: prevContainer.setLayout(new BorderLayout()); 280: chooser.add(prevContainer, BorderLayout.CENTER); 281: } 282: prevContainer.removeAll(); 283: prevContainer.add(preview, BorderLayout.CENTER); 284: } 285: 286: /** 287: * This method installs the default properties given by the Basic Look and 288: * Feel. 289: */ 290: protected void installDefaults() 291: { 292: LookAndFeel.installColorsAndFont(chooser, "ColorChooser.background", 293: "ColorChooser.foreground", 294: "ColorChooser.font"); 295: } 296: 297: /** 298: * This method uninstalls the default properties given by the Basic Look and 299: * Feel. 300: */ 301: protected void uninstallDefaults() 302: { 303: chooser.setBackground(null); 304: chooser.setForeground(null); 305: chooser.setFont(null); 306: } 307: 308: /** 309: * This method installs any listeners required for this UI to function. 310: */ 311: protected void installListeners() 312: { 313: propertyChangeListener = createPropertyChangeListener(); 314: previewListener = new PreviewListener(); 315: 316: chooser.addPropertyChangeListener(propertyChangeListener); 317: chooser.getSelectionModel().addChangeListener(previewListener); 318: 319: pane.addChangeListener(new TabPaneListener()); 320: } 321: 322: /** 323: * This method creates the PropertyChangeListener used for listening to the 324: * JColorChooser. 325: * 326: * @return A PropertyChangeListener. 327: */ 328: protected PropertyChangeListener createPropertyChangeListener() 329: { 330: return new PropertyHandler(); 331: } 332: 333: /** 334: * This method uninstalls any listeners that were previously installed by 335: * the UI. 336: */ 337: protected void uninstallListeners() 338: { 339: chooser.removePropertyChangeListener(propertyChangeListener); 340: chooser.getSelectionModel().removeChangeListener(previewListener); 341: 342: previewListener = null; 343: propertyChangeListener = null; 344: } 345: }
GNU Classpath (0.20) |