OSDN Git Service

Save/Restore the width of all columns in the LogCatPanel table.
[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 import com.android.ddmuilib.TableHelper;
21
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;
31
32 /**
33  * LogCatPanel displays a table listing the logcat messages.
34  */
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;
41
42     private TableViewer mViewer;
43     private LogCatReceiver mReceiver;
44     private IPreferenceStore mPrefStore;
45
46     /**
47      * Construct a logcat panel.
48      * @param r source of logcat messages.
49      * @param prefStore preference store where UI preferences will be saved
50      */
51     public LogCatPanel(LogCatReceiver r, IPreferenceStore prefStore) {
52         mReceiver = r;
53         mPrefStore = prefStore;
54         mReceiver.addMessageReceivedEventListener(this);
55     }
56
57     @Override
58     public void deviceSelected() {
59         mReceiver.stop();
60         mReceiver.start(getCurrentDevice());
61         mViewer.setInput(mReceiver.getMessages());
62     }
63
64     @Override
65     public void clientSelected() {
66     }
67
68     @Override
69     protected void postCreation() {
70     }
71
72     @Override
73     protected Control createControl(Composite parent) {
74         GridLayout layout = new GridLayout(2, false);
75         parent.setLayout(layout);
76
77         createLogcatViewTable(parent);
78
79         return null;
80     }
81
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);
87
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);
92
93         /** Columns to show in the table. */
94         String[] properties = {
95                 "Level",
96                 "Time",
97                 "PID",
98                 "Tag",
99                 "Text",
100         };
101
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 = {
105                 "    II",
106                 "    00-00 00:00:00.0000 ",
107                 "    0000",
108                 "    SampleTagText",
109                 "    Log Message field should be pretty long by default.",
110         };
111
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 */
118                     mPrefStore);
119         }
120
121         mViewer.getTable().setLinesVisible(true); /* zebra stripe the table */
122         mViewer.getTable().setHeaderVisible(true);
123
124         mViewer.setLabelProvider(new LogCatMessageLabelProvider(MSG_WRAP_WIDTH));
125         mViewer.setContentProvider(new LogCatMessageContentProvider());
126         mViewer.setInput(mReceiver.getMessages());
127     }
128
129     private String getPreferenceKey(String field) {
130         return "logcat.view.colsize." + field;
131     }
132
133     @Override
134     public void setFocus() {
135     }
136
137     /**
138      * Update view whenever a message is received.
139      * Implements {@link ILogCatMessageEventListener#messageReceived()}.
140      */
141     public void messageReceived() {
142         Display.getDefault().asyncExec(new Runnable() {
143             public void run() {
144                 if (mViewer.getTable().isDisposed()) {
145                     return;
146                 }
147                 mViewer.refresh();
148
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);
154                 }
155             }
156         });
157     }
158 }