1. /*
  2. * @(#)JSeparator.java 1.42 00/04/06
  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;
  11. import javax.swing.plaf.*;
  12. import javax.accessibility.*;
  13. import java.io.ObjectOutputStream;
  14. import java.io.ObjectInputStream;
  15. import java.io.IOException;
  16. /**
  17. * An implementation of a menu separator -- a divider between menu items
  18. * that breaks them up into logical groupings.
  19. * Instead of using <code>JSeparator</code> directly,
  20. * you can use the <code>JMenu</code> or <code>JPopupMenu</code>
  21. * <code>addSeparator</code> method
  22. * to create and add a separator.
  23. *
  24. * <p>
  25. *
  26. * For more information and examples see
  27. * <a
  28. href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
  29. * a section in <em>The Java Tutorial.</em>
  30. * <p>
  31. * <strong>Warning:</strong>
  32. * Serialized objects of this class will not be compatible with
  33. * future Swing releases. The current serialization support is appropriate
  34. * for short term storage or RMI between applications running the same
  35. * version of Swing. A future release of Swing will provide support for
  36. * long term persistence.
  37. *
  38. * @beaninfo
  39. * attribute: isContainer false
  40. * description: A divider between menu items.
  41. *
  42. * @version 1.42 04/06/00
  43. * @author Georges Saab
  44. * @author Jeff Shapiro
  45. */
  46. public class JSeparator extends JComponent implements SwingConstants, Accessible
  47. {
  48. /**
  49. * @see #getUIClassID
  50. * @see #readObject
  51. */
  52. private static final String uiClassID = "SeparatorUI";
  53. private int orientation = HORIZONTAL;
  54. /** Creates a new horizontal separator. */
  55. public JSeparator()
  56. {
  57. this( HORIZONTAL );
  58. }
  59. /**
  60. * Creates a new separator with the specified horizontal or
  61. * vertical orientation.
  62. *
  63. * @param orientation an integer specifying
  64. * <code>SwingConstants.HORIZONTAL</code> or
  65. * <code>SwingConstants.VERTICAL</code>
  66. * @exception IllegalArgumentException if <code>orientation</code>
  67. * is neither <code>SwingConstants.HORIZONTAL</code> nor
  68. * <code>SwingConstants.VERTICAL</code>
  69. */
  70. public JSeparator( int orientation )
  71. {
  72. checkOrientation( orientation );
  73. this.orientation = orientation;
  74. updateUI();
  75. }
  76. /**
  77. * Returns the L&F object that renders this component.
  78. *
  79. * @return the SeparatorUI object that renders this component
  80. */
  81. public SeparatorUI getUI() {
  82. return (SeparatorUI)ui;
  83. }
  84. /**
  85. * Sets the L&F object that renders this component.
  86. *
  87. * @param ui the SeparatorUI L&F object
  88. * @see UIDefaults#getUI
  89. * @beaninfo
  90. * description: The menu item's UI delegate
  91. * bound: true
  92. * expert: true
  93. * hidden: true
  94. */
  95. public void setUI(SeparatorUI ui) {
  96. super.setUI(ui);
  97. }
  98. /**
  99. * Notification from the <code>UIFactory</code> that the L&F has changed.
  100. * Called to replace the UI with the latest version from the
  101. * <code>UIFactorys</code>.
  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 ((ui != null) && (getUIClassID().equals(uiClassID))) {
  126. ui.installUI(this);
  127. }
  128. }
  129. /**
  130. * Returns the orientation of this separator.
  131. *
  132. * @return The value of the orientation property, one of the
  133. * following constants defined in <code>SwingConstants</code>:
  134. * <code>VERTICAL</code>, or
  135. * <code>HORIZONTAL</code>.
  136. *
  137. * @see SwingConstants
  138. * @see #setOrientation
  139. */
  140. public int getOrientation() {
  141. return this.orientation;
  142. }
  143. /**
  144. * Sets the orientation of the separator.
  145. * The default value of this property is HORIZONTAL.
  146. * @param orientation either <code>SwingConstants.HORIZONTAL</code>
  147. * or <code>SwingConstants.VERTICAL</code>
  148. * @exception IllegalArgumentException if <code>orientation</code>
  149. * is neither <code>SwingConstants.HORIZONTAL</code>
  150. * nor <code>SwingConstants.VERTICAL</code>
  151. *
  152. * @see SwingConstants
  153. * @see #getOrientation
  154. * @beaninfo
  155. * bound: true
  156. * preferred: true
  157. * enum: HORIZONTAL SwingConstants.HORIZONTAL
  158. * VERTICAL SwingConstants.VERTICAL
  159. * attribute: visualUpdate true
  160. * description: The orientation of the separator.
  161. */
  162. public void setOrientation( int orientation ) {
  163. if (this.orientation == orientation) {
  164. return;
  165. }
  166. int oldValue = this.orientation;
  167. checkOrientation( orientation );
  168. this.orientation = orientation;
  169. firePropertyChange("orientation", oldValue, orientation);
  170. revalidate();
  171. repaint();
  172. }
  173. private void checkOrientation( int orientation )
  174. {
  175. switch ( orientation )
  176. {
  177. case VERTICAL:
  178. case HORIZONTAL:
  179. break;
  180. default:
  181. throw new IllegalArgumentException( "orientation must be one of: VERTICAL, HORIZONTAL" );
  182. }
  183. }
  184. /**
  185. * Returns a string representation of this <code>JSeparator</code>.
  186. * This method
  187. * is intended to be used only for debugging purposes, and the
  188. * content and format of the returned string may vary between
  189. * implementations. The returned string may be empty but may not
  190. * be <code>null</code>.
  191. *
  192. * @return a string representation of this <code>JSeparator</code>
  193. */
  194. protected String paramString() {
  195. String orientationString = (orientation == HORIZONTAL ?
  196. "HORIZONTAL" : "VERTICAL");
  197. return super.paramString() +
  198. ",orientation=" + orientationString;
  199. }
  200. /**
  201. * Identifies whether or not this component can receive the focus.
  202. * <code>JSeparator</code>s cannot recieve focus.
  203. *
  204. * @return false
  205. */
  206. public boolean isFocusTraversable()
  207. {
  208. return false;
  209. }
  210. /////////////////
  211. // Accessibility support
  212. ////////////////
  213. /**
  214. * Gets the AccessibleContext associated with this JSeparator.
  215. * For separators, the AccessibleContext takes the form of an
  216. * AccessibleJSeparator.
  217. * A new AccessibleJSeparator instance is created if necessary.
  218. *
  219. * @return an AccessibleJSeparator that serves as the
  220. * AccessibleContext of this JSeparator
  221. */
  222. public AccessibleContext getAccessibleContext() {
  223. if (accessibleContext == null) {
  224. accessibleContext = new AccessibleJSeparator();
  225. }
  226. return accessibleContext;
  227. }
  228. /**
  229. * This class implements accessibility support for the
  230. * <code>JSeparator</code> class. It provides an implementation of the
  231. * Java Accessibility API appropriate to separator user-interface elements.
  232. * <p>
  233. * <strong>Warning:</strong>
  234. * Serialized objects of this class will not be compatible with
  235. * future Swing releases. The current serialization support is appropriate
  236. * for short term storage or RMI between applications running the same
  237. * version of Swing. A future release of Swing will provide support for
  238. * long term persistence.
  239. */
  240. protected class AccessibleJSeparator extends AccessibleJComponent {
  241. /**
  242. * Get the role of this object.
  243. *
  244. * @return an instance of AccessibleRole describing the role of the
  245. * object
  246. */
  247. public AccessibleRole getAccessibleRole() {
  248. return AccessibleRole.SEPARATOR;
  249. }
  250. }
  251. }