/** * set text * * @param lines lines * @param widgetLineNumbers number text widget * @param widgetText text widget * @param widgetHorizontalScrollBar horizontal scrollbar widget * @param widgetVerticalScrollBar horizontal scrollbar widget */ private void setText( String[] lines, StyledText widgetLineNumbers, StyledText widgetText, ScrollBar widgetHorizontalScrollBar, ScrollBar widgetVerticalScrollBar) { if (!widgetLineNumbers.isDisposed() && !widgetText.isDisposed() && !widgetVerticalScrollBar.isDisposed() && !widgetHorizontalScrollBar.isDisposed()) { StringBuilder lineNumbers = new StringBuilder(); StringBuilder text = new StringBuilder(); int lineNb = 1; int maxWidth = 0; // get text for (String line : lines) { lineNumbers.append(String.format("%d\n", lineNb)); lineNb++; text.append(line); text.append('\n'); maxWidth = Math.max(maxWidth, line.length()); } // set text widgetLineNumbers.setText(lineNumbers.toString()); widgetText.setText(text.toString()); widgetText.setCaretOffset(0); // set scrollbars widgetHorizontalScrollBar.setMinimum(0); widgetHorizontalScrollBar.setMaximum(maxWidth); widgetVerticalScrollBar.setMinimum(0); widgetVerticalScrollBar.setMaximum(lineNb - 1); // force redraw (Note: for some reason this is necessary to keep texts and scrollbars in sync) widgetLineNumbers.redraw(); widgetText.redraw(); widgetLineNumbers.update(); widgetText.update(); // show top widgetLineNumbers.setTopIndex(0); widgetLineNumbers.setSelection(0); widgetText.setTopIndex(0); widgetText.setCaretOffset(0); widgetVerticalScrollBar.setSelection(0); } }
public void applyFont(FontType type, FontData data) { Font newFont = new Font(SwtUtils.DISPLAY, data); Font oldFont = null; switch (type) { case GLOBAL: { oldFont = iniAppearance.getFontGlobal(); for (Control control : controls) { if (control.isDisposed()) continue; control.setFont(newFont); } for (Label label : labels) { if (label.isDisposed()) continue; label.setFont(newFont); } for (Button button : buttons) { if (button.isDisposed()) continue; button.setFont(newFont); } iniAppearance.setFontGlobal(newFont); break; } case LOG: { oldFont = iniAppearance.getFontLog(); for (StyledText control : logControls) { if (control.isDisposed()) continue; control.setFont(newFont); } iniAppearance.setFontLog(newFont); break; } case CHAT: { oldFont = iniAppearance.getFontChat(); for (Text control : chatControls) { if (control.isDisposed()) continue; control.setFont(newFont); } iniAppearance.setFontChat(newFont); break; } } if (oldFont != null) oldFont.dispose(); }
/** * Eclipse hack. Disables/enabled all key bindings in specified site's part. Works only if host * editor is extender of AbstractTextEditor Uses reflection because setActionActivation is private * method TODO: find better way to disable key bindings or prioritize event handling to widgets * * @param partSite workbench part site * @param enable enable or disable */ @Deprecated public static void enableHostEditorKeyBindings(IWorkbenchPartSite partSite, boolean enable) { IWorkbenchPart part = partSite.getPart(); if (part instanceof AbstractTextEditor) { AbstractTextEditor hostEditor = (AbstractTextEditor) part; if (hostEditor instanceof BaseTextEditor) { StyledText textWidget = ((BaseTextEditor) hostEditor).getTextViewer().getTextWidget(); if (textWidget == null || textWidget.isDisposed()) { return; } } try { Method activatorMethod = AbstractTextEditor.class.getDeclaredMethod("setActionActivation", Boolean.TYPE); activatorMethod.setAccessible(true); activatorMethod.invoke(hostEditor, enable); } catch (Throwable e) { if (e instanceof InvocationTargetException) { e = ((InvocationTargetException) e).getTargetException(); } log.warn("Can't disable text editor action activations", e); } // hostEditor.getEditorSite().getActionBarContributor().setActiveEditor(hostEditor); } }
/** * Creates a style range for the text viewer. * * @param viewer the text viewer * @return the new style range for the text viewer or <code>null</code> */ private StyleRange createStyleRange(ITextViewer viewer) { StyledText text = viewer.getTextWidget(); if (text == null || text.isDisposed()) { return null; } int widgetCaret = text.getCaretOffset(); int modelCaret = 0; if (viewer instanceof ITextViewerExtension5) { ITextViewerExtension5 extension = (ITextViewerExtension5) viewer; modelCaret = extension.widgetOffset2ModelOffset(widgetCaret); } else { IRegion visibleRegion = viewer.getVisibleRegion(); modelCaret = widgetCaret + visibleRegion.getOffset(); } if (modelCaret >= getReplacementOffset() + getReplacementLength()) { return null; } int length = getReplacementOffset() + getReplacementLength() - modelCaret; Color foreground = getForegroundColor(); Color background = getBackgroundColor(); return new StyleRange(modelCaret, length, foreground, background); }
private void updateSizeAndLocations() { if (busyLabel == null || busyLabel.isDisposed()) { return; } Point leftToolBarSize = new Point(0, 0); if (leftToolBar != null && !leftToolBar.isDisposed()) { // bottom align tool bar in title region leftToolBarSize = leftToolBar.getSize(); int y = leftToolBar.getParent().getSize().y - leftToolBarSize.y - 2; if (!hasLeftToolBar()) { // hide tool bar to avoid overlaying busyLabel on windows leftToolBarSize.x = 0; } leftToolBar.setBounds(busyLabel.getLocation().x, y, leftToolBarSize.x, leftToolBarSize.y); } if (titleLabel != null && !titleLabel.isDisposed()) { // center align title text in title region Point size = titleLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); int y = (titleLabel.getParent().getSize().y - size.y) / 2; titleLabel.setBounds( busyLabel.getLocation().x + LEFT_TOOLBAR_HEADER_TOOLBAR_PADDING + leftToolBarSize.x, y, size.x, size.y); } }
/** * search next text * * @param widgetText text widget * @param widgetFind search text widget */ private void findNext(StyledText widgetText, Text widgetFind) { if (!widgetText.isDisposed()) { String findText = widgetFind.getText().toLowerCase(); if (!findText.isEmpty()) { // get cursor position int cursorIndex = widgetText.getCaretOffset(); // search int offset = -1; if (cursorIndex >= 0) { String text = widgetText.getText(); offset = (cursorIndex + 1 < text.length()) ? text.substring(cursorIndex + 1).toLowerCase().indexOf(findText) : -1; } if (offset >= 0) { int index = cursorIndex + 1 + offset; widgetText.setCaretOffset(index); widgetText.setSelection(index); widgetText.redraw(); int topIndex = widgetText.getTopIndex(); widgetLineNumbers.setTopIndex(topIndex); widgetVerticalScrollBar.setSelection(topIndex); } else { Widgets.flash(widgetFind); } } } }
public void resetLineBackground() { StyledText t = getSourceViewer().getTextWidget(); if (t != null && !t.isDisposed()) { int lines = getLineCount(); t.setLineBackground(0, lines, null); } }
/* * (non-Javadoc) * * @see org.eclipse.jface.preference.FieldEditorPreferencePage#performDefaults() */ @Override protected void performDefaults() { super.performDefaults(); if (scriptTxt != null && !scriptTxt.isDisposed()) { isDefaultPresentedForScriptTxt = true; scriptTxt.setText(getPreferenceStore().getDefaultString(getPreferenceKey())); } }
public void applyColor(ColorType type, RGB rgb) { Color newColor = new Color(SwtUtils.DISPLAY, rgb); Color oldColor = null; switch (type) { case BACKGROUND: { oldColor = iniAppearance.getColorBackground(); for (Control control : controls) { if (control.isDisposed()) continue; control.setBackground(newColor); } for (Text chat : chatControls) { if (chat.isDisposed()) continue; chat.setBackground(newColor); } iniAppearance.setColorBackground(newColor); break; } case FOREGROUND: { oldColor = iniAppearance.getColorForeground(); for (Control control : controls) { if (control.isDisposed()) continue; control.setForeground(newColor); } for (Text chat : chatControls) { if (chat.isDisposed()) continue; chat.setForeground(newColor); } iniAppearance.setColorForeground(newColor); break; } case LOG_BACKGROUND: { oldColor = iniAppearance.getColorLogBackground(); for (StyledText text : logControls) { if (text.isDisposed()) continue; text.setBackground(newColor); text.setForeground(newColor); } iniAppearance.setColorLogBackground(newColor); } } if (oldColor != null) oldColor.dispose(); }
public void install() { ISourceViewer sourceViewer = editor.getISourceViewer(); if (sourceViewer == null) return; StyledText text = sourceViewer.getTextWidget(); if (text == null || text.isDisposed()) return; sourceViewer.addTextInputListener(this); IDocument document = sourceViewer.getDocument(); if (document != null) document.addDocumentListener(this); }
private void drawCurrentLine(LineBackgroundEvent event, final IRegion lineRegion) { final StyledText textWidget = fViewer.getTextWidget(); final int offset = event.lineOffset; final RGBa lineHighlight = getCurrentTheme().getLineHighlight(); event.lineBackground = getColorManager().getColor(lineHighlight.toRGB()); // In this case, we should be overriding the bg of the style ranges for the line too! if (textWidget.isDisposed()) { return; } // FIXME Only change bg colors of visible ranges! int replaceLength = 160; if (lineRegion != null) { replaceLength = Math.min(replaceLength, lineRegion.getLength()); } // be safe about offsets int charCount = textWidget.getCharCount(); if (offset + replaceLength > charCount) { replaceLength = charCount - offset; if (replaceLength < 0) { // Just playing safe here replaceLength = 0; } } final StyleRange[] ranges = textWidget.getStyleRanges(offset, replaceLength, true); if (ranges == null || ranges.length == 0) { return; } Color background = textWidget.getBackground(); final int[] positions = new int[ranges.length << 1]; int x = 0; boolean apply = false; for (StyleRange range : ranges) { if (range.background != null) { if (!range.background.equals(background)) { positions[x] = range.start; positions[x + 1] = range.length; x += 2; continue; } apply = true; } range.background = null; positions[x] = range.start; positions[x + 1] = range.length; x += 2; } if (apply) { textWidget.setStyleRanges(offset, replaceLength, positions, ranges); } }
@Inject public void setTerminalForegroundColor( Display display, @Preference( nodePath = IConstant.PREFERENCE_NODE, value = IPreferenceKey.COLOR_TERMINAL_FOREGROUND) String rgbText) { terminalForegroundColor = new Color(display, StringConverter.asRGB(rgbText)); if (terminalText != null && !terminalText.isDisposed()) terminalText.setForeground(terminalForegroundColor); }
/* * (non-Javadoc) * * @see org.eclipse.jface.preference.FieldEditorPreferencePage#performOk() */ @Override public boolean performOk() { boolean ok = super.performOk(); if (scriptTxt != null && !scriptTxt.isDisposed()) { if (isDefaultPresentedForScriptTxt) { getPreferenceStore().setToDefault(getPreferenceKey()); } else { getPreferenceStore().setValue(getPreferenceKey(), scriptTxt.getText()); } isDefaultPresentedForScriptTxt = false; } return ok; }
@Inject public void setTerminalFontData( @Preference(nodePath = IConstant.PREFERENCE_NODE, value = IPreferenceKey.TERMINAL_FONT_DATA) String fontDataString) { LOG.debug("setFontData: fontDataString=" + fontDataString); terminalFont = new Font(display, new FontData(fontDataString)); if (terminalText != null && !terminalText.isDisposed()) { terminalText.setFont(terminalFont); LOG.info("setFontData: fontdata=" + fontDataString); } }
public static RenameLinkedMode getActiveLinkedMode() { if (fgActiveLinkedMode != null) { ISourceViewer viewer = fgActiveLinkedMode.fEditor.getViewer(); if (viewer != null) { StyledText textWidget = viewer.getTextWidget(); if (textWidget != null && !textWidget.isDisposed()) { return fgActiveLinkedMode; } } // make sure we don't hold onto the active linked mode if anything went wrong with canceling: fgActiveLinkedMode = null; } return null; }
public void setLineBackground(Position position, Color c) { StyledText t = getSourceViewer().getTextWidget(); if (t != null && !t.isDisposed()) { Point region = new Point(0, 0); getLineRange(position, region); region.x -= getDocumentRegionOffset(); try { t.setLineBackground(region.x, region.y, c); } catch (IllegalArgumentException ex) { // silently ignored } } }
protected void updateUISize() { // This function can be run asynchronously, check that UI is still usable. if (commandLineText == null || commandLineText.isDisposed()) { VrapperLog.error("Command line UI is already disposed or nulled."); return; } Point preferredSize = commandLineText.computeSize(width, SWT.DEFAULT, true); int selHeight = Math.min(preferredSize.y, maxHeight); commandLineText.setSize(width, selHeight); if (bottom > 0) { // Move the command line higher up to show all of it. commandLineText.setLocation(0, bottom - selHeight); } commandLineText.redraw(); }
@Inject void trackFontSize( @Preference( nodePath = RelationsConstants.PREFERENCE_NODE, value = RelationsConstants.KEY_TEXT_FONT_SIZE) final int inFontSize) { if (textWidget == null || textWidget.isDisposed()) { fontSizeToUse = inFontSize; } else { final FontData lData = textWidget.getFont().getFontData()[0]; if (inFontSize != lData.getHeight()) { lData.setHeight(inFontSize); final Font lNewFont = new Font(Display.getCurrent(), lData); textWidget.setFont(lNewFont); } } }
protected void setLastRulerMouseLocation(ISourceViewer viewer, int line) { // set last mouse activity in order to get the correct context menu if (fCompositeRuler != null) { StyledText st = viewer.getTextWidget(); if (st != null && !st.isDisposed()) { if (viewer instanceof ITextViewerExtension5) { int widgetLine = ((ITextViewerExtension5) viewer).modelLine2WidgetLine(line); Point loc = st.getLocationAtOffset(st.getOffsetAtLine(widgetLine)); fCompositeRuler.setLocationOfLastMouseButtonActivity(0, loc.y); } else if (viewer instanceof TextViewer) { // TODO remove once TextViewer implements the extension int widgetLine = ((TextViewer) viewer).modelLine2WidgetLine(line); Point loc = st.getLocationAtOffset(st.getOffsetAtLine(widgetLine)); fCompositeRuler.setLocationOfLastMouseButtonActivity(0, loc.y); } } } }
public void install() { final ISourceViewer sourceViewer = erlangEditor.getViewer(); if (sourceViewer == null) { return; } final StyledText text = sourceViewer.getTextWidget(); if (text == null || text.isDisposed()) { return; } sourceViewer.addTextInputListener(this); final IDocument document = sourceViewer.getDocument(); if (document != null) { document.addDocumentListener(this); } }
/** * Called when Ctrl is selected during the completions * * @see * org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, * boolean) */ public void selected(ITextViewer viewer, boolean smartToggle) { if (smartToggle) { StyledText text = viewer.getTextWidget(); if (text == null || text.isDisposed()) { return; } int widgetCaret = text.getCaretOffset(); IDocument document = viewer.getDocument(); int finalOffset = widgetCaret; try { if (finalOffset >= document.getLength()) { unselected(viewer); return; } char c; do { c = document.getChar(finalOffset); finalOffset++; } while (isValidChar(c) && finalOffset < document.getLength()); if (c == '(') { fLastIsPar = true; } else { fLastIsPar = false; } if (!isValidChar(c)) { finalOffset--; } this.fLen = finalOffset - widgetCaret; this.getPresentationUpdater().updateStyle(viewer, widgetCaret, this.fLen); } catch (BadLocationException e) { Log.log(e); } } else { unselected(viewer); } }
/** * update view find text * * @param widgetText text widget * @param widgetFind search text widget */ private void updateViewFindText(StyledText widgetText, Text widgetFind) { if (!widgetText.isDisposed()) { String findText = widgetFind.getText().toLowerCase(); ; if (!findText.isEmpty()) { // get cursor position int cursorIndex = widgetText.getCaretOffset(); // search int offset = widgetText.getText().toLowerCase().substring(cursorIndex).indexOf(findText); if (offset >= 0) { widgetText.redraw(); } else { Widgets.flash(widgetFind); } } else { widgetText.redraw(); } } }
/* * @see ISourceViewer#configure(SourceViewerConfiguration) */ @Override public void configure(SourceViewerConfiguration configuration) { /* * Prevent access to colors disposed in unconfigure(), see: * https://bugs.eclipse.org/bugs/show_bug.cgi?id=53641 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=86177 */ StyledText textWidget = getTextWidget(); if (textWidget != null && !textWidget.isDisposed()) { Color foregroundColor = textWidget.getForeground(); if (foregroundColor != null && foregroundColor.isDisposed()) textWidget.setForeground(null); Color backgroundColor = textWidget.getBackground(); if (backgroundColor != null && backgroundColor.isDisposed()) textWidget.setBackground(null); } super.configure(configuration); if (configuration instanceof JavaSourceViewerConfiguration) { JavaSourceViewerConfiguration javaSVCconfiguration = (JavaSourceViewerConfiguration) configuration; fOutlinePresenter = javaSVCconfiguration.getOutlinePresenter(this, false); if (fOutlinePresenter != null) fOutlinePresenter.install(this); fStructurePresenter = javaSVCconfiguration.getOutlinePresenter(this, true); if (fStructurePresenter != null) fStructurePresenter.install(this); fHierarchyPresenter = javaSVCconfiguration.getHierarchyPresenter(this, true); if (fHierarchyPresenter != null) fHierarchyPresenter.install(this); } if (fPreferenceStore != null) { fPreferenceStore.addPropertyChangeListener(this); initializeViewerColors(); } fIsConfigured = true; }
public boolean isControlOkToUse() { StyledText t = getSourceViewer().getTextWidget(); return t != null && !t.isDisposed(); }
@Override public void paintControl(PaintEvent e) { if (inDraw || styledText == null || styledText.isDisposed()) { return; } try { inDraw = true; boolean showIndentGuide = this.indentGuide.getShowIndentGuide(); if (!showIndentGuide) { return; } int xOffset = styledText.getHorizontalPixel(); int yOffset = styledText.getTopPixel(); // Important: call all to cache the new values (instead of doing all inside the or below). boolean styledTextContentChanged = getStyledTextContentChangedAndStoreNew(); boolean clientAreaChanged = getClientAreaChangedAndStoreNew(); boolean charCountChanged = getCharCountChangedAndStoreNew(); boolean tabWidthChanged = getTabWidthChangedAndStoreNew(); boolean redrawAll = styledTextContentChanged || clientAreaChanged || charCountChanged || tabWidthChanged || xOffset != lastXOffset || yOffset != lastYOffset; StyledTextContent currentContent = this.content; if (currClientArea == null || currClientArea.width < 5 || currClientArea.height < 5 || currCharCount < 1 || currentContent == null || currTabWidth <= 0) { return; } lastXOffset = xOffset; lastYOffset = yOffset; int topIndex; try { topIndex = JFaceTextUtil.getPartialTopIndex(styledText); } catch (IllegalArgumentException e1) { // Just silence it... // java.lang.IllegalArgumentException: Index out of bounds // at org.eclipse.swt.SWT.error(SWT.java:4458) // at org.eclipse.swt.SWT.error(SWT.java:4392) // at org.eclipse.swt.SWT.error(SWT.java:4363) // at org.eclipse.swt.custom.StyledText.getOffsetAtLine(StyledText.java:4405) // at org.eclipse.jface.text.JFaceTextUtil.getPartialTopIndex(JFaceTextUtil.java:103) // at // org.python.pydev.shared_ui.editor.VerticalIndentGuidesPainter.paintControl(VerticalIndentGuidesPainter.java:93) return; } int bottomIndex = JFaceTextUtil.getPartialBottomIndex(styledText); if (redrawAll) { this.lineToVerticalLinesToDraw = this.indentGuide.computeVerticalLinesToDrawInRegion(styledText, topIndex, bottomIndex); // This is a bit unfortunate: when something changes, we may have to repaint out of the // clipping // region, but even setting the clipping region (e.gc.setClipping), the clipping region may // still // be unchanged (because the system said that it only wants to repaint some specific area // already // and we can't make it bigger -- so, what's left for us is asking for a repaint of the full // area // in this case). if (askFullRedraw) { askFullRedraw = false; if (Math.abs(currClientArea.height - e.gc.getClipping().height) > 40) { // Only do it if the difference is really high (some decorations make it usually a bit // lower than // the actual client area -- usually around 14 in my tests, but make it a bit higher as // the usual // difference when a redraw is needed is pretty high). RunInUiThread.async( new Runnable() { @Override public void run() { StyledText s = styledText; if (s != null && !s.isDisposed()) { s.redraw(); } } }); } else { } } } if (this.lineToVerticalLinesToDraw != null) { try (AutoCloseable temp = configGC(e.gc)) { Collection<List<VerticalLinesToDraw>> values = lineToVerticalLinesToDraw.values(); for (List<VerticalLinesToDraw> list : values) { for (VerticalLinesToDraw verticalLinesToDraw : list) { verticalLinesToDraw.drawLine(e.gc); } } } } } catch (Exception e1) { Log.log(e1); } finally { inDraw = false; } }
@Override public void dispose() { if (text != null && !text.isDisposed()) text.removeModifyListener(modifyListener); super.dispose(); }
/** @return text */ public String getText() { if (text.isDisposed()) { return null; } return text.getText(); }