Changeset 32093 for main/trunk


Ignore:
Timestamp:
12/11/17 19:02:38 (3 years ago)
Author:
ak19
Message:

Ctr (or equiv on Mac) +F on GS3 GLI's ConfigFileEditor now focuses on the searchField.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/gui/ConfigFileEditor.java

    r32092 r32093  
    6262   
    6363    // https://github.com/bobbylight/RSyntaxTextArea/wiki/Example:-Using-Find-and-Replace
    64     private JTextField searchField;
     64    final private JTextField searchField;
    6565    private JCheckBox regexCB;
    6666    private JCheckBox matchCaseCB;
     
    123123    editor = new NumberedJTextArea(Dictionary.get("ConfigFileEditor.Tooltip"));
    124124    editor.getDocument().addDocumentListener(this);
    125 
     125    // if Ctrl (or Mac equiv) + F is pressed in the config file editing area,
     126    // then move the focus to the searchField, so the user can start typing the searchTerm
     127    editor.addKeyListener(new ConfigFileEditorKeyListener());
     128   
    126129    save_button = new GLIButton(Dictionary.get("General.Save"), Dictionary.get("ConfigFileEditor.Save_Tooltip"));
    127130    cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("ConfigFileEditor.Cancel_Tooltip")); // tooltip is the same
     
    202205    setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
    203206    editor.setCaretPosition(0); // start at top of config XML
     207
     208    // Following has no effect: not sure why on dialog show, with or without requesting focus,
     209    // it takes some time for the editor to get focus and only if user's mouse curser is
     210    // hovering over ConfigFileEditor and moves about a bit there.
     211    // Give editor focus on dialog show, so that Ctrl-F, to shift focus to searchField, will be
     212    // responsive too. Not working until mouse moves about over editor, despite the suggestion
     213    // at https://stackoverflow.com/questions/17828264/java-swing-jdialog-default-focus
     214    editor.requestFocusInWindow();
    204215    }
    205216
     
    342353        }
    343354    }
    344    
     355
     356
     357    // if Ctrl (or Mac equiv) + F is pressed in the config file editing area,
     358    // then move the focus to the searchField, so the user can start typing the searchTerm
     359    private class ConfigFileEditorKeyListener
     360    extends KeyAdapter
     361    {
     362    /** Gives notification of key events on the text field */
     363    public void keyPressed(KeyEvent key_event)
     364    {
     365        // Don't hardcode check for Ctrl key "(e.getModifiers() & KeyEvent.CTRL_MASK) != 0)"
     366        // because Mac uses another modifier key, Command-F instead of Ctrl-F.
     367        // https://stackoverflow.com/questions/5970765/java-detect-ctrlx-key-combination-on-a-jtree
     368        if((key_event.getModifiers() == Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())
     369           && key_event.getKeyCode() == KeyEvent.VK_F) {
     370
     371        // Instead of requestFocus() or the more forceful sounding grabFocus(),
     372        // use requestFocusInWindow(), see
     373        // https://stackoverflow.com/questions/1425392/how-do-you-set-a-focus-on-textfield-in-swing
     374        ConfigFileEditor.this.searchField.requestFocusInWindow();
     375        //JOptionPane.showMessageDialog(ConfigFileEditor.this, "Got a key", "Got key " + key_event.getKeyCode(), JOptionPane.INFORMATION_MESSAGE);
     376        }
     377    }
     378    }
    345379}
Note: See TracChangeset for help on using the changeset viewer.