001: package snow.utils.gui;
002:
003: //import javax.swing.border.EmptyBorder;
004: import javax.swing.*;
005: import java.awt.*;
006:
007: /** Either file or directory shape, with specified color and optional letter
008: */
009: public class FileIcon implements Icon {
010: public enum IconColor {
011: Red, Green, RedGreen, White, System
012: }
013:
014: private IconColor type;
015: private int size;
016:
017: // the file or directory outer shape
018: Polygon poly;
019:
020: private String letter = null;
021: private Font letterFont;
022:
023: public FileIcon(boolean directory, int size) {
024: this (directory, IconColor.White, size);
025: }
026:
027: public FileIcon(boolean directory, IconColor type, int size) {
028: this .type = type;
029: this .size = size;
030:
031: if (directory) {
032: initShapeDirectory();
033: } else {
034: initShapeFile();
035: }
036: letterFont = new Font("Dialog", Font.PLAIN, size * 5 / 8);
037: }
038:
039: public void setType(IconColor type) {
040: this .type = type;
041: }
042:
043: public void setLetter(String letter) {
044: this .letter = letter;
045: }
046:
047: boolean delete;
048:
049: public void setRemovedFromProject(boolean delete) {
050: this .delete = delete;
051: }
052:
053: private void initShapeFile() {
054: poly = new Polygon();
055: poly.addPoint(size / 8, 0);
056: poly.addPoint(size * 5 / 8, 0);
057: poly.addPoint(size * 7 / 8, 2 * size / 8);
058: poly.addPoint(size * 7 / 8, size);
059: poly.addPoint(size / 8, size);
060: }
061:
062: private void initShapeDirectory() {
063: poly = new Polygon();
064: poly.addPoint(0, size / 6);
065: poly.addPoint((int) (size * 0.5), size / 6);
066: poly.addPoint((int) (size * 0.65), size / 11);
067: poly.addPoint((int) (size * 0.85), size / 11);
068: poly.addPoint(size, size / 6);
069: poly.addPoint(size, size * 7 / 8);
070: poly.addPoint(0, size * 7 / 8);
071: }
072:
073: public int getIconHeight() {
074: return size;
075: }
076:
077: public int getIconWidth() {
078: return size;
079: }
080:
081: public void paintIcon(Component c, Graphics g, int x, int y) {
082: Graphics2D g2 = (Graphics2D) g;
083:
084: g.translate(x, y);
085:
086: Color color1 = Color.white;
087: Color color2 = Color.black;
088:
089: if (type == IconColor.RedGreen) {
090: color1 = new Color(220, 0, 0);
091: color2 = new Color(0, 220, 0);
092: } else if (type == IconColor.Red) {
093: color1 = new Color(120, 80, 80);
094: color2 = new Color(220, 0, 0);
095: } else if (type == IconColor.Green) {
096: color1 = new Color(80, 120, 80);
097: color2 = new Color(0, 220, 0);
098: } else if (type == IconColor.White) {
099: color1 = Color.white;
100: color2 = Color.white;
101: } else if (type == IconColor.System) {
102: //blue color1 = new Color(0,80,80);
103: // color2 = new Color(0,220,220);
104: color1 = new Color(180, 180, 10);
105: color2 = new Color(220, 220, 10);
106:
107: }
108:
109: g2.setPaint(new GradientPaint(0, 0, color1, size, size, color2,
110: false));
111: g.fillPolygon(poly);
112:
113: // for mixed folders
114: if (type == IconColor.RedGreen) {
115: }
116:
117: if (letter != null) {
118: Font oldFont = g2.getFont();
119:
120: g2.setColor(Color.black);
121: g2.setFont(letterFont);
122: g2.drawString(letter, size / 4 + 1, size * 6 / 8); // May2007: shift 1
123:
124: g2.setFont(oldFont);
125: }
126:
127: if (delete) {
128: g.setColor(Color.red);
129:
130: g.drawLine(0, 0, size, size);
131: g.drawLine(1, 0, size, size - 1);
132: g.drawLine(0, size, size, 0);
133: g.drawLine(0, size - 1, size - 1, 0);
134:
135: }
136:
137: // border
138: g.setColor(Color.black);
139: g.drawPolygon(poly);
140:
141: g.translate(-x, -y);
142: }
143:
144: /*test
145: public static void main(String[] a)
146: {
147: JFrame frame = new JFrame();
148: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
149:
150: frame.setLocationRelativeTo(null);
151: FileIcon fic = new FileIcon(true, 16);
152: fic.setLetter("A");
153: fic.setType(IconColor.System);
154: JLabel lab = new JLabel(fic);
155: frame.add(lab);
156: lab.setBorder(new EmptyBorder(5,5,5,5));
157: frame.pack();
158: frame.setVisible(true);
159: }*/
160: }
|