eclipsercpeclipse-gef

handle key combination when an action is already registered for it


My use case would be to handle the Ctrl (Cmd) + A key combination in a TextCellEditors Text control which opens on an "editable" label using gef (language is scala, sorry about that):

text.addKeyListener(new KeyAdapter {
  override def keyPressed(e: KeyEvent) = {
    val ctrlKey = if (Util.isMac) SWT.COMMAND else SWT.CTRL
    if (e.stateMask == ctrlKey && e.keyCode == 'a') {
      text.selectAll()
      e.doit = false
    }
  }
})

But since a "global" action is registered for the whole gef GraphicalEditor, I don't even receive the event.

How could I get around this and make the handler receive the key event?


Solution

  • Here is what I ended up doing, not sure if it's the most elegant solution, but certainly works (in the cell editor):

    var originalEnabled: Boolean = true
    
    def globalSelectAllAction = Option(
      PlatformUI.getWorkbench
        .getActiveWorkbenchWindow
        .getActivePage
        .getActiveEditor
        .getEditorSite
        .getActionBars
        .getGlobalActionHandler(ActionFactory.SELECT_ALL.getId)
    )
    
    override def activate() {
      super.activate()
      originalEnabled = globalSelectAllAction.map(_.isEnabled).getOrElse(true)
      globalSelectAllAction.foreach(_.setEnabled(false))
    }
    
    override def deactivate() {
      super.deactivate()
      globalSelectAllAction.foreach(_.setEnabled(originalEnabled))
    }
    

    This temporarily disables the CMD (Ctrl) + A action while the cell editor is active and re-enables it once the editor is gone