Search in sources :

Example 1 with Query

use of org.opensolaris.opengrok.egrok.query.Query in project OpenGrok by OpenGrok.

the class ToolBarControl method createControl.

@Override
protected Control createControl(Composite parent) {
    final Text searchBox = new Text(parent, SWT.FILL | SWT.BORDER);
    TextUtils.setToDefault(searchBox, "{OpenGrok");
    searchBox.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            if (!disableFocusLostEvent) {
                TextUtils.setToDefault(searchBox, "{OpenGrok");
            }
        }

        @Override
        public void focusGained(FocusEvent e) {
            TextUtils.makeEditable(searchBox);
        }
    });
    searchBox.addKeyListener(new KeyAdapter() {

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
                doSearch();
            } else if (e.keyCode == SWT.ARROW_UP) {
                historyIndex++;
                if (historyIndex > history.size()) {
                    historyIndex = history.size();
                }
                if (historyIndex >= 0 && history.size() >= historyIndex) {
                    searchBox.setText(history.get(historyIndex - 1));
                }
            } else if (e.keyCode == SWT.ARROW_DOWN) {
                historyIndex--;
                if (historyIndex < 0) {
                    historyIndex = 0;
                } else if (history.size() > historyIndex) {
                    searchBox.setText(history.get(historyIndex));
                }
            } else if ((e.stateMask & SWT.CTRL) == SWT.CTRL && e.keyCode == 'v') {
                searchBox.setText("");
                searchBox.paste();
                if ((e.stateMask & SWT.SHIFT) == SWT.SHIFT) {
                    doSearch();
                }
            } else if (e.stateMask == SWT.CTRL && e.keyCode == 'c') {
                searchBox.copy();
            }
        }

        private void doSearch() {
            String text = searchBox.getText();
            if (text != null && !"".equals(text)) {
                history.add(0, text);
                historyIndex = 0;
                Rectangle bounds = searchBox.getBounds();
                Point topLeft = new Point(bounds.x, bounds.y + bounds.height);
                topLeft = searchBox.getShell().toDisplay(topLeft);
                final ResultsDialog dialog = new ResultsDialog(Display.getCurrent().getActiveShell(), text, topLeft);
                Query query = new Query(text);
                disableFocusLostEvent = true;
                query.run(dialog);
                disableFocusLostEvent = false;
            }
        }
    });
    return parent;
}
Also used : KeyEvent(org.eclipse.swt.events.KeyEvent) Query(org.opensolaris.opengrok.egrok.query.Query) KeyAdapter(org.eclipse.swt.events.KeyAdapter) Rectangle(org.eclipse.swt.graphics.Rectangle) Text(org.eclipse.swt.widgets.Text) Point(org.eclipse.swt.graphics.Point) FocusListener(org.eclipse.swt.events.FocusListener) FocusEvent(org.eclipse.swt.events.FocusEvent)

Aggregations

FocusEvent (org.eclipse.swt.events.FocusEvent)1 FocusListener (org.eclipse.swt.events.FocusListener)1 KeyAdapter (org.eclipse.swt.events.KeyAdapter)1 KeyEvent (org.eclipse.swt.events.KeyEvent)1 Point (org.eclipse.swt.graphics.Point)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 Text (org.eclipse.swt.widgets.Text)1 Query (org.opensolaris.opengrok.egrok.query.Query)1