Skip to content

Commit

Permalink
create and use TextWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
koentsje committed Mar 25, 2014
1 parent c1b0abc commit d93ffff
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

public class DocumentImpl implements org.jboss.tools.aesh.core.document.Document {

private Document delegate;
private Document document;
private StyleImpl currentStyle;
private int savedCursor = 0;
private int cursorOffset = 0;
private Set<CursorListener> cursorListeners = new HashSet<CursorListener>();

public DocumentImpl() {
this.delegate = new Document();
this.document = new Document();
this.currentStyle = StyleImpl.getDefault();
}

Expand All @@ -32,7 +32,7 @@ public int getCursorOffset() {
public int getLineOfOffset(int offset) {
int result = -1;
try {
result = delegate.getLineOfOffset(offset);
result = document.getLineOfOffset(offset);
} catch (BadLocationException e) {
AeshUIPlugin.log(e);
}
Expand All @@ -43,7 +43,7 @@ public int getLineOfOffset(int offset) {
public int getLineOffset(int line) {
int result = -1;
try {
result = delegate.getLineOffset(line);
result = document.getLineOffset(line);
} catch (BadLocationException e) {
AeshUIPlugin.log(e);
}
Expand All @@ -54,7 +54,7 @@ public int getLineOffset(int line) {
public int getLineLength(int line) {
int result = -1;
try {
result = delegate.getLineLength(line);
result = document.getLineLength(line);
} catch (BadLocationException e) {
AeshUIPlugin.log(e);
}
Expand All @@ -79,7 +79,7 @@ public void reset() {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
delegate.set("");
document.set("");
}
});
moveCursorTo(0);
Expand All @@ -88,7 +88,7 @@ public void run() {

@Override
public int getLength() {
return delegate.getLength();
return document.getLength();
}

@Override
Expand All @@ -97,7 +97,7 @@ public void replace(final int pos, final int length, final String text) {
@Override
public void run() {
try {
delegate.replace(pos, length, text);
document.replace(pos, length, text);
} catch (BadLocationException e) {
AeshUIPlugin.log(e);
}
Expand Down Expand Up @@ -145,13 +145,13 @@ public Style getCurrentStyle() {
public void setDefaultStyle() {
StyleImpl defaultStyle = StyleImpl.getDefault();
StyleRange styleRange = defaultStyle.getStyleRange();
styleRange.start = delegate.getLength();
styleRange.start = document.getLength();
styleRange.length = 0;
setCurrentStyle(defaultStyle);
}

public Document getDelegate() {
return delegate;
return document;
}

public StyleImpl getCurrentStyleRange() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.jboss.tools.aesh.ui.internal.widget;

import org.eclipse.swt.custom.ST;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.jboss.tools.aesh.core.console.Console;
import org.jboss.tools.aesh.ui.internal.util.CharacterConstants;

public class TextWidget extends StyledText {

private Console console = null;

public TextWidget(Composite parent, int style) {
super(parent, style);
}

public void setConsole(Console console) {
this.console = console;
}

public void invokeAction(int action) {
switch (action) {
case ST.LINE_END:
console.sendInput(CharacterConstants.END_LINE);
break;
case ST.LINE_START:
console.sendInput(CharacterConstants.START_LINE);
break;
case ST.LINE_UP:
console.sendInput(CharacterConstants.PREV_HISTORY);
break;
case ST.LINE_DOWN:
console.sendInput(CharacterConstants.NEXT_HISTORY);
break;
case ST.COLUMN_PREVIOUS:
console.sendInput(CharacterConstants.PREV_CHAR);
break;
case ST.COLUMN_NEXT:
console.sendInput(CharacterConstants.NEXT_CHAR);
break;
case ST.DELETE_PREVIOUS:
console.sendInput(CharacterConstants.DELETE_PREV_CHAR);
break;
case ST.DELETE_NEXT:
console.sendInput(CharacterConstants.DELETE_NEXT_CHAR);
break;
default: super.invokeAction(action);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ST;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.VerifyKeyListener;
Expand All @@ -18,11 +17,13 @@
import org.jboss.tools.aesh.ui.internal.document.StyleImpl;
import org.jboss.tools.aesh.ui.internal.util.CharacterConstants;
import org.jboss.tools.aesh.ui.internal.util.FontManager;
import org.jboss.tools.aesh.ui.internal.widget.TextWidget;

public abstract class AeshTextViewer extends TextViewer {

private Console console;
private DocumentImpl aeshDocument;
private TextWidget textWidget;

private CursorListener cursorListener = new CursorListener() {
@Override
Expand Down Expand Up @@ -88,39 +89,8 @@ public void run() {
protected abstract Console createConsole();

protected StyledText createTextWidget(Composite parent, int styles) {
StyledText styledText= new StyledText(parent, styles) {
public void invokeAction(int action) {
switch (action) {
case ST.LINE_END:
console.sendInput(CharacterConstants.END_LINE);
break;
case ST.LINE_START:
console.sendInput(CharacterConstants.START_LINE);
break;
case ST.LINE_UP:
console.sendInput(CharacterConstants.PREV_HISTORY);
break;
case ST.LINE_DOWN:
console.sendInput(CharacterConstants.NEXT_HISTORY);
break;
case ST.COLUMN_PREVIOUS:
console.sendInput(CharacterConstants.PREV_CHAR);
break;
case ST.COLUMN_NEXT:
console.sendInput(CharacterConstants.NEXT_CHAR);
break;
case ST.DELETE_PREVIOUS:
console.sendInput(CharacterConstants.DELETE_PREV_CHAR);
break;
case ST.DELETE_NEXT:
console.sendInput(CharacterConstants.DELETE_NEXT_CHAR);
break;
default: super.invokeAction(action);
}
}
};
styledText.setLeftMargin(Math.max(styledText.getLeftMargin(), 2));
return styledText;
textWidget = new TextWidget(parent, styles);
return textWidget;
}

protected void handleVerifyEvent(VerifyEvent e) {
Expand All @@ -145,8 +115,9 @@ private void initializeDocument() {
}

private void initializeTextWidget() {
getTextWidget().setFont(JFaceResources.getFont(FontManager.AESH_CONSOLE_FONT));
getTextWidget().addVerifyKeyListener(new VerifyKeyListener() {
textWidget.setConsole(console);
textWidget.setFont(JFaceResources.getFont(FontManager.AESH_CONSOLE_FONT));
textWidget.addVerifyKeyListener(new VerifyKeyListener() {
@Override
public void verifyKey(VerifyEvent event) {
if ((event.stateMask & SWT.CTRL) == SWT.CTRL ) {
Expand Down

0 comments on commit d93ffff

Please sign in to comment.