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-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.loaders;

import java.lang.reflect.InvocationTargetException;
import java.io.IOException;
import java.util.Enumeration;



import org.openide.ServiceType;
import org.openide.actions.AbstractCompileAction;
import org.openide.loaders.MultiDataObject.Entry;
import org.openide.cookies.CompilerCookie;
import org.openide.compiler.Compiler;
import org.openide.compiler.CompilerType;
import org.openide.compiler.Compiler.Depth;
import org.openide.nodes.PropertySupport;
import org.openide.util.enum.*;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;


/** Support for compilation of data objects.
*
* @author Jaroslav Tulach
*/
public class CompilerSupport extends Object implements org.openide.cookies.CompilerCookie {
    /** extended attribute for the type of compiler */
    // [PENDING] this is a misleading name, since it actually points to a handle to a CompilerType
    private static final java.lang.String EA_COMPILER_MANAGER = "NetBeansAttrDataObjectCompilerManager"; // NOI18N
    /** Name of property providing a custom {@link CompilerType} for a file. */
    public static final java.lang.String PROP_COMPILER_TYPE = "compiler"; // NOI18N

    /** entry to be associated with */
    private Entry entry;

    /** cookie class for the compilation */
    private Class cookie;
    
    /**  readOnlyAttrs is name of virtual attribute. This name of virtual attribute 
     * is shared between classes (and should be changed everywhere): 
     * - org.openide.filesystems.DefaultAttributes
     * - org.openide.loaders.ExecutionSupport
     * - org.openide.loaders.CompilerSupport
     * - org.netbeans.core.ExJarFileSystem
     */    
    private final static String READONLY_ATTRIBUTES = "readOnlyAttrs";//NOI18N

    /** New support for given entry. The file is taken from the
    * entry and is updated if the entry moves or renames itself.
    * @param entry entry to create instance from
    * @param cookie cookie class for the compilation (e.g. {@link org.openide.cookies.CompilerCookie.Build})
    */
    protected CompilerSupport(Entry entry, Class cookie) {
        this.entry = entry;
        this.cookie = cookie;
    }

    /** Supports only {@link Compiler#DEPTH_ONE depth one}.
    */
    public boolean isDepthSupported(Depth depth) {
        return Compiler.DEPTH_ONE == depth;
    }

    /* Adds the right compiler to the job.
    */
    public void addToJob(org.openide.compiler.CompilerJob job, Depth depth) {
        CompilerType comp = getCompilerType (entry);
        if (comp == null) {
            comp = defaultCompilerType ();
        }
        if (comp == null) {
            // #15256 etc.
            throw new IllegalStateException(getClass().getName() + ".defaultCompilerType returned null"); // NOI18N
        }

        comp.prepareJob (job, cookie, entry.getDataObject ());
    }

    /** Allows subclasses to override the default compiler that should
    * be used for this support.
    */
    protected org.openide.compiler.CompilerType defaultCompilerType() {
        return CompilerType.getDefault ();
    }

    /** Set compiler manager for a given file.
    * @param entry entry to set the compiler for
    * @param man type to use
    * @exception IOException if compiler cannot be set
    */
    public static void setCompilerType(Entry entry, org.openide.compiler.CompilerType man) throws java.io.IOException {
        entry.getFile ().setAttribute (
            EA_COMPILER_MANAGER, man == null ? null : new ServiceType.Handle (man)
        );
    }

    /** Get compiler manager associated with a given file.
    * @param entry entry to obtain the compiler for
    * @return associated manager or null
    */
    public static org.openide.compiler.CompilerType getCompilerType(Entry entry) {
        Object man = entry.getFile ().getAttribute (EA_COMPILER_MANAGER);
        if (man == null || ! (man instanceof ServiceType.Handle)) return null;
        ServiceType type = ((ServiceType.Handle) man).getServiceType ();
        if (type instanceof CompilerType) {
            return (CompilerType)type;
        }
        
        
        Lookup l = Environment.find (entry.getDataObject ());
        return (CompilerType)l.lookup (CompilerType.class); // compiler type or null
    }

