001 /* BasicSeparatorUI.java --
002 Copyright (C) 2004 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.swing.plaf.basic;
040
041 import java.awt.Color;
042 import java.awt.Dimension;
043 import java.awt.Graphics;
044 import java.awt.Rectangle;
045
046 import javax.swing.JComponent;
047 import javax.swing.JSeparator;
048 import javax.swing.SwingUtilities;
049 import javax.swing.UIManager;
050 import javax.swing.plaf.ComponentUI;
051 import javax.swing.plaf.SeparatorUI;
052
053 /**
054 * The Basic Look and Feel UI delegate for JSeparator.
055 */
056 public class BasicSeparatorUI extends SeparatorUI
057 {
058 /** The shadow color. */
059 protected Color shadow;
060
061 /** The highlight color. */
062 protected Color highlight;
063
064 /**
065 * Creates a new UI delegate for the given JComponent.
066 *
067 * @param c The JComponent to create a delegate for.
068 *
069 * @return A new BasicSeparatorUI.
070 */
071 public static ComponentUI createUI(JComponent c)
072 {
073 return new BasicSeparatorUI();
074 }
075
076 /**
077 * This method installs the UI for the given JComponent.
078 * This can include installing defaults, listeners, and
079 * initializing any instance data.
080 *
081 * @param c The JComponent that is having this UI installed.
082 */
083 public void installUI(JComponent c)
084 {
085 super.installUI(c);
086
087 if (c instanceof JSeparator)
088 {
089 JSeparator s = (JSeparator) c;
090
091 installDefaults(s);
092 installListeners(s);
093 }
094 }
095
096 /**
097 * Uninstalls the UI for the given JComponent. This
098 * method reverses what was done when installing
099 * the UI on the JComponent.
100 *
101 * @param c The JComponent that is having this UI uninstalled.
102 */
103 public void uninstallUI(JComponent c)
104 {
105 if (c instanceof JSeparator)
106 {
107 JSeparator s = (JSeparator) c;
108
109 uninstallListeners(s);
110 uninstallDefaults(s);
111 }
112 }
113
114 /**
115 * This method installs the defaults that are given by
116 * the Basic Look and Feel.
117 *
118 * @param s The JSeparator that is being installed.
119 */
120 protected void installDefaults(JSeparator s)
121 {
122 shadow = UIManager.getColor("Separator.shadow");
123 highlight = UIManager.getColor("Separator.highlight");
124 s.setOpaque(false);
125 }
126
127 /**
128 * This method removes the defaults that were given
129 * by the Basic Look and Feel.
130 *
131 * @param s The JSeparator that is being uninstalled.
132 */
133 protected void uninstallDefaults(JSeparator s)
134 {
135 shadow = null;
136 highlight = null;
137 }
138
139 /**
140 * This method installs any listeners that need
141 * to be attached to the JSeparator or any of its
142 * components.
143 *
144 * @param s The JSeparator that is being installed.
145 */
146 protected void installListeners(JSeparator s)
147 {
148 // Separators don't receive events.
149 }
150
151 /**
152 * This method uninstalls any listeners that
153 * were installed during the install UI process.
154 *
155 * @param s The JSeparator that is being uninstalled.
156 */
157 protected void uninstallListeners(JSeparator s)
158 {
159 // Separators don't receive events.
160 }
161
162 /**
163 * The separator is made of two lines. The top line will be
164 * the shadow color (or left line if it's vertical). The bottom
165 * or right line will be the highlight color. The two lines will
166 * be centered inside the bounds box. If the separator is horizontal,
167 * then it will be vertically centered, or if it's vertical, it will
168 * be horizontally centered.
169 *
170 * @param g The Graphics object to paint with
171 * @param c The JComponent to paint.
172 */
173 public void paint(Graphics g, JComponent c)
174 {
175 Rectangle r = new Rectangle();
176 SwingUtilities.calculateInnerArea(c, r);
177 Color saved = g.getColor();
178
179 JSeparator s;
180 if (c instanceof JSeparator)
181 s = (JSeparator) c;
182 else
183 return;
184
185 if (s.getOrientation() == JSeparator.HORIZONTAL)
186 {
187 int midAB = r.height / 2;
188 g.setColor(shadow);
189 g.drawLine(r.x, r.y + midAB - 1, r.x + r.width, r.y + midAB - 1);
190
191 g.setColor(highlight);
192 g.fillRect(r.x, r.y + midAB, r.x + r.width, r.y + midAB);
193 }
194 else
195 {
196 int midAD = r.height / 2 + r.y;
197 g.setColor(shadow);
198 g.drawLine(r.x, r.y, r.x, r.y + r.height);
199
200 g.setColor(highlight);
201 g.fillRect(r.x + midAD, r.y + r.height, r.x + midAD, r.y + r.height);
202 }
203 g.setColor(saved);
204 }
205
206 /**
207 * This method returns the preferred size of the
208 * JComponent.
209 *
210 * @param c The JComponent to measure.
211 *
212 * @return The preferred size.
213 */
214 public Dimension getPreferredSize(JComponent c)
215 {
216 Dimension pref = new Dimension(2, 0);
217 if (c instanceof JSeparator)
218 {
219 JSeparator s = (JSeparator) c;
220 if (s.getOrientation() == JSeparator.HORIZONTAL)
221 pref = new Dimension(0, 2);
222 }
223 return pref;
224 }
225
226 /**
227 * This method returns the minimum size of the
228 * JComponent.
229 *
230 * @param c The JComponent to measure.
231 *
232 * @return The minimum size.
233 */
234 public Dimension getMinimumSize(JComponent c)
235 {
236 return new Dimension(0, 0);
237 }
238
239 /**
240 * This method returns the maximum size of the
241 * JComponent.
242 *
243 * @param c The JComponent to measure.
244 *
245 * @return The maximum size.
246 */
247 public Dimension getMaximumSize(JComponent c)
248 {
249 return new Dimension(Short.MAX_VALUE,
250 Short.MAX_VALUE);
251 }
252 }