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;
20 import com.android.ddmuilib.TableHelper;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.viewers.TableViewer;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Table;
33 * LogCatPanel displays a table listing the logcat messages.
35 public final class LogCatPanel extends SelectionDependentPanel
36 implements ILogCatMessageEventListener {
37 /** Width (in characters) at which to wrap messages. SWT Tables do not
38 * auto wrap long text - they simply clip the text.
39 * FIXME: this should be a preference. */
40 private static final int MSG_WRAP_WIDTH = 150;
42 private TableViewer mViewer;
43 private LogCatReceiver mReceiver;
44 private IPreferenceStore mPrefStore;
47 * Construct a logcat panel.
48 * @param r source of logcat messages.
49 * @param prefStore preference store where UI preferences will be saved
51 public LogCatPanel(LogCatReceiver r, IPreferenceStore prefStore) {
53 mPrefStore = prefStore;
54 mReceiver.addMessageReceivedEventListener(this);
58 public void deviceSelected() {
60 mReceiver.start(getCurrentDevice());
61 mViewer.setInput(mReceiver.getMessages());
65 public void clientSelected() {
69 protected void postCreation() {
73 protected Control createControl(Composite parent) {
74 GridLayout layout = new GridLayout(2, false);
75 parent.setLayout(layout);
77 createLogcatViewTable(parent);
82 private void createLogcatViewTable(Composite parent) {
83 /* SWT.VIRTUAL style will make the table render faster, but all rows will be
84 * of equal heights which causes wrapped messages to just be clipped. */
85 final Table table = new Table(parent, SWT.FULL_SELECTION);
86 mViewer = new TableViewer(table);
88 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
89 gd.horizontalSpan = 2;
90 mViewer.getTable().setLayoutData(gd);
91 table.getHorizontalBar().setVisible(true);
93 /** Columns to show in the table. */
94 String[] properties = {
102 /** The sampleText for each column is used to determine the default widths
103 * for each column. The contents do not matter, only their lengths are needed. */
104 String[] sampleText = {
106 " 00-00 00:00:00.0000 ",
109 " Log Message field should be pretty long by default.",
112 for (int i = 0; i < properties.length; i++) {
113 TableHelper.createTableColumn(mViewer.getTable(),
114 properties[i], /* Column title */
115 SWT.LEFT, /* Column Style */
116 sampleText[i], /* String to compute default col width */
117 getPreferenceKey(properties[i]), /* Preference Store key for this column */
121 mViewer.getTable().setLinesVisible(true); /* zebra stripe the table */
122 mViewer.getTable().setHeaderVisible(true);
124 mViewer.setLabelProvider(new LogCatMessageLabelProvider(MSG_WRAP_WIDTH));
125 mViewer.setContentProvider(new LogCatMessageContentProvider());
126 mViewer.setInput(mReceiver.getMessages());
129 private String getPreferenceKey(String field) {
130 return "logcat.view.colsize." + field;
134 public void setFocus() {
138 * Update view whenever a message is received.
139 * Implements {@link ILogCatMessageEventListener#messageReceived()}.
141 public void messageReceived() {
142 Display.getDefault().asyncExec(new Runnable() {
144 if (mViewer.getTable().isDisposed()) {
149 /* if an item has been selected, then don't scroll the table,
150 * otherwise, always display the latest output.
151 * FIXME: this behavior should be controlled via a "scroll lock" button in UI. */
152 if (mViewer.getTable().getSelectionCount() == 0) {
153 mViewer.getTable().setTopIndex(mViewer.getTable().getItemCount() - 1);