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 package com.android.ide.eclipse.ddms.views;
17 
18 import com.android.ddmlib.logcat.LogCatMessage;
19 import com.android.ddmuilib.logcat.ILogCatMessageSelectionListener;
20 import com.android.ddmuilib.logcat.LogCatPanel;
21 import com.android.ddmuilib.logcat.LogCatStackTraceParser;
22 import com.android.ide.eclipse.ddms.DdmsPlugin;
23 import com.android.ide.eclipse.ddms.JavaSourceRevealer;
24 import com.android.ide.eclipse.ddms.i18n.Messages;
25 import com.android.ide.eclipse.ddms.preferences.PreferenceInitializer;
26 
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.swt.dnd.Clipboard;
30 import org.eclipse.swt.layout.FillLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.ui.IActionBars;
33 import org.eclipse.ui.actions.ActionFactory;
34 
35 public class LogCatView extends SelectionDependentViewPart {
36     /** LogCatView ID as defined in plugin.xml. */
37     public static final String ID = "com.android.ide.eclipse.ddms.views.LogCatView"; //$NON-NLS-1$
38 
39     /** Switch perspective when a Java file is opened from logcat view. */
40     public static final boolean DEFAULT_SWITCH_PERSPECTIVE = true;
41 
42     /** Target perspective to open when a Java file is opened from logcat view. */
43     public static final String DEFAULT_PERSPECTIVE_ID =
44             "org.eclipse.jdt.ui.JavaPerspective"; //$NON-NLS-1$
45 
46     private LogCatPanel mLogCatPanel;
47     private LogCatStackTraceParser mStackTraceParser = new LogCatStackTraceParser();
48 
49     private Clipboard mClipboard;
50 
51     @Override
createPartControl(Composite parent)52     public void createPartControl(Composite parent) {
53         parent.setLayout(new FillLayout());
54 
55         IPreferenceStore prefStore = DdmsPlugin.getDefault().getPreferenceStore();
56         mLogCatPanel = new LogCatPanel(prefStore);
57         mLogCatPanel.createPanel(parent);
58         setSelectionDependentPanel(mLogCatPanel);
59 
60         mLogCatPanel.addLogCatMessageSelectionListener(new ILogCatMessageSelectionListener() {
61             @Override
62             public void messageDoubleClicked(LogCatMessage m) {
63                 onDoubleClick(m);
64             }
65         });
66 
67         mClipboard = new Clipboard(parent.getDisplay());
68         IActionBars actionBars = getViewSite().getActionBars();
69         actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(),
70                 new Action(Messages.LogCatView_Copy) {
71             @Override
72             public void run() {
73                 mLogCatPanel.copySelectionToClipboard(mClipboard);
74             }
75         });
76 
77         actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
78                 new Action(Messages.LogCatView_Select_All) {
79             @Override
80             public void run() {
81                 mLogCatPanel.selectAll();
82             }
83         });
84 
85         actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(),
86                 new Action("Find") {
87             @Override
88             public void run() {
89                 mLogCatPanel.showFindDialog();
90             }
91         });
92     }
93 
94     @Override
setFocus()95     public void setFocus() {
96     }
97 
onDoubleClick(LogCatMessage m)98     private void onDoubleClick(LogCatMessage m) {
99         String msg = m.getMessage();
100         if (!mStackTraceParser.isValidExceptionTrace(msg)) {
101             return;
102         }
103 
104         IPreferenceStore store = DdmsPlugin.getDefault().getPreferenceStore();
105         String perspectiveId = null;
106         if (store.getBoolean(PreferenceInitializer.ATTR_SWITCH_PERSPECTIVE)) {
107             perspectiveId = store.getString(PreferenceInitializer.ATTR_PERSPECTIVE_ID);
108         }
109 
110 
111         String fileName = mStackTraceParser.getFileName(msg);
112         int lineNumber = mStackTraceParser.getLineNumber(msg);
113         String methodName = mStackTraceParser.getMethodName(msg);
114         JavaSourceRevealer.revealMethod(methodName, fileName, lineNumber, perspectiveId);
115     }
116 
selectTransientAppFilter(String appName)117     public void selectTransientAppFilter(String appName) {
118         mLogCatPanel.selectTransientAppFilter(appName);
119     }
120 }
121