inital plugin from webtools project
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.ui / src / net / sourceforge / phpdt / monitor / ui / internal / viewers / ImageViewer.java
1 /**********************************************************************
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.monitor.ui.internal.viewers;
12
13 import java.io.ByteArrayInputStream;
14
15 import net.sourceforge.phpdt.monitor.ui.IContentViewer;
16 import net.sourceforge.phpdt.monitor.ui.internal.MonitorUIPlugin;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.graphics.ImageData;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Label;
25
26 /**
27  * An image viewer.
28  */
29 public class ImageViewer implements IContentViewer {
30         protected Composite rootComp;
31         protected Composite viewerComp;
32         protected Label messageLabel;
33
34         /* (non-Javadoc)
35          * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#init(Composite)
36          */
37         public void init(Composite parent) {
38                 rootComp = parent;
39                 
40                 viewerComp = new Composite(parent, SWT.NONE);
41                 GridLayout layout = new GridLayout();
42                 layout.numColumns = 1;
43                 layout.marginHeight = 0;
44                 layout.marginWidth = 0;
45                 viewerComp.setLayout(layout);
46                 GridData data = new GridData(GridData.FILL_BOTH);
47                 viewerComp.setLayoutData(data);
48
49                 messageLabel = new Label(viewerComp, SWT.NONE);
50                 messageLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
51         }
52         
53         /* (non-Javadoc)
54          * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#setContent()
55          */
56         public void setContent(byte[] b) {
57                 if (b == null || b.length == 0) {
58                         messageLabel.setText("<" + MonitorUIPlugin.getResource("%imageViewInvalid") + ">");
59                 } else {
60                         byte cr = '\r';
61                         byte lf = '\n';
62                         int trimFront = 0;
63                         int trimBack = 0;
64                         int len = b.length - 1;
65                         while(b[trimFront] == cr || b[trimFront] == lf)
66                                 trimFront++;
67                         while(b[len - trimBack] == cr || b[len - trimBack] == lf)
68                                 trimBack++;
69                                 
70                         if (trimFront + trimBack > 0) {
71                                 byte[] temp = b;
72                                 b = new byte[temp.length - trimBack - trimFront];
73                                 for(int i = trimFront; i < temp.length - trimBack; i++) {
74                                         b[i - trimFront] = temp[i];
75                                 }
76                         }
77                         try {
78                                 ImageData imgD = new ImageData(new ByteArrayInputStream(b));
79                                 Image img = new Image(null, imgD);
80                                 messageLabel.setImage(img);
81                         } catch(Exception e) {
82                                 messageLabel.setText("<" + MonitorUIPlugin.getResource("%imageViewInvalid") + ">");
83                         }
84                 }
85                 
86                 viewerComp.layout(true);
87         }
88         
89         /* (non-Javadoc)
90          * @see net.sourceforge.phpdt.monitor.ui.IContentViewer#dispose()
91          */
92         public void dispose() {
93                 viewerComp.dispose();
94         }
95 }