Skip to content

Commit

Permalink
Corrected color resources leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentpetit committed Feb 21, 2010
1 parent ceb060b commit d0ee917
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
Expand Up @@ -115,7 +115,8 @@ protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler rule
ISourceViewer viewer = new ClojureSourceViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(), styles, getPreferenceStore()); ISourceViewer viewer = new ClojureSourceViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(), styles, getPreferenceStore());
// ensure decoration support has been created and configured. // ensure decoration support has been created and configured.
getSourceViewerDecorationSupport(viewer); getSourceViewerDecorationSupport(viewer);
viewer.getTextWidget().addCaretListener(new SameWordHighlightingCaretListener(this)); viewer.getTextWidget().addCaretListener(new SameWordHighlightingCaretListener(this, CCWPlugin
.getDefault().getColorRegistry()));
return viewer; return viewer;
} }


Expand Down Expand Up @@ -180,7 +181,7 @@ protected void createActions() {
action = new LoadFileAction(this); action = new LoadFileAction(this);
action.setActionDefinitionId(IClojureEditorActionDefinitionIds.LOAD_FILE); action.setActionDefinitionId(IClojureEditorActionDefinitionIds.LOAD_FILE);
setAction(LoadFileAction.ID, action); setAction(LoadFileAction.ID, action);
action = new RunTestsAction(this); action = new RunTestsAction(this, CCWPlugin.getDefault().getColorRegistry());
action.setActionDefinitionId(IClojureEditorActionDefinitionIds.RUN_TESTS); action.setActionDefinitionId(IClojureEditorActionDefinitionIds.RUN_TESTS);
setAction(RunTestsAction.RUN_TESTS_ID, action); setAction(RunTestsAction.RUN_TESTS_ID, action);
action = new CompileLibAction(this); action = new CompileLibAction(this);
Expand Down
25 changes: 19 additions & 6 deletions ccw.core/src/ccw/editors/antlrbased/RunTestsAction.java
Expand Up @@ -14,20 +14,33 @@
import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action; import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.console.IOConsole; import org.eclipse.ui.console.IOConsole;


import ccw.debug.ClojureClient; import ccw.debug.ClojureClient;


public class RunTestsAction extends Action { public class RunTestsAction extends Action {
public static final String RUN_TESTS_ID = "RunTestsAction"; public static final String RUN_TESTS_ID = "RunTestsAction";
private static final String FAILED_TESTS_COLOR_KEY = "ccw.editors.RunTestsAction.COLOR_KEY";
private static final String PASSED_TESTS_COLOR_KEY = "ccw.editors.RunTestsAction.COLOR_KEY";
private final AntlrBasedClojureEditor editor; private final AntlrBasedClojureEditor editor;
private final ColorRegistry colorRegistry;


public RunTestsAction(AntlrBasedClojureEditor editor) { public RunTestsAction(AntlrBasedClojureEditor editor, ColorRegistry colorRegistry) {
this.editor = editor; this.editor = editor;
this.colorRegistry = colorRegistry;
initColorRegistry();
}
private void initColorRegistry() {
if (!colorRegistry.hasValueFor(FAILED_TESTS_COLOR_KEY)) {
colorRegistry.put(FAILED_TESTS_COLOR_KEY, new RGB(0xff, 0xff, 0xcf));
}
if (!colorRegistry.hasValueFor(PASSED_TESTS_COLOR_KEY)) {
colorRegistry.put(PASSED_TESTS_COLOR_KEY, new RGB(0xcf, 0xff, 0xcf));
}
} }

@Override @Override
public void run() { public void run() {
super.run(); super.run();
Expand All @@ -39,18 +52,18 @@ public void run() {
runTests(lib, clojure); runTests(lib, clojure);
} else { } else {
editor.setStatusLineErrorMessage(ClojureEditorMessages.Compilation_failed); editor.setStatusLineErrorMessage(ClojureEditorMessages.Compilation_failed);
setReplBackgroundColor(new Color(Display.getDefault(), 0xff, 0xff, 0xcf)); setReplBackgroundColor(colorRegistry.get(FAILED_TESTS_COLOR_KEY));
} }
} }


private void runTests(String lib, ClojureClient clojure) { private void runTests(String lib, ClojureClient clojure) {
String results = clojure.remoteLoad(runTestsCommand(lib)); String results = clojure.remoteLoad(runTestsCommand(lib));
if (results.contains(":fail 0, :error 0")) { if (results.contains(":fail 0, :error 0")) {
editor.setStatusLineErrorMessage(ClojureEditorMessages.Tests_passed); editor.setStatusLineErrorMessage(ClojureEditorMessages.Tests_passed);
setReplBackgroundColor(new Color(Display.getDefault(), 0xcf, 0xff, 0xcf)); setReplBackgroundColor(colorRegistry.get(PASSED_TESTS_COLOR_KEY));
} else { } else {
editor.setStatusLineErrorMessage(ClojureEditorMessages.Tests_failed); editor.setStatusLineErrorMessage(ClojureEditorMessages.Tests_failed);
setReplBackgroundColor(new Color(Display.getDefault(), 0xff, 0xcf, 0xcf)); setReplBackgroundColor(colorRegistry.get(FAILED_TESTS_COLOR_KEY));
} }
} }


Expand Down
Expand Up @@ -10,19 +10,35 @@
*******************************************************************************/ *******************************************************************************/
package ccw.editors.antlrbased; package ccw.editors.antlrbased;


import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.rules.IToken; import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.ITokenScanner; import org.eclipse.jface.text.rules.ITokenScanner;
import org.eclipse.swt.custom.CaretEvent; import org.eclipse.swt.custom.CaretEvent;
import org.eclipse.swt.custom.CaretListener; import org.eclipse.swt.custom.CaretListener;
import org.eclipse.swt.custom.StyleRange; import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB;


public class SameWordHighlightingCaretListener implements CaretListener { public class SameWordHighlightingCaretListener implements CaretListener {
private static final String COLOR_KEY = "ccw.editors.SameWordHighlightingCaretListener.COLOR_KEY";
private static final String OTHER_MATCHES_COLOR_KEY = "ccw.editors.SameWordHighlightingCaretListener.OTHER_MATCHES_COLOR_KEY";

private final AntlrBasedClojureEditor editor; private final AntlrBasedClojureEditor editor;
private final ColorRegistry colorRegistry;


public SameWordHighlightingCaretListener(AntlrBasedClojureEditor editor) { public SameWordHighlightingCaretListener(AntlrBasedClojureEditor editor, ColorRegistry colorRegistry) {
this.editor = editor; this.editor = editor;
this.colorRegistry = colorRegistry;
initColorRegistry();
}

private void initColorRegistry() {
if (!colorRegistry.hasValueFor(COLOR_KEY)) {
colorRegistry.put(COLOR_KEY, new RGB(225, 225, 225));
}
if (!colorRegistry.hasValueFor(OTHER_MATCHES_COLOR_KEY)) {
colorRegistry.put(OTHER_MATCHES_COLOR_KEY, new RGB(255, 255, 180));
}
} }


public void caretMoved(CaretEvent event) { public void caretMoved(CaretEvent event) {
Expand All @@ -41,7 +57,7 @@ public void caretMoved(CaretEvent event) {
} }


private StyleRange createRange(Tokens tokens) { private StyleRange createRange(Tokens tokens) {
return tokens.styleRange(new Color(editor.sourceViewer().getTextWidget().getDisplay(), 225, 225, 225)); return tokens.styleRange(colorRegistry.get(COLOR_KEY));
} }


private void colorOtherMatches(IDocument document, Tokens tokens, String original) { private void colorOtherMatches(IDocument document, Tokens tokens, String original) {
Expand All @@ -52,7 +68,7 @@ private void colorOtherMatches(IDocument document, Tokens tokens, String origina
if (token.getData() == null) { if (token.getData() == null) {
String tokenContents = tokens.tokenContents(); String tokenContents = tokens.tokenContents();
if (tokenContents.equals(original)) { if (tokenContents.equals(original)) {
StyleRange range = tokens.styleRange(new Color(editor.sourceViewer().getTextWidget().getDisplay(), 255, 255, 180)); StyleRange range = tokens.styleRange(colorRegistry.get(OTHER_MATCHES_COLOR_KEY));
editor.sourceViewer().getTextWidget().setStyleRange(range); editor.sourceViewer().getTextWidget().setStyleRange(range);
} }
} }
Expand Down

0 comments on commit d0ee917

Please sign in to comment.