/** * Creates a corresponding Undo or Redo step from the given event and pushes * it to the stack. The Redo stack is, logically, emptied if the event comes * from a normal user action. * * @param event * @see org.eclipse.swt.custom.ExtendedModifyListener#modifyText(org.eclipse. * swt.custom.ExtendedModifyEvent) */ public void modifyText(ExtendedModifyEvent event) { if (!enabled) return; long time = 0xFFFFFFFFL & event.time; logger.debug("modified: "+event+", l="+event.length+" rt="+event.replacedText+" time= "+time); if (isUndo) { stack.pushRedo(event); } else { // is Redo or a normal user action stack.pushUndo(event); if (!isRedo) { stack.clearRedo(); // TODO Switch to treat consecutive characters as one event? } } }
@Override public void modifyText(ExtendedModifyEvent event) { //TODO: is this ever not true? if(source != currentText) return; if(undoing) changes.add(changeIndex, event); else if(redoing) changes.add(changeIndex++, event); else { if(changeIndex < changes.size()) changes.subList(changeIndex, changes.size()).clear(); changes.add(changeIndex++, event); } undoing = redoing = false; // need to redraw page lines int lineCount = source.getLineCount(); if(lineCount != prevLineCount) redraw(); prevLineCount = lineCount; }
@Override public void modifyText(ExtendedModifyEvent event) { clearAll(); lastFindIndex = -1; originalTextSelection = new Point(0, 0); textModifyTimer.cancel(); textModifyTimer = new Timer(); textModifyTimer.schedule(new TimerTask() { @Override public void run() { textModifyTimer.cancel(); Display.getDefault().syncExec(() -> { clearAll(); doFind(); }); } }, 250); }
@Override public void modifyText(ExtendedModifyEvent event) { if (ignore) return; captured = false; currentUndoPoint = -1; quietTimer.cancel(); quietTimer = new Timer(); quietTimer.schedule(new TimerTask() { @Override public void run() { quietTimer.cancel(); Display.getDefault().syncExec(() -> capture()); } }, 250); }
@Override public void modifyText(ExtendedModifyEvent event){ if (ranges != null) { int pos = event.start; int len = event.length; String text = event.replacedText; int diff = len - text.length(); for (Samdas.Range r : ranges) { int spos = r.getPos(); if (updateXRefMode) { if (spos > pos) { r.setPos(spos + diff); } } else { if (spos >= pos) { r.setPos(spos + diff); } } } } }
public undoManager(StyledText text,int level){ if(text != null) { this.text = text; text.addExtendedModifyListener(new ExtendedModifyListener() { @Override public void modifyText(ExtendedModifyEvent event) { // TODO Auto-generated method stub // if(event.st) } }); } this.level = level; }
public void addModifyListener(final StyledText st) { st.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { // TODO Auto-generated method stub black.ba.setFileChanged(); } }); // Ϊ��������ַ�������ʽ st.addExtendedModifyListener(new ExtendedModifyListener() { public void modifyText(ExtendedModifyEvent event) { if (!st.isDisposed()) { if (event.start > 0 && event.start <= st.getCharCount()) { int alignment = st.getLineAlignment(st.getLineAtOffset(event.start - 1)); st.setLineAlignment(st.getLineAtOffset(event.start), 1, alignment); StyleRange s = st.getStyleRangeAtOffset(event.start - 1); if (s != null && s.start + s.length < st.getCharCount()) { s.start = event.start; s.length = 1; st.setStyleRange(s); } } if (event.start + event.length == st.getCharCount() && defaultStyle != null) { StyleRange sr = new StyleRange(defaultStyle); sr.start = event.start; sr.length = event.length; st.setStyleRange(sr); defaultStyle = null; } } } }); }
/** * Performs the Undo action. A new corresponding Redo step is automatically * pushed to the stack. */ public void undo() { if (USE_TIME_DELTA) { long timeBefore = -1; while (stack.hasUndo()) { ExtendedModifyEvent e = stack.popUndo(); if (timeBefore >= 0) { long diff = timeBefore - (0xFFFFFFFFL & e.time); if (diff > DELTA) { stack.pushUndo(e); break; } } isUndo = true; revertEvent(e); isUndo = false; timeBefore = 0xFFFFFFFFL & e.time; } } else { if (stack.hasUndo()) { isUndo = true; revertEvent(stack.popUndo()); isUndo = false; } } }
/** * Performs the Redo action. A new corresponding Undo step is automatically * pushed to the stack. */ public void redo() { if (USE_TIME_DELTA) { long timeBefore = -1; while (stack.hasRedo()) { ExtendedModifyEvent e = stack.popRedo(); if (timeBefore >= 0) { long diff = timeBefore - (0xFFFFFFFFL & e.time); if (diff > DELTA) { stack.pushRedo(e); break; } } isRedo = true; revertEvent(e); isRedo = false; timeBefore = 0xFFFFFFFFL & e.time; } } else { if (stack.hasRedo()) { isRedo = true; revertEvent(stack.popRedo()); isRedo = false; } } }
/** * Reverts the given modify event, in the way as the Eclipse text editor * does it. * * @param event */ private void revertEvent(ExtendedModifyEvent event) { editor.replaceTextRange(event.start, event.length, event.replacedText); // (causes the modifyText() listener method to be called) editor.setSelectionRange(event.start, event.replacedText.length()); }
/** * Creates a corresponding Undo or Redo step from the given event and pushes it to the stack. The Redo stack is, * logically, emptied if the event comes from a normal user action. * * @param event The event * @see org.eclipse.swt.custom.ExtendedModifyListener#modifyText(org.eclipse.swt.custom.ExtendedModifyEvent) */ @Override public void modifyText(ExtendedModifyEvent event) { if (isUndo) { stack.pushRedo(event); } else { // is Redo or a normal user action stack.pushUndo(event); if (!isRedo) { stack.clearRedo(); // TODO Switch to treat consecutive characters as one event? } } }
/** * <p> * Undoes the last change. * </p><p> * Currently only simply changes are recorded. * </p> * * @see #redo() * @see #getModified() */ public void undo() { if(changeIndex < 1) return; undoing = true; changeIndex--; ExtendedModifyEvent change = changes.remove(changeIndex); currentText.replaceTextRange(change.start, change.length, change.replacedText); currentText.setCaretOffset(change.start + change.replacedText.length()); scrollToCaret(); }
/** * <p> * Undoes the last undo. * </p> * * @see #undo() * @see #getModified() */ public void redo() { if(changeIndex == changes.size()) return; redoing = true; ExtendedModifyEvent change = changes.remove(changeIndex); currentText.replaceTextRange(change.start, change.length, change.replacedText); currentText.setCaretOffset(change.start + change.replacedText.length()); scrollToCaret(); }
/** * Creates a corresponding Undo or Redo step from the given event and pushes * it to the stack. The Redo stack is, logically, emptied if the event comes * from a normal user action. * * @param event * @see org.eclipse.swt.custom.ExtendedModifyListener#modifyText(org.eclipse.swt.custom.ExtendedModifyEvent) */ public void modifyText(ExtendedModifyEvent event) { if (isUndo) { stack.pushRedo(event); } else { // is Redo or a normal user action stack.pushUndo(event); if (!isRedo) { stack.clearRedo(); // TODO Switch to treat consecutive characters as one event? } } }
/** * Creates a new instance of this class. Automatically starts listening to * corresponding key and modify events coming from the given * <var>editor</var>. * * @param editor * the text field to which the Undo-Redo functionality should be * added */ public UndoRedoImpl(StyledText editor) { editor.addExtendedModifyListener(this); editor.addKeyListener(this); this.editor = editor; stack = new UndoRedoStack<ExtendedModifyEvent>(); if ("cocoa".equals(SWT.getPlatform())) undoKey = SWT.COMMAND; else undoKey = SWT.CTRL; }
/** * Creates a corresponding Undo or Redo step from the given event and pushes * it to the stack. The Redo stack is, logically, emptied if the event comes * from a normal user action. * * @param event * @see org.eclipse.swt.custom.ExtendedModifyListener#modifyText(org.eclipse. * swt.custom.ExtendedModifyEvent) */ public void modifyText(ExtendedModifyEvent event) { if (isUndo) { stack.pushRedo(event); } else { // is Redo or a normal user action stack.pushUndo(event); if (!isRedo) { stack.clearRedo(); // TODO Switch to treat consecutive characters as one event? } } }
/** * Reverts the given modify event, in the way as the Eclipse text editor * does it. * * @param event */ private void revertEvent(ExtendedModifyEvent event) { editor.replaceTextRange(event.start, event.length, event.replacedText); // (causes the modifyText() listener method to be called) editor.setSelection(event.start+ event.replacedText.length()); // editor.setSelectionRange(event.start, event.replacedText.length()); }
public void modifyText(ExtendedModifyEvent event) { /* * every changes should be record into undo list * (except the change is caused by redo/undo action) */ addUndoList(event); /* * update the styles */ updateStyledRanges(event); /* * auto completion */ int start = event.start; int length = event.length; String textRange = getTextRange(start, length); String previousChar = null; if(start-1>0){ previousChar = getTextRange(start-1, 1); } if("/".equals(textRange) && "<".equals(previousChar) && !isCommentBlock(start-1)){ String openedTag = searchNearestOpenedTag(start-2); if(openedTag != null && !openedTag.isEmpty()){ replaceTextRange(start+length, 0 , openedTag); setSelection(start + event.length + openedTag.length()); } } }
private void updateStyledRanges(ExtendedModifyEvent event) { StyledText st = (StyledText) event.widget; int start = event.start; int length = event.length; String replacedText = event.replacedText; String textRange = st.getTextRange(start, length); /* * if no real change * then return */ if ("".equals(textRange.trim()) && "".equals(replacedText.trim())) { return; } List<StyleRange> styles = new ArrayList<StyleRange>(); scanner.setRange(st.getText()); int token = scanner.nextToken(); while (token != EOF) { int startOffset = scanner.getStartOffset(); int tokenLength = scanner.getLength(); String tokenText = st.getTextRange(startOffset, tokenLength).trim(); for (String s : fgKeywords) { if (s.equals(tokenText)) { token = KEY; break; } } Color color = getColor(token); StyleRange style = new StyleRange(startOffset, tokenLength, color, null); if (token == KEY) { style.fontStyle = SWT.BOLD; } styles.add(style); token = scanner.nextToken(); } st.setStyleRanges(styles.toArray(new StyleRange[0])); }
private void addUndoList(ExtendedModifyEvent event) { if(fromUndoRedo ){ return; } StyledText st = (StyledText) event.widget; String currText = st.getText(); String newText = currText.substring(event.start, event.start + event.length); if (undoStack.size() == MAX_STACK_SIZE) { undoStack.remove(undoStack.size() - 1); } undoStack.add(0, new UndoRedoModel(newText, event.start, event.length, event.replacedText)); }
/** * Constructor. * * @param parent parent for the styled text * @param style style to be used */ public ScriptConsoleStyledText(Composite parent, int style) { super(parent, style); /** * The StyledText will change the caretOffset that we've updated during the modifications, * so, the verify and the extended modify listener will keep track if it actually does * that and will reset the caret to the position we actually added it. * * Feels like a hack but I couldn't find a better way to do it. */ addVerifyListener(new VerifyListener() { @Override public void verifyText(VerifyEvent e) { internalCaretSet = -1; } }); /** * Set it to the location we've set it to be. */ addExtendedModifyListener(new ExtendedModifyListener() { @Override public void modifyText(ExtendedModifyEvent event) { if (internalCaretSet != -1) { if (internalCaretSet != getCaretOffset()) { setCaretOffset(internalCaretSet); } internalCaretSet = -1; } } }); initDragDrop(); handleDeletePreviousWord = new HandleDeletePreviousWord(); handleLineStartAction = new HandleLineStartAction(); }
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER |SWT.MULTI ); styledText.setText(text); FontData data = display.getSystemFont().getFontData()[0]; Font font = new Font(display, data.getName(), 16, SWT.BOLD); styledText.setFont(font); styledText.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); // styledText.setOrientation(SWT.RIGHT_TO_LEFT); // text.setTextDirection(SWT.RIGHT_TO_LEFT); styledText.setAlignment(SWT.RIGHT); styledText.addExtendedModifyListener(new ExtendedModifyListener() { @Override public void modifyText(ExtendedModifyEvent event) { styledText.setCaretOffset(event.start); } }); // styledText.addListener(SWT.Resize, new Listener() { // public void handleEvent(Event event) { // Rectangle rect = styledText.getClientArea(); // Image newImage = new Image(display, 1, Math.max(1, rect.height)); // GC gc = new GC(newImage); // gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); // gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); // gc.fillGradientRectangle(rect.x, rect.y, 1, rect.height, true); // gc.dispose(); // styledText.setBackgroundImage(newImage); // if (oldImage != null) // oldImage.dispose(); // oldImage = newImage; // } // }); shell.setSize(700, 400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } if (oldImage != null) oldImage.dispose(); font.dispose(); display.dispose(); }
/** * Reverts the given modify event, in the way as the Eclipse text editor does it. * * @param event The event */ private void revertEvent(ExtendedModifyEvent event) { // This causes the modifyText() listener method to be called. styledText.replaceTextRange(event.start, event.length, event.replacedText); styledText.setSelectionRange(event.start, event.replacedText.length()); }
public void modifyText(ExtendedModifyEvent e) { parser.textModified(e.start, e.length, e.replacedText); }
@Override public void modifyText(ExtendedModifyEvent event) { this.currClientArea = null; //will force redrawing everything askFullRedraw = true; }
public void modifyText(final ExtendedModifyEvent event){ StyledText text = (StyledText) event.widget; if (event.length == 0) { return; } StyleRange style; if ((event.length == 1) || text.getTextRange(event.start, event.length).equals(text.getLineDelimiter())) { // Have the new text take on the style of the text to its right // (during // typing) if no style information is active. int caretOffset = text.getCaretOffset() - 1; style = null; if (caretOffset < text.getCharCount()) { style = text.getStyleRangeAtOffset(caretOffset); } if (style != null) { style = (StyleRange) style.clone(); style.start = event.start; style.length = event.length; } else { style = new StyleRange(event.start, event.length, null, null, SWT.NORMAL); } if (isBold) { style.fontStyle |= SWT.BOLD; } if (isItalic) { style.fontStyle |= SWT.ITALIC; } style.underline = isUnderline; style.font = new Font(getDisplay(), fontName, fontHeight, style.fontStyle); if (!style.isUnstyled()) { text.setStyleRange(style); } } else { // pasteToolItem occurring, have text take on the styles it had when it was // cutToolItem/copied if (!cachedStyles.isEmpty()) { for (int i = 0; i < cachedStyles.size(); i++) { style = cachedStyles.elementAt(i); StyleRange newStyle = (StyleRange) style.clone(); newStyle.start = style.start + event.start; text.setStyleRange(newStyle); } } else { text.setSelection(event.start, event.start + event.length); updateStylesOnText(); } } updateControls(); }
/** * Creates a new instance of this class. Automatically starts listening to * corresponding key and modify events coming from the given * <var>editor</var>. * * @param editor * the text field to which the Undo-Redo functionality should be * added */ public UndoRedoImpl(StyledText editor) { editor.addExtendedModifyListener(this); editor.addKeyListener(this); this.editor = editor; stack = new UndoRedoStack<ExtendedModifyEvent>(); }