2008-12-16 05:07:36 +00:00
|
|
|
/*
|
|
|
|
* AppleCommander - An Apple ][ image utility.
|
2009-01-03 05:48:26 +00:00
|
|
|
* Copyright (C) 2008 by David Schmidt
|
|
|
|
* david__schmidt at users.sourceforge.net
|
2008-12-16 05:07:36 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
package com.webcodepro.applecommander.ui.swt.filteradapter;
|
|
|
|
|
|
|
|
import org.eclipse.swt.SWT;
|
|
|
|
import org.eclipse.swt.custom.StyleRange;
|
|
|
|
import org.eclipse.swt.custom.StyledText;
|
|
|
|
import org.eclipse.swt.graphics.Image;
|
|
|
|
import org.eclipse.swt.graphics.Point;
|
|
|
|
|
|
|
|
import com.webcodepro.applecommander.ui.swt.FileViewerWindow;
|
|
|
|
import com.webcodepro.applecommander.ui.swt.util.contentadapter.StyledTextAdapter;
|
|
|
|
import com.webcodepro.applecommander.util.BusinessBASICToken;
|
|
|
|
import com.webcodepro.applecommander.util.BusinessBASICTokenizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a view of a syntax-colored Apple /// Business BASIC program listing.
|
|
|
|
*
|
2009-01-03 05:48:26 +00:00
|
|
|
* @author David Schmidt
|
2008-12-16 05:07:36 +00:00
|
|
|
*/
|
|
|
|
public class BusinessBASICFilterAdapter extends FilterAdapter {
|
|
|
|
private StyledText styledText;
|
|
|
|
|
|
|
|
public BusinessBASICFilterAdapter(FileViewerWindow window, String text, String toolTipText, Image image) {
|
|
|
|
super(window, text, toolTipText, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void display() {
|
|
|
|
if (styledText == null) {
|
|
|
|
createStyledText();
|
|
|
|
}
|
|
|
|
Point size = styledText.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
|
|
|
|
getComposite().setContent(styledText);
|
|
|
|
getComposite().setExpandHorizontal(true);
|
|
|
|
getComposite().setExpandVertical(true);
|
|
|
|
getComposite().setMinWidth(size.x);
|
|
|
|
getComposite().setMinHeight(size.y);
|
|
|
|
getComposite().getContent().addListener(SWT.KeyUp, getToolbarCommandHandler());
|
|
|
|
|
|
|
|
setContentTypeAdapter(new StyledTextAdapter(styledText, getFileEntry().getFilename()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void dispose() {
|
|
|
|
styledText.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void createStyledText() {
|
|
|
|
styledText = new StyledText(getComposite(), SWT.NONE);
|
|
|
|
styledText.setForeground(getBlackColor());
|
|
|
|
styledText.setFont(getCourierFont());
|
|
|
|
styledText.setEditable(false);
|
|
|
|
|
|
|
|
BusinessBASICTokenizer tokenizer = new BusinessBASICTokenizer(getFileEntry());
|
|
|
|
boolean firstLine = true;
|
2009-01-02 20:45:40 +00:00
|
|
|
boolean firstData = true;
|
|
|
|
int nestLevels = 0;
|
2008-12-16 05:07:36 +00:00
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
BusinessBASICToken token = tokenizer.getNextToken();
|
|
|
|
if (token == null) {
|
|
|
|
continue; // should be end of program...
|
|
|
|
} else if (token.isLineNumber()) {
|
|
|
|
if (firstLine) {
|
|
|
|
firstLine = false;
|
|
|
|
} else {
|
|
|
|
styledText.append("\n"); //$NON-NLS-1$
|
|
|
|
}
|
2009-01-02 20:45:40 +00:00
|
|
|
firstData = true;
|
2008-12-16 05:07:36 +00:00
|
|
|
styledText.append(Integer.toString(token.getLineNumber()));
|
2009-01-03 05:48:26 +00:00
|
|
|
styledText.append(" "); //$NON-NLS-1$
|
2009-01-02 20:45:40 +00:00
|
|
|
if (nestLevels > 0) {
|
|
|
|
for (int i = 0; i < nestLevels; i++)
|
|
|
|
styledText.append(" "); //$NON-NLS-1$
|
|
|
|
}
|
2008-12-16 05:07:36 +00:00
|
|
|
} else if (token.isCommandSeparator() || token.isExpressionSeparator()) {
|
|
|
|
styledText.append(token.getStringValue());
|
2009-01-03 05:48:26 +00:00
|
|
|
firstData = true;
|
2008-12-16 05:07:36 +00:00
|
|
|
} else if (token.isEndOfCommand()) {
|
|
|
|
styledText.append("\n"); //$NON-NLS-1$
|
2009-01-02 20:45:40 +00:00
|
|
|
firstData = false;
|
2008-12-16 05:07:36 +00:00
|
|
|
} else if (token.isString()) {
|
2009-01-03 05:48:26 +00:00
|
|
|
if (!firstData)
|
2009-01-02 20:45:40 +00:00
|
|
|
styledText.append(" "); //$NON-NLS-1$
|
2008-12-16 05:07:36 +00:00
|
|
|
int caretOffset = styledText.getCharCount();
|
2009-01-03 05:48:26 +00:00
|
|
|
styledText.append(token.getStringValue().trim());
|
2008-12-16 05:07:36 +00:00
|
|
|
StyleRange styleRange = new StyleRange();
|
|
|
|
styleRange.start = caretOffset;
|
2009-01-03 05:48:26 +00:00
|
|
|
styleRange.length = token.getStringValue().trim().length();
|
2008-12-16 05:07:36 +00:00
|
|
|
styleRange.foreground = getGreenColor();
|
|
|
|
styledText.setStyleRange(styleRange);
|
2009-01-02 20:45:40 +00:00
|
|
|
firstData = false;
|
2008-12-16 05:07:36 +00:00
|
|
|
} else if (token.isToken()) {
|
2009-01-03 05:48:26 +00:00
|
|
|
if (!firstData)
|
|
|
|
styledText.append(" "); //$NON-NLS-1$
|
2008-12-16 05:07:36 +00:00
|
|
|
int caretOffset = styledText.getCharCount();
|
|
|
|
styledText.append(token.getTokenString());
|
|
|
|
StyleRange styleRange = new StyleRange();
|
|
|
|
styleRange.start = caretOffset;
|
|
|
|
styleRange.length = token.getTokenString().length();
|
|
|
|
//styleRange.fontStyle = SWT.BOLD;
|
|
|
|
styleRange.foreground = getBlueColor();
|
|
|
|
styledText.setStyleRange(styleRange);
|
2009-01-02 20:45:40 +00:00
|
|
|
firstData = false;
|
|
|
|
if (token.isIndenter()) {
|
|
|
|
nestLevels ++; }
|
|
|
|
else if (token.isOutdenter()) {
|
|
|
|
nestLevels --; }
|
2008-12-16 05:07:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-02 20:45:40 +00:00
|
|
|
}
|