1. /*
  2. * @(#)JSeparator.java 1.35 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;
  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. * An implementation of a Menu Separator -- a divider between menu items
  15. * that breaks them up into logical groupings.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is appropriate
  20. * for short term storage or RMI between applications running the same
  21. * version of Swing. A future release of Swing will provide support for
  22. * long term persistence.
  23. *
  24. * @beaninfo
  25. * attribute: isContainer false
  26. * @version 1.35 11/29/01
  27. * @author Georges Saab
  28. * @author Jeff Shapiro
  29. */
  30. public class JSeparator extends JComponent implements SwingConstants, Accessible
  31. {
  32. /**
  33. * @see #getUIClassID
  34. * @see #readObject
  35. */
  36. private static final String uiClassID = "SeparatorUI";
  37. private int orientation = HORIZONTAL;
  38. /** Create a new horizontal separator. */
  39. public JSeparator()
  40. {
  41. this( HORIZONTAL );
  42. }
  43. /**
  44. * Create a new separator with the specified horizontal or
  45. * vertical orientation.
  46. *
  47. * @param orientation an int specifying SwingConstants.HORIZONTAL or
  48. * SwingConstants.VERTICAL
  49. */
  50. public JSeparator( int orientation )
  51. {
  52. checkOrientation( orientation );
  53. this.orientation = orientation;
  54. updateUI();
  55. }
  56. /**
  57. * Returns the L&F object that renders this component.
  58. *
  59. * @return the SeparatorUI object that renders this component
  60. */
  61. public SeparatorUI getUI() {
  62. return (SeparatorUI)ui;
  63. }
  64. /**
  65. * Sets the L&F object that renders this component.
  66. *
  67. * @param ui the SeparatorUI L&F object
  68. * @see UIDefaults#getUI
  69. * @beaninfo
  70. * description: The menu item's UI delegate
  71. * bound: true
  72. * expert: true
  73. * hidden: true
  74. */
  75. public void setUI(SeparatorUI ui) {
  76. super.setUI(ui);
  77. }
  78. /**
  79. * Notification from the UIFactory that the L&F has changed.
  80. * Called to replace the UI with the latest version from the
  81. * UIFactory.
  82. *
  83. * @see JComponent#updateUI
  84. */
  85. public void updateUI() {
  86. setUI((SeparatorUI)UIManager.getUI(this));
  87. }
  88. /**
  89. * Returns the name of the L&F class that renders this component.
  90. *
  91. * @return "SeparatorUI"
  92. * @see JComponent#getUIClassID
  93. * @see UIDefaults#getUI
  94. */
  95. public String getUIClassID() {
  96. return uiClassID;
  97. }
  98. /**
  99. * See readObject() and writeObject() in JComponent for more
  100. * information about serialization in Swing.
  101. */
  102. private void writeObject(ObjectOutputStream s) throws IOException {
  103. s.defaultWriteObject();
  104. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  105. ui.installUI(this);
  106. }
  107. }
  108. /**
  109. * Returns the orientation of this separator.
  110. *
  111. * @return The value of the orientation property, one of the
  112. * following constants defined in <code>SwingConstants</code>:
  113. * <code>VERTICAL</code>, or
  114. * <code>HORIZONTAL</code>.
  115. *
  116. * @see SwingConstants
  117. * @see #setOrientation
  118. */
  119. public int getOrientation() {
  120. return this.orientation;
  121. }
  122. /**
  123. * Sets the orientation of the separator.
  124. * The default value of this property is HORIZONTAL.
  125. *
  126. * @see SwingConstants
  127. * @see #getOrientation
  128. * @beaninfo
  129. * bound: true
  130. * preferred: true
  131. * enum: HORIZONTAL SwingConstants.HORIZONTAL
  132. * VERTICAL SwingConstants.VERTICAL
  133. * attribute: visualUpdate true
  134. * description: The orientation of the separator.
  135. */
  136. public void setOrientation( int orientation ) {
  137. if (this.orientation == orientation) {
  138. return;
  139. }
  140. int oldValue = this.orientation;
  141. checkOrientation( orientation );
  142. this.orientation = orientation;
  143. firePropertyChange("orientation", oldValue, orientation);
  144. revalidate();
  145. repaint();
  146. }
  147. private void checkOrientation( int orientation )
  148. {
  149. switch ( orientation )
  150. {
  151. case VERTICAL:
  152. case HORIZONTAL:
  153. break;
  154. default:
  155. throw new IllegalArgumentException( "orientation must be one of: VERTICAL, HORIZONTAL" );
  156. }
  157. }
  158. /**
  159. * Returns a string representation of this JSeparator. This method
  160. * is intended to be used only for debugging purposes, and the
  161. * content and format of the returned string may vary between
  162. * implementations. The returned string may be empty but may not
  163. * be <code>null</code>.
  164. *
  165. * @return a string representation of this JSeparator.
  166. */
  167. protected String paramString() {
  168. String orientationString = (orientation == HORIZONTAL ?
  169. "HORIZONTAL" : "VERTICAL");
  170. return super.paramString() +
  171. ",orientation=" + orientationString;
  172. }
  173. /**
  174. * Identifies whether or not this component can receive the focus.
  175. * JSeparators cannot recieve focus.
  176. *
  177. * @return false
  178. */
  179. public boolean isFocusTraversable()
  180. {
  181. return false;
  182. }
  183. /////////////////
  184. // Accessibility support
  185. ////////////////
  186. /**
  187. * Get the AccessibleContext associated with this JComponent
  188. *
  189. * @return the AccessibleContext of this JComponent
  190. */
  191. public AccessibleContext getAccessibleContext() {
  192. if (accessibleContext == null) {
  193. accessibleContext = new AccessibleJSeparator();
  194. }
  195. return accessibleContext;
  196. }
  197. /**
  198. * The class used to obtain the accessible role for this object.
  199. * <p>
  200. * <strong>Warning:</strong>
  201. * Serialized objects of this class will not be compatible with
  202. * future Swing releases. The current serialization support is appropriate
  203. * for short term storage or RMI between applications running the same
  204. * version of Swing. A future release of Swing will provide support for
  205. * long term persistence.
  206. */
  207. protected class AccessibleJSeparator extends AccessibleJComponent {
  208. /**
  209. * Get the role of this object.
  210. *
  211. * @return an instance of AccessibleRole describing the role of the
  212. * object
  213. */
  214. public AccessibleRole getAccessibleRole() {
  215. return AccessibleRole.SEPARATOR;
  216. }
  217. }
  218. }