1 19 20 package org.netbeans.modules.editor.lib2; 21 22 import java.awt.Component ; 23 import java.awt.Frame ; 24 import java.lang.reflect.Method ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 import javax.swing.plaf.TextUI ; 28 import javax.swing.text.BadLocationException ; 29 import javax.swing.text.JTextComponent ; 30 31 35 public final class ComponentUtils { 36 37 private static final Logger LOG = Logger.getLogger(Logger .class.getName()); 38 39 public static boolean isGuardedException(BadLocationException exc) { 40 return exc.getClass().getName().equals("GuardedException"); 41 } 42 43 public static void returnFocus() { 44 JTextComponent c = DocumentsRegistry.getMostActiveComponent(); 45 if (c != null) { 46 requestFocus(c); 47 } 48 } 49 50 public static void requestFocus(JTextComponent c) { 51 if (c != null) { 52 if (!EditorImplementation.getDefault().activateComponent(c)) { 53 Frame f = getParentFrame(c); 54 if (f != null) { 55 f.requestFocus(); 56 } 57 c.requestFocus(); 58 } 59 } 60 } 61 62 public static void setStatusText(JTextComponent c, String text) { 63 try { 65 Object editorUI = getEditorUI(c); 66 Method getSbMethod = editorUI.getClass().getMethod("getStatusBar"); 67 Object statusBar = getSbMethod.invoke(editorUI); 68 Method setTextMethod = statusBar.getClass().getMethod("setText", String .class, String .class); 69 setTextMethod.invoke(statusBar, "main", text); 70 } catch (Exception e) { 71 LOG.log(Level.WARNING, e.getMessage(), e); 72 } 73 } 78 79 95 public static void setStatusBoldText(JTextComponent c, String text) { 96 try { 98 Object editorUI = getEditorUI(c); 99 Method getSbMethod = editorUI.getClass().getMethod("getStatusBar"); Object statusBar = getSbMethod.invoke(editorUI); 101 Method setTextMethod = statusBar.getClass().getMethod("setBoldText", String .class, String .class); setTextMethod.invoke(statusBar, "main", text); } catch (Exception e) { 104 LOG.log(Level.WARNING, e.getMessage(), e); 105 } 106 } 111 112 public static String getStatusText(JTextComponent c) { 113 try { 115 Object editorUI = getEditorUI(c); 116 Method getSbMethod = editorUI.getClass().getMethod("getStatusBar"); Object statusBar = getSbMethod.invoke(editorUI); 118 Method getTextMethod = statusBar.getClass().getMethod("getText", String .class); return (String ) getTextMethod.invoke(statusBar, "main"); } catch (Exception e) { 121 LOG.log(Level.WARNING, e.getMessage(), e); 122 return ""; } 124 } 127 128 public static void clearStatusText(JTextComponent c) { 129 setStatusText(c, ""); } 131 132 133 private static Object getEditorUI(JTextComponent c) throws Exception { 134 TextUI textUI = c.getUI(); 136 Method getEuiMethod = textUI.getClass().getMethod("getEditorUI"); return getEuiMethod.invoke(textUI); 138 } 139 140 private static Frame getParentFrame(Component c) { 141 do { 142 c = c.getParent(); 143 if (c instanceof Frame ) { 144 return (Frame )c; 145 } 146 } while (c != null); 147 return null; 148 } 149 150 151 private ComponentUtils() { 152 } 153 } 154 | Popular Tags |