Issue #2346 - fixed cursor memory leaks

Former-commit-id: 24c55ec007 [formerly 24c55ec007 [formerly 645aceffd1d205eb4be4c8fd122e6cdf7b3b368e]]
Former-commit-id: 2c46c54a87
Former-commit-id: 71f4f58a1b
This commit is contained in:
Lee Venable 2013-09-09 15:12:05 -05:00
parent 66d023227d
commit f6da35ba37
2 changed files with 79 additions and 65 deletions

View file

@ -57,6 +57,7 @@ import com.raytheon.viz.ui.dialogs.CaveSWTDialog;
* Date Ticket# Engineer Description * Date Ticket# Engineer Description
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* 29 NOV 2007 373 lvenable Initial creation * 29 NOV 2007 373 lvenable Initial creation
* 09 Sep 2013 #2346 lvenable Fixed cursor memory leak.
* *
* </pre> * </pre>
* *
@ -164,8 +165,8 @@ public class DamDisplayControlDlg extends CaveSWTDialog {
super(parent, SWT.DIALOG_TRIM, CAVE.DO_NOT_BLOCK); super(parent, SWT.DIALOG_TRIM, CAVE.DO_NOT_BLOCK);
setText("Dam Display Control"); setText("Dam Display Control");
waitCursor = new Cursor(parent.getDisplay(), SWT.CURSOR_WAIT); waitCursor = parent.getDisplay().getSystemCursor(SWT.CURSOR_WAIT);
arrowCursor = new Cursor(parent.getDisplay(), SWT.CURSOR_ARROW); arrowCursor = parent.getDisplay().getSystemCursor(SWT.CURSOR_ARROW);
} }
/* /*

View file

@ -28,6 +28,8 @@ import java.util.TimeZone;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener; import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintEvent;
@ -54,6 +56,7 @@ import com.raytheon.viz.hydrocommon.HydroConstants;
* Date Ticket# Engineer Description * Date Ticket# Engineer Description
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* Sep 4, 2008 2259 lvenable Initial creation * Sep 4, 2008 2259 lvenable Initial creation
* 09 Sep 2013 #2346 lvenable Fixed cursor memory leak.
* *
* </pre> * </pre>
* *
@ -87,7 +90,7 @@ public class FloodReportCanvasComp {
* Canvas composite containing the 2 canvases. * Canvas composite containing the 2 canvases.
*/ */
private Composite canvasComp; private Composite canvasComp;
/** /**
* The scrolling composite that holds the graph * The scrolling composite that holds the graph
*/ */
@ -112,10 +115,10 @@ public class FloodReportCanvasComp {
* Graph canvas width. * Graph canvas width.
*/ */
private final int CANVAS_WIDTH = 350; private final int CANVAS_WIDTH = 350;
/** Pixels per hour */ /** Pixels per hour */
private final int PIXELS_PER_HOUR = 7; private final int PIXELS_PER_HOUR = 7;
/** /**
* Scrolled composite height. * Scrolled composite height.
*/ */
@ -184,16 +187,16 @@ public class FloodReportCanvasComp {
/** Flood Stage */ /** Flood Stage */
private double fs = 0; private double fs = 0;
/** /**
* Calculated width of the graph canvas * Calculated width of the graph canvas
*/ */
private int canvasWidth = CANVAS_WIDTH; private int canvasWidth = CANVAS_WIDTH;
private long zeroTime = 0; private long zeroTime = 0;
private long lastTime = 0; private long lastTime = 0;
/** /**
* Constructor. * Constructor.
* *
@ -209,8 +212,9 @@ public class FloodReportCanvasComp {
gl.horizontalSpacing = 0; gl.horizontalSpacing = 0;
canvasComp.setLayout(gl); canvasComp.setLayout(gl);
crossHairCursor = new Cursor(parentComp.getDisplay(), SWT.CURSOR_CROSS); crossHairCursor = parentComp.getDisplay().getSystemCursor(
arrowCursor = new Cursor(parentComp.getDisplay(), SWT.CURSOR_ARROW); SWT.CURSOR_CROSS);
arrowCursor = parentComp.getDisplay().getSystemCursor(SWT.CURSOR_ARROW);
setupLabelCanvas(); setupLabelCanvas();
setupGraphCanvas(); setupGraphCanvas();
@ -218,11 +222,22 @@ public class FloodReportCanvasComp {
/* Graph Area Rectangle */ /* Graph Area Rectangle */
// Set to 1 to prevent cross hairs from staying on // Set to 1 to prevent cross hairs from staying on
// graph after mouse has left the area // graph after mouse has left the area
graphAreaRectangle = new Rectangle(1, 0, SCROLLED_COMP_WIDTH, CANVAS_HEIGHT graphAreaRectangle = new Rectangle(1, 0, SCROLLED_COMP_WIDTH,
- BOTTOM_OFFSET); CANVAS_HEIGHT - BOTTOM_OFFSET);
canvasFont = new Font(parentComp.getDisplay(), "Monospace", 10, canvasFont = new Font(parentComp.getDisplay(), "Monospace", 10,
SWT.NORMAL); SWT.NORMAL);
parentComp.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (canvasFont != null) {
canvasFont.dispose();
canvasFont = null;
}
}
});
} }
/** /**
@ -249,8 +264,8 @@ public class FloodReportCanvasComp {
* Setup the graph canvas. * Setup the graph canvas.
*/ */
private void setupGraphCanvas() { private void setupGraphCanvas() {
scrolledComp = new ScrolledComposite(canvasComp, scrolledComp = new ScrolledComposite(canvasComp, SWT.H_SCROLL
SWT.H_SCROLL | SWT.V_SCROLL); | SWT.V_SCROLL);
GridLayout gl = new GridLayout(1, false); GridLayout gl = new GridLayout(1, false);
scrolledComp.setLayout(gl); scrolledComp.setLayout(gl);
GridData gd = new GridData(SCROLLED_COMP_WIDTH, SCROLLED_COMP_HEIGHT); GridData gd = new GridData(SCROLLED_COMP_WIDTH, SCROLLED_COMP_HEIGHT);
@ -261,8 +276,8 @@ public class FloodReportCanvasComp {
gd.heightHint = CANVAS_HEIGHT; gd.heightHint = CANVAS_HEIGHT;
gd.widthHint = CANVAS_WIDTH; gd.widthHint = CANVAS_WIDTH;
graphCanvas.setSize(CANVAS_WIDTH, CANVAS_HEIGHT); graphCanvas.setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
graphCanvas.setLayoutData(gd); graphCanvas.setLayoutData(gd);
graphCanvas.addPaintListener(new PaintListener() { graphCanvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) { public void paintControl(PaintEvent e) {
@ -276,16 +291,16 @@ public class FloodReportCanvasComp {
handleMouseMoveEvent(e); handleMouseMoveEvent(e);
} }
}); });
graphCanvas.addListener(SWT.MouseExit, new Listener() { graphCanvas.addListener(SWT.MouseExit, new Listener() {
@Override @Override
public void handleEvent(Event e) { public void handleEvent(Event e) {
handleMouseExitEvent(e); handleMouseExitEvent(e);
} }
}); });
scrolledComp.setContent(graphCanvas); scrolledComp.setContent(graphCanvas);
} }
@ -432,38 +447,37 @@ public class FloodReportCanvasComp {
/** /**
* Get the pixel value corresponding to the date * Get the pixel value corresponding to the date
* *
* @param date The date to convert to pixel value * @param date
* @return * The date to convert to pixel value
* The pixel value * @return The pixel value
*/ */
private int x2pixel(Date date) { private int x2pixel(Date date) {
double millisPerPixel = (60 * 60 * 1000) / PIXELS_PER_HOUR; double millisPerPixel = (60 * 60 * 1000) / PIXELS_PER_HOUR;
double xValue = (date.getTime() - zeroTime) / millisPerPixel; double xValue = (date.getTime() - zeroTime) / millisPerPixel;
return (int) Math.round(xValue); return (int) Math.round(xValue);
} }
/** /**
* Get the Date (X) value corresponding to the pixel value * Get the Date (X) value corresponding to the pixel value passed in
* passed in
* *
* @param xpix The pixel value to convert to Date * @param xpix
* The pixel value to convert to Date
* *
* @return * @return The date corresponding to the pixel value
* The date corresponding to the pixel value
*/ */
private Date pixel2x(int xpix) { private Date pixel2x(int xpix) {
double millisPerPixel = (60 * 60 * 1000) / PIXELS_PER_HOUR; double millisPerPixel = (60 * 60 * 1000) / PIXELS_PER_HOUR;
double millisTime = xpix * millisPerPixel + zeroTime; double millisTime = xpix * millisPerPixel + zeroTime;
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.setTimeInMillis((long)millisTime); cal.setTimeInMillis((long) millisTime);
if (cal.get(Calendar.SECOND) >= 30) { if (cal.get(Calendar.SECOND) >= 30) {
cal.add(Calendar.MINUTE, 1); cal.add(Calendar.MINUTE, 1);
} }
cal.set(Calendar.SECOND, 0); cal.set(Calendar.SECOND, 0);
return cal.getTime(); return cal.getTime();
} }
/** /**
* convert real Y value to pixel value. * convert real Y value to pixel value.
* *
@ -475,7 +489,8 @@ public class FloodReportCanvasComp {
*/ */
private int y2pixel(double y) { private int y2pixel(double y) {
double yDiff = this.maxY - this.minY; double yDiff = this.maxY - this.minY;
double yValue = ((CANVAS_HEIGHT - BOTTOM_OFFSET - TOP_OFFSET)/ yDiff) * (y - minY); double yValue = ((CANVAS_HEIGHT - BOTTOM_OFFSET - TOP_OFFSET) / yDiff)
* (y - minY);
return (int) ((CANVAS_HEIGHT - BOTTOM_OFFSET) - Math.round(yValue)); return (int) ((CANVAS_HEIGHT - BOTTOM_OFFSET) - Math.round(yValue));
} }
@ -491,7 +506,8 @@ public class FloodReportCanvasComp {
*/ */
private double pixel2y(int ypix) { private double pixel2y(int ypix) {
double ydiff = maxY - minY; double ydiff = maxY - minY;
double pixPerUnit = (CANVAS_HEIGHT - BOTTOM_OFFSET - TOP_OFFSET) / ydiff; double pixPerUnit = (CANVAS_HEIGHT - BOTTOM_OFFSET - TOP_OFFSET)
/ ydiff;
return (maxY - ((ypix - TOP_OFFSET) / pixPerUnit)); return (maxY - ((ypix - TOP_OFFSET) / pixPerUnit));
} }
@ -582,19 +598,19 @@ public class FloodReportCanvasComp {
gc.drawString("00Z", x - 5, y + 15); gc.drawString("00Z", x - 5, y + 15);
/* Draw the month/day text. */ /* Draw the month/day text. */
gc.drawString(String.format(format, gc.drawString(
c.get(Calendar.MONTH) + 1) String.format(format, c.get(Calendar.MONTH) + 1)
+ "/" + "/"
+ String.format(format, c + String.format(format,
.get(Calendar.DAY_OF_MONTH)), x - 19, c.get(Calendar.DAY_OF_MONTH)),
y + 28); x - 19, y + 28);
} else { /* just a regular 6 hour period */ } else { /* just a regular 6 hour period */
/* Draw the hour text */ /* Draw the hour text */
gc.drawString(c.get(Calendar.HOUR_OF_DAY) + "", x - 5, gc.drawString(c.get(Calendar.HOUR_OF_DAY) + "", x - 5,
y + 15); y + 15);
} }
} }
x += PIXELS_PER_HOUR; x += PIXELS_PER_HOUR;
} }
} }
@ -636,7 +652,7 @@ public class FloodReportCanvasComp {
gc.setLineWidth(lineWidth); gc.setLineWidth(lineWidth);
} }
/** /**
* Reset the graph canvas * Reset the graph canvas
*/ */
@ -647,7 +663,7 @@ public class FloodReportCanvasComp {
Date endDate = (Date) floodData.get(floodData.size() - 1)[1]; Date endDate = (Date) floodData.get(floodData.size() - 1)[1];
long start = startDate.getTime() - HydroConstants.MILLIS_PER_DAY; long start = startDate.getTime() - HydroConstants.MILLIS_PER_DAY;
long end = endDate.getTime() + HydroConstants.MILLIS_PER_DAY; long end = endDate.getTime() + HydroConstants.MILLIS_PER_DAY;
TimeZone gmt = TimeZone.getTimeZone("GMT"); TimeZone gmt = TimeZone.getTimeZone("GMT");
Calendar startCal = Calendar.getInstance(); Calendar startCal = Calendar.getInstance();
startCal.setTimeZone(gmt); startCal.setTimeZone(gmt);
@ -667,28 +683,29 @@ public class FloodReportCanvasComp {
this.zeroTime = startCal.getTimeInMillis(); this.zeroTime = startCal.getTimeInMillis();
this.lastTime = endCal.getTimeInMillis(); this.lastTime = endCal.getTimeInMillis();
int hours = (int) ((lastTime - zeroTime)/HydroConstants.MILLIS_PER_HOUR); int hours = (int) ((lastTime - zeroTime) / HydroConstants.MILLIS_PER_HOUR);
// 1 hour border at the right // 1 hour border at the right
canvasWidth = hours * PIXELS_PER_HOUR - PIXELS_PER_HOUR; canvasWidth = hours * PIXELS_PER_HOUR - PIXELS_PER_HOUR;
if (canvasWidth < CANVAS_WIDTH) { if (canvasWidth < CANVAS_WIDTH) {
canvasWidth = CANVAS_WIDTH - PIXELS_PER_HOUR; canvasWidth = CANVAS_WIDTH - PIXELS_PER_HOUR;
} }
graphAreaRectangle = new Rectangle(1, 0, canvasWidth, CANVAS_HEIGHT graphAreaRectangle = new Rectangle(1, 0, canvasWidth, CANVAS_HEIGHT
- BOTTOM_OFFSET); - BOTTOM_OFFSET);
graphCanvas.setSize(canvasWidth, CANVAS_HEIGHT); graphCanvas.setSize(canvasWidth, CANVAS_HEIGHT);
graphCanvas.layout(); graphCanvas.layout();
labelCanvas.layout(); labelCanvas.layout();
canvasComp.layout(); canvasComp.layout();
} }
private void getFloodData() { private void getFloodData() {
FloodReportDataManager dman = FloodReportDataManager.getInstance(); FloodReportDataManager dman = FloodReportDataManager.getInstance();
Map<String, FloodReportData> dataMap = dman.getReportData(); Map<String, FloodReportData> dataMap = dman.getReportData();
FloodReportData data = dataMap.get(selectedKey); FloodReportData data = dataMap.get(selectedKey);
floodData = dman.getFloodEventData(data.getLid(), data.getFloodEventId()); floodData = dman.getFloodEventData(data.getLid(),
data.getFloodEventId());
} }
/** /**
@ -699,7 +716,8 @@ public class FloodReportCanvasComp {
String display = sdf.format(date); String display = sdf.format(date);
double y = pixel2y(currentY); double y = pixel2y(currentY);
parentDlg.setStageLbl(String.format("Stage: %6.2f at %s Z", y, display)); parentDlg.setStageLbl(String
.format("Stage: %6.2f at %s Z", y, display));
} }
/** /**
@ -711,16 +729,16 @@ public class FloodReportCanvasComp {
private void handleMouseMoveEvent(MouseEvent e) { private void handleMouseMoveEvent(MouseEvent e) {
/* Set the cursor location and cursor type */ /* Set the cursor location and cursor type */
Rectangle bounds = graphCanvas.getBounds(); Rectangle bounds = graphCanvas.getBounds();
if (((e.x > 0) && //bounds.contains(e.x, e.y) && if (((e.x > 0) && // bounds.contains(e.x, e.y) &&
(e.y >= 10) && (e.y < CANVAS_HEIGHT - BOTTOM_OFFSET) && (e.y >= 10) && (e.y < CANVAS_HEIGHT - BOTTOM_OFFSET) && (e.x < bounds.width
(e.x < bounds.width - PIXELS_PER_HOUR))) { - PIXELS_PER_HOUR))) {
if (selectedLid != null) { if (selectedLid != null) {
graphCanvas.setCursor(crossHairCursor); graphCanvas.setCursor(crossHairCursor);
currentX = e.x; currentX = e.x;
currentY = e.y; currentY = e.y;
drawCrossHairs = true; drawCrossHairs = true;
graphCanvas.redraw(); graphCanvas.redraw();
updateStageLbl(); updateStageLbl();
@ -732,11 +750,12 @@ public class FloodReportCanvasComp {
graphCanvas.redraw(); graphCanvas.redraw();
} }
} }
/** /**
* Remove the crosshairs when mouse leaves the graph. * Remove the crosshairs when mouse leaves the graph.
* *
* @param Event The mouse Event * @param Event
* The mouse Event
*/ */
private void handleMouseExitEvent(Event e) { private void handleMouseExitEvent(Event e) {
drawCrossHairs = false; drawCrossHairs = false;
@ -761,17 +780,11 @@ public class FloodReportCanvasComp {
Map<String, FloodReportData> dataMap = dman.getReportData(); Map<String, FloodReportData> dataMap = dman.getReportData();
FloodReportData data = dataMap.get(selectedKey); FloodReportData data = dataMap.get(selectedKey);
selectedLid = data.getLid(); selectedLid = data.getLid();
if (data != null) { if (data != null) {
fs = data.getFloodStage(); fs = data.getFloodStage();
floodData = dman.getFloodEventData(selectedLid, data.getFloodEventId()); floodData = dman.getFloodEventData(selectedLid,
} data.getFloodEventId());
}
protected void dispose() {
if (canvasFont != null) {
canvasFont.dispose();
canvasFont = null;
} }
} }
} }