001: /*
002: * Copyright 2005 Patrick Gotthardt
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.pagosoft.plaf;
017:
018: import com.jidesoft.swing.JideSwingUtilities;
019: import com.pagosoft.swing.ColorUtils;
020:
021: import javax.swing.*;
022: import javax.swing.plaf.*;
023: import javax.swing.text.*;
024: import javax.swing.text.html.*;
025: import java.awt.*;
026: import java.awt.image.*;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: public class PgsUtils {
031: public static boolean isFlat(JComponent b) {
032: return Boolean.TRUE.equals(b.getClientProperty("pgs.isFlat"));
033: }
034:
035: public static boolean isFlat(String id) {
036: return Boolean.TRUE.equals(UIManager.get(id + ".isFlat"));
037: }
038:
039: /**
040: * This code is taken from the "TabContainer" of NetBeans
041: */
042: /*
043: * Sun Public License Notice
044: *
045: * The contents of this file are subject to the Sun Public License
046: * Version 1.0 (the "License"). You may not use this file except in
047: * compliance with the License. A copy of the License is available at
048: * http://www.sun.com/
049: *
050: * The Original Code is NetBeans. The Initial Developer of the Original
051: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
052: * Microsystems, Inc. All Rights Reserved.
053: */
054: private static HashMap gpCache;
055:
056: public static GradientPaint getGradientPaint(int x, int y,
057: int width, int height, Color from, Color to) {
058: if (gpCache == null) {
059: gpCache = new HashMap(20);
060: }
061:
062: //Generate a hash code for looking up an existing paint
063: long bits = Double.doubleToLongBits(x)
064: + Double.doubleToLongBits(y) * 37
065: + Double.doubleToLongBits(width) * 43
066: + Double.doubleToLongBits(height) * 47;
067: int hash = ((((int) bits) ^ ((int) (bits >> 32)))
068: ^ from.hashCode() ^ (to.hashCode() * 17)) * 31;
069:
070: Object key = new Integer(hash);
071: GradientPaint result = (GradientPaint) gpCache.get(key);
072: if (result == null) {
073: result = new GradientPaint(0, 0, from, 0, height, to, true);
074: if (gpCache.size() > 40) {
075: gpCache.clear();
076: }
077: gpCache.put(key, result);
078: }
079: return result;
080: }
081:
082: /* End of code */
083:
084: public static void drawGradient(Graphics g, int width, int height,
085: Color from, Color to) {
086: drawGradient(g, 0, 0, width, height, from, to);
087: }
088:
089: public static void drawGradient(Graphics g, int x, int y,
090: int width, int height, Color from, Color to) {
091: Graphics2D gfx = (Graphics2D) g;
092: if (PlafOptions.isJideFastGradientEnabled()) {
093: JideSwingUtilities.fillGradient(gfx, new Rectangle(x, y,
094: width, height), from, to, true);
095: } else {
096: gfx
097: .setPaint(getGradientPaint(x, y, width, height,
098: from, to));
099: gfx.fill(new Rectangle(x, y, width, height));
100: }
101: }
102:
103: public static void drawGradient(Graphics g, JComponent c,
104: String prefix) {
105: drawGradient(g, c.getWidth(), c.getHeight(), (Color) c
106: .getClientProperty(prefix + ".gradientStart"),
107: (Color) c.getClientProperty(prefix + ".gradientEnd"));
108: }
109:
110: public static void drawGradient(Graphics g, JComponent c,
111: String prefix1, String prefix2) {
112: drawGradient(g, c.getWidth(), c.getHeight(), (Color) c
113: .getClientProperty(prefix1 + ".gradientStart"),
114: (Color) c.getClientProperty(prefix2 + ".gradientEnd"));
115: }
116:
117: public static void drawGradient(Graphics g, JComponent c) {
118: drawGradient(g, c.getWidth(), c.getHeight(), (Color) c
119: .getClientProperty("gradientStart"), (Color) c
120: .getClientProperty("gradientEnd"));
121: }
122:
123: public static void drawVistaBackground(Graphics g, Component b,
124: String prefix) {
125: drawVistaBackground(g, 0, 0, b.getWidth(), b.getHeight(),
126: UIManager.getColor(prefix + ".gradientStart"),
127: UIManager.getColor(prefix + ".gradientMiddle"),
128: UIManager.getColor(prefix + ".gradientEnd"));
129: }
130:
131: public static void drawVistaBackground(Graphics g, Component b,
132: Color start, Color mid, Color end) {
133: drawVistaBackground(g, 0, 0, b.getWidth(), b.getHeight(),
134: start, mid, end);
135: }
136:
137: public static void drawVistaBackground(Graphics g, int x, int y,
138: int width, int height, Color start, Color mid, Color end) {
139: g.setColor(start);
140: g.fillRect(x, y, width, height / 2);
141: g.setColor(end);
142: g.fillRect(x, y + height / 2, width, height / 2 + 1);
143: g.setColor(mid);
144: g.drawLine(x, y + height / 2, width, y + height / 2);
145: }
146:
147: protected static void paintMenuItemBackground(Graphics g,
148: AbstractButton menuItem, Color bgColor, String prefix) {
149: ButtonModel model = menuItem.getModel();
150: Color oldColor = g.getColor();
151: Dimension size = menuItem.getSize();
152: Insets ins = UIManager.getInsets(prefix
153: + ".selectedBorderMargin");
154: Rectangle rect = new Rectangle(ins.left, ins.top, size.width
155: - ins.right - ins.left, size.height - ins.top
156: - ins.bottom - 2);
157:
158: if (menuItem.isOpaque()) {
159: g.setColor(menuItem.getBackground());
160: g.fillRect(0, 0, size.width, size.height);
161: if (model.isArmed()
162: || (menuItem instanceof JMenu && model.isSelected())) {
163: g.setColor(bgColor);
164: if (PgsUtils.isFlat("MenuItem")) {
165: g.fillRect((int) rect.getX(), (int) rect.getY(),
166: (int) rect.getWidth(), (int) rect
167: .getHeight());
168: } else {
169: PgsUtils.drawGradient(g, (int) rect.getX(),
170: (int) rect.getY(), (int) rect.getWidth(),
171: (int) rect.getHeight(),
172: UIManager.getColor(prefix
173: + ".gradientStart"), UIManager
174: .getColor(prefix + ".gradientEnd"));
175: }
176: g.setColor(UIManager.getColor(prefix
177: + ".selectedBorderColor"));
178: g.drawRect((int) rect.getX(), (int) rect.getY(),
179: (int) rect.getWidth(), (int) rect.getHeight());
180: }
181: g.setColor(oldColor);
182: }
183: }
184:
185: public static boolean isLeftToRight(Component c) {
186: return c.getComponentOrientation().isLeftToRight();
187: }
188:
189: // Toolbar icons specific
190: public static Icon getToolBarIcon(Image i) {
191: if (!PlafOptions.isToolBarIconUsed()) {
192: return null;
193: }
194: return new IconUIResource(new ShadowedIcon(new ImageIcon(i)));
195: /*ImageProducer prod = new FilteredImageSource(i.getSource(),
196: new ToolBarImageFilter());
197: return new IconUIResource(new ImageIcon(
198: Toolkit.getDefaultToolkit().createImage(prod)));*/
199: }
200:
201: public static Icon getDisabledButtonIcon(Image image) {
202: if (!PlafOptions.isDisabledIconUsed()) {
203: return null;
204: }
205: return new IconUIResource(new ImageIcon(GrayFilter
206: .createDisabledImage(image)));
207: }
208:
209: // http://jroller.com/page/santhosh/20050521#beautify_swing_applications_toolbar_with
210: public static class ShadowedIcon implements Icon {
211: private int shadowWidth = 2;
212: private int shadowHeight = 2;
213: private Icon icon, shadow;
214:
215: public ShadowedIcon(Icon icon) {
216: this .icon = icon;
217: shadow = new ImageIcon(GrayFilter
218: .createDisabledImage(((ImageIcon) icon).getImage()));
219: }
220:
221: public ShadowedIcon(Icon icon, int shadowWidth, int shadowHeight) {
222: this (icon);
223: this .shadowWidth = shadowWidth;
224: this .shadowHeight = shadowHeight;
225: }
226:
227: public int getIconHeight() {
228: return icon.getIconWidth() + shadowWidth;
229: }
230:
231: public int getIconWidth() {
232: return icon.getIconHeight() + shadowHeight;
233: }
234:
235: public void paintIcon(Component c, Graphics g, int x, int y) {
236: shadow.paintIcon(c, g, x + shadowWidth, y + shadowHeight);
237: icon.paintIcon(c, g, x, y);
238: }
239: }
240:
241: private static class ToolBarImageFilter extends RGBImageFilter {
242: public ToolBarImageFilter() {
243: canFilterIndexColorModel = true;
244: }
245:
246: public int filterRGB(int x, int y, int rgb) {
247: int r = ((rgb >> 16) & 0xff);
248: int g = ((rgb >> 8) & 0xff);
249: int b = (rgb & 0xff);
250: int gray = Math.max(Math.max(r, g), b);
251: return (rgb & 0xff000000) | (gray << 16) | (gray << 8)
252: | (gray << 0);
253: }
254: }
255:
256: public static void drawVerticalBumps(Graphics g, int x, int y,
257: int height) {
258: int loops = height / 6;
259: for (int i = 0; i < loops; i++) {
260: g.setColor(PgsLookAndFeel.getControlShadow());
261: g.fillRect(x, y + (i * 6), 2, 2);
262: g.fillRect(x + 3, 3 + y + (i * 6), 2, 2);
263:
264: g.setColor(ColorUtils.getTranslucentColor(PgsLookAndFeel
265: .getControl(), 180));
266: g.fillRect(x + 1, 1 + y + (i * 6), 2, 2);
267: g.fillRect(x + 4, 4 + y + (i * 6), 2, 2);
268: }
269: }
270:
271: public static void drawHorizontalBumps(Graphics g, int x, int y,
272: int width) {
273: int loops = width / 6;
274: for (int i = 0; i < loops; i++) {
275: g.setColor(PgsLookAndFeel.getControlShadow());
276: g.fillRect(x + (i * 6), y, 2, 2);
277: g.fillRect(3 + x + (i * 6), y + 3, 2, 2);
278:
279: g.setColor(ColorUtils.getTranslucentColor(PgsLookAndFeel
280: .getControl(), 180));
281: g.fillRect(1 + x + (i * 6), y + 1, 2, 2);
282: g.fillRect(4 + x + (i * 6), y + 4, 2, 2);
283: }
284: }
285:
286: public static boolean hasFocus(Component component) {
287: if (component instanceof Container) {
288: Container c = (Container) component;
289: for (int i = 0; i < c.getComponentCount(); i++) {
290: if (hasFocus(c.getComponent(i))) {
291: return true;
292: }
293: }
294: }
295: return component != null && component.isFocusOwner();
296: }
297:
298: //--------------------------------------------------------------------------
299: //public static Stroke borderStroke = new BasicStroke(1.4f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
300: public static Stroke borderStroke = new BasicStroke(1.3f,
301: BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
302: public static Stroke rolloverBorderStroke = new BasicStroke(2.4f,
303: BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
304:
305: public static void regenerateBorderStroke() {
306: if (PlafOptions.isClearBorderEnabled()) {
307: rolloverBorderStroke = new BasicStroke(2.4f,
308: BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
309: } else {
310: rolloverBorderStroke = new BasicStroke(2.4f,
311: BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
312: }
313: }
314:
315: public static void drawButtonBorder(Graphics g, int x, int y,
316: int w, int h, Stroke st, Color c) {
317: Graphics2D gfx = (Graphics2D) g;
318: Stroke s = gfx.getStroke();
319: gfx.setStroke(st);
320: g.setColor(c);
321: if (PlafOptions.isClearBorderEnabled()) {
322: g.drawRect(x + 1, y + 1, w - 1, h - 1);
323: // Stupid hack, but it works...
324: g.drawLine(x, y, x, y + h);
325: } else {
326: drawRoundRect(g, x, y, w, h);
327: }
328: gfx.setStroke(s);
329: }
330:
331: public static void drawButtonBorder(Graphics g, int x, int y,
332: int w, int h, Color c) {
333: if (PlafOptions.isClearBorderEnabled()) {
334: g.setColor(c);
335: g.drawRect(x, y, w, h);
336: } else {
337: Graphics2D gfx = (Graphics2D) g;
338: Stroke s = gfx.getStroke();
339: gfx.setStroke(borderStroke);
340: g.setColor(c);
341: drawRoundRect(g, x, y, w, h);
342: gfx.setStroke(s);
343: }
344: }
345:
346: public static void drawButtonBorder(Graphics g, int x, int y,
347: int w, int h, int c1, int c2, Color c) {
348: if (PlafOptions.isClearBorderEnabled()) {
349: g.setColor(c);
350: g.drawRect(x, y, w, h);
351: } else {
352: Graphics2D gfx = (Graphics2D) g;
353: Stroke s = gfx.getStroke();
354: gfx.setStroke(borderStroke);
355: g.setColor(c);
356: drawRoundRect(g, x, y, w, h, c1, c2);
357: gfx.setStroke(s);
358: }
359: }
360:
361: public static void drawDefaultButtonBorder(Graphics g, int x,
362: int y, int w, int h, boolean isRollover) {
363: clearButtonBorder(g, x, y, w, h);
364: drawButtonBorder(g, x + 1, y + 1, w - 3, h - 3, borderStroke,
365: PgsLookAndFeel.getPrimaryControlShadow());
366: if (isRollover) {
367: drawRolloverButtonBorder(g, x + 1, y + 1, w - 3, h - 3);
368: }
369: drawButtonBorder(g, x, y, w - 1, h - 1, PgsLookAndFeel
370: .getPrimaryControlDarkShadow());
371: }
372:
373: public static void drawButtonBorder(Graphics g, int x, int y,
374: int w, int h) {
375: clearButtonBorder(g, x, y, w, h);
376: drawButtonBorder(g, x, y, w - 1, h - 1, PgsLookAndFeel
377: .getControlDarkShadow());
378: }
379:
380: public static void drawRolloverButtonBorder(Graphics g, int x,
381: int y, int w, int h) {
382: if (PlafOptions.isVistaStyle()) {
383: drawButtonBorder(g, x, y, w - 1, h - 1, PgsLookAndFeel
384: .getPrimaryControlDarkShadow());
385: } else {
386: drawButtonBorder(g, x + 1, y + 1, w - 3, h - 3,
387: rolloverBorderStroke, PgsLookAndFeel.getGlow());
388: PgsUtils.drawButtonBorder(g, x, y, w, h);
389: }
390: }
391:
392: private static void clearButtonBorder(Graphics g, int x, int y,
393: int w, int h) {
394: if (PlafOptions.isClearBorderEnabled()) {
395: // We don't need to clear anything if we use ClearBorder
396: return;
397: }
398: g.setColor(UIManager.getColor("Panel.background"));
399: g.drawRect(x, y, w - 1, h - 1);
400: }
401:
402: public static void drawDisabledBorder(Graphics g, int x, int y,
403: int w, int h) {
404: clearButtonBorder(g, x, y, w, h);
405: g.setColor(PgsLookAndFeel.getControlShadow());
406: if (PlafOptions.isClearBorderEnabled()) {
407: g.drawRect(x, y, w, h);
408: } else {
409: drawRoundRect(g, x, y, w, h);
410: }
411: }
412:
413: public static void drawRoundRect(Graphics g, int x, int y, int w,
414: int h) {
415: drawRoundRect(g, x, y, w, h, 4, 4);
416: }
417:
418: public static void drawRoundRect(Graphics g, int x, int y, int w,
419: int h, int c1, int c2) {
420: Graphics2D gfx = (Graphics2D) g;
421: gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
422: RenderingHints.VALUE_ANTIALIAS_ON);
423: gfx.drawRoundRect(x, y, w, h, c1, c2);
424: gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
425: RenderingHints.VALUE_ANTIALIAS_DEFAULT);
426: }
427:
428: // drawing text
429: private static Map hintsMap, oldHintsMap;
430:
431: public static void installAntialiasing(Graphics g) {
432: if (!PlafOptions.isAntialiasingEnabled()) {
433: return;
434: }
435: Graphics2D gfx = (Graphics2D) g;
436: if (hintsMap == null) {
437: hintsMap = new HashMap();
438: hintsMap.put(RenderingHints.KEY_FRACTIONALMETRICS,
439: RenderingHints.VALUE_FRACTIONALMETRICS_ON);
440: hintsMap.put(RenderingHints.KEY_TEXT_ANTIALIASING,
441: RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
442: hintsMap.put(RenderingHints.KEY_ANTIALIASING,
443: RenderingHints.VALUE_ANTIALIAS_ON);
444:
445: oldHintsMap = new HashMap();
446: oldHintsMap
447: .put(
448: RenderingHints.KEY_FRACTIONALMETRICS,
449: gfx
450: .getRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS));
451: oldHintsMap
452: .put(
453: RenderingHints.KEY_TEXT_ANTIALIASING,
454: gfx
455: .getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING));
456: oldHintsMap.put(RenderingHints.KEY_ANTIALIASING, gfx
457: .getRenderingHint(RenderingHints.KEY_ANTIALIASING));
458: }
459: gfx.addRenderingHints(hintsMap);
460: }
461:
462: public static void uninstallAntialiasing(Graphics g) {
463: if (!PlafOptions.isAntialiasingEnabled()) {
464: return;
465: }
466: ((Graphics2D) g).addRenderingHints(oldHintsMap);
467: }
468:
469: /*
470: * $ $ License.
471: *
472: * Copyright $ L2FProd.com
473: *
474: * Licensed under the Apache License, Version 2.0 (the "License");
475: * you may not use this file except in compliance with the License.
476: * You may obtain a copy of the License at
477: *
478: * http://www.apache.org/licenses/LICENSE-2.0
479: *
480: * Unless required by applicable law or agreed to in writing, software
481: * distributed under the License is distributed on an "AS IS" BASIS,
482: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
483: * See the License for the specific language governing permissions and
484: * limitations under the License.
485: */
486:
487: /**
488: * This method fixes the display of HTML enabled components (like JEditorPanes
489: * and JTextPanes).
490: */
491: public static void fixHtmlDisplay(JComponent component) {
492: Font defaultFont = UIManager.getFont("Button.font");
493:
494: String stylesheet = "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
495: + defaultFont.getName()
496: + "; font-size: "
497: + defaultFont.getSize()
498: + "pt; }"
499: + "a, p, li { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
500: + defaultFont.getName()
501: + "; font-size: "
502: + defaultFont.getSize() + "pt; }";
503:
504: try {
505: HTMLDocument doc = null;
506: if (component instanceof JEditorPane) {
507: if (((JEditorPane) component).getDocument() instanceof HTMLDocument) {
508: doc = (HTMLDocument) ((JEditorPane) component)
509: .getDocument();
510: }
511: } else {
512: View v = (View) component
513: .getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
514: if (v != null
515: && v.getDocument() instanceof HTMLDocument) {
516: doc = (HTMLDocument) v.getDocument();
517: }
518: }
519: if (doc != null) {
520: doc.getStyleSheet().loadRules(
521: new java.io.StringReader(stylesheet), null);
522: } // end of if (doc != null)
523: } catch (Exception e) {
524: e.printStackTrace();
525: }
526: }
527: }
|