2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.ddmuilib.logcat;
19 import com.android.ddmuilib.SelectionDependentPanel;
21 import org.eclipse.jface.viewers.TableViewer;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Table;
29 import org.eclipse.swt.widgets.TableColumn;
32 * LogCatPanel displays a table listing the logcat messages.
34 public final class LogCatPanel extends SelectionDependentPanel
35 implements ILogCatMessageEventListener {
36 /** Width (in characters) at which to wrap messages. SWT Tables do not
37 * auto wrap long text - they simply clip the text.
38 * FIXME: this should be a preference. */
39 private static final int MSG_WRAP_WIDTH = 150;
41 private TableViewer mViewer;
42 private LogCatReceiver mReceiver;
45 * Construct a logcat panel.
46 * @param r source of logcat messages.
48 public LogCatPanel(LogCatReceiver r) {
50 mReceiver.addMessageReceivedEventListener(this);
54 public void deviceSelected() {
56 mReceiver.start(getCurrentDevice());
57 mViewer.setInput(mReceiver.getMessages());
61 public void clientSelected() {
65 protected void postCreation() {
69 protected Control createControl(Composite parent) {
70 GridLayout layout = new GridLayout(2, false);
71 parent.setLayout(layout);
73 createLogcatViewTable(parent);
78 private void createLogcatViewTable(Composite parent) {
79 /* SWT.VIRTUAL style will make the table render faster, but all rows will be
80 * of equal heights which causes wrapped messages to just be clipped. */
81 final Table table = new Table(parent, SWT.FULL_SELECTION);
82 mViewer = new TableViewer(table);
84 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
85 gd.horizontalSpan = 2;
86 mViewer.getTable().setLayoutData(gd);
87 table.getHorizontalBar().setVisible(true);
89 /** Fields to show in the table. */
90 String []properties = {
98 /** Column widths (in px) corresponding to the above fields. */
107 for (int i = 0; i < properties.length; i++) {
108 TableColumn col = new TableColumn(mViewer.getTable(), SWT.NONE, i);
109 col.setWidth(colWidths[i]);
110 col.setText(properties[i]);
113 mViewer.getTable().setLinesVisible(true); /* zebra stripe the table */
114 mViewer.getTable().setHeaderVisible(true);
116 mViewer.setLabelProvider(new LogCatMessageLabelProvider(MSG_WRAP_WIDTH));
117 mViewer.setContentProvider(new LogCatMessageContentProvider());
118 mViewer.setInput(mReceiver.getMessages());
122 public void setFocus() {
126 * Update view whenever a message is received.
127 * Implements {@link ILogCatMessageEventListener#messageReceived()}.
129 public void messageReceived() {
130 Display.getDefault().asyncExec(new Runnable() {
132 if (mViewer.getTable().isDisposed()) {
137 /* if an item has been selected, then don't scroll the table,
138 * otherwise, always display the latest output.
139 * FIXME: this behavior should be controlled via a "scroll lock" button in UI. */
140 if (mViewer.getTable().getSelectionCount() == 0) {
141 mViewer.getTable().setTopIndex(mViewer.getTable().getItemCount() - 1);