1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.jface.action.IMenuManager; 16 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; 17 import org.eclipse.jface.viewers.ILabelProvider; 18 import org.eclipse.jface.viewers.ISelection; 19 import org.eclipse.jface.viewers.ITreeContentProvider; 20 import org.eclipse.jface.viewers.LabelProvider; 21 import org.eclipse.jface.viewers.ViewerComparator; 22 import org.eclipse.pde.core.plugin.IPluginBase; 23 import org.eclipse.pde.core.plugin.IPluginModelBase; 24 import org.eclipse.pde.core.plugin.IPluginObject; 25 import org.eclipse.pde.internal.core.plugin.ImportObject; 26 import org.eclipse.pde.internal.core.text.IDocumentAttribute; 27 import org.eclipse.pde.internal.core.text.IDocumentNode; 28 import org.eclipse.pde.internal.core.text.IDocumentRange; 29 import org.eclipse.pde.internal.core.text.IDocumentTextNode; 30 import org.eclipse.pde.internal.core.text.plugin.PluginModelBase; 31 import org.eclipse.pde.internal.ui.PDELabelProvider; 32 import org.eclipse.pde.internal.ui.PDEPlugin; 33 import org.eclipse.pde.internal.ui.PDEPluginImages; 34 import org.eclipse.pde.internal.ui.PDEUIMessages; 35 import org.eclipse.pde.internal.ui.editor.PDEFormEditor; 36 import org.eclipse.pde.internal.ui.editor.XMLSourcePage; 37 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 38 import org.eclipse.pde.internal.ui.search.PluginSearchActionGroup; 39 import org.eclipse.pde.internal.ui.util.SharedLabelProvider; 40 import org.eclipse.swt.graphics.Image; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.ui.actions.ActionContext; 43 44 public class ManifestSourcePage extends XMLSourcePage { 45 46 private Object fLibraries = new Object (); 47 private Object fImports = new Object (); 48 private Object fExtensionPoints = new Object (); 49 private Object fExtensions = new Object (); 50 private ExtensionAttributePointDectector fDetector; 51 private PluginSearchActionGroup fActionGroup; 52 53 class OutlineLabelProvider extends LabelProvider { 54 private PDELabelProvider fProvider; 55 56 public OutlineLabelProvider() { 57 fProvider = PDEPlugin.getDefault().getLabelProvider(); 58 } 59 60 63 64 public String getText(Object obj) { 65 if (obj == fLibraries) 66 return PDEUIMessages.ManifestSourcePage_libraries; 67 if (obj == fImports) 68 return PDEUIMessages.ManifestSourcePage_dependencies; 69 if (obj == fExtensionPoints) 70 return PDEUIMessages.ManifestSourcePage_extensionPoints; 71 if (obj == fExtensions) 72 return PDEUIMessages.ManifestSourcePage_extensions; 73 String text = fProvider.getText(obj); 74 if ((text == null || text.trim().length() == 0) && obj instanceof IDocumentNode) 75 text = ((IDocumentNode)obj).getXMLTagName(); 76 return text; 77 } 78 79 public Image getImage(Object obj) { 80 if (obj == fLibraries) 81 return fProvider.get(PDEPluginImages.DESC_RUNTIME_OBJ); 82 if (obj == fImports) 83 return fProvider.get(PDEPluginImages.DESC_REQ_PLUGINS_OBJ); 84 if (obj == fExtensionPoints) 85 return fProvider.get(PDEPluginImages.DESC_EXT_POINTS_OBJ); 86 if (obj == fExtensions) 87 return fProvider.get(PDEPluginImages.DESC_EXTENSIONS_OBJ); 88 89 Image image = fProvider.getImage(obj); 90 int flags = ((IDocumentNode)obj).isErrorNode() ? SharedLabelProvider.F_ERROR : 0; 91 return (flags == 0) ? image : fProvider.get(image, flags); 92 } 93 } 94 95 class ContentProvider extends DefaultContentProvider implements ITreeContentProvider { 96 97 100 public Object [] getChildren(Object parent) { 101 PluginModelBase model = (PluginModelBase)getInputContext().getModel(); 102 103 ArrayList result = new ArrayList (); 104 if (parent instanceof IPluginBase) { 105 IPluginBase pluginBase = (IPluginBase)parent; 106 if (pluginBase.getLibraries().length > 0) 107 result.add(fLibraries); 108 if (pluginBase.getImports().length > 0) 109 result.add(fImports); 110 if (pluginBase.getExtensionPoints().length > 0) 111 result.add(fExtensionPoints); 112 if (pluginBase.getExtensions().length > 0) 113 result.add(fExtensions); 114 return result.toArray(); 115 } 116 if (parent == fLibraries) 117 return model.getPluginBase().getLibraries(); 118 119 if (parent == fImports) 120 return model.getPluginBase().getImports(); 121 122 if (parent == fExtensionPoints) 123 return model.getPluginBase().getExtensionPoints(); 124 125 if (parent == fExtensions) 126 return model.getPluginBase().getExtensions(); 127 128 return new Object [0]; 129 } 130 131 134 public Object getParent(Object element) { 135 if (element instanceof IDocumentNode) 136 return ((IDocumentNode)element).getParentNode(); 137 return null; 138 } 139 140 143 public boolean hasChildren(Object element) { 144 if (element instanceof IPluginBase) { 145 return ((IDocumentNode) element).getChildNodes().length > 0; 146 } 147 return element == fLibraries || element == fImports 148 || element == fExtensionPoints || element == fExtensions; 149 } 150 151 154 public Object [] getElements(Object inputElement) { 155 if (inputElement instanceof IPluginModelBase) { 156 return new Object [] {((IPluginModelBase)inputElement).getPluginBase()}; 157 } 158 return new Object [0]; 159 } 160 } 161 162 class OutlineComparator extends ViewerComparator{ 163 166 public int category(Object element) { 167 if (element == fLibraries) 168 return 0; 169 if (element == fImports) 170 return 1; 171 if (element == fExtensionPoints) 172 return 2; 173 if (element == fExtensions) 174 return 3; 175 return 4; 176 } 177 } 178 179 public ManifestSourcePage(PDEFormEditor editor, String id, String title) { 180 super(editor, id, title); 181 fDetector = new ExtensionAttributePointDectector(); 182 fActionGroup = new PluginSearchActionGroup(); 183 } 184 185 public ILabelProvider createOutlineLabelProvider() { 186 return new OutlineLabelProvider(); 187 } 188 public ITreeContentProvider createOutlineContentProvider() { 189 return new ContentProvider(); 190 } 191 192 195 public void updateSelection(Object object) { 196 if ((object instanceof IDocumentNode) && 197 !((IDocumentNode)object).isErrorNode()) { 198 fSelection = object; 199 setHighlightRange((IDocumentNode)object, true); 200 setSelectedRange((IDocumentNode)object, false); 201 } else { 202 } 204 } 205 206 209 public ViewerComparator createOutlineComparator() { 210 return new OutlineComparator(); 211 } 212 213 public IDocumentRange getRangeElement(int offset, boolean searchChildren) { 214 IPluginBase base = ((IPluginModelBase)getInputContext().getModel()).getPluginBase(); 215 IDocumentRange 216 node = findNode(base.getLibraries(), offset, searchChildren); 217 if (node == null) 218 node = findNode(base.getImports(), offset, searchChildren); 219 if (node == null) 220 node = findNode(base.getExtensionPoints(), offset, searchChildren); 221 if (node == null) 222 node = findNode(base.getExtensions(), offset, searchChildren); 223 if (node == null) 224 node = findNode(new IPluginObject[] { base }, offset, searchChildren); 225 226 return node; 227 } 228 229 private IDocumentRange findNode(Object [] nodes, int offset, boolean searchChildren) { 230 for (int i = 0; i < nodes.length; i++) { 231 IDocumentNode node = (IDocumentNode)nodes[i]; 232 if (node.getOffset() <= offset 233 && offset < node.getOffset() + node.getLength()) { 234 235 if (!searchChildren) 236 return node; 237 238 if (node.getOffset() < offset && 239 offset <= node.getOffset() + node.getXMLTagName().length() + 1) 240 return node; 241 242 IDocumentAttribute[] attrs = node.getNodeAttributes(); 243 if (attrs != null) 244 for (int a = 0; a < attrs.length; a++) 245 if (attrs[a].getNameOffset() <= offset && 246 offset <= attrs[a].getValueOffset() + attrs[a].getValueLength()) 247 return (IDocumentNode)attrs[a]; 248 249 IDocumentTextNode textNode = node.getTextNode(); 250 if (textNode != null && 251 textNode.getOffset() <= offset && 252 offset < textNode.getOffset() + textNode.getLength()) 253 return textNode; 254 255 IDocumentNode[] children = node.getChildNodes(); 256 if (children != null) 257 for (int c = 0; c < children.length; c++) 258 if (children[c].getOffset() <= offset && 259 offset < children[c].getOffset() + children[c].getLength()) 260 return findNode(new Object [] {children[c]}, offset, searchChildren); 261 262 return node; 264 } 265 } 266 return null; 267 } 268 269 public IDocumentRange findRange() { 270 if (fSelection instanceof ImportObject) 271 fSelection = ((ImportObject)fSelection).getImport(); 272 if (fSelection instanceof IDocumentNode) 273 return (IDocumentNode)fSelection; 274 return null; 275 } 276 277 protected boolean isSelectionListener() { 278 return true; 279 } 280 281 public Object getAdapter(Class adapter) { 282 if (IHyperlinkDetector.class.equals(adapter)) 283 return new ManifestHyperlinkDetector(this); 284 return super.getAdapter(adapter); 285 } 286 287 290 protected void editorContextMenuAboutToShow(IMenuManager menu) { 291 292 ISelection selection = fDetector.getSelection(); 293 if (selection != null) { 294 fActionGroup.setContext(new ActionContext(selection)); 295 fActionGroup.fillContextMenu(menu); 296 } 297 super.editorContextMenuAboutToShow(menu); 298 } 299 300 303 public void createPartControl(Composite parent) { 304 super.createPartControl(parent); 305 fDetector.setTextEditor(this); 308 } 309 310 313 public boolean isQuickOutlineEnabled() { 314 return true; 315 } 316 317 320 public void setActive(boolean active) { 321 super.setActive(active); 322 if (active) { 324 updateTextSelection(); 325 } 326 } 327 328 } 329 | Popular Tags |