Getting toggleContinuousSpellChecking() to remember its state Today I was trying to write some cocoa-java code to remember the setting for the 'Edit -> Spelling -> Check Spelling as You Type' across application runs. It is made difficult by the fact that NSTextField doesn't respond to the toggleContinuousSpellChecking() message. It has to go to the First Responder object, and then the system magically turns on the spell checker.
After some fooling around I believe I have got it working. My steps: In Interface Builder I added a new method to the First Responder (called toggleSpellCheckMemory()). Then I changed the target of the 'Check Spelling as You Type' NSMenuItem to be my new method.
That will allow you to execute any code you want at the time the menuitem is selected, as long as some object in the responder chain catches it. The tricky part was then passing the message call along the responder chain so the spelling thing actually got turned on. The key was in a NSApplication method. I believe the same thing should have worked using NSResponder.tryToPerform(), but it didn't. Anyway, here's my code (still a rough draft, but it works)...
public void toggleSpellCheckMemory(NSMenuItem sender) { if (sender.state() == NSCell.OffState ) { // turn it on sender.setState( NSCell.OnState ); Prefs.putBoolean("spellCheck", true); } else { // turn it off sender.setState( NSCell.OffState ); Prefs.putBoolean("spellCheck", false); }
NSApplication.sharedApplication().sendActionToTargetFromSender(spellingSelector, null, sender); }
private NSSelector spellingSelector = new NSSelector("toggleContinuousSpellChecking", new Class[] {Object.class}); Maybe this will be useful to somebody out there. I know that I would have liked to find this info on the web when I went searching. Note: this has only been tested on MacOS X 10.3.3 with java 1.4.2_03-117.1
|