Search in sources :

Example 1 with IColorManager

use of org.erlide.ui.util.IColorManager in project erlide_eclipse by erlang.

the class ErlangSourceViewer method createErlangPreviewer.

public static SourceViewer createErlangPreviewer(final Composite parent, final IColorManager colorManager0, final IPreferenceStore topStore, final Map<TokenHighlight, HighlightStyle> colors0, final String content) {
    // TODO we should move this method, to a utility class (or maybe create
    // an ErlangPreviewSourceViewer class)
    final IColorManager colorManager = colorManager0 != null ? colorManager0 : new ColorManager();
    Map<TokenHighlight, HighlightStyle> colors;
    if (colors0 == null) {
        colors = Maps.newHashMap();
        for (final TokenHighlight th : TokenHighlight.values()) {
            colors.put(th, null);
        }
    } else {
        colors = colors0;
    }
    final IPreferenceStore generalTextStore = EditorsUI.getPreferenceStore();
    final IPreferenceStore store = topStore == null ? new ChainedPreferenceStore(new IPreferenceStore[] { generalTextStore }) : new ChainedPreferenceStore(new IPreferenceStore[] { topStore, generalTextStore });
    final SourceViewer viewer = new SourceViewer(parent, null, null, false, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
    final IDocument document = new Document(content);
    viewer.setDocument(document);
    final ErlangDocumentSetupParticipant setupParticipant = new ErlangDocumentSetupParticipant();
    setupParticipant.setup(document);
    final ErlangSourceViewerConfiguration configuration = new SyntaxColorPreviewEditorConfiguration(store, colorManager);
    viewer.configure(configuration);
    final Font font = JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT);
    viewer.getTextWidget().setFont(font);
    new ErlangSourceViewerUpdater(viewer, configuration, store);
    viewer.setEditable(false);
    final Cursor arrowCursor = viewer.getTextWidget().getDisplay().getSystemCursor(SWT.CURSOR_ARROW);
    viewer.getTextWidget().setCursor(arrowCursor);
    return viewer;
}
Also used : SourceViewer(org.eclipse.jface.text.source.SourceViewer) IColorManager(org.erlide.ui.util.IColorManager) ChainedPreferenceStore(org.eclipse.ui.texteditor.ChainedPreferenceStore) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) Cursor(org.eclipse.swt.graphics.Cursor) TokenHighlight(org.erlide.ui.prefs.TokenHighlight) Font(org.eclipse.swt.graphics.Font) HighlightStyle(org.erlide.ui.prefs.HighlightStyle) ErlangSourceViewerUpdater(org.erlide.ui.prefs.plugin.internal.ErlangSourceViewerUpdater) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) IColorManager(org.erlide.ui.util.IColorManager) ColorManager(org.erlide.ui.util.ColorManager) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with IColorManager

use of org.erlide.ui.util.IColorManager in project erlide_eclipse by erlang.

the class ErlangConsolePage method createControl.

/**
 * @wbp.parser.entryPoint
 */
