KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > elements > adapters > VariableLabelAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.elements.adapters;
12
13 import java.util.Arrays JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.model.IValue;
18 import org.eclipse.debug.core.model.IVariable;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
21 import org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext;
22 import org.eclipse.debug.ui.IDebugUIConstants;
23 import org.eclipse.swt.graphics.RGB;
24 import org.eclipse.ui.IWorkbenchPart;
25
26 /**
27  * Label adapter for variables.
28  *
29  * @since 3.2
30  */

31 public class VariableLabelAdapter extends AsynchronousDebugLabelAdapter {
32
33     /* (non-Javadoc)
34      * @see org.eclipse.debug.internal.ui.elements.adapters.AsynchronousDebugLabelAdapter#getLabels(java.lang.Object, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext)
35      */

36     protected String JavaDoc[] getLabels(Object JavaDoc element, IPresentationContext context) throws CoreException {
37         IWorkbenchPart part = context.getPart();
38         String JavaDoc[] ids = context.getColumns();
39         if (part != null && ids != null) {
40             String JavaDoc viewId = part.getSite().getId();
41             if (IDebugUIConstants.ID_VARIABLE_VIEW.equals(viewId) || IDebugUIConstants.ID_REGISTER_VIEW.equals(viewId)) {
42                 IVariable variable = (IVariable) element;
43                 IValue value = variable.getValue();
44                 String JavaDoc[] columns = new String JavaDoc[ids.length];
45                 for (int i = 0; i < ids.length; i++) {
46                     columns[i] = getColumnText(variable, value, ids[i], context);
47                 }
48                 return columns;
49             }
50         }
51         String JavaDoc[] labels = super.getLabels(element, context);
52         String JavaDoc[] escaped = new String JavaDoc[labels.length];
53         for (int i = 0; i < escaped.length; i++) {
54             escaped[i] = escapeSpecialChars(labels[i]);
55         }
56         return escaped;
57     }
58     
59     /**
60      * Returns the text for the given variable and value for the specified column.
61      *
62      * @param variable
63      * @param value
64      * @param columnId
65      * @return text
66      * @throws CoreException
67      */

68     protected String JavaDoc getColumnText(IVariable variable, IValue value, String JavaDoc columnId, IPresentationContext context) throws CoreException {
69         if (VariableColumnPresentation.COLUMN_VARIABLE_NAME.equals(columnId)) {
70             return getVariableName(variable, context);
71         } else if (VariableColumnPresentation.COLUMN_VARIABLE_TYPE.equals(columnId)) {
72             return getVariableTypeName(variable, context);
73         } else if (VariableColumnPresentation.COLUMN_VARIABLE_VALUE.equals(columnId)) {
74             return getValueText(variable, value, context);
75         } else if (VariableColumnPresentation.COLUMN_VALUE_TYPE.equals(columnId)) {
76             return getValueTypeName(variable, value, context);
77         }
78         return null;
79     }
80     
81     /**
82      * Returns the name of the given variable to display in <code>COLUMN_VARIABLE_NAME</code>.
83      *
84      * @param variable
85      * @return variable name
86      * @throws CoreException
87      */

88     protected String JavaDoc getVariableName(IVariable variable, IPresentationContext context) throws CoreException {
89         return variable.getName();
90     }
91     
92     /**
93      * Returns the type name of the given variable to display in <code>COLUMN_VARIABLE_TYPE</code>.
94      *
95      * @param variable
96      * @return variable type name
97      * @throws CoreException
98      */

99     protected String JavaDoc getVariableTypeName(IVariable variable, IPresentationContext context) throws CoreException {
100         return variable.getReferenceTypeName();
101     }
102     
103     /**
104      * Returns the label for the given value's type to display in <code>COLUMN_VARIABLE_VALUE</code>
105      *
106      * @param variable
107      * @param value
108      * @return value label
109      * @throws CoreException
110      */

111     protected String JavaDoc getValueTypeName(IVariable variable, IValue value, IPresentationContext context) throws CoreException {
112         return value.getReferenceTypeName();
113     }
114     
115     /**
116      * Returns the label for the given value to display in <code>COLUMN_VALUE_TYPE</code>
117      *
118      * @param variable
119      * @param value
120      * @return value label
121      * @throws CoreException
122      */

123     protected String JavaDoc getValueText(IVariable variable, IValue value, IPresentationContext context) throws CoreException {
124         return escapeSpecialChars(value.getValueString());
125     }
126     
127     /* (non-Javadoc)
128      * @see org.eclipse.debug.ui.viewers.AsynchronousLabelAdapter#getForeground(java.lang.Object, org.eclipse.debug.ui.viewers.IPresentationContext)
129      */

130     protected RGB[] getForegrounds(Object JavaDoc element, IPresentationContext context) throws CoreException {
131         int numElements = getNumElements(context);
132         if (numElements == 1) {
133             if (element instanceof IVariable) {
134                 IVariable variable = (IVariable) element;
135                 try {
136                     if (variable.hasValueChanged()) {
137                         RGB[] rgbs = new RGB[numElements];
138                         Arrays.fill(rgbs, DebugUIPlugin.getPreferenceColor(IDebugUIConstants.PREF_CHANGED_DEBUG_ELEMENT_COLOR).getRGB());
139                         return rgbs;
140                     }
141                 } catch (DebugException e) {
142                 }
143             }
144         }
145         return super.getForegrounds(element, context);
146     }
147
148     protected RGB[] getBackgrounds(Object JavaDoc element, IPresentationContext context) throws CoreException {
149         int numElements = getNumElements(context);
150         if (numElements > 1) {
151             if (element instanceof IVariable) {
152                 IVariable variable = (IVariable) element;
153                 try {
154                     if (variable.hasValueChanged()) {
155                         RGB[] rgbs = new RGB[numElements];
156                         Arrays.fill(rgbs, DebugUIPlugin.getPreferenceColor(IInternalDebugUIConstants.PREF_CHANGED_VALUE_BACKGROUND).getRGB());
157                         return rgbs;
158                     }
159                 } catch (DebugException e) {
160                 }
161             }
162         }
163         return super.getBackgrounds(element, context);
164     }
165     
166     protected String JavaDoc escapeSpecialChars(String JavaDoc label) {
167         if (label == null) {
168             return null;
169         }
170         StringBuffer JavaDoc escaped = new StringBuffer JavaDoc();
171         for (int i = 0; i < label.length(); i++) {
172             char c = label.charAt(i);
173             switch (c) {
174                 case '\b':
175                     escaped.append("\\b"); //$NON-NLS-1$
176
break;
177                 case '\f':
178                     escaped.append("\\f"); //$NON-NLS-1$
179
break;
180                 case '\n':
181                     escaped.append("\\n"); //$NON-NLS-1$
182
break;
183                 case '\r':
184                     escaped.append("\\r"); //$NON-NLS-1$
185
break;
186                 case '\t':
187                     escaped.append("\\t"); //$NON-NLS-1$
188
break;
189                 default:
190                     escaped.append(c);
191                     break;
192             }
193         }
194         return escaped.toString();
195     }
196
197     
198 }
199
Popular Tags