KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > test > util > Helper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
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 JavaDoc {
47                 return (multiViewElement = dObj.getActiveMultiViewElement0()) != null;
48             }
49         }.multiViewElement;
50         SectionView sectionView = new StepIterator() {
51             SectionView sectionView;
52             public boolean step() throws Exception JavaDoc {
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 JavaDoc text, File file) throws IOException {
68         BufferedReader reader = new BufferedReader(new FileReader(file));
69         String JavaDoc 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 JavaDoc ex){
82             // ignore
83
}
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 JavaDoc() {
92             public void run() {
93                 finished[0] = true;
94             }
95         });
96         new StepIterator() {
97             public boolean step() throws Exception JavaDoc {
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 JavaDoc {
108                 return ((cookie = (SaveCookie) dataObject.getCookie(SaveCookie.class)) != null);
109             }
110         }.cookie;
111     }
112
113     public static Document JavaDoc getDocument(final XmlMultiViewEditorSupport editor) {
114         return new StepIterator() {
115             Document JavaDoc document;
116
117             public boolean step() throws Exception JavaDoc {
118                 document = editor.getDocument();
119                 return (document.getLength() > 0);
120             }
121         }.document;
122     }
123 }
Popular Tags