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
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.monitor.ui.internal.view;
13 import java.text.SimpleDateFormat;
14 import java.util.Iterator;
15 import java.util.List;
17 import net.sourceforge.phpdt.monitor.core.IContentFilter;
18 import net.sourceforge.phpdt.monitor.core.IRequest;
19 import net.sourceforge.phpdt.monitor.core.IRequestListener;
20 import net.sourceforge.phpdt.monitor.core.MonitorCore;
21 import net.sourceforge.phpdt.monitor.ui.internal.ContextIds;
22 import net.sourceforge.phpdt.monitor.ui.internal.MonitorPrefrencesDialog;
23 import net.sourceforge.phpdt.monitor.ui.internal.MonitorUIPlugin;
24 import net.sourceforge.phpdt.monitor.ui.internal.Trace;
26 import org.eclipse.core.runtime.IConfigurationElement;
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.action.IToolBarManager;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.ISelectionChangedListener;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.SelectionChangedEvent;
35 import org.eclipse.jface.viewers.StructuredSelection;
36 import org.eclipse.jface.viewers.TreeViewer;
37 import org.eclipse.jface.window.Window;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.custom.SashForm;
40 import org.eclipse.swt.events.ModifyEvent;
41 import org.eclipse.swt.events.ModifyListener;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Combo;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Display;
47 import org.eclipse.swt.widgets.Label;
48 import org.eclipse.swt.widgets.Tree;
49 import org.eclipse.ui.IViewPart;
50 import org.eclipse.ui.IWorkbench;
51 import org.eclipse.ui.IWorkbenchPage;
52 import org.eclipse.ui.IWorkbenchWindow;
53 import org.eclipse.ui.PlatformUI;
54 import org.eclipse.ui.help.WorkbenchHelp;
55 import org.eclipse.ui.part.ViewPart;
57 * View of TCP/IP activity.
59 public class MonitorView extends ViewPart {
61 protected TreeViewer treeViewer;
62 protected MonitorTreeContentProvider contentProvider;
64 protected IRequestListener listener;
65 protected IViewerManager vm;
66 protected List requestViewers;
67 protected List responseViewers;
69 protected static SimpleDateFormat format = new SimpleDateFormat(MonitorUIPlugin.getResource("%viewDateFormat"));
70 protected static final String VIEW_ID = "net.sourceforge.phpeclipse.monitor.core.view";
71 protected static final String DEFAULT_VIEWER = "net.sourceforge.phpeclipse.monitor.core.viewers.byteviewer";
73 protected IAction httpHeaderAction;
75 protected static MonitorView view;
78 * MonitorView constructor comment.
80 public MonitorView() {
85 protected void addListener() {
86 listener = new IRequestListener() {
87 public void requestAdded(final IRequest rr) {
88 Display.getDefault().asyncExec(new Runnable() {
90 Integer in = new Integer(rr.getLocalPort());
91 treeViewer.add(MonitorTreeContentProvider.ROOT, in);
92 treeViewer.add(in, rr);
93 treeViewer.setSelection(new StructuredSelection(rr), true);
98 public void requestChanged(final IRequest rr) {
99 Display.getDefault().asyncExec(new Runnable() {
101 IStructuredSelection sel = (IStructuredSelection) treeViewer.getSelection();
103 treeViewer.refresh(rr);
105 treeViewer.setSelection(sel);
110 public void requestRemoved(final IRequest rr) {
111 Display.getDefault().asyncExec(new Runnable() {
113 treeViewer.remove(rr);
119 MonitorCore.addRequestListener(listener);
125 protected void clear() {
126 Display.getDefault().asyncExec(new Runnable() {
128 treeViewer.setSelection(null);
129 treeViewer.setInput(MonitorTreeContentProvider.ROOT);
134 protected void setSelection(IRequest request) {
135 if (treeViewer != null)
136 treeViewer.setSelection(new StructuredSelection(request));
141 * Returns the inner component in a desktop part.
143 public void createPartControl(Composite parent) {
144 SashForm sashFparent = new SashForm(parent, SWT.VERTICAL);
145 GridLayout layout = new GridLayout();
146 layout.numColumns = 2;
147 layout.horizontalSpacing = 4;
148 layout.verticalSpacing = 4;
149 sashFparent.setLayout(layout);
150 sashFparent.setLayoutData(new GridData(GridData.FILL_BOTH));
151 WorkbenchHelp.setHelp(sashFparent, ContextIds.VIEW);
154 Composite treePanel = new Composite(sashFparent, SWT.NONE);
155 layout = new GridLayout();
156 layout.numColumns = 2;
157 layout.marginHeight = 0;
158 layout.marginWidth = 0;
159 treePanel.setLayout(layout);
160 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
161 data.heightHint = 110;
162 data.horizontalSpan = 2;
163 treePanel.setLayoutData(data);
165 tree = new Tree(treePanel, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE);
166 data = new GridData(GridData.FILL_BOTH);
167 //data.widthHint = 120;
168 tree.setLayoutData(data);
169 treeViewer = new TreeViewer(tree);
170 contentProvider = new MonitorTreeContentProvider();
171 treeViewer.setContentProvider(contentProvider);
172 treeViewer.setInput(MonitorTreeContentProvider.ROOT);
173 treeViewer.setLabelProvider(new TreeLabelProvider());
174 WorkbenchHelp.setHelp(tree, ContextIds.VIEW_TREE);
176 Composite detailsPanel = new Composite(treePanel, SWT.NONE);
177 layout = new GridLayout();
178 layout.numColumns = 1;
179 layout.marginHeight = 2;
180 layout.marginWidth = 0;
181 detailsPanel.setLayout(layout);
182 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
183 data.widthHint = 200;
184 detailsPanel.setLayoutData(data);
186 final Label label = new Label(detailsPanel, SWT.NONE);
187 label.setText(MonitorUIPlugin.getResource("%viewTime", ""));
188 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
190 final Label label2 = new Label(detailsPanel, SWT.NONE);
191 label2.setText(MonitorUIPlugin.getResource("%viewResponseTime", ""));
192 label2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
194 final Label label3 = new Label(detailsPanel, SWT.NONE);
195 label3.setText(MonitorUIPlugin.getResource("%viewType", ""));
196 label3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
198 // create center and right panels
199 SashForm sashFchild = new SashForm(sashFparent, SWT.NONE);
200 layout = new GridLayout();
201 layout.numColumns = 2;
202 layout.horizontalSpacing = 2;
203 layout.verticalSpacing = 4;
204 sashFchild.setLayout(layout);
205 sashFparent.setWeights(new int[] { 30, 70 });
208 Composite request = new Composite(sashFchild, SWT.NONE);
209 layout = new GridLayout();
210 layout.verticalSpacing = 3;
211 layout.marginHeight = 2;
212 layout.marginWidth = 0;
213 request.setLayout(layout);
214 request.setLayoutData(new GridData(GridData.FILL_BOTH));
216 Composite requestHeader = new Composite(request, SWT.NONE);
217 layout = new GridLayout();
218 layout.verticalSpacing = 0;
219 layout.numColumns = 2;
220 layout.marginHeight = 0;
221 layout.marginWidth = 2;
222 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
223 data.heightHint = 30;
224 requestHeader.setLayout(layout);
225 requestHeader.setLayoutData(data);
227 Composite requestHeaderLeft = new Composite(requestHeader, SWT.NONE);
228 layout = new GridLayout();
229 layout.verticalSpacing = 2;
230 layout.marginHeight = 0;
231 layout.marginWidth = 0;
232 data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
233 data.heightHint = 30;
234 requestHeaderLeft.setLayout(layout);
235 requestHeaderLeft.setLayoutData(data);
237 Label empty1 = new Label(requestHeaderLeft, SWT.NONE);
238 empty1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
240 final Label requestLabel = new Label(requestHeaderLeft, SWT.NONE);
241 requestLabel.setText(MonitorUIPlugin.getResource("%viewRequest", ""));
242 requestLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_END));
244 final Label requestSizeLabel = new Label(requestHeaderLeft, SWT.NONE);
245 requestSizeLabel.setText(MonitorUIPlugin.getResource("%viewSize", ""));
246 requestSizeLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_END));
248 Composite requestHeaderRight = new Composite(requestHeader, SWT.NONE);
249 layout = new GridLayout();
250 layout.verticalSpacing = 0;
251 layout.marginHeight = 0;
252 layout.marginWidth = 0;
253 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.FILL_VERTICAL);
254 data.heightHint = 30;
255 requestHeaderRight.setLayout(layout);
256 requestHeaderRight.setLayoutData(data);
258 Label empty2 = new Label(requestHeaderRight, SWT.NONE);
259 empty2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
261 Combo requestViewerCombo = new Combo(requestHeaderRight, SWT.DROP_DOWN | SWT.READ_ONLY);
262 requestViewerCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_END));
265 Composite response = new Composite(sashFchild, SWT.NONE);
266 layout = new GridLayout();
267 layout.verticalSpacing = 3;
268 layout.marginHeight = 2;
269 layout.marginWidth = 0;
270 response.setLayout(layout);
271 response.setLayoutData(new GridData(GridData.FILL_BOTH));
273 Composite responseHeader = new Composite(response, SWT.NONE);
274 layout = new GridLayout();
275 layout.verticalSpacing = 0;
276 layout.numColumns = 2;
277 layout.marginHeight = 0;
278 layout.marginWidth = 2;
279 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
280 data.heightHint = 30;
281 responseHeader.setLayout(layout);
282 responseHeader.setLayoutData(data);
284 Composite responseHeaderLeft = new Composite(responseHeader, SWT.NONE);
285 layout = new GridLayout();
286 layout.verticalSpacing = 2;
287 layout.marginHeight = 0;
288 layout.marginWidth = 0;
289 data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
290 data.heightHint = 30;
291 responseHeaderLeft.setLayout(layout);
292 responseHeaderLeft.setLayoutData(data);
294 Label empty3 = new Label(responseHeaderLeft, SWT.NONE);
295 empty3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
297 final Label responseLabel = new Label(responseHeaderLeft, SWT.NONE);
298 responseLabel.setText(MonitorUIPlugin.getResource("%viewResponse", ""));
299 responseLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
301 final Label responseSizeLabel = new Label(responseHeaderLeft, SWT.NONE);
302 responseSizeLabel.setText(MonitorUIPlugin.getResource("%viewSize", ""));
303 responseSizeLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_END));
305 Composite responseHeaderRight = new Composite(responseHeader, SWT.NONE);
306 layout = new GridLayout();
307 layout.verticalSpacing = 0;
308 layout.marginHeight = 0;
309 layout.marginWidth = 0;
310 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.FILL_VERTICAL);
311 data.heightHint = 30;
312 responseHeaderRight.setLayout(layout);
313 responseHeaderRight.setLayoutData(data);
315 Label empty4 = new Label(responseHeaderRight, SWT.NONE);
316 empty4.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
318 Combo responseViewerCombo = new Combo(responseHeaderRight, SWT.DROP_DOWN | SWT.READ_ONLY);
319 responseViewerCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_END));
322 vm = new ViewerManager(request, request, response, response);
323 requestViewers = vm.getRequestViewers();
324 responseViewers = vm.getResponseViewers();
326 //Set up the viewer combo boxes
327 Iterator iterator = requestViewers.iterator();
329 while(iterator.hasNext()) {
330 IConfigurationElement element = (IConfigurationElement) iterator.next();
331 requestViewerCombo.add(element.getAttribute("label"), ctr);
332 if (element.getAttribute("id").equals(DEFAULT_VIEWER)) {
333 requestViewerCombo.select(ctr);
334 vm.setRequestViewer(element);
338 requestViewerCombo.addModifyListener(new ModifyListener() {
339 public void modifyText(ModifyEvent arg0) {
340 Combo rvCombo = (Combo) arg0.getSource();
341 vm.setRequestViewer((IConfigurationElement) requestViewers.get(rvCombo.getSelectionIndex()));
344 requestHeader.layout(true);
346 iterator = responseViewers.iterator();
348 while(iterator.hasNext()) {
349 IConfigurationElement element = (IConfigurationElement) iterator.next();
350 responseViewerCombo.add(element.getAttribute("label"), ctr);
351 if(element.getAttribute("id").equals(DEFAULT_VIEWER)) {
352 responseViewerCombo.select(ctr);
353 vm.setResponseViewer(element);
357 responseViewerCombo.addModifyListener(new ModifyListener() {
358 public void modifyText(ModifyEvent arg0) {
359 Combo rvCombo = (Combo) arg0.getSource();
360 vm.setResponseViewer((IConfigurationElement) requestViewers.get(rvCombo.getSelectionIndex()));
363 responseHeader.layout(true);
365 // selection listener
366 treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
367 public void selectionChanged(SelectionChangedEvent event) {
368 ISelection selection = event.getSelection();
371 if (selection != null && !selection.isEmpty()) {
372 StructuredSelection sel = (StructuredSelection) selection;
373 Object obj = sel.iterator().next();
374 if (obj instanceof IRequest)
375 req = (IRequest) obj;
379 label.setText(MonitorUIPlugin.getResource("%viewTime", format.format(req.getDate())));
381 if (req.getResponseTime() == -1)
382 label2.setText(MonitorUIPlugin.getResource("%viewResponseTime", ""));
384 String time = MonitorUIPlugin.getResource("%viewResponseTimeFormat", req.getResponseTime() + "");
385 label2.setText(MonitorUIPlugin.getResource("%viewResponseTime", time));
387 label3.setText(MonitorUIPlugin.getResource("%viewType", req.getType().getName()));
389 // request information
390 requestLabel.setText(MonitorUIPlugin.getResource("%viewRequest", "localhost:" + req.getLocalPort()));
391 requestSizeLabel.setText(getSizeString(req.getRequest(IRequest.CONTENT), req.getRequest(IRequest.ALL)));
393 // response information
394 responseLabel.setText(MonitorUIPlugin.getResource("%viewResponse", req.getRemoteHost() + ":" + req.getRemotePort()));
395 responseSizeLabel.setText(getSizeString(req.getResponse(IRequest.CONTENT), req.getResponse(IRequest.ALL)));
399 label.setText(MonitorUIPlugin.getResource("%viewTime", ""));
400 label2.setText(MonitorUIPlugin.getResource("%viewResponseTime", ""));
401 requestLabel.setText(MonitorUIPlugin.getResource("%viewRequest", ""));
402 requestSizeLabel.setText(MonitorUIPlugin.getResource("%viewSize", ""));
403 responseLabel.setText(MonitorUIPlugin.getResource("%viewResponse", ""));
404 responseSizeLabel.setText(MonitorUIPlugin.getResource("%viewSize", ""));
410 treeViewer.expandToLevel(2);
417 protected String getSizeString(byte[] a, byte[] b) {
424 String size = MonitorUIPlugin.getResource("%viewSizeFormat", new Object[] { aa, bb});
425 return MonitorUIPlugin.getResource("%viewSize", size);
428 public void dispose() {
431 MonitorCore.removeRequestListener(listener);
437 public void initializeActions() {
438 final IAction sortByResponseTimeAction = new Action() {
440 Display.getDefault().asyncExec(new Runnable() {
442 boolean b = contentProvider.getSortByResponseTime();
443 contentProvider.setSortByResponseTime(!b);
444 treeViewer.refresh();
450 sortByResponseTimeAction.setChecked(false);
451 sortByResponseTimeAction.setToolTipText(MonitorUIPlugin.getResource("%actionSortByResponseTime"));
452 sortByResponseTimeAction.setImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_ELCL_SORT_RESPONSE_TIME));
453 sortByResponseTimeAction.setHoverImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_CLCL_SORT_RESPONSE_TIME));
454 sortByResponseTimeAction.setDisabledImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_DLCL_SORT_RESPONSE_TIME));
456 IAction clearAction = new Action() {
458 MonitorCore.removeAllRequests();
461 clearAction.setToolTipText(MonitorUIPlugin.getResource("%actionClearToolTip"));
462 clearAction.setImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_ELCL_CLEAR));
463 clearAction.setHoverImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_CLCL_CLEAR));
464 clearAction.setDisabledImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_DLCL_CLEAR));
466 httpHeaderAction = new Action() {
468 Display.getDefault().asyncExec(new Runnable() {
470 boolean b = vm.getDisplayHeaderInfo();
471 vm.setDisplayHeaderInfo(!b);
477 httpHeaderAction.setChecked(vm.getDisplayHeaderInfo());
478 httpHeaderAction.setText(MonitorUIPlugin.getResource("%actionShowHeader"));
480 IAction preferenceAction = new Action() {
482 IWorkbench workbench = PlatformUI.getWorkbench();
483 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
485 MonitorPrefrencesDialog monitorPrefDialog = new MonitorPrefrencesDialog(workbenchWindow.getShell());
486 if (monitorPrefDialog.open() == Window.CANCEL)
490 preferenceAction.setText(MonitorUIPlugin.getResource("%actionProperties"));
493 IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
494 tbm.add(sortByResponseTimeAction);
495 tbm.add(clearAction);
497 IContentFilter[] filters = MonitorCore.getContentFilters();
498 IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
499 menuManager.add(httpHeaderAction);
500 int size = filters.length;
501 for (int i = 0; i < size; i++) {
502 FilterAction action = new FilterAction(vm, filters[i]);
503 menuManager.add(action);
505 menuManager.add(preferenceAction);
511 public static void open(final IRequest request) {
512 Display.getDefault().asyncExec(new Runnable() {
515 IWorkbench workbench = MonitorUIPlugin.getInstance().getWorkbench();
516 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
517 //if (workbenchWindow == null)
518 // workbenchWindow = workbench.getWorkbenchWindows()[0];
520 IWorkbenchPage page = workbenchWindow.getActivePage();
522 IViewPart view2 = page.findView(VIEW_ID);
525 page.bringToTop(view2);
527 page.showView(VIEW_ID);
530 view.setSelection(request);
531 } catch (Exception e) {
532 Trace.trace(Trace.SEVERE, "Error opening TCP/IP view", e);
541 public void setFocus() {