1 package org.netbeans.modules.tasklist.copyright; 2 3 import javax.swing.text.BadLocationException ; 4 import javax.swing.text.Document ; 5 import javax.swing.text.Element ; 6 import javax.swing.text.PlainDocument ; 7 import javax.swing.text.StyledDocument ; 8 import org.netbeans.modules.tasklist.client.Suggestion; 9 import org.netbeans.modules.tasklist.client.SuggestionPerformer; 10 import org.netbeans.modules.tasklist.core.ConfPanel; 11 import org.netbeans.modules.tasklist.core.TLUtils; 12 import org.netbeans.modules.tasklist.providers.SuggestionContext; 13 import org.openide.ErrorManager; 14 import org.openide.text.Line; 15 import org.openide.util.NbBundle; 16 17 22 public class ChangeCopyrightDatesPerformer implements SuggestionPerformer { 23 private SuggestionContext env; 24 private int fRangeEnd, fBegin, fListEnd, fDateEnd, lineno; 25 private Document doc; 26 private String year; 27 28 29 public ChangeCopyrightDatesPerformer(SuggestionContext env, 30 int fRangeEnd, int fBegin, int fListEnd, int fDateEnd, Document doc, 31 int lineno, String year) { 32 this.env = env; 33 this.fRangeEnd = fRangeEnd; 34 this.fBegin = fBegin; 35 this.fListEnd = fListEnd; 36 this.fDateEnd = fDateEnd; 37 this.doc = doc; 38 this.lineno = lineno; 39 this.year = year; 40 } 41 42 public void perform(Suggestion s) { 43 substitute(doc, fRangeEnd, fBegin, fListEnd, 45 fDateEnd, year); 46 } 47 48 public boolean hasConfirmation() { 49 return true; 50 } 51 52 public Object getConfirmation(Suggestion s) { 53 String text = getLineContents(doc, lineno - 1); 54 Document newdoc = new PlainDocument (); 55 String preview = null; 56 try { 57 newdoc.insertString(0, text, null); 58 int rangeEnd = fRangeEnd; 60 int listEnd = fListEnd; 61 int dateEnd = fDateEnd; 62 70 substitute(newdoc, rangeEnd, 0, listEnd, dateEnd, 71 year); 72 preview = newdoc.getText(0, newdoc.getLength()); 73 } catch (BadLocationException ex) { 74 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 75 return null; 76 } 77 78 String filename = env.getFileObject().getNameExt(); 81 String beforeDesc = NbBundle.getMessage( 82 ChangeCopyrightDatesPerformer.class, 83 "CopyrightConfirmation"); String afterDesc = NbBundle.getMessage( 86 ChangeCopyrightDatesPerformer.class, 87 "CopyrightConfirmationAfter"); 90 91 int fd = TLUtils.firstDiff(text, preview); 92 int ld = TLUtils.lastDiff(text, preview); 93 94 Line l = s.getLine(); 95 StringBuffer sb = new StringBuffer (200); 96 sb.append("<html>"); sb.append("<b></b>"); 103 TLUtils.appendSurroundingLine(sb, l, -1); 105 TLUtils.appendAttributed(sb, text, fd, 107 text.length() - ld, 108 true, true); 109 TLUtils.appendSurroundingLine(sb, l, +1); 111 sb.append("</html>"); String beforeContents = sb.toString(); 113 114 115 sb.setLength(0); 116 sb.append("<html>"); 117 sb.append("<b></b>"); 122 123 TLUtils.appendSurroundingLine(sb, l, -1); 124 TLUtils.appendAttributed(sb, preview, fd, 126 preview.length() - ld, 127 true, true); 128 130 TLUtils.appendSurroundingLine(sb, l, +1); 131 sb.append("</html>"); String afterContents = sb.toString(); 133 134 return new ConfPanel(beforeDesc, 135 beforeContents, afterDesc, 136 afterContents, 137 filename, lineno, null); 138 } 139 140 private String getLineContents(Document doc, int linenumber) { 141 Element elm = getElement(doc, linenumber); 142 if (elm == null) { 143 ErrorManager.getDefault().log(ErrorManager.USER, "getElement was null"); 144 return null; 145 } 146 int offset = elm.getStartOffset(); 147 int endOffset = elm.getEndOffset(); 148 149 try { 150 String text = doc.getText(offset, endOffset - offset); 151 return text; 152 } catch (BadLocationException ex) { 153 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 154 } 155 return null; 156 } 157 158 161 private static void substitute(Document doc, int rangeEnd, int begin, int listEnd, 162 int dateEnd, String year) { 163 if (rangeEnd != -1) { 164 final int pos = rangeEnd + begin; 166 try { 167 doc.remove(pos, 4); 168 doc.insertString(pos, year, null); 169 } catch (BadLocationException e) { 170 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 171 } 172 } else if (listEnd != -1) { 173 final int pos = listEnd + 4 + begin; 175 try { 176 doc.insertString(pos, ", " + year, null); 177 } catch (BadLocationException e) { 178 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 179 } 180 } else { final int pos = dateEnd + 4 + begin; 182 try { 184 doc.insertString(pos, "-" + year, null); 185 } catch (BadLocationException e) { 186 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 187 } 188 } 189 } 190 191 private static Element getElement(Document d, int linenumber) { 192 if (d == null) { 193 ErrorManager.getDefault().log(ErrorManager.USER, "d was null"); 194 return null; 195 } 196 197 if (!(d instanceof StyledDocument )) { 198 ErrorManager.getDefault().log(ErrorManager.USER, "Not a styleddocument"); 199 return null; 200 } 201 202 StyledDocument doc = (StyledDocument ) d; 203 Element e = doc.getParagraphElement(0).getParentElement(); 204 if (e == null) { 205 e = doc.getDefaultRootElement(); 207 } 208 Element elm = e.getElement(linenumber); 209 return elm; 210 } 211 }; 212 | Popular Tags |