///////////////////////////////////////////////////////////////////////////////
// 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 test editors)
    * 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) {
      String t = ke.getSource().getClass().toString();
      if (t.equals("class bsh.util.JConsole$1"))
         return false;
      if (t.equals("class org.jeditsyntax.JEditTextArea"))
         return false;
      if (t.equals("class java.awt.TextArea"))
         return false;
      if (t.equals("class java.awt.TextField"))
         return false;
      if (t.equals("class ij.text.TextCanvas"))
         return false;
      if (t.equals("class javax.swing.JTextField"))
         return false;
      if (t.equals("class javax.swing.JFormattedTextField"))
         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 whne 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;
   }
}

@@ -1438,559 +1438,256 @@
     
-String t = ke.getSource().getClass().toString();%0A      if (t.equals(%22class bsh.util.JConsole$1%22))%0A         return false;%0A      if (t.equals(%22class org.jeditsyntax.JEditTextArea%22))%0A         return false;%0A      if (t.equals(%22class java.awt.TextArea%22))%0A         return false;%0A      if (t.equals(%22class java.aw
+Class %5B%5D forbiddenClasses = %7B%0A         java.awt.TextComponent.class,%0A         javax.swing.tex
 t.
+J
 Text
-Field%22))%0A         return false;%0A      if (t.equals(%22class ij.text.TextCanvas%22))%0A         return false;%0A      if (t.equals(%22class javax.swing.JTextField%22))%0A         return false;
+Component.class%0A      %7D;%0A      Object source = ke.getSource();%0A      for (Class clazz:forbiddenClasses) %7B
 %0A      
+   
 if (
-t.equals(%22class javax.swing.JFormattedTextField%22))%0A
+clazz.isInstance(source))%0A   
     
@@ -1705,16 +1705,24 @@
  false;%0A
+      %7D%0A
       re

generalize code to prevent key trapping on any text component (awt or swing)