KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JSeparator


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
8 package javax.swing;
9
10 import javax.swing.plaf.*;
11 import javax.accessibility.*;
12
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16
17
18 /**
19  * <code>JSeparator</code> provides a general purpose component for
20  * implementing divider lines - most commonly used as a divider
21  * between menu items that breaks them up into logical groupings.
22  * Instead of using <code>JSeparator</code> directly,
23  * you can use the <code>JMenu</code> or <code>JPopupMenu</code>
24  * <code>addSeparator</code> method to create and add a separator.
25  * <code>JSeparator</code>s may also be used elsewhere in a GUI
26  * wherever a visual divider is useful.
27  *
28  * <p>
29  *
30  * For more information and examples see
31  * <a
32  href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
33  * a section in <em>The Java Tutorial.</em>
34  * <p>
35  * <strong>Warning:</strong>
36  * Serialized objects of this class will not be compatible with
37  * future Swing releases. The current serialization support is
38  * appropriate for short term storage or RMI between applications running
39  * the same version of Swing. As of 1.4, support for long term storage
40  * of all JavaBeans<sup><font size="-2">TM</font></sup>
41  * has been added to the <code>java.beans</code> package.
42  * Please see {@link java.beans.XMLEncoder}.
43  *
44  * @beaninfo
45  * attribute: isContainer false
46  * description: A divider between menu items.
47  *
48  * @version 1.50 12/19/03
49  * @author Georges Saab
50  * @author Jeff Shapiro
51  */

52 public class JSeparator extends JComponent JavaDoc implements SwingConstants JavaDoc, Accessible
53 {
54     /**
55      * @see #getUIClassID
56      * @see #readObject
57      */

58     private static final String JavaDoc uiClassID = "SeparatorUI";
59
60     private int orientation = HORIZONTAL;
61
62     /** Creates a new horizontal separator. */
63     public JSeparator()
64     {
65         this( HORIZONTAL );
66     }
67
68     /**
69      * Creates a new separator with the specified horizontal or
70      * vertical orientation.
71      *
72      * @param orientation an integer specifying
73      * <code>SwingConstants.HORIZONTAL</code> or
74      * <code>SwingConstants.VERTICAL</code>
75      * @exception IllegalArgumentException if <code>orientation</code>
76      * is neither <code>SwingConstants.HORIZONTAL</code> nor
77      * <code>SwingConstants.VERTICAL</code>
78      */

79     public JSeparator( int orientation )
80     {
81         checkOrientation( orientation );
82     this.orientation = orientation;
83     setFocusable(false);
84         updateUI();
85     }
86
87     /**
88      * Returns the L&F object that renders this component.
89      *
90      * @return the SeparatorUI object that renders this component
91      */

92     public SeparatorUI getUI() {
93         return (SeparatorUI)ui;
94     }
95     
96     /**
97      * Sets the L&F object that renders this component.
98      *
99      * @param ui the SeparatorUI L&F object
100      * @see UIDefaults#getUI
101      * @beaninfo
102      * bound: true
103      * hidden: true
104      * attribute: visualUpdate true
105      * description: The UI object that implements the Component's LookAndFeel.
106      */

107     public void setUI(SeparatorUI ui) {
108         super.setUI(ui);
109     }
110     
111     /**
112      * Resets the UI property to a value from the current look and feel.
113      *
114      * @see JComponent#updateUI
115      */

116     public void updateUI() {
117         setUI((SeparatorUI)UIManager.getUI(this));
118     }
119     
120
121     /**
122      * Returns the name of the L&F class that renders this component.
123      *
124      * @return the string "SeparatorUI"
125      * @see JComponent#getUIClassID
126      * @see UIDefaults#getUI
127      */

128     public String JavaDoc getUIClassID() {
129         return uiClassID;
130     }
131
132
133     /**
134      * See <code>readObject</code> and <code>writeObject</code> in
135      * <code>JComponent</code> for more
136      * information about serialization in Swing.
137      */

138     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
139         s.defaultWriteObject();
140         if (getUIClassID().equals(uiClassID)) {
141             byte count = JComponent.getWriteObjCounter(this);
142             JComponent.setWriteObjCounter(this, --count);
143             if (count == 0 && ui != null) {
144                 ui.installUI(this);
145             }
146         }
147     }
148
149     /**
150      * Returns the orientation of this separator.
151      *
152      * @return The value of the orientation property, one of the
153      * following constants defined in <code>SwingConstants</code>:
154      * <code>VERTICAL</code>, or
155      * <code>HORIZONTAL</code>.
156      *
157      * @see SwingConstants
158      * @see #setOrientation
159      */

