Search in sources :

Example 1 with IFocusService

use of org.eclipse.ui.swt.IFocusService in project mylyn.docs by eclipse.

the class MarkupEditor method createSourceViewer.

@Override
protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
    sourceViewerConfiguration.initializeDefaultFonts();
    tabFolder = new CTabFolder(parent, SWT.BOTTOM);
    {
        sourceTab = new CTabItem(tabFolder, SWT.NONE);
        updateSourceTabLabel();
        viewer = new MarkupProjectionViewer(tabFolder, ruler, getOverviewRuler(), isOverviewRulerVisible(), styles | SWT.WRAP);
        sourceTab.setControl(((Viewer) viewer).getControl());
        tabFolder.setSelection(sourceTab);
    }
    try {
        previewTab = new CTabItem(tabFolder, SWT.NONE);
        previewTab.setText(Messages.MarkupEditor_preview);
        previewTab.setToolTipText(Messages.MarkupEditor_preview_tooltip);
        browser = new Browser(tabFolder, SWT.NONE);
        // bug 260479: open hyperlinks in a browser
        browser.addLocationListener(new LocationListener() {

            public void changed(LocationEvent event) {
                event.doit = false;
            }

            public void changing(LocationEvent event) {
                // if it looks like an absolute URL
                if (event.location.matches("([a-zA-Z]{3,8})://?.*")) {
                    // $NON-NLS-1$
                    // workaround for browser problem (bug 262043)
                    // $NON-NLS-1$
                    int idxOfSlashHash = event.location.indexOf("/#");
                    if (idxOfSlashHash != -1) {
                        // allow javascript-based scrolling to work
                        if (!event.location.startsWith("file:///#")) {
                            // $NON-NLS-1$
                            event.doit = false;
                        }
                        return;
                    }
                    // workaround end
                    event.doit = false;
                    try {
                        PlatformUI.getWorkbench().getBrowserSupport().createBrowser(// $NON-NLS-1$
                        "org.eclipse.ui.browser").openURL(new URL(event.location));
                    } catch (Exception e) {
                        new URLHyperlink(new Region(0, 1), event.location).open();
                    }
                }
            }
        });
        previewTab.setControl(browser);
    } catch (SWTError e) {
        // disable preview, the exception is probably due to the internal browser not being available
        if (previewTab != null) {
            previewTab.dispose();
            previewTab = null;
        }
        logPreviewTabUnavailable(e);
    }
    tabFolder.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent selectionevent) {
            widgetSelected(selectionevent);
        }

        public void widgetSelected(SelectionEvent selectionevent) {
            if (isShowingPreview()) {
                updatePreview();
            }
        }
    });
    viewer.getTextWidget().addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {
        }

        public void widgetSelected(SelectionEvent e) {
            updateOutlineSelection();
        }
    });
    viewer.getTextWidget().addKeyListener(new KeyAdapter() {

        @Override
        public void keyReleased(KeyEvent e) {
            if (isRelevantKeyCode(e.keyCode)) {
                updateOutlineSelection();
            }
        }

        private boolean isRelevantKeyCode(int keyCode) {
            // for some reason not all key presses result in a selection change
            switch(keyCode) {
                case SWT.ARROW_DOWN:
                case SWT.ARROW_LEFT:
                case SWT.ARROW_RIGHT:
                case SWT.ARROW_UP:
                case SWT.PAGE_DOWN:
                case SWT.PAGE_UP:
                    return true;
            }
            return false;
        }
    });
    viewer.getTextWidget().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseUp(MouseEvent e) {
            updateOutlineSelection();
        }
    });
    IFocusService focusService = PlatformUI.getWorkbench().getService(IFocusService.class);
    if (focusService != null) {
        focusService.addFocusTracker(viewer.getTextWidget(), MarkupEditor.EDITOR_SOURCE_VIEWER);
    }
    viewer.getTextWidget().setData(MarkupLanguage.class.getName(), getMarkupLanguage());
    viewer.getTextWidget().setData(ISourceViewer.class.getName(), viewer);
    getSourceViewerDecorationSupport(viewer);
    updateDocument();
    if (preferencesListener == null) {
        preferencesListener = new IPropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent event) {
                if (viewer.getTextWidget() == null || viewer.getTextWidget().isDisposed()) {
                    return;
                }
                if (isFontPreferenceChange(event)) {
                    viewer.getTextWidget().getDisplay().asyncExec(new Runnable() {

                        public void run() {
                            reloadPreferences();
                        }
                    });
                }
            }
        };
        WikiTextUiPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(preferencesListener);
    }
    return viewer;
}
Also used : CTabFolder(org.eclipse.swt.custom.CTabFolder) KeyAdapter(org.eclipse.swt.events.KeyAdapter) ProjectionViewer(org.eclipse.jface.text.source.projection.ProjectionViewer) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) Viewer(org.eclipse.jface.viewers.Viewer) IFocusService(org.eclipse.ui.swt.IFocusService) CTabItem(org.eclipse.swt.custom.CTabItem) URL(java.net.URL) KeyEvent(org.eclipse.swt.events.KeyEvent) URLHyperlink(org.eclipse.jface.text.hyperlink.URLHyperlink) LocationListener(org.eclipse.swt.browser.LocationListener) SelectionEvent(org.eclipse.swt.events.SelectionEvent) LocationEvent(org.eclipse.swt.browser.LocationEvent) SWTError(org.eclipse.swt.SWTError) IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) MouseEvent(org.eclipse.swt.events.MouseEvent) MouseAdapter(org.eclipse.swt.events.MouseAdapter) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) SWTException(org.eclipse.swt.SWTException) Point(org.eclipse.swt.graphics.Point) Region(org.eclipse.jface.text.Region) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) Browser(org.eclipse.swt.browser.Browser) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 2 with IFocusService

