001 /*
002 * Copyright 2002-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 package javax.swing.plaf.synth;
026
027 import java.awt.*;
028 import javax.swing.*;
029 import javax.swing.text.JTextComponent;
030 import javax.swing.border.*;
031 import javax.swing.plaf.UIResource;
032 import sun.swing.plaf.synth.SynthUI;
033
034 /**
035 * SynthBorder is a border that delegates to a Painter. The Insets
036 * are determined at construction time.
037 *
038 * @version 1.21, 05/05/07
039 * @author Scott Violet
040 */
041 class SynthBorder extends AbstractBorder implements UIResource {
042 private SynthUI ui;
043 private Insets insets;
044
045 SynthBorder(SynthUI ui, Insets insets) {
046 this .ui = ui;
047 this .insets = insets;
048 }
049
050 SynthBorder(SynthUI ui) {
051 this (ui, null);
052 }
053
054 public void paintBorder(Component c, Graphics g, int x, int y,
055 int width, int height) {
056 JComponent jc = (JComponent) c;
057 SynthContext context = ui.getContext(jc);
058 SynthStyle style = context.getStyle();
059 if (style == null) {
060 assert false : "SynthBorder is being used outside after the UI "
061 + "has been uninstalled";
062 return;
063 }
064 ui.paintBorder(context, g, x, y, width, height);
065 context.dispose();
066 }
067
068 /**
069 * This default implementation returns a new <code>Insets</code>
070 * instance where the <code>top</code>, <code>left</code>,
071 * <code>bottom</code>, and
072 * <code>right</code> fields are set to <code>0</code>.
073 * @param c the component for which this border insets value applies
074 * @return the new <code>Insets</code> object initialized to 0
075 */
076 public Insets getBorderInsets(Component c) {
077 return getBorderInsets(c, null);
078 }
079
080 /**
081 * Reinitializes the insets parameter with this Border's current Insets.
082 * @param c the component for which this border insets value applies
083 * @param insets the object to be reinitialized
084 * @return the <code>insets</code> object
085 */
086 public Insets getBorderInsets(Component c, Insets insets) {
087 if (this .insets != null) {
088 if (insets == null) {
089 insets = new Insets(this .insets.top, this .insets.left,
090 this .insets.bottom, this .insets.right);
091 } else {
092 insets.top = this .insets.top;
093 insets.bottom = this .insets.bottom;
094 insets.left = this .insets.left;
095 insets.right = this .insets.right;
096 }
097 } else if (insets == null) {
098 insets = new Insets(0, 0, 0, 0);
099 } else {
100 insets.top = insets.bottom = insets.left = insets.right = 0;
101 }
102 if (c instanceof JComponent) {
103 Region region = Region.getRegion((JComponent) c);
104 Insets margin = null;
105 if ((region == Region.ARROW_BUTTON
106 || region == Region.BUTTON
107 || region == Region.CHECK_BOX
108 || region == Region.CHECK_BOX_MENU_ITEM
109 || region == Region.MENU
110 || region == Region.MENU_ITEM
111 || region == Region.RADIO_BUTTON
112 || region == Region.RADIO_BUTTON_MENU_ITEM || region == Region.TOGGLE_BUTTON)
113 && (c instanceof AbstractButton)) {
114 margin = ((AbstractButton) c).getMargin();
115 } else if ((region == Region.EDITOR_PANE
116 || region == Region.FORMATTED_TEXT_FIELD
117 || region == Region.PASSWORD_FIELD
118 || region == Region.TEXT_AREA
119 || region == Region.TEXT_FIELD || region == Region.TEXT_PANE)
120 && (c instanceof JTextComponent)) {
121 margin = ((JTextComponent) c).getMargin();
122 } else if (region == Region.TOOL_BAR
123 && (c instanceof JToolBar)) {
124 margin = ((JToolBar) c).getMargin();
125 } else if (region == Region.MENU_BAR
126 && (c instanceof JMenuBar)) {
127 margin = ((JMenuBar) c).getMargin();
128 }
129 if (margin != null) {
130 insets.top += margin.top;
131 insets.bottom += margin.bottom;
132 insets.left += margin.left;
133 insets.right += margin.right;
134 }
135 }
136 return insets;
137 }
138
139 /**
140 * This default implementation returns false.
141 * @return false
142 */
143 public boolean isBorderOpaque() {
144 return false;
145 }
146 }
|