diff -r 9079db2108da editor.lib2/apichanges.xml --- a/editor.lib2/apichanges.xml Fri May 15 13:15:50 2009 +0400 +++ b/editor.lib2/apichanges.xml Fri May 15 18:36:28 2009 +0200 @@ -105,6 +105,20 @@ + + DialogBinding added + + + + + DialogBinding allows for content of some text component to be virtually placed + to the given offset inside of a file or document. This way, the necessary context is + provided for code completion and similar features to work in any text component. + + + + + EditorActionRegistration annotation added diff -r 9079db2108da editor.lib2/nbproject/project.properties --- a/editor.lib2/nbproject/project.properties Fri May 15 13:15:50 2009 +0400 +++ b/editor.lib2/nbproject/project.properties Fri May 15 18:36:28 2009 +0200 @@ -40,7 +40,7 @@ is.autoload=true javac.source=1.5 javac.compilerargs=-Xlint:unchecked -spec.version.base=1.10.0 +spec.version.base=1.11.0 cp.extra=${nb_all}/apisupport.harness/external/openjdk-javac-6-b12.jar javadoc.arch=${basedir}/arch.xml diff -r 9079db2108da editor.lib2/src/org/netbeans/api/editor/DialogBinding.java --- a/editor.lib2/src/org/netbeans/api/editor/DialogBinding.java Fri May 15 13:15:50 2009 +0400 +++ b/editor.lib2/src/org/netbeans/api/editor/DialogBinding.java Fri May 15 18:36:28 2009 +0200 @@ -55,12 +55,30 @@ Parameters.notNull("component", component); //NOI18N if (!fileObject.isValid() || !fileObject.isData()) return; + bind(component, null, fileObject, offset, length, fileObject.getMIMEType()); + } + + /** + * Bind given component and given document together. + * @param document to bind + * @param offset position at which content of the component will be virtually placed + * @param length how many characters replace from the original document + * @param component component to bind + */ + public static void bindComponentToDocument(final Document document, int offset, int length, final JTextComponent component) { + Parameters.notNull("document", document); //NOI18N + Parameters.notNull("component", component); //NOI18N + bind(component, document, null, offset, length, (String)document.getProperty("mimeType")); //NOI18N + } + + private static void bind(final JTextComponent component, final Document document, final FileObject fileObject, int offset, int length, final String mimeType) { if (component instanceof JEditorPane) - ((JEditorPane)component).setEditorKit(MimeLookup.getLookup(fileObject.getMIMEType()).lookup(EditorKit.class)); + ((JEditorPane) component).setEditorKit(MimeLookup.getLookup(mimeType).lookup(EditorKit.class)); Document doc = component.getDocument(); doc.putProperty("mimeType", "text/x-dialog-binding"); //NOI18N InputAttributes inputAttributes = new InputAttributes(); Language language = MimeLookup.getLookup("text/x-dialog-binding").lookup(Language.class); //NOI18N + inputAttributes.setValue(language, "dialogBinding.document", document, true); //NOI18N inputAttributes.setValue(language, "dialogBinding.fileObject", fileObject, true); //NOI18N inputAttributes.setValue(language, "dialogBinding.offset", offset, true); //NOI18N inputAttributes.setValue(language, "dialogBinding.length", length, true); //NOI18N diff -r 9079db2108da editor.lib2/src/org/netbeans/modules/editor/lib2/DialogBindingTokenId.java --- a/editor.lib2/src/org/netbeans/modules/editor/lib2/DialogBindingTokenId.java Fri May 15 13:15:50 2009 +0400 +++ b/editor.lib2/src/org/netbeans/modules/editor/lib2/DialogBindingTokenId.java Fri May 15 18:36:28 2009 +0200 @@ -30,6 +30,7 @@ import java.util.Collection; import java.util.EnumSet; import java.util.Map; +import javax.swing.text.Document; import org.netbeans.api.editor.mimelookup.MimeLookup; import org.netbeans.api.lexer.InputAttributes; import org.netbeans.api.lexer.Language; @@ -77,10 +78,18 @@ protected LanguageEmbedding embedding(Token token, LanguagePath languagePath, InputAttributes inputAttributes) { if (inputAttributes == null) return null; - FileObject fo = (FileObject)inputAttributes.getValue(languagePath, "dialogBinding.fileObject"); //NOI18N - if (fo == null) + String mimeType = null; + Document doc = (Document) inputAttributes.getValue(languagePath, "dialogBinding.document"); //NOI18N + if (doc != null) { + mimeType = (String)doc.getProperty("mimeType"); //NOI18N + } else { + FileObject fo = (FileObject)inputAttributes.getValue(languagePath, "dialogBinding.fileObject"); //NOI18N + if (fo != null) + mimeType = fo.getMIMEType(); + } + if (mimeType == null) return null; - Language l = MimeLookup.getLookup(fo.getMIMEType()).lookup(Language.class); + Language l = MimeLookup.getLookup(mimeType).lookup(Language.class); return l != null ? LanguageEmbedding.create(l, 0, 0) : null; } diff -r 9079db2108da java.editor/src/org/netbeans/modules/editor/java/Utilities.java --- a/java.editor/src/org/netbeans/modules/editor/java/Utilities.java Fri May 15 13:15:50 2009 +0400 +++ b/java.editor/src/org/netbeans/modules/editor/java/Utilities.java Fri May 15 18:36:28 2009 +0200 @@ -81,6 +81,7 @@ import org.netbeans.api.lexer.TokenHierarchy; import org.netbeans.api.lexer.TokenSequence; import org.netbeans.editor.ext.java.JavaTokenContext; +import org.netbeans.modules.editor.NbEditorUtilities; import org.netbeans.modules.java.editor.options.CodeCompletionPanel; import org.openide.filesystems.FileObject; import org.openide.util.WeakListeners; @@ -350,6 +351,9 @@ if (doc.getLength() == 0 && "text/x-dialog-binding".equals(doc.getProperty("mimeType"))) { //NOI18N InputAttributes attributes = (InputAttributes) doc.getProperty(InputAttributes.class); LanguagePath path = LanguagePath.get(MimeLookup.getLookup("text/x-dialog-binding").lookup(Language.class)); //NOI18N + Document d = (Document) attributes.getValue(path, "dialogBinding.document"); //NOI18N + if (d != null) + return "text/x-java".equals(NbEditorUtilities.getMimeType(d)); //NOI18N FileObject fo = (FileObject)attributes.getValue(path, "dialogBinding.fileObject"); //NOI18N return "text/x-java".equals(fo.getMIMEType()); //NOI18N } diff -r 9079db2108da java.source/src/org/netbeans/api/java/source/ClasspathInfo.java --- a/java.source/src/org/netbeans/api/java/source/ClasspathInfo.java Fri May 15 13:15:50 2009 +0400 +++ b/java.source/src/org/netbeans/api/java/source/ClasspathInfo.java Fri May 15 18:36:28 2009 +0200 @@ -227,6 +227,14 @@ if ("text/x-dialog-binding".equals(mimeType)) { //NOI18N InputAttributes attributes = (InputAttributes) doc.getProperty(InputAttributes.class); LanguagePath path = LanguagePath.get(MimeLookup.getLookup(mimeType).lookup(Language.class)); + Document d = (Document) attributes.getValue(path, "dialogBinding.document"); //NOI18N + if (d != null) { + Object obj = d.getProperty(Document.StreamDescriptionProperty); + if (obj instanceof DataObject) { + DataObject dObj = (DataObject) obj; + return create(dObj.getPrimaryFile()); + } + } FileObject fileObject = (FileObject) attributes.getValue(path, "dialogBinding.fileObject"); //NOI18N if (fileObject != null) return create(fileObject); diff -r 9079db2108da parsing.api/src/org/netbeans/modules/parsing/api/Source.java --- a/parsing.api/src/org/netbeans/modules/parsing/api/Source.java Fri May 15 13:15:50 2009 +0400 +++ b/parsing.api/src/org/netbeans/modules/parsing/api/Source.java Fri May 15 18:36:28 2009 +0200 @@ -182,7 +182,12 @@ if ("text/x-dialog-binding".equals(mimeType)) { //NOI18N InputAttributes attributes = (InputAttributes) document.getProperty(InputAttributes.class); LanguagePath path = LanguagePath.get(MimeLookup.getLookup(mimeType).lookup(Language.class)); - fileObject = (FileObject) attributes.getValue(path, "dialogBinding.fileObject"); //NOI18N + Document doc = (Document) attributes.getValue(path, "dialogBinding.document"); //NOI18N + if (doc != null) { + fileObject = NbEditorUtilities.getFileObject(doc); + } else { + fileObject = (FileObject) attributes.getValue(path, "dialogBinding.fileObject"); //NOI18N + } } source = new Source(mimeType, document, fileObject); } diff -r 9079db2108da parsing.api/src/org/netbeans/modules/parsing/impl/DialogBindingEmbeddingProvider.java --- a/parsing.api/src/org/netbeans/modules/parsing/impl/DialogBindingEmbeddingProvider.java Fri May 15 13:15:50 2009 +0400 +++ b/parsing.api/src/org/netbeans/modules/parsing/impl/DialogBindingEmbeddingProvider.java Fri May 15 18:36:28 2009 +0200 @@ -41,10 +41,10 @@ import org.netbeans.api.lexer.Language; import org.netbeans.api.lexer.LanguagePath; import org.netbeans.api.queries.FileEncodingQuery; +import org.netbeans.modules.editor.NbEditorUtilities; import org.netbeans.modules.parsing.api.Embedding; import org.netbeans.modules.parsing.api.Snapshot; import org.netbeans.modules.parsing.spi.EmbeddingProvider; -import org.netbeans.modules.parsing.spi.Scheduler; import org.netbeans.modules.parsing.spi.SchedulerTask; import org.netbeans.modules.parsing.spi.TaskFactory; import org.openide.filesystems.FileObject; @@ -61,29 +61,39 @@ try { LanguagePath path = LanguagePath.get(MimeLookup.getLookup(snapshot.getMimeType()).lookup(Language.class)); InputAttributes attributes = (InputAttributes) doc.getProperty(InputAttributes.class); - FileObject fileObject = (FileObject) attributes.getValue(path, "dialogBinding.fileObject"); //NOI18N int offset = (Integer)attributes.getValue(path, "dialogBinding.offset"); //NOI18N int length = (Integer)attributes.getValue(path, "dialogBinding.length"); //NOI18N - String mimeType = fileObject.getMIMEType(); - InputStream inputStream = fileObject.getInputStream(); - try { - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, FileEncodingQuery.getEncoding(fileObject))); - CharBuffer charBuffer = CharBuffer.allocate(4096); - StringBuilder sb = new StringBuilder(); - int i = bufferedReader.read(charBuffer); - while (i > 0) { - charBuffer.flip(); - sb.append(charBuffer); - charBuffer.clear(); - i = bufferedReader.read(charBuffer); + Document d = (Document) attributes.getValue(path, "dialogBinding.document"); //NOI18N + if (d != null) { + String mimeType = NbEditorUtilities.getMimeType(d); + ArrayList ret = new ArrayList(3); + ret.add(snapshot.create(d.getText(0, offset), mimeType)); + ret.add(snapshot.create(0, snapshot.getText().length(), mimeType)); + ret.add(snapshot.create(d.getText(offset + length, d.getLength() - offset - length), mimeType)); + return Collections.singletonList(Embedding.create(ret)); + } else { + FileObject fileObject = (FileObject) attributes.getValue(path, "dialogBinding.fileObject"); //NOI18N + String mimeType = fileObject.getMIMEType(); + InputStream inputStream = fileObject.getInputStream(); + try { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, FileEncodingQuery.getEncoding(fileObject))); + CharBuffer charBuffer = CharBuffer.allocate(4096); + StringBuilder sb = new StringBuilder(); + int i = bufferedReader.read(charBuffer); + while (i > 0) { + charBuffer.flip(); + sb.append(charBuffer); + charBuffer.clear(); + i = bufferedReader.read(charBuffer); + } + ArrayList ret = new ArrayList(3); + ret.add(snapshot.create(sb.subSequence(0, offset), mimeType)); + ret.add(snapshot.create(0, snapshot.getText().length(), mimeType)); + ret.add(snapshot.create(sb.subSequence(offset + length, sb.length()), mimeType)); + return Collections.singletonList(Embedding.create(ret)); + } finally { + inputStream.close(); } - ArrayList ret = new ArrayList(3); - ret.add(snapshot.create(sb.subSequence(0, offset), mimeType)); - ret.add(snapshot.create(0, snapshot.getText().length(), mimeType)); - ret.add(snapshot.create(sb.subSequence(offset + length, sb.length()), mimeType)); - return Collections.singletonList(Embedding.create(ret)); - } finally { - inputStream.close(); } } catch (Exception e) { e.printStackTrace();