1. /*
  2. * @(#)JSeparator.java 1.50 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import javax.swing.plaf.*;
  9. import javax.accessibility.*;
  10. import java.io.ObjectOutputStream;
  11. import java.io.ObjectInputStream;
  12. import java.io.IOException;
  13. /**
  14. * <code>JSeparator</code> provides a general purpose component for
  15. * implementing divider lines - most commonly used as a divider
  16. * between menu items that breaks them up into logical groupings.
  17. * Instead of using <code>JSeparator</code> directly,
  18. * you can use the <code>JMenu</code> or <code>JPopupMenu</code>
  19. * <code>addSeparator</code> method to create and add a separator.
  20. * <code>JSeparator</code>s may also be used elsewhere in a GUI
  21. * wherever a visual divider is useful.
  22. *
  23. * <p>
  24. *
  25. * For more information and examples see
  26. * <a
  27. href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
  28. * a section in <em>The Java Tutorial.</em>
  29. * <p>
  30. * <strong>Warning:</strong>
  31. * Serialized objects of this class will not be compatible with
  32. * future Swing releases. The current serialization support is
  33. * appropriate for short term storage or RMI between applications running
  34. * the same version of Swing. As of 1.4, support for long term storage
  35. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  36. * has been added to the <code>java.beans</code> package.
  37. * Please see {@link java.beans.XMLEncoder}.
  38. *
  39. * @beaninfo
  40. * attribute: isContainer false
  41. * description: A divider between menu items.
  42. *
  43. * @version 1.50 12/19/03
  44. * @author Georges Saab
  45. * @author Jeff Shapiro
  46. */
  47. public class JSeparator extends JComponent implements SwingConstants, Accessible
  48. {
  49. /**
  50. * @see #getUIClassID
  51. * @see #readObject
  52. */
  53. private static final String uiClassID = "SeparatorUI";
  54. private int orientation = HORIZONTAL;
  55. /** Creates a new horizontal separator. */
  56. public JSeparator()
  57. {
  58. this( HORIZONTAL );
  59. }
  60. /**
  61. * Creates a new separator with the specified horizontal or
  62. * vertical orientation.
  63. *
  64. * @param orientation an integer specifying
  65. * <code>SwingConstants.HORIZONTAL</code> or
  66. * <code>SwingConstants.VERTICAL</code>
  67. * @exception IllegalArgumentException if <code>orientation</code>
  68. * is neither <code>SwingConstants.HORIZONTAL</code> nor
  69. * <code>SwingConstants.VERTICAL</code>
  70. */
  71. public JSeparator( int orientation )
  72. {
  73. checkOrientation( orientation );
  74. this.orientation = orientation;
  75. setFocusable(false);
  76. updateUI();
  77. }
  78. /**
  79. * Returns the L&F object that renders this component.
  80. *
  81. * @return the SeparatorUI object that renders this component
  82. */
  83. public SeparatorUI getUI() {
  84. return (SeparatorUI)ui;
  85. }
  86. /**
  87. * Sets the L&F object that renders this component.
  88. *
  89. * @param ui the SeparatorUI L&F object
  90. * @see UIDefaults#getUI
  91. * @beaninfo
  92. * bound: true
  93. * hidden: true
  94. * attribute: visualUpdate true
  95. * description: The UI object that implements the Component's LookAndFeel.
  96. */
  97. public void setUI(SeparatorUI ui) {
  98. super.setUI(ui);
  99. }
  100. /**
  101. * Resets the UI property to a value from the current look and feel.
  102. *
  103. * @see JComponent#updateUI
  104. */
  105. public void updateUI() {
  106. setUI((SeparatorUI)UIManager.getUI(this));
  107. }
  108. /**
  109. * Returns the name of the L&F class that renders this component.
  110. *
  111. * @return the string "SeparatorUI"
  112. * @see JComponent#getUIClassID
  113. * @see UIDefaults#getUI
  114. */
  115. public String getUIClassID() {
  116. return uiClassID;
  117. }
  118. /**
  119. * See <code>readObject</code> and <code>writeObject</code> in
  120. * <code>JComponent</code> for more
  121. * information about serialization in Swing.
  122. */
  123. private void writeObject(ObjectOutputStream s) throws IOException {
  124. s.defaultWriteObject();
  125. if (getUIClassID().equals(uiClassID)) {
  126. byte count = JComponent.getWriteObjCounter(this);
  127. JComponent.setWriteObjCounter(this, --count);
  128. if (count == 0 && ui != null) {
  129. ui.installUI(this);
  130. }
  131. }
  132. }
  133. /**
  134. * Returns the orientation of this separator.
  135. *
  136. * @return The value of the orientation property, one of the
  137. * following constants defined in <code>SwingConstants</code>:
  138. * <code>VERTICAL</code>, or
  139. * <code>HORIZONTAL</code>.
  140. *
  141. * @see SwingConstants
  142. * @see #setOrientation
  143. */
  144. public int getOrientation() {
  145. return this.orientation;
  146. }
  147. /**
  148. * Sets the orientation of the separator.
  149. * The default value of this property is HORIZONTAL.
  150. * @param orientation either <code>SwingConstants.HORIZONTAL</code>
  151. * or <code>SwingConstants.VERTICAL</code>
  152. * @exception IllegalArgumentException if <code>orientation</code>
  153. * is neither <code>SwingConstants.HORIZONTAL</code>
  154. * nor <code>SwingConstants.VERTICAL</code>
  155. *
  156. * @see SwingConstants
  157. * @see #getOrientation
  158. * @beaninfo
  159. * bound: true
  160. * preferred: true
  161. * enum: HORIZONTAL SwingConstants.HORIZONTAL
  162. * VERTICAL SwingConstants.VERTICAL
  163. * attribute: visualUpdate true
  164. * description: The orientation of the separator.
  165. */
  166. public void setOrientation( int orientation ) {
  167. if (this.orientation == orientation) {
  168. return;
  169. }
  170. int oldValue = this.orientation;
  171. checkOrientation( orientation );
  172. this.orientation = orientation;
  173. firePropertyChange("orientation", oldValue, orientation);
  174. revalidate();
  175. repaint();
  176. }
  177. private void checkOrientation( int orientation )
  178. {
  179. switch ( orientation )
  180. {
  181. case VERTICAL:
  182. case HORIZONTAL:
  183. break;
  184. default:
  185. throw new IllegalArgumentException( "orientation must be one of: VERTICAL, HORIZONTAL" );
  186. }
  187. }
  188. /**
  189. * Returns a string representation of this <code>JSeparator</code>.
  190. * This method
  191. * is intended to be used only for debugging purposes, and the
  192. * content and format of the returned string may vary between
  193. * implementations. The returned string may be empty but may not
  194. * be <code>null</code>.
  195. *
  196. * @return a string representation of this <code>JSeparator</code>
  197. */
  198. protected String paramString() {
  199. String orientationString = (orientation == HORIZONTAL ?
  200. "HORIZONTAL" : "VERTICAL");
  201. return super.paramString() +
  202. ",orientation=" + orientationString;
  203. }
  204. /////////////////
  205. // Accessibility support
  206. ////////////////
  207. /**
  208. * Gets the AccessibleContext associated with this JSeparator.
  209. * For separators, the AccessibleContext takes the form of an
  210. * AccessibleJSeparator.
  211. * A new AccessibleJSeparator instance is created if necessary.
  212. *
  213. * @return an AccessibleJSeparator that serves as the
  214. * AccessibleContext of this JSeparator
  215. */
  216. public AccessibleContext getAccessibleContext() {
  217. if (accessibleContext == null) {
  218. accessibleContext = new AccessibleJSeparator();
  219. }
  220. return accessibleContext;
  221. }
  222. /**
  223. * This class implements accessibility support for the
  224. * <code>JSeparator</code> class. It provides an implementation of the
  225. * Java Accessibility API appropriate to separator user-interface elements.
  226. * <p>
  227. * <strong>Warning:</strong>
  228. * Serialized objects of this class will not be compatible with
  229. * future Swing releases. The current serialization support is
  230. * appropriate for short term storage or RMI between applications running
  231. * the same version of Swing. As of 1.4, support for long term storage
  232. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  233. * has been added to the <code>java.beans</code> package.
  234. * Please see {@link java.beans.XMLEncoder}.
  235. */
  236. protected class AccessibleJSeparator extends AccessibleJComponent {
  237. /**
  238. * Get the role of this object.
  239. *
  240. * @return an instance of AccessibleRole describing the role of the
  241. * object
  242. */
  243. public AccessibleRole getAccessibleRole() {
  244. return AccessibleRole.SEPARATOR;
  245. }
  246. }
  247. }