1. /*
  2. * @(#)MultiLookAndFeel.java 1.27 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.multi;
  8. import java.util.Vector;
  9. import java.lang.reflect.Method;
  10. import javax.swing.*;
  11. import javax.swing.plaf.*;
  12. /**
  13. * <p>A Multiplexing UI Look and Feel that allows more than one UI
  14. * to be associated with a component at the same time.
  15. * <p>
  16. * <strong>Warning:</strong>
  17. * Serialized objects of this class will not be compatible with
  18. * future Swing releases. The current serialization support is appropriate
  19. * for short term storage or RMI between applications running the same
  20. * version of Swing. A future release of Swing will provide support for
  21. * long term persistence.
  22. *
  23. * @version 1.27 11/29/01
  24. * @author Willie Walker
  25. */
  26. public class MultiLookAndFeel extends LookAndFeel {
  27. //////////////////////////////
  28. // LookAndFeel methods
  29. //////////////////////////////
  30. public String getName() {
  31. return "Multiplexing Look and Feel";
  32. }
  33. public String getID() {
  34. return "Multiplex";
  35. }
  36. public String getDescription() {
  37. return "Allows multiple UI instances per component instance";
  38. }
  39. public boolean isNativeLookAndFeel() {
  40. return false;
  41. }
  42. public boolean isSupportedLookAndFeel() {
  43. return true;
  44. }
  45. public UIDefaults getDefaults() {
  46. UIDefaults table = new MultiUIDefaults();
  47. String packageName = "javax.swing.plaf.multi.Multi";
  48. Object[] uiDefaults = {
  49. "ButtonUI", packageName + "ButtonUI",
  50. "CheckBoxMenuItemUI", packageName + "MenuItemUI",
  51. "CheckBoxUI", packageName + "ButtonUI",
  52. "ColorChooserUI", packageName + "ColorChooserUI",
  53. "ComboBoxUI", packageName + "ComboBoxUI",
  54. "DesktopIconUI", packageName + "DesktopIconUI",
  55. "DesktopPaneUI", packageName + "DesktopPaneUI",
  56. "EditorPaneUI", packageName + "TextUI",
  57. "FileChooserUI", packageName + "FileChooserUI",
  58. "InternalFrameUI", packageName + "InternalFrameUI",
  59. "LabelUI", packageName + "LabelUI",
  60. "ListUI", packageName + "ListUI",
  61. "MenuBarUI", packageName + "MenuBarUI",
  62. "MenuItemUI", packageName + "MenuItemUI",
  63. "MenuUI", packageName + "MenuItemUI",
  64. "OptionPaneUI", packageName + "OptionPaneUI",
  65. "PanelUI", packageName + "PanelUI",
  66. "PasswordFieldUI", packageName + "TextUI",
  67. "PopupMenuSeparatorUI", packageName + "SeparatorUI",
  68. "PopupMenuUI", packageName + "PopupMenuUI",
  69. "ProgressBarUI", packageName + "ProgressBarUI",
  70. "RadioButtonMenuItemUI", packageName + "MenuItemUI",
  71. "RadioButtonUI", packageName + "ButtonUI",
  72. "ScrollBarUI", packageName + "ScrollBarUI",
  73. "ScrollPaneUI", packageName + "ScrollPaneUI",
  74. "SeparatorUI", packageName + "SeparatorUI",
  75. "SliderUI", packageName + "SliderUI",
  76. "SplitPaneUI", packageName + "SplitPaneUI",
  77. "TabbedPaneUI", packageName + "TabbedPaneUI",
  78. "TableHeaderUI", packageName + "TableHeaderUI",
  79. "TableUI", packageName + "TableUI",
  80. "TextAreaUI", packageName + "TextUI",
  81. "TextFieldUI", packageName + "TextUI",
  82. "TextPaneUI", packageName + "TextUI",
  83. "ToggleButtonUI", packageName + "ButtonUI",
  84. "ToolBarSeparatorUI", packageName + "SeparatorUI",
  85. "ToolBarUI", packageName + "ToolBarUI",
  86. "ToolTipUI", packageName + "ToolTipUI",
  87. "TreeUI", packageName + "TreeUI",
  88. "ViewportUI", packageName + "ViewportUI",
  89. };
  90. table.putDefaults(uiDefaults);
  91. return table;
  92. }
  93. ///////////////////////////////
  94. // Utility methods for the UI's
  95. ///////////////////////////////
  96. /**
  97. * Create the real UI's from the default and auxiliary look and feels,
  98. * placing the results in the uis vector passed in.
  99. * @return the ComponentUI for the component.
  100. */
  101. public static ComponentUI createUIs(ComponentUI mui,
  102. Vector uis,
  103. JComponent target) {
  104. ComponentUI ui;
  105. // Make sure we can at least get the default UI
  106. //
  107. ui = UIManager.getDefaults().getUI(target);
  108. if (ui != null) {
  109. uis.addElement(ui);
  110. LookAndFeel[] auxiliaryLookAndFeels;
  111. auxiliaryLookAndFeels = UIManager.getAuxiliaryLookAndFeels();
  112. if (auxiliaryLookAndFeels != null) {
  113. for (int i = 0; i < auxiliaryLookAndFeels.length; i++) {
  114. ui = auxiliaryLookAndFeels[i].getDefaults().getUI(target);
  115. if (ui != null) {
  116. uis.addElement(ui);
  117. }
  118. }
  119. }
  120. } else {
  121. return null;
  122. }
  123. // Don't bother returning the multiplexing UI if all we did was
  124. // get a UI from just the default look and feel.
  125. //
  126. if (uis.size() == 1) {
  127. return (ComponentUI) uis.elementAt(0);
  128. } else {
  129. return mui;
  130. }
  131. }
  132. /**
  133. * Turn the Vector of UI's into an array.
  134. */
  135. protected static ComponentUI[] uisToArray(Vector uis) {
  136. if (uis == null) {
  137. return new ComponentUI[0];
  138. } else {
  139. int count = uis.size();
  140. if (count > 0) {
  141. ComponentUI[] u = new ComponentUI[count];
  142. for (int i = 0; i < count; i++) {
  143. u[i] = (ComponentUI)uis.elementAt(i);
  144. }
  145. return u;
  146. } else {
  147. return null;
  148. }
  149. }
  150. }
  151. }
  152. /**
  153. * We want the Multiplexing LookAndFeel to be quiet and fallback
  154. * gracefully if it cannot find a UI. This class overrides the
  155. * getUIError method of UIDefaults, which is the method that
  156. * emits error messages when it cannot find a UI class in the
  157. * LAF.
  158. */
  159. class MultiUIDefaults extends UIDefaults {
  160. protected void getUIError(String msg) {
  161. System.err.println("Multiplexing LAF: " + msg);
  162. }
  163. }