KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > ComponentUtils


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
20 package org.netbeans.modules.editor.lib2;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Frame JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import javax.swing.plaf.TextUI JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import javax.swing.text.JTextComponent JavaDoc;
30
31 /**
32  *
33  * @author Vita Stejskal
34  */

35 public final class ComponentUtils {
36
37     private static final Logger JavaDoc LOG = Logger.getLogger(Logger JavaDoc.class.getName());
38     
39     public static boolean isGuardedException(BadLocationException JavaDoc exc) {
40         return exc.getClass().getName().equals("GuardedException");
41     }
42
43     public static void returnFocus() {
44          JTextComponent JavaDoc c = DocumentsRegistry.getMostActiveComponent();
45          if (c != null) {
46              requestFocus(c);
47          }
48     }
49
50     public static void requestFocus(JTextComponent JavaDoc c) {
51         if (c != null) {
52             if (!EditorImplementation.getDefault().activateComponent(c)) {
53                 Frame JavaDoc f = getParentFrame(c);
54                 if (f != null) {
55                     f.requestFocus();
56                 }
57                 c.requestFocus();
58             }
59         }
60     }
61
62     public static void setStatusText(JTextComponent JavaDoc c, String JavaDoc text) {
63         // TODO: fix this, do not use reflection
64
try {
65             Object JavaDoc editorUI = getEditorUI(c);
66             Method JavaDoc getSbMethod = editorUI.getClass().getMethod("getStatusBar");
67             Object JavaDoc statusBar = getSbMethod.invoke(editorUI);
68             Method JavaDoc setTextMethod = statusBar.getClass().getMethod("setText", String JavaDoc.class, String JavaDoc.class);
69             setTextMethod.invoke(statusBar, "main", text);
70         } catch (Exception JavaDoc e) {
71             LOG.log(Level.WARNING, e.getMessage(), e);
72         }
73 // StatusBar sb = getEditorUI(c).getStatusBar();
74
// if (sb != null) {
75
// sb.setText(StatusBar.CELL_MAIN, text);
76
// }
77
}
78
79 // public static void setStatusText(JTextComponent c, String text, Coloring extraColoring) {
80
// TextUI textUI = c.getUI();
81
// try {
82
// Method getSbMethod = textUI.getClass().getMethod("getStatusBar");
83
// Object statusBar = getSbMethod.invoke(textUI);
84
// Method setTextMethod = statusBar.getClass().getMethod("setText", String.class, String.class);
85
// setTextMethod.invoke(statusBar, "main", text);
86
// } catch (Exception e) {
87
// LOG.log(Level.WARNING, e.getMessage(), e);
88
// }
89
//// StatusBar sb = getEditorUI(c).getStatusBar();
90
//// if (sb != null) {
91
//// sb.setText(StatusBar.CELL_MAIN, text, extraColoring);
92
//// }
93
// }
94

95     public static void setStatusBoldText(JTextComponent JavaDoc c, String JavaDoc text) {
96         // TODO: fix this, do not use reflection
97
try {
98             Object JavaDoc editorUI = getEditorUI(c);
99             Method JavaDoc getSbMethod = editorUI.getClass().getMethod("getStatusBar"); //NOI18N
100
Object JavaDoc statusBar = getSbMethod.invoke(editorUI);
101             Method JavaDoc setTextMethod = statusBar.getClass().getMethod("setBoldText", String JavaDoc.class, String JavaDoc.class); //NOI18N
102
setTextMethod.invoke(statusBar, "main", text); //NOI18N
103
} catch (Exception JavaDoc e) {
104             LOG.log(Level.WARNING, e.getMessage(), e);
105         }
106 // StatusBar sb = getEditorUI(c).getStatusBar();
107
// if (sb != null) {
108
// sb.setBoldText(StatusBar.CELL_MAIN, text);
109
// }
110
}
111
112     public static String JavaDoc getStatusText(JTextComponent JavaDoc c) {
113         // TODO: fix this, do not use reflection
114
try {
115             Object JavaDoc editorUI = getEditorUI(c);
116             Method JavaDoc getSbMethod = editorUI.getClass().getMethod("getStatusBar"); //NOI18N
117
Object JavaDoc statusBar = getSbMethod.invoke(editorUI);
118             Method JavaDoc getTextMethod = statusBar.getClass().getMethod("getText", String JavaDoc.class); //NOI18N
119
return (String JavaDoc) getTextMethod.invoke(statusBar, "main"); //NOI18N
120
} catch (Exception JavaDoc e) {
121             LOG.log(Level.WARNING, e.getMessage(), e);
122             return ""; //NOI18N
123
}
124 // StatusBar sb = getEditorUI(c).getStatusBar();
125
// return (sb != null) ? sb.getText(StatusBar.CELL_MAIN) : null;
126
}
127
128     public static void clearStatusText(JTextComponent JavaDoc c) {
129         setStatusText(c, ""); // NOI18N
130
}
131     
132     
133     private static Object JavaDoc getEditorUI(JTextComponent JavaDoc c) throws Exception JavaDoc {
134         // TODO: fix this, do not use reflection
135
TextUI JavaDoc textUI = c.getUI();
136         Method JavaDoc getEuiMethod = textUI.getClass().getMethod("getEditorUI"); //NOI18N
137
return getEuiMethod.invoke(textUI);
138     }
139     
140     private static Frame JavaDoc getParentFrame(Component JavaDoc c) {
141         do {
142             c = c.getParent();
143             if (c instanceof Frame JavaDoc) {
144                 return (Frame JavaDoc)c;
145             }
146         } while (c != null);
147         return null;
148     }
149     
150     /** Creates a new instance of DocUtils */
151     private ComponentUtils() {
152     }
153 }
154
Popular Tags