|
Added
Link Here
|
| 1 |
package org.eclipse.mylar.viz.seesoft; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Iterator; |
| 5 |
import java.util.List; |
| 6 |
|
| 7 |
import org.eclipse.contribution.visualiser.VisualiserPlugin; |
| 8 |
import org.eclipse.contribution.visualiser.core.ProviderManager; |
| 9 |
import org.eclipse.contribution.visualiser.core.resources.VisualiserImages; |
| 10 |
import org.eclipse.contribution.visualiser.interfaces.IContentProvider; |
| 11 |
import org.eclipse.contribution.visualiser.interfaces.IGroup; |
| 12 |
import org.eclipse.contribution.visualiser.interfaces.IMember; |
| 13 |
import org.eclipse.contribution.visualiser.jdtImpl.JDTGroup; |
| 14 |
import org.eclipse.contribution.visualiser.jdtImpl.JDTMember; |
| 15 |
import org.eclipse.contribution.visualiser.utils.JDTUtils; |
| 16 |
import org.eclipse.jdt.core.ICompilationUnit; |
| 17 |
import org.eclipse.jdt.core.IJavaElement; |
| 18 |
import org.eclipse.jface.resource.ImageDescriptor; |
| 19 |
import org.eclipse.mylar.core.IMylarContext; |
| 20 |
import org.eclipse.mylar.core.IMylarContextListener; |
| 21 |
import org.eclipse.mylar.core.IMylarElement; |
| 22 |
import org.eclipse.mylar.core.MylarPlugin; |
| 23 |
import org.eclipse.mylar.java.JavaStructureBridge; |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Provides interesting java packages and classes for display in the Visualiser view. |
| 28 |
* |
| 29 |
* Note: This currently updates after the interest changes for any context node. |
| 30 |
* If performance is a problem, this could be modified to only update when a |
| 31 |
* landmark is added or removed. |
| 32 |
* |
| 33 |
* @author Wesley Coelho |
| 34 |
*/ |
| 35 |
public class JavaContextContentProvider implements IContentProvider, IMylarContextListener { |
| 36 |
|
| 37 |
/** IGroups representing packages with interesting elements in them */ |
| 38 |
protected List<JDTGroup> groups = new ArrayList<JDTGroup>(); |
| 39 |
|
| 40 |
/** True if there is currently an active context */ |
| 41 |
protected boolean contextActive = false; |
| 42 |
|
| 43 |
/** True if the visualiser has set this provider active*/ |
| 44 |
protected boolean viewActive = false; |
| 45 |
|
| 46 |
public void initialise(){ |
| 47 |
MylarPlugin.getContextManager().addListener(this); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns all registered groups |
| 52 |
* @see org.eclipse.contribution.visualiser.interfaces.IContentProvider#getAllGroups() |
| 53 |
* @return List of IGroups |
| 54 |
*/ |
| 55 |
public List getAllGroups() { |
| 56 |
buildModel(); |
| 57 |
return groups; |
| 58 |
} |
| 59 |
|
| 60 |
public List getAllMembers(IGroup group) { |
| 61 |
return group.getMembers(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns the List of all IMembers in all registered groups |
| 66 |
* Adapted from org.eclipse.contribution.visualiser.SimpleContentProvider |
| 67 |
*/ |
| 68 |
public List getAllMembers() { |
| 69 |
List grps = getAllGroups(); |
| 70 |
List<IMember> members = new ArrayList<IMember>(); |
| 71 |
if (grps == null) return members; |
| 72 |
Iterator iter = grps.iterator(); |
| 73 |
while (iter.hasNext()) { |
| 74 |
IGroup grp = (IGroup)iter.next(); |
| 75 |
List membersInGroup = getAllMembers(grp); |
| 76 |
Iterator iter2 = membersInGroup.iterator(); |
| 77 |
while (iter2.hasNext()) { |
| 78 |
IMember im = (IMember)iter2.next(); |
| 79 |
members.add(im); |
| 80 |
} |
| 81 |
} |
| 82 |
return members; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Constructs the model to be displayed in the visualiser by requesting |
| 87 |
* the active context's interesting elements and converting them to the |
| 88 |
* groups and members structure |
| 89 |
*/ |
| 90 |
protected void buildModel(){ |
| 91 |
try { |
| 92 |
if(!(ProviderManager.getContentProvider().equals(this))){ |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
groups.clear(); |
| 97 |
|
| 98 |
if (!contextActive || !viewActive){ |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
List<IMylarElement> interestingNodes = MylarPlugin.getContextManager().getActiveContext().getInteresting(); |
| 103 |
JavaStructureBridge jBridge = new JavaStructureBridge(); |
| 104 |
|
| 105 |
for (int i = 0; i < interestingNodes.size(); i++){ |
| 106 |
IMylarElement node = interestingNodes.get(i); |
| 107 |
|
| 108 |
if (node.getContentType().equals(JavaStructureBridge.CONTENT_TYPE)){ |
| 109 |
String handleID = node.getHandleIdentifier(); |
| 110 |
|
| 111 |
IJavaElement jElement = (IJavaElement) jBridge.getObjectForHandle(handleID); |
| 112 |
|
| 113 |
if (jElement.getElementType() == IJavaElement.TYPE){ |
| 114 |
|
| 115 |
String typeName = jBridge.getName(jElement); |
| 116 |
if (typeName.equals("")){ |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
JDTMember javaType = new JDTMember(typeName, jElement); |
| 121 |
addTypeToGroups(javaType); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
} catch (RuntimeException e) { |
| 127 |
MylarPlugin.fail(e, "Could not provide data for the visualiser", false); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Adds the specified visualiser member to the group representing its package |
| 133 |
* in the groups list. The package group is created as required. |
| 134 |
*/ |
| 135 |
protected void addTypeToGroups(JDTMember javaType){ |
| 136 |
|
| 137 |
IJavaElement packageElt = javaType.getResource().getParent(); |
| 138 |
while (packageElt.getParent() != null){ |
| 139 |
if (packageElt.getElementType() == IJavaElement.PACKAGE_FRAGMENT){ |
| 140 |
break; |
| 141 |
} |
| 142 |
packageElt = packageElt.getParent(); |
| 143 |
} |
| 144 |
|
| 145 |
if (packageElt.getElementType() != IJavaElement.PACKAGE_FRAGMENT){ |
| 146 |
MylarPlugin.fail(new Exception("Could not find package for java element"), "Could not find package for java element", false); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
ICompilationUnit compilationUnit = Util.getCompilationUnit(javaType.getResource()); |
| 151 |
if (compilationUnit == null || !compilationUnit.exists()){ |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
javaType.setSize(Util.getLength(compilationUnit)); |
| 156 |
|
| 157 |
JDTGroup parentGroup = new JDTGroup(packageElt.getElementName()); |
| 158 |
|
| 159 |
//Check for an existing group for this package |
| 160 |
for (int i = 0; i < groups.size(); i++){ |
| 161 |
JDTGroup currGroup = groups.get(i); |
| 162 |
|
| 163 |
if (currGroup.getFullname().equals(parentGroup.getFullname())){ |
| 164 |
parentGroup = currGroup; |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
List<IMember> memberList = new ArrayList<IMember>(); |
| 170 |
memberList.add(javaType); |
| 171 |
parentGroup.addMembers(memberList); |
| 172 |
|
| 173 |
if (!groups.contains(parentGroup)){ |
| 174 |
groups.add(parentGroup); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @see org.eclipse.contribution.visualiser.interfaces.IContentProvider#getMemberViewIcon() |
| 180 |
*/ |
| 181 |
public ImageDescriptor getMemberViewIcon() { |
| 182 |
return VisualiserImages.CLASS_VIEW; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* @see org.eclipse.contribution.visualiser.interfaces.IContentProvider#getGroupViewIcon() |
| 188 |
*/ |
| 189 |
public ImageDescriptor getGroupViewIcon() { |
| 190 |
return VisualiserImages.PACKAGE_VIEW; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Process a mouse click on a member |
| 196 |
* @see org.eclipse.contribution.visualiser.interfaces.IContentProvider#processMouseclick(IMember, boolean, int) |
| 197 |
* Copied from JDTContentProvider |
| 198 |
*/ |
| 199 |
public boolean processMouseclick( |
| 200 |
IMember member, |
| 201 |
boolean markupWasClicked, |
| 202 |
int buttonClicked) { |
| 203 |
|
| 204 |
if(buttonClicked != 1){ |
| 205 |
return true; |
| 206 |
} |
| 207 |
if(markupWasClicked) { |
| 208 |
return false; |
| 209 |
} |
| 210 |
if(member instanceof JDTMember) { |
| 211 |
IJavaElement jEl = ((JDTMember)member).getResource(); |
| 212 |
if(jEl != null) { |
| 213 |
JDTUtils.openInEditor(jEl.getResource(), JDTUtils.getClassDeclLineNum(jEl)); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
public String getEmptyMessage() { |
| 222 |
return "No active context to display."; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
//Begin implementation of IMylarContextListener |
| 227 |
|
| 228 |
public void contextActivated(IMylarContext context) { |
| 229 |
contextActive = true; |
| 230 |
VisualiserPlugin.refresh(); |
| 231 |
} |
| 232 |
|
| 233 |
public void contextDeactivated(IMylarContext context) { |
| 234 |
contextActive = false; |
| 235 |
VisualiserPlugin.refresh(); |
| 236 |
} |
| 237 |
|
| 238 |
public void presentationSettingsChanging(UpdateKind kind) { |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
public void presentationSettingsChanged(UpdateKind kind) { |
| 243 |
if (kind == UpdateKind.HIGHLIGHTER){ |
| 244 |
VisualiserPlugin.refresh(); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
public void interestChanged(IMylarElement node) { |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
public void interestChanged(List<IMylarElement> nodes) { |
| 253 |
VisualiserPlugin.refresh(); |
| 254 |
} |
| 255 |
|
| 256 |
public void nodeDeleted(IMylarElement node) { |
| 257 |
VisualiserPlugin.refresh(); |
| 258 |
} |
| 259 |
|
| 260 |
public void landmarkAdded(IMylarElement node) { |
| 261 |
VisualiserPlugin.refresh(); |
| 262 |
} |
| 263 |
|
| 264 |
public void landmarkRemoved(IMylarElement node) { |
| 265 |
VisualiserPlugin.refresh(); |
| 266 |
} |
| 267 |
|
| 268 |
public void edgesChanged(IMylarElement node) { |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
public void activate() { |
| 273 |
viewActive = true; |
| 274 |
} |
| 275 |
|
| 276 |
public void deactivate() { |
| 277 |
viewActive = false; |
| 278 |
} |
| 279 |
|
| 280 |
} |