001 package org.springframework.richclient.taskpane; 002 003 import org.springframework.richclient.command.AbstractCommand; 004 005 import javax.swing.*; 006 import java.awt.*; 007 import java.awt.image.BufferedImage; 008 009 public class DefaultTaskPaneIconGenerator implements IconGenerator<AbstractCommand> 010 { 011 public static final int ROUND = 0; 012 public static final int DIAMOND = 1; 013 public static final int SQUARE = 2; 014 public static final int OCTAGON = 3; 015 016 private Color iconColor = Color.DARK_GRAY; 017 private int iconShape = SQUARE; 018 019 public Color getIconColor() 020 { 021 return iconColor; 022 } 023 024 public void setIconColor(Color iconColor) 025 { 026 this.iconColor = iconColor; 027 } 028 029 public int getIconShape() 030 { 031 return iconShape; 032 } 033 034 public void setIconShape(int iconShape) 035 { 036 this.iconShape = iconShape; 037 } 038 039 public ImageIcon generateIcon(AbstractCommand forObject) 040 { 041 char textChar = forObject.getText().charAt(0); 042 return new ImageIcon(createIcon(getIconShape(), getIconColor(), textChar)); 043 } 044 045 private static BufferedImage createIcon(int style, Color color, char text) 046 { 047 BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); 048 Graphics2D g = img.createGraphics(); 049 Color lighterColor = color.brighter().brighter(); 050 GradientPaint paint = new GradientPaint(0, 0, lighterColor, 16, 16, color); 051 g.setPaint(paint); 052 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 053 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 054 Polygon p = new Polygon(); 055 switch (style) 056 { 057 case ROUND: 058 g.fillOval(0, 0, 16, 16); 059 break; 060 case SQUARE: 061 g.fillRect(0, 0, 16, 16); 062 break; 063 case DIAMOND: 064 p.addPoint(8, 0); 065 p.addPoint(16, 8); 066 p.addPoint(8, 16); 067 p.addPoint(0, 8); 068 g.fillPolygon(p); 069 break; 070 case OCTAGON: 071 p.addPoint(0, 5); 072 p.addPoint(5, 0); 073 p.addPoint(11, 0); 074 p.addPoint(16, 5); 075 p.addPoint(16, 11); 076 p.addPoint(11, 16); 077 p.addPoint(5, 16); 078 p.addPoint(0, 11); 079 g.fillPolygon(p); 080 break; 081 default: 082 } 083 084 g.setFont(new Font("SansSerif", Font.PLAIN, 10)); 085 g.setColor(Color.white); 086 char uppercaseText = Character.toUpperCase(text); 087 if (uppercaseText == 'W' || uppercaseText == 'M') 088 { 089 g.drawString(Character.toString(text), 3f, 11f); 090 } 091 else 092 { 093 g.drawString(Character.toString(text), 4.5f, 11f); 094 } 095 img.flush(); 096 return img; 097 } 098 }