blob: f8618ed17af2478b6e6352a70fda969a65511777 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2003, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - Initial API and implementation
*******************************************************************************/
package org.eclipse.wst.internet.monitor.ui.internal.viewers;
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.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wst.internet.monitor.ui.internal.Messages;
import org.eclipse.wst.internet.monitor.ui.internal.provisional.ContentViewer;
/**
* An image viewer.
*/
public class ImageViewer extends ContentViewer {
protected Label messageLabel;
protected byte[] content;
/** (non-Javadoc)
* @see ContentViewer#init(Composite)
*/
public void init(Composite parent) {
messageLabel = new Label(parent, SWT.NONE);
messageLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
messageLabel.setText(Messages.imageViewInvalid);
}
/** (non-Javadoc)
* @see ContentViewer#setContent(byte[])
*/
public void setContent(byte[] b) {
content = b;
if (b == null || b.length == 0) {
messageLabel.setImage(null);
} else {
byte cr = '\r';
byte lf = '\n';
int trimFront = 0;
int trimBack = 0;
int len = b.length - 1;
while (b[trimFront] == cr || b[trimFront] == lf)
trimFront++;
while (b[len - trimBack] == cr || b[len - trimBack] == lf)
trimBack++;
if (trimFront + trimBack > 0) {
byte[] temp = b;
b = new byte[temp.length - trimBack - trimFront];
for (int i = trimFront; i < temp.length - trimBack; i++) {
b[i - trimFront] = temp[i];
}
}
try {
ImageData imgD = new ImageData(new ByteArrayInputStream(b));
Image img = new Image(null, imgD);
messageLabel.setImage(img);
} catch(Exception e) {
messageLabel.setImage(null);
}
}
Composite parent = messageLabel.getParent();
messageLabel.setFont(parent.getFont());
parent.layout(true);
}
/**
* @see ContentViewer#getContent()
*/
public byte[] getContent() {
return content;
}
/** (non-Javadoc)
* @see ContentViewer#dispose()
*/
public void dispose() {
messageLabel.dispose();
messageLabel = null;
content = null;
}
}