Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b3f1d8eeff9345bfafab921eb3e65af2f9fa8383 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************************
 * Copyright (c) 2006, 2014 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Wind Rvier Systems - added support for columns (bug 235646)
 *     Ralf M Petter <ralf.petter@gmail.com> - (bug 470536)
 *******************************************************************************/
package org.eclipse.debug.internal.ui.model.elements;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.model.IErrorReportingExpression;
import org.eclipse.debug.core.model.IExpression;
import org.eclipse.debug.core.model.IValue;
import org.eclipse.debug.core.model.IWatchExpression;
import org.eclipse.debug.internal.ui.DebugUIMessages;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.graphics.RGB;

/**
 * @since 3.3
 */
public class ExpressionLabelProvider extends VariableLabelProvider {

	@Override
	protected RGB getForeground(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException {
		Object element = elementPath.getLastSegment();
        if (element instanceof IErrorReportingExpression) {
            IErrorReportingExpression expression = (IErrorReportingExpression) element;
            if (expression.hasErrors()) {
                if (columnId == null || columnId.equals(IDebugUIConstants.COLUMN_ID_VARIABLE_VALUE)) {
                    return new RGB(255, 0, 0);
                }
            }
        }
		return super.getForeground(elementPath, presentationContext, columnId);
	}

   @Override
protected String getLabel(TreePath elementPath, IPresentationContext context, String columnId) throws CoreException {
       if (columnId == null) {
           return super.getLabel(elementPath, context, columnId);
       } else {
           IExpression expression = (IExpression) elementPath.getLastSegment();
           IValue value = expression.getValue();
           return getColumnText(expression, value, context, columnId);
       }
    }

    /**
     * Returns text for a specific columns for the expression/value.
     *
     * @param expression expression to retrieve text for
     * @param value the value associated with the variable
     * @param context presentation context specifying how to display the text
     * @param columnId the column to get the text for
     * @return the label text
     * @throws CoreException Error while retrieving data from model.
     *
     * @since 3.6
     */
    private String getColumnText(IExpression expression, IValue value, IPresentationContext context, String columnId) throws CoreException {
        if (IDebugUIConstants.COLUMN_ID_VARIABLE_NAME.equals(columnId)) {
            return getExpressionName(expression, context);
        } else if (IDebugUIConstants.COLUMN_ID_VARIABLE_VALUE.equals(columnId)) {
            return getExpressionValueText(expression, value, context);
        } else if (IDebugUIConstants.COLUMN_ID_VARIABLE_TYPE.equals(columnId) ||
        		IDebugUIConstants.COLUMN_ID_VARIABLE_VALUE_TYPE.equals(columnId))
        {
            if (value != null) {
                return getValueTypeName(null, value, context);
            }
        }
        return null;
    }

    /**
     * Returns the expression's text to show in the view's name column.
     *
     * @param expression expression to retrieve text for
     * @param context presentation context specifying how to display the text
     * @return Returns the expression's text to show in the view's name column.
     * @exception CoreException in an error occurs
     * @since 3.6
     */
    protected String getExpressionName(IExpression expression, IPresentationContext context) throws CoreException {
        if (expression instanceof IWatchExpression) {
            return getWatchExpressionName((IWatchExpression) expression, context);
        }
        return expression.getExpressionText();
    }

    /**
     * Returns the watch expression's text to show in the view's name column.
     *
     * @param expression the expression
     * @param context associated presentation context
     * @return Returns the watch expression's text to show in the view's name column.
     * @since 3.6
     */
    private String getWatchExpressionName(IWatchExpression expression, IPresentationContext context) {
        StringBuffer result= new StringBuffer();

        String snippet = expression.getExpressionText().trim();
        StringBuffer snippetBuffer = new StringBuffer();
		if (snippet.length() > 254) {
			snippetBuffer.append(snippet.substring(0, 127));
            snippetBuffer.append(DebugUIMessages.DefaultLabelProvider_0);
			snippetBuffer.append(snippet.substring(snippet.length() - 127));
        } else {
            snippetBuffer.append(snippet);
        }
        snippet = snippetBuffer.toString().replaceAll("[\n\r\t]+", " ");  //$NON-NLS-1$//$NON-NLS-2$

        result.append('"');
        result.append(snippet);
        result.append('"');

        return result.toString();
    }

    /**
     * Returns the expression's value, or a message to show in the value column,
     * if the value is not available.
     *
     * @param expression expression to retrieve text for
     * @param value the value associated with the variable
     * @param context presentation context specifying how to display the text
     * @return string representing the expression's value
     * @throws CoreException Error while retrieving data from model.
     *
     * @since 3.6
     */
    protected String getExpressionValueText(IExpression expression, IValue value, IPresentationContext context) throws CoreException {
        if (expression instanceof IWatchExpression) {
            IWatchExpression watchExpression = (IWatchExpression)expression;
            StringBuffer result = new StringBuffer();

			if (watchExpression.isPending() && value == null) {
                result.append(DebugUIMessages.DefaultLabelProvider_12);
            } else if (watchExpression.hasErrors()) {
                result.append(DebugUIMessages.DefaultLabelProvider_13);
            } else if (value != null) {
                result.append( getValueText(null, value, context) );
            }
            if (!watchExpression.isEnabled()) {
                result.append(DebugUIMessages.DefaultLabelProvider_15);
            }

            return result.toString();
        }

        if (value != null) {
            return getValueText(null, value, context);
        }
        return null;
    }

}

Back to the top