@Override
public void createControl(final Composite parent) {
    setBackgroundColors();
    composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));
    final SashForm sashForm = new SashForm(composite, SWT.VERTICAL);
    sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    consoleOutputViewer = new SourceViewer(sashForm, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.READ_ONLY);
    consoleOutputText = consoleOutputViewer.getTextWidget();
    consoleOutputText.setFont(JFaceResources.getTextFont());
    bgcolor = DebugUIPlugin.getPreferenceColor(IDebugPreferenceConstants.CONSOLE_BAKGROUND_COLOR);
    consoleOutputText.setBackground(bgcolor);
    DebugUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {

        @Override
        public void propertyChange(final PropertyChangeEvent event) {
            if (event.getProperty().equals(IDebugPreferenceConstants.CONSOLE_BAKGROUND_COLOR)) {
                final Color color = DebugUIPlugin.getPreferenceColor(IDebugPreferenceConstants.CONSOLE_BAKGROUND_COLOR);
                consoleOutputText.setBackground(color);
                consoleInputText.setBackground(color);
            }
        }
    });
    consoleOutputText.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(final KeyEvent e) {
            if (e.stateMask == 0 && e.character != '\0') {
                consoleInputText.setFocus();
                consoleInputText.append("" + e.character);
                consoleInputText.setCaretOffset(consoleInputText.getText().length());
            }
            e.doit = true;
        }
    });
    final IPreferenceStore store = ErlideUIPlugin.getDefault().getPreferenceStore();
    final IColorManager colorManager = new ColorManager();
    consoleOutputViewer.setDocument(fDoc);
    consoleOutputViewer.configure(new ErlangConsoleSourceViewerConfiguration(store, colorManager, backend));
    consoleInputViewer = new SourceViewer(sashForm, null, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
    consoleInputText = consoleInputViewer.getTextWidget();
    consoleInputViewer.setDocument(new Document());
    consoleInputViewer.configure(new ErlangConsoleSourceViewerConfiguration(store, colorManager, backend));
    sashForm.setWeights(new int[] { 2, 1 });
    final Label helpLabel = new Label(composite, SWT.NONE);
    helpLabel.setText("To send the input to the console: press Enter at the end of an expression." + "Ctrl/Cmd-arrows navigate the input history.");
    helpLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    final ModifyListener modifyListener = new ModifyListener() {

        @Override
        public void modifyText(final ModifyEvent e) {
            final String consoleText = ErlangConsolePage.trimInput(consoleInputText.getText());
            final boolean atEndOfInput = consoleText.endsWith(".") && consoleInputText.getCaretOffset() >= consoleText.length();
            if (atEndOfInput) {
                final boolean inputComplete = isInputComplete();
                if (inputComplete) {
                    consoleInputText.setBackground(bgColor_Ok);
                }
            } else {
                consoleInputText.setBackground(bgcolor);
            }
        }
    };
    // consoleInput.addModifyListener(modifyListener);
    consoleInputText.addCaretListener(new CaretListener() {

        @Override
        public void caretMoved(final CaretEvent event) {
            modifyListener.modifyText(null);
        }
    });
    consoleInputText.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(final KeyEvent e) {
            final boolean ctrlOrCommandPressed = (e.stateMask & SWT.MOD1) == SWT.MOD1;
            final String conText = ErlangConsolePage.trimInput(consoleInputText.getText());
            final boolean atEndOfInput = consoleInputText.getCaretOffset() >= conText.length() && conText.endsWith(".");
            e.doit = true;
            if (e.keyCode == 13 && (ctrlOrCommandPressed || atEndOfInput)) {
                final boolean inputComplete = isInputComplete();
                if (inputComplete) {
                    sendInput();
                }
            } else if (ctrlOrCommandPressed && e.keyCode == SWT.SPACE) {
                consoleInputViewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
            } else if (ctrlOrCommandPressed && e.keyCode == SWT.ARROW_UP) {
                e.doit = false;
                history.prev();
                final String s = history.get();
                if (s != null) {
                    consoleInputText.setText(s);
                    consoleInputText.setSelection(consoleInputText.getText().length());
                }
            } else if (ctrlOrCommandPressed && e.keyCode == SWT.ARROW_DOWN) {
                e.doit = false;
                history.next();
                final String s = history.get();
                if (s != null) {
                    consoleInputText.setText(s);
                    consoleInputText.setSelection(consoleInputText.getText().length());
                }
            }
        }
    });
    consoleInputText.setFont(consoleOutputText.getFont());
    consoleInputText.setBackground(consoleOutputText.getBackground());
    consoleInputText.setWordWrap(true);
    consoleInputText.setFocus();
    // end layout
    final IDocumentListener documentListener = new IDocumentListener() {

        @Override
        public void documentAboutToBeChanged(final DocumentEvent event) {
        }

        @Override
        public void documentChanged(final DocumentEvent event) {
            final int end = consoleOutputViewer.getDocument().getLength();
            consoleOutputViewer.setSelectedRange(end, end);
            consoleOutputViewer.revealRange(end, 0);
        }
    };
    addDocumentListener(documentListener);
    // $NON-NLS-1$
    final String id = "#ContextMenu";
    // if (getConsole().getType() != null) {
    // id = getConsole().getType() + "." + id; //$NON-NLS-1$
    // }
    // $NON-NLS-1$
    fMenuManager = new MenuManager("#ContextMenu", id);
    fMenuManager.setRemoveAllWhenShown(true);
    fMenuManager.addMenuListener(new IMenuListener() {

        @Override
        public void menuAboutToShow(final IMenuManager m) {
            contextMenuAboutToShow(m);
        }
    });
    final Menu menu = fMenuManager.createContextMenu(getControl());
    getControl().setMenu(menu);
    createActions();
    configureToolBar(getSite().getActionBars().getToolBarManager());
    getSite().registerContextMenu(id, fMenuManager, consoleOutputViewer);
    getSite().setSelectionProvider(consoleOutputViewer);
    consoleOutputViewer.getSelectionProvider().addSelectionChangedListener(selectionChangedListener);
}
Also used : CaretEvent(org.eclipse.swt.custom.CaretEvent) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) SourceViewer(org.eclipse.jface.text.source.SourceViewer) ModifyListener(org.eclipse.swt.events.ModifyListener) KeyAdapter(org.eclipse.swt.events.KeyAdapter) IColorManager(org.erlide.ui.util.IColorManager) Label(org.eclipse.swt.widgets.Label) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) ModifyEvent(org.eclipse.swt.events.ModifyEvent) Menu(org.eclipse.swt.widgets.Menu) IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) Composite(org.eclipse.swt.widgets.Composite) Color(org.eclipse.swt.graphics.Color) IDocumentListener(org.eclipse.jface.text.IDocumentListener) DocumentEvent(org.eclipse.jface.text.DocumentEvent) IMenuListener(org.eclipse.jface.action.IMenuListener) SashForm(org.eclipse.swt.custom.SashForm) CaretListener(org.eclipse.swt.custom.CaretListener) GridData(org.eclipse.swt.layout.GridData) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) IColorManager(org.erlide.ui.util.IColorManager) ColorManager(org.erlide.ui.util.ColorManager)

Aggregations

IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)2 Document (org.eclipse.jface.text.Document)2 IDocument (org.eclipse.jface.text.IDocument)2 SourceViewer (org.eclipse.jface.text.source.SourceViewer)2 ColorManager (org.erlide.ui.util.ColorManager)2 IColorManager (org.erlide.ui.util.IColorManager)2 IMenuListener (org.eclipse.jface.action.IMenuListener)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1 MenuManager (org.eclipse.jface.action.MenuManager)1 DocumentEvent (org.eclipse.jface.text.DocumentEvent)1 IDocumentListener (org.eclipse.jface.text.IDocumentListener)1 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)1 IPropertyChangeListener (org.eclipse.jface.util.IPropertyChangeListener)1 PropertyChangeEvent (org.eclipse.jface.util.PropertyChangeEvent)1 CaretEvent (org.eclipse.swt.custom.CaretEvent)1 CaretListener (org.eclipse.swt.custom.CaretListener)1 SashForm (org.eclipse.swt.custom.SashForm)1 KeyAdapter (org.eclipse.swt.events.KeyAdapter)1 KeyEvent (org.eclipse.swt.events.KeyEvent)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1