1 19 package org.netbeans.modules.java.editor.overridden; 20 21 import java.awt.Point ; 22 import java.awt.event.ActionEvent ; 23 import java.util.logging.Level ; 24 import javax.swing.AbstractAction ; 25 import javax.swing.Action ; 26 import javax.swing.text.BadLocationException ; 27 import javax.swing.text.Document ; 28 import javax.swing.text.JTextComponent ; 29 import org.netbeans.editor.AnnotationDesc; 30 import org.netbeans.editor.Annotations; 31 import org.netbeans.editor.BaseDocument; 32 import org.netbeans.editor.ImplementationProvider; 33 import org.netbeans.editor.JumpList; 34 import org.netbeans.editor.Utilities; 35 import org.openide.ErrorManager; 36 import org.openide.filesystems.FileObject; 37 import org.openide.loaders.DataObject; 38 39 43 public final class IsOverriddenAnnotationAction extends AbstractAction { 44 45 public IsOverriddenAnnotationAction() { 46 setEnabled(true); 47 } 48 49 public void actionPerformed(ActionEvent e) { 50 if (!invokeDefaultAction((JTextComponent ) e.getSource())) { 51 Action actions[] = ImplementationProvider.getDefault().getGlyphGutterActions((JTextComponent ) e.getSource()); 52 53 if (actions == null) 54 return ; 55 56 int nextAction = 0; 57 58 while (nextAction < actions.length && actions[nextAction] != this) 59 nextAction++; 60 61 nextAction++; 62 63 if (actions.length > nextAction) { 64 Action a = actions[nextAction]; 65 if (a!=null && a.isEnabled()){ 66 a.actionPerformed(e); 67 } 68 } 69 } 70 } 71 72 private FileObject getFile(JTextComponent component) { 73 Document doc = component.getDocument(); 74 DataObject od = (DataObject) doc.getProperty(Document.StreamDescriptionProperty); 75 76 if (od == null) { 77 return null; 78 } 79 80 return od.getPrimaryFile(); 81 } 82 83 private IsOverriddenAnnotation findAnnotation(JTextComponent component, AnnotationDesc desc, int offset) { 84 FileObject file = getFile(component); 85 86 if (file == null) { 87 if (ErrorManager.getDefault().isLoggable(ErrorManager.WARNING)) { 88 ErrorManager.getDefault().log(ErrorManager.WARNING, "component=" + component + " does not have a file specified in the document."); 89 } 90 return null; 91 } 92 93 AnnotationsHolder ah = AnnotationsHolder.get(file); 94 95 if (ah == null) { 96 IsOverriddenAnnotationHandler.LOG.log(Level.INFO, "component=" + component + " does not have attached a IsOverriddenAnnotationHandler"); 97 98 return null; 99 } 100 101 for(IsOverriddenAnnotation a : ah.getAnnotations()) { 102 if ( a.getPosition().getOffset() == offset 103 && desc.getShortDescription().equals(a.getShortDescription())) { 104 return a; 105 } 106 } 107 108 return null; 109 } 110 111 boolean invokeDefaultAction(final JTextComponent comp) { 112 final Document doc = comp.getDocument(); 113 114 if (doc instanceof BaseDocument) { 115 final int currentPosition = comp.getCaretPosition(); 116 final Annotations annotations = ((BaseDocument) doc).getAnnotations(); 117 final IsOverriddenAnnotation[] annotation = new IsOverriddenAnnotation[1]; 118 final Point [] p = new Point [1]; 119 120 doc.render(new Runnable () { 121 public void run() { 122 try { 123 int line = Utilities.getLineOffset((BaseDocument) doc, currentPosition); 124 int startOffset = Utilities.getRowStartFromLineOffset((BaseDocument) doc, line); 125 AnnotationDesc desc = annotations.getActiveAnnotation(line); 126 p[0] = comp.modelToView(startOffset).getLocation(); 127 annotation[0] = findAnnotation(comp, desc, startOffset); 128 } catch (BadLocationException ex) { 129 ErrorManager.getDefault().notify(ex); 130 } 131 } 132 }); 133 134 if (annotation[0] == null) 135 return false; 136 137 JumpList.checkAddEntry(comp, currentPosition); 138 139 annotation[0].mouseClicked(comp, p[0]); 140 141 return true; 142 } 143 144 return false; 145 } 146 147 } | Popular Tags |