001: package tide.sources;
002:
003: import javax.swing.border.EmptyBorder;
004: import snow.utils.gui.GUIUtils;
005: import snow.lookandfeel.ThemesManager;
006: import java.awt.geom.GeneralPath;
007:
008: import javax.swing.*;
009: import java.awt.*;
010:
011: /** Either file or directory shape, with specified color and optional letter, and compiled, edited status, ...
012: */
013: public class SourcesTreeIcon implements Icon {
014:
015: public enum IconColor {
016: Red, Green, RedGreen, White
017: } // white for empty dirs
018:
019: public enum Ignored {
020: Yes, No, Partially
021: } // Partially for directories
022:
023: private IconColor type;
024: private int size;
025: private Ignored ignoredType = Ignored.No;
026:
027: // the file or directory outer shape
028: private Polygon poly;
029:
030: private String letter = null;
031: private Font letterFont;
032:
033: public boolean compiled = false;
034: public final boolean isDirectory;
035:
036: public boolean hasMessages = false;
037: public boolean hasMainMethod = false;
038: public boolean isMainProjectClass = false;
039:
040: public boolean isVisible = true;
041:
042: public boolean doNotDrawCompiledBar = false; // not draw ! [Dec2006]
043:
044: public SourcesTreeIcon(boolean directory, int size) {
045: this (directory, IconColor.White, size);
046: }
047:
048: public SourcesTreeIcon(boolean directory, IconColor type, int size) {
049: this .type = type;
050: this .size = size;
051: this .isDirectory = directory;
052:
053: if (directory) {
054: initShapeDirectory();
055: letterFont = new Font("Lucida Sans Typewriter", Font.PLAIN,
056: size * 5 / 8);
057: } else {
058: initShapeFile();
059: letterFont = new Font("Lucida Sans Typewriter", Font.PLAIN,
060: size * 5 / 8);
061: }
062:
063: }
064:
065: public void setType(IconColor type) {
066: this .type = type;
067: }
068:
069: public void setLetter(String letter) {
070: this .letter = letter;
071: }
072:
073: /** will draw a red cross above the icon
074: */
075: public void setIgnored(Ignored type) {
076: this .ignoredType = type;
077: }
078:
079: private void initLockShape() {
080: }
081:
082: private void initShapeFile() {
083: poly = new Polygon();
084:
085: poly.addPoint((int) (size * 0.15), (int) (size * 0.1)); // upper left
086: poly.addPoint((int) (size * 0.55), (int) (size * 0.1));
087: poly.addPoint((int) (size * 0.8), (int) (size * 0.34));
088: poly.addPoint((int) (size * 0.8), size);
089: poly.addPoint((int) (size * 0.15), size);
090: }
091:
092: private void initShapeDirectory() {
093: poly = new Polygon();
094: poly.addPoint((int) (size * 0.10), (int) (size * 0.16)); // upper left
095: poly.addPoint((int) (size * 0.45), (int) (size * 0.16));
096: poly.addPoint((int) (size * 0.50), (int) (size * 0.04));
097: poly.addPoint((int) (size * 0.88), (int) (size * 0.04));
098: poly.addPoint(size - 1, (int) (size * 0.18));
099: poly.addPoint(size - 1, (int) (size * 0.85));
100: poly.addPoint((int) (size * 0.1), (int) (size * 0.85));
101: }
102:
103: public int getIconHeight() {
104: return size;
105: }
106:
107: public int getIconWidth() {
108: return size;
109: }
110:
111: public Color green1 = new Color(60, 180, 60); // contrast
112: public Color green2 = new Color(120, 220, 120); // light
113:
114: public Color red1 = new Color(180, 60, 60); // contrast
115: public Color red2 = new Color(230, 160, 160); // light
116:
117: /** TODO !! */
118: public void setColorsFromChoosenUI() {
119: green1 = ThemesManager.getInstance().getGreen();
120: // ... TODO ...
121:
122: }
123:
124: public void paintIcon(Component c, Graphics g, int x, int y) {
125: if (!isVisible)
126: return;
127:
128: Graphics2D g2 = (Graphics2D) g;
129: Color oldColor = g.getColor();
130:
131: g.translate(x, y);
132:
133: Color color1 = Color.white;
134: Color color2 = Color.black;
135:
136: if (type == IconColor.RedGreen) {
137: color1 = green2;
138: color2 = red2;
139: } else if (type == IconColor.Red) {
140: color1 = red1;
141: color2 = red2;
142: } else if (type == IconColor.Green) {
143: color1 = green1;
144: color2 = green2;
145: } else if (type == IconColor.White) {
146: color1 = Color.white;
147: color2 = Color.white;
148: }
149:
150: g2.setPaint(new GradientPaint(0, 0, color1, size, size, color2,
151: false));
152: g.fillPolygon(poly);
153:
154: if (letter != null) {
155: Font oldFont = g2.getFont();
156:
157: g2.setColor(Color.black);
158: g2.setFont(letterFont);
159: if (this .isDirectory) {
160: g2.drawString(letter, size / 3 + 1, size * 6 / 8 + 1);
161: } else {
162: g2.drawString(letter, size / 4, size * 7 / 8);
163: }
164:
165: g2.setFont(oldFont);
166: }
167:
168: if (ignoredType == Ignored.Partially
169: || ignoredType == Ignored.Yes) {
170: g.setColor(Color.red);
171: g.drawLine(0, 0, size, size);
172: g.drawLine(1, 0, size, size - 1);
173:
174: if (ignoredType == Ignored.Yes) {
175: g.drawLine(0, size, size, 0);
176: g.drawLine(0, size - 1, size - 1, 0);
177: }
178: }
179:
180: // border
181: g.setColor(Color.black);
182: g.drawPolygon(poly);
183:
184: if (!doNotDrawCompiledBar) {
185: if (compiled) {
186: // [Dec2006]: do nothing if compiled
187: g.setColor(Color.green);
188: } else {
189: g.setColor(Color.red);
190: g.drawLine(0, size - 2, 0, 2);
191: g.drawLine(1, size - 2, 1, 2);
192:
193: }
194:
195: }
196:
197: if (this .hasMessages) {
198: g.setColor(Color.cyan);
199: g.fillArc(size - 2, size - 6, 4, 4, 0, 360);
200: //g.drawLine(size, size-2, size, 2);
201: //g.drawLine(size-1, size-2, size-1, 2);
202: }
203:
204: if (hasMainMethod) {
205:
206: if (isMainProjectClass) {
207: g.setColor(new Color(0, 0, 220)); // blue
208: } else {
209: g.setColor(new Color(0, 220, 0)); // green
210: }
211: //g.drawRect(0,0,size,size);
212: GeneralPath gp = new GeneralPath();
213: gp.moveTo(size, 0);
214: gp.lineTo(size * 7 / 10, 0);
215: gp.lineTo(size, size * 2 / 10);
216: gp.closePath();
217: g.fillPolygon(new int[] { size, size * 7 / 10, size },
218: new int[] { 0, 0, size * 3 / 10 }, 3);
219:
220: }
221:
222: // restore old graphics settings
223: g.translate(-x, -y);
224: g.setColor(oldColor);
225: }
226:
227: public static SourcesTreeIcon createSimpleLetterIcon(String le) {
228: SourcesTreeIcon jarIcon = new SourcesTreeIcon(false,
229: SourcesTreeIcon.IconColor.White, 17);
230: jarIcon.setLetter(le);
231: jarIcon.doNotDrawCompiledBar = true;
232: return jarIcon;
233: }
234:
235: /*test*/
236: public static void main(String[] a) {
237: JPanel pan = new JPanel();
238: pan.setLayout(new BoxLayout(pan, BoxLayout.X_AXIS));
239: for (int i = 10; i < 30; i++) {
240: SourcesTreeIcon sti = new SourcesTreeIcon(false, i);
241: sti.setLetter("J");
242: sti.hasMessages = true;
243: sti.hasMainMethod = true;
244: sti.setType(IconColor.Green);
245: JLabel lab = new JLabel(sti);
246: pan.add(lab);
247: pan.add(Box.createHorizontalStrut(5));
248: lab.setBorder(new EmptyBorder(4, 4, 4, 4));
249: }
250:
251: pan.setBorder(new EmptyBorder(4, 4, 4, 4));
252: GUIUtils.displayInFrame("test", pan, true);
253: }
254: }
|