1 19 package org.netbeans.modules.xml.multiview.test.util; 20 21 import java.io.*; 22 import java.awt.*; 23 24 import org.openide.cookies.SaveCookie; 25 import org.openide.loaders.DataObject; 26 27 import org.netbeans.modules.xml.multiview.ToolBarMultiViewElement; 28 import org.netbeans.modules.xml.multiview.XmlMultiViewEditorSupport; 29 import org.netbeans.modules.xml.multiview.ui.SectionView; 30 import org.netbeans.modules.xml.multiview.test.BookDataObject; 31 import org.netbeans.modules.xml.multiview.test.bookmodel.Chapter; 32 33 import javax.swing.*; 34 import javax.swing.text.Document ; 35 36 public class Helper { 37 38 public static File getBookFile(File dataDir) { 39 return new File(dataDir, "sample.book"); 40 } 41 42 public static JTextField getChapterTitleTF(final BookDataObject dObj, Chapter chapter) { 43 final ToolBarMultiViewElement multiViewElement = new StepIterator() { 44 ToolBarMultiViewElement multiViewElement; 45 46 public boolean step() throws Exception { 47 return (multiViewElement = dObj.getActiveMultiViewElement0()) != null; 48 } 49 }.multiViewElement; 50 SectionView sectionView = new StepIterator() { 51 SectionView sectionView; 52 public boolean step() throws Exception { 53 return (sectionView = multiViewElement.getSectionView()) != null; 54 55 } 56 }.sectionView; 57 JPanel sectionPanel = sectionView.findSectionPanel(chapter).getInnerPanel(); 58 Component[] children = sectionPanel.getComponents(); 59 for (int i = 0; i < children.length; i++) { 60 if (children[i] instanceof JTextField) { 61 return (JTextField) children[i]; 62 } 63 } 64 return null; 65 } 66 67 public static boolean isTextInFile(String text, File file) throws IOException { 68 BufferedReader reader = new BufferedReader(new FileReader(file)); 69 String line; 70 while ((line=reader.readLine())!=null) { 71 if (line.indexOf(text) >= 0) { 72 return true; 73 } 74 } 75 return false; 76 } 77 78 public static void sleep(int ms) { 79 try { 80 Thread.sleep(ms); 81 } catch (InterruptedException ex){ 82 } 84 } 85 86 public static void waitForDispatchThread() { 87 if (SwingUtilities.isEventDispatchThread()) { 88 return; 89 } 90 final boolean[] finished = new boolean[]{false}; 91 SwingUtilities.invokeLater(new Runnable () { 92 public void run() { 93 finished[0] = true; 94 } 95 }); 96 new StepIterator() { 97 public boolean step() throws Exception { 98 return finished[0]; 99 } 100 }; 101 } 102 103 public static SaveCookie getSaveCookie(final DataObject dataObject) { 104 return new StepIterator() { 105 SaveCookie cookie; 106 107 public boolean step() throws Exception { 108 return ((cookie = (SaveCookie) dataObject.getCookie(SaveCookie.class)) != null); 109 } 110 }.cookie; 111 } 112 113 public static Document getDocument(final XmlMultiViewEditorSupport editor) { 114 return new StepIterator() { 115 Document document; 116 117 public boolean step() throws Exception { 118 document = editor.getDocument(); 119 return (document.getLength() > 0); 120 } 121 }.document; 122 } 123 } | Popular Tags |