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.*;
18 import net.sourceforge.phpdt.monitor.ui.internal.*;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.IToolBarManager;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.ISelectionChangedListener;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.SelectionChangedEvent;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.jface.viewers.TreeViewer;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.custom.SashForm;
35 import org.eclipse.swt.events.ModifyEvent;
36 import org.eclipse.swt.events.ModifyListener;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Combo;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Tree;
44 import org.eclipse.ui.IViewPart;
45 import org.eclipse.ui.IWorkbench;
46 import org.eclipse.ui.IWorkbenchPage;
47 import org.eclipse.ui.IWorkbenchWindow;
48 import org.eclipse.ui.PlatformUI;
49 import org.eclipse.ui.help.WorkbenchHelp;
50 import org.eclipse.ui.part.ViewPart;
52 * View of TCP/IP activity.
54 public class MonitorView extends ViewPart {
56 protected TreeViewer treeViewer;
57 protected MonitorTreeContentProvider contentProvider;
59 protected IRequestListener listener;
60 protected IViewerManager vm;
61 protected List requestViewers;
62 protected List responseViewers;
64 protected static SimpleDateFormat format = new SimpleDateFormat(MonitorUIPlugin.getResource("%viewDateFormat"));
65 protected static final String VIEW_ID = "net.sourceforge.phpeclipse.monitor.core.view";
66 protected static final String DEFAULT_VIEWER = "net.sourceforge.phpeclipse.monitor.core.viewers.byteviewer";
68 protected IAction httpHeaderAction;
70 protected static MonitorView view;
73 * MonitorView constructor comment.
75 public MonitorView() {
80 protected void addListener() {
81 listener = new IRequestListener() {
82 public void requestAdded(final IRequest rr) {
83 Display.getDefault().asyncExec(new Runnable() {
85 Integer in = new Integer(rr.getLocalPort());
86 treeViewer.add(MonitorTreeContentProvider.ROOT, in);
87 treeViewer.add(in, rr);
88 treeViewer.setSelection(new StructuredSelection(rr), true);
93 public void requestChanged(final IRequest rr) {
94 Display.getDefault().asyncExec(new Runnable() {
96 IStructuredSelection sel = (IStructuredSelection) treeViewer.getSelection();
98 treeViewer.refresh(rr);
100 treeViewer.setSelection(sel);
105 public void requestRemoved(final IRequest rr) {
106 Display.getDefault().asyncExec(new Runnable() {
108 treeViewer.remove(rr);
114 MonitorCore.addRequestListener(listener);
120 protected void clear() {
121 Display.getDefault().asyncExec(new Runnable() {
123 treeViewer.setSelection(null);
124 treeViewer.setInput(MonitorTreeContentProvider.ROOT);
129 protected void setSelection(IRequest request) {
130 if (treeViewer != null)
131 treeViewer.setSelection(new StructuredSelection(request));
136 * Returns the inner component in a desktop part.
138 public void createPartControl(Composite parent) {
139 SashForm sashFparent = new SashForm(parent, SWT.VERTICAL);
140 GridLayout layout = new GridLayout();
141 layout.numColumns = 2;
142 layout.horizontalSpacing = 4;
143 layout.verticalSpacing = 4;
144 sashFparent.setLayout(layout);
145 sashFparent.setLayoutData(new GridData(GridData.FILL_BOTH));
146 WorkbenchHelp.setHelp(sashFparent, ContextIds.VIEW);
149 Composite treePanel = new Composite(sashFparent, SWT.NONE);
150 layout = new GridLayout();
151 layout.numColumns = 2;
152 layout.marginHeight = 0;
153 layout.marginWidth = 0;
154 treePanel.setLayout(layout);
155 GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
156 data.heightHint = 110;
157 data.horizontalSpan = 2;
158 treePanel.setLayoutData(data);
160 tree = new Tree(treePanel, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE);
161 data = new GridData(GridData.FILL_BOTH);
162 //data.widthHint = 120;
163 tree.setLayoutData(data);
164 treeViewer = new TreeViewer(tree);
165 contentProvider = new MonitorTreeContentProvider();
166 treeViewer.setContentProvider(contentProvider);
167 treeViewer.setInput(MonitorTreeContentProvider.ROOT);
168 treeViewer.setLabelProvider(new TreeLabelProvider());
169 WorkbenchHelp.setHelp(tree, ContextIds.VIEW_TREE);
171 Composite detailsPanel = new Composite(treePanel, SWT.NONE);
172 layout = new GridLayout();
173 layout.numColumns = 1;
174 layout.marginHeight = 2;
175 layout.marginWidth = 0;
176 detailsPanel.setLayout(layout);
177 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
178 data.widthHint = 200;
179 detailsPanel.setLayoutData(data);
181 final Label label = new Label(detailsPanel, SWT.NONE);
182 label.setText(MonitorUIPlugin.getResource("%viewTime", ""));
183 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
185 final Label label2 = new Label(detailsPanel, SWT.NONE);
186 label2.setText(MonitorUIPlugin.getResource("%viewResponseTime", ""));
187 label2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
189 final Label label3 = new Label(detailsPanel, SWT.NONE);
190 label3.setText(MonitorUIPlugin.getResource("%viewType", ""));
191 label3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
193 // create center and right panels
194 SashForm sashFchild = new SashForm(sashFparent, SWT.NONE);
195 layout = new GridLayout();
196 layout.numColumns = 2;
197 layout.horizontalSpacing = 2;
198 layout.verticalSpacing = 4;
199 sashFchild.setLayout(layout);
200 sashFparent.setWeights(new int[] { 30, 70 });
203 Composite request = new Composite(sashFchild, SWT.NONE);
204 layout = new GridLayout();
205 layout.verticalSpacing = 3;
206 layout.marginHeight = 2;
207 layout.marginWidth = 0;
208 request.setLayout(layout);
209 request.setLayoutData(new GridData(GridData.FILL_BOTH));
211 Composite requestHeader = new Composite(request, SWT.NONE);
212 layout = new GridLayout();
213 layout.verticalSpacing = 0;
214 layout.numColumns = 2;
215 layout.marginHeight = 0;
216 layout.marginWidth = 2;
217 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
218 data.heightHint = 30;
219 requestHeader.setLayout(layout);
220 requestHeader.setLayoutData(data);
222 Composite requestHeaderLeft = new Composite(requestHeader, SWT.NONE);
223 layout = new GridLayout();
224 layout.verticalSpacing = 2;
225 layout.marginHeight = 0;
226 layout.marginWidth = 0;
227 data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
228 data.heightHint = 30;
229 requestHeaderLeft.setLayout(layout);
230 requestHeaderLeft.setLayoutData(data);
232 Label empty1 = new Label(requestHeaderLeft, SWT.NONE);
233 empty1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
235 final Label requestLabel = new Label(requestHeaderLeft, SWT.NONE);
236 requestLabel.setText(MonitorUIPlugin.getResource("%viewRequest", ""));
237 requestLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_END));
239 final Label requestSizeLabel = new Label(requestHeaderLeft, SWT.NONE);
240 requestSizeLabel.setText(MonitorUIPlugin.getResource("%viewSize", ""));
241 requestSizeLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_END));
243 Composite requestHeaderRight = new Composite(requestHeader, SWT.NONE);
244 layout = new GridLayout();
245 layout.verticalSpacing = 0;
246 layout.marginHeight = 0;
247 layout.marginWidth = 0;
248 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.FILL_VERTICAL);
249 data.heightHint = 30;
250 requestHeaderRight.setLayout(layout);
251 requestHeaderRight.setLayoutData(data);
253 Label empty2 = new Label(requestHeaderRight, SWT.NONE);
254 empty2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
256 Combo requestViewerCombo = new Combo(requestHeaderRight, SWT.DROP_DOWN | SWT.READ_ONLY);
257 requestViewerCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_END));
260 Composite response = new Composite(sashFchild, SWT.NONE);
261 layout = new GridLayout();
262 layout.verticalSpacing = 3;
263 layout.marginHeight = 2;
264 layout.marginWidth = 0;
265 response.setLayout(layout);
266 response.setLayoutData(new GridData(GridData.FILL_BOTH));
268 Composite responseHeader = new Composite(response, SWT.NONE);
269 layout = new GridLayout();
270 layout.verticalSpacing = 0;
271 layout.numColumns = 2;
272 layout.marginHeight = 0;
273 layout.marginWidth = 2;
274 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
275 data.heightHint = 30;
276 responseHeader.setLayout(layout);
277 responseHeader.setLayoutData(data);
279 Composite responseHeaderLeft = new Composite(responseHeader, SWT.NONE);
280 layout = new GridLayout();
281 layout.verticalSpacing = 2;
282 layout.marginHeight = 0;
283 layout.marginWidth = 0;
284 data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
285 data.heightHint = 30;
286 responseHeaderLeft.setLayout(layout);
287 responseHeaderLeft.setLayoutData(data);
289 Label empty3 = new Label(responseHeaderLeft, SWT.NONE);
290 empty3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
292 final Label responseLabel = new Label(responseHeaderLeft, SWT.NONE);
293 responseLabel.setText(MonitorUIPlugin.getResource("%viewResponse", ""));
294 responseLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
296 final Label responseSizeLabel = new Label(responseHeaderLeft, SWT.NONE);
297 responseSizeLabel.setText(MonitorUIPlugin.getResource("%viewSize", ""));
298 responseSizeLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_END));
300 Composite responseHeaderRight = new Composite(responseHeader, SWT.NONE);
301 layout = new GridLayout();
302 layout.verticalSpacing = 0;
303 layout.marginHeight = 0;
304 layout.marginWidth = 0;
305 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.FILL_VERTICAL);
306 data.heightHint = 30;
307 responseHeaderRight.setLayout(layout);
308 responseHeaderRight.setLayoutData(data);
310 Label empty4 = new Label(responseHeaderRight, SWT.NONE);
311 empty4.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
313 Combo responseViewerCombo = new Combo(responseHeaderRight, SWT.DROP_DOWN | SWT.READ_ONLY);
314 responseViewerCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_END));
317 vm = new ViewerManager(request, request, response, response);
318 requestViewers = vm.getRequestViewers();
319 responseViewers = vm.getResponseViewers();
321 //Set up the viewer combo boxes
322 Iterator iterator = requestViewers.iterator();
324 while(iterator.hasNext()) {
325 IConfigurationElement element = (IConfigurationElement) iterator.next();
326 requestViewerCombo.add(element.getAttribute("label"), ctr);
327 if (element.getAttribute("id").equals(DEFAULT_VIEWER)) {
328 requestViewerCombo.select(ctr);
329 vm.setRequestViewer(element);
333 requestViewerCombo.addModifyListener(new ModifyListener() {
334 public void modifyText(ModifyEvent arg0) {
335 Combo rvCombo = (Combo) arg0.getSource();
336 vm.setRequestViewer((IConfigurationElement) requestViewers.get(rvCombo.getSelectionIndex()));
339 requestHeader.layout(true);
341 iterator = responseViewers.iterator();
343 while(iterator.hasNext()) {
344 IConfigurationElement element = (IConfigurationElement) iterator.next();
345 responseViewerCombo.add(element.getAttribute("label"), ctr);
346 if(element.getAttribute("id").equals(DEFAULT_VIEWER)) {
347 responseViewerCombo.select(ctr);
348 vm.setResponseViewer(element);
352 responseViewerCombo.addModifyListener(new ModifyListener() {
353 public void modifyText(ModifyEvent arg0) {
354 Combo rvCombo = (Combo) arg0.getSource();
355 vm.setResponseViewer((IConfigurationElement) requestViewers.get(rvCombo.getSelectionIndex()));
358 responseHeader.layout(true);
360 // selection listener
361 treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
362 public void selectionChanged(SelectionChangedEvent event) {
363 ISelection selection = event.getSelection();
366 if (selection != null && !selection.isEmpty()) {
367 StructuredSelection sel = (StructuredSelection) selection;
368 Object obj = sel.iterator().next();
369 if (obj instanceof IRequest)
370 req = (IRequest) obj;
374 label.setText(MonitorUIPlugin.getResource("%viewTime", format.format(req.getDate())));
376 if (req.getResponseTime() == -1)
377 label2.setText(MonitorUIPlugin.getResource("%viewResponseTime", ""));
379 String time = MonitorUIPlugin.getResource("%viewResponseTimeFormat", req.getResponseTime() + "");
380 label2.setText(MonitorUIPlugin.getResource("%viewResponseTime", time));
382 label3.setText(MonitorUIPlugin.getResource("%viewType", req.getType().getName()));
384 // request information
385 requestLabel.setText(MonitorUIPlugin.getResource("%viewRequest", "localhost:" + req.getLocalPort()));
386 requestSizeLabel.setText(getSizeString(req.getRequest(IRequest.CONTENT), req.getRequest(IRequest.ALL)));
388 // response information
389 responseLabel.setText(MonitorUIPlugin.getResource("%viewResponse", req.getRemoteHost() + ":" + req.getRemotePort()));
390 responseSizeLabel.setText(getSizeString(req.getResponse(IRequest.CONTENT), req.getResponse(IRequest.ALL)));
394 label.setText(MonitorUIPlugin.getResource("%viewTime", ""));
395 label2.setText(MonitorUIPlugin.getResource("%viewResponseTime", ""));
396 requestLabel.setText(MonitorUIPlugin.getResource("%viewRequest", ""));
397 requestSizeLabel.setText(MonitorUIPlugin.getResource("%viewSize", ""));
398 responseLabel.setText(MonitorUIPlugin.getResource("%viewResponse", ""));
399 responseSizeLabel.setText(MonitorUIPlugin.getResource("%viewSize", ""));
405 treeViewer.expandToLevel(2);
412 protected String getSizeString(byte[] a, byte[] b) {
419 String size = MonitorUIPlugin.getResource("%viewSizeFormat", new Object[] { aa, bb});
420 return MonitorUIPlugin.getResource("%viewSize", size);
423 public void dispose() {
426 MonitorCore.removeRequestListener(listener);
432 public void initializeActions() {
433 final IAction sortByResponseTimeAction = new Action() {
435 Display.getDefault().asyncExec(new Runnable() {
437 boolean b = contentProvider.getSortByResponseTime();
438 contentProvider.setSortByResponseTime(!b);
439 treeViewer.refresh();
445 sortByResponseTimeAction.setChecked(false);
446 sortByResponseTimeAction.setToolTipText(MonitorUIPlugin.getResource("%actionSortByResponseTime"));
447 sortByResponseTimeAction.setImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_ELCL_SORT_RESPONSE_TIME));
448 sortByResponseTimeAction.setHoverImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_CLCL_SORT_RESPONSE_TIME));
449 sortByResponseTimeAction.setDisabledImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_DLCL_SORT_RESPONSE_TIME));
451 IAction clearAction = new Action() {
453 MonitorCore.removeAllRequests();
456 clearAction.setToolTipText(MonitorUIPlugin.getResource("%actionClearToolTip"));
457 clearAction.setImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_ELCL_CLEAR));
458 clearAction.setHoverImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_CLCL_CLEAR));
459 clearAction.setDisabledImageDescriptor(MonitorUIPlugin.getImageDescriptor(MonitorUIPlugin.IMG_DLCL_CLEAR));
461 httpHeaderAction = new Action() {
463 Display.getDefault().asyncExec(new Runnable() {
465 boolean b = vm.getDisplayHeaderInfo();
466 vm.setDisplayHeaderInfo(!b);
472 httpHeaderAction.setChecked(vm.getDisplayHeaderInfo());
473 httpHeaderAction.setText(MonitorUIPlugin.getResource("%actionShowHeader"));
475 IAction preferenceAction = new Action() {
477 IWorkbench workbench = PlatformUI.getWorkbench();
478 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
480 MonitorPrefrencesDialog monitorPrefDialog = new MonitorPrefrencesDialog(workbenchWindow.getShell());
481 if (monitorPrefDialog.open() == Window.CANCEL)
485 preferenceAction.setText(MonitorUIPlugin.getResource("%actionProperties"));
488 IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
489 tbm.add(sortByResponseTimeAction);
490 tbm.add(clearAction);
492 IContentFilter[] filters = MonitorCore.getContentFilters();
493 IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
494 menuManager.add(httpHeaderAction);
495 int size = filters.length;
496 for (int i = 0; i < size; i++) {
497 FilterAction action = new FilterAction(vm, filters[i]);
498 menuManager.add(action);
500 menuManager.add(preferenceAction);
506 public static void open(final IRequest request) {
507 Display.getDefault().asyncExec(new Runnable() {
510 IWorkbench workbench = MonitorUIPlugin.getInstance().getWorkbench();
511 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
512 //if (workbenchWindow == null)
513 // workbenchWindow = workbench.getWorkbenchWindows()[0];
515 IWorkbenchPage page = workbenchWindow.getActivePage();
517 IViewPart view2 = page.findView(VIEW_ID);
520 page.bringToTop(view2);
522 page.showView(VIEW_ID);
525 view.setSelection(request);
526 } catch (Exception e) {
527 Trace.trace(Trace.SEVERE, "Error opening TCP/IP view", e);
536 public void setFocus() {