org.eclipse.swt.custom.VerifyKeyListener Java Examples

The following examples show how to use org.eclipse.swt.custom.VerifyKeyListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractEditStrategyProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public List<IAutoEditStrategy> getStrategies(final ISourceViewer sourceViewer,final String contentType) {
	final List<IAutoEditStrategy> strategies = Lists.newArrayList();
	configure(new IEditStrategyAcceptor() {
		@Override
		public void accept(IAutoEditStrategy strategy, String type) {
			if (type == null || contentType.equals(type)) {
				if (strategy instanceof ISourceViewerAware) {
					((ISourceViewerAware) strategy).setSourceViewer(sourceViewer);
				}
				if (strategy instanceof VerifyKeyListener) {
					sourceViewer.getTextWidget().addVerifyKeyListener((VerifyKeyListener) strategy);
				}
				strategies.add(strategy);
			}
		}
	});
	return strategies;
}
 
Example #2
Source File: ContentAssistSubjectControlAdapter.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#prependVerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener)
 */
public boolean prependVerifyKeyListener(VerifyKeyListener verifyKeyListener)
{
	if (fContentAssistSubjectControl != null)
	{
		return fContentAssistSubjectControl.prependVerifyKeyListener(verifyKeyListener);
	}
	else if (fViewer instanceof ITextViewerExtension)
	{
		ITextViewerExtension e = (ITextViewerExtension) fViewer;
		e.prependVerifyKeyListener(verifyKeyListener);
		return true;
	}
	else
	{

		StyledText textWidget = fViewer.getTextWidget();
		if (Helper.okToUse(textWidget))
		{
			textWidget.addVerifyKeyListener(verifyKeyListener);
			return true;
		}
	}
	return false;
}
 
Example #3
Source File: ContentAssistSubjectControlAdapter.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#appendVerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener)
 */
public boolean appendVerifyKeyListener(VerifyKeyListener verifyKeyListener)
{
	if (fContentAssistSubjectControl != null)
	{
		return fContentAssistSubjectControl.appendVerifyKeyListener(verifyKeyListener);
	}
	else if (fViewer instanceof ITextViewerExtension)
	{
		ITextViewerExtension extension = (ITextViewerExtension) fViewer;
		extension.appendVerifyKeyListener(verifyKeyListener);
		return true;
	}
	else
	{
		StyledText textWidget = fViewer.getTextWidget();
		if (Helper.okToUse(textWidget))
		{
			textWidget.addVerifyKeyListener(verifyKeyListener);
			return true;
		}
	}
	return false;
}
 
Example #4
Source File: ContentAssistSubjectControlAdapter.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#removeVerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener)
 */
public void removeVerifyKeyListener(VerifyKeyListener verifyKeyListener)
{
	if (fContentAssistSubjectControl != null)
	{
		fContentAssistSubjectControl.removeVerifyKeyListener(verifyKeyListener);
	}
	else if (fViewer instanceof ITextViewerExtension)
	{
		ITextViewerExtension extension = (ITextViewerExtension) fViewer;
		extension.removeVerifyKeyListener(verifyKeyListener);
	}
	else
	{
		StyledText textWidget = fViewer.getTextWidget();
		if (Helper.okToUse(textWidget))
		{
			textWidget.removeVerifyKeyListener(verifyKeyListener);
		}
	}
}
 
Example #5
Source File: KbdMacroSupport.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
public void setExecuting(boolean is, ITextEditor editor, VerifyKeyListener vkf) {
	boolean wasExecuting = isExecuting();
	// keep track of nested macro executions
	if (is) {
		++executeCount;
	} else {
		--executeCount;
	}
	if (!wasExecuting && is) {
		whileExecuting = vkf;
		setViewer(findSourceViewer(editor));
		if (viewer != null) {
			setRedraw(viewer,false);
			if (whileExecuting != null
					&& viewer instanceof ITextViewerExtension) {
				((ITextViewerExtension) viewer)
						.prependVerifyKeyListener(whileExecuting);
			}
		}
		setEditor(editor); 
	} else if (!isExecuting() && viewer != null) {
		setRedraw(viewer,true);
		if (whileExecuting != null && viewer instanceof ITextViewerExtension){
			((ITextViewerExtension) viewer).removeVerifyKeyListener(whileExecuting);
			whileExecuting = null;
		}
		setEditor(null); 
	} 
}
 
Example #6
Source File: KbdMacroSupport.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
private void prependKeyListener(ITextEditor editor, VerifyKeyListener key) {
	if (key != null && editor != null) {
		if (viewer == findSourceViewer(editor)) {
			((ITextViewerExtension) viewer).prependVerifyKeyListener(key);
		}
	} 
}
 
Example #7
Source File: KbdMacroSupport.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
private void prependKeyListener(ISourceViewer newViewer, VerifyKeyListener key) {
	if (key != null && viewer != newViewer) {
		((ITextViewerExtension) viewer).removeVerifyKeyListener(key);
		((ITextViewerExtension) newViewer).prependVerifyKeyListener(key);
	}		
}
 
Example #8
Source File: PyPeerLinker.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates a handler that will properly treat backspaces considering python code.
 */
