blob: d4504c0710a3ea59675428d421933bd14ae9b322 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqEditorCodeCompletionAnyJEditorPane
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqEditorCodeCompletionAnyJEditorPane
:description: Apache NetBeans wiki DevFaqEditorCodeCompletionAnyJEditorPane
:toc: left
:toc-title:
:syntax: true
==== How to add code completion to any JEditorPane
You can add the built-in Java code completion from the Netbeans 6 Java editor to any arbitrary JEditorPane. See the code below for how this can be achieved. Note that Netbeans Java editor functionality is derived from an underlying FileObject, so we are essentially creating a dummy Java FileObject and tying our JEditorPane document in with the appropriate hooks.
The end result of this code is that we create a Java context for our JEditorPane. This context initializes code completion with a default class path, and that grants us access to the standard Java APIs (i.e. the code completion box can include classes such as java.lang.String, java.util.List, etc.). However, this context has no visibility into any additional jars nor Java projects. In order to expand this default Java context, you will need to create your own class path provider (see the link:http://www.netbeans.org/download/dev/javadoc/org-netbeans-api-java/org/netbeans/spi/java/classpath/package-summary.html["Java Support APIs"] module).
First, let's take a look at some of the classes we'll be using:
* import java.io.File;
* import javax.swing.JEditorPane;
* import javax.swing.text.Document;
* import javax.swing.text.EditorKit;
* import org.netbeans.spi.java.classpath.ClassPathProvider; <span style='color:orange;'> from the "Java Support APIs" module</span>
* import org.openide.filesystems.FileObject; <span style='color:orange;'> from the "File System API" module</span>
* import org.openide.filesystems.FileUtil; <span style='color:orange;'> from the "File System API" module</span>
* import org.openide.loaders.DataObject; <span style='color:orange;'> from the "Datasystems API" module</span>
* import org.openide.text.CloneableEditorSupport; <span style='color:orange;'> from the "Text API" module</span>
* import org.openide.util.Lookup; <span style='color:orange;'> from the "Utilities API" module</span>
* import org.netbeans.api.java.source.ui.DialogBinding; <span style='color:orange;'> from the "Java Source UI" module</span>
Now we're ready to take a look at the actual code:
[source,java]
----
JEditorPane editorPane = new JEditorPane();
// This will find the Java editor kit and associate it with
// our editor pane. But that does not give us code completion
// just yet because we have no Java context (i.e. no class path, etc.).
// However, this does give us syntax coloring.
EditorKit kit = CloneableEditorSupport.getEditorKit("text/x-java");
editorPane.setEditorKit(kit);
// You can specify any ".java" file.
// If the file does not exist, it will be created.
// The contents of the file does not matter.
// The extension must be ".java", however.
String newSourcePath = "tmp.java";
File tmpFile = new File(newSourcePath);
FileObject fob = FileUtil.createData(tmpFile);
DataObject dob = DataObject.find(fob);
editorPane.getDocument().putProperty(
Document.StreamDescriptionProperty,
dob);
// This sets up a default class path for us so that
// we can find all the JDK classes via code completion.
DialogBinding.bindComponentToFile(fob, 0, 0, editorPane);
// Last but not least, we need to fill the editor pane with
// some initial dummy code - as it seems somehow required to
// kick-start code completion.
// A simple dummy package declaration will do.
editorPane.setText("package dummy;");
----
Applies to: Netbeans 6.0, 6.1 and 6.5. Since 6.7 `DialogBinding` class was moved to `org.netbeans.api.editor` package in `Editor Library 2` module.
Platforms: All
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqEditorCodeCompletionAnyJEditorPane[http://wiki.netbeans.org/DevFaqEditorCodeCompletionAnyJEditorPane] ,
that was last modified by NetBeans user Vstejskal
on 2010-06-16T14:25:10Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.