OSDN Git Service

Merge "Add eclipse version to usage stat ping."
[android-x86/sdk.git] / ddms / libs / ddmuilib / src / com / android / ddmuilib / logcat / LogCatPanel.java
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.ddmuilib.logcat;
18
19 import com.android.ddmuilib.SelectionDependentPanel;
20
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;
30
31 /**
32  * LogCatPanel displays a table listing the logcat messages.
33  */
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;
40
41     private TableViewer mViewer;
42     private LogCatReceiver mReceiver;
43
44     /**
45      * Construct a logcat panel.
46      * @param r source of logcat messages.
47      */
48     public LogCatPanel(LogCatReceiver r) {
49         mReceiver = r;
50         mReceiver.addMessageReceivedEventListener(this);
51     }
52
53     @Override
54     public void deviceSelected() {
55         mReceiver.stop();
56         mReceiver.start(getCurrentDevice());
57         mViewer.setInput(mReceiver.getMessages());
58     }
59
60     @Override
61     public void clientSelected() {
62     }
63
64     @Override
65     protected void postCreation() {
66     }
67
68     @Override
69     protected Control createControl(Composite parent) {
70         GridLayout layout = new GridLayout(2, false);
71         parent.setLayout(layout);
72
73         createLogcatViewTable(parent);
74
75         return null;
76     }
77
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);
83
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);
88
89         /** Fields to show in the table. */
90         String []properties = {
91                 "Level",
92                 "Time",
93                 "PID",
94                 "Tag",
95                 "Text",
96         };
97
98         /** Column widths (in px) corresponding to the above fields. */
99         int []colWidths = {
100                 50,
101                 150,
102                 50,
103                 200,
104                 1000,
105         };
106
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]);
111         }
112
113         mViewer.getTable().setLinesVisible(true); /* zebra stripe the table */
114         mViewer.getTable().setHeaderVisible(true);
115
116         mViewer.setLabelProvider(new LogCatMessageLabelProvider(MSG_WRAP_WIDTH));
117         mViewer.setContentProvider(new LogCatMessageContentProvider());
118         mViewer.setInput(mReceiver.getMessages());
119     }
120
121     @Override
122     public void setFocus() {
123     }
124
125     /**
126      * Update view whenever a message is received.
127      * Implements {@link ILogCatMessageEventListener#messageReceived()}.
128      */
129     public void messageReceived() {
130         Display.getDefault().asyncExec(new Runnable() {
131             public void run() {
132                 if (mViewer.getTable().isDisposed()) {
133                     return;
134                 }
135                 mViewer.refresh();
136
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);
142                 }
143             }
144         });
145     }
146 }