KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > loadprofile > gui > DrawableCanvas


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF
20 *
21 * Contact: clif@objectweb.org
22 */

23 package org.objectweb.clif.scenario.util.isac.loadprofile.gui;
24
25 import java.util.Vector JavaDoc;
26
27 import org.apache.log4j.Category;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.graphics.Color;
30 import org.eclipse.swt.graphics.GC;
31 import org.eclipse.swt.widgets.Canvas;
32 import org.eclipse.swt.widgets.Composite;
33 import org.objectweb.clif.scenario.util.isac.loadprofile.GroupDescription;
34 import org.objectweb.clif.scenario.util.isac.loadprofile.Point;
35 import org.objectweb.clif.scenario.util.isac.loadprofile.RampDescription;
36
37 /**
38  * This class extends Canvas and add some methods which permit to draw some curves
39  *
40  * @author JC Meillaud
41  * @author A Peyrard
42  */

43 public class DrawableCanvas extends Canvas {
44     // logger
45
static Category cat = Category.getInstance(DrawableCanvas.class.getName()) ;
46     // selection size (must be dividable by 2 !!!)
47
public static final int SELECTION_SIZE = 8 ;
48     // selection color
49
private static final int SELECTION_COLOR = SWT.COLOR_BLACK ;
50     // back color
51
public static final int BACK_COLOR = SWT.COLOR_WHITE ;
52     
53     /////////////////////////////////////////////////////////////
54
// Constructors...
55
/////////////////////////////////////////////////////////////
56

57     /**
58      * Build a new Drawable Canvas
59      * @param parent The parent composite
60      * @param style The style of this widget
61      */

62     public DrawableCanvas(Composite parent, int style) {
63         super(parent, style);
64         this.setBackground(parent.getDisplay().getSystemColor(BACK_COLOR)) ;
65     }
66
67     /////////////////////////////////////////////////////////////////
68
// Drawing methods...
69
/////////////////////////////////////////////////////////////////
70

71     /**
72      * Draw a ramp with a specified color
73      * @param c The ramp description of the curve to draw
74      * @param color The color of the ramp which will be drawed
75      */

76     public void drawRamp(RampDescription c, Color color) {
77         // analyse the type of the ramp
78
switch (c.getType()) {
79             case RampDescription.LINE :
80                 this.drawLineRamp(c, color) ;
81                 break ;
82             case RampDescription.ARC :
83                 this.drawArcRamp(c, color) ;
84                 break ;
85             case RampDescription.CRENEL_HV :
86                 this.drawCrenelHVRamp(c,color) ;
87                 break ;
88             case RampDescription.CRENEL_VH :
89                 this.drawCrenelVHRamp(c,color) ;
90                 break ;
91             default :
92                 cat.warn("UNKNOW RAMP TYPE : " + c.getType()) ;
93         }
94     }
95     
96     /**
97      * Draw a crenel horizontal-vertical
98      * @param rd The ramp description
99      * @param c The color
100      */

101     private void drawCrenelHVRamp(RampDescription rd, Color c) {
102         // draw the crenel
103
this.drawCrenelHV(rd.getStart(), rd.getEnd(), c) ;
104     }
105     
106     /**
107      * Draw a crenel vertical-horizontal
108      * @param rd The ramp description
109      * @param c The color
110      */

111     private void drawCrenelVHRamp(RampDescription rd, Color c) {
112         // draw the crenel
113
this.drawCrenelVH(rd.getStart(), rd.getEnd(), c) ;
114     }
115     
116     /**
117      * Draw a line ramp
118      * @param c The ramp
119      * @param color The color of the draw
120      */

121     private void drawLineRamp(RampDescription c, Color color) {
122         // get the drawable element
123
GC gc = new GC(this) ;
124         gc.setForeground(color) ;
125         gc.drawLine((int)c.getStart().x, (int)c.getStart().y, (int)c.getEnd().x, (int)c.getEnd().y) ;
126         gc.dispose() ;
127     }
128     
129     /**
130      * Draw an arc ramp
131      * @param c The ramp
132      * @param color The color of the draw
133      */

134     private void drawArcRamp(RampDescription c, Color color) {
135         // TODO
136
}
137     
138     /**
139      * Draw an arrow
140      * @param p The poit begining of the arrow
141      * @param size The size of the arrow
142      * @param orientation The orientation 0, or 1
143      * @param color The color of the arrow
144      */

145     public void drawArrow(Point p, int size, int orientation, Color color) {
146         // calculate the three points of the arrow
147
Point p1 = new Point(0,0);
148         Point p2 = new Point(0,0) ;
149         Point p3 = new Point(0,0) ;
150         if (orientation == 0) {
151             p1.x = p.x - (size / 2) ;
152             p1.y = p.y ;
153             p2.x = p.x + (size/2) ;
154             p2.y = p.y ;
155             p3.x = p.x ;
156             p3.y = p.y - size ;
157         } else if (orientation == 1) {
158             p1.x = p.x ;
159             p1.y = p.y - (size / 2) ;
160             p2.x = p.x ;
161             p2.y = p.y + (size / 2) ;
162             p3.x = p.x + size ;
163             p3.y = p.y ;
164         } else
165             return ;
166         // get the gc of the canvas
167
GC gc = new GC(this) ;
168         gc.setBackground(color) ;
169         gc.fillPolygon(new int[]{(int)p1.x,(int)p1.y,(int)p2.x,(int)p2.y,(int)p3.x,(int)p3.y}) ;
170         gc.dispose() ;
171     }
172     
173     /**
174      * Draw a text on the canvas
175      * @param p The point of the begining of the text
176      * @param text The text to be printed
177      * @param color The color of the text
178      */

179     public void drawString(Point p, String JavaDoc text, Color color) {
180         // get the gc of the canvas
181
GC gc = new GC(this) ;
182         gc.setForeground(color) ;
183         gc.drawString(text, (int)p.x, (int)p.y) ;
184         gc.dispose() ;
185     }
186     
187     /**
188      * Draw a line between the two given points in parameters
189      * @param p The first point
190      * @param q The second point
191      * @param color The color of the line
192      */

193     public void drawLine(Point p, Point q, Color color) {
194         // get the gc of the canvas
195
GC gc = new GC(this) ;
196         gc.setForeground(color) ;
197         gc.drawLine((int)p.x, (int)p.y, (int)q.x, (int)q.y) ;
198         gc.dispose() ;
199     }
200     
201     /**
202      * Erase the line between the two given points
203      * @param p The first point
204      * @param q The second point
205      */

206     public void eraseLine(Point p, Point q) {
207         // draw a line with the back canvas color
208
this.drawLine(p, q, this.getDisplay().getSystemColor(BACK_COLOR)) ;
209     }
210     
211     /**
212      * Draw a sub-crenel which is composed by two lines, the first is horizontal and the second one vertical
213      * @param p The starting point
214      * @param q The ending point
215      * @param c The color of the crenel to draw
216      */

217     private void drawCrenelHV(Point p, Point q, Color c) {
218         // Calculate the third point which will be the intersection of the two lines
219
Point intersection = new Point(q.x-1, p.y) ;
220         // draw the two lines to form the crenel
221
this.drawLine(p,intersection,c) ;
222         this.drawLine(intersection,q,c) ;
223     }
224
225     /**
226      * Draw a sub-crenel which is composed by two lines, the first is vertical and the second one horizontal
227      * @param p The starting point
228      * @param q The ending point
229      * @param c The color of the crenel to draw
230      */

231     private void drawCrenelVH(Point p, Point q, Color c) {
232         // Calculate the third point which will be the intersection of the two lines
233
Point intersection = new Point(p.x+1, q.y) ;
234         // draw the two lines to form the crenel
235
this.drawLine(p,intersection,c) ;
236         this.drawLine(intersection,q,c) ;
237     }
238     
239     ///////////////////////////////////////////////////////////////////////////////////////
240
// Selection methods...
241
//////////////////////////////////////////////////////////////////////////////////////
242

243     /**
244      * Select a given point (draw a rectangle arround the point to show that the point is selected)
245      * @param p The point to be selected
246      * @param fill True if the selection rectangle must be fill
247      */

248     public void selectPoint(Point p, boolean fill) {
249         // get th gc of the canvas
250
GC gc = new GC(this) ;
251         if (!fill) {
252             gc.setForeground(this.getDisplay().getSystemColor(SELECTION_COLOR)) ;
253             // draw the selection rectangle
254
gc.drawRectangle((int)p.x - (SELECTION_SIZE / 2), (int)p.y - (SELECTION_SIZE / 2), SELECTION_SIZE, SELECTION_SIZE) ;
255         }
256         else {
257             gc.setBackground(this.getDisplay().getSystemColor(SELECTION_COLOR)) ;
258             // draw the selection filled rectangle
259
gc.fillRectangle((int)p.x - (SELECTION_SIZE / 2), (int)p.y - (SELECTION_SIZE / 2), SELECTION_SIZE, SELECTION_SIZE) ;
260         }
261         gc.dispose() ;
262     }
263     
264     /**
265      * Erase the square which is arround the point to show the selection
266      * @param p The editor point
267      */

268     public void eraseSelectionPoint(Point p) {
269         // get the gc of the canvas
270
GC gc = new GC(this) ;
271         gc.setForeground(this.getDisplay().getSystemColor(BACK_COLOR)) ;
272         gc.drawRectangle((int)p.x - (SELECTION_SIZE / 2), (int)p.y - (SELECTION_SIZE / 2), SELECTION_SIZE, SELECTION_SIZE) ;
273         gc.dispose() ;
274     }
275     
276     /**
277      * Select the given group
278      * @param g The given group to select
279      */

280     public void selectGroup(GroupDescription g) {
281         // get the ramps of the group
282
Vector JavaDoc ramps = g.getRamps() ;
283         // for each ramp select it
284
for (int i=0;i<ramps.size();i++)
285             this.selectRamp((RampDescription)ramps.elementAt(i)) ;
286     }
287     
288     /**
289      * Select all the point of the given ramp
290      * @param c The curve to be selected
291      */

292     private void selectRamp(RampDescription c) {
293         // analyse the type of the ramp
294
switch (c.getType()) {
295             case RampDescription.LINE :
296                 this.selectLineRamp(c) ;
297                 break ;
298             case RampDescription.ARC :
299                 this.selectArcRamp(c) ;
300                 break ;
301             case RampDescription.CRENEL_HV :
302             case RampDescription.CRENEL_VH :
303                 this.selectCrenelXXRamp(c) ;
304             default :
305                 cat.warn("UNKNOW RAMP TYPE : " + c.getType()) ;
306         }
307     }
308     
309     /**
310      * Select a line ramp
311      * @param c The ramp to select
312      */

313     private void selectLineRamp(RampDescription c) {
314         // select the starting and the ending point
315
this.selectPoint(c.getStart(), false) ;
316         this.selectPoint(c.getEnd(), false) ;
317     }
318     
319     /**
320      * Select a crenel ramp
321      * @param c The ramp to select
322      */

323     private void selectCrenelXXRamp(RampDescription c) {
324         // select the starting and the ending point
325
this.selectPoint(c.getStart(), false) ;
326         this.selectPoint(c.getEnd(), false) ;
327     }
328     
329     /**
330      * Select an arc ramp
331      * @param c The ramp to select
332      */

333     private void selectArcRamp(RampDescription c) {
334         // TODO
335
}
336 }
337
Popular Tags