Added FilterAdapters to handle display of specific content.

This commit is contained in:
Robert Greene 2004-06-16 03:20:52 +00:00
parent 81c804fad3
commit 3725062579
6 changed files with 468 additions and 0 deletions

View File

@ -0,0 +1,104 @@
/*
* AppleCommander - An Apple ][ image utility.
* Copyright (C) 2002-2004 by Robert Greene
* robgreene at users.sourceforge.net
*
* 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.ApplesoftToken;
import com.webcodepro.applecommander.util.ApplesoftTokenizer;
/**
* Provides a view of a syntax-colored Applesoft program listing.
*
* @author Rob Greene
*/
public class ApplesoftFilterAdapter extends FilterAdapter {
private StyledText styledText;
public ApplesoftFilterAdapter(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()));
}
protected void createStyledText() {
styledText = new StyledText(getComposite(), SWT.NONE);
styledText.setForeground(getBlackColor());
styledText.setFont(getCourierFont());
styledText.setEditable(false);
ApplesoftTokenizer tokenizer = new ApplesoftTokenizer(getFileEntry());
boolean firstLine = true;
while (tokenizer.hasMoreTokens()) {
ApplesoftToken token = tokenizer.getNextToken();
if (token == null) {
continue; // should be end of program...
} else if (token.isLineNumber()) {
if (firstLine) {
firstLine = false;
} else {
styledText.append("\n");
}
styledText.append(Integer.toString(token.getLineNumber()));
styledText.append(" ");
} else if (token.isCommandSeparator() || token.isExpressionSeparator()) {
styledText.append(token.getStringValue());
} else if (token.isEndOfCommand()) {
styledText.append("\n");
} else if (token.isString()) {
int caretOffset = styledText.getCharCount();
styledText.append(token.getStringValue());
StyleRange styleRange = new StyleRange();
styleRange.start = caretOffset;
styleRange.length = token.getStringValue().length();
styleRange.foreground = getGreenColor();
styledText.setStyleRange(styleRange);
} else if (token.isToken()) {
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);
}
}
}
}

View File

@ -0,0 +1,139 @@
/*
* AppleCommander - An Apple ][ image utility.
* Copyright (C) 2002-2004 by Robert Greene
* robgreene at users.sourceforge.net
*
* 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.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import com.webcodepro.applecommander.storage.FileEntry;
import com.webcodepro.applecommander.storage.FileFilter;
import com.webcodepro.applecommander.ui.swt.FileViewerWindow;
import com.webcodepro.applecommander.ui.swt.util.contentadapter.ContentTypeAdapter;
/**
* Represents a visual adapter for a FileFilter. Generally, the display method is the
* only variance between the many FileFilters available.
*
* @author Rob Greene
*/
public abstract class FilterAdapter {
private final FileViewerWindow window;
private Image image;
private String text;
private String toolTipText;
private ToolItem toolItem;
private boolean nativeSelected = true;
private boolean hexSelected = false;
private boolean dumpSelected = false;
public FilterAdapter(FileViewerWindow window, String text, String toolTipText,
Image image) {
this.text = text;
this.window = window;
this.toolTipText = toolTipText;
this.image = image;
setNativeSelected();
}
public abstract void display();
public ToolItem create(ToolBar toolBar) {
if (toolItem == null) {
toolItem = new ToolItem(toolBar, SWT.RADIO);
toolItem.setImage(getImage());
toolItem.setText(getText());
toolItem.setToolTipText(getToolTipText());
toolItem.setSelection(false);
toolItem.addSelectionListener(new SelectionAdapter () {
public void widgetSelected(SelectionEvent e) {
display();
window.setFilterToolItemSelection(nativeSelected, hexSelected, dumpSelected);
}
});
}
return toolItem;
}
protected Image getImage() {
return image;
}
protected String getText() {
return text;
}
protected String getToolTipText() {
return toolTipText;
}
protected FileFilter getFileFilter() {
return window.getFileFilter();
}
protected FileEntry getFileEntry() {
return window.getFileEntry();
}
protected ToolItem getCopyToolItem() {
return window.getCopyToolItem();
}
protected ScrolledComposite getComposite() {
return window.getComposite();
}
protected void setContentTypeAdapter(ContentTypeAdapter adapter) {
window.setContentTypeAdapter(adapter);
}
protected Font getCourierFont() {
return window.getCourierFont();
}
protected Listener getToolbarCommandHandler() {
return window.createToolbarCommandHandler();
}
protected ToolItem getToolItem() {
return toolItem;
}
protected Color getBlackColor() {
return window.getBlackColor();
}
protected Color getGreenColor() {
return window.getGreenColor();
}
protected Color getBlueColor() {
return window.getBlueColor();
}
public void setDumpSelected() {
nativeSelected = false;
hexSelected = false;
dumpSelected = true;
}
public void setHexSelected() {
nativeSelected = false;
hexSelected = true;
dumpSelected = false;
}
public void setNativeSelected() {
nativeSelected = true;
hexSelected = false;
dumpSelected = false;
}
}

