KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.tasklist.copyright;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.Date JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.JScrollPane JavaDoc;
28 import javax.swing.JTextArea JavaDoc;
29 import javax.swing.UIManager JavaDoc;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import javax.swing.text.Document JavaDoc;
32
33 import org.netbeans.modules.tasklist.client.Suggestion;
34 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
35 import org.netbeans.modules.tasklist.core.ConfPanel;
36 import org.netbeans.modules.tasklist.core.TLUtils;
37 import org.netbeans.modules.tasklist.providers.SuggestionContext;
38 import org.openide.DialogDisplayer;
39 import org.openide.ErrorManager;
40 import org.openide.NotifyDescriptor;
41 import org.openide.util.NbBundle;
42
43 /**
44  * SuggestionPerformer for adding copyright
45  *
46  * @author Tor Norbye
47  * @author Tim Lebedkov
48  */

49 public class AddCopyrightPerformer implements SuggestionPerformer {
50     private SuggestionContext env;
51
52     /** For some file formats comment must be inseretd into middle of file. */
53     private int prologEnd = 0;
54
55     /**
56      * todo
57      */

58     public AddCopyrightPerformer(SuggestionContext env) {
59         this.env = env;
60     }
61     
62     public void perform(Suggestion s) {
63         String JavaDoc comment = getComment(false);
64         if ((comment != null) && (comment.length() > 0)) {
65             try {
66                 env.getDocument().insertString(prologEnd, comment, null);
67             } catch (BadLocationException JavaDoc e) {
68                 ErrorManager.getDefault().notify(
69                 ErrorManager.WARNING, e);
70             }
71             // TODO Should I put a message in the status
72
// window which tells you how to change the
73
// copyright that is used?
74
} else {
75             JTextArea JavaDoc labelArea = new JTextArea JavaDoc();
76             labelArea.setWrapStyleWord(true);
77             labelArea.setLineWrap(true);
78             labelArea.setEditable(false);
79             labelArea.setText(
80             NbBundle.getMessage(CopyrightChecker.class,
81                 "NoChosenCopyright")); // NOI18N
82
labelArea.setBackground((Color JavaDoc) UIManager.getDefaults().get(
83                 "Label.background")); // NOI18N
84
JTextArea JavaDoc textArea = new JTextArea JavaDoc();
85             textArea.setRows(8);
86             String JavaDoc sample = getSampleLicense();
87             textArea.setText(sample);
88             textArea.select(0, sample.length());
89             JScrollPane JavaDoc pane = new JScrollPane JavaDoc(textArea);
90             // TODO: add text area to panel!
91

92             JPanel JavaDoc body = new JPanel JavaDoc();
93             body.setLayout(new BorderLayout JavaDoc());
94             body.add(labelArea, BorderLayout.NORTH);
95             body.add(pane, BorderLayout.CENTER);
96             body.setPreferredSize(new Dimension JavaDoc(400, 300));
97             NotifyDescriptor nd =
98             new NotifyDescriptor.Confirmation(
99                 body,
100                 NotifyDescriptor.OK_CANCEL_OPTION
101             );
102             Object JavaDoc result =
103             DialogDisplayer.getDefault().notify(nd);
104             if (NotifyDescriptor.OK_OPTION == result) {
105                 String JavaDoc copyright = textArea.getText().trim();
106                 if (copyright.length() > 0) {
107                     CopyrightSettings settings =
108                     (CopyrightSettings) CopyrightSettings.findObject(
109                     CopyrightSettings.class, true);
110                     settings.setScanCopyright(copyright);
111                     // recurse!
112
perform(s);
113                 }
114             }
115         }
116     }
117     
118     private String JavaDoc getCopyright() {
119         CopyrightSettings settings =
120             (CopyrightSettings) CopyrightSettings.findObject(CopyrightSettings.class, true);
121         String JavaDoc copyright = settings.getScanCopyright();
122         return copyright;
123     }
124
125     /** Side effect sets prologEnd */
126     private String JavaDoc getComment(boolean makeHtml) {
127         String JavaDoc copyright = getCopyright();
128         prologEnd = 0;
129         if ((copyright == null) || (copyright.length() == 0)) {
130             return null;
131         }
132         String JavaDoc prefix = "";
133         String JavaDoc suffix = "";
134         String JavaDoc linefix = null;
135         String JavaDoc ext = env.getFileObject().getExt();
136         if (ext.equalsIgnoreCase("java") || // NOI18N
137
ext.equalsIgnoreCase("cc") || // NOI18N
138
ext.equalsIgnoreCase("cpp")) { // NOI18N
139
linefix = "//"; // NOI18N
140
prefix = "/*"; // NOI18N
141
suffix = "*/"; // NOI18N
142
} else if (ext.equalsIgnoreCase("html") || // NOI18N
143
ext.equalsIgnoreCase("htm") || // NOI18N
144
ext.equalsIgnoreCase("xml")) { // NOI18N
145
prefix = "<!--"; // NOI18N
146
suffix = "-->"; // NOI18N
147

148             // #45151 for XML <?xml version=".." encoding="..."?> prolog must be the first
149
// XXX works well only for ASCI based encodings, EBDIC ignored
150
Document JavaDoc doc = env.getDocument();
151             int prologLength = Math.max(doc.getLength(), 80);
152             try {
153                 String JavaDoc prolog = doc.getText(0, prologLength);
154                 if (prolog.startsWith("<?xml")) {
155                     int end = prolog.indexOf("?>");
156                     if (end != -1) {
157                         prologEnd = end + 2;
158                         prefix = "\n<!--";
159                     }
160                 }
161             } catch (BadLocationException JavaDoc e) {
162                 assert false;
163             }
164         } else if (ext.equalsIgnoreCase("jsp")) { // NOI18N
165
prefix = "<%--"; // NOI18N
166
suffix = "--%>"; // NOI18N
167
} else if (ext.equalsIgnoreCase("c")) { // NOI18N
168
prefix = "/*"; // NOI18N
169
suffix = "*/"; // NOI18N
170
} else if (ext.equalsIgnoreCase("properties") || // NOI18N
171
ext.equalsIgnoreCase("sh")) { // NOI18N
172
linefix = "#"; // NOI18N
173
}
174         int n = copyright.length();
175         if (linefix != null) {
176             // Insert a comment string at the beginning of
177
// every line
178
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(2 * n);
179             if (makeHtml) {
180                 sb.append("<html><body>"); // NOI18N
181
}
182             boolean commentOut = true;
183             if (prefix != "") {
184                 commentOut = !startsWithComment(copyright, 0, n,
185                 prefix);
186             }
187             boolean newline = true;
188             for (int i = 0; i < n; i++) {
189                 if (newline && commentOut &&
190                 !startsWithComment(copyright, i, n, linefix)) {
191                     if (makeHtml) {
192                         if (i != 0) {
193                             sb.append("</i>"); // NOI18N
194
}
195                         sb.append("<b>"); // NOI18N
196
TLUtils.appendHTMLString(sb, linefix);
197                         sb.append("</b>&nbsp;<i>"); // NOI18N
198
} else {
199                         sb.append(linefix);
200                         sb.append(' ');
201                     }
202                 }
203                 newline = false;
204                 char c = copyright.charAt(i);
205                 if (c == '\n') {
206                     newline = true;
207                 }
208                 if (makeHtml) {
209                     TLUtils.appendHTMLChar(sb, c);
210                 } else {
211                     sb.append(c);
212                 }
213             }
214             if (makeHtml) {
215                 sb.append("</i><br></body></html>"); // NOI18N
216
} else {
217                 sb.append('\n');
218             }
219             return sb.toString();
220         } else {
221             // TODO - check to see if license already contains
222
// a comment prefix.
223
StringBuffer JavaDoc sb =
224             new StringBuffer JavaDoc(n + 20);
225             if (makeHtml) {
226                 sb.append("<html><body>"); // NOI18N
227
// HACK: When the text begins with
228
// "/* Hello" it does NOT get rendered
229
// by Swing! (On this Apple JDK that
230
// I'm developing it on anyway). So
231
// hack around it by putting some
232
// useless attributes in there.
233
sb.append("<b></b><i>"); // NOI18N
234
}
235             boolean commentOut = true;
236             if (startsWithComment(copyright, 0, n, prefix) ||
237             ((linefix != null) &&
238             (startsWithComment(copyright, 0, n, linefix)))) {
239                 commentOut = false;
240             }
241             if (commentOut) {
242                 if (makeHtml) {
243                     sb.append("<b>");
244                     TLUtils.appendHTMLString(sb, prefix);
245                     sb.append("</b>");
246                 } else {
247                     sb.append(prefix);
248                 }
249                 sb.append('\n');
250             }
251             if (makeHtml) {
252                 TLUtils.appendHTMLString(sb, copyright);
253             } else {
254                 sb.append(copyright);
255             }
256             if (commentOut) {
257                 sb.append('\n');
258                 if (makeHtml) {
259                     sb.append("<b>");
260                     TLUtils.appendHTMLString(sb, suffix);
261                     sb.append("</b>");
262                 } else {
263                     sb.append(suffix);
264                 }
265             }
266             if (makeHtml) {
267                 sb.append("</i><br></body></html>"); // NOI18N
268
} else {
269                 sb.append('\n');
270             }
271             return sb.toString();
272         }
273     }
274     
275     public boolean hasConfirmation() {
276         String JavaDoc copyright = getCopyright();
277         return ((copyright != null) && (copyright.length() > 0));
278     }
279     
280     public Object JavaDoc getConfirmation(Suggestion s) {
281         String JavaDoc comment = getComment(true);
282         String JavaDoc filename = env.getFileObject().getNameExt();
283         return new ConfPanel(
284             NbBundle.getMessage(CopyrightChecker.class,
285             "InsertCopyright"), // NOI18N
286
comment,
287             NbBundle.getMessage(CopyrightChecker.class,
288             "ChangeCopyright"), // NOI18N
289
null,
290             filename, 1, null);
291     }
292
293     private String JavaDoc getSampleLicense() {
294         return "YOUR LICENSE HERE. For example, here's the CDDL\n" +
295           "used by the tasklist modules:\n\n" +
296           "The contents of this file are subject to the terms of the Common Development\n" +
297           "and Distribution License (the License). You may not use this file except in\n" +
298           "compliance with the License.\n" +
299           "\n" +
300           "You can obtain a copy of the License at http://www.netbeans.org/cddl.html\n" +
301           "or http://www.netbeans.org/cddl.txt.\n" +
302           "\n" +
303           "When distributing Covered Code, include this CDDL Header Notice in each file\n" +
304           "and include the License file at http://www.netbeans.org/cddl.txt.\n" +
305           "If applicable, add the following below the CDDL Header, with the fields\n" +
306           "enclosed by brackets [] replaced by your own identifying information:\n" +
307           "\"Portions Copyrighted [year] [name of copyright owner]\"\n" +
308           "\n" +
309           "The Original Software is NetBeans. The Initial Developer of the Original\n" +
310           "Software is Sun Microsystems, Inc. Portions Copyright 1997-" +
311             new SimpleDateFormat JavaDoc("yyyy").format(new Date JavaDoc()) +
312             " Sun\n" +
313             "Microsystems, Inc. All Rights Reserved.\n\n\n";
314     }
315     /** Returns true if the given line (starting at str+index) begins
316      * with the given comment prefix (after an optional range of whitespace) */

317     private static boolean startsWithComment(String JavaDoc str, int index, int len,
318     String JavaDoc commentPrefix) {
319         while ((index < len) &&
320         (Character.isSpaceChar(str.charAt(index)))) {
321             index++;
322         }
323         return str.startsWith(commentPrefix, index);
324     }
325 };
Popular Tags