Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6d9fa033615f7b0bdb6909ae05efab60bc0e4fb9 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*******************************************************************************
 * Copyright (c) 2006, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Wind Rvier Systems - added support for columns (bug 235646)
 *     Abeer Bagul (Tensilica) - Working set support for Expressions view (bug 372181)
 *******************************************************************************/
package org.eclipse.debug.internal.ui.model.elements;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IExpressionManager;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IWatchExpression;
import org.eclipse.debug.internal.core.IInternalDebugCoreConstants;
import org.eclipse.debug.internal.ui.DebugUIMessages;
import org.eclipse.debug.internal.ui.DefaultLabelProvider;
import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementEditor;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementLabelProvider;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI;

/**
 * Default content provider for the expression manager.
 */
public class ExpressionManagerContentProvider extends ElementContentProvider {

    /**
     * An element representing the "Add new expression" entry in the 
     * expressions view.
     * 
     * @since 3.6
     */
    private static class AddNewExpressionElement implements IElementLabelProvider, IElementEditor {
        
        public void update(ILabelUpdate[] updates) {
        	
            for (int i = 0; i < updates.length; i++) {
                String[] columnIds = updates[i].getColumnIds();
                if (columnIds == null) {
                    updateLabel(updates[i], 0);
                } else {
                    for (int j = 0; j < columnIds.length; j++) {
                        if (IDebugUIConstants.COLUMN_ID_VARIABLE_NAME.equals(columnIds[j])) {
                            updateLabel(updates[i], j);
                        } else {
                            updates[i].setLabel(IInternalDebugCoreConstants.EMPTY_STRING, j);
                        }
                    }
                }
                    
                updates[i].done();
            }
        }
        
        private void updateLabel(ILabelUpdate update, int columnIndex) {
            update.setLabel(DebugUIMessages.ExpressionManagerContentProvider_1, columnIndex);
            update.setImageDescriptor(DebugUITools.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_MONITOR_EXPRESSION), columnIndex);
            
            // Display the "Add new expression" element in italic to 
            // distinguish it from user elements in view.
            FontData fontData = JFaceResources.getFontDescriptor(IDebugUIConstants.PREF_VARIABLE_TEXT_FONT).getFontData()[0];
            fontData.setStyle(SWT.ITALIC);            
            update.setFontData(fontData, columnIndex);
        }
        
        public CellEditor getCellEditor(IPresentationContext context, String columnId, Object element, Composite parent) {
            return new TextCellEditor(parent);
        }
        
        public ICellModifier getCellModifier(IPresentationContext context, Object element) {
            return new AddNewExpressionCellModifider(context);
        }                
    }
    
    private static class AddNewExpressionCellModifider implements ICellModifier {
    	
    	private final IPresentationContext fPresentationContext;
    	
    	AddNewExpressionCellModifider(IPresentationContext presentationContext) {
    		fPresentationContext = presentationContext;
    	}
        public boolean canModify(Object element, String property) {
            return (IDebugUIConstants.COLUMN_ID_VARIABLE_NAME.equals(property));
        }
        
        public Object getValue(Object element, String property) {
            return IInternalDebugCoreConstants.EMPTY_STRING;
        }
        
        public void modify(Object element, String property, Object value) {
            // If an expression is entered, add a new watch expression to the 
            // manager. 
            if (value instanceof String && 
                !IInternalDebugCoreConstants.EMPTY_STRING.equals( ((String)value).trim()) ) 
            {
                String expressionText = DefaultLabelProvider.encodeEsacpedChars((String)value);
                IWatchExpression newExpression= 
                    DebugPlugin.getDefault().getExpressionManager().newWatchExpression(expressionText);
                DebugPlugin.getDefault().getExpressionManager().addExpression(newExpression);
                newExpression.setExpressionContext(getContext());
                
                //if any working sets are applied to this view,
                //add this expression to all applied working sets,
                //otherwise it will be filtered out from the view.
                Object workingSetsProp = fPresentationContext.getProperty(IDebugUIConstants.PROP_EXPRESSIONS_WORKING_SETS);
                if (workingSetsProp instanceof String[])
                {
                	String[] workingSetNames = (String[])workingSetsProp;
                	for (int i=0; i<workingSetNames.length; i++)
                	{
                		String workingSetName = workingSetNames[i];
                    	IWorkingSet workingSet = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(workingSetName);
                    	IAdaptable[] existingElements = workingSet.getElements();
                    	IAdaptable[] newElements = new IAdaptable[existingElements.length + 1];
                    	System.arraycopy(existingElements, 0, newElements, 0, existingElements.length);
                    	newElements[newElements.length - 1] = newExpression;
                    	workingSet.setElements(newElements);
                	}
                }
            }
        }
	
        private IDebugElement getContext() {
            IAdaptable object = DebugUITools.getDebugContext();
            IDebugElement context = null;
            if (object instanceof IDebugElement) {
                context = (IDebugElement) object;
            } else if (object instanceof ILaunch) {
                context = ((ILaunch) object).getDebugTarget();
            }
            return context;
        }
    }
    
    private static final AddNewExpressionElement ADD_NEW_EXPRESSION_ELEMENT = new AddNewExpressionElement();

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider#getChildCount(java.lang.Object, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext)
	 */
	protected int getChildCount(Object element, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
	    // Add the "Add new expression" element only if columns are displayed.
		return ((IExpressionManager) element).getExpressions().length + 
		    (context.getColumns() != null ? 1 : 0); 
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider#getChildren(java.lang.Object, int, int, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext)
	 */
	protected Object[] getChildren(Object parent, int index, int length, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
	    if (context.getColumns() != null) {
	        return getElements(((IExpressionManager) parent).getExpressions(), ADD_NEW_EXPRESSION_ELEMENT, index, length);
	    } else {
	        return getElements(((IExpressionManager) parent).getExpressions(), index, length);
	    }
	}

	/**
	 * Returns a subrange of elements from the given elements array plus the last element.
	 * 
	 * @see ElementContentProvider#getElements(Object[], int, int)
	 * 
	 * @since 3.6
	 */
    private Object[] getElements(Object[] elements, Object lastElement, int index, int length) {

        int max = elements.length + 1;
        if (index < max && ((index + length) > max)) {
            length = max - index;
        }
        if ((index + length) <= max) {
            Object[] sub = new Object[length];
            System.arraycopy(elements, index, sub, 0, Math.min(elements.length - index, length));
            if (index + length > elements.length) {
                sub[length - 1] = lastElement;
            }
            return sub;
        }
        return null;
    }

	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider#supportsContextId(java.lang.String)
	 */
	protected boolean supportsContextId(String id) {
		return id.equals(IDebugUIConstants.ID_EXPRESSION_VIEW);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.ui.model.elements.ElementContentProvider#hasChildren(java.lang.Object, org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext, org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate)
	 */
	protected boolean hasChildren(Object element, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
	    return true;
	}
	
}

Back to the top