1. /*
  2. * @(#)BasicColorChooserUI.java 1.25 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.plaf.basic;
  11. import javax.swing.*;
  12. import javax.swing.colorchooser.*;
  13. import javax.swing.event.*;
  14. import javax.swing.border.*;
  15. import javax.swing.plaf.*;
  16. import java.util.*;
  17. import java.awt.*;
  18. import java.awt.image.*;
  19. import java.awt.event.*;
  20. import java.beans.PropertyChangeEvent;
  21. import java.beans.PropertyChangeListener;
  22. import java.io.Serializable;
  23. /**
  24. * Provides the basic look and feel for a JColorChooser.
  25. * <p>
  26. * @version 1.25 02/02/00
  27. * @author Tom Santos
  28. * @author Steve Wilson
  29. */
  30. public class BasicColorChooserUI extends ColorChooserUI
  31. {
  32. JColorChooser chooser;
  33. JTabbedPane tabbedPane;
  34. JPanel singlePanel;
  35. JPanel previewPanelHolder;
  36. JComponent previewPanel;
  37. boolean isMultiPanel = false;
  38. protected AbstractColorChooserPanel[] defaultChoosers;
  39. protected ChangeListener previewListener;
  40. protected PropertyChangeListener propertyChangeListener;
  41. public static ComponentUI createUI(JComponent c) {
  42. return new BasicColorChooserUI();
  43. }
  44. protected AbstractColorChooserPanel[] createDefaultChoosers() {
  45. AbstractColorChooserPanel[] panels = ColorChooserComponentFactory.getDefaultChooserPanels();
  46. return panels;
  47. }
  48. protected void uninstallDefaultChoosers() {
  49. for( int i = 0 ; i < defaultChoosers.length; i++) {
  50. chooser.removeChooserPanel( defaultChoosers[i] );
  51. }
  52. }
  53. public void installUI( JComponent c ) {
  54. chooser = (JColorChooser)c;
  55. super.installUI( c );
  56. installDefaults();
  57. installListeners();
  58. tabbedPane = new JTabbedPane();
  59. singlePanel = new JPanel(new CenterLayout());
  60. chooser.setLayout( new BorderLayout() );
  61. defaultChoosers = createDefaultChoosers();
  62. chooser.setChooserPanels(defaultChoosers);
  63. installPreviewPanel();
  64. }
  65. public void uninstallUI( JComponent c ) {
  66. uninstallListeners();
  67. uninstallDefaultChoosers();
  68. uninstallDefaults();
  69. defaultChoosers = null;
  70. chooser = null;
  71. tabbedPane = null;
  72. }
  73. protected void installPreviewPanel() {
  74. previewPanelHolder = new JPanel(new CenterLayout());
  75. String previewString = UIManager.getString("ColorChooser.previewText");
  76. previewPanelHolder.setBorder(new TitledBorder(previewString));
  77. previewPanel = chooser.getPreviewPanel();
  78. if (previewPanel == null || previewPanel instanceof UIResource) {
  79. previewPanel = ColorChooserComponentFactory.getPreviewPanel(); // get from table?
  80. }
  81. previewPanel.setForeground(chooser.getColor());
  82. previewPanelHolder.add(previewPanel);
  83. chooser.add(previewPanelHolder, BorderLayout.SOUTH);
  84. }
  85. protected void installDefaults() {
  86. LookAndFeel.installColorsAndFont(chooser, "ColorChooser.background",
  87. "ColorChooser.foreground",
  88. "ColorChooser.font");
  89. }
  90. protected void uninstallDefaults() {
  91. }
  92. protected void installListeners() {
  93. propertyChangeListener = createPropertyChangeListener();
  94. chooser.addPropertyChangeListener( propertyChangeListener );
  95. previewListener = new PreviewListener();
  96. chooser.getSelectionModel().addChangeListener(previewListener);
  97. }
  98. protected PropertyChangeListener createPropertyChangeListener() {
  99. return new PropertyHandler();
  100. }
  101. protected void uninstallListeners() {
  102. chooser.removePropertyChangeListener( propertyChangeListener );
  103. chooser.getSelectionModel().removeChangeListener(previewListener);
  104. }
  105. class PreviewListener implements ChangeListener {
  106. public void stateChanged( ChangeEvent e ) {
  107. ColorSelectionModel model = (ColorSelectionModel)e.getSource();
  108. if (previewPanel != null) {
  109. previewPanel.setForeground(model.getSelectedColor());
  110. previewPanel.repaint();
  111. }
  112. }
  113. }
  114. /**
  115. * This inner class is marked "public" due to a compiler bug.
  116. * This class should be treated as a "protected" inner class.
  117. * Instantiate it only within subclasses of <Foo>.
  118. */
  119. public class PropertyHandler implements PropertyChangeListener {
  120. public void propertyChange(PropertyChangeEvent e) {
  121. if ( e.getPropertyName().equals( JColorChooser.CHOOSER_PANELS_PROPERTY ) ) {
  122. AbstractColorChooserPanel[] oldPanels = (AbstractColorChooserPanel[]) e.getOldValue();
  123. AbstractColorChooserPanel[] newPanels = (AbstractColorChooserPanel[]) e.getNewValue();
  124. for (int i = 0; i < oldPanels.length; i++) { // remove old panels
  125. Container wrapper = oldPanels[i].getParent();
  126. if (wrapper != null) {
  127. Container parent = wrapper.getParent();
  128. if (parent != null)
  129. parent.remove(wrapper); // remove from hierarchy
  130. oldPanels[i].uninstallChooserPanel(chooser); // uninstall
  131. }
  132. }
  133. int numNewPanels = newPanels.length;
  134. if (numNewPanels == 0) { // removed all panels and added none
  135. chooser.remove(tabbedPane);
  136. return;
  137. }
  138. else if (numNewPanels == 1) { // one panel case
  139. chooser.remove(tabbedPane);
  140. JPanel centerWrapper = new JPanel( new CenterLayout() );
  141. centerWrapper.add(newPanels[0]);
  142. singlePanel.add(centerWrapper, BorderLayout.CENTER);
  143. chooser.add(singlePanel);
  144. }
  145. else { // multi-panel case
  146. if ( oldPanels.length < 2 ) {// moving from single to multiple
  147. chooser.remove(singlePanel);
  148. tabbedPane = new JTabbedPane();
  149. chooser.add(tabbedPane, BorderLayout.CENTER);
  150. }
  151. for (int i = 0; i < newPanels.length; i++) {
  152. JPanel centerWrapper = new JPanel( new CenterLayout() );
  153. centerWrapper.add(newPanels[i]);
  154. tabbedPane.addTab(newPanels[i].getDisplayName(), centerWrapper);
  155. }
  156. }
  157. for (int i = 0; i < newPanels.length; i++) {
  158. newPanels[i].installChooserPanel(chooser);
  159. }
  160. }
  161. if ( e.getPropertyName().equals( JColorChooser.PREVIEW_PANEL_PROPERTY ) ) {
  162. JComponent oldPanel = (JComponent) e.getOldValue();
  163. JComponent newPanel = (JComponent) e.getNewValue();
  164. if (oldPanel != null) { // fix for 4166059
  165. chooser.remove(oldPanel);
  166. }
  167. chooser.add(newPanel, BorderLayout.SOUTH);
  168. }
  169. }
  170. }
  171. }