KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > gui > PolyPanel


1 package sellwin.gui;
2
3 import sellwin.domain.*;
4 import javax.swing.*;
5 import javax.swing.event.*;
6 import javax.swing.table.*;
7 import java.awt.*;
8 import java.awt.event.*;
9 import java.util.*;
10 import java.text.*;
11
12 // SellWin http://sourceforge.net/projects/sellwincrm
13
//Contact support@open-app.com for commercial help with SellWin
14
//This software is provided "AS IS", without a warranty of any kind.
15

16 /**
17  * This class is the viewable part of the sales
18  * funnel, polygons are used to draw the funnel, and
19  * each polygon is clickable.
20  */

21 public class PolyPanel extends JPanel implements MouseListener {
22     
23     private NumberFormat numFormat = new DecimalFormat("##0.00");
24     private OppListDialog oppListDialog = new OppListDialog();
25
26     private Whiteboard wb=null;
27
28     private int currentSelection = 0;
29
30     private static final int MAX=8;
31
32     private static final int X_OFF = 10, Y_OFF=-50;
33     private static final int TEXT_OFFSET_Y = 14, TEXT_OFFSET_X=7;
34
35     private ArrayList oppIndex = null;
36     private int totalOpps = 0;
37     private int[] oppCounts = new int[MAX];
38
39     private String JavaDoc[] str = {
40         Opportunity.STAGE_IDENTIFIED,
41         Opportunity.STAGE_CONTACTED,
42         Opportunity.STAGE_BIDDING,
43         Opportunity.STAGE_CHANCE,
44         Opportunity.STAGE_WINNING,
45         Opportunity.STAGE_AWARDED,
46         Opportunity.STAGE_CONTRACT,
47         Opportunity.STAGE_BILLED };
48
49     private Color[] colors = {
50             Color.blue,
51             Color.cyan,
52             Color.lightGray,
53             Color.green,
54             Color.magenta,
55             Color.orange,
56             Color.pink,
57             Color.red
58         };
59
60     private MyPolygon[] polys = {
61             new MyPolygon(
62                 new Point(121 + X_OFF,164 + Y_OFF),
63                 new Point(489 + X_OFF,164 + Y_OFF),
64                 new Point(478 + X_OFF,214 + Y_OFF),
65                 new Point(134 + X_OFF,214 + Y_OFF)),
66             new MyPolygon(
67                 new Point(134 + X_OFF,214 + Y_OFF),
68                 new Point(478 + X_OFF,214 + Y_OFF),
69                 new Point(465 + X_OFF,263 + Y_OFF),
70                 new Point(147 + X_OFF,263 + Y_OFF)),
71             new MyPolygon(
72                 new Point(147 + X_OFF,263 + Y_OFF),
73                 new Point(465 + X_OFF,263 + Y_OFF),
74                 new Point(454 + X_OFF,312 + Y_OFF),
75                 new Point(160 + X_OFF,312 + Y_OFF)),
76             new MyPolygon(
77                 new Point(160 + X_OFF,312 + Y_OFF),
78                 new Point(454 + X_OFF,312 + Y_OFF),
79                 new Point(442 + X_OFF,360 + Y_OFF),
80                 new Point(172 + X_OFF,360 + Y_OFF)),
81             new MyPolygon(
82                 new Point(172 + X_OFF,360 + Y_OFF),
83                 new Point(442 + X_OFF,360 + Y_OFF),
84                 new Point(429 + X_OFF,409 + Y_OFF),
85                 new Point(185 + X_OFF,409 + Y_OFF)),
86             new MyPolygon(
87                 new Point(185 + X_OFF,409 + Y_OFF),
88                 new Point(429 + X_OFF,409 + Y_OFF),
89                 new Point(418 + X_OFF,457 + Y_OFF),
90                 new Point(197 + X_OFF,457 + Y_OFF)),
91             new MyPolygon(
92                 new Point(197 + X_OFF,457 + Y_OFF),
93                 new Point(418 + X_OFF,457 + Y_OFF),
94                 new Point(406 + X_OFF,505 + Y_OFF),
95                 new Point(210 + X_OFF,505 + Y_OFF)),
96             new MyPolygon(
97                 new Point(210 + X_OFF,505 + Y_OFF),
98                 new Point(406 + X_OFF,505 + Y_OFF),
99                 new Point(393 + X_OFF,553 + Y_OFF),
100                 new Point(223 + X_OFF,553 + Y_OFF))
101         };
102
103     
104         
105     /**
106      * construct the polygon panel
107      */

108     public PolyPanel()
109         throws AngError {
110
111         this.addMouseListener(this);
112
113         refresh();
114
115         setSize(400,400);
116
117     }
118
119     /**
120      * refresh the opps
121      */

122     public final void refresh() {
123         wb = MainWindow.getWhiteboard();
124         try {
125             oppIndex = wb.getOpportunityIndex();
126             totalOpps = oppIndex.size();
127             countOpps();
128             this.repaint();
129         } catch (AngError e) {
130             ErrorHandler.show(this, e);
131         }
132     }
133
134     /** part of the MouseListener interface */
135     public void mouseEntered(MouseEvent e) { }
136
137     /** part of the MouseListener interface */
138     public void mouseExited(MouseEvent e) { }
139
140     /** part of the MouseListener interface */
141     public void mouseReleased(MouseEvent e) { }
142
143     /** part of the MouseListener interface */
144     public void mouseClicked(MouseEvent e) { }
145
146     /** part of the MouseListener interface */
147     public final void mousePressed(MouseEvent e) {
148
149         for (int i=0;i<8;i++) {
150             if (polys[i].contains(e.getX(), e.getY())) {
151                 currentSelection = i;
152                 showList(i);
153                 this.repaint();
154                 break;
155             }
156         }
157     
158     }
159
160     /**
161      * show the opportunities using the OppListDialog's
162      * contents
163      * @param i description
164      */

165     private final void showList(int i) {
166         ArrayList opps = new ArrayList();
167         OppIndex oi;
168         for (int j=0;j<oppIndex.size();j++) {
169             oi = (OppIndex)(oppIndex.get(j));
170             if (oi.getStage().equals(str[i]))
171                 opps.add(oi.getName());
172         }
173     
174         oppListDialog.setData(opps.toArray());
175         oppListDialog.show();
176     }
177
178     /**
179      * redraw the funnel polygons
180      * @param g the graphics context to draw with
181      */

182     public final void paintComponent(Graphics g) {
183         super.paintComponent(g);
184     
185         String JavaDoc str = null;
186
187         g.setFont(MainWindow.LABEL_FONT);
188     
189         //draw polys
190
for (int i=0;i<8;i++) {
191             g.drawPolygon(polys[i]);
192             if (i == currentSelection)
193                 g.setColor(Color.white);
194             else
195                 g.setColor(colors[i]);
196             g.fillPolygon(polys[i]);
197             g.setColor(Color.black);
198             str = buildStat(i);
199             g.drawString(str, polys[i].p1.x + TEXT_OFFSET_X, polys[i].p1.y + TEXT_OFFSET_Y);
200         }
201     }
202
203     /**
204      * figure the statistics for each polygon and
205      * construct a string to be drawn in
206      * @param i the number of status to
207      * @return the formatted stat string
208      */

209     private final String JavaDoc buildStat(int i) {
210         double ax, bx;
211
212
213         if (totalOpps <= 0) return str[i] + " 0 - 0%";
214
215         ax = oppCounts[i] * 1.00;
216         bx = totalOpps * 1.00;
217
218         double pct = (ax/bx) * 100.00;
219         return str[i] + " (" + oppCounts[i] + ")" + " " + numFormat.format(pct) + "%";
220     }
221
222     /**
223      * count all the opportunities for all stages
224      */

225     private final void countOpps() {
226         String JavaDoc stage;
227         for (int i=0;i<MAX;i++) {
228             stage = str[i];
229             oppCounts[i] = countStage(stage);
230         }
231     }
232
233     /**
234      * count all the opportunities for a given stage
235      * @param stage the stage to count
236      * @return the number of stages
237      */

238     private final int countStage(String JavaDoc stage) {
239         int cnt=0;
240         OppIndex opp;
241         for (int i=0;i<oppIndex.size();i++) {
242             opp = (OppIndex)(oppIndex.get(i));
243             if (opp.getStage().equals(stage))
244                 cnt++;
245         }
246         return cnt;
247     }
248 }
249
250 /**
251  * This class represents each polygon that makes up the
252  * sales funnel
253  *
254  * @author Jeff McCormick
255  */

256 class MyPolygon extends Polygon {
257     public Point p1, p2, p3, p4;
258
259     /** construct the polygon using 4 points
260     * @param p1 point 1
261     * @param p2 point 2
262     * @param p3 point 3
263     * @param p4 point 4
264     */

265     public MyPolygon(Point p1, Point p2, Point p3, Point p4) {
266         addPoint(p1.x, p1.y);
267         addPoint(p2.x, p2.y);
268         addPoint(p3.x, p3.y);
269         addPoint(p4.x, p4.y);
270     
271         this.p1 = p1;
272         this.p2 = p2;
273         this.p3 = p3;
274         this.p4 = p4;
275     }
276 }
277
Popular Tags