OSDN Git Service

android-2.1_r1 snapshot
[android-x86/sdk.git] / eclipse / plugins / com.android.ide.eclipse.ddms / src / com / android / ide / eclipse / ddms / views / LogCatView.java
1 /*
2  * Copyright (C) 2007 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.ide.eclipse.ddms.views;
18
19 import com.android.ide.eclipse.ddms.CommonAction;
20 import com.android.ide.eclipse.ddms.DdmsPlugin;
21 import com.android.ide.eclipse.ddms.ImageLoader;
22 import com.android.ide.eclipse.ddms.preferences.PreferenceInitializer;
23 import com.android.ddmlib.Log.LogLevel;
24 import com.android.ddmuilib.logcat.LogColors;
25 import com.android.ddmuilib.logcat.LogFilter;
26 import com.android.ddmuilib.logcat.LogPanel;
27 import com.android.ddmuilib.logcat.LogPanel.ILogFilterStorageManager;
28
29 import org.eclipse.jface.action.Action;
30 import org.eclipse.jface.action.IAction;
31 import org.eclipse.jface.action.IMenuManager;
32 import org.eclipse.jface.action.IToolBarManager;
33 import org.eclipse.jface.action.Separator;
34 import org.eclipse.swt.dnd.Clipboard;
35 import org.eclipse.swt.graphics.Color;
36 import org.eclipse.swt.graphics.Font;
37 import org.eclipse.swt.graphics.FontData;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.ui.IActionBars;
41 import org.eclipse.ui.actions.ActionFactory;
42
43 import java.util.ArrayList;
44
45 /**
46  * The log cat view displays log output from the current device selection.
47  *
48  */
49 public final class LogCatView extends SelectionDependentViewPart {
50
51     public static final String ID =
52         "com.android.ide.eclipse.ddms.views.LogCatView"; // $NON-NLS-1$
53
54     private static final String PREFS_COL_TIME =
55         DdmsPlugin.PLUGIN_ID + ".logcat.time"; // $NON-NLS-1$
56     private static final String PREFS_COL_LEVEL =
57         DdmsPlugin.PLUGIN_ID + ".logcat.level"; // $NON-NLS-1$
58     private static final String PREFS_COL_PID =
59         DdmsPlugin.PLUGIN_ID + ".logcat.pid"; // $NON-NLS-1$
60     private static final String PREFS_COL_TAG =
61         DdmsPlugin.PLUGIN_ID + ".logcat.tag"; // $NON-NLS-1$
62     private static final String PREFS_COL_MESSAGE =
63         DdmsPlugin.PLUGIN_ID + ".logcat.message"; // $NON-NLS-1$
64
65     private static final String PREFS_FILTERS =
66         DdmsPlugin.PLUGIN_ID + ".logcat.filters"; // $NON-NLS-1$
67
68     private static LogCatView sThis;
69     private LogPanel mLogPanel;
70
71     private CommonAction mCreateFilterAction;
72     private CommonAction mDeleteFilterAction;
73     private CommonAction mEditFilterAction;
74     private CommonAction mExportAction;
75
76     private CommonAction[] mLogLevelActions;
77     private String[] mLogLevelIcons = {
78             "v.png", //$NON-NLS-1S
79             "d.png", //$NON-NLS-1S
80             "i.png", //$NON-NLS-1S
81             "w.png", //$NON-NLS-1S
82             "e.png", //$NON-NLS-1S
83     };
84
85     private Action mClearAction;
86
87     private Clipboard mClipboard;
88
89     /**
90      * An implementation of {@link ILogFilterStorageManager} to bridge to the eclipse preference
91      * store, and saves the log filters.
92      */
93     private final class FilterStorage implements ILogFilterStorageManager {
94
95         public LogFilter[] getFilterFromStore() {
96             String filterPrefs = DdmsPlugin.getDefault().getPreferenceStore().getString(
97                     PREFS_FILTERS);
98
99             // split in a string per filter
100             String[] filters = filterPrefs.split("\\|"); // $NON-NLS-1$
101
102             ArrayList<LogFilter> list =
103                 new ArrayList<LogFilter>(filters.length);
104
105             for (String f : filters) {
106                 if (f.length() > 0) {
107                     LogFilter logFilter = new LogFilter();
108                     if (logFilter.loadFromString(f)) {
109                         list.add(logFilter);
110                     }
111                 }
112             }
113
114             return list.toArray(new LogFilter[list.size()]);
115         }
116
117         public void saveFilters(LogFilter[] filters) {
118             StringBuilder sb = new StringBuilder();
119             for (LogFilter f : filters) {
120                 String filterString = f.toString();
121                 sb.append(filterString);
122                 sb.append('|');
123             }
124
125             DdmsPlugin.getDefault().getPreferenceStore().setValue(PREFS_FILTERS, sb.toString());
126         }
127
128         public boolean requiresDefaultFilter() {
129             return true;
130         }
131     }
132
133     public LogCatView() {
134         sThis = this;
135         LogPanel.PREFS_TIME = PREFS_COL_TIME;
136         LogPanel.PREFS_LEVEL = PREFS_COL_LEVEL;
137         LogPanel.PREFS_PID = PREFS_COL_PID;
138         LogPanel.PREFS_TAG = PREFS_COL_TAG;
139         LogPanel.PREFS_MESSAGE = PREFS_COL_MESSAGE;
140     }
141
142     /**
143      * Returns the singleton instance.
144      */
145     public static LogCatView getInstance() {
146         return sThis;
147     }
148
149     /**
150      * Sets the display font.
151      * @param font The font.
152      */
153     public static void setFont(Font font) {
154         if (sThis != null && sThis.mLogPanel != null) {
155             sThis.mLogPanel.setFont(font);
156         }
157     }
158
159     @Override
160     public void createPartControl(Composite parent) {
161         Display d = parent.getDisplay();
162         LogColors colors = new LogColors();
163
164         ImageLoader loader = DdmsPlugin.getImageLoader();
165
166         colors.infoColor = new Color(d, 0, 127, 0);
167         colors.debugColor = new Color(d, 0, 0, 127);
168         colors.errorColor = new Color(d, 255, 0, 0);
169         colors.warningColor = new Color(d, 255, 127, 0);
170         colors.verboseColor = new Color(d, 0, 0, 0);
171
172         mCreateFilterAction = new CommonAction("Create Filter") {
173             @Override
174             public void run() {
175                 mLogPanel.addFilter();
176             }
177         };
178         mCreateFilterAction.setToolTipText("Create Filter");
179         mCreateFilterAction.setImageDescriptor(loader
180                 .loadDescriptor("add.png")); // $NON-NLS-1$
181
182         mEditFilterAction = new CommonAction("Edit Filter") {
183             @Override
184             public void run() {
185                 mLogPanel.editFilter();
186             }
187         };
188         mEditFilterAction.setToolTipText("Edit Filter");
189         mEditFilterAction.setImageDescriptor(loader
190                 .loadDescriptor("edit.png")); // $NON-NLS-1$
191
192         mDeleteFilterAction = new CommonAction("Delete Filter") {
193             @Override
194             public void run() {
195                 mLogPanel.deleteFilter();
196             }
197         };
198         mDeleteFilterAction.setToolTipText("Delete Filter");
199         mDeleteFilterAction.setImageDescriptor(loader
200                 .loadDescriptor("delete.png")); // $NON-NLS-1$
201
202         mExportAction = new CommonAction("Export Selection As Text...") {
203             @Override
204             public void run() {
205                 mLogPanel.save();
206             }
207         };
208         mExportAction.setToolTipText("Export Selection As Text...");
209         mExportAction.setImageDescriptor(loader.loadDescriptor("save.png")); // $NON-NLS-1$
210
211         LogLevel[] levels = LogLevel.values();
212         mLogLevelActions = new CommonAction[mLogLevelIcons.length];
213         for (int i = 0 ; i < mLogLevelActions.length; i++) {
214             String name = levels[i].getStringValue();
215             mLogLevelActions[i] = new CommonAction(name, IAction.AS_CHECK_BOX) {
216                 @Override
217                 public void run() {
218                     // disable the other actions and record current index
219                     for (int i = 0 ; i < mLogLevelActions.length; i++) {
220                         Action a = mLogLevelActions[i];
221                         if (a == this) {
222                             a.setChecked(true);
223
224                             // set the log level
225                             mLogPanel.setCurrentFilterLogLevel(i+2);
226                         } else {
227                             a.setChecked(false);
228                         }
229                     }
230                 }
231             };
232
233             mLogLevelActions[i].setToolTipText(name);
234             mLogLevelActions[i].setImageDescriptor(loader.loadDescriptor(mLogLevelIcons[i]));
235         }
236
237         mClearAction = new Action("Clear Log") {
238             @Override
239             public void run() {
240                 mLogPanel.clear();
241             }
242         };
243         mClearAction.setImageDescriptor(loader
244                 .loadDescriptor("clear.png")); // $NON-NLS-1$
245
246
247         // now create the log view
248         mLogPanel = new LogPanel(loader, colors, new FilterStorage(), LogPanel.FILTER_MANUAL);
249         mLogPanel.setActions(mDeleteFilterAction, mEditFilterAction, mLogLevelActions);
250
251         // get the font
252         String fontStr = DdmsPlugin.getDefault().getPreferenceStore().getString(
253                 PreferenceInitializer.ATTR_LOGCAT_FONT);
254         if (fontStr != null) {
255             FontData data = new FontData(fontStr);
256
257             if (fontStr != null) {
258                 mLogPanel.setFont(new Font(parent.getDisplay(), data));
259             }
260         }
261
262         mLogPanel.createPanel(parent);
263         setSelectionDependentPanel(mLogPanel);
264
265         // place the actions.
266         placeActions();
267
268         // setup the copy action
269         mClipboard = new Clipboard(d);
270         IActionBars actionBars = getViewSite().getActionBars();
271         actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), new Action("Copy") {
272             @Override
273             public void run() {
274                 mLogPanel.copy(mClipboard);
275             }
276         });
277
278         // setup the select all action
279         actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
280                 new Action("Select All") {
281             @Override
282             public void run() {
283                 mLogPanel.selectAll();
284             }
285         });
286     }
287
288     @Override
289     public void dispose() {
290         mLogPanel.stopLogCat(true);
291         mClipboard.dispose();
292     }
293
294     @Override
295     public void setFocus() {
296         mLogPanel.setFocus();
297     }
298
299     /**
300      * Place the actions in the ui.
301      */
302     private void placeActions() {
303         IActionBars actionBars = getViewSite().getActionBars();
304
305         // first in the menu
306         IMenuManager menuManager = actionBars.getMenuManager();
307         menuManager.add(mCreateFilterAction);
308         menuManager.add(mEditFilterAction);
309         menuManager.add(mDeleteFilterAction);
310         menuManager.add(new Separator());
311         menuManager.add(mClearAction);
312         menuManager.add(new Separator());
313         menuManager.add(mExportAction);
314
315         // and then in the toolbar
316         IToolBarManager toolBarManager = actionBars.getToolBarManager();
317         for (CommonAction a : mLogLevelActions) {
318             toolBarManager.add(a);
319         }
320         toolBarManager.add(new Separator());
321         toolBarManager.add(mCreateFilterAction);
322         toolBarManager.add(mEditFilterAction);
323         toolBarManager.add(mDeleteFilterAction);
324         toolBarManager.add(new Separator());
325         toolBarManager.add(mClearAction);
326     }
327  }
328