alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

package org.netbeans.modules.tasklist.copyright;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import org.netbeans.modules.tasklist.client.Suggestion;
import org.netbeans.modules.tasklist.client.SuggestionPerformer;
import org.netbeans.modules.tasklist.core.ConfPanel;
import org.netbeans.modules.tasklist.core.TLUtils;
import org.netbeans.modules.tasklist.providers.SuggestionContext;
import org.openide.DialogDisplayer;
import org.openide.ErrorManager;
import org.openide.NotifyDescriptor;
import org.openide.util.NbBundle;

/**
 * SuggestionPerformer for adding copyright
 *
 * @author Tor Norbye
 * @author Tim Lebedkov
 */
public class AddCopyrightPerformer implements SuggestionPerformer {
    private SuggestionContext env;

    /** For some file formats comment must be inseretd into middle of file. */
    private int prologEnd = 0;

    /**
     * todo
     */
    public AddCopyrightPerformer(SuggestionContext env) {
        this.env = env;
    }
    
    public void perform(Suggestion s) {
        String comment = getComment(false);
        if ((comment != null) && (comment.length() > 0)) {
            try {
                env.getDocument().insertString(prologEnd, comment, null);
            } catch (BadLocationException e) {
                ErrorManager.getDefault().notify(
                ErrorManager.WARNING, e);
            }
            // TODO Should I put a message in the status
            // window which tells you how to change the
            // copyright that is used?
        } else {
            JTextArea labelArea = new JTextArea();
            labelArea.setWrapStyleWord(true);
            labelArea.setLineWrap(true);
            labelArea.setEditable(false);
            labelArea.setText(
            NbBundle.getMessage(CopyrightChecker.class,
                "NoChosenCopyright")); // NOI18N
            labelArea.setBackground((Color) UIManager.getDefaults().get(
                "Label.background")); // NOI18N
            JTextArea textArea = new JTextArea();
            textArea.setRows(8);
            String sample = getSampleLicense();
            textArea.setText(sample);
            textArea.select(0, sample.length());
            JScrollPane pane = new JScrollPane(textArea);
            // TODO: add text area to panel!
            
            JPanel body = new JPanel();
            body.setLayout(new BorderLayout());
            body.add(labelArea, BorderLayout.NORTH);
            body.add(pane, BorderLayout.CENTER);
            body.setPreferredSize(new Dimension(400, 300));
            NotifyDescriptor nd =
            new NotifyDescriptor.Confirmation(
                body,
                NotifyDescriptor.OK_CANCEL_OPTION
            );
            Object result =
            DialogDisplayer.getDefault().notify(nd);
            if (NotifyDescriptor.OK_OPTION == result) {
                String copyright = textArea.getText().trim();
                if (copyright.length() > 0) {
                    CopyrightSettings settings =
                    (CopyrightSettings) CopyrightSettings.findObject(
                    CopyrightSettings.class, true);
                    settings.setScanCopyright(copyright);
                    // recurse!
                    perform(s);
                }
            }
        }
    }
    
    private String getCopyright() {
        CopyrightSettings settings =
            (CopyrightSettings) CopyrightSettings.findObject(CopyrightSettings.class, true);
        String copyright = settings.getScanCopyright();
        return copyright;
    }

    /** Side effect sets prologEnd */
    private String getComment(boolean makeHtml) {
        String copyright = getCopyright();
        prologEnd = 0;
        if ((copyright == null) || (copyright.length() == 0)) {
            return null;
        }
        String prefix = "";
        String suffix = "";
        String linefix = null;
        String ext = env.getFileObject().getExt();
        if (ext.equalsIgnoreCase("java") || // NOI18N
        ext.equalsIgnoreCase("cc") || // NOI18N
        ext.equalsIgnoreCase("cpp")) {  // NOI18N
            linefix = "//"; // NOI18N
            prefix = "/*"; // NOI18N
            suffix = "*/"; // NOI18N
        } else if (ext.equalsIgnoreCase("html") || // NOI18N
            ext.equalsIgnoreCase("htm") || // NOI18N
            ext.equalsIgnoreCase("xml")) {  // NOI18N
            prefix = ""; // NOI18N

            // #45151 for XML  prolog must be the first
            // XXX works well only for ASCI based encodings, EBDIC ignored
            Document doc = env.getDocument();
            int prologLength = Math.max(doc.getLength(), 80);
            try {
                String prolog = doc.getText(0, prologLength);
                if (prolog.startsWith("");
                    if (end != -1) {
                        prologEnd =  end + 2;
                        prefix = "\n