diff --git a/openide.awt/src/org/netbeans/modules/openide/awt/MenuProcessor.java b/openide.awt/src/org/netbeans/modules/openide/awt/MenuProcessor.java new file mode 100644 --- /dev/null +++ b/openide.awt/src/org/netbeans/modules/openide/awt/MenuProcessor.java @@ -0,0 +1,131 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.openide.awt; + +import java.util.Set; +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.PackageElement; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; +import javax.lang.model.util.Elements; +import org.openide.awt.MenuRegistration; +import org.openide.filesystems.annotations.LayerBuilder.File; +import org.openide.filesystems.annotations.LayerGeneratingProcessor; +import org.openide.filesystems.annotations.LayerGenerationException; +import org.openide.util.lookup.ServiceProvider; + +/** + * Registration of a menu with its position and display name. Thus, a package + * annotation such as + * + * @MenuRegistration(location={"Data","SubMenu"}, displayName="My Data", position=1000) + * in + * package-info.java + * + * should lead to + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * which could be useful for performing explicit ordering of menus using + * annotations only. Annotating types, fields or methods also supported, though + * usage at package level is envisaged for the most part. + * + * @author Ernest Lötter (elotter@netbeans.org) + */ +@ServiceProvider(service = Processor.class) +@SupportedAnnotationTypes("org.openide.awt.MenuRegistration") +@SupportedSourceVersion(SourceVersion.RELEASE_6) +public class MenuProcessor extends LayerGeneratingProcessor { + @Override + protected boolean handleProcess( + Set set, RoundEnvironment env) + throws LayerGenerationException { + for (Element e : env.getElementsAnnotatedWith(MenuRegistration.class)) { + MenuRegistration menu = null; + if (e instanceof PackageElement) { + menu = ((PackageElement)e).getAnnotation(MenuRegistration.class); + } else if (e instanceof TypeElement) { + menu = ((TypeElement)e).getAnnotation(MenuRegistration.class); + } else if (e instanceof VariableElement) { + menu = ((VariableElement)e).getAnnotation(MenuRegistration.class); + } else { + throw new LayerGenerationException("@MenuRegistration of type "+e+" not supported."); + } + + if (menu.location() == null || menu.location().length == 0) { + throw new LayerGenerationException("Empty location for @MenuRegistration not supported."); + } + + String path = "Menu"; + for (String pathElement : menu.location()) { + path += "/"+pathElement; + } + + File f = layer(e).folder(path); + + if (!menu.displayName().isEmpty()) { + f.bundlevalue("displayName", menu.displayName()); + } + f.intvalue("position", menu.position()); + + f.write(); + } + + return true; + } +} diff --git a/openide.awt/src/org/openide/awt/MenuRegistration.java b/openide.awt/src/org/openide/awt/MenuRegistration.java new file mode 100644 --- /dev/null +++ b/openide.awt/src/org/openide/awt/MenuRegistration.java @@ -0,0 +1,72 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2009 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.openide.awt; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** Registers a menu, with the intent (at this stage) to ensure correct ordering, + * relating to #203556. Usage is envisaged mostly at the package level, in package-info.java. + * + * @author Ernest Lötter (elotter@netbeans.org) + */ +@Retention(RetentionPolicy.SOURCE) +@Target({ElementType.PACKAGE, ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) +public @interface MenuRegistration { + /** Display name. Usually prefixed with '#' to reference value from a + * Bundle.properties file in the same package. + * @return display name for the action + */ + String displayName() default ""; + + /** Menu location, e.g. {"File"} or {"Tools", "Advanced Tools"} + */ + String[] location(); + + /** Position in the location. + */ + int position() default Integer.MAX_VALUE; +}