Issue #2865 - fixed font memory leak

Change-Id: I0cfbd494924aadcb134c60313836cb9ab815253d

Former-commit-id: 2555d382f26278c5f972c17da778338142cdaaec
This commit is contained in:
Lee Venable 2014-03-11 08:54:07 -05:00
parent 6b51b05bdf
commit 2c67c4f6c4

View file

@ -88,6 +88,8 @@ import com.raytheon.viz.ui.views.CaveFloatingView;
* Feb 13, 2014 2751 bclement made generic * Feb 13, 2014 2751 bclement made generic
* Feb 18, 2014 2631 mpduff Add ability to play sounds on join actions * Feb 18, 2014 2631 mpduff Add ability to play sounds on join actions
* Feb 24, 2014 2632 mpduff Moved sound generation code to CollaborationUtils * Feb 24, 2014 2632 mpduff Moved sound generation code to CollaborationUtils
* Mar 06, 2014 #2865 lvenable Fixed font memory leaks added SWT dispose checks when
* running in an asynchronous thread.
* *
* </pre> * </pre>
* *
@ -115,6 +117,9 @@ public abstract class AbstractSessionView<T extends IUser> extends
protected StyledText messagesText; protected StyledText messagesText;
/** Font used with the messagesText control. */
private Font messagesTextFont;
private StyledText composeText; private StyledText composeText;
protected SessionMsgArchive msgArchive; protected SessionMsgArchive msgArchive;
@ -205,9 +210,10 @@ public abstract class AbstractSessionView<T extends IUser> extends
} }
}); });
// here need to grab the font from preferences and use that font // here need to grab the font from preferences and use that font
messagesText.setFont(new Font(Display.getCurrent(), PreferenceConverter messagesTextFont = new Font(Display.getCurrent(),
.getFontData(Activator.getDefault().getPreferenceStore(), PreferenceConverter.getFontData(Activator.getDefault()
"font"))); .getPreferenceStore(), "font"));
messagesText.setFont(messagesTextFont);
searchComp.setSearchText(messagesText); searchComp.setSearchText(messagesText);
@ -341,92 +347,111 @@ public abstract class AbstractSessionView<T extends IUser> extends
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (messagesText.getCharCount() != 0) {
if (messagesText.isDisposed() == false
&& messagesText.getCharCount() != 0) {
sb.append("\n"); sb.append("\n");
} }
sb.append("(").append(time).append(") "); sb.append("(").append(time).append(") ");
int offset = sb.length(); int offset = sb.length();
sb.append(name).append(": ").append(body); sb.append(name).append(": ").append(body);
// here is the place to put the font and color changes for // here is the place to put the font and color changes for
// keywords // keywords
// read in localization file once and then don't read in again, // read in localization file once and then don't read in again,
// per // per
// chat room? // chat room?
List<AlertWord> alertWords = retrieveAlertWords(); if (messagesText.isDisposed() == false) {
List<StyleRange> ranges = new ArrayList<StyleRange>(); List<AlertWord> alertWords = retrieveAlertWords();
if (alertWords != null) { List<StyleRange> ranges = new ArrayList<StyleRange>();
for (AlertWord keyword : alertWords) { if (alertWords != null) {
String text = keyword.getText().toLowerCase(); for (AlertWord keyword : alertWords) {
if (sb.toString().toLowerCase().contains(text)) { String text = keyword.getText().toLowerCase();
String lowerCase = sb.toString().toLowerCase(); if (sb.toString().toLowerCase().contains(text)) {
// getting the current length of the text String lowerCase = sb.toString().toLowerCase();
int currentLength = messagesText.getCharCount(); // getting the current length of the text
int index = lowerCase.indexOf(text); int currentLength = messagesText.getCharCount();
while (index >= 0) { int index = lowerCase.indexOf(text);
Font font = null; while (index >= 0) {
// storing off fonts so we don't leak Font font = null;
if (fonts.containsKey(keyword.getFont())) { // storing off fonts so we don't leak
font = fonts.get(keyword.getFont()); if (fonts.containsKey(keyword.getFont())) {
} else { font = fonts.get(keyword.getFont());
FontData fd = StringConverter } else {
.asFontData(keyword.getFont()); FontData fd = StringConverter
font = new Font(Display.getCurrent(), fd); .asFontData(keyword.getFont());
fonts.put(keyword.getFont(), font); font = new Font(Display.getCurrent(),
} fd);
fonts.put(keyword.getFont(), font);
}
RGB rgb = new RGB(keyword.getRed(), keyword RGB rgb = new RGB(keyword.getRed(), keyword
.getGreen(), keyword.getBlue()); .getGreen(), keyword.getBlue());
Color color = null; Color color = null;
// using the stored colors so we don't leak // using the stored colors so we don't leak
if (colors.containsKey(rgb)) { if (colors.containsKey(rgb)) {
color = colors.get(rgb); color = colors.get(rgb);
} else { } else {
color = new Color(Display.getCurrent(), rgb); color = new Color(Display.getCurrent(),
colors.put(rgb, color); rgb);
} colors.put(rgb, color);
TextStyle style = new TextStyle(font, color, }
null); TextStyle style = new TextStyle(font,
StyleRange keywordRange = new StyleRange(style); color, null);
keywordRange.start = currentLength + index; StyleRange keywordRange = new StyleRange(
keywordRange.length = keyword.getText() style);
.length(); keywordRange.start = currentLength + index;
keywordRange.length = keyword.getText()
.length();
ranges.add(keywordRange); ranges.add(keywordRange);
// compare to see if this position is already // compare to see if this position is
// styled // already
List<StyleRange> rnges = new ArrayList<StyleRange>(); // styled
rnges.addAll(ranges); List<StyleRange> rnges = new ArrayList<StyleRange>();
for (StyleRange range : rnges) { rnges.addAll(ranges);
if (range.start <= keywordRange.start for (StyleRange range : rnges) {
&& (range.start + range.length) >= keywordRange.start) { if (range.start <= keywordRange.start
if (keywordRange != range) { && (range.start + range.length) >= keywordRange.start) {
if (range.length < keywordRange.length) { if (keywordRange != range) {
ranges.remove(range); if (range.length < keywordRange.length) {
} else { ranges.remove(range);
ranges.remove(keywordRange); } else {
ranges.remove(keywordRange);
}
} }
} }
} }
}
// only execute things if the same user didn't // only execute things if the same user
// type it // didn't
if (!myUser.equals(userId)) { // type it
executeSightsSounds(keyword); if (!myUser.equals(userId)) {
executeSightsSounds(keyword);
}
// need to handle all instances of the
// keyword
// within the chat
index = lowerCase.indexOf(text,
text.length() + index);
} }
// need to handle all instances of the keyword
// within the chat
index = lowerCase.indexOf(text, text.length()
+ index);
} }
} }
} }
styleAndAppendText(sb, offset, name, userId, subject,
ranges);
} }
styleAndAppendText(sb, offset, name, userId, subject, ranges); // Archive the message
msgArchive.archive(sb.toString()); msgArchive.archive(sb.toString());
searchComp.appendText(sb.toString());
// Append the text to the search control.
if (searchComp.isDisposed() == false) {
searchComp.appendText(sb.toString());
}
} }
}); });
} }
@ -500,6 +525,10 @@ public abstract class AbstractSessionView<T extends IUser> extends
@Override @Override
public void dispose() { public void dispose() {
if (messagesTextFont != null) {
messagesTextFont.dispose();
}
for (Image im : imageMap.values()) { for (Image im : imageMap.values()) {
im.dispose(); im.dispose();
} }
@ -540,7 +569,11 @@ public abstract class AbstractSessionView<T extends IUser> extends
@Subscribe @Subscribe
public void changeFont(FontData data) { public void changeFont(FontData data) {
messagesText.setFont(new Font(Display.getCurrent(), data)); if (messagesTextFont != null) {
messagesTextFont.dispose();
}
messagesTextFont = new Font(Display.getCurrent(), data);
messagesText.setFont(messagesTextFont);
} }
public void setAlertWords(List<AlertWord> words) { public void setAlertWords(List<AlertWord> words) {
@ -582,21 +615,33 @@ public abstract class AbstractSessionView<T extends IUser> extends
VizApp.runAsync(new Runnable() { VizApp.runAsync(new Runnable() {
@Override @Override
public void run() { public void run() {
Color color = Display.getCurrent().getSystemColor(swtColor);
Date date = new Date(); Date date = new Date();
String time = dateFormatter.get().format(date); String time = dateFormatter.get().format(date);
builder.insert(0, "(" + time + ") : "); builder.insert(0, "(" + time + ") : ");
if (messagesText.getCharCount() != 0) {
builder.insert(0, "\n"); // Update the messagesText with the StyleRange highlights
if (messagesText.isDisposed() == false) {
if (messagesText.getCharCount() != 0) {
builder.insert(0, "\n");
}
Color color = Display.getCurrent().getSystemColor(swtColor);
StyleRange range = new StyleRange(messagesText
.getCharCount(), builder.length(), color, null,
SWT.BOLD);
List<StyleRange> ranges = new ArrayList<StyleRange>();
ranges.add(range);
styleAndAppendText(builder, 0, builder.toString(), null,
ranges, color);
} }
StyleRange range = new StyleRange(messagesText.getCharCount(),
builder.length(), color, null, SWT.BOLD); // Archive the message
List<StyleRange> ranges = new ArrayList<StyleRange>();
ranges.add(range);
styleAndAppendText(builder, 0, builder.toString(), null,
ranges, color);
msgArchive.archiveLine(builder.toString()); msgArchive.archiveLine(builder.toString());
searchComp.appendText(builder.toString());
// Append the text to the search control.
if (searchComp.isDisposed() == false) {
searchComp.appendText(builder.toString());
}
} }
}); });
} }