java - 为 Eclipse 文本控件实现 "select on focus"行为

标签 java eclipse textbox swt eclipse-3.6

目标

我正在尝试为 Eclipse 实现“选择焦点”行为 Text控制:

  • 当控件已经获得焦点时:

    • 点击和拖动行为正常
  • 当控件获得焦点时:

    • 单击并拖动以选择文本的行为正常

    • 单击而不选择文本将选择所有文本

    • 使用键盘给予焦点将选择所有文本

问题

  • 简单地选择 SWT.FocusIn 的所有文本不会在通过鼠标单击聚焦时选择文本。

  • SWT.FocusIn SWT.MouseDown 之前触发,所以无法判断如果当用户按下鼠标时控件已经获得焦点。

问题

  1. 为什么 Eclipse 按此顺序触发事件?这对我来说没有任何意义。是否受某些支持的操作系统的限制?

  2. 我可以使用一些解决方法来实现此功能吗?

最佳答案

#eclipse 中有人将我与很久以前提出的 Eclipse 错误联系起来:Can't use focus listener to select all text

根据那里的其中一项建议,我提出了以下解决方案(适用于 Windows,未在其他平台上测试):

/**
 * This method adds select-on-focus functionality to a {@link Text} component.
 * 
 * Specific behavior:
 *  - when the Text is already focused -> normal behavior
 *  - when the Text is not focused:
 *    -> focus by keyboard -> select all text
 *    -> focus by mouse click -> select all text unless user manually selects text
 * 
 * @param text
 */
public static void addSelectOnFocusToText(Text text) {
  Listener listener = new Listener() {

    private boolean hasFocus = false;
    private boolean hadFocusOnMousedown = false;

    @Override
    public void handleEvent(Event e) {
      switch(e.type) {
        case SWT.FocusIn: {
          Text t = (Text) e.widget;

          // Covers the case where the user focuses by keyboard.
          t.selectAll();

          // The case where the user focuses by mouse click is special because Eclipse,
          // for some reason, fires SWT.FocusIn before SWT.MouseDown, and on mouse down
          // it cancels the selection. So we set a variable to keep track of whether the
          // control is focused (can't rely on isFocusControl() because sometimes it's wrong),
          // and we make it asynchronous so it will get set AFTER SWT.MouseDown is fired.
          t.getDisplay().asyncExec(new Runnable() {
            @Override
            public void run() {
              hasFocus = true;
            }
          });

          break;
        }
        case SWT.FocusOut: {
          hasFocus = false;
          ((Text) e.widget).clearSelection();

          break;
        }
        case SWT.MouseDown: {
          // Set the variable which is used in SWT.MouseUp.
          hadFocusOnMousedown = hasFocus;

          break;
        }
        case SWT.MouseUp: {
          Text t = (Text) e.widget;
          if(t.getSelectionCount() == 0 && !hadFocusOnMousedown) {
            ((Text) e.widget).selectAll();
          }

          break;
        }
      }
    }

  };

  text.addListener(SWT.FocusIn, listener);
  text.addListener(SWT.FocusOut, listener);
  text.addListener(SWT.MouseDown, listener);
  text.addListener(SWT.MouseUp, listener);
}

关于java - 为 Eclipse 文本控件实现 "select on focus"行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10038570/

相关文章:

java - 自动更新 web.xml 中的 servlet-class

javascript - Firefox 在 .NET 中提前记住文本框值

java - 来回更改 ListView 适配器?

java - 多种形式的 ID 生成

c++ - C 程序执行

php - 如何为表单文本字段提供固定大小 Laravel 4

c# - 自定义控件 BorderThickness

java - Tomcat:如何动态配置 server.xml?

java - 使用 Java 将 AWS 文件上传到 S3

eclipse - Eclipse Project Explorer可以突出显示Vim中存在编译错误的文件?