|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2006 Wind River Systems, Inc. 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 |
* Anton Leherbauer (Wind River Systems) - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.cdt.internal.ui.navigator; |
| 12 |
|
| 13 |
import java.util.Arrays; |
| 14 |
import java.util.Iterator; |
| 15 |
import java.util.LinkedHashSet; |
| 16 |
import java.util.List; |
| 17 |
import java.util.Set; |
| 18 |
|
| 19 |
import org.eclipse.cdt.core.model.CoreModel; |
| 20 |
import org.eclipse.cdt.core.model.ICElement; |
| 21 |
import org.eclipse.cdt.core.model.ICModel; |
| 22 |
import org.eclipse.cdt.internal.ui.cview.CViewContentProvider; |
| 23 |
import org.eclipse.cdt.ui.CUIPlugin; |
| 24 |
import org.eclipse.cdt.ui.PreferenceConstants; |
| 25 |
import org.eclipse.core.resources.IContainer; |
| 26 |
import org.eclipse.core.resources.IResource; |
| 27 |
import org.eclipse.core.resources.IWorkspaceRoot; |
| 28 |
import org.eclipse.core.runtime.IAdaptable; |
| 29 |
import org.eclipse.jface.preference.IPreferenceStore; |
| 30 |
import org.eclipse.jface.util.IPropertyChangeListener; |
| 31 |
import org.eclipse.jface.util.PropertyChangeEvent; |
| 32 |
import org.eclipse.jface.viewers.Viewer; |
| 33 |
import org.eclipse.ui.IMemento; |
| 34 |
import org.eclipse.ui.navigator.ICommonContentExtensionSite; |
| 35 |
import org.eclipse.ui.navigator.IExtensionStateModel; |
| 36 |
import org.eclipse.ui.navigator.IPipelinedTreeContentProvider; |
| 37 |
import org.eclipse.ui.navigator.PipelinedShapeModification; |
| 38 |
import org.eclipse.ui.navigator.PipelinedViewerUpdate; |
| 39 |
|
| 40 |
/** |
| 41 |
* A content provider populating a Common Navigator view with CDT model content. |
| 42 |
*/ |
| 43 |
public class CNavigatorContentProvider extends CViewContentProvider implements IPipelinedTreeContentProvider { |
| 44 |
|
| 45 |
/** The CN extension state model for persisting viewer options */ |
| 46 |
private IExtensionStateModel fStateModel; |
| 47 |
/** The input object as supplied in the call to {@link #inputChanged()} */ |
| 48 |
private Object fRealInput; |
| 49 |
private IPropertyChangeListener fPropertyChangeListener; |
| 50 |
|
| 51 |
/* |
| 52 |
* @see org.eclipse.ui.navigator.ICommonContentProvider#init(org.eclipse.ui.navigator.ICommonContentExtensionSite) |
| 53 |
*/ |
| 54 |
public void init(ICommonContentExtensionSite commonContentExtensionSite) { |
| 55 |
IExtensionStateModel stateModel= commonContentExtensionSite.getExtensionStateModel(); |
| 56 |
IMemento memento= commonContentExtensionSite.getMemento(); |
| 57 |
|
| 58 |
fStateModel= stateModel; |
| 59 |
restoreState(memento); |
| 60 |
fPropertyChangeListener= new IPropertyChangeListener() { |
| 61 |
public void propertyChange(PropertyChangeEvent event) { |
| 62 |
boolean refreshViewer= false; |
| 63 |
String property= event.getProperty(); |
| 64 |
|
| 65 |
if (property.equals(PreferenceConstants.PREF_SHOW_CU_CHILDREN)) { |
| 66 |
boolean showCUChildren= fStateModel.getBooleanProperty(PreferenceConstants.PREF_SHOW_CU_CHILDREN); |
| 67 |
setProvideMembers(showCUChildren); |
| 68 |
refreshViewer= true; |
| 69 |
} else if (property.equals(PreferenceConstants.CVIEW_GROUP_INCLUDES)) { |
| 70 |
boolean groupIncludes= fStateModel.getBooleanProperty(PreferenceConstants.CVIEW_GROUP_INCLUDES); |
| 71 |
setIncludesGrouping(groupIncludes); |
| 72 |
refreshViewer= true; |
| 73 |
} |
| 74 |
|
| 75 |
if (refreshViewer && getViewer() != null) { |
| 76 |
getViewer().refresh(); |
| 77 |
} |
| 78 |
} |
| 79 |
}; |
| 80 |
CUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyChangeListener); |
| 81 |
// TLETODO [CN] use extension state model for view options persistence |
| 82 |
// fStateModel.addPropertyChangeListener(listener); |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* @see org.eclipse.cdt.ui.CElementContentProvider#dispose() |
| 87 |
*/ |
| 88 |
public void dispose() { |
| 89 |
CUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener); |
| 90 |
// TLETODO [CN] use extension state model for view options persistence |
| 91 |
// fStateModel.removePropertyChangeListener(fPropertyChangeListener); |
| 92 |
super.dispose(); |
| 93 |
} |
| 94 |
|
| 95 |
/* |
| 96 |
* @see org.eclipse.ui.navigator.IMementoAware#restoreState(org.eclipse.ui.IMemento) |
| 97 |
*/ |
| 98 |
public void restoreState(IMemento memento) { |
| 99 |
IPreferenceStore store= PreferenceConstants.getPreferenceStore(); |
| 100 |
boolean showCUChildren= store.getBoolean(PreferenceConstants.PREF_SHOW_CU_CHILDREN); |
| 101 |
boolean groupIncludes= store.getBoolean(PreferenceConstants.CVIEW_GROUP_INCLUDES); |
| 102 |
if (memento != null) { |
| 103 |
String mementoValue= memento.getString(PreferenceConstants.PREF_SHOW_CU_CHILDREN); |
| 104 |
if (mementoValue != null) { |
| 105 |
showCUChildren= Boolean.parseBoolean(mementoValue); |
| 106 |
} |
| 107 |
mementoValue= memento.getString(PreferenceConstants.CVIEW_GROUP_INCLUDES); |
| 108 |
if (mementoValue != null) { |
| 109 |
groupIncludes= Boolean.parseBoolean(mementoValue); |
| 110 |
} |
| 111 |
} |
| 112 |
setProvideMembers(showCUChildren); |
| 113 |
setIncludesGrouping(groupIncludes); |
| 114 |
setProvideWorkingCopy(true); |
| 115 |
} |
| 116 |
|
| 117 |
/* |
| 118 |
* @see org.eclipse.ui.navigator.IMementoAware#saveState(org.eclipse.ui.IMemento) |
| 119 |
*/ |
| 120 |
public void saveState(IMemento memento) { |
| 121 |
if (memento != null) { |
| 122 |
memento.putString(PreferenceConstants.PREF_SHOW_CU_CHILDREN, String.valueOf(getProvideMembers())); |
| 123 |
memento.putString(PreferenceConstants.CVIEW_GROUP_INCLUDES, String.valueOf(areIncludesGroup())); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
* @see org.eclipse.cdt.ui.CElementContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) |
| 129 |
*/ |
| 130 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| 131 |
fRealInput= newInput; |
| 132 |
super.inputChanged(viewer, oldInput, findInputElement(newInput)); |
| 133 |
} |
| 134 |
|
| 135 |
private Object findInputElement(Object newInput) { |
| 136 |
if (newInput instanceof IWorkspaceRoot) { |
| 137 |
return CoreModel.create((IWorkspaceRoot) newInput); |
| 138 |
} |
| 139 |
return newInput; |
| 140 |
} |
| 141 |
|
| 142 |
/* |
| 143 |
* @see org.eclipse.cdt.internal.ui.BaseCElementContentProvider#getParent(java.lang.Object) |
| 144 |
*/ |
| 145 |
public Object getParent(Object element) { |
| 146 |
Object parent= super.getParent(element); |
| 147 |
if (parent instanceof ICModel) { |
| 148 |
return getViewerInput() != null ? fRealInput : parent; |
| 149 |
} |
| 150 |
return parent; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Access the viewer input. |
| 155 |
* @return the viewer input |
| 156 |
*/ |
| 157 |
protected Object getViewerInput() { |
| 158 |
return fInput; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Access the viewer. |
| 163 |
* @return the viewer |
| 164 |
*/ |
| 165 |
protected Viewer getViewer() { |
| 166 |
return fViewer; |
| 167 |
} |
| 168 |
|
| 169 |
/* |
| 170 |
* @see org.eclipse.cdt.internal.ui.BaseCElementContentProvider#getElements(java.lang.Object) |
| 171 |
*/ |
| 172 |
public Object[] getElements(Object parent) { |
| 173 |
if (parent instanceof IWorkspaceRoot) { |
| 174 |
return super.getElements(CoreModel.create((IWorkspaceRoot)parent)); |
| 175 |
} |
| 176 |
return super.getElements(parent); |
| 177 |
} |
| 178 |
|
| 179 |
/* |
| 180 |
* @see org.eclipse.cdt.internal.ui.cview.CViewContentProvider#getChildren(java.lang.Object) |
| 181 |
*/ |
| 182 |
public Object[] getChildren(Object element) { |
| 183 |
Object children[]; |
| 184 |
if (element instanceof IWorkspaceRoot) { |
| 185 |
children = super.getChildren(CoreModel.create((IWorkspaceRoot)element)); |
| 186 |
} else { |
| 187 |
children = super.getChildren(element); |
| 188 |
} |
| 189 |
return children; |
| 190 |
} |
| 191 |
|
| 192 |
/* |
| 193 |
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedChildren(java.lang.Object, java.util.Set) |
| 194 |
*/ |
| 195 |
public void getPipelinedChildren(Object parent, Set currentChildren) { |
| 196 |
Object[] children= getChildren(parent); |
| 197 |
for (Iterator iter= currentChildren.iterator(); iter.hasNext();) { |
| 198 |
if (iter.next() instanceof IResource) { |
| 199 |
iter.remove(); |
| 200 |
} |
| 201 |
} |
| 202 |
currentChildren.addAll(Arrays.asList(children)); |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedElements(java.lang.Object, java.util.Set) |
| 207 |
*/ |
| 208 |
public void getPipelinedElements(Object input, Set currentElements) { |
| 209 |
// only replace plain resource elements with custom elements |
| 210 |
// and avoid duplicating elements already customized |
| 211 |
// by upstream content providers |
| 212 |
Object[] elements= getElements(input); |
| 213 |
List elementList= Arrays.asList(elements); |
| 214 |
for (Iterator iter= currentElements.iterator(); iter.hasNext();) { |
| 215 |
Object element= iter.next(); |
| 216 |
IResource resource= null; |
| 217 |
if (element instanceof IResource) { |
| 218 |
resource= (IResource)element; |
| 219 |
} else if (element instanceof IAdaptable) { |
| 220 |
resource= (IResource)((IAdaptable)element).getAdapter(IResource.class); |
| 221 |
} |
| 222 |
if (resource != null) { |
| 223 |
int i= elementList.indexOf(resource); |
| 224 |
if (i >= 0) { |
| 225 |
elements[i]= null; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
for (int i= 0; i < elements.length; i++) { |
| 230 |
Object element= elements[i]; |
| 231 |
if (element instanceof ICElement) { |
| 232 |
ICElement cElement= (ICElement)element; |
| 233 |
IResource resource= cElement.getResource(); |
| 234 |
if (resource != null) { |
| 235 |
currentElements.remove(resource); |
| 236 |
} |
| 237 |
currentElements.add(element); |
| 238 |
} else if (element != null) { |
| 239 |
currentElements.add(element); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/* |
| 245 |
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedParent(java.lang.Object, java.lang.Object) |
| 246 |
*/ |
| 247 |
public Object getPipelinedParent(Object object, Object suggestedParent) { |
| 248 |
return getParent(object); |
| 249 |
} |
| 250 |
|
| 251 |
/* |
| 252 |
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptAdd(org.eclipse.ui.navigator.PipelinedShapeModification) |
| 253 |
*/ |
| 254 |
public PipelinedShapeModification interceptAdd(PipelinedShapeModification addModification) { |
| 255 |
convertToCElements(addModification); |
| 256 |
return addModification; |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptRefresh(org.eclipse.ui.navigator.PipelinedViewerUpdate) |
| 261 |
*/ |
| 262 |
public boolean interceptRefresh(PipelinedViewerUpdate refreshSynchronization) { |
| 263 |
return convertToCElements(refreshSynchronization.getRefreshTargets()); |
| 264 |
} |
| 265 |
|
| 266 |
/* |
| 267 |
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptRemove(org.eclipse.ui.navigator.PipelinedShapeModification) |
| 268 |
*/ |
| 269 |
public PipelinedShapeModification interceptRemove(PipelinedShapeModification removeModification) { |
| 270 |
convertToCElements(removeModification.getChildren()); |
| 271 |
return removeModification; |
| 272 |
} |
| 273 |
|
| 274 |
/* |
| 275 |
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptUpdate(org.eclipse.ui.navigator.PipelinedViewerUpdate) |
| 276 |
*/ |
| 277 |
public boolean interceptUpdate(PipelinedViewerUpdate updateSynchronization) { |
| 278 |
return convertToCElements(updateSynchronization.getRefreshTargets()); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Converts the shape modification to use ICElements. |
| 283 |
* |
| 284 |
* @param modification |
| 285 |
* the shape modification to convert |
| 286 |
* @return <code>true</code> if the shape modification set was modified |
| 287 |
*/ |
| 288 |
private boolean convertToCElements( |
| 289 |
PipelinedShapeModification modification) { |
| 290 |
Object parent= modification.getParent(); |
| 291 |
if (parent instanceof IContainer) { |
| 292 |
ICElement element= CoreModel.getDefault().create((IContainer) parent); |
| 293 |
if (element != null && element.exists()) { |
| 294 |
// don't convert the root |
| 295 |
if( !(element instanceof ICModel)) { |
| 296 |
modification.setParent(element); |
| 297 |
} |
| 298 |
return convertToCElements(modification.getChildren()); |
| 299 |
|
| 300 |
} |
| 301 |
} |
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Converts the given set to ICElements. |
| 307 |
* |
| 308 |
* @param currentChildren |
| 309 |
* The set of current children that would be contributed or |
| 310 |
* refreshed in the viewer. |
| 311 |
* @return <code>true</code> if the input set was modified |
| 312 |
*/ |
| 313 |
private boolean convertToCElements(Set currentChildren) { |
| 314 |
LinkedHashSet convertedChildren= new LinkedHashSet(); |
| 315 |
ICElement newChild; |
| 316 |
for (Iterator iter= currentChildren.iterator(); iter.hasNext();) { |
| 317 |
Object child= iter.next(); |
| 318 |
if (child instanceof IResource) { |
| 319 |
if ((newChild= CoreModel.getDefault().create((IResource) child)) != null |
| 320 |
&& newChild.exists()) { |
| 321 |
iter.remove(); |
| 322 |
convertedChildren.add(newChild); |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
if (!convertedChildren.isEmpty()) { |
| 327 |
currentChildren.addAll(convertedChildren); |
| 328 |
return true; |
| 329 |
} |
| 330 |
return false; |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
} |