1 11 12 package org.eclipse.pde.internal.ui.editor.plugin; 13 14 import org.eclipse.jface.text.Region; 15 import org.eclipse.jface.text.source.ISourceViewer; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.StructuredSelection; 18 import org.eclipse.pde.core.plugin.IPluginExtension; 19 import org.eclipse.pde.core.plugin.IPluginObject; 20 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute; 21 import org.eclipse.pde.internal.core.schema.SchemaRootElement; 22 import org.eclipse.pde.internal.core.text.IDocumentAttribute; 23 import org.eclipse.pde.internal.core.text.IDocumentNode; 24 import org.eclipse.pde.internal.core.text.IDocumentRange; 25 import org.eclipse.pde.internal.ui.editor.text.XMLUtil; 26 import org.eclipse.swt.custom.StyledText; 27 import org.eclipse.swt.events.KeyEvent; 28 import org.eclipse.swt.events.KeyListener; 29 import org.eclipse.swt.events.MouseEvent; 30 import org.eclipse.swt.events.MouseListener; 31 import org.eclipse.swt.graphics.Point; 32 33 37 public class ExtensionAttributePointDectector implements MouseListener, 38 KeyListener { 39 40 private ManifestSourcePage fSourcePage; 41 42 private StyledText fStyledText; 43 44 private ISelection fSelection; 45 46 49 public ExtensionAttributePointDectector() { 50 fSelection = null; 51 fStyledText = null; 52 fSourcePage = null; 53 } 54 55 58 public ISelection getSelection() { 59 return fSelection; 60 } 61 62 65 public void setTextEditor(ManifestSourcePage editor) { 66 fSourcePage = editor; 67 ISourceViewer viewer = fSourcePage.getViewer(); 69 if (viewer == null) { 71 return; 72 } 73 StyledText newStyledText = viewer.getTextWidget(); 74 if ((fStyledText != null) && 76 fStyledText.equals(newStyledText)) { 77 return; 78 } 79 removeListeners(); 81 fStyledText = newStyledText; 83 addListeners(); 85 checkIfOnTarget(); 88 } 89 90 93 private void checkIfOnTarget() { 94 fSelection = null; 96 if (isInputInitialized() == false) { 98 return; 99 } 100 Point selectionPoint = fStyledText.getSelection(); 102 Region selectionRegion = new Region(selectionPoint.x, 103 selectionPoint.y - selectionPoint.x); 104 if ((selectionRegion == null) || 107 (fSourcePage == null)) { 108 return; 109 } 110 IDocumentRange element = 112 fSourcePage.getRangeElement(selectionRegion.getOffset(), true); 113 if (XMLUtil.withinRange(element, selectionRegion.getOffset()) == false) { 115 return; 116 } 117 if ((element instanceof IDocumentAttribute) == false) { 119 return; 120 } 121 IDocumentAttribute documentAttribute = ((IDocumentAttribute)element); 124 String attributeValue = documentAttribute.getAttributeValue(); 125 if ((attributeValue == null) || 127 (attributeValue.length() == 0)) { 128 return; 129 } 130 IPluginObject node = 132 XMLUtil.getTopLevelParent((IDocumentNode)documentAttribute); 133 if ((node == null) || 135 (node.getModel().isEditable() == false)) { 136 return; 137 } 138 if ((node instanceof IPluginExtension) == false) { 140 return; 141 } 142 IPluginExtension extension = (IPluginExtension)node; 144 ISchemaAttribute schemaAttribute = 146 XMLUtil.getSchemaAttribute(documentAttribute, 147 extension.getPoint()); 148 if (schemaAttribute == null) { 150 return; 151 } 152 if (((schemaAttribute.getParent() instanceof SchemaRootElement) == false) || 154 (documentAttribute.getAttributeName().equals(IPluginExtension.P_POINT) == false)) { 155 return; 156 } 157 fSelection = new StructuredSelection(extension); 158 } 159 160 163 private void removeListeners() { 164 if (isInputInitialized() == false) { 165 return; 166 } 167 fStyledText.removeMouseListener(this); 168 fStyledText.removeKeyListener(this); 169 } 170 171 174 private boolean isInputInitialized() { 175 if ((fStyledText == null) || 176 (fStyledText.isDisposed())) { 177 return false; 178 } 179 return true; 180 } 181 182 185 private void addListeners() { 186 if (isInputInitialized() == false) { 187 return; 188 } 189 fStyledText.addMouseListener(this); 190 fStyledText.addKeyListener(this); 191 } 192 193 196 public void mouseDoubleClick(MouseEvent e) { 197 } 199 200 203 public void mouseDown(MouseEvent e) { 204 } 206 207 public void mouseUp(MouseEvent e) { 208 checkIfOnTarget(); 209 } 210 211 public void keyPressed(KeyEvent e) { 212 checkIfOnTarget(); 213 } 214 215 218 public void keyReleased(KeyEvent e) { 219 } 221 222 } 223 | Popular Tags |