• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ddmlib.Log.LogLevel;
20 
21 import org.eclipse.jface.viewers.ColumnLabelProvider;
22 import org.eclipse.jface.viewers.ViewerCell;
23 import org.eclipse.swt.graphics.Color;
24 import org.eclipse.swt.graphics.Font;
25 import org.eclipse.swt.graphics.Point;
26 
27 /**
28  * A JFace Column label provider for the LogCat log messages. It expects elements of type
29  * {@link LogCatMessage}.
30  */
31 public final class LogCatMessageLabelProvider extends ColumnLabelProvider {
32     private static final int INDEX_LOGLEVEL = 0;
33     private static final int INDEX_LOGTIME = 1;
34     private static final int INDEX_PID = 2;
35     private static final int INDEX_APPNAME = 3;
36     private static final int INDEX_TAG = 4;
37     private static final int INDEX_TEXT = 5;
38 
39     /* Default Colors for different log levels. */
40     private static final Color INFO_MSG_COLOR =    new Color(null, 0, 127, 0);
41     private static final Color DEBUG_MSG_COLOR =   new Color(null, 0, 0, 127);
42     private static final Color ERROR_MSG_COLOR =   new Color(null, 255, 0, 0);
43     private static final Color WARN_MSG_COLOR =    new Color(null, 255, 127, 0);
44     private static final Color VERBOSE_MSG_COLOR = new Color(null, 0, 0, 0);
45 
46     /** Amount of pixels to shift the tooltip by. */
47     private static final Point LOGCAT_TOOLTIP_SHIFT = new Point(10, 10);
48 
49     private Font mLogFont;
50     private int mWrapWidth = 100;
51 
52     /**
53      * Construct a column label provider for the logcat table.
54      * @param font default font to use
55      */
LogCatMessageLabelProvider(Font font)56     public LogCatMessageLabelProvider(Font font) {
57         mLogFont = font;
58     }
59 
getCellText(LogCatMessage m, int columnIndex)60     private String getCellText(LogCatMessage m, int columnIndex) {
61         switch (columnIndex) {
62             case INDEX_LOGLEVEL:
63                 return Character.toString(m.getLogLevel().getPriorityLetter());
64             case INDEX_LOGTIME:
65                 return m.getTime();
66             case INDEX_PID:
67                 return m.getPid();
68             case INDEX_APPNAME:
69                 return m.getAppName();
70             case INDEX_TAG:
71                 return m.getTag();
72             case INDEX_TEXT:
73                 return m.getMessage();
74             default:
75                 return "";
76         }
77     }
78 
79     @Override
update(ViewerCell cell)80     public void update(ViewerCell cell) {
81         Object element = cell.getElement();
82         if (!(element instanceof LogCatMessage)) {
83             return;
84         }
85         LogCatMessage m = (LogCatMessage) element;
86 
87         String text = getCellText(m, cell.getColumnIndex());
88         cell.setText(text);
89         cell.setFont(mLogFont);
90         cell.setForeground(getForegroundColor(m));
91     }
92 
getForegroundColor(LogCatMessage m)93     private Color getForegroundColor(LogCatMessage m) {
94         LogLevel l = m.getLogLevel();
95 
96         if (l.equals(LogLevel.VERBOSE)) {
97             return VERBOSE_MSG_COLOR;
98         } else if (l.equals(LogLevel.INFO)) {
99             return INFO_MSG_COLOR;
100         } else if (l.equals(LogLevel.DEBUG)) {
101             return DEBUG_MSG_COLOR;
102         } else if (l.equals(LogLevel.ERROR)) {
103             return ERROR_MSG_COLOR;
104         } else if (l.equals(LogLevel.WARN)) {
105             return WARN_MSG_COLOR;
106         }
107 
108         return null;
109     }
110 
setFont(Font preferredFont)111     public void setFont(Font preferredFont) {
112         if (mLogFont != null) {
113             mLogFont.dispose();
114         }
115 
116         mLogFont = preferredFont;
117     }
118 
setMinimumLengthForToolTips(int widthInChars)119     public void setMinimumLengthForToolTips(int widthInChars) {
120         mWrapWidth  = widthInChars;
121     }
122 
123     /**
124      * Obtain the tool tip to show for a particular logcat message.
125      * We display a tool tip only for messages longer than the width set by
126      * {@link #setMinimumLengthForToolTips(int)}.
127      */
128     @Override
getToolTipText(Object element)129     public String getToolTipText(Object element) {
130         String text = element.toString();
131         if (text.length() > mWrapWidth) {
132             return text;
133         } else {
134             return null;
135         }
136     }
137 
138     @Override
getToolTipShift(Object object)139     public Point getToolTipShift(Object object) {
140         // The only reason we override this method is that the default shift amounts
141         // don't seem to work on OS X Lion.
142         return LOGCAT_TOOLTIP_SHIFT;
143     }
144 }
145