View File

@ -0,0 +1,73 @@
/*
* AppleCommander - An Apple ][ image utility.
* Copyright (C) 2002-2004 by Robert Greene
* robgreene at users.sourceforge.net
*
* 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 java.io.ByteArrayInputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import com.webcodepro.applecommander.ui.swt.FileViewerWindow;
import com.webcodepro.applecommander.ui.swt.util.ImageCanvas;
import com.webcodepro.applecommander.ui.swt.util.contentadapter.ImageCanvasAdapter;
/**
* Provides a view of an Apple graphic image.
*
* @author Rob Greene
*/
public class GraphicsFilterAdapter extends FilterAdapter {
private Image image;
public GraphicsFilterAdapter(FileViewerWindow window, String text, String toolTipText, Image image) {
super(window, text, toolTipText, image);
}
public void display() {
getCopyToolItem().setEnabled(false);
if (image == null) {
byte[] imageBytes = getFileFilter().filter(getFileEntry());
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ImageLoader imageLoader = new ImageLoader();
ImageData[] imageData = imageLoader.load(inputStream);
image = new Image(getComposite().getDisplay(), imageData[0]);
}
GridLayout layout = new GridLayout();
getComposite().setLayout(layout);
GridData gridData = new GridData();
gridData.widthHint = image.getImageData().width;
gridData.heightHint = image.getImageData().height;
ImageCanvas imageCanvas = new ImageCanvas(getComposite(), SWT.NONE, image, gridData);
getComposite().setContent(imageCanvas);
getComposite().setExpandHorizontal(true);
getComposite().setExpandVertical(true);
getComposite().setMinWidth(image.getImageData().width);
getComposite().setMinHeight(image.getImageData().height);
setContentTypeAdapter(new ImageCanvasAdapter(imageCanvas, getFileEntry().getFilename()));
}
}

View File

@ -0,0 +1,40 @@
/*
* AppleCommander - An Apple ][ image utility.
* Copyright (C) 2002-2004 by Robert Greene
* robgreene at users.sourceforge.net
*
* 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.graphics.Image;
import com.webcodepro.applecommander.storage.filters.HexDumpFileFilter;
import com.webcodepro.applecommander.ui.swt.FileViewerWindow;
/**
* Provides a view of a hex dump of the program as seen when loaded from the disk.
*
* @author Rob Greene
*/
public class HexFilterAdapter extends TextFilterAdapter {
public HexFilterAdapter(FileViewerWindow window, String text, String toolTipText, Image image) {
super(window, text, toolTipText, image);
}
protected String createTextContent() {
return new String(new HexDumpFileFilter().filter(getFileEntry()));
}
}

View File

@ -0,0 +1,41 @@
/*
* AppleCommander - An Apple ][ image utility.
* Copyright (C) 2002-2004 by Robert Greene
* robgreene at users.sourceforge.net
*
* 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.graphics.Image;
import com.webcodepro.applecommander.ui.swt.FileViewerWindow;
import com.webcodepro.applecommander.util.AppleUtil;
/**
* Provides a view of a raw hex dump from the diskette. This includes any operating
* system-specific data (ie, for DOS it may include starting address and/or length).
*
* @author Rob Greene
*/
public class RawDumpFilterAdapter extends TextFilterAdapter {
public RawDumpFilterAdapter(FileViewerWindow window, String text, String toolTipText, Image image) {
super(window, text, toolTipText, image);
}
protected String createTextContent() {
return AppleUtil.getHexDump(getFileEntry().getFormattedDisk().getFileData(getFileEntry()));
}
}

View File

@ -0,0 +1,71 @@
/*
* AppleCommander - An Apple ][ image utility.
* Copyright (C) 2002-2004 by Robert Greene
* robgreene at users.sourceforge.net
*
* 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.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;
/**
* Provides a view of a simple text file.
*
* @author Rob Greene
*/
public class TextFilterAdapter extends FilterAdapter {
private String textContent;
public TextFilterAdapter(FileViewerWindow window, String text, String toolTipText, Image image) {
super(window, text, toolTipText, image);
}
public void display() {
if (textContent == null) {
textContent = createTextContent();
}
getCopyToolItem().setEnabled(true);
createTextWidget(textContent);
}
protected String createTextContent() {
return new String(getFileFilter().filter(getFileEntry()));
}
protected void createTextWidget(String textContents) {
StyledText styledText = new StyledText(getComposite(), SWT.NONE);
styledText.setText(textContents);
styledText.setFont(getCourierFont());
styledText.setEditable(false);
//styledText.setWordWrap(true); // seems to throw size out-of-whack
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());
getToolItem().setSelection(true);
setContentTypeAdapter(new StyledTextAdapter(styledText, getFileEntry().getFilename()));
}
}