001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing;
027
028 import javax.swing.plaf.*;
029 import javax.accessibility.*;
030
031 import java.io.ObjectOutputStream;
032 import java.io.ObjectInputStream;
033 import java.io.IOException;
034
035 /**
036 * <code>JSeparator</code> provides a general purpose component for
037 * implementing divider lines - most commonly used as a divider
038 * between menu items that breaks them up into logical groupings.
039 * Instead of using <code>JSeparator</code> directly,
040 * you can use the <code>JMenu</code> or <code>JPopupMenu</code>
041 * <code>addSeparator</code> method to create and add a separator.
042 * <code>JSeparator</code>s may also be used elsewhere in a GUI
043 * wherever a visual divider is useful.
044 *
045 * <p>
046 *
047 * For more information and examples see
048 * <a
049 href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
050 * a section in <em>The Java Tutorial.</em>
051 * <p>
052 * <strong>Warning:</strong> Swing is not thread safe. For more
053 * information see <a
054 * href="package-summary.html#threading">Swing's Threading
055 * Policy</a>.
056 * <p>
057 * <strong>Warning:</strong>
058 * Serialized objects of this class will not be compatible with
059 * future Swing releases. The current serialization support is
060 * appropriate for short term storage or RMI between applications running
061 * the same version of Swing. As of 1.4, support for long term storage
062 * of all JavaBeans<sup><font size="-2">TM</font></sup>
063 * has been added to the <code>java.beans</code> package.
064 * Please see {@link java.beans.XMLEncoder}.
065 *
066 * @beaninfo
067 * attribute: isContainer false
068 * description: A divider between menu items.
069 *
070 * @version 1.58 05/05/07
071 * @author Georges Saab
072 * @author Jeff Shapiro
073 */
074 public class JSeparator extends JComponent implements SwingConstants,
075 Accessible {
076 /**
077 * @see #getUIClassID
078 * @see #readObject
079 */
080 private static final String uiClassID = "SeparatorUI";
081
082 private int orientation = HORIZONTAL;
083
084 /** Creates a new horizontal separator. */
085 public JSeparator() {
086 this (HORIZONTAL);
087 }
088
089 /**
090 * Creates a new separator with the specified horizontal or
091 * vertical orientation.
092 *
093 * @param orientation an integer specifying
094 * <code>SwingConstants.HORIZONTAL</code> or
095 * <code>SwingConstants.VERTICAL</code>
096 * @exception IllegalArgumentException if <code>orientation</code>
097 * is neither <code>SwingConstants.HORIZONTAL</code> nor
098 * <code>SwingConstants.VERTICAL</code>
099 */
100 public JSeparator(int orientation) {
101 checkOrientation(orientation);
102 this .orientation = orientation;
103 setFocusable(false);
104 updateUI();
105 }
106
107 /**
108 * Returns the L&F object that renders this component.
109 *
110 * @return the SeparatorUI object that renders this component
111 */
112 public SeparatorUI getUI() {
113 return (SeparatorUI) ui;
114 }
115
116 /**
117 * Sets the L&F object that renders this component.
118 *
119 * @param ui the SeparatorUI L&F object
120 * @see UIDefaults#getUI
121 * @beaninfo
122 * bound: true
123 * hidden: true
124 * attribute: visualUpdate true
125 * description: The UI object that implements the Component's LookAndFeel.
126 */
127 public void setUI(SeparatorUI ui) {
128 super .setUI(ui);
129 }
130
131 /**
132 * Resets the UI property to a value from the current look and feel.
133 *
134 * @see JComponent#updateUI
135 */
136 public void updateUI() {
137 setUI((SeparatorUI) UIManager.getUI(this ));
138 }
139
140 /**
141 * Returns the name of the L&F class that renders this component.
142 *
143 * @return the string "SeparatorUI"
144 * @see JComponent#getUIClassID
145 * @see UIDefaults#getUI
146 */
147 public String getUIClassID() {
148 return uiClassID;
149 }
150
151 /**
152 * See <code>readObject</code> and <code>writeObject</code> in
153 * <code>JComponent</code> for more
154 * information about serialization in Swing.
155 */
156 private void writeObject(ObjectOutputStream s) throws IOException {
157 s.defaultWriteObject();
158 if (getUIClassID().equals(uiClassID)) {
159 byte count = JComponent.getWriteObjCounter(this );
160 JComponent.setWriteObjCounter(this , --count);
161 if (count == 0 && ui != null) {
162 ui.installUI(this );
163 }
164 }
165 }
166
167 /**
168 * Returns the orientation of this separator.
169 *
170 * @return The value of the orientation property, one of the
171 * following constants defined in <code>SwingConstants</code>:
172 * <code>VERTICAL</code>, or
173 * <code>HORIZONTAL</code>.
174 *
175 * @see SwingConstants
176 * @see #setOrientation
177 */
178 public int getOrientation() {
179 return this .orientation;
180 }
181
182 /**
183 * Sets the orientation of the separator.
184 * The default value of this property is HORIZONTAL.
185 * @param orientation either <code>SwingConstants.HORIZONTAL</code>
186 * or <code>SwingConstants.VERTICAL</code>
187 * @exception IllegalArgumentException if <code>orientation</code>
188 * is neither <code>SwingConstants.HORIZONTAL</code>
189 * nor <code>SwingConstants.VERTICAL</code>
190 *
191 * @see SwingConstants
192 * @see #getOrientation
193 * @beaninfo
194 * bound: true
195 * preferred: true
196 * enum: HORIZONTAL SwingConstants.HORIZONTAL
197 * VERTICAL SwingConstants.VERTICAL
198 * attribute: visualUpdate true
199 * description: The orientation of the separator.
200 */
201 public void setOrientation(int orientation) {
202 if (this .orientation == orientation) {
203 return;
204 }
205 int oldValue = this .orientation;
206 checkOrientation(orientation);
207 this .orientation = orientation;
208 firePropertyChange("orientation", oldValue, orientation);
209 revalidate();
210 repaint();
211 }
212
213 private void checkOrientation(int orientation) {
214 switch (orientation) {
215 case VERTICAL:
216 case HORIZONTAL:
217 break;
218 default:
219 throw new IllegalArgumentException(
220 "orientation must be one of: VERTICAL, HORIZONTAL");
221 }
222 }
223
224 /**
225 * Returns a string representation of this <code>JSeparator</code>.
226 * This method
227 * is intended to be used only for debugging purposes, and the
228 * content and format of the returned string may vary between
229 * implementations. The returned string may be empty but may not
230 * be <code>null</code>.
231 *
232 * @return a string representation of this <code>JSeparator</code>
233 */
234 protected String paramString() {
235 String orientationString = (orientation == HORIZONTAL ? "HORIZONTAL"
236 : "VERTICAL");
237
238 return super .paramString() + ",orientation="
239 + orientationString;
240 }
241
242 /////////////////
243 // Accessibility support
244 ////////////////
245
246 /**
247 * Gets the AccessibleContext associated with this JSeparator.
248 * For separators, the AccessibleContext takes the form of an
249 * AccessibleJSeparator.
250 * A new AccessibleJSeparator instance is created if necessary.
251 *
252 * @return an AccessibleJSeparator that serves as the
253 * AccessibleContext of this JSeparator
254 */
255 public AccessibleContext getAccessibleContext() {
256 if (accessibleContext == null) {
257 accessibleContext = new AccessibleJSeparator();
258 }
259 return accessibleContext;
260 }
261
262 /**
263 * This class implements accessibility support for the
264 * <code>JSeparator</code> class. It provides an implementation of the
265 * Java Accessibility API appropriate to separator user-interface elements.
266 * <p>
267 * <strong>Warning:</strong>
268 * Serialized objects of this class will not be compatible with
269 * future Swing releases. The current serialization support is
270 * appropriate for short term storage or RMI between applications running
271 * the same version of Swing. As of 1.4, support for long term storage
272 * of all JavaBeans<sup><font size="-2">TM</font></sup>
273 * has been added to the <code>java.beans</code> package.
274 * Please see {@link java.beans.XMLEncoder}.
275 */
276 protected class AccessibleJSeparator extends AccessibleJComponent {
277
278 /**
279 * Get the role of this object.
280 *
281 * @return an instance of AccessibleRole describing the role of the
282 * object
283 */
284 public AccessibleRole getAccessibleRole() {
285 return AccessibleRole.SEPARATOR;
286 }
287 }
288 }
|