001: /*
002: * SmoothGradientIconFactory.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.plaf.smoothgradient;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.GradientPaint;
027: import java.awt.Graphics;
028: import java.awt.Graphics2D;
029: import java.awt.Paint;
030:
031: import java.io.Serializable;
032:
033: import javax.swing.ButtonModel;
034: import javax.swing.Icon;
035: import javax.swing.JButton;
036: import javax.swing.JCheckBox;
037: import javax.swing.JComponent;
038: import javax.swing.JMenuItem;
039: import javax.swing.plaf.UIResource;
040: import javax.swing.plaf.metal.MetalLookAndFeel;
041:
042: /* ----------------------------------------------------------
043: * CVS NOTE: Changes to the CVS repository prior to the
044: * release of version 3.0.0beta1 has meant a
045: * resetting of CVS revision numbers.
046: * ----------------------------------------------------------
047: */
048:
049: /**
050: *
051: * @author Takis Diakoumis
052: * @version $Revision: 1.4 $
053: * @date $Date: 2006/05/14 06:56:07 $
054: */
055: final class SmoothGradientIconFactory {
056:
057: public static Icon getInternalFrameMinimizeIcon(int size) {
058: return new InternalFrameMinimizeIcon(size);
059: }
060:
061: private static class InternalFrameMinimizeIcon implements Icon,
062: UIResource, Serializable {
063: int iconSize = 16;
064:
065: public InternalFrameMinimizeIcon(int size) {
066: iconSize = size;
067: }
068:
069: public void paintIcon(Component c, Graphics g, int x, int y) {
070: JButton parentButton = (JButton) c;
071: ButtonModel buttonModel = parentButton.getModel();
072:
073: Color mainItemColor = SmoothGradientLookAndFeel
074: .getPrimaryControlDarkShadow();
075: Color darkHighlightColor = MetalLookAndFeel.getBlack();
076:
077: // background gradients
078: Color bgStart = SmoothGradientLookAndFeel.FRAME_BUTTON_START_ACTIVE;
079: Color bgStop = SmoothGradientLookAndFeel.FRAME_BUTTON_STOP_ACTIVE;
080:
081: // if the internal frame is inactive
082: if (parentButton.getClientProperty("paintActive") != Boolean.TRUE) {
083: mainItemColor = SmoothGradientLookAndFeel
084: .getControlDarkShadow();
085: bgStart = SmoothGradientLookAndFeel.FRAME_BUTTON_START_INACTIVE;
086: bgStop = SmoothGradientLookAndFeel.FRAME_BUTTON_STOP_INACTIVE;
087:
088: // if inactive and pressed
089: if (buttonModel.isPressed() && buttonModel.isArmed())
090: mainItemColor = darkHighlightColor;
091:
092: }
093:
094: // if the button is pressed and the mouse is over it
095: else if (buttonModel.isPressed() && buttonModel.isArmed()) {
096: mainItemColor = darkHighlightColor;
097: }
098:
099: Graphics2D g2d = (Graphics2D) g;
100: g2d.translate(x, y);
101:
102: g2d.setPaint(new GradientPaint(0, iconSize, bgStart, 0, 0,
103: bgStop));
104:
105: g2d.fillRect(0, 0, iconSize, iconSize);
106:
107: g2d.setColor(mainItemColor);
108: g2d.drawLine(4, 9, 12, 9);
109: g2d.drawLine(3, 10, 13, 10);
110: g2d.drawLine(3, 11, 13, 11);
111: g2d.drawLine(4, 12, 12, 12);
112:
113: g.translate(-x, -y);
114: }
115:
116: public int getIconWidth() {
117: return iconSize;
118: }
119:
120: public int getIconHeight() {
121: return iconSize;
122: }
123:
124: } // class InternalFrameMinimizeIcon
125:
126: public static Icon getInternalFrameMaximizeIcon(int size) {
127: return new InternalFrameMaximizeIcon(size);
128: }
129:
130: private static class InternalFrameMaximizeIcon implements Icon,
131: UIResource, Serializable {
132: int iconSize = 16;
133:
134: public InternalFrameMaximizeIcon(int size) {
135: iconSize = size;
136: }
137:
138: public void paintIcon(Component c, Graphics g, int x, int y) {
139: JButton parentButton = (JButton) c;
140: ButtonModel buttonModel = parentButton.getModel();
141:
142: Color mainItemColor = SmoothGradientLookAndFeel
143: .getPrimaryControlDarkShadow();
144: Color darkHighlightColor = MetalLookAndFeel.getBlack();
145:
146: // background gradients
147: Color bgStart = SmoothGradientLookAndFeel.FRAME_BUTTON_START_ACTIVE;
148: Color bgStop = SmoothGradientLookAndFeel.FRAME_BUTTON_STOP_ACTIVE;
149:
150: // if the internal frame is inactive
151: if (parentButton.getClientProperty("paintActive") != Boolean.TRUE) {
152: mainItemColor = SmoothGradientLookAndFeel
153: .getControlDarkShadow();
154: bgStart = SmoothGradientLookAndFeel.FRAME_BUTTON_START_INACTIVE;
155: bgStop = SmoothGradientLookAndFeel.FRAME_BUTTON_STOP_INACTIVE;
156:
157: // if inactive and pressed
158: if (buttonModel.isPressed() && buttonModel.isArmed())
159: mainItemColor = darkHighlightColor;
160:
161: }
162:
163: // if the button is pressed and the mouse is over it
164: else if (buttonModel.isPressed() && buttonModel.isArmed()) {
165: mainItemColor = darkHighlightColor;
166: }
167:
168: Graphics2D g2d = (Graphics2D) g;
169: g2d.translate(x, y);
170:
171: Paint _paint = g2d.getPaint();
172: // fill the background
173: g2d.setPaint(new GradientPaint(0, iconSize, bgStart, 0, 0,
174: bgStop));
175: g2d.fillRect(0, 0, iconSize, iconSize);
176:
177: g2d.setPaint(_paint);
178: // paint the image
179: g2d.setColor(mainItemColor);
180: g2d.drawLine(4, 3, 11, 3);
181: g2d.fillRect(3, 4, 10, 3);
182: g2d.drawLine(3, 7, 3, 11);
183: g2d.drawLine(12, 7, 12, 11);
184: g2d.drawLine(4, 12, 11, 12);
185:
186: g.translate(-x, -y);
187: }
188:
189: public int getIconWidth() {
190: return iconSize;
191: }
192:
193: public int getIconHeight() {
194: return iconSize;
195: }
196:
197: } // class InternalFrameMaximizeIcon
198:
199: public static Icon getInternalFrameAltMaximizeIcon(int size) {
200: return new InternalFrameAltMaximizeIcon(size);
201: }
202:
203: private static class InternalFrameAltMaximizeIcon implements Icon,
204: UIResource, Serializable {
205: int iconSize = 16;
206:
207: public InternalFrameAltMaximizeIcon(int size) {
208: iconSize = size;
209: }
210:
211: public void paintIcon(Component c, Graphics g, int x, int y) {
212: JButton parentButton = (JButton) c;
213: ButtonModel buttonModel = parentButton.getModel();
214:
215: Color mainItemColor = SmoothGradientLookAndFeel
216: .getPrimaryControlDarkShadow();
217: Color darkHighlightColor = MetalLookAndFeel.getBlack();
218:
219: // background gradients
220: Color bgStart = SmoothGradientLookAndFeel.FRAME_BUTTON_START_ACTIVE;
221: Color bgStop = SmoothGradientLookAndFeel.FRAME_BUTTON_STOP_ACTIVE;
222:
223: // if the internal frame is inactive
224: if (parentButton.getClientProperty("paintActive") != Boolean.TRUE) {
225: mainItemColor = SmoothGradientLookAndFeel
226: .getControlDarkShadow();
227: bgStart = SmoothGradientLookAndFeel.FRAME_BUTTON_START_INACTIVE;
228: bgStop = SmoothGradientLookAndFeel.FRAME_BUTTON_STOP_INACTIVE;
229:
230: // if inactive and pressed
231: if (buttonModel.isPressed() && buttonModel.isArmed()) {
232: mainItemColor = darkHighlightColor;
233: }
234:
235: }
236:
237: // if the button is pressed and the mouse is over it
238: else if (buttonModel.isPressed() && buttonModel.isArmed()) {
239: mainItemColor = darkHighlightColor;
240: }
241:
242: Graphics2D g2d = (Graphics2D) g;
243: g2d.translate(x, y);
244:
245: Paint _paint = g2d.getPaint();
246: // fill the background
247: g2d.setPaint(new GradientPaint(0, iconSize, bgStart, 0, 0,
248: bgStop));
249: g2d.fillRect(0, 0, iconSize, iconSize);
250:
251: g2d.setPaint(_paint);
252: // paint the image
253: g2d.setColor(mainItemColor);
254: g2d.drawLine(6, 3, 11, 3);
255: g2d.drawLine(5, 4, 12, 4);
256: g2d.drawLine(12, 5, 12, 10);
257: g2d.fillRect(3, 6, 8, 3);
258: g2d.drawLine(3, 9, 3, 12);
259: g2d.drawLine(4, 12, 10, 12);
260: g2d.drawLine(10, 9, 10, 12);
261:
262: g.translate(-x, -y);
263: }
264:
265: public int getIconWidth() {
266: return iconSize;
267: }
268:
269: public int getIconHeight() {
270: return iconSize;
271: }
272:
273: } // class InternalFrameAltMaximizeIcon
274:
275: public static Icon getInternalFrameCloseIcon(int size) {
276: return new InternalFrameCloseIcon(size);
277: }
278:
279: private static class InternalFrameCloseIcon implements Icon,
280: UIResource, Serializable {
281: int iconSize = 16;
282:
283: public InternalFrameCloseIcon(int size) {
284: iconSize = size;
285: }
286:
287: public void paintIcon(Component c, Graphics g, int x, int y) {
288: JButton parentButton = (JButton) c;
289: ButtonModel buttonModel = parentButton.getModel();
290:
291: Color mainItemColor = SmoothGradientLookAndFeel
292: .getPrimaryControlDarkShadow();
293: Color darkHighlightColor = MetalLookAndFeel.getBlack();
294:
295: // background gradients
296: Color bgStart = SmoothGradientLookAndFeel.FRAME_BUTTON_START_ACTIVE;
297: Color bgStop = SmoothGradientLookAndFeel.FRAME_BUTTON_STOP_ACTIVE;
298:
299: // if the internal frame is inactive
300: if (parentButton.getClientProperty("paintActive") != Boolean.TRUE) {
301: mainItemColor = SmoothGradientLookAndFeel
302: .getControlDarkShadow();
303: bgStart = SmoothGradientLookAndFeel.FRAME_BUTTON_START_INACTIVE;
304: bgStop = SmoothGradientLookAndFeel.FRAME_BUTTON_STOP_INACTIVE;
305:
306: // if inactive and pressed
307: if (buttonModel.isPressed() && buttonModel.isArmed())
308: mainItemColor = darkHighlightColor;
309:
310: }
311:
312: // if the button is pressed and the mouse is over it
313: else if (buttonModel.isPressed() && buttonModel.isArmed()) {
314: mainItemColor = darkHighlightColor;
315: }
316:
317: Graphics2D g2d = (Graphics2D) g;
318: g2d.translate(x, y);
319:
320: Paint _paint = g2d.getPaint();
321: // fill the background
322: g2d.setPaint(new GradientPaint(0, iconSize, bgStart, 0, 0,
323: bgStop));
324: g2d.fillRect(0, 0, iconSize, iconSize);
325:
326: g2d.setPaint(_paint);
327: // paint the image
328: g2d.setColor(mainItemColor);
329: g2d.fillRect(5, 5, 6, 6);
330: g2d.fillRect(3, 4, 4, 2);
331: g2d.fillRect(4, 3, 2, 4);
332: g2d.fillRect(10, 3, 2, 4);
333: g2d.fillRect(9, 4, 4, 2);
334: g2d.fillRect(4, 9, 2, 4);
335: g2d.fillRect(3, 10, 4, 2);
336: g2d.fillRect(10, 9, 2, 4);
337: g2d.fillRect(9, 10, 4, 2);
338: g.translate(-x, -y);
339: }
340:
341: public int getIconWidth() {
342: return iconSize;
343: }
344:
345: public int getIconHeight() {
346: return iconSize;
347: }
348:
349: } // class InternalFrameCloseIcon
350:
351: // Helper method utilized by the CheckBoxIcon and the CheckBoxMenuItemIcon.
352: private static void drawCheck(Graphics g, int x, int y) {
353: g.translate(x, y);
354: g.drawLine(3, 5, 3, 5);
355: g.fillRect(3, 6, 2, 2);
356: g.drawLine(4, 8, 9, 3);
357: g.drawLine(5, 8, 9, 4);
358: g.drawLine(5, 9, 9, 5);
359: g.translate(-x, -y);
360: }
361:
362: private static class CheckBoxIcon implements Icon, UIResource,
363: Serializable {
364:
365: private static final int SIZE = 13;
366:
367: public int getIconWidth() {
368: return SIZE;
369: }
370:
371: public int getIconHeight() {
372: return SIZE;
373: }
374:
375: public void paintIcon(Component c, Graphics g, int x, int y) {
376: JCheckBox cb = (JCheckBox) c;
377: ButtonModel model = cb.getModel();
378:
379: if (model.isEnabled()) {
380: if (cb.isBorderPaintedFlat()) {
381: g.setColor(SmoothGradientLookAndFeel
382: .getControlDarkShadow());
383: g.drawRect(x, y, SIZE - 2, SIZE - 2);
384: // inside box
385: g.setColor(SmoothGradientLookAndFeel
386: .getControlHighlight());
387: g.fillRect(x + 1, y + 1, SIZE - 3, SIZE - 3);
388: } else if (model.isPressed() && model.isArmed()) {
389: g.setColor(MetalLookAndFeel.getControlShadow());
390: g.fillRect(x, y, SIZE - 1, SIZE - 1);
391: SmoothGradientUtils.drawPressed3DBorder(g, x, y,
392: SIZE, SIZE);
393: } else {
394: SmoothGradientUtils.drawFlush3DBorder(g, x, y,
395: SIZE, SIZE);
396: }
397: g.setColor(MetalLookAndFeel.getControlInfo());
398: } else {
399: g.setColor(MetalLookAndFeel.getControlShadow());
400: g.drawRect(x, y, SIZE - 2, SIZE - 2);
401: }
402:
403: if (model.isSelected()) {
404: drawCheck(g, x, y);
405: }
406: }
407:
408: }
409:
410: private static class CheckBoxMenuItemIcon implements Icon,
411: UIResource, Serializable {
412:
413: private static final int SIZE = 13;
414:
415: public int getIconWidth() {
416: return SIZE;
417: }
418:
419: public int getIconHeight() {
420: return SIZE;
421: }
422:
423: public void paintIcon(Component c, Graphics g, int x, int y) {
424: JMenuItem b = (JMenuItem) c;
425: if (b.isSelected()) {
426: drawCheck(g, x, y + 1);
427: }
428: }
429: }
430:
431: private static class RadioButtonMenuItemIcon implements Icon,
432: UIResource, Serializable {
433:
434: private static final int SIZE = 13;
435:
436: public int getIconWidth() {
437: return SIZE;
438: }
439:
440: public int getIconHeight() {
441: return SIZE;
442: }
443:
444: public void paintIcon(Component c, Graphics g, int x, int y) {
445: JMenuItem b = (JMenuItem) c;
446: if (b.isSelected()) {
447: drawDot(g, x, y);
448: }
449: }
450:
451: private void drawDot(Graphics g, int x, int y) {
452: g.translate(x, y);
453: g.drawLine(5, 4, 8, 4);
454: g.fillRect(4, 5, 6, 4);
455: g.drawLine(5, 9, 8, 9);
456: g.translate(-x, -y);
457: }
458: }
459:
460: private static class MenuArrowIcon implements Icon, UIResource,
461: Serializable {
462:
463: private static final int WIDTH = 4;
464: private static final int HEIGHT = 8;
465:
466: public void paintIcon(Component c, Graphics g, int x, int y) {
467: JMenuItem b = (JMenuItem) c;
468:
469: g.translate(x, y);
470: if (SmoothGradientUtils.isLeftToRight(b)) {
471: g.drawLine(0, 0, 0, 7);
472: g.drawLine(1, 1, 1, 6);
473: g.drawLine(2, 2, 2, 5);
474: g.drawLine(3, 3, 3, 4);
475: } else {
476: g.drawLine(4, 0, 4, 7);
477: g.drawLine(3, 1, 3, 6);
478: g.drawLine(2, 2, 2, 5);
479: g.drawLine(1, 3, 1, 4);
480: }
481: g.translate(-x, -y);
482: }
483:
484: public int getIconWidth() {
485: return WIDTH;
486: }
487:
488: public int getIconHeight() {
489: return HEIGHT;
490: }
491:
492: }
493:
494: /**
495: * The minus sign button icon used in trees
496: */
497: private static class ExpandedTreeIcon implements Icon, Serializable {
498:
499: protected static final int SIZE = 9;
500: protected static final int HALF_SIZE = 4;
501:
502: public void paintIcon(Component c, Graphics g, int x, int y) {
503: Color backgroundColor = c.getBackground();
504:
505: g.setColor(backgroundColor != null ? backgroundColor
506: : Color.white);
507: g.fillRect(x, y, SIZE - 1, SIZE - 1);
508: g.setColor(Color.gray);
509: g.drawRect(x, y, SIZE - 1, SIZE - 1);
510: g.setColor(Color.black);
511: g.drawLine(x + 2, y + HALF_SIZE, x + (SIZE - 3), y
512: + HALF_SIZE);
513: }
514:
515: public int getIconWidth() {
516: return SIZE;
517: }
518:
519: public int getIconHeight() {
520: return SIZE;
521: }
522: }
523:
524: /**
525: * The plus sign button icon used in trees.
526: */
527: private static class CollapsedTreeIcon extends ExpandedTreeIcon {
528: public void paintIcon(Component c, Graphics g, int x, int y) {
529: super .paintIcon(c, g, x, y);
530: g.drawLine(x + HALF_SIZE, y + 2, x + HALF_SIZE, y
531: + (SIZE - 3));
532: }
533: }
534:
535: /**
536: * The arrow button used in comboboxes.
537: */
538: private static class ComboBoxButtonIcon implements Icon,
539: Serializable {
540:
541: public void paintIcon(Component c, Graphics g, int x, int y) {
542: JComponent component = (JComponent) c;
543: int iconWidth = getIconWidth();
544:
545: g.translate(x, y);
546:
547: g.setColor(component.isEnabled() ? MetalLookAndFeel
548: .getControlInfo() : MetalLookAndFeel
549: .getControlShadow());
550: g.drawLine(0, 0, iconWidth - 1, 0);
551: g.drawLine(1, 1, 1 + (iconWidth - 3), 1);
552: g.drawLine(2, 2, 2 + (iconWidth - 5), 2);
553: g.drawLine(3, 3, 3 + (iconWidth - 7), 3);
554:
555: /*
556: int startY = (((h + 1) - arrowHeight) / 2) + arrowHeight - 1;
557: int startX = (w / 2);
558:
559: // System.out.println( "startX2 :" + startX + " startY2 :"+startY);
560:
561: for (int line = 0; line < arrowHeight; line++) {
562: g.drawLine(
563: startX - line,
564: startY - line,
565: startX + line + 1,
566: startY - line);
567: }*/
568: g.translate(-x, -y);
569: }
570:
571: public int getIconWidth() {
572: return 8;
573: }
574:
575: public int getIconHeight() {
576: return 4;
577: }
578: }
579:
580: // Cached Access to Icons ***********************************************************
581:
582: private static Icon checkBoxIcon;
583: private static Icon checkBoxMenuItemIcon;
584: private static Icon radioButtonMenuItemIcon;
585: private static Icon menuArrowIcon;
586: private static Icon expandedTreeIcon;
587: private static Icon collapsedTreeIcon;
588:
589: /**
590: * Answers an <code>Icon</code> used for <code>JCheckBox</code>es.
591: */
592: static Icon getCheckBoxIcon() {
593: if (checkBoxIcon == null) {
594: checkBoxIcon = new CheckBoxIcon();
595: }
596: return checkBoxIcon;
597: }
598:
599: /**
600: * Answers an <code>Icon</code> used for <code>JCheckButtonMenuItem</code>s.
601: */
602: static Icon getCheckBoxMenuItemIcon() {
603: if (checkBoxMenuItemIcon == null) {
604: checkBoxMenuItemIcon = new CheckBoxMenuItemIcon();
605: }
606: return checkBoxMenuItemIcon;
607: }
608:
609: /**
610: * Answers an <code>Icon</code> used for <code>JRadioButtonMenuItem</code>s.
611: */
612: static Icon getRadioButtonMenuItemIcon() {
613: if (radioButtonMenuItemIcon == null) {
614: radioButtonMenuItemIcon = new RadioButtonMenuItemIcon();
615: }
616: return radioButtonMenuItemIcon;
617: }
618:
619: /**
620: * Answers an <code>Icon</code> used for arrows in <code>JMenu</code>s.
621: */
622: static Icon getMenuArrowIcon() {
623: if (menuArrowIcon == null) {
624: menuArrowIcon = new MenuArrowIcon();
625: }
626: return menuArrowIcon;
627: }
628:
629: /**
630: * Answers an <code>Icon</code> used in <code>JTree</code>s.
631: */
632: static Icon getExpandedTreeIcon() {
633: if (expandedTreeIcon == null) {
634: expandedTreeIcon = new ExpandedTreeIcon();
635: }
636: return expandedTreeIcon;
637: }
638:
639: /**
640: * Answers an <code>Icon</code> used in <code>JTree</code>s.
641: */
642: static Icon getCollapsedTreeIcon() {
643: if (collapsedTreeIcon == null) {
644: collapsedTreeIcon = new CollapsedTreeIcon();
645: }
646: return collapsedTreeIcon;
647: }
648:
649: /**
650: * Answers an <code>Icon</code> used in <code>JComboBox</code>es.
651: */
652: static Icon getComboBoxButtonIcon() {
653: return new ComboBoxButtonIcon();
654: }
655:
656: }
|