KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > copyright > ChangeCopyrightDatesPerformer


1 package org.netbeans.modules.tasklist.copyright;
2
3 import javax.swing.text.BadLocationException JavaDoc;
4 import javax.swing.text.Document JavaDoc;
5 import javax.swing.text.Element JavaDoc;
6 import javax.swing.text.PlainDocument JavaDoc;
7 import javax.swing.text.StyledDocument JavaDoc;
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 /**
18  * todo
19  * @author Tor Norbye
20  * @author Tim Lebedkov
21  */

22 public class ChangeCopyrightDatesPerformer implements SuggestionPerformer {
23     private SuggestionContext env;
24     private int fRangeEnd, fBegin, fListEnd, fDateEnd, lineno;
25     private Document JavaDoc doc;
26     private String JavaDoc year;
27     
28     /** Creates a new instance of ChangeCopyrightDatesPerformer todo */
29     public ChangeCopyrightDatesPerformer(SuggestionContext env,
30         int fRangeEnd, int fBegin, int fListEnd, int fDateEnd, Document JavaDoc doc,
31         int lineno, String JavaDoc 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         // Replace the end of the range
44
substitute(doc, fRangeEnd, fBegin, fListEnd,
45             fDateEnd, year);
46     }
47
48     public boolean hasConfirmation() {
49         return true;
50     }
51
52     public Object JavaDoc getConfirmation(Suggestion s) {
53         String JavaDoc text = getLineContents(doc, lineno - 1);
54         Document JavaDoc newdoc = new PlainDocument JavaDoc();
55         String JavaDoc preview = null;
56         try {
57             newdoc.insertString(0, text, null);
58             // XXX Gotta subtract from the indices!
59
int rangeEnd = fRangeEnd;
60             int listEnd = fListEnd;
61             int dateEnd = fDateEnd;
62                    /*
63                     int rangeEnd = (fRangeEnd != -1) ?
64                         fRangeEnd-fBegin : -1;
65                     int listEnd = (fListEnd != -1) ?
66                         fListEnd-fBegin : -1;
67                     int dateEnd = (fDateEnd != -1) ?
68                         fDateEnd-fBegin : -1;
69                      */

70              substitute(newdoc, rangeEnd, 0, listEnd, dateEnd,
71                 year);
72              preview = newdoc.getText(0, newdoc.getLength());
73          } catch (BadLocationException JavaDoc ex) {
74              ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
75              return null;
76          }
77
78          //return NbBundle.getMessage(CopyrightChecker.class,
79
// "CopyrightConfirmation", oldRange, newRange, text);
80
String JavaDoc filename = env.getFileObject().getNameExt();
81          String JavaDoc beforeDesc = NbBundle.getMessage(
82             ChangeCopyrightDatesPerformer.class,
83             "CopyrightConfirmation"); // NOI18N
84
//String beforeContents = "<html><b>" + text.trim() + "</b></html>";
85
String JavaDoc afterDesc = NbBundle.getMessage(
86             ChangeCopyrightDatesPerformer.class,
87          "CopyrightConfirmationAfter"); // NOI18N
88
//String afterContents = "<html><b>" + preview.trim() + "</b></html>";
89

90
91          int fd = TLUtils.firstDiff(text, preview);
92          int ld = TLUtils.lastDiff(text, preview);
93
94          Line l = s.getLine();
95          StringBuffer JavaDoc sb = new StringBuffer JavaDoc(200);
96          sb.append("<html>"); // NOI18N
97
// HACK: When the text begins with
98
// "// Hello" it does NOT get rendered by
99
// Swing! (On this Apple JDK that I'm developing it
100
// on anyway). So hack around it by putting some
101
// useless attributes in there.
102
sb.append("<b></b>");
103          // XXX Make sure it can begin with // copyright 2000 !
104
TLUtils.appendSurroundingLine(sb, l, -1);
105          //sb.append("<b>");
106
TLUtils.appendAttributed(sb, text, fd,
107          text.length() - ld,
108          true, true);
109          //sb.append("</b>");
110
TLUtils.appendSurroundingLine(sb, l, +1);
111          sb.append("</html>"); // NOI18N
112
String JavaDoc beforeContents = sb.toString();
113
114
115          sb.setLength(0);
116          sb.append("<html>");
117          // HACK: I also noticed that "/*\n* Copyright"
118
// wouldn't correctly draw the first line, so
119
// hack around it by putting some useless
120
// attributes in there.
121
sb.append("<b></b>");
122
123          TLUtils.appendSurroundingLine(sb, l, -1);
124          //sb.append("<b>");
125
TLUtils.appendAttributed(sb, preview, fd,
126          preview.length() - ld,
127          true, true);
128          //sb.append("</b>");
129

130          TLUtils.appendSurroundingLine(sb, l, +1);
131          sb.append("</html>"); // NOI18N
132
String JavaDoc afterContents = sb.toString();
133
134          return new ConfPanel(beforeDesc,
135             beforeContents, afterDesc,
136             afterContents,
137             filename, lineno, null);
138      }
139
140     private String JavaDoc getLineContents(Document JavaDoc doc, int linenumber) {
141         Element JavaDoc 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 JavaDoc text = doc.getText(offset, endOffset - offset);
151             return text;
152         } catch (BadLocationException JavaDoc ex) {
153             ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
154         }
155         return null;
156     }
157
158     /**
159      * @param begin Position within the document to start
160      */

161     private static void substitute(Document JavaDoc doc, int rangeEnd, int begin, int listEnd,
162     int dateEnd, String JavaDoc year) {
163         if (rangeEnd != -1) {
164             // Replace the end of the range
165
final int pos = rangeEnd + begin;
166             try {
167                 doc.remove(pos, 4);
168                 doc.insertString(pos, year, null);
169             } catch (BadLocationException JavaDoc e) {
170                 ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
171             }
172         } else if (listEnd != -1) {
173             // Add one more item to the list
174
final int pos = listEnd + 4 + begin;
175             try {
176                 doc.insertString(pos, ", " + year, null);
177             } catch (BadLocationException JavaDoc e) {
178                 ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
179             }
180         } else { // assert dateEnd != -1
181
final int pos = dateEnd + 4 + begin;
182             // Create a date range
183
try {
184                 doc.insertString(pos, "-" + year, null);
185             } catch (BadLocationException JavaDoc e) {
186                 ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
187             }
188         }
189     }
190     
191     private static Element JavaDoc getElement(Document JavaDoc 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 JavaDoc)) {
198             ErrorManager.getDefault().log(ErrorManager.USER, "Not a styleddocument");
199             return null;
200         }
201         
202         StyledDocument JavaDoc doc = (StyledDocument JavaDoc) d;
203         Element JavaDoc e = doc.getParagraphElement(0).getParentElement();
204         if (e == null) {
205             // try default root (should work for text/plain)
206
e = doc.getDefaultRootElement();
207         }
208         Element JavaDoc elm = e.getElement(linenumber);
209         return elm;
210     }
211 };
212
Popular Tags