160     public int getOrientation() {
161         return this.orientation;
162     }
163
164     /**
165      * Sets the orientation of the separator.
166      * The default value of this property is HORIZONTAL.
167      * @param orientation either <code>SwingConstants.HORIZONTAL</code>
168      * or <code>SwingConstants.VERTICAL</code>
169      * @exception IllegalArgumentException if <code>orientation</code>
170      * is neither <code>SwingConstants.HORIZONTAL</code>
171      * nor <code>SwingConstants.VERTICAL</code>
172      *
173      * @see SwingConstants
174      * @see #getOrientation
175      * @beaninfo
176      * bound: true
177      * preferred: true
178      * enum: HORIZONTAL SwingConstants.HORIZONTAL
179      * VERTICAL SwingConstants.VERTICAL
180      * attribute: visualUpdate true
181      * description: The orientation of the separator.
182      */

183     public void setOrientation( int orientation ) {
184         if (this.orientation == orientation) {
185             return;
186         }
187         int oldValue = this.orientation;
188         checkOrientation( orientation );
189         this.orientation = orientation;
190         firePropertyChange("orientation", oldValue, orientation);
191         revalidate();
192     repaint();
193     }
194
195     private void checkOrientation( int orientation )
196     {
197         switch ( orientation )
198     {
199             case VERTICAL:
200             case HORIZONTAL:
201                 break;
202             default:
203                 throw new IllegalArgumentException JavaDoc( "orientation must be one of: VERTICAL, HORIZONTAL" );
204         }
205     }
206
207
208     /**
209      * Returns a string representation of this <code>JSeparator</code>.
210      * This method
211      * is intended to be used only for debugging purposes, and the
212      * content and format of the returned string may vary between
213      * implementations. The returned string may be empty but may not
214      * be <code>null</code>.
215      *
216      * @return a string representation of this <code>JSeparator</code>
217      */

218     protected String JavaDoc paramString() {
219     String JavaDoc orientationString = (orientation == HORIZONTAL ?
220                     "HORIZONTAL" : "VERTICAL");
221
222     return super.paramString() +
223     ",orientation=" + orientationString;
224     }
225
226 /////////////////
227
// Accessibility support
228
////////////////
229

230     /**
231      * Gets the AccessibleContext associated with this JSeparator.
232      * For separators, the AccessibleContext takes the form of an
233      * AccessibleJSeparator.
234      * A new AccessibleJSeparator instance is created if necessary.
235      *
236      * @return an AccessibleJSeparator that serves as the
237      * AccessibleContext of this JSeparator
238      */

239     public AccessibleContext getAccessibleContext() {
240         if (accessibleContext == null) {
241             accessibleContext = new AccessibleJSeparator();
242         }
243         return accessibleContext;
244     }
245
246     /**
247      * This class implements accessibility support for the
248      * <code>JSeparator</code> class. It provides an implementation of the
249      * Java Accessibility API appropriate to separator user-interface elements.
250      * <p>
251      * <strong>Warning:</strong>
252      * Serialized objects of this class will not be compatible with
253      * future Swing releases. The current serialization support is
254      * appropriate for short term storage or RMI between applications running
255      * the same version of Swing. As of 1.4, support for long term storage
256      * of all JavaBeans<sup><font size="-2">TM</font></sup>
257      * has been added to the <code>java.beans</code> package.
258      * Please see {@link java.beans.XMLEncoder}.
259      */

260     protected class AccessibleJSeparator extends AccessibleJComponent {
261
262         /**
263          * Get the role of this object.
264          *
265          * @return an instance of AccessibleRole describing the role of the
266          * object
267          */

268         public AccessibleRole getAccessibleRole() {
269             return AccessibleRole.SEPARATOR;
270         }
271     }
272 }
273
Popular Tags