Skip to content

Commit

Permalink
move character constants out of AeshTextViewer, move reset() function…
Browse files Browse the repository at this point in the history
…ality to DelegatingDocument
  • Loading branch information
koentsje committed Mar 25, 2014
1 parent 46aa3af commit 56c3131
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

import org.eclipse.jface.text.Document;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.TextStyle;
import org.jboss.tools.aesh.ui.internal.util.ColorConstants;
import org.jboss.tools.aesh.ui.internal.util.FontManager;

public class DelegateDocument extends Document {

Expand All @@ -24,15 +19,8 @@ public interface CursorListener {

public DelegateDocument() {
proxy = new DelegatingDocument(this);
currentStyleRange = getDefaultStyleRange();
}

public void reset() {
set("");
moveCursorTo(0);
currentStyleRange = getDefaultStyleRange();
}

void moveCursorTo(int newOffset) {
cursorOffset = newOffset;
for (CursorListener listener : cursorListeners) {
Expand Down Expand Up @@ -64,11 +52,4 @@ public DelegatingDocument getProxy() {
return proxy;
}

StyleRange getDefaultStyleRange() {
Font font = FontManager.INSTANCE.getDefault();
Color foreground = ColorConstants.BLACK;
Color background = ColorConstants.WHITE;
return new StyleRange(new TextStyle(font, foreground, background));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
public class DelegatingDocument implements Document {

private DelegateDocument delegate;
private Style currentStyle;
private DelegatingStyleRange currentStyle;
private int savedCursor = 0;


public DelegatingDocument(DelegateDocument document) {
this.delegate = document;
this.currentStyle = DelegatingStyleRange.getDefault();
}

@Override
Expand Down Expand Up @@ -71,9 +71,11 @@ public void reset() {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
delegate.reset();
delegate.set("");
}
});
moveCursorTo(0);
setDefaultStyle();
}

@Override
Expand Down Expand Up @@ -112,7 +114,7 @@ public void saveCursor() {

@Override
public Style newStyleFromCurrent() {
StyleRange oldStyleRange = delegate.getCurrentStyleRange();
StyleRange oldStyleRange = currentStyle.getDelegate();
StyleRange newStyleRange = new StyleRange(oldStyleRange);
newStyleRange.start = oldStyleRange.start + oldStyleRange.length;
newStyleRange.length = 0;
Expand All @@ -122,9 +124,7 @@ public Style newStyleFromCurrent() {
@Override
public void setCurrentStyle(Style styleRangeProxy) {
if (styleRangeProxy instanceof DelegatingStyleRange) {
StyleRange styleRange = ((DelegatingStyleRange)styleRangeProxy).getStyleRange();
delegate.setCurrentStyleRange(styleRange);
currentStyle = styleRangeProxy;
currentStyle = (DelegatingStyleRange)styleRangeProxy;
}
}

Expand All @@ -135,10 +135,19 @@ public Style getCurrentStyle() {

@Override
public void setDefaultStyle() {
StyleRange styleRange = delegate.getDefaultStyleRange();
DelegatingStyleRange defaultStyle = DelegatingStyleRange.getDefault();
StyleRange styleRange = defaultStyle.getStyleRange();
styleRange.start = delegate.getLength();
styleRange.length = 0;
setCurrentStyle(new DelegatingStyleRange(styleRange));
setCurrentStyle(defaultStyle);
}


public DelegateDocument getDelegate() {
return delegate;
}

public DelegatingStyleRange getCurrentStyleRange() {
return currentStyle;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.TextStyle;
import org.jboss.tools.aesh.core.document.Style;
import org.jboss.tools.aesh.ui.internal.util.ColorConstants;
import org.jboss.tools.aesh.ui.internal.util.FontManager;
Expand All @@ -15,7 +17,7 @@ public DelegatingStyleRange(StyleRange styleRange) {
this.styleRange = styleRange;
}

public StyleRange getStyleRange() {
StyleRange getStyleRange() {
return styleRange;
}

Expand Down Expand Up @@ -214,4 +216,17 @@ private void reverseVideo() {
styleRange.foreground = background;
}

public static DelegatingStyleRange getDefault() {
Font font = FontManager.INSTANCE.getDefault();
Color foreground = ColorConstants.BLACK;
Color background = ColorConstants.WHITE;
TextStyle textStyle = new TextStyle(font, foreground, background);
StyleRange styleRange = new StyleRange(textStyle);
return new DelegatingStyleRange(styleRange);
}

public StyleRange getDelegate() {
return styleRange;
}

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

public class CharacterConstants {

public static final String START_LINE = new Character((char)1).toString();
public static final String PREV_CHAR = new Character((char)2).toString();
public static final String CTRL_C = new Character((char)3).toString();
public static final String CTRL_D = new Character((char)4).toString();
public static final String END_LINE = new Character((char)5).toString();
public static final String NEXT_CHAR = new Character((char)6).toString();
public static final String DELETE_PREV_CHAR = new Character((char)8).toString();
public static final String PREV_HISTORY = new Character((char)16).toString();
public static final String NEXT_HISTORY = new Character((char)14).toString();
public static final String DELETE_NEXT_CHAR = new String(new char[] {(char)27,(char)91,(char)51,(char)126});

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,15 @@
import org.jboss.tools.aesh.core.console.Console;
import org.jboss.tools.aesh.ui.internal.document.DelegateDocument;
import org.jboss.tools.aesh.ui.internal.document.DelegateDocument.CursorListener;
import org.jboss.tools.aesh.ui.internal.document.DelegatingDocument;
import org.jboss.tools.aesh.ui.internal.document.DelegatingStyleRange;
import org.jboss.tools.aesh.ui.internal.util.CharacterConstants;
import org.jboss.tools.aesh.ui.internal.util.FontManager;

public abstract class AeshTextViewer extends TextViewer {

private static final String START_LINE = new Character((char)1).toString();
private static final String PREV_CHAR = new Character((char)2).toString();
private static final String CTRL_C = new Character((char)3).toString();
private static final String CTRL_D = new Character((char)4).toString();
private static final String END_LINE = new Character((char)5).toString();
private static final String NEXT_CHAR = new Character((char)6).toString();
private static final String DELETE_PREV_CHAR = new Character((char)8).toString();
private static final String PREV_HISTORY = new Character((char)16).toString();
private static final String NEXT_HISTORY = new Character((char)14).toString();
private static final String DELETE_NEXT_CHAR = new String(new char[] {(char)27,(char)91,(char)51,(char)126});

private Console console;
private DelegateDocument aeshDocument;
private DelegatingDocument aeshDocument;

private CursorListener cursorListener = new CursorListener() {
@Override
Expand All @@ -53,7 +45,8 @@ public void documentChanged(final DocumentEvent event) {
if (textWidget != null && !textWidget.isDisposed()) {
int lineCount = textWidget.getLineCount();
textWidget.setTopIndex(lineCount - 1);
StyleRange styleRange = aeshDocument.getCurrentStyleRange();
DelegatingStyleRange style = aeshDocument.getCurrentStyleRange();
StyleRange styleRange = style.getDelegate();
if (styleRange != null &&
event.getLength() == 0 &&
styleRange.start <= getDocument().getLength() &&
Expand All @@ -74,8 +67,8 @@ public void startConsole() {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
console.connect(aeshDocument.getProxy());
setDocument(aeshDocument);
console.connect(aeshDocument);
setDocument(aeshDocument.getDelegate());
console.start();
}
});
Expand All @@ -100,28 +93,28 @@ protected StyledText createTextWidget(Composite parent, int styles) {
public void invokeAction(int action) {
switch (action) {
case ST.LINE_END:
console.sendInput(END_LINE);
console.sendInput(CharacterConstants.END_LINE);
break;
case ST.LINE_START:
console.sendInput(START_LINE);
console.sendInput(CharacterConstants.START_LINE);
break;
case ST.LINE_UP:
console.sendInput(PREV_HISTORY);
console.sendInput(CharacterConstants.PREV_HISTORY);
break;
case ST.LINE_DOWN:
console.sendInput(NEXT_HISTORY);
console.sendInput(CharacterConstants.NEXT_HISTORY);
break;
case ST.COLUMN_PREVIOUS:
console.sendInput(PREV_CHAR);
console.sendInput(CharacterConstants.PREV_CHAR);
break;
case ST.COLUMN_NEXT:
console.sendInput(NEXT_CHAR);
console.sendInput(CharacterConstants.NEXT_CHAR);
break;
case ST.DELETE_PREVIOUS:
console.sendInput(DELETE_PREV_CHAR);
console.sendInput(CharacterConstants.DELETE_PREV_CHAR);
break;
case ST.DELETE_NEXT:
console.sendInput(DELETE_NEXT_CHAR);
console.sendInput(CharacterConstants.DELETE_NEXT_CHAR);
break;
default: super.invokeAction(action);
}
Expand All @@ -147,9 +140,10 @@ private void initializeConsole() {
}

private void initializeDocument() {
aeshDocument = new DelegateDocument();
aeshDocument.addCursorListener(cursorListener);
aeshDocument.addDocumentListener(documentListener);
DelegateDocument delegate = new DelegateDocument();
aeshDocument = new DelegatingDocument(delegate);
delegate.addCursorListener(cursorListener);
delegate.addDocumentListener(documentListener);
}

private void initializeTextWidget() {
Expand All @@ -159,9 +153,9 @@ private void initializeTextWidget() {
public void verifyKey(VerifyEvent event) {
if ((event.stateMask & SWT.CTRL) == SWT.CTRL ) {
if (event.keyCode == 'd') {
console.sendInput(CTRL_D);
console.sendInput(CharacterConstants.CTRL_D);
} else if (event.keyCode == 'c') {
console.sendInput(CTRL_C);
console.sendInput(CharacterConstants.CTRL_C);
}
}
}
Expand Down

0 comments on commit 56c3131

Please sign in to comment.