diff -r 9387523afe84 beans/nbproject/project.xml --- a/beans/nbproject/project.xml Thu Apr 24 17:30:30 2008 +0200 +++ b/beans/nbproject/project.xml Wed Apr 30 11:59:16 2008 +0200 @@ -106,6 +106,15 @@ 2 1.7 + + + + org.netbeans.modules.editor.lib2 + + + + 1 + 1.8 diff -r 9387523afe84 beans/src/org/netbeans/modules/beans/addproperty/AddPropertyCodeGenerator.java --- a/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyCodeGenerator.java Thu Apr 24 17:30:30 2008 +0200 +++ b/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyCodeGenerator.java Wed Apr 30 11:59:16 2008 +0200 @@ -64,6 +64,7 @@ import org.netbeans.api.java.source.JavaSource.Phase; import org.netbeans.api.java.source.Task; import org.netbeans.api.java.source.WorkingCopy; +import org.netbeans.spi.editor.codegen.CodeGenerator; import org.openide.DialogDisplayer; import org.openide.filesystems.FileObject; import org.openide.util.Exceptions; @@ -72,10 +73,10 @@ import javax.swing.text.StyledDocument; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.modules.editor.indent.api.Reformat; -import org.netbeans.modules.java.editor.codegen.CodeGenerator; import org.openide.DialogDescriptor; import org.openide.loaders.DataObject; import org.openide.text.NbDocument; +import org.openide.util.Lookup; import org.openide.util.NbBundle; /** @@ -84,12 +85,14 @@ */ public class AddPropertyCodeGenerator implements CodeGenerator { + private JTextComponent component; private String className; private List existingFields; private String[] pcsName; private String[] vcsName; - public AddPropertyCodeGenerator(String className, List existingFields, String[] pcsName, String[] vcsName) { + public AddPropertyCodeGenerator(JTextComponent component, String className, List existingFields, String[] pcsName, String[] vcsName) { + this.component = component; this.className = className; this.existingFields = existingFields; this.pcsName = pcsName; @@ -100,7 +103,7 @@ return NbBundle.getMessage(AddPropertyCodeGenerator.class, "DN_AddProperty"); } - public void invoke(JTextComponent component) { + public void invoke() { Object o = component.getDocument().getProperty(Document.StreamDescriptionProperty); if (o instanceof DataObject) { @@ -207,12 +210,15 @@ public static final class Factory implements CodeGenerator.Factory { - public Iterable create(CompilationController cc, TreePath path) throws IOException { + public List create(Lookup context) { + JTextComponent component = context.lookup(JTextComponent.class); + CompilationController cc = context.lookup(CompilationController.class); + TreePath path = context.lookup(TreePath.class); while (path != null && path.getLeaf().getKind() != Kind.CLASS) { path = path.getParentPath(); } - if (path == null) { + if (component == null || cc == null || path == null) { return Collections.emptyList(); } @@ -253,7 +259,7 @@ String className = ((TypeElement) e).getQualifiedName().toString(); - return Collections.singleton(new AddPropertyCodeGenerator( className, existingFields, pcsName,vcsName)); + return Collections.singletonList(new AddPropertyCodeGenerator(component, className, existingFields, pcsName,vcsName)); } private static TypeMirror resolve(CompilationInfo info, String s) { diff -r 9387523afe84 beans/src/org/netbeans/modules/beans/resources/mf-layer.xml --- a/beans/src/org/netbeans/modules/beans/resources/mf-layer.xml Thu Apr 24 17:30:30 2008 +0200 +++ b/beans/src/org/netbeans/modules/beans/resources/mf-layer.xml Wed Apr 30 11:59:16 2008 +0200 @@ -141,7 +141,7 @@ - + diff -r 9387523afe84 editor.lib2/apichanges.xml --- a/editor.lib2/apichanges.xml Thu Apr 24 17:30:30 2008 +0200 +++ b/editor.lib2/apichanges.xml Wed Apr 30 11:59:16 2008 +0200 @@ -105,6 +105,24 @@ + + Code Generation SPI added + + + + + + The Code Generation SPI consists of two interfaces. + The CodeGenerator implementations registered for + various mime types serve for creating code snippets and inserting + them into documents on the Insert Code editor action invocation. + The CodeGeneratorContextProvider implementations + registered for the mime types could provide the respective + CodeGenerators with an additional context information. + + + + HighlightAttributeValue interface added diff -r 9387523afe84 editor.lib2/nbproject/project.properties --- a/editor.lib2/nbproject/project.properties Thu Apr 24 17:30:30 2008 +0200 +++ b/editor.lib2/nbproject/project.properties Wed Apr 30 11:59:16 2008 +0200 @@ -40,7 +40,7 @@ is.autoload=true javac.source=1.5 javac.compilerargs=-Xlint:unchecked -spec.version.base=1.7.0 +spec.version.base=1.8.0 javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml diff -r 9387523afe84 editor.lib2/nbproject/project.xml --- a/editor.lib2/nbproject/project.xml Thu Apr 24 17:30:30 2008 +0200 +++ b/editor.lib2/nbproject/project.xml Wed Apr 30 11:59:16 2008 +0200 @@ -143,6 +143,7 @@ org.netbeans.api.editor + org.netbeans.spi.editor.codegen org.netbeans.spi.editor.highlighting org.netbeans.spi.editor.highlighting.support diff -r 9387523afe84 editor.lib2/src/META-INF/services/org.netbeans.spi.editor.mimelookup.Class2LayerFolder --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editor.lib2/src/META-INF/services/org.netbeans.spi.editor.mimelookup.Class2LayerFolder Wed Apr 30 11:59:16 2008 +0200 @@ -0,0 +1,2 @@ + org.netbeans.modules.editor.lib2.codegen.GeneratorFactoryClass2LayerFolder + org.netbeans.modules.editor.lib2.codegen.ContextProviderClass2LayerFolder \ No newline at end of file diff -r 9387523afe84 editor.lib2/src/org/netbeans/modules/editor/lib2/codegen/ContextProviderClass2LayerFolder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editor.lib2/src/org/netbeans/modules/editor/lib2/codegen/ContextProviderClass2LayerFolder.java Wed Apr 30 11:59:16 2008 +0200 @@ -0,0 +1,67 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun 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-2006 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.netbeans.modules.editor.lib2.codegen; + + +import org.netbeans.spi.editor.codegen.CodeGeneratorContextProvider; +import org.netbeans.spi.editor.mimelookup.Class2LayerFolder; +import org.netbeans.spi.editor.mimelookup.InstanceProvider; + +/** + * + * + * @author Dusan Balek + */ +public class ContextProviderClass2LayerFolder implements Class2LayerFolder { + + public Class getClazz() { + return CodeGeneratorContextProvider.class; + } + + public String getLayerFolderName() { + return "CodeGeneratorContextProviders"; //NOI18N + } + + public InstanceProvider getInstanceProvider() { + return null; + } + +} diff -r 9387523afe84 editor.lib2/src/org/netbeans/modules/editor/lib2/codegen/GeneratorFactoryClass2LayerFolder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editor.lib2/src/org/netbeans/modules/editor/lib2/codegen/GeneratorFactoryClass2LayerFolder.java Wed Apr 30 11:59:17 2008 +0200 @@ -0,0 +1,64 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun 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 2007 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.editor.lib2.codegen; + +import org.netbeans.spi.editor.codegen.CodeGenerator.Factory; +import org.netbeans.spi.editor.mimelookup.Class2LayerFolder; +import org.netbeans.spi.editor.mimelookup.InstanceProvider; + +/** + * + * @author Jan Lahoda + */ +public class GeneratorFactoryClass2LayerFolder implements Class2LayerFolder { + + public Class getClazz() { + return Factory.class; + } + + public String getLayerFolderName() { + return "CodeGenerators"; //NOI18N + } + + public InstanceProvider getInstanceProvider() { + return null; + } + +} diff -r 9387523afe84 editor.lib2/src/org/netbeans/spi/editor/codegen/CodeGenerator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editor.lib2/src/org/netbeans/spi/editor/codegen/CodeGenerator.java Wed Apr 30 11:59:17 2008 +0200 @@ -0,0 +1,92 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun 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-2006 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.netbeans.spi.editor.codegen; + +import java.util.List; +import org.openide.util.Lookup; + +/** + * Interface to be implemented by all generators inserting their code snippets + * into documents using the Insert Code editor action. + * + * @author Dusan Balek + * @since 1.8 + */ +public interface CodeGenerator { + + /** + * Gets the generator's name to be displayed in the popup that appears on + * the Insert Code action invocation. + * @return non-null name + * @since 1.8 + */ + public String getDisplayName(); + + /** + * Invokes the generator to create the code snippet and insert it into a + * document. + * @since 1.8 + */ + public void invoke(); + + /** + * Factory creating code generators.
The factory instances are looked up + * by the {@link org.netbeans.api.editor.mimelookup.MimeLookup} so they + * should be registered in an xml-layer in + * Editors/<mime-type>/CodeGenerators directory. + * @since 1.8 + */ + public interface Factory { + + /** + * Creates code generators valid for the particular context. + * @param context Contains an instance of + * {@link javax.swing.text.JTextComponent} by default. Additonal content + * could be added by the {@link CodeGeneratorContextProvider} registered + * for the mime type. + * @return the list of created code generators. An empty list should be + * returned if no generator could be created in the particular context. + * @since 1.8 + */ + public List create(Lookup context); + } +} diff -r 9387523afe84 editor.lib2/src/org/netbeans/spi/editor/codegen/CodeGeneratorContextProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editor.lib2/src/org/netbeans/spi/editor/codegen/CodeGeneratorContextProvider.java Wed Apr 30 11:59:17 2008 +0200 @@ -0,0 +1,81 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. + * + * 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun 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 2008 Sun Microsystems, Inc. + */ + +package org.netbeans.spi.editor.codegen; + +import org.openide.util.Lookup; + +/** + * Serves for adding an additonal content to the context which is passed + * as a parameter to the {@link CodeGeneratorFactory#create()} method.
+ * Instances of this interface are looked up by the + * {@link org.netbeans.api.editor.mimelookup.MimeLookup} so they should be + * registered in an xml-layer in + * Editors/<mime-type>/CodeGeneratorContextProviders directory. + * + * @author Dusan Balek + * @since 1.8 + */ +public interface CodeGeneratorContextProvider { + + /** + * Adds an additional content to the original context and runs the given task + * with the new context as a parameter. + * @param context the original context + * @param task the task to be run + * @since 1.8 + */ + public void runTaskWithinContext(Lookup context, Task task); + + /** + * Represents the task passed to the {@link #runTaskInContext()} method. + * It is supposed to be implemented by the infrastructure implementor + * (not by the SPI clients). + * @since 1.8 + */ + public interface Task { + + /** + * Runs the task. + * @param context the task's context + * @since 1.8 + */ + public void run(Lookup context); + } +} diff -r 9387523afe84 editor/src/org/netbeans/modules/editor/NbEditorKit.java --- a/editor/src/org/netbeans/modules/editor/NbEditorKit.java Thu Apr 24 17:30:30 2008 +0200 +++ b/editor/src/org/netbeans/modules/editor/NbEditorKit.java Wed Apr 30 11:59:17 2008 +0200 @@ -83,6 +83,7 @@ import org.netbeans.editor.Settings; import org.netbeans.editor.SettingsNames; import org.netbeans.editor.ext.ExtSettingsNames; +import org.netbeans.modules.editor.codegen.NbGenerateCodeAction; import org.netbeans.modules.editor.impl.ActionsList; import org.netbeans.modules.editor.impl.CustomizableSideBar; import org.netbeans.modules.editor.impl.SearchBar; @@ -191,6 +192,7 @@ new ToggleToolbarAction(), new NbGenerateGoToPopupAction(), new GenerateFoldPopupAction(), + new NbGenerateCodeAction(), new NavigationHistoryLastEditAction(), new NavigationHistoryBackAction(), new NavigationHistoryForwardAction(), diff -r 9387523afe84 editor/src/org/netbeans/modules/editor/codegen/Bundle.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editor/src/org/netbeans/modules/editor/codegen/Bundle.properties Wed Apr 30 11:59:17 2008 +0200 @@ -0,0 +1,44 @@ +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. +# +# 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. Sun designates this +# particular file as subject to the "Classpath" exception as provided +# by Sun 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-2006 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. + +generate-code-trimmed=Insert Code +desc-generate-code=Insert Code +popup-generate-code=Insert Code... +generate-code-main-menu-source-item=&Insert Code... +LBL_generate_code=Generate diff -r 9387523afe84 editor/src/org/netbeans/modules/editor/codegen/GenerateCodePanel.form --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editor/src/org/netbeans/modules/editor/codegen/GenerateCodePanel.form Wed Apr 30 11:59:17 2008 +0200 @@ -0,0 +1,72 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 9387523afe84 editor/src/org/netbeans/modules/editor/codegen/GenerateCodePanel.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editor/src/org/netbeans/modules/editor/codegen/GenerateCodePanel.java Wed Apr 30 11:59:17 2008 +0200 @@ -0,0 +1,186 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun 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-2006 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.netbeans.modules.editor.codegen; + +import java.awt.Color; +import java.awt.Component; +import java.awt.event.KeyEvent; +import java.util.List; +import javax.swing.DefaultListCellRenderer; +import javax.swing.DefaultListModel; +import javax.swing.JList; +import javax.swing.KeyStroke; +import javax.swing.text.JTextComponent; +import org.netbeans.spi.editor.codegen.CodeGenerator; +import org.openide.util.Utilities; + +/** + * + * @author Dusan Balek, Jan Lahoda + */ +public class GenerateCodePanel extends javax.swing.JPanel { + + private JTextComponent component; + + /** Creates new form GenerateCodePanel */ + public GenerateCodePanel(JTextComponent component, List generators) { + this.component = component; + initComponents(); + setFocusable(false); + setNextFocusableComponent(jList1); + setBackground(jList1.getBackground()); + jScrollPane1.setBackground(jList1.getBackground()); + jList1.setModel(createModel(generators)); + jList1.setSelectedIndex(0); + jList1.setVisibleRowCount(generators.size() > 8 ? 8 : generators.size()); + jList1.setCellRenderer(new Renderer(jList1)); + jList1.grabFocus(); + } + + /** This method is called from within the constructor to + * initialize the form. + * WARNING: Do NOT modify this code. The content of this method is + * always regenerated by the Form Editor. + */ + // //GEN-BEGIN:initComponents + private void initComponents() { + + jScrollPane1 = new javax.swing.JScrollPane(); + jList1 = new javax.swing.JList(); + jLabel1 = new javax.swing.JLabel(); + + setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(64, 64, 64))); + setLayout(new java.awt.BorderLayout()); + + jScrollPane1.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 4, 4)); + + jList1.addKeyListener(new java.awt.event.KeyAdapter() { + public void keyReleased(java.awt.event.KeyEvent evt) { + listKeyReleased(evt); + } + }); + jList1.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseReleased(java.awt.event.MouseEvent evt) { + listMouseReleased(evt); + } + }); + jScrollPane1.setViewportView(jList1); + + add(jScrollPane1, java.awt.BorderLayout.CENTER); + + jLabel1.setText(org.openide.util.NbBundle.getMessage(GenerateCodePanel.class, "LBL_generate_code")); // NOI18N + jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4)); + jLabel1.setOpaque(true); + add(jLabel1, java.awt.BorderLayout.PAGE_START); + }// //GEN-END:initComponents + + private void listMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listMouseReleased + invokeSelected(); + }//GEN-LAST:event_listMouseReleased + + private void listKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_listKeyReleased + KeyStroke ks = KeyStroke.getKeyStrokeForEvent(evt); + if (ks.getKeyCode() == KeyEvent.VK_ENTER || ks.getKeyCode() == KeyEvent.VK_SPACE) + invokeSelected(); + }//GEN-LAST:event_listKeyReleased + + + // Variables declaration - do not modify//GEN-BEGIN:variables + public javax.swing.JLabel jLabel1; + public javax.swing.JList jList1; + public javax.swing.JScrollPane jScrollPane1; + // End of variables declaration//GEN-END:variables + + private DefaultListModel createModel(List generators) { + DefaultListModel model = new DefaultListModel(); + for (CodeGenerator generator : generators) + model.addElement(generator); + return model; + } + + private void invokeSelected() { + PopupUtil.hidePopup(); + if (Utilities.isMac()) { + // see issue #115106 + component.requestFocus(); + } + Object value = jList1.getSelectedValue(); + if (value instanceof CodeGenerator) + ((CodeGenerator)value).invoke(); + } + + private static class Renderer extends DefaultListCellRenderer { + + private static int DARKER_COLOR_COMPONENT = 5; + + private Color fgColor; + private Color bgColor; + private Color bgColorDarker; + private Color bgSelectionColor; + private Color fgSelectionColor; + + public Renderer(JList list) { + setFont(list.getFont()); + fgColor = list.getForeground(); + bgColor = list.getBackground(); + bgColorDarker = new Color(Math.abs(bgColor.getRed() - DARKER_COLOR_COMPONENT), + Math.abs(bgColor.getGreen() - DARKER_COLOR_COMPONENT), + Math.abs(bgColor.getBlue() - DARKER_COLOR_COMPONENT)); + bgSelectionColor = list.getSelectionBackground(); + fgSelectionColor = list.getSelectionForeground(); + } + + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, + boolean isSelected, boolean hasFocus) { + if (isSelected) { + setForeground(fgSelectionColor); + setBackground(bgSelectionColor); + } else { + setForeground(fgColor); + setBackground(index % 2 == 0 ? bgColor : bgColorDarker); + } + setText(value instanceof CodeGenerator ? ((CodeGenerator)value).getDisplayName() : value.toString()); + return this; + } + } +} diff -r 9387523afe84 editor/src/org/netbeans/modules/editor/codegen/NbGenerateCodeAction.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editor/src/org/netbeans/modules/editor/codegen/NbGenerateCodeAction.java Wed Apr 30 11:59:17 2008 +0200 @@ -0,0 +1,174 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun 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-2007 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.netbeans.modules.editor.codegen; + +import javax.swing.text.AbstractDocument; +import javax.swing.text.Document; +import org.netbeans.api.editor.mimelookup.MimePath; +import org.netbeans.api.lexer.TokenHierarchy; +import org.netbeans.api.lexer.TokenSequence; +import org.netbeans.modules.editor.*; +import java.awt.Point; +import java.awt.Rectangle; +import java.awt.event.ActionEvent; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import javax.swing.JMenuItem; +import javax.swing.SwingUtilities; +import javax.swing.text.BadLocationException; +import javax.swing.text.JTextComponent; +import org.netbeans.api.editor.mimelookup.MimeLookup; +import org.netbeans.editor.BaseAction; +import org.netbeans.editor.ext.ExtKit; +import org.netbeans.spi.editor.codegen.CodeGenerator; +import org.netbeans.spi.editor.codegen.CodeGeneratorContextProvider; +import org.openide.util.Lookup; +import org.openide.util.NbBundle; +import org.openide.util.lookup.Lookups; + +/** + * + * @author Dusan Balek, Jan Lahoda + */ +public class NbGenerateCodeAction extends BaseAction { + + public static final String generateCode = "generate-code"; //NOI18N + + public NbGenerateCodeAction(){ + super(generateCode); + putValue(ExtKit.TRIMMED_TEXT, NbBundle.getBundle(NbGenerateCodeAction.class).getString("generate-code-trimmed")); //NOI18N + putValue(SHORT_DESCRIPTION, NbBundle.getBundle(NbGenerateCodeAction.class).getString("desc-generate-code")); //NOI18N + putValue(POPUP_MENU_TEXT, NbBundle.getBundle(NbGenerateCodeAction.class).getString("popup-generate-code")); //NOI18N + } + + public void actionPerformed(ActionEvent evt, final JTextComponent target) { + Task task = new Task(getFullMimePath(target.getDocument(), target.getCaretPosition())); + task.run(Lookups.singleton(target)); + if (task.codeGenerators.size() > 0) { + int altHeight = -1; + Point where = null; + try { + Rectangle carretRectangle = target.modelToView(target.getCaretPosition()); + altHeight = carretRectangle.height; + where = new Point( carretRectangle.x, carretRectangle.y + carretRectangle.height ); + SwingUtilities.convertPointToScreen(where, target); + } catch (BadLocationException ble) { + } + if (where == null) + where = new Point(-1, -1); + PopupUtil.showPopup(new GenerateCodePanel(target, task.codeGenerators), null, where.x, where.y, true, altHeight); + } else { + target.getToolkit().beep(); + } + } + + private static MimePath getFullMimePath(Document document, int offset) { + String langPath = null; + + if (document instanceof AbstractDocument) { + AbstractDocument adoc = (AbstractDocument)document; + adoc.readLock(); + try { + List> list = TokenHierarchy.get(document).embeddedTokenSequences(offset, true); + if (list.size() > 1) { + langPath = list.get(list.size() - 1).languagePath().mimePath(); + } + } finally { + adoc.readUnlock(); + } + } + + if (langPath == null) { + langPath = NbEditorUtilities.getMimeType(document); + } + + if (langPath != null) { + return MimePath.parse(langPath); + } else { + return null; + } + } + + private static class Task implements CodeGeneratorContextProvider.Task { + private MimePath mimePath; + private Iterator contextProviders; + private List codeGenerators = new ArrayList(); + + private Task(MimePath mimePath) { + this.mimePath = mimePath; + contextProviders = MimeLookup.getLookup(mimePath).lookupAll(CodeGeneratorContextProvider.class).iterator(); + } + + public void run(Lookup context) { + if (contextProviders.hasNext()) { + contextProviders.next().runTaskWithinContext(context, this); + } else { + for (CodeGenerator.Factory factory : MimeLookup.getLookup(mimePath).lookupAll(CodeGenerator.Factory.class)) + codeGenerators.addAll(factory.create(context)); + } + } + } + + public static final class GlobalAction extends MainMenuAction { + + private final JMenuItem menuPresenter; + + public GlobalAction() { + super(); + this.menuPresenter = new JMenuItem(getMenuItemText()); + setMenu(); + } + + protected String getMenuItemText() { + return NbBundle.getBundle(GlobalAction.class).getString("generate-code-main-menu-source-item"); //NOI18N + } + + protected String getActionName() { + return generateCode; + } + + public JMenuItem getMenuPresenter() { + return menuPresenter; + } + + } // End of GlobalAction class +} diff -r 9387523afe84 editor/src/org/netbeans/modules/editor/codegen/PopupUtil.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editor/src/org/netbeans/modules/editor/codegen/PopupUtil.java Wed Apr 30 11:59:17 2008 +0200 @@ -0,0 +1,274 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun 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-2007 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.netbeans.modules.editor.codegen; + +import java.awt.AWTEvent; +import java.awt.Component; +import java.awt.Container; +import java.awt.Frame; +import java.awt.Point; +import java.awt.Rectangle; +import java.awt.Toolkit; +import java.awt.event.AWTEventListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.awt.event.FocusListener; +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.awt.event.WindowEvent; +import java.awt.event.WindowStateListener; +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.KeyStroke; +import javax.swing.SwingUtilities; +import org.openide.windows.WindowManager; + +/** + * + * @author phrebejk + */ +public final class PopupUtil { + + // private static MyFocusListener mfl = new MyFocusListener(); + + private static final String CLOSE_KEY = "CloseKey"; //NOI18N + private static final Action CLOSE_ACTION = new CloseAction(); + private static final KeyStroke ESC_KEY_STROKE = KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ); + + private static final String POPUP_NAME = "popupComponent"; //NOI18N + private static JDialog popupWindow; + private static HideAWTListener hideListener = new HideAWTListener(); + + // Singleton + private PopupUtil() { + } + + + public static void showPopup( JComponent content, String title ) { + showPopup(content, title, -1, -1, false); + } + + public static void showPopup( JComponent content, String title, int x, int y, boolean undecorated ) { + showPopup(content, title, x, y, false, -1 ); + } + + public static void showPopup( JComponent content, String title, int x, int y, boolean undecorated, int altHeight ) { + if (popupWindow != null ) { + return; // Content already showing + } + + Toolkit.getDefaultToolkit().addAWTEventListener(hideListener, AWTEvent.MOUSE_EVENT_MASK); + + // NOT using PopupFactory + // 1. on linux, creates mediumweight popup taht doesn't refresh behind visible glasspane + // 2. on mac, needs an owner frame otherwise hiding tooltip also hides the popup. (linux requires no owner frame to force heavyweight) + // 3. the created window is not focusable window + + popupWindow = new JDialog( getMainWindow() ); + popupWindow.setName( POPUP_NAME ); + popupWindow.setUndecorated(undecorated); + popupWindow.getRootPane().getInputMap( JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ).put( ESC_KEY_STROKE, CLOSE_KEY ); + popupWindow.getRootPane().getActionMap().put( CLOSE_KEY, CLOSE_ACTION ); + + //set a11y + String a11yName = content.getAccessibleContext().getAccessibleName(); + if(a11yName != null && !a11yName.equals("")) + popupWindow.getAccessibleContext().setAccessibleName(a11yName); + String a11yDesc = content.getAccessibleContext().getAccessibleDescription(); + if(a11yDesc != null && !a11yDesc.equals("")) + popupWindow.getAccessibleContext().setAccessibleDescription(a11yDesc); + + if ( title != null ) { + // popupWindow.setTitle( title ); + } + // popupWindow.setAlwaysOnTop( true ); + popupWindow.getContentPane().add(content); + // popupWindow.addFocusListener( mfl ); + // content.addFocusListener( mfl ); + + WindowManager.getDefault().getMainWindow().addWindowStateListener(hideListener); + WindowManager.getDefault().getMainWindow().addComponentListener(hideListener); + resizePopup(); + + if (x != (-1)) { + Point p = fitToScreen( x, y, altHeight ); + popupWindow.setLocation(p.x, p.y); + + } + + popupWindow.setVisible( true ); + // System.out.println(" RFIW ==" + popupWindow.requestFocusInWindow() ); + content.requestFocus(); + content.requestFocusInWindow(); +// System.out.println(" has focus =" + content.hasFocus()); +// System.out.println(" has focus =" + popupWindow.hasFocus()); +// System.out.println(" window focusable=" + popupWindow.isFocusableWindow()); + } + + public static void hidePopup() { + if (popupWindow != null) { +// popupWindow.getContentPane().removeAll(); + Toolkit.getDefaultToolkit().removeAWTEventListener(hideListener); + + popupWindow.setVisible( false ); + popupWindow.dispose(); + } + WindowManager.getDefault().getMainWindow().removeWindowStateListener(hideListener); + WindowManager.getDefault().getMainWindow().removeComponentListener(hideListener); + popupWindow = null; + } + + + private static void resizePopup() { + popupWindow.pack(); + Point point = new Point(0,0); + SwingUtilities.convertPointToScreen(point, getMainWindow()); + popupWindow.setLocation( point.x + (getMainWindow().getWidth() - popupWindow.getWidth()) / 2, + point.y + (getMainWindow().getHeight() - popupWindow.getHeight()) / 3); + } + + private static final int X_INSET = 10; + private static final int Y_INSET = X_INSET; + + private static Point fitToScreen( int x, int y, int altHeight ) { + + Rectangle screen = org.openide.util.Utilities.getUsableScreenBounds(); + + Point p = new Point( x, y ); + + // Adjust the x postition if necessary + if ( ( p.x + popupWindow.getWidth() ) > ( screen.x + screen.width - X_INSET ) ) { + p.x = screen.x + screen.width - X_INSET - popupWindow.getWidth(); + } + + // Adjust the y position if necessary + if ( ( p.y + popupWindow.getHeight() ) > ( screen.y + screen.height - X_INSET ) ) { + p.y = p.y - popupWindow.getHeight() - altHeight; + } + + return p; + } + + + private static Frame getMainWindow() { + return WindowManager.getDefault().getMainWindow(); + } + + // Innerclasses ------------------------------------------------------------ + + private static class HideAWTListener extends ComponentAdapter implements AWTEventListener, WindowStateListener { + + public void eventDispatched(java.awt.AWTEvent aWTEvent) { + if (aWTEvent instanceof MouseEvent) { + MouseEvent mv = (MouseEvent)aWTEvent; + if (mv.getID() == MouseEvent.MOUSE_CLICKED && mv.getClickCount() > 0) { + //#118828 + if (! (aWTEvent.getSource() instanceof Component)) { + hidePopup(); + return; + } + + Component comp = (Component)aWTEvent.getSource(); + Container par = SwingUtilities.getAncestorNamed(POPUP_NAME, comp); //NOI18N + // Container barpar = SwingUtilities.getAncestorOfClass(PopupUtil.class, comp); + // if (par == null && barpar == null) { + if ( par == null ) { + hidePopup(); + } + } + } + } + + public void windowStateChanged(WindowEvent windowEvent) { + if (popupWindow != null ) { + int oldState = windowEvent.getOldState(); + int newState = windowEvent.getNewState(); + + if (((oldState & Frame.ICONIFIED) == 0) && + ((newState & Frame.ICONIFIED) == Frame.ICONIFIED)) { + hidePopup(); +// } else if (((oldState & Frame.ICONIFIED) == Frame.ICONIFIED) && +// ((newState & Frame.ICONIFIED) == 0 )) { +// //TODO remember we showed before and show again? I guess not worth the efford, not part of spec. + } + } + + } + + public void componentResized(ComponentEvent evt) { + if (popupWindow != null) { + resizePopup(); + } + } + + public void componentMoved(ComponentEvent evt) { + if (popupWindow!= null) { + resizePopup(); + } + } + + } + + private static class MyFocusListener implements FocusListener { + + public void focusLost(java.awt.event.FocusEvent e) { + System.out.println( e ); + } + + public void focusGained(java.awt.event.FocusEvent e) { + System.out.println( e ); + } + + } + + private static class CloseAction extends AbstractAction { + + public void actionPerformed(java.awt.event.ActionEvent e) { + hidePopup(); + } + + + } + +} diff -r 9387523afe84 editor/src/org/netbeans/modules/editor/resources/layer.xml --- a/editor/src/org/netbeans/modules/editor/resources/layer.xml Thu Apr 24 17:30:30 2008 +0200 +++ b/editor/src/org/netbeans/modules/editor/resources/layer.xml Wed Apr 30 11:59:17 2008 +0200 @@ -191,6 +191,9 @@ + + + diff -r 9387523afe84 java.editor/nbproject/project.xml --- a/java.editor/nbproject/project.xml Thu Apr 24 17:30:30 2008 +0200 +++ b/java.editor/nbproject/project.xml Wed Apr 30 11:59:17 2008 +0200 @@ -127,7 +127,7 @@ 1 - 1.2 + 1.8
diff -r 9387523afe84 java.editor/src/META-INF/services/org.netbeans.spi.editor.mimelookup.Class2LayerFolder --- a/java.editor/src/META-INF/services/org.netbeans.spi.editor.mimelookup.Class2LayerFolder Thu Apr 24 17:30:30 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -org.netbeans.modules.java.editor.codegen.Class2Folder diff -r 9387523afe84 java.editor/src/org/netbeans/modules/editor/java/JavaKit.java --- a/java.editor/src/org/netbeans/modules/editor/java/JavaKit.java Thu Apr 24 17:30:30 2008 +0200 +++ b/java.editor/src/org/netbeans/modules/editor/java/JavaKit.java Wed Apr 30 11:59:17 2008 +0200 @@ -59,15 +59,12 @@ import org.netbeans.api.java.queries.SourceLevelQuery; import org.netbeans.api.lexer.TokenHierarchy; import org.netbeans.api.lexer.TokenSequence; -import org.netbeans.editor.ext.ExtKit.CommentAction; import org.netbeans.editor.ext.ExtKit.PrefixMakerAction; -import org.netbeans.editor.ext.ExtKit.UncommentAction; import org.netbeans.lib.editor.codetemplates.api.CodeTemplateManager; import org.netbeans.modules.editor.MainMenuAction; import org.netbeans.modules.editor.NbEditorKit; import org.netbeans.modules.editor.NbEditorUtilities; import org.netbeans.modules.java.editor.codegen.InsertSemicolonAction; -import org.netbeans.modules.java.editor.codegen.GenerateCodeAction; import org.netbeans.modules.java.editor.imports.FastImportAction; import org.netbeans.modules.java.editor.imports.JavaFixAllImports; import org.netbeans.modules.java.editor.overridden.GoToSuperTypeAction; @@ -252,7 +249,6 @@ new JavaGotoHelpAction(), new InstantRenameAction(), new JavaFixImports(), - new GenerateCodeAction(), new InsertSemicolonAction(true), new InsertSemicolonAction(false), new SelectCodeElementAction(selectNextElementAction, true), diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/Bundle.properties --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/Bundle.properties Thu Apr 24 17:30:30 2008 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/codegen/Bundle.properties Wed Apr 30 11:59:17 2008 +0200 @@ -37,14 +37,9 @@ # Version 2 license, then the option applies only if the new code is # made subject to such option by the copyright holder. -generate-code-trimmed=Insert Code -desc-generate-code=Insert Code -popup-generate-code=Insert Code... -generate-code-main-menu-source-item=&Insert Code... complete-line-newline=Complete Line and Create New Line complete-line=Complete Line -LBL_generate_code=Generate LBL_constructor=Constructor... LBL_generate_constructor=Generate Constructor LBL_super_constructor_select=Select super constructor: diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/Class2Folder.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/Class2Folder.java Thu Apr 24 17:30:30 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. - * - * 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. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun 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 2007 Sun Microsystems, Inc. - */ - -package org.netbeans.modules.java.editor.codegen; - -import org.netbeans.modules.java.editor.codegen.CodeGenerator.Factory; -import org.netbeans.spi.editor.mimelookup.Class2LayerFolder; -import org.netbeans.spi.editor.mimelookup.InstanceProvider; - -/** - * - * @author Jan Lahoda - */ -public class Class2Folder implements Class2LayerFolder { - - public Class getClazz() { - return Factory.class; - } - - public String getLayerFolderName() { - return "codegenerators"; //NOI18N - } - - public InstanceProvider getInstanceProvider() { - return null; - } - -} diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/CodeGenerator.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/CodeGenerator.java Thu Apr 24 17:30:30 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. - * - * 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. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun 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-2006 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.netbeans.modules.java.editor.codegen; - -import com.sun.source.util.TreePath; -import java.io.IOException; -import javax.swing.text.JTextComponent; -import org.netbeans.api.java.source.CompilationController; - -/** - * - * @author Dusan Balek - */ -public interface CodeGenerator { - - public String getDisplayName(); - public void invoke(JTextComponent component); - - public interface Factory { - public Iterable create(CompilationController controller, TreePath path) throws IOException; - } -} diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/ConstructorGenerator.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/ConstructorGenerator.java Thu Apr 24 17:30:30 2008 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/codegen/ConstructorGenerator.java Wed Apr 30 11:59:17 2008 +0200 @@ -46,7 +46,6 @@ import java.awt.Dialog; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -70,9 +69,11 @@ import org.netbeans.modules.editor.java.Utilities; import org.netbeans.modules.java.editor.codegen.ui.ConstructorPanel; import org.netbeans.modules.java.editor.codegen.ui.ElementNode; +import org.netbeans.spi.editor.codegen.CodeGenerator; import org.openide.DialogDescriptor; import org.openide.DialogDisplayer; import org.openide.util.Exceptions; +import org.openide.util.Lookup; import org.openide.util.NbBundle; /** @@ -83,17 +84,22 @@ public static class Factory implements CodeGenerator.Factory { - public Factory() { - } - - public Iterable create(CompilationController controller, TreePath path) throws IOException { - path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path); - if (path == null) - return Collections.emptySet(); - controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + public List create(Lookup context) { + ArrayList ret = new ArrayList(); + JTextComponent component = context.lookup(JTextComponent.class); + CompilationController controller = context.lookup(CompilationController.class); + TreePath path = context.lookup(TreePath.class); + path = path != null ? Utilities.getPathElementOfKind(Tree.Kind.CLASS, path) : null; + if (component == null || controller == null || path == null) + return ret; + try { + controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + } catch (IOException ioe) { + return ret; + } TypeElement typeElement = (TypeElement)controller.getTrees().getElement(path); if (typeElement == null || !typeElement.getKind().isClass() || NestingKind.ANONYMOUS.equals(typeElement.getNestingKind())) - return Collections.emptySet(); + return ret; final Set initializedFields = new LinkedHashSet(); final Set uninitializedFields = new LinkedHashSet(); final List constructors = new ArrayList(); @@ -124,18 +130,20 @@ fieldDescriptions.add(ElementNode.Description.create(variableElement, null, true, false)); fieldsDescription = ElementNode.Description.create(typeElement, fieldDescriptions, false, false); } - if (constructorHandle == null && constructorDescription == null && fieldsDescription == null) - return Collections.emptySet(); - return Collections.singleton(new ConstructorGenerator(constructorHandle, constructorDescription, fieldsDescription)); + if (constructorHandle != null || constructorDescription != null || fieldsDescription != null) + ret.add(new ConstructorGenerator(component, constructorHandle, constructorDescription, fieldsDescription)); + return ret; } } + private JTextComponent component; private ElementHandle constructorHandle; private ElementNode.Description constructorDescription; private ElementNode.Description fieldsDescription; /** Creates a new instance of ConstructorGenerator */ - private ConstructorGenerator(ElementHandle constructorHandle, ElementNode.Description constructorDescription, ElementNode.Description fieldsDescription) { + private ConstructorGenerator(JTextComponent component, ElementHandle constructorHandle, ElementNode.Description constructorDescription, ElementNode.Description fieldsDescription) { + this.component = component; this.constructorHandle = constructorHandle; this.constructorDescription = constructorDescription; this.fieldsDescription = fieldsDescription; @@ -145,7 +153,7 @@ return org.openide.util.NbBundle.getMessage(ConstructorGenerator.class, "LBL_constructor"); //NOI18N } - public void invoke(JTextComponent component) { + public void invoke() { final List> fieldHandles; final List> constrHandles; if (constructorDescription != null || fieldsDescription != null) { diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/ContextProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java.editor/src/org/netbeans/modules/java/editor/codegen/ContextProvider.java Wed Apr 30 11:59:17 2008 +0200 @@ -0,0 +1,81 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun 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-2007 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.netbeans.modules.java.editor.codegen; + +import com.sun.source.util.TreePath; +import java.io.IOException; +import javax.swing.text.JTextComponent; +import org.netbeans.api.java.source.CompilationController; +import org.netbeans.api.java.source.JavaSource; +import org.netbeans.spi.editor.codegen.CodeGeneratorContextProvider; +import org.openide.util.Lookup; +import org.openide.util.lookup.Lookups; +import org.openide.util.lookup.ProxyLookup; + +/** + * + * @author Dusan Balek + */ +public class ContextProvider implements CodeGeneratorContextProvider { + + public void runTaskWithinContext(final Lookup context, final Task task) { + JTextComponent component = context.lookup(JTextComponent.class); + if (component != null) { + try { + JavaSource js = JavaSource.forDocument(component.getDocument()); + if (js != null) { + final int caretOffset = component.getCaretPosition(); + js.runUserActionTask(new org.netbeans.api.java.source.Task() { + public void run(CompilationController controller) throws Exception { + controller.toPhase(JavaSource.Phase.PARSED); + TreePath path = controller.getTreeUtilities().pathFor(caretOffset); + Lookup newContext = new ProxyLookup(context, Lookups.fixed(controller, path)); + task.run(newContext); + } + }, true); + return; + } + } catch (IOException ioe) { + } + } + task.run(context); + } +} diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/DelegateMethodGenerator.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/DelegateMethodGenerator.java Thu Apr 24 17:30:30 2008 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/codegen/DelegateMethodGenerator.java Wed Apr 30 11:59:17 2008 +0200 @@ -40,6 +40,7 @@ */ package org.netbeans.modules.java.editor.codegen; +import org.netbeans.spi.editor.codegen.CodeGenerator; import com.sun.source.tree.BlockTree; import com.sun.source.tree.ClassTree; import com.sun.source.tree.ExpressionTree; @@ -81,6 +82,7 @@ import org.openide.DialogDescriptor; import org.openide.DialogDisplayer; import org.openide.util.Exceptions; +import org.openide.util.Lookup; import org.openide.util.NbBundle; /** @@ -93,26 +95,34 @@ public static class Factory implements CodeGenerator.Factory { - public Factory() { - } - - public Iterable create(CompilationController controller, TreePath path) throws IOException { - path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path); - if (path == null) - return Collections.emptySet(); - controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + public List create(Lookup context) { + ArrayList ret = new ArrayList(); + JTextComponent component = context.lookup(JTextComponent.class); + CompilationController controller = context.lookup(CompilationController.class); + TreePath path = context.lookup(TreePath.class); + path = path != null ? Utilities.getPathElementOfKind(Tree.Kind.CLASS, path) : null; + if (component == null || controller == null || path == null) + return ret; + try { + controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + } catch (IOException ioe) { + return ret; + } List descriptions = computeUsableFieldsDescriptions(controller, path); - if (descriptions.isEmpty()) - return Collections.emptySet(); - Collections.reverse(descriptions); - return Collections.singleton(new DelegateMethodGenerator(ElementNode.Description.create(descriptions))); + if (!descriptions.isEmpty()) { + Collections.reverse(descriptions); + ret.add(new DelegateMethodGenerator(component, ElementNode.Description.create(descriptions))); + } + return ret; } } + private JTextComponent component; private ElementNode.Description description; /** Creates a new instance of DelegateMethodGenerator */ - private DelegateMethodGenerator(ElementNode.Description description) { + private DelegateMethodGenerator(JTextComponent component, ElementNode.Description description) { + this.component = component; this.description = description; } @@ -120,7 +130,7 @@ return org.openide.util.NbBundle.getMessage(DelegateMethodGenerator.class, "LBL_delegate_method"); //NOI18N } - public void invoke(JTextComponent component) { + public void invoke() { final DelegatePanel panel = new DelegatePanel(component, description); DialogDescriptor dialogDescriptor = GeneratorUtils.createDialogDescriptor(panel, NbBundle.getMessage(ConstructorGenerator.class, "LBL_generate_delegate")); //NOI18N Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/EqualsHashCodeGenerator.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/EqualsHashCodeGenerator.java Thu Apr 24 17:30:30 2008 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/codegen/EqualsHashCodeGenerator.java Wed Apr 30 11:59:17 2008 +0200 @@ -40,6 +40,7 @@ */ package org.netbeans.modules.java.editor.codegen; +import org.netbeans.spi.editor.codegen.CodeGenerator; import com.sun.source.tree.AnnotationTree; import com.sun.source.tree.BlockTree; import com.sun.source.tree.ClassTree; @@ -91,6 +92,7 @@ import org.openide.DialogDescriptor; import org.openide.DialogDisplayer; import org.openide.util.Exceptions; +import org.openide.util.Lookup; import org.openide.util.NbBundle; /** @@ -103,35 +105,36 @@ public static class Factory implements CodeGenerator.Factory { - public Factory() { - } - - public Iterable create(CompilationController cc, TreePath path) throws IOException { - path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path); - if (path == null) - return Collections.emptySet(); - cc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); - - Element elem = cc.getTrees().getElement(path); - if (elem == null) { - return Collections.emptySet(); + public List create(Lookup context) { + ArrayList ret = new ArrayList(); + JTextComponent component = context.lookup(JTextComponent.class); + CompilationController controller = context.lookup(CompilationController.class); + TreePath path = context.lookup(TreePath.class); + path = path != null ? Utilities.getPathElementOfKind(Tree.Kind.CLASS, path) : null; + if (component == null || controller == null || path == null) + return ret; + try { + controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + Element elem = controller.getTrees().getElement(path); + if (elem != null) { + EqualsHashCodeGenerator gen = createEqualsHashCodeGenerator(component, controller, elem); + if (gen != null) + ret.add(gen); + } + } catch (IOException ioe) { } - EqualsHashCodeGenerator gen = createEqualsHashCodeGenerator(cc, elem); - - if (gen == null) { - return Collections.emptySet(); - } else { - return Collections.singleton(gen); - } + return ret; } } - final ElementNode.Description description; - final boolean generateEquals; - final boolean generateHashCode; + private JTextComponent component; + private ElementNode.Description description; + private boolean generateEquals; + private boolean generateHashCode; /** Creates a new instance of EqualsHashCodeGenerator */ - private EqualsHashCodeGenerator(ElementNode.Description description, boolean generateEquals, boolean generateHashCode) { + private EqualsHashCodeGenerator(JTextComponent component, ElementNode.Description description, boolean generateEquals, boolean generateHashCode) { + this.component = component; this.description = description; this.generateEquals = generateEquals; this.generateHashCode = generateHashCode; @@ -146,7 +149,7 @@ return org.openide.util.NbBundle.getMessage(EqualsHashCodeGenerator.class, "LBL_equals"); //NOI18N } - static EqualsHashCodeGenerator createEqualsHashCodeGenerator(CompilationController cc, Element el) throws IOException { + static EqualsHashCodeGenerator createEqualsHashCodeGenerator(JTextComponent component, CompilationController cc, Element el) throws IOException { if (el.getKind() != ElementKind.CLASS) return null; //#125114: ignore anonymous innerclasses: @@ -165,6 +168,7 @@ if (descriptions.isEmpty() || (equalsHashCode[0] != null && equalsHashCode[1] != null)) return null; return new EqualsHashCodeGenerator( + component, ElementNode.Description.create(typeElement, descriptions, false, false), equalsHashCode[0] == null, equalsHashCode[1] == null @@ -280,12 +284,12 @@ cc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); Element e = handle.resolveElement(cc); - gen = createEqualsHashCodeGenerator(cc, e); + gen = createEqualsHashCodeGenerator(component, cc, e); } public void invoke() { if (gen != null) { - gen.invoke(component); + gen.invoke(); } } @@ -300,7 +304,7 @@ } } - public void invoke(JTextComponent component) { + public void invoke() { final EqualsHashCodePanel panel = new EqualsHashCodePanel(description, generateEquals, generateHashCode); String title = NbBundle.getMessage(ConstructorGenerator.class, "LBL_generate_equals_and_hashcode"); //NOI18N if( !generateEquals ) diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/GenerateCodeAction.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/GenerateCodeAction.java Thu Apr 24 17:30:30 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,141 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. - * - * 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. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun 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-2007 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.netbeans.modules.java.editor.codegen; - -import com.sun.source.util.TreePath; -import java.awt.Point; -import java.awt.Rectangle; -import java.awt.event.ActionEvent; -import java.io.IOException; -import java.util.ArrayList; -import javax.swing.JMenuItem; -import javax.swing.SwingUtilities; -import javax.swing.text.BadLocationException; -import javax.swing.text.JTextComponent; -import org.netbeans.api.editor.mimelookup.MimeLookup; -import org.netbeans.api.java.source.Task; -import org.netbeans.api.java.source.CompilationController; -import org.netbeans.api.java.source.JavaSource; -import org.netbeans.editor.BaseAction; -import org.netbeans.editor.ext.ExtKit; -import org.netbeans.modules.editor.MainMenuAction; -import org.netbeans.modules.java.editor.codegen.ui.GenerateCodePanel; -import org.netbeans.modules.java.editor.overridden.PopupUtil; -import org.openide.util.Exceptions; -import org.openide.util.NbBundle; - -/** - * - * @author Dusan Balek, Jan Lahoda - */ -public class GenerateCodeAction extends BaseAction { - - public static final String generateCode = "generate-code"; //NOI18N - - public GenerateCodeAction(){ - super(generateCode); - putValue(ExtKit.TRIMMED_TEXT, NbBundle.getBundle(GenerateCodeAction.class).getString("generate-code-trimmed")); //NOI18N - putValue(SHORT_DESCRIPTION, NbBundle.getBundle(GenerateCodeAction.class).getString("desc-generate-code")); //NOI18N - putValue(POPUP_MENU_TEXT, NbBundle.getBundle(GenerateCodeAction.class).getString("popup-generate-code")); //NOI18N - } - - public void actionPerformed(ActionEvent evt, final JTextComponent target) { - try { - JavaSource js = JavaSource.forDocument(target.getDocument()); - if (js != null) { - final int caretOffset = target.getCaretPosition(); - final ArrayList gens = new ArrayList(); - js.runUserActionTask(new Task() { - - public void run(CompilationController controller) throws Exception { - controller.toPhase(JavaSource.Phase.PARSED); - TreePath path = controller.getTreeUtilities().pathFor(caretOffset); - for (CodeGenerator.Factory factory : getCodeGeneratorFactories()) { - for (CodeGenerator gen : factory.create(controller, path)) - gens.add(gen); - } - } - }, true); - if (gens.size() > 0) { - Rectangle carretRectangle = target.modelToView(target.getCaretPosition()); - Point where = new Point( carretRectangle.x, carretRectangle.y + carretRectangle.height ); - SwingUtilities.convertPointToScreen( where, target); - GenerateCodePanel panel = new GenerateCodePanel(target, gens); - PopupUtil.showPopup(panel, null, where.x, where.y, true, carretRectangle.height); - } else { - target.getToolkit().beep(); - } - } - } catch (IOException ioe) { - Exceptions.printStackTrace(ioe); - } catch (BadLocationException ble) { - Exceptions.printStackTrace(ble); - } - } - - private Iterable getCodeGeneratorFactories() { - return MimeLookup.getLookup("text/x-java").lookupAll(CodeGenerator.Factory.class); - } - - public static final class GlobalAction extends MainMenuAction { - - private final JMenuItem menuPresenter; - - public GlobalAction() { - super(); - this.menuPresenter = new JMenuItem(getMenuItemText()); - setMenu(); - } - - protected String getMenuItemText() { - return NbBundle.getBundle(GlobalAction.class).getString("generate-code-main-menu-source-item"); //NOI18N - } - - protected String getActionName() { - return generateCode; - } - - public JMenuItem getMenuPresenter() { - return menuPresenter; - } - - } // End of GlobalAction class -} diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/GetterSetterGenerator.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/GetterSetterGenerator.java Thu Apr 24 17:30:30 2008 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/codegen/GetterSetterGenerator.java Wed Apr 30 11:59:17 2008 +0200 @@ -40,6 +40,7 @@ */ package org.netbeans.modules.java.editor.codegen; +import org.netbeans.spi.editor.codegen.CodeGenerator; import com.sun.source.tree.ClassTree; import com.sun.source.tree.Tree; import com.sun.source.util.TreePath; @@ -71,6 +72,7 @@ import org.openide.DialogDescriptor; import org.openide.DialogDisplayer; import org.openide.util.Exceptions; +import org.openide.util.Lookup; import org.openide.util.NbBundle; /** @@ -82,16 +84,20 @@ public static class Factory implements CodeGenerator.Factory { private static final String ERROR = ""; //NOI18N - - public Factory() { - } - public Iterable create(CompilationController controller, TreePath path) throws IOException { - List ret = new ArrayList(); - path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path); - if (path == null) + public List create(Lookup context) { + ArrayList ret = new ArrayList(); + JTextComponent component = context.lookup(JTextComponent.class); + CompilationController controller = context.lookup(CompilationController.class); + TreePath path = context.lookup(TreePath.class); + path = path != null ? Utilities.getPathElementOfKind(Tree.Kind.CLASS, path) : null; + if (component == null || controller == null || path == null) return ret; - controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + try { + controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + } catch (IOException ioe) { + return ret; + } Elements elements = controller.getElements(); TypeElement typeElement = (TypeElement)controller.getTrees().getElement(path); if (typeElement == null || !typeElement.getKind().isClass()) @@ -144,31 +150,33 @@ for (Map.Entry> entry : gDescriptions.entrySet()) descriptions.add(ElementNode.Description.create(entry.getKey(), entry.getValue(), false, false)); Collections.reverse(descriptions); - ret.add(new GetterSetterGenerator(ElementNode.Description.create(typeElement, descriptions, false, false), GeneratorUtils.GETTERS_ONLY)); + ret.add(new GetterSetterGenerator(component, ElementNode.Description.create(typeElement, descriptions, false, false), GeneratorUtils.GETTERS_ONLY)); } if (!sDescriptions.isEmpty()) { List descriptions = new ArrayList(); for (Map.Entry> entry : sDescriptions.entrySet()) descriptions.add(ElementNode.Description.create(entry.getKey(), entry.getValue(), false, false)); Collections.reverse(descriptions); - ret.add(new GetterSetterGenerator(ElementNode.Description.create(typeElement, descriptions, false, false), GeneratorUtils.SETTERS_ONLY)); + ret.add(new GetterSetterGenerator(component, ElementNode.Description.create(typeElement, descriptions, false, false), GeneratorUtils.SETTERS_ONLY)); } if (!gsDescriptions.isEmpty()) { List descriptions = new ArrayList(); for (Map.Entry> entry : gsDescriptions.entrySet()) descriptions.add(ElementNode.Description.create(entry.getKey(), entry.getValue(), false, false)); Collections.reverse(descriptions); - ret.add(new GetterSetterGenerator(ElementNode.Description.create(typeElement, descriptions, false, false), 0)); + ret.add(new GetterSetterGenerator(component, ElementNode.Description.create(typeElement, descriptions, false, false), 0)); } return ret; } } + private JTextComponent component; private ElementNode.Description description; private int type; /** Creates a new instance of GetterSetterGenerator */ - private GetterSetterGenerator(ElementNode.Description description, int type) { + private GetterSetterGenerator(JTextComponent component, ElementNode.Description description, int type) { + this.component = component; this.description = description; this.type = type; } @@ -181,7 +189,7 @@ return org.openide.util.NbBundle.getMessage(GetterSetterGenerator.class, "LBL_getter_and_setter"); //NOI18N } - public void invoke(JTextComponent component) { + public void invoke() { final GetterSetterPanel panel = new GetterSetterPanel(description, type); String title; if (type == GeneratorUtils.GETTERS_ONLY) diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/ImplementOverrideMethodGenerator.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/ImplementOverrideMethodGenerator.java Thu Apr 24 17:30:30 2008 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/codegen/ImplementOverrideMethodGenerator.java Wed Apr 30 11:59:17 2008 +0200 @@ -40,13 +40,13 @@ */ package org.netbeans.modules.java.editor.codegen; +import org.netbeans.spi.editor.codegen.CodeGenerator; import com.sun.source.tree.ClassTree; import com.sun.source.tree.Tree; import com.sun.source.util.TreePath; import java.awt.Dialog; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -66,6 +66,7 @@ import org.openide.DialogDescriptor; import org.openide.DialogDisplayer; import org.openide.util.Exceptions; +import org.openide.util.Lookup; import org.openide.util.NbBundle; /** @@ -75,19 +76,23 @@ public class ImplementOverrideMethodGenerator implements CodeGenerator { public static class Factory implements CodeGenerator.Factory { - - public Factory() { - } - - public Iterable create(CompilationController controller, TreePath path) throws IOException { - List ret = new ArrayList(); - path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path); - if (path == null) + + public List create(Lookup context) { + ArrayList ret = new ArrayList(); + JTextComponent component = context.lookup(JTextComponent.class); + CompilationController controller = context.lookup(CompilationController.class); + TreePath path = context.lookup(TreePath.class); + path = path != null ? Utilities.getPathElementOfKind(Tree.Kind.CLASS, path) : null; + if (component == null || controller == null || path == null) return ret; - controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + try { + controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); + } catch (IOException ioe) { + return ret; + } TypeElement typeElement = (TypeElement)controller.getTrees().getElement(path); if (typeElement == null || !typeElement.getKind().isClass()) - return Collections.emptySet(); + return ret; Map> map = new LinkedHashMap>(); for (ExecutableElement method : GeneratorUtils.findUndefs(controller, typeElement)) { List descriptions = map.get(method.getEnclosingElement()); @@ -101,7 +106,7 @@ for (Map.Entry> entry : map.entrySet()) implementDescriptions.add(ElementNode.Description.create(entry.getKey(), entry.getValue(), false, false)); if (!implementDescriptions.isEmpty()) - ret.add(new ImplementOverrideMethodGenerator(ElementNode.Description.create(implementDescriptions), true)); + ret.add(new ImplementOverrideMethodGenerator(component, ElementNode.Description.create(implementDescriptions), true)); map = new LinkedHashMap>(); ArrayList orderedElements = new ArrayList(); for (ExecutableElement method : GeneratorUtils.findOverridable(controller, typeElement)) { @@ -119,16 +124,18 @@ for (Element e : orderedElements) overrideDescriptions.add(ElementNode.Description.create(e, map.get( e ), false, false)); if (!overrideDescriptions.isEmpty()) - ret.add(new ImplementOverrideMethodGenerator(ElementNode.Description.create(overrideDescriptions), false)); + ret.add(new ImplementOverrideMethodGenerator(component, ElementNode.Description.create(overrideDescriptions), false)); return ret; } } - + + private JTextComponent component; private ElementNode.Description description; private boolean isImplement; /** Creates a new instance of OverrideMethodGenerator */ - private ImplementOverrideMethodGenerator(ElementNode.Description description, boolean isImplement) { + private ImplementOverrideMethodGenerator(JTextComponent component, ElementNode.Description description, boolean isImplement) { + this.component = component; this.description = description; this.isImplement = isImplement; } @@ -137,7 +144,7 @@ return org.openide.util.NbBundle.getMessage(ImplementOverrideMethodGenerator.class, isImplement ? "LBL_implement_method" : "LBL_override_method"); //NOI18N } - public void invoke(JTextComponent component) { + public void invoke() { final ImplementOverridePanel panel = new ImplementOverridePanel(description, isImplement); DialogDescriptor dialogDescriptor = GeneratorUtils.createDialogDescriptor(panel, NbBundle.getMessage(ConstructorGenerator.class, isImplement ? "LBL_generate_implement" : "LBL_generate_override")); //NOI18N //NOI18N diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/ui/GenerateCodePanel.form --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/ui/GenerateCodePanel.form Thu Apr 24 17:30:30 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,71 +0,0 @@ - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/codegen/ui/GenerateCodePanel.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/ui/GenerateCodePanel.java Thu Apr 24 17:30:30 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,187 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. - * - * 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. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun 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-2006 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.netbeans.modules.java.editor.codegen.ui; - -import java.awt.Color; -import java.awt.Component; -import java.awt.event.KeyEvent; -import java.util.List; -import javax.swing.DefaultListCellRenderer; -import javax.swing.DefaultListModel; -import javax.swing.JList; -import javax.swing.KeyStroke; -import javax.swing.text.JTextComponent; -import org.netbeans.modules.java.editor.codegen.CodeGenerator; -import org.netbeans.modules.java.editor.overridden.PopupUtil; -import org.openide.util.Utilities; - -/** - * - * @author Dusan Balek, Jan Lahoda - */ -public class GenerateCodePanel extends javax.swing.JPanel { - - private JTextComponent component; - - /** Creates new form GenerateCodePanel */ - public GenerateCodePanel(JTextComponent component, List generators) { - this.component = component; - initComponents(); - setFocusable(false); - setNextFocusableComponent(jList1); - setBackground(jList1.getBackground()); - jScrollPane1.setBackground(jList1.getBackground()); - jList1.setModel(createModel(generators)); - jList1.setSelectedIndex(0); - jList1.setVisibleRowCount(generators.size() > 8 ? 8 : generators.size()); - jList1.setCellRenderer(new Renderer(jList1)); - jList1.grabFocus(); - } - - /** This method is called from within the constructor to - * initialize the form. - * WARNING: Do NOT modify this code. The content of this method is - * always regenerated by the Form Editor. - */ - // //GEN-BEGIN:initComponents - private void initComponents() { - - jScrollPane1 = new javax.swing.JScrollPane(); - jList1 = new javax.swing.JList(); - jLabel1 = new javax.swing.JLabel(); - - setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(64, 64, 64))); - setLayout(new java.awt.BorderLayout()); - - jScrollPane1.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 4, 4)); - - jList1.addKeyListener(new java.awt.event.KeyAdapter() { - public void keyReleased(java.awt.event.KeyEvent evt) { - listKeyReleased(evt); - } - }); - jList1.addMouseListener(new java.awt.event.MouseAdapter() { - public void mouseReleased(java.awt.event.MouseEvent evt) { - listMouseReleased(evt); - } - }); - jScrollPane1.setViewportView(jList1); - - add(jScrollPane1, java.awt.BorderLayout.CENTER); - - jLabel1.setText(org.openide.util.NbBundle.getMessage(CodeGenerator.class, "LBL_generate_code")); // NOI18N - jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4)); - jLabel1.setOpaque(true); - add(jLabel1, java.awt.BorderLayout.PAGE_START); - }// //GEN-END:initComponents - - private void listMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listMouseReleased - invokeSelected(); - }//GEN-LAST:event_listMouseReleased - - private void listKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_listKeyReleased - KeyStroke ks = KeyStroke.getKeyStrokeForEvent(evt); - if (ks.getKeyCode() == KeyEvent.VK_ENTER || ks.getKeyCode() == KeyEvent.VK_SPACE) - invokeSelected(); - }//GEN-LAST:event_listKeyReleased - - - // Variables declaration - do not modify//GEN-BEGIN:variables - public javax.swing.JLabel jLabel1; - public javax.swing.JList jList1; - public javax.swing.JScrollPane jScrollPane1; - // End of variables declaration//GEN-END:variables - - private DefaultListModel createModel(List generators) { - DefaultListModel model = new DefaultListModel(); - for (CodeGenerator generator : generators) - model.addElement(generator); - return model; - } - - private void invokeSelected() { - PopupUtil.hidePopup(); - if (Utilities.isMac()) { - // see issue #115106 - component.requestFocus(); - } - Object value = jList1.getSelectedValue(); - if (value instanceof CodeGenerator) - ((CodeGenerator)value).invoke(component); - } - - private static class Renderer extends DefaultListCellRenderer { - - private static int DARKER_COLOR_COMPONENT = 5; - - private Color fgColor; - private Color bgColor; - private Color bgColorDarker; - private Color bgSelectionColor; - private Color fgSelectionColor; - - public Renderer(JList list) { - setFont(list.getFont()); - fgColor = list.getForeground(); - bgColor = list.getBackground(); - bgColorDarker = new Color(Math.abs(bgColor.getRed() - DARKER_COLOR_COMPONENT), - Math.abs(bgColor.getGreen() - DARKER_COLOR_COMPONENT), - Math.abs(bgColor.getBlue() - DARKER_COLOR_COMPONENT)); - bgSelectionColor = list.getSelectionBackground(); - fgSelectionColor = list.getSelectionForeground(); - } - - @Override - public Component getListCellRendererComponent(JList list, Object value, int index, - boolean isSelected, boolean hasFocus) { - if (isSelected) { - setForeground(fgSelectionColor); - setBackground(bgSelectionColor); - } else { - setForeground(fgColor); - setBackground(index % 2 == 0 ? bgColor : bgColorDarker); - } - setText(value instanceof CodeGenerator ? ((CodeGenerator)value).getDisplayName() : value.toString()); - return this; - } - } -} diff -r 9387523afe84 java.editor/src/org/netbeans/modules/java/editor/resources/layer.xml --- a/java.editor/src/org/netbeans/modules/java/editor/resources/layer.xml Thu Apr 24 17:30:30 2008 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/resources/layer.xml Wed Apr 30 11:59:17 2008 +0200 @@ -51,7 +51,6 @@ - @@ -285,7 +284,11 @@ - + + + + + diff -r 9387523afe84 java.editor/test/qa-functional/src/org/netbeans/test/java/editor/jelly/GenerateCodeOperator.java --- a/java.editor/test/qa-functional/src/org/netbeans/test/java/editor/jelly/GenerateCodeOperator.java Thu Apr 24 17:30:30 2008 +0200 +++ b/java.editor/test/qa-functional/src/org/netbeans/test/java/editor/jelly/GenerateCodeOperator.java Wed Apr 30 11:59:17 2008 +0200 @@ -37,7 +37,7 @@ import org.netbeans.jellytools.EditorOperator; import org.netbeans.jemmy.operators.JDialogOperator; import org.netbeans.jemmy.operators.JListOperator; -import org.netbeans.modules.java.editor.codegen.CodeGenerator; +import org.netbeans.spi.editor.codegen.CodeGenerator; import org.netbeans.modules.java.editor.codegen.ConstructorGenerator; import org.netbeans.modules.java.editor.codegen.DelegateMethodGenerator; import org.netbeans.modules.java.editor.codegen.EqualsHashCodeGenerator;