public static VerifyKeyListener createVerifyKeyListener(final TextViewer viewer) {
    return new VerifyKeyListener() {

        private final PyPeerLinker pyPeerLinker = new PyPeerLinker();

        @Override
        public void verifyKey(VerifyEvent event) {
            if (!event.doit) {
                return;
            }
            switch (event.character) {
                case '\'':
                case '\"':
                case '[':
                case '{':
                case '(':
                case ']':
                case '}':
                case ')':
                    break;
                default:
                    return;
            }
            if (viewer != null && viewer.isEditable()) {
                boolean blockSelection = false;
                try {
                    blockSelection = viewer.getTextWidget().getBlockSelection();
                } catch (Throwable e) {
                    //that's OK (only available in eclipse 3.5)
                }
                if (!blockSelection) {
                    if (viewer instanceof ITextViewerExtensionAutoEditions) {
                        ITextViewerExtensionAutoEditions autoEditions = (ITextViewerExtensionAutoEditions) viewer;
                        if (!autoEditions.getAutoEditionsEnabled()) {
                            return;
                        }
                    }

                    ISelection selection = viewer.getSelection();
                    if (selection instanceof ITextSelection) {
                        IAdaptable adaptable;
                        if (viewer instanceof IAdaptable) {
                            adaptable = (IAdaptable) viewer;
                        } else {
                            adaptable = new IAdaptable() {

                                @Override
                                public <T> T getAdapter(Class<T> adapter) {
                                    return null;
                                }
                            };
                        }

                        //Don't bother in getting the indent prefs from the editor: the default indent prefs are
                        //always global for the settings we want.
                        pyPeerLinker.setIndentPrefs(new DefaultIndentPrefs(adaptable));
                        ITextSelection textSelection = (ITextSelection) selection;
                        PySelection ps = PySelectionFromEditor.createPySelectionFromEditor(viewer, textSelection);

                        if (pyPeerLinker.perform(ps, event.character, viewer)) {
                            event.doit = false;
                        }
                    }
                }
            }
        }
    };
}
 
Example #9
Source File: FirstCharAction.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates a handler that will properly treat home considering python code (if it's still not defined
 * by the platform -- otherwise, just go with what the platform provides).
 */
public static VerifyKeyListener createVerifyKeyListener(final SourceViewer viewer, final IWorkbenchPartSite site,
        boolean forceCreation) {
    // This only needs to be done for eclipse 3.2 (where line start is not
    // defined).
    // Eclipse 3.3 onwards already defines the home key in the text editor.

    final boolean isDefined;
    if (site != null) {
        ICommandService commandService = (ICommandService) site.getService(ICommandService.class);
        Collection definedCommandIds = commandService.getDefinedCommandIds();
        isDefined = definedCommandIds.contains("org.eclipse.ui.edit.text.goto.lineStart");

    } else {
        isDefined = false;
    }

    if (forceCreation || !isDefined) {
        return new VerifyKeyListener() {

            @Override
            public void verifyKey(VerifyEvent event) {
                if (event.doit) {
                    boolean isHome;
                    if (isDefined) {
                        isHome = KeyBindingHelper.matchesKeybinding(event.keyCode, event.stateMask,
                                "org.eclipse.ui.edit.text.goto.lineStart");
                    } else {
                        isHome = event.keyCode == SWT.HOME && event.stateMask == 0;
                    }
                    if (isHome) {
                        ISelection selection = viewer.getSelection();
                        if (selection instanceof ITextSelection) {
                            FirstCharAction firstCharAction = new FirstCharAction();
                            firstCharAction.viewer = viewer;
                            firstCharAction.perform(viewer.getDocument(), (ITextSelection) selection);
                            event.doit = false;
                        }
                    }
                }
            }
        };
    }
    return null;
}
 
Example #10
Source File: PyBackspace.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates a handler that will properly treat backspaces considering python code.
 */
public static VerifyKeyListener createVerifyKeyListener(final TextViewer viewer, final PyEdit edit) {
    return new VerifyKeyListener() {

        @Override
        public void verifyKey(VerifyEvent event) {
            if ((event.doit && event.character == SWT.BS && event.stateMask == 0 && viewer != null && viewer
                    .isEditable())) { //isBackspace
                boolean blockSelection = false;
                try {
                    blockSelection = viewer.getTextWidget().getBlockSelection();
                } catch (Throwable e) {
                    //that's OK (only available in eclipse 3.5)
                }
                if (!blockSelection) {
                    if (viewer instanceof ITextViewerExtensionAutoEditions) {
                        ITextViewerExtensionAutoEditions autoEditions = (ITextViewerExtensionAutoEditions) viewer;
                        if (!autoEditions.getAutoEditionsEnabled()) {
                            return;
                        }
                    }

                    ISelection selection = viewer.getSelection();
                    if (selection instanceof ITextSelection) {
                        //Only do our custom backspace if we're not in block selection mode.
                        PyBackspace pyBackspace = new PyBackspace();
                        if (edit != null) {
                            pyBackspace.setEditor(edit);
                        } else {
                            IAdaptable adaptable;
                            if (viewer instanceof IAdaptable) {
                                adaptable = (IAdaptable) viewer;
                            } else {
                                adaptable = new IAdaptable() {

                                    @Override
                                    public <T> T getAdapter(Class<T> adapter) {
                                        return null;
                                    }
                                };
                            }
                            pyBackspace.setIndentPrefs(new DefaultIndentPrefs(adaptable));
                        }
                        PySelection ps = PySelectionFromEditor.createPySelectionFromEditor(viewer,
                                (ITextSelection) selection);
                        pyBackspace.perform(ps);
                        event.doit = false;
                    }
                }
            }
        }
    };
}
 
Example #11
Source File: ScriptConsoleViewerWrapper.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public void appendVerifyKeyListener(VerifyKeyListener listener) {
    viewer.appendVerifyKeyListener(listener);
}
 
Example #12
Source File: ScriptConsoleViewerWrapper.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public void prependVerifyKeyListener(VerifyKeyListener listener) {
    viewer.prependVerifyKeyListener(listener);
}
 
Example #13
Source File: ScriptConsoleViewerWrapper.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public void removeVerifyKeyListener(VerifyKeyListener listener) {
    viewer.removeVerifyKeyListener(listener);
}