use of org.eclipse.ui.swt.IFocusService in project mylyn.docs by eclipse.

the class MarkupTaskEditorExtension method createEditor.

@SuppressWarnings("unchecked")
@Override
public SourceViewer createEditor(TaskRepository taskRepository, Composite parent, int style, IAdaptable context) {
    final MarkupLanguageType markupLanguageCopy = createRepositoryMarkupLanguage(taskRepository);
    configureMarkupLanguage(taskRepository, markupLanguageCopy);
    SourceViewer viewer = new MarkupSourceViewer(parent, null, style | SWT.WRAP, markupLanguageCopy);
    // configure the viewer
    MarkupSourceViewerConfiguration configuration = createSourceViewerConfiguration(taskRepository, viewer, context);
    configuration.setEnableSelfContainedIncrementalFind(true);
    configuration.setMarkupLanguage(markupLanguageCopy);
    configuration.setShowInTarget(new ShowInTargetBridge(viewer));
    viewer.configure(configuration);
    // we want the viewer to show annotations
    viewer.showAnnotations(true);
    DefaultMarkerAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess();
    MarkerAnnotationPreferences annotationPreferences = new MarkerAnnotationPreferences();
    // configure viewer annotation/decoration support
    final SourceViewerDecorationSupport support = new SourceViewerDecorationSupport(viewer, null, annotationAccess, EditorsUI.getSharedTextColors());
    // hook the support up to the preference store
    Iterator<AnnotationPreference> e = annotationPreferences.getAnnotationPreferences().iterator();
    while (e.hasNext()) {
        AnnotationPreference preference = e.next();
        support.setAnnotationPreference(preference);
    }
    support.setCursorLinePainterPreferenceKeys(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE_COLOR);
    support.setMarginPainterPreferenceKeys(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLOR, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN);
    support.install(new EditorExtensionPreferenceStore(EditorsUI.getPreferenceStore(), viewer.getControl()));
    viewer.getControl().addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent e) {
            support.dispose();
        }
    });
    IFocusService focusService = PlatformUI.getWorkbench().getService(IFocusService.class);
    if (focusService != null) {
        focusService.addFocusTracker(viewer.getTextWidget(), MarkupEditor.EDITOR_SOURCE_VIEWER);
    }
    viewer.getTextWidget().setData(MarkupLanguage.class.getName(), markupLanguageCopy);
    viewer.getTextWidget().setData(ISourceViewer.class.getName(), viewer);
    return viewer;
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) MarkupSourceViewer(org.eclipse.mylyn.wikitext.ui.editor.MarkupSourceViewer) SourceViewer(org.eclipse.jface.text.source.SourceViewer) MarkerAnnotationPreferences(org.eclipse.ui.texteditor.MarkerAnnotationPreferences) DisposeEvent(org.eclipse.swt.events.DisposeEvent) IFocusService(org.eclipse.ui.swt.IFocusService) SourceViewerDecorationSupport(org.eclipse.ui.texteditor.SourceViewerDecorationSupport) MarkupSourceViewer(org.eclipse.mylyn.wikitext.ui.editor.MarkupSourceViewer) DefaultMarkerAnnotationAccess(org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess) ShowInTargetBridge(org.eclipse.mylyn.wikitext.ui.editor.ShowInTargetBridge) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) AnnotationPreference(org.eclipse.ui.texteditor.AnnotationPreference) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) MarkupSourceViewerConfiguration(org.eclipse.mylyn.wikitext.ui.editor.MarkupSourceViewerConfiguration)

