src/com/github/isarthur/netbeans/editor/generator/java/ImportGenerator.java
@Override
    public void invoke() {
        ElementHandle<TypeElement> typeHandle = TypeElementFinder.find(null, null, null);
        if (typeHandle == null) {
            return;
        }
        String qualifiedName = typeHandle.getQualifiedName();
        if (qualifiedName.startsWith("java.lang")) { //NOI18N
            return;
        }
        JavaSource javaSource = JavaSource.forDocument(document);
        if (javaSource == null) {
            throw new IllegalStateException("The document is not associated with data type providing the JavaSource."); //NOI18N
        }
        try {
            javaSource.runModificationTask(copy -> {
                JavaSource.Phase phase = copy.toPhase(JavaSource.Phase.PARSED);
                if (phase.compareTo(JavaSource.Phase.PARSED) < 0) {
                    throw new IllegalStateException("Cannot move state to Phase.PARSED."); //NOI18N
                }
                CompilationUnitTree compilationUnitTree = copy.getCompilationUnit();
                List<? extends ImportTree> imports = compilationUnitTree.getImports();
                for (ImportTree importTree : imports) {
                    String importIdentifier = importTree.getQualifiedIdentifier().toString();
                    if (importIdentifier.equals(qualifiedName)) {
                        return;
                    }
                }
                TreeMaker treeMaker = copy.getTreeMaker();
                ImportTree importTree = treeMaker.Import(treeMaker.Identifier(qualifiedName), false);
                CompilationUnitTree newCompilationUnitTree;
                int insertIndex = findInsertIndexInImportTree(copy);
                if (insertIndex == -1) {
                    newCompilationUnitTree = treeMaker.addCompUnitImport(compilationUnitTree, importTree);
                } else {
                    newCompilationUnitTree =
                            treeMaker.insertCompUnitImport(compilationUnitTree, insertIndex, importTree);
                }
                copy.rewrite(compilationUnitTree, newCompilationUnitTree);
            }).commit();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
/**
     * This will be invoked when user chooses this Generator from Insert Code dialog
     */
This will be invoked when user chooses this Generator from Insert Code dialog
This will be invoked when user chooses this Generator from Insert Code dialog
@Override
    public void invoke() {
        ElementHandle<TypeElement> typeHandle = TypeElementFinder.find(null, null, null);
        if (typeHandle == null) {
            return;
        }
        String qualifiedName = typeHandle.getQualifiedName();
        if (qualifiedName.startsWith("java.lang")) { 
            return;
        }
        JavaSource javaSource = JavaSource.forDocument(document);
        if (javaSource == null) {
            throw new IllegalStateException("The document is not associated with data type providing the JavaSource."); 
        }
        try {
            javaSource.runModificationTask(copy -> {
                JavaSource.Phase phase = copy.toPhase(JavaSource.Phase.PARSED);
                if (phase.compareTo(JavaSource.Phase.PARSED) < 0) {
                    throw new IllegalStateException("Cannot move state to Phase.PARSED."); 
                }
                CompilationUnitTree compilationUnitTree = copy.getCompilationUnit();
                List<? extends ImportTree> imports = compilationUnitTree.getImports();
                for (ImportTree importTree : imports) {
                    String importIdentifier = importTree.getQualifiedIdentifier().toString();
                    if (importIdentifier.equals(qualifiedName)) {
                        return;
                    }
                }
                TreeMaker treeMaker = copy.getTreeMaker();
                ImportTree importTree = treeMaker.Import(treeMaker.Identifier(qualifiedName), false);
                CompilationUnitTree newCompilationUnitTree;
                int insertIndex = findInsertIndexInImportTree(copy);
                if (insertIndex == -1) {
                    newCompilationUnitTree = treeMaker.addCompUnitImport(compilationUnitTree, importTree);
                } else {
                    newCompilationUnitTree =
                            treeMaker.insertCompUnitImport(compilationUnitTree, insertIndex, importTree);
                }
                copy.rewrite(compilationUnitTree, newCompilationUnitTree);
            }).commit();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
@ Override public void invoke ( ) { ElementHandle < TypeElement > typeHandle = TypeElementFinder . find ( null , null , null ) ; if ( typeHandle == null ) { return ; } String qualifiedName = typeHandle . getQualifiedName ( ) ; if ( qualifiedName . startsWith ( "java.lang" ) ) { return ; } JavaSource javaSource = JavaSource . forDocument ( document ) ; if ( javaSource == null ) { throw new IllegalStateException ( "The document is not associated with data type providing the JavaSource." ) ; } try { javaSource . runModificationTask ( copy -> { JavaSource . Phase phase = copy . toPhase ( JavaSource . Phase . PARSED ) ; if ( phase . compareTo ( JavaSource . Phase . PARSED ) < 0 ) { throw new IllegalStateException ( "Cannot move state to Phase.PARSED." ) ; } CompilationUnitTree compilationUnitTree = copy . getCompilationUnit ( ) ; List < ? extends ImportTree > imports = compilationUnitTree . getImports ( ) ; for ( ImportTree importTree : imports ) { String importIdentifier = importTree . getQualifiedIdentifier ( ) . toString ( ) ; if ( importIdentifier . equals ( qualifiedName ) ) { return ; } } TreeMaker treeMaker = copy . getTreeMaker ( ) ; ImportTree importTree = treeMaker . Import ( treeMaker . Identifier ( qualifiedName ) , false ) ; CompilationUnitTree newCompilationUnitTree ; int insertIndex = findInsertIndexInImportTree ( copy ) ; if ( insertIndex == - 1 ) { newCompilationUnitTree = treeMaker . addCompUnitImport ( compilationUnitTree , importTree ) ; } else { newCompilationUnitTree = treeMaker . insertCompUnitImport ( compilationUnitTree , insertIndex , importTree ) ; } copy . rewrite ( compilationUnitTree , newCompilationUnitTree ) ; } ) . commit ( ) ; } catch ( IOException ex ) { Exceptions . printStackTrace ( ex ) ; } }
This will be invoked when user chooses this Generator from Insert Code dialog
This will be invoked when user chooses this Generator from Insert Code dialog
//NOI18N //NOI18N //NOI18N