inital plugin from webtools project
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.ui / src / net / sourceforge / phpdt / monitor / ui / internal / viewers / HeaderViewer.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 net.sourceforge.phpdt.monitor.core.IRequest;
14 import net.sourceforge.phpdt.monitor.core.MonitorCore;
15 import net.sourceforge.phpdt.monitor.ui.internal.ContextIds;
16 import net.sourceforge.phpdt.monitor.ui.internal.MonitorUIPlugin;
17
18 import org.eclipse.jface.resource.JFaceResources;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Text;
26 import org.eclipse.ui.help.WorkbenchHelp;
27
28 /**
29  * An transport (header) viewer.
30  */
31 public class HeaderViewer {
32         protected boolean displayHeader;
33
34         protected Composite headerComp;
35         protected Composite innerComp;
36         protected Composite rootComp;
37
38         protected Label headerLabel;
39         protected Text headerText;
40         protected IRequest rr;
41         protected byte msg;
42         protected GridLayout layout;
43         protected GridData data;
44
45         protected boolean hidden;
46
47         protected static int HEADER_LABEL_SIZE = 15;
48         protected static int HEADER_TEXT_SIZE = 110;
49         public static byte REQUEST_HEADER = 0;
50         public static byte RESPONSE_HEADER = 1;
51
52         public HeaderViewer(Composite parent, byte message) {
53                 rootComp = parent;
54                 displayHeader = true;
55                 hidden = false;
56                 
57                 headerComp = new Composite(parent, SWT.NONE);
58                 layout = new GridLayout();
59                 layout.numColumns = 1;
60                 layout.marginHeight = 0;
61                 layout.marginWidth = 0;
62                 headerComp.setLayout(layout);
63                 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
64                 headerComp.setLayoutData(data);
65
66                 innerComp = new Composite(headerComp, SWT.NONE);
67                 layout = new GridLayout();
68                 layout.numColumns = 1;
69                 layout.marginHeight = 0;
70                 layout.marginWidth = 2;
71                 innerComp.setLayout(layout);
72                 data = new GridData(GridData.FILL_BOTH);
73                 data.heightHint = HEADER_LABEL_SIZE;
74                 innerComp.setLayoutData(data);
75
76                 rr = null;
77                 msg = message;
78
79                 setDisplayHeader(false);
80         }
81         
82         /*public boolean isHidden() {
83                 return hidden;
84         }*/
85         
86         /*public void hideViewer() {
87                 hidden = true;
88                 innerComp.dispose();
89                 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
90                 data.heightHint = 0;
91                 headerComp.setLayoutData(data); 
92                 rootComp.layout(true);
93         }
94         
95         public void showViewer() {
96                 hidden = false;
97                 
98                 innerComp = new Composite(headerComp, SWT.NONE);
99                 layout = new GridLayout();
100                 layout.numColumns = 1;
101                 layout.marginHeight = 0;
102                 layout.marginWidth = 2;
103                 innerComp.setLayout(layout);
104                 data = new GridData(GridData.FILL_BOTH);
105                 data.heightHint = HEADER_LABEL_SIZE;
106                 innerComp.setLayoutData(data);
107                 
108                 displayHeader = true;
109                 setDisplayHeader(false);
110         }*/
111         
112         public void setRequestResponse(IRequest reqresp) {
113                 rr = reqresp;
114                 if (!hidden)
115                         getView();
116         }
117         
118         public void setDisplayHeader(boolean b) {
119                 if (displayHeader != b) {
120                         displayHeader = b;
121                         if (displayHeader) {
122                                 innerComp.dispose();
123
124                                 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
125                                 data.heightHint = HEADER_TEXT_SIZE;
126                                 headerComp.setLayoutData(data);
127                                                                 
128                                 innerComp = new Composite(headerComp, SWT.NONE);
129                                 layout = new GridLayout();
130                                 layout.numColumns = 1;
131                                 layout.marginHeight = 0;
132                                 layout.marginWidth = 0;
133                                 innerComp.setLayout(layout);
134                                 data = new GridData(GridData.FILL_BOTH);
135                                 data.heightHint = HEADER_TEXT_SIZE;
136                                 innerComp.setLayoutData(data);
137                                                                 
138                                 headerText = new Text(innerComp, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL);
139                                 Display display = innerComp.getDisplay();
140                                 headerText.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
141                                 headerText.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
142                                 headerText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
143                                 headerText.setFont(JFaceResources.getTextFont());
144                                 WorkbenchHelp.setHelp(headerText, ContextIds.VIEW_RESPONSE);
145
146                                 rootComp.layout(true);
147                         } else {
148                                 innerComp.dispose();
149
150                                 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
151                                 data.heightHint = HEADER_LABEL_SIZE;
152                                 headerComp.setLayoutData(data);
153                                 
154                                 innerComp = new Composite(headerComp, SWT.NONE);
155                                 layout = new GridLayout();
156                                 layout.numColumns = 1;
157                                 layout.marginHeight = 0;
158                                 layout.marginWidth = 2;
159                                 innerComp.setLayout(layout);
160                                 data = new GridData(GridData.FILL_BOTH);
161                                 data.heightHint = HEADER_LABEL_SIZE;
162                                 innerComp.setLayoutData(data);
163
164                                 headerLabel = new Label(innerComp, SWT.NONE);
165                                 headerLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
166                                 
167                                 rootComp.layout(true);
168                         }
169                 }
170                 getView();
171         }
172
173         /*public boolean getDisplayHeader() {
174                 return displayHeader;
175         }*/
176
177         private void getView() {
178                 String out = "";
179                 if (rr != null) {
180                         if (msg == REQUEST_HEADER) {
181                                 out = MonitorCore.parse(rr.getRequest(IRequest.TRANSPORT));
182                         } else if (msg == RESPONSE_HEADER) {
183                                 out = MonitorCore.parse(rr.getResponse(IRequest.TRANSPORT));
184                         }
185                 }
186                 
187                 if (displayHeader) {
188                         headerText.setText(out);
189                 } else {
190                         String lineSeparator = System.getProperty("line.separator");
191                         int index = out.indexOf(lineSeparator);
192                         if(index > 0)
193                                 headerLabel.setText(MonitorUIPlugin.getResource("%headerLabel") + ": " + out.substring(0, index));
194                         else 
195                                 headerLabel.setText(MonitorUIPlugin.getResource("%headerLabel") + ":  " + out);
196                 }
197         }
198
199         public void dispose() {
200                 headerComp.dispose();
201         }
202 }