Example 3 with IFocusService

use of org.eclipse.ui.swt.IFocusService in project mylyn.docs by eclipse.

the class MarkupTaskEditorExtension method createViewer.

@Override
public SourceViewer createViewer(TaskRepository taskRepository, Composite parent, int style, IAdaptable context) {
    if (markupLanguage == null) {
        throw new IllegalStateException();
    }
    MarkupViewer markupViewer = new MarkupViewer(parent, null, style | SWT.FLAT | SWT.WRAP);
    MarkupLanguageType markupLanguageCopy = createRepositoryMarkupLanguage(taskRepository);
    configureMarkupLanguage(taskRepository, markupLanguageCopy);
    markupViewer.setMarkupLanguage(markupLanguageCopy);
    MarkupViewerConfiguration configuration = createViewerConfiguration(taskRepository, markupViewer, context);
    configuration.setDisableHyperlinkModifiers(true);
    configuration.setEnableSelfContainedIncrementalFind(true);
    if (markupLanguageCopy instanceof AbstractMarkupLanguage && ((AbstractMarkupLanguage) markupLanguageCopy).isDetectingRawHyperlinks()) {
        // bug 264612 don't detect hyperlinks twice
        configuration.addHyperlinkDetectorDescriptorFilter(new DefaultHyperlinkDetectorDescriptorFilter(// $NON-NLS-1$
        "org.eclipse.mylyn.tasks.ui.hyperlinks.detectors.url"));
    }
    markupViewer.configure(configuration);
    markupViewer.setEditable(false);
    markupViewer.getTextWidget().setCaret(null);
    if (JFaceResources.getFontRegistry().hasValueFor(WikiTextTasksUiPlugin.FONT_REGISTRY_KEY_DEFAULT_FONT)) {
        markupViewer.getTextWidget().setFont(JFaceResources.getFontRegistry().get(WikiTextTasksUiPlugin.FONT_REGISTRY_KEY_DEFAULT_FONT));
    }
    if (JFaceResources.getFontRegistry().hasValueFor(WikiTextTasksUiPlugin.FONT_REGISTRY_KEY_MONOSPACE_FONT)) {
        markupViewer.setDefaultMonospaceFont(JFaceResources.getFontRegistry().get(WikiTextTasksUiPlugin.FONT_REGISTRY_KEY_MONOSPACE_FONT));
    }
    markupViewer.setStylesheet(WikiTextUiPlugin.getDefault().getPreferences().getStylesheet());
    IFocusService focusService = PlatformUI.getWorkbench().getService(IFocusService.class);
    if (focusService != null) {
        focusService.addFocusTracker(markupViewer.getTextWidget(), MARKUP_VIEWER);
    }
    markupViewer.getTextWidget().setData(ISourceViewer.class.getName(), markupViewer);
    return markupViewer;
}
Also used : MarkupViewerConfiguration(org.eclipse.mylyn.wikitext.ui.viewer.MarkupViewerConfiguration) DefaultHyperlinkDetectorDescriptorFilter(org.eclipse.mylyn.wikitext.ui.viewer.DefaultHyperlinkDetectorDescriptorFilter) MarkupViewer(org.eclipse.mylyn.wikitext.ui.viewer.MarkupViewer) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) IFocusService(org.eclipse.ui.swt.IFocusService) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer)

Example 4 with IFocusService

use of org.eclipse.ui.swt.IFocusService in project mylyn.docs by eclipse.

