1 package org.argosfields.widget;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argosfields.image.ImageSet;
6 import org.argosfields.image.RankImageSet;
7 import org.argosfields.image.UnitImageSet;
8 import org.argosfields.model.Unit;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.PaintEvent;
11 import org.eclipse.swt.events.PaintListener;
12 import org.eclipse.swt.graphics.Color;
13 import org.eclipse.swt.graphics.Font;
14 import org.eclipse.swt.graphics.FontData;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.graphics.Rectangle;
17 import org.eclipse.swt.widgets.Canvas;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Display;
20
21 /***
22 * UnitView.java
23 *
24 * @author Xavier Cho
25 * @version $Revision: 1.3 $ $Date: 2003/12/01 07:38:04 $
26 */
27 public class UnitView extends Canvas {
28 private static Log log = LogFactory.getLog(UnitView.class);
29
30 private ImageSet unitSet;
31 private ImageSet rankSet;
32 private Unit unit;
33 private int unitWidth;
34 private int unitHeight;
35 private int rankWidth;
36 private int rankHeight;
37
38 /***
39 * @param parent
40 * @param style
41 */
42 public UnitView(final Composite parent, final int style) {
43 super(parent, style);
44
45 addPaintListener(new PaintListener() {
46 public void paintControl(final PaintEvent event) {
47 onPaint(event);
48 }
49 });
50
51 this.rankSet = RankImageSet.getInstance();
52 this.unitSet = UnitImageSet.getInstance();
53
54 this.unitWidth = unitSet.getIconWidth();
55 this.unitHeight = unitSet.getIconHeight();
56
57 this.rankWidth = rankSet.getIconWidth();
58 this.rankHeight = rankSet.getIconHeight();
59
60 Color color = getDisplay().getSystemColor(SWT.COLOR_BLACK);
61 setBackground(color);
62 }
63
64 /***
65 * @see org.eclipse.swt.widgets.Control#computeSize(int, int)
66 */
67 public Point computeSize(final int wHint, final int hHint) {
68 int width = unitWidth + 6;
69 int height = unitHeight + 10;
70
71 return new Point(width, height);
72 }
73
74 /***
75 * @return Returns the unit.
76 */
77 protected Unit getUnit() {
78 return unit;
79 }
80
81 /***
82 * @param unit The unit to set.
83 */
84 protected void setUnit(final Unit unit) {
85 this.unit = unit;
86 redraw();
87 }
88
89 private void onPaint(final PaintEvent event) {
90 if (unit != null) {
91 int index = unit.getIconIndex() - unit.getFacing().toInt();
92
93 Rectangle bounds = new Rectangle(0, 2, unitWidth, unitHeight);
94 unitSet.drawIcon(index, bounds, this, event.gc);
95
96 int x = unitWidth + 4 - rankWidth;
97 bounds = new Rectangle(x, 2, rankWidth, rankHeight);
98
99 rankSet.drawIcon(unit.getRank(), bounds, this, event.gc);
100
101 int strength = unit.getStrength();
102
103 Display display = getDisplay();
104
105 Font font = display.getSystemFont();
106 FontData fontData = font.getFontData()[0];
107 font = new Font(display, fontData.getName(), 10, SWT.NORMAL);
108
109 String value = Integer.toString(unit.getStrength());
110
111 Color color = display.getSystemColor(SWT.COLOR_WHITE);
112 event.gc.setForeground(color);
113
114 try {
115 event.gc.setFont(font);
116 event.gc.drawString(value, x + 3, rankHeight);
117 } finally {
118 font.dispose();
119 }
120
121 switch (strength) {
122 case 2 :
123 color = display.getSystemColor(SWT.COLOR_YELLOW);
124 break;
125 case 1 :
126 color = display.getSystemColor(SWT.COLOR_RED);
127 break;
128 default :
129 color = display.getSystemColor(SWT.COLOR_GREEN);
130 }
131
132 int width =
133 Math.max(
134 0,
135 (int) ((float) (getSize().x - 4)
136 / (float) Unit.MAX_STRENGTH)
137 * strength);
138
139 event.gc.setForeground(getBackground());
140 event.gc.setBackground(color);
141
142 event.gc.fillGradientRectangle(4, unitHeight + 2, width, 3, false);
143 }
144 }
145 }
This page was automatically generated by Maven