1 19 package org.netbeans.modules.tasklist.copyright; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Date ; 26 import javax.swing.JPanel ; 27 import javax.swing.JScrollPane ; 28 import javax.swing.JTextArea ; 29 import javax.swing.UIManager ; 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.text.Document ; 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 49 public class AddCopyrightPerformer implements SuggestionPerformer { 50 private SuggestionContext env; 51 52 53 private int prologEnd = 0; 54 55 58 public AddCopyrightPerformer(SuggestionContext env) { 59 this.env = env; 60 } 61 62 public void perform(Suggestion s) { 63 String comment = getComment(false); 64 if ((comment != null) && (comment.length() > 0)) { 65 try { 66 env.getDocument().insertString(prologEnd, comment, null); 67 } catch (BadLocationException e) { 68 ErrorManager.getDefault().notify( 69 ErrorManager.WARNING, e); 70 } 71 } else { 75 JTextArea labelArea = new JTextArea (); 76 labelArea.setWrapStyleWord(true); 77 labelArea.setLineWrap(true); 78 labelArea.setEditable(false); 79 labelArea.setText( 80 NbBundle.getMessage(CopyrightChecker.class, 81 "NoChosenCopyright")); labelArea.setBackground((Color ) UIManager.getDefaults().get( 83 "Label.background")); JTextArea textArea = new JTextArea (); 85 textArea.setRows(8); 86 String sample = getSampleLicense(); 87 textArea.setText(sample); 88 textArea.select(0, sample.length()); 89 JScrollPane pane = new JScrollPane (textArea); 90 92 JPanel body = new JPanel (); 93 body.setLayout(new BorderLayout ()); 94 body.add(labelArea, BorderLayout.NORTH); 95 body.add(pane, BorderLayout.CENTER); 96 body.setPreferredSize(new Dimension (400, 300)); 97 NotifyDescriptor nd = 98 new NotifyDescriptor.Confirmation( 99 body, 100 NotifyDescriptor.OK_CANCEL_OPTION 101 ); 102 Object result = 103 DialogDisplayer.getDefault().notify(nd); 104 if (NotifyDescriptor.OK_OPTION == result) { 105 String 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 perform(s); 113 } 114 } 115 } 116 } 117 118 private String getCopyright() { 119 CopyrightSettings settings = 120 (CopyrightSettings) CopyrightSettings.findObject(CopyrightSettings.class, true); 121 String copyright = settings.getScanCopyright(); 122 return copyright; 123 } 124 125 126 private String getComment(boolean makeHtml) { 127 String copyright = getCopyright(); 128 prologEnd = 0; 129 if ((copyright == null) || (copyright.length() == 0)) { 130 return null; 131 } 132 String prefix = ""; 133 String suffix = ""; 134 String linefix = null; 135 String ext = env.getFileObject().getExt(); 136 if (ext.equalsIgnoreCase("java") || ext.equalsIgnoreCase("cc") || ext.equalsIgnoreCase("cpp")) { linefix = "//"; prefix = "/*"; suffix = "*/"; } else if (ext.equalsIgnoreCase("html") || ext.equalsIgnoreCase("htm") || ext.equalsIgnoreCase("xml")) { prefix = "<!--"; suffix = "-->"; 148 Document doc = env.getDocument(); 151 int prologLength = Math.max(doc.getLength(), 80); 152 try { 153 String 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 e) { 162 assert false; 163 } 164 } else if (ext.equalsIgnoreCase("jsp")) { prefix = "<%--"; suffix = "--%>"; } else if (ext.equalsIgnoreCase("c")) { prefix = "/*"; suffix = "*/"; } else if (ext.equalsIgnoreCase("properties") || ext.equalsIgnoreCase("sh")) { linefix = "#"; } 174 int n = copyright.length(); 175 if (linefix != null) { 176 StringBuffer sb = new StringBuffer (2 * n); 179 if (makeHtml) { 180 sb.append("<html><body>"); } 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>"); } 195 sb.append("<b>"); TLUtils.appendHTMLString(sb, linefix); 197 sb.append("</b> <i>"); } 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>"); } else { 217 sb.append('\n'); 218 } 219 return sb.toString(); 220 } else { 221 StringBuffer sb = 224 new StringBuffer (n + 20); 225 if (makeHtml) { 226 sb.append("<html><body>"); sb.append("<b></b><i>"); } 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>"); } else { 269 sb.append('\n'); 270 } 271 return sb.toString(); 272 } 273 } 274 275 public boolean hasConfirmation() { 276 String copyright = getCopyright(); 277 return ((copyright != null) && (copyright.length() > 0)); 278 } 279 280 public Object getConfirmation(Suggestion s) { 281 String comment = getComment(true); 282 String filename = env.getFileObject().getNameExt(); 283 return new ConfPanel( 284 NbBundle.getMessage(CopyrightChecker.class, 285 "InsertCopyright"), comment, 287 NbBundle.getMessage(CopyrightChecker.class, 288 "ChangeCopyright"), null, 290 filename, 1, null); 291 } 292 293 private String 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 ("yyyy").format(new Date ()) + 312 " Sun\n" + 313 "Microsystems, Inc. All Rights Reserved.\n\n\n"; 314 } 315 317 private static boolean startsWithComment(String str, int index, int len, 318 String commentPrefix) { 319 while ((index < len) && 320 (Character.isSpaceChar(str.charAt(index)))) { 321 index++; 322 } 323 return str.startsWith(commentPrefix, index); 324 } 325 }; | Popular Tags |