1 19 20 package org.netbeans.modules.tasklist.checkstyle; 21 22 23 import javax.swing.text.BadLocationException ; 24 import javax.swing.text.Document ; 25 import javax.swing.text.Element ; 26 import javax.swing.text.StyledDocument ; 27 28 import org.netbeans.modules.tasklist.client.Suggestion; 29 import org.netbeans.modules.tasklist.client.SuggestionPerformer; 30 31 import org.openide.ErrorManager; 32 33 38 public abstract class AbstractSuggestionPerformer implements SuggestionPerformer { 39 40 41 protected final Document doc; 42 43 44 protected final int lineno; 45 46 47 protected String columnOnwards; 48 49 protected String originalLine; 50 51 54 AbstractSuggestionPerformer( 55 final Document doc, 56 final int lineno, 57 final int column) { 58 59 this.doc = doc; 60 this.lineno = lineno; 61 62 final Element elm = getElement(doc, lineno - 1); 64 if (elm == null) { 65 ErrorManager.getDefault().log(ErrorManager.USER, "getElement was null"); 66 return; 67 } 68 final int offset = elm.getStartOffset(); 69 final int endOffset = elm.getEndOffset() - 1; 70 final int columnOffset = offset + Math.max(0, column) - 1; 71 try { 72 73 originalLine = doc.getText(columnOffset, endOffset); 74 columnOnwards = doc.getText(columnOffset, endOffset - columnOffset); 75 76 } catch (BadLocationException ex) { 77 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 78 } 79 } 80 81 82 public void perform(final Suggestion suggestion) { 83 84 if (!performOnAdjacentLine(lineno, false) && !performOnAdjacentLine(lineno, true)) { 85 86 ErrorManager.getDefault().log(ErrorManager.USER, 87 "Lost position of violation, no fix performed"); 88 } 89 } 90 91 92 protected abstract void performImpl(int docPosition) throws BadLocationException ; 93 94 97 public Object getConfirmation(final Suggestion suggestion) { 98 return null; 99 } 100 101 102 public boolean hasConfirmation() { 103 return false; 104 } 105 106 107 protected final static Element getElement(final Document d, final int linenumber) { 108 if (d == null) { 109 ErrorManager.getDefault().log(ErrorManager.USER, "d was null"); 110 return null; 111 } 112 113 if (!(d instanceof StyledDocument )) { 114 ErrorManager.getDefault().log(ErrorManager.USER, "Not a styleddocument"); 115 return null; 116 } 117 118 final StyledDocument doc = (StyledDocument ) d; 119 Element e = doc.getParagraphElement(0).getParentElement(); 120 if (e == null) { 121 e = doc.getDefaultRootElement(); 123 } 124 final Element elm = e.getElement(linenumber); 125 return elm; 126 } 127 128 129 private boolean performOnAdjacentLine(final int lNumber, final boolean incrementLine) { 130 131 boolean result = false; 132 if (lNumber > 0) { 133 final Element elm = getElement(doc, lNumber - 1); 134 if (elm == null) { 135 ErrorManager.getDefault().log(ErrorManager.USER, "getElement was null"); 136 137 } else { 138 final int offset = elm.getStartOffset(); 139 final int endOffset = elm.getEndOffset() - 1; 140 try { 141 final String line = doc.getText(offset, endOffset - offset); 142 final int idx = line.indexOf(columnOnwards); 143 if (line.equals(originalLine) && idx >= 0) { 144 performImpl(offset + idx); 145 result = true; 146 147 } else { 148 result = performOnAdjacentLine(incrementLine ? lNumber + 1 : lNumber - 1, incrementLine); 150 } 151 } catch (BadLocationException ex) { 152 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 153 } 154 } 155 } 156 return result; 157 } 158 } 159 | Popular Tags |