Skip to content

Commit

Permalink
extract and use DocumentListenerImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
koentsje committed Mar 25, 2014
1 parent 4a37677 commit fb04c9e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.widgets.Display;
import org.jboss.tools.aesh.core.document.Style;
Expand Down Expand Up @@ -149,12 +150,12 @@ public Document getDelegate() {
return document;
}

public StyleImpl getCurrentStyleRange() {
return currentStyle;
}

public void setCursorListener(CursorListener listener) {
this.cursorListener = listener;
}

public void setDocumentListener(IDocumentListener listener) {
document.addDocumentListener(listener);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jboss.tools.aesh.ui.internal.viewer;

import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.swt.custom.StyleRange;
import org.jboss.tools.aesh.ui.internal.document.DocumentImpl;
import org.jboss.tools.aesh.ui.internal.document.StyleImpl;

public class DocumentListenerImpl implements IDocumentListener {

private TextWidget textWidget;
private DocumentImpl document;

public DocumentListenerImpl(TextWidget textWidget, DocumentImpl document) {
this.textWidget = textWidget;
this.document = document;
}

@Override
public void documentAboutToBeChanged(DocumentEvent event) {
}

@Override
public void documentChanged(final DocumentEvent event) {
if (textWidget != null && !textWidget.isDisposed()) {
int lineCount = textWidget.getLineCount();
textWidget.setTopIndex(lineCount - 1);
StyleImpl style = (StyleImpl)document.getCurrentStyle();
StyleRange styleRange = style.getStyleRange();
if (styleRange != null &&
event.getLength() == 0 &&
styleRange.start <= document.getLength() &&
styleRange.length >= 0 &&
styleRange.start + styleRange.length <= document.getLength()) {
textWidget.setStyleRange(styleRange);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package org.jboss.tools.aesh.ui.view;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.jboss.tools.aesh.core.console.Console;
import org.jboss.tools.aesh.ui.internal.document.DocumentImpl;
import org.jboss.tools.aesh.ui.internal.document.StyleImpl;
import org.jboss.tools.aesh.ui.internal.util.FontManager;
import org.jboss.tools.aesh.ui.internal.viewer.CursorListenerImpl;
import org.jboss.tools.aesh.ui.internal.viewer.DocumentListenerImpl;
import org.jboss.tools.aesh.ui.internal.viewer.TextWidget;
import org.jboss.tools.aesh.ui.internal.viewer.VerifyKeyListenerImpl;

Expand All @@ -24,29 +21,6 @@ public abstract class AeshTextViewer extends TextViewer {
private DocumentImpl document;
private TextWidget textWidget;

private IDocumentListener documentListener = new IDocumentListener() {
@Override
public void documentAboutToBeChanged(DocumentEvent event) {
}
@Override
public void documentChanged(final DocumentEvent event) {
StyledText textWidget = getTextWidget();
if (textWidget != null && !textWidget.isDisposed()) {
int lineCount = textWidget.getLineCount();
textWidget.setTopIndex(lineCount - 1);
StyleImpl style = document.getCurrentStyleRange();
StyleRange styleRange = style.getStyleRange();
if (styleRange != null &&
event.getLength() == 0 &&
styleRange.start <= getDocument().getLength() &&
styleRange.length >= 0 &&
styleRange.start + styleRange.length <= getDocument().getLength()) {
textWidget.setStyleRange(styleRange);
}
}
}
};

public AeshTextViewer(Composite parent) {
super(parent, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
initialize();
Expand Down Expand Up @@ -100,7 +74,7 @@ private void initializeConsole() {
private void initializeDocument() {
document = new DocumentImpl();
document.setCursorListener(new CursorListenerImpl(textWidget, document));
document.getDelegate().addDocumentListener(documentListener);
document.setDocumentListener(new DocumentListenerImpl(textWidget, document));
}

private void initializeTextWidget() {
Expand Down

0 comments on commit fb04c9e

Please sign in to comment.