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

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.tasklist.javaparser;

import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import org.openide.ErrorManager;
import org.openide.cookies.SourceCookie;
import org.openide.explorer.view.*;
import org.openide.nodes.*;
import org.netbeans.modules.java.*;



import org.openide.src.Identifier;
import org.openide.src.Import;
import org.openide.src.SourceElement;
import org.openide.src.SourceException;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;

import org.openide.cookies.LineCookie;
import org.openide.loaders.DataObject;
import org.openide.text.Line;
import org.openide.ErrorManager;

import java.util.TreeSet;
import java.lang.reflect.Modifier;
import org.netbeans.editor.ext.java.*;
import org.netbeans.modules.editor.java.*;

import org.netbeans.modules.tasklist.core.ConfPanel;
import org.netbeans.modules.tasklist.core.TLUtils;
import org.netbeans.modules.tasklist.client.Suggestion;
import org.netbeans.modules.tasklist.client.SuggestionPerformer;


/**
 * This class performs class import suggestions
 *
 * @author Tor Norbye
 */
class ImportPerformer implements SuggestionPerformer {
    
    private DataObject dobj;
    private Document doc;
    private String beforeDesc;
    private JCClass importClass;
    private int lineno;

    ImportPerformer(int lineno, DataObject dobj,
                    Document doc, JCClass importClass) {
        this.lineno = lineno;
        this.dobj = dobj;
        this.doc = doc;
        this.importClass = importClass;

    }

    // Yay - it's a casing-error
    public void perform(Suggestion s) {
        importClass(doc, importClass);
    }

     public boolean hasConfirmation() {
         return true;
     }
     
     public Object getConfirmation(Suggestion s) {
         String filename =
             dobj.getPrimaryFile().getNameExt();
         String beforeDesc =
             NbBundle.getMessage(ImportPerformer.class,
                                 "ImportClassConfirmation"); // NOI18N
         String beforeContents =
             "import " + // NOI18N
             importClass.toString() +
             ""; //NOI18N
         return new ConfPanel(beforeDesc,
                              beforeContents, null,
                              null,
                              filename, lineno,
                              null);
     }


    /** Import a particular class into the given document */
    static void importClass(Document doc, Object item) {
        if (item instanceof JCClass || item instanceof JCPackage) {
            Object o = doc.getProperty(Document.StreamDescriptionProperty);
            if (o instanceof DataObject) {
                DataObject dob = (DataObject)o;
                SourceCookie sc = (SourceCookie)dob.getCookie(SourceCookie.class);
                if (sc != null) {
                    SourceElement se = sc.getSource();
                    if (se != null) {
                        try {
                            if (item instanceof JCClass){
                                JCClass cls = (JCClass)item;
                                se.addImport(new Import(Identifier.create(cls.getFullName()), false));
                            }else if (item instanceof JCPackage){
                                JCPackage pkg = (JCPackage)item;
                                se.addImport(new Import(Identifier.create(pkg.getName()), true));
                            }
                        } catch (SourceException e) {
                        }
                    }
                }
            }
        }
    }
    

}

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.