001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander T. Simbirtsev
020: * @version $Revision$
021: */package javax.swing.plaf.basic;
022:
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.Graphics;
026: import java.awt.Insets;
027: import java.lang.reflect.Method;
028: import java.security.AccessController;
029: import java.security.PrivilegedAction;
030:
031: import javax.swing.AbstractButton;
032: import javax.swing.UIManager;
033: import javax.swing.border.AbstractBorder;
034: import javax.swing.border.BevelBorder;
035: import javax.swing.border.Border;
036: import javax.swing.plaf.BorderUIResource;
037: import javax.swing.plaf.UIResource;
038: import javax.swing.text.JTextComponent;
039:
040: import org.apache.harmony.x.swing.Utilities;
041:
042: public class BasicBorders {
043:
044: /*
045: * All color constants used in this class are selected to be consistent with
046: * Basic L&F color pattern.
047: */
048:
049: public static class ButtonBorder extends AbstractBorder implements
050: UIResource {
051: private static Insets borderInsets = new Insets(2, 3, 3, 3);
052:
053: protected Color shadow;
054: protected Color darkShadow;
055: protected Color highlight;
056: protected Color lightHighlight;
057:
058: public ButtonBorder(final Color shadow, final Color darkShadow,
059: final Color highlight, final Color lightHighlight) {
060: this .shadow = shadow;
061: this .darkShadow = darkShadow;
062: this .highlight = highlight;
063: this .lightHighlight = lightHighlight;
064: }
065:
066: public Insets getBorderInsets(final Component c,
067: final Insets insets) {
068: Insets borderInsets = getBorderInsets(c);
069: insets.bottom = borderInsets.bottom;
070: insets.left = borderInsets.left;
071: insets.right = borderInsets.right;
072: insets.top = borderInsets.top;
073:
074: return insets;
075: }
076:
077: public Insets getBorderInsets(final Component c) {
078: return (Insets) borderInsets.clone();
079: }
080:
081: public void paintBorder(final Component c, final Graphics g,
082: final int x, final int y, final int w, final int h) {
083: if (c.isEnabled()) {
084: Utilities.draw3DRect(g, x, y, w, h, darkShadow,
085: highlight, false);
086: if (!((AbstractButton) c).getModel().isArmed()) {
087: Utilities.draw3DRect(g, x + 1, y + 1, w - 2, h - 2,
088: shadow, highlight, true);
089: }
090: } else {
091: Color oldColor = g.getColor();
092: g.setColor(shadow);
093: g.drawRect(x, y, w - 1, h - 1);
094: g.setColor(oldColor);
095: }
096: }
097: }
098:
099: public static class FieldBorder extends AbstractBorder implements
100: UIResource {
101: protected Color shadow;
102: protected Color darkShadow;
103: protected Color highlight;
104: protected Color lightHighlight;
105:
106: public FieldBorder(final Color shadow, final Color darkShadow,
107: final Color highlight, final Color lightHighlight) {
108: this .shadow = shadow;
109: this .darkShadow = darkShadow;
110: this .highlight = highlight;
111: this .lightHighlight = lightHighlight;
112: }
113:
114: public Insets getBorderInsets(final Component c,
115: final Insets insets) {
116: Insets borderInsets = getBorderInsets(c);
117: insets.bottom = borderInsets.bottom;
118: insets.left = borderInsets.left;
119: insets.right = borderInsets.right;
120: insets.top = borderInsets.top;
121: return insets;
122: }
123:
124: public Insets getBorderInsets(final Component c) {
125: Insets result;
126: if (c instanceof JTextComponent) {
127: Insets ins = ((JTextComponent) c).getMargin();
128: result = new Insets(2, 2, 2, 2);
129: result.left += ins.left;
130: result.right += ins.right;
131: result.top += ins.top;
132: result.bottom += ins.bottom;
133: } else {
134: result = new Insets(2, 2, 2, 2);
135: }
136: return result;
137: }
138:
139: public void paintBorder(final Component c, final Graphics g,
140: final int x, final int y, final int w, final int h) {
141: Color oldColor = g.getColor();
142: g.setColor(shadow);
143: g.drawLine(x, y, x + w - 1, y);
144: g.drawLine(x, y, x, y + h - 1);
145: g.setColor(darkShadow);
146: g.drawLine(x + 1, y + 1, x + w - 3, y + 1);
147: g.drawLine(x + 1, y + 1, x + 1, y + h - 2);
148: g.setColor(highlight);
149: g.drawLine(x + w - 2, y + h - 2, x + w - 2, y + 1);
150: g.drawLine(x + w - 2, y + h - 2, x + 1, y + h - 2);
151: g.setColor(lightHighlight);
152: g.drawLine(x + w - 1, y + h - 1, x + w - 1, y);
153: g.drawLine(x + w - 1, y + h - 1, x, y + h - 1);
154: g.setColor(oldColor);
155: }
156: }
157:
158: public static class MarginBorder extends AbstractBorder implements
159: UIResource {
160: public Insets getBorderInsets(final Component c,
161: final Insets insets) {
162: Insets borderInsets = getBorderInsets(c);
163: insets.bottom = borderInsets.bottom;
164: insets.left = borderInsets.left;
165: insets.right = borderInsets.right;
166: insets.top = borderInsets.top;
167:
168: return insets;
169: }
170:
171: public Insets getBorderInsets(final Component c) {
172: Insets result = (Insets) AccessController
173: .doPrivileged(new PrivilegedAction() {
174: public Object run() {
175: try {
176: Method m = c.getClass().getMethod(
177: "getMargin", new Class[0]);
178: return m.invoke(c, new Object[0]);
179: } catch (Exception e) {
180: return null;
181: }
182: }
183: });
184:
185: return result != null ? result : super .getBorderInsets(c);
186: }
187: }
188:
189: public static class MenuBarBorder extends AbstractBorder implements
190: UIResource {
191: private Color shadow;
192: private Color highlight;
193:
194: public MenuBarBorder(final Color shadow, final Color highlight) {
195: this .shadow = shadow;
196: this .highlight = highlight;
197: }
198:
199: public Insets getBorderInsets(final Component c,
200: final Insets insets) {
201: Insets borderInsets = getBorderInsets(c);
202: insets.bottom = borderInsets.bottom;
203: insets.left = borderInsets.left;
204: insets.right = borderInsets.right;
205: insets.top = borderInsets.top;
206:
207: return insets;
208: }
209:
210: public Insets getBorderInsets(final Component c) {
211: return new Insets(0, 0, 2, 0);
212: }
213:
214: public void paintBorder(final Component c, final Graphics g,
215: final int x, final int y, final int w, final int h) {
216: Color oldColor = g.getColor();
217: g.setColor(highlight);
218: g.drawLine(x, y + h - 2, x + w, y + h - 2);
219: g.setColor(shadow);
220: g.drawLine(x, y + h - 1, x + w, y + h - 1);
221: g.setColor(oldColor);
222: }
223: }
224:
225: public static class RadioButtonBorder extends ButtonBorder {
226: public RadioButtonBorder(final Color shadow,
227: final Color darkShadow, final Color highlight,
228: final Color lightHighlight) {
229: super (shadow, darkShadow, highlight, lightHighlight);
230: }
231:
232: public Insets getBorderInsets(final Component c,
233: final Insets insets) {
234: Insets borderInsets = getBorderInsets(c);
235: insets.bottom = borderInsets.bottom;
236: insets.left = borderInsets.left;
237: insets.right = borderInsets.right;
238: insets.top = borderInsets.top;
239:
240: return insets;
241: }
242:
243: public Insets getBorderInsets(final Component c) {
244: return new Insets(2, 2, 2, 2);
245: }
246:
247: public void paintBorder(final Component c, final Graphics g,
248: final int x, final int y, final int w, final int h) {
249: }
250: }
251:
252: public static class RolloverButtonBorder extends ButtonBorder {
253: public RolloverButtonBorder(final Color shadow,
254: final Color darkShadow, final Color highlight,
255: final Color lightHighlight) {
256: super (shadow, darkShadow, highlight, lightHighlight);
257: }
258:
259: public void paintBorder(final Component c, final Graphics g,
260: final int x, final int y, final int w, final int h) {
261: if (((AbstractButton) c).getModel().isRollover()) {
262: super .paintBorder(c, g, x, y, w, h);
263: }
264: }
265: }
266:
267: public static class SplitPaneBorder implements Border, UIResource {
268: protected Color highlight;
269: protected Color shadow;
270:
271: public SplitPaneBorder(final Color highlight, final Color shadow) {
272: this .shadow = shadow;
273: this .highlight = highlight;
274: }
275:
276: public Insets getBorderInsets(final Component c) {
277: return new Insets(1, 1, 1, 1);
278: }
279:
280: public void paintBorder(final Component c, final Graphics g,
281: final int x, final int y, final int w, final int h) {
282: Utilities
283: .draw3DRect(g, x, y, w, h, shadow, highlight, true);
284: }
285:
286: public boolean isBorderOpaque() {
287: return true;
288: }
289: }
290:
291: public static class ToggleButtonBorder extends ButtonBorder {
292: public ToggleButtonBorder(final Color shadow,
293: final Color darkShadow, final Color highlight,
294: final Color lightHighlight) {
295: super (shadow, darkShadow, highlight, lightHighlight);
296: }
297:
298: public Insets getBorderInsets(final Component c,
299: final Insets insets) {
300: Insets borderInsets = getBorderInsets(c);
301: insets.bottom = borderInsets.bottom;
302: insets.left = borderInsets.left;
303: insets.right = borderInsets.right;
304: insets.top = borderInsets.top;
305:
306: return insets;
307: }
308:
309: public Insets getBorderInsets(final Component c) {
310: return new Insets(2, 2, 2, 2);
311: }
312:
313: public void paintBorder(final Component c, final Graphics g,
314: final int x, final int y, final int w, final int h) {
315: super .paintBorder(c, g, x, y, w, h);
316: }
317: }
318:
319: static class ToolBarButtonMarginBorder extends MarginBorder {
320: public Insets getBorderInsets(final Component c) {
321: Insets result = super .getBorderInsets(c);
322: return Utilities.isUIResource(result) ? new Insets(3, 3, 3,
323: 3) : result;
324: }
325: }
326:
327: public static Border getToggleButtonBorder() {
328: final String prefix = "ToggleButton.";
329: ToggleButtonBorder toggleButtonBorder = new ToggleButtonBorder(
330: getShadow(prefix), getDarkShadow(prefix),
331: getHighlight(prefix), getLightHighlight(prefix));
332: MarginBorder marginBorder = new MarginBorder();
333:
334: return new BorderUIResource.CompoundBorderUIResource(
335: toggleButtonBorder, marginBorder);
336: }
337:
338: public static Border getTextFieldBorder() {
339: final String prefix = "TextField.";
340: return new FieldBorder(getShadow(prefix),
341: getDarkShadow(prefix), getHighlight(prefix),
342: getLightHighlight(prefix));
343: }
344:
345: public static Border getSplitPaneDividerBorder() {
346: return new SplitPaneDividerBorder();
347: }
348:
349: public static Border getSplitPaneBorder() {
350: final String prefix = "SplitPane.";
351: return new SplitPaneBorder(getHighlight(prefix),
352: getShadow(prefix));
353: }
354:
355: public static Border getRadioButtonBorder() {
356: final String prefix = "RadioButton.";
357: RadioButtonBorder radioButtonBorder = new RadioButtonBorder(
358: getShadow(prefix), getDarkShadow(prefix),
359: getHighlight(prefix), getLightHighlight(prefix));
360: MarginBorder marginBorder = new MarginBorder();
361:
362: return new BorderUIResource.CompoundBorderUIResource(
363: radioButtonBorder, marginBorder);
364: }
365:
366: public static Border getProgressBarBorder() {
367: return new BorderUIResource.LineBorderUIResource(Color.GREEN, 2);
368: }
369:
370: public static Border getMenuBarBorder() {
371: final String prefix = "MenuBar.";
372: return new MenuBarBorder(getShadow(prefix),
373: getHighlight(prefix));
374: }
375:
376: public static Border getInternalFrameBorder() {
377: final String prefix = "InternalFrame.";
378: final Color borderColor = UIManager.getColor(prefix
379: + "borderColor");
380: final Color shadow = UIManager
381: .getColor(prefix + "borderShadow");
382: final Color darkShadow = UIManager.getColor(prefix
383: + "borderDarkShadow");
384: final Color highlight = UIManager.getColor(prefix
385: + "borderLight");
386: final Color lightHighlight = UIManager.getColor(prefix
387: + "borderHighlight");
388: BevelBorder bevelBorder = new BevelBorder(0, highlight,
389: lightHighlight, darkShadow, shadow);
390: BorderUIResource.LineBorderUIResource lineBorder = new BorderUIResource.LineBorderUIResource(
391: borderColor, 1);
392:
393: return new BorderUIResource.CompoundBorderUIResource(
394: bevelBorder, lineBorder);
395: }
396:
397: public static Border getButtonBorder() {
398: final String prefix = "Button.";
399: ButtonBorder buttonBorder = new ButtonBorder(getShadow(prefix),
400: getDarkShadow(prefix), getHighlight(prefix),
401: getLightHighlight(prefix));
402: MarginBorder marginBorder = new MarginBorder();
403:
404: return new BorderUIResource.CompoundBorderUIResource(
405: buttonBorder, marginBorder);
406: }
407:
408: /*
409: * Border used for divider of JSplitPane
410: */
411: private static class SplitPaneDividerBorder implements Border,
412: UIResource {
413: private static final Color shadow = Color.GRAY;
414: private static final Color lightHighlight = Color.WHITE;
415:
416: public SplitPaneDividerBorder() {
417: }
418:
419: public Insets getBorderInsets(final Component c) {
420: return new Insets(1, 1, 1, 1);
421: }
422:
423: public void paintBorder(final Component c, final Graphics g,
424: final int x, final int y, final int w, final int h) {
425: Utilities.draw3DRect(g, x, y, w, h, shadow, lightHighlight,
426: true);
427: }
428:
429: public boolean isBorderOpaque() {
430: return true;
431: }
432: }
433:
434: private static Color getLightHighlight(final String prefix) {
435: return UIManager.getColor(prefix + "light");
436: }
437:
438: private static Color getHighlight(final String prefix) {
439: return UIManager.getColor(prefix + "highlight");
440: }
441:
442: private static Color getDarkShadow(final String prefix) {
443: return UIManager.getColor(prefix + "darkShadow");
444: }
445:
446: private static Color getShadow(final String prefix) {
447: return UIManager.getColor(prefix + "shadow");
448: }
449: }
|