the class WikiTextSourceEditor method createSourceViewer.

@Override
protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
    if (getMarkupLanguage() == null) {
        // $NON-NLS-1$
        setMarkupLanguage(ServiceLocator.getInstance().getMarkupLanguage("Textile"));
    }
    viewer = createMarkupSourceViewer(parent, ruler, styles);
    IFocusService focusService = PlatformUI.getWorkbench().getService(IFocusService.class);
    if (focusService != null) {
        focusService.addFocusTracker(viewer.getTextWidget(), EDITOR_SOURCE_VIEWER);
    }
    viewer.getTextWidget().setData(MarkupLanguage.class.getName(), getMarkupLanguage());
    viewer.getTextWidget().setData(ISourceViewer.class.getName(), viewer);
    viewer.getTextWidget().addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            detectOutlineLocationChanged();
        }
    });
    viewer.getTextWidget().addKeyListener(new KeyAdapter() {

        @Override
        public void keyReleased(KeyEvent e) {
            if (isRelevantKeyCode(e.keyCode)) {
                detectOutlineLocationChanged();
            }
        }

        private boolean isRelevantKeyCode(int keyCode) {
            // for some reason not all key presses result in a selection change
            switch(keyCode) {
                case SWT.ARROW_DOWN:
                case SWT.ARROW_LEFT:
                case SWT.ARROW_RIGHT:
                case SWT.ARROW_UP:
                case SWT.PAGE_DOWN:
                case SWT.PAGE_UP:
                    return true;
            }
            return false;
        }
    });
    viewer.getTextWidget().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseUp(MouseEvent e) {
            detectOutlineLocationChanged();
        }
    });
    getSourceViewerDecorationSupport(viewer);
    updateDocument();
    return viewer;
}
Also used : KeyEvent(org.eclipse.swt.events.KeyEvent) MouseEvent(org.eclipse.swt.events.MouseEvent) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) KeyAdapter(org.eclipse.swt.events.KeyAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MouseAdapter(org.eclipse.swt.events.MouseAdapter) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) IFocusService(org.eclipse.ui.swt.IFocusService) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) Point(org.eclipse.swt.graphics.Point)

Example 5 with IFocusService

use of org.eclipse.ui.swt.IFocusService in project dbeaver by dbeaver.

the class UIUtils method addFocusTracker.

public static void addFocusTracker(IServiceLocator serviceLocator, String controlID, Control control) {
    IFocusService focusService = serviceLocator.getService(IFocusService.class);
    if (focusService == null) {
        focusService = UIUtils.getActiveWorkbenchWindow().getService(IFocusService.class);
    }
    if (focusService != null) {
        IFocusService finalFocusService = focusService;
        finalFocusService.addFocusTracker(control, controlID);
        control.addDisposeListener(e -> {
            // Unregister from focus service
            finalFocusService.removeFocusTracker(control);
        });
    } else {
        log.debug("Focus service not found in " + serviceLocator);
    }
}
Also used : IFocusService(org.eclipse.ui.swt.IFocusService)

Aggregations

IFocusService (org.eclipse.ui.swt.IFocusService)8 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)5 AbstractMarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage)4 MarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)3 SelectionEvent (org.eclipse.swt.events.SelectionEvent)3 ProjectionViewer (org.eclipse.jface.text.source.projection.ProjectionViewer)2 DisposeEvent (org.eclipse.swt.events.DisposeEvent)2 DisposeListener (org.eclipse.swt.events.DisposeListener)2 KeyAdapter (org.eclipse.swt.events.KeyAdapter)2 KeyEvent (org.eclipse.swt.events.KeyEvent)2 MouseAdapter (org.eclipse.swt.events.MouseAdapter)2 MouseEvent (org.eclipse.swt.events.MouseEvent)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 Point (org.eclipse.swt.graphics.Point)2 URL (java.net.URL)1 YEditSourceViewerConfiguration (org.dadacoalition.yedit.editor.YEditSourceViewerConfiguration)1 CoreException (org.eclipse.core.runtime.CoreException)1 Region (org.eclipse.jface.text.Region)1 IContentAssistant (org.eclipse.jface.text.contentassist.IContentAssistant)1 URLHyperlink (org.eclipse.jface.text.hyperlink.URLHyperlink)1