Added error handler for invalid graphics files.

This commit is contained in:
Robert Greene 2004-06-18 02:14:23 +00:00
parent 98a561630f
commit 8fc1b7fad3

View File

@ -27,10 +27,12 @@ import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader; import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import com.webcodepro.applecommander.ui.swt.FileViewerWindow; import com.webcodepro.applecommander.ui.swt.FileViewerWindow;
import com.webcodepro.applecommander.ui.swt.util.ImageCanvas; import com.webcodepro.applecommander.ui.swt.util.ImageCanvas;
import com.webcodepro.applecommander.ui.swt.util.contentadapter.ImageCanvasAdapter; import com.webcodepro.applecommander.ui.swt.util.contentadapter.ImageCanvasAdapter;
import com.webcodepro.applecommander.ui.swt.util.contentadapter.NoActionContentTypeAdapter;
/** /**
* Provides a view of an Apple graphic image. * Provides a view of an Apple graphic image.
@ -39,6 +41,7 @@ import com.webcodepro.applecommander.ui.swt.util.contentadapter.ImageCanvasAdapt
*/ */
public class GraphicsFilterAdapter extends FilterAdapter { public class GraphicsFilterAdapter extends FilterAdapter {
private Image image; private Image image;
private boolean error = false;
public GraphicsFilterAdapter(FileViewerWindow window, String text, String toolTipText, Image image) { public GraphicsFilterAdapter(FileViewerWindow window, String text, String toolTipText, Image image) {
super(window, text, toolTipText, image); super(window, text, toolTipText, image);
@ -47,14 +50,19 @@ public class GraphicsFilterAdapter extends FilterAdapter {
public void display() { public void display() {
getCopyToolItem().setEnabled(false); getCopyToolItem().setEnabled(false);
if (image == null) { if (image == null && error == false) {
try {
byte[] imageBytes = getFileFilter().filter(getFileEntry()); byte[] imageBytes = getFileFilter().filter(getFileEntry());
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes); ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ImageLoader imageLoader = new ImageLoader(); ImageLoader imageLoader = new ImageLoader();
ImageData[] imageData = imageLoader.load(inputStream); ImageData[] imageData = imageLoader.load(inputStream);
image = new Image(getComposite().getDisplay(), imageData[0]); image = new Image(getComposite().getDisplay(), imageData[0]);
} catch (Throwable t) {
error = true;
}
} }
if (!error) {
GridLayout layout = new GridLayout(); GridLayout layout = new GridLayout();
getComposite().setLayout(layout); getComposite().setLayout(layout);
GridData gridData = new GridData(); GridData gridData = new GridData();
@ -66,8 +74,15 @@ public class GraphicsFilterAdapter extends FilterAdapter {
getComposite().setExpandVertical(true); getComposite().setExpandVertical(true);
getComposite().setMinWidth(image.getImageData().width); getComposite().setMinWidth(image.getImageData().width);
getComposite().setMinHeight(image.getImageData().height); getComposite().setMinHeight(image.getImageData().height);
setContentTypeAdapter(new ImageCanvasAdapter(imageCanvas, getFileEntry().getFilename())); setContentTypeAdapter(new ImageCanvasAdapter(imageCanvas, getFileEntry().getFilename()));
} else {
Label label = new Label(getComposite(), SWT.NULL);
label.setText("Unexpected graphic file encountered!");
getComposite().setContent(label);
getComposite().setExpandHorizontal(true);
getComposite().setExpandVertical(true);
setContentTypeAdapter(new NoActionContentTypeAdapter());
}
} }
} }