|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.test.web.editor.completion; import java.io.*; import java.lang.reflect.InvocationTargetException; import javax.swing.text.*; import javax.swing.*; import java.util.List; import java.util.Arrays; import java.lang.reflect.UndeclaredThrowableException; import java.util.Vector; import org.netbeans.api.project.Project; import org.netbeans.api.project.ProjectManager; import org.netbeans.editor.*; import org.netbeans.editor.ext.*; import org.openide.nodes.*; import org.openide.*; import org.openide.cookies.*; import org.openide.filesystems.*; import org.openide.loaders.*; import org.openide.util.*; import org.openide.cookies.EditorCookie; import org.netbeans.editor.Utilities; import org.netbeans.modules.project.ui.OpenProjectList; import org.openide.cookies.SourceCookie; import org.openide.src.SourceElement; import org.openide.text.CloneableEditorSupport; /** * * @author Marek Fukala */ public class CompletionTestSupport extends java.lang.Object { private File projectDir; private Project project; public CompletionTestSupport(File projectDir) throws IOException { this.projectDir = projectDir; //find project FileObject fo = FileUtil.toFileObject(projectDir); project = ProjectManager.getDefault().findProject(fo); if (project == null) throw new IllegalStateException("Given directory does not contain a project."); //open the project if necessary if (!OpenProjectList.getDefault().isOpen(project)) { OpenProjectList.getDefault().open(project); } } public FileCompletionRequester getFileCompletionRequester(String fileName) throws DataObjectNotFoundException, IOException { return new FileCompletionRequester(project, fileName); } public static class FileCompletionRequester { private JEditorPane jep; private String[] completionResult = null; private Exception exceptionFromAnonymoutClass = null; public FileCompletionRequester(Project project, String fileName) throws DataObjectNotFoundException, IOException { FileObject fileObj = project.getProjectDirectory().getFileObject(fileName); //open editor jep = getAnEditorPane(DataObject.find(fileObj)); } public String[] getCompletion(final int row, final int lineoffset) throws IOException, BadLocationException { try { //the test has to run in AWT Event-Queue SwingUtilities.invokeAndWait(new Runnable() { public void run() { try { String[] a = getCompletionContent(jep, row, lineoffset); FileCompletionRequester.this.completionResult = a; }catch(Exception e ) { e.printStackTrace(); //remember the exc. in the outer class FileCompletionRequester.this.exceptionFromAnonymoutClass = e; } } }); }catch(InterruptedException e) { ; }catch(InvocationTargetException e) { ; } //test whether there has been an exception in the AWT thread & rethrown it if necessary if(exceptionFromAnonymoutClass != null) if(exceptionFromAnonymoutClass instanceof IOException) throw (IOException)exceptionFromAnonymoutClass; else if(exceptionFromAnonymoutClass instanceof BadLocationException) throw (BadLocationException)exceptionFromAnonymoutClass; else throw new IllegalStateException("Error in the code completion thread: " + exceptionFromAnonymoutClass.getMessage()); //return the completion content return completionResult; } private String[] completionQuery(JEditorPane editor, boolean sort) { BaseDocument doc = Utilities.getDocument(editor); SyntaxSupport support = doc.getSyntaxSupport(); Completion completion = ExtUtilities.getCompletion(editor); if (completion != null) { CompletionQuery completionQuery = completion.getQuery(); if (completionQuery != null) { CompletionQuery.Result query = completionQuery.query(editor, editor.getCaret().getDot(), support); if (query != null) { List list = query.getData(); if (list != null) { String[] texts = new String[list.size()]; for (int cntr = 0; cntr < list.size(); cntr++) { Object completionItem = list.get(cntr); if(completionItem instanceof org.netbeans.editor.ext.CompletionQuery.AbstractResultItem) { texts[cntr] = ((org.netbeans.editor.ext.CompletionQuery.AbstractResultItem)completionItem).getItemText(); } else { texts[cntr] = completionItem.toString(); } }; if (sort) Arrays.sort(texts); //return the completion content return texts; } else { throw new IllegalStateException("CompletionTest: query.getData() == null"); } } else { throw new IllegalStateException("CompletionTest: completionQuery.query(pane, end, support) == null"); } } else { throw new IllegalStateException("CompletionTest: completion.getQuery() == null"); } } else { throw new IllegalStateException("CompletionTest: ExtUtilities.getCompletion(pane) == null"); } } private String[] getCompletionContent(JEditorPane editor, int line, int lineoffset) throws BadLocationException, IOException { if (!SwingUtilities.isEventDispatchThread()) throw new IllegalStateException("The testPerform method may be called only inside AWT event dispatch thread."); BaseDocument doc = Utilities.getDocument(editor); int offset = Utilities.getRowStartFromLineOffset(doc, line -1) + lineoffset -1; editor.grabFocus(); editor.getCaret().setDot(offset); //doc.insertString(lineOffset, assign, null); reparseDocument((DataObject) doc.getProperty(doc.StreamDescriptionProperty)); return completionQuery(editor, false); } private static JEditorPane getAnEditorPane(DataObject file) throws IOException { EditorCookie cookie = (EditorCookie)file.getCookie(EditorCookie.class); if (cookie == null) throw new IllegalStateException("Given file (\"" + file.getName() + "\") does not have EditorCookie."); JEditorPane[] panes = cookie.getOpenedPanes(); long start = System.currentTimeMillis(); if (panes == null) { //Prepare by opening a document. The actual opening into the editor //should be faster (hopefully...). cookie.openDocument(); cookie.open(); while ((panes = cookie.getOpenedPanes()) == null && (System.currentTimeMillis() - start) < OPENING_TIMEOUT) { try { Thread.sleep(SLEEP_TIME); } catch (InterruptedException e) { ; } }; }; if (panes == null) throw new IllegalStateException("The editor was not opened. The timeout was: " + OPENING_TIMEOUT + "ms."); return panes[0]; } private static void reparseDocument(DataObject file) throws IOException { // saveDocument(file); SourceCookie sc = (SourceCookie) file.getCookie(SourceCookie.class); if (sc != null) { SourceElement se = sc.getSource(); se.prepare().waitFinished(); } else { //System.err.println("reparseDocument: SourceCookie cookie not found in testFile!"); } } private static final long OPENING_TIMEOUT = 60 * 1000; private static final long SLEEP_TIME = 1000; } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.