///////////////////////////////////////////////////////////////////////////////
// FILE:          MMKeyDispatcher.java
// PROJECT:       Micro-Manager
// SUBSYSTEM:     mmstudio
//-----------------------------------------------------------------------------

// COPYRIGHT:    University of California, San Francisco, 2008

// LICENSE:      This file is distributed under the BSD license.
// License text is included with the source distribution.

// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.

package org.micromanager.utils;

import java.awt.KeyEventDispatcher;
import java.awt.event.KeyEvent;
import org.micromanager.MMStudioMainFrame;

/**
 *
 * @author nico
 */
public class MMKeyDispatcher implements KeyEventDispatcher{
   private MMStudioMainFrame gui_;

   public MMKeyDispatcher(MMStudioMainFrame gui) {
      gui_ = gui;
	}

   /*
    * Exclude key events coming from specific sources (like text components)
    * Only way I could come up with was introspection
    * If there are other areas in the application in which keyevents should not
    * be processed, add those here
    */
   private boolean checkSource(KeyEvent ke) {
      Class [] forbiddenClasses = {
         java.awt.TextComponent.class,
         javax.swing.text.JTextComponent.class,
         org.jeditsyntax.JEditTextArea.class
      };
      Object source = ke.getSource();
      for (Class clazz:forbiddenClasses) {
         if (clazz.isInstance(source))
            return false;
      }
      return true;
   }

   /*
    * 
    */
   public boolean dispatchKeyEvent(KeyEvent ke) {
      if (ke.getID() != KeyEvent.KEY_PRESSED)
         return false;

      // Since all key events in the application go through here
      // we need to efficiently determinne whether or not to deal with this
      // key event will be dealt with.  CheckSource seems relatively expensive
      // so only call this when the key matches
      char key = ke.getKeyChar();
      // snap
      if (key == 's' && ke.getModifiers() == 0) {
         if (checkSource(ke)) {
            gui_.snapSingleImage();
            return true;
         }
      }
      // toggle live
      if (key == 'l' && ke.getModifiers() == 0) {
         if (checkSource(ke)) {
            if (gui_.getLiveMode())
               gui_.enableLiveMode(false);
            else
               gui_.enableLiveMode(true);
            return true;
         }
      }
      
      // acquire
      if (key == 'a' && ke.getModifiers() == 0) {
         if (checkSource(ke)) {
            gui_.snapAndAddToImage5D(null);
            return true;
         }
      }

      // toggle shutter
      if (key == ' ' && ke.getModifiers() == 0) {
         if (checkSource(ke)) {
            gui_.toggleShutter();
            return true;
         }
      }
      
      return false;
   }
}

@@ -1056,79 +1056,635 @@
 i_;%0A
-%0A   public MMKeyDispatcher(MMStudioMainFrame gui) %7B%0A      gui_ = gui;%0A%09
+      Class textCanvasClass = null;%0A      final Class %5B%5D forbiddenClasses_;%0A%0A   public MMKeyDispatcher(MMStudioMainFrame gui) %7B%0A      gui_ = gui;%0A      try %7B%0A         textCanvasClass = ClassLoader.getSystemClassLoader().loadClass(%22ij.text.TextCanvas%22);%0A      %7D catch (ClassNotFoundException ex) %7B%0A         textCanvasClass = null;%0A         ReportingUtils.logError(ex);%0A      %7D%0A%0A      Class %5B%5D forbiddenClasses = %7B%0A         java.awt.TextComponent.class,%0A         javax.swing.text.JTextComponent.class,%0A         org.jeditsyntax.JEditTextArea.class,%0A         textCanvasClass%0A      %7D;%0A      forbiddenClasses_ = forbiddenClasses;%0A   
 %7D%0A%0A 
@@ -1991,185 +1991,8 @@
 ) %7B%0A
-      Class %5B%5D forbiddenClasses = %7B%0A         java.awt.TextComponent.class,%0A         javax.swing.text.JTextComponent.class,%0A         org.jeditsyntax.JEditTextArea.class%0A      %7D;%0A
     
@@ -2064,16 +2064,17 @@
 nClasses
+_
 ) %7B%0A    
@@ -2087,16 +2087,33 @@
 f (clazz
+ != null && clazz
 .isInsta

use the classloader to get a reference to the ij.text.TextCanvas class type.