001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor.lib2;
043:
044: import java.awt.Component;
045: import java.awt.Frame;
046: import java.lang.reflect.Method;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import javax.swing.plaf.TextUI;
050: import javax.swing.text.BadLocationException;
051: import javax.swing.text.JTextComponent;
052: import org.netbeans.api.editor.EditorRegistry;
053:
054: /**
055: *
056: * @author Vita Stejskal
057: */
058: public final class ComponentUtils {
059:
060: private static final Logger LOG = Logger.getLogger(Logger.class
061: .getName());
062:
063: public static boolean isGuardedException(BadLocationException exc) {
064: return exc.getClass().getName().equals("GuardedException");
065: }
066:
067: public static void returnFocus() {
068: JTextComponent c = EditorRegistry.lastFocusedComponent();
069: if (c != null) {
070: requestFocus(c);
071: }
072: }
073:
074: public static void requestFocus(JTextComponent c) {
075: if (c != null) {
076: if (!EditorImplementation.getDefault().activateComponent(c)) {
077: Frame f = getParentFrame(c);
078: if (f != null) {
079: f.requestFocus();
080: }
081: c.requestFocus();
082: }
083: }
084: }
085:
086: public static void setStatusText(JTextComponent c, String text) {
087: // TODO: fix this, do not use reflection
088: try {
089: Object editorUI = getEditorUI(c);
090: Method getSbMethod = editorUI.getClass().getMethod(
091: "getStatusBar");
092: Object statusBar = getSbMethod.invoke(editorUI);
093: Method setTextMethod = statusBar.getClass().getMethod(
094: "setText", String.class, String.class);
095: setTextMethod.invoke(statusBar, "main", text);
096: } catch (Exception e) {
097: LOG.log(Level.WARNING, e.getMessage(), e);
098: }
099: // StatusBar sb = getEditorUI(c).getStatusBar();
100: // if (sb != null) {
101: // sb.setText(StatusBar.CELL_MAIN, text);
102: // }
103: }
104:
105: // public static void setStatusText(JTextComponent c, String text, Coloring extraColoring) {
106: // TextUI textUI = c.getUI();
107: // try {
108: // Method getSbMethod = textUI.getClass().getMethod("getStatusBar");
109: // Object statusBar = getSbMethod.invoke(textUI);
110: // Method setTextMethod = statusBar.getClass().getMethod("setText", String.class, String.class);
111: // setTextMethod.invoke(statusBar, "main", text);
112: // } catch (Exception e) {
113: // LOG.log(Level.WARNING, e.getMessage(), e);
114: // }
115: //// StatusBar sb = getEditorUI(c).getStatusBar();
116: //// if (sb != null) {
117: //// sb.setText(StatusBar.CELL_MAIN, text, extraColoring);
118: //// }
119: // }
120:
121: public static void setStatusBoldText(JTextComponent c, String text) {
122: // TODO: fix this, do not use reflection
123: try {
124: Object editorUI = getEditorUI(c);
125: Method getSbMethod = editorUI.getClass().getMethod(
126: "getStatusBar"); //NOI18N
127: Object statusBar = getSbMethod.invoke(editorUI);
128: Method setTextMethod = statusBar.getClass().getMethod(
129: "setBoldText", String.class, String.class); //NOI18N
130: setTextMethod.invoke(statusBar, "main", text); //NOI18N
131: } catch (Exception e) {
132: LOG.log(Level.WARNING, e.getMessage(), e);
133: }
134: // StatusBar sb = getEditorUI(c).getStatusBar();
135: // if (sb != null) {
136: // sb.setBoldText(StatusBar.CELL_MAIN, text);
137: // }
138: }
139:
140: public static String getStatusText(JTextComponent c) {
141: // TODO: fix this, do not use reflection
142: try {
143: Object editorUI = getEditorUI(c);
144: Method getSbMethod = editorUI.getClass().getMethod(
145: "getStatusBar"); //NOI18N
146: Object statusBar = getSbMethod.invoke(editorUI);
147: Method getTextMethod = statusBar.getClass().getMethod(
148: "getText", String.class); //NOI18N
149: return (String) getTextMethod.invoke(statusBar, "main"); //NOI18N
150: } catch (Exception e) {
151: LOG.log(Level.WARNING, e.getMessage(), e);
152: return ""; //NOI18N
153: }
154: // StatusBar sb = getEditorUI(c).getStatusBar();
155: // return (sb != null) ? sb.getText(StatusBar.CELL_MAIN) : null;
156: }
157:
158: public static void clearStatusText(JTextComponent c) {
159: setStatusText(c, ""); // NOI18N
160: }
161:
162: private static Object getEditorUI(JTextComponent c)
163: throws Exception {
164: // TODO: fix this, do not use reflection
165: TextUI textUI = c.getUI();
166: Method getEuiMethod = textUI.getClass()
167: .getMethod("getEditorUI"); //NOI18N
168: return getEuiMethod.invoke(textUI);
169: }
170:
171: private static Frame getParentFrame(Component c) {
172: do {
173: c = c.getParent();
174: if (c instanceof Frame) {
175: return (Frame) c;
176: }
177: } while (c != null);
178: return null;
179: }
180:
181: /** Creates a new instance of DocUtils */
182: private ComponentUtils() {
183: }
184: }
|