    /** Helper method that creates default properties for compilation of
    * given file entry.
    *
    * @param set sheet set to add properties to
    */
    public void addProperties(org.openide.nodes.Sheet.Set set) {
        set.put (createCompilerProperty ());
    }
    
    /** Utility method to handle compilation on given set of DataObject.
     * Extracts compiler cookies from the provided enumeration of 
     * DataObjects and adds all of them into the provided compiler job.
     *
     * @param job compiler job to add objects to
     * @param en enumeration of DataObject or Node objects
     * @param type class that should be requested as a cookie
     *   (CompilerCookie.Compile,CompilerCookie.Build,
     *   CompilerCookie.Clean)
     * @param depth the initial depth of compilation to start with
     * @deprecated Use {@link AbstractCompileAction#prepareJobFor} instead.
     */
    public static void prepareJob(
        org.openide.compiler.CompilerJob job, Enumeration en, Class type, Depth depth
    ) {
        AbstractCompileAction.prepareJobFor (job, en, type, depth);
    }

    /** Creates the compiler property.
    * @return the property
    */
    private org.openide.nodes.PropertySupport createCompilerProperty() {
        
        return (PropertySupport)new PropertySupport.ReadWrite (
                   PROP_COMPILER_TYPE,
                   CompilerType.class,
                   NbBundle.getMessage(Compiler.class, "PROP_compilerType"),
                   NbBundle.getMessage(Compiler.class, "HINT_compilerType")
               ) {
                   public Object getValue() {
                       CompilerType ct = getCompilerType(entry);
                       if (ct == null)
                           return defaultCompilerType ();
                       else
                           return ct;
                   }
                   public void setValue (Object val) throws InvocationTargetException {
                       try {
                           setCompilerType(entry, (CompilerType) val);
                       } catch (IOException ex) {
                           throw new InvocationTargetException (ex);
                       }
                   }

                   public boolean supportsDefaultValue () {
                       return true;
                   }

                   public void restoreDefaultValue () throws InvocationTargetException {
                       setValue (null);
                   }
                   
                   public boolean canWrite () {
                       Boolean isReadOnly = (Boolean)entry.getFile().getAttribute(READONLY_ATTRIBUTES);
                       return (isReadOnly == null)?false:(!isReadOnly.booleanValue());
                   }
               };
                       
    }


    /** Compile cookie support.
    * Note that as a special case, when {@link Compiler#DEPTH_ONE} is requested,
    * a {@link org.openide.cookies.CompilerCookie.Build} will actually be sent to the compiler manager,
    * rather than a {@link org.openide.cookies.CompilerCookie.Compile}, on the assumption that the user
    * wished to force (re-)compilation of the single data object.
    */
    public static class Compile extends org.openide.loaders.CompilerSupport implements org.openide.cookies.CompilerCookie.Compile {
        /** New support for given entry. The file is taken from the
        * entry and is updated if the entry moves or renames itself.
        * @param entry entry to create instance from
        */
        public Compile(Entry entry) {
            super (entry, CompilerCookie.Compile.class);
        }
    }

    /** Build cookie support.
    */
    public static class Build extends org.openide.loaders.CompilerSupport implements org.openide.cookies.CompilerCookie.Build {
        /** New support for given entry. The file is taken from the
        * entry and is updated if the entry moves or renames itself.
        * @param entry entry to create instance from
        */
        public Build(Entry entry) {
            super (entry, CompilerCookie.Build.class);
        }
    }

    /** Clean cookie support.
    */
    public static class Clean extends org.openide.loaders.CompilerSupport implements org.openide.cookies.CompilerCookie.Clean {
        /** New support for given entry. The file is taken from the
        * entry and is updated if the entry moves or renames itself.
        * @param entry entry to create instance from
        */
        public Clean(Entry entry) {
            super (entry, CompilerCookie.Clean.class);
        }
    }

}
... 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.