00001
00058 package com.arcle.rmt.j2se.swing.ui;
00059
00060 import javax.swing.*;
00061
00062 import java.awt.Component;
00063 import java.awt.Container;
00064 import java.awt.Window;
00065 import java.awt.Frame;
00066 import java.beans.PropertyVetoException;
00067 import java.io.StringWriter;
00068 import java.io.PrintWriter;
00069
00070 import com.arcle.rmt.xplat.util.MutableReference;
00071
00077 public final class MoreSwingUtilities {
00078
00079 private MoreSwingUtilities() {
00080 throw new Error("MoreSwingUtilities is just a container for static methods");
00081 }
00082
00083
00091 public static boolean traverseAncestors(Component start, ComponentVisitor visitor) {
00092 Component current = start;
00093 boolean keepGoing;
00094 do {
00095 keepGoing = visitor.visitComponent(current);
00096 current = current.getParent();
00097 } while(keepGoing && current != null);
00098 return current == null;
00099 }
00100
00104 public static Frame findFirstParentFrame(Component start) {
00105 return (Frame) SwingUtilities.getAncestorOfClass(Frame.class, start);
00106 }
00107
00108
00117 public static void disposeImmediateParent(Component start) {
00118 traverseAncestors(start, new ComponentVisitor() {
00119 public boolean visitComponent(Component c) {
00120 if (c instanceof Window) {
00121 final Window w = (Window) c;
00122 SwingUtilities.invokeLater(new Runnable() {
00123 public void run() {
00124 removeFromParent(w);
00125 w.dispose();
00126 }
00127 });
00128 } else if (c instanceof JInternalFrame) {
00129 final JInternalFrame f = (JInternalFrame) c;
00147 JDesktopPane dp = f.getDesktopPane();
00148 try {
00149 f.doDefaultCloseAction();
00150 } catch(NullPointerException e) { }
00151 if (dp != null) {
00152 dp.invalidate();
00153 }
00154 } else {
00155
00156 return true;
00157 }
00158 return false;
00159 }
00160 });
00161 }
00162
00169 public static boolean removeFromParent(Component c) {
00170 if (c != null) {
00171 Container parent = c.getParent();
00172 if (parent != null) {
00173 parent.remove(c);
00174 return true;
00175 }
00176 }
00177 return false;
00178 }
00179
00185 public static void showException(Component parent, Exception e) {
00186 showException(parent, e, "Unhandled Exception");
00187 }
00188
00195 public static void showException(Component parent, Exception e,
00196 String title) {
00197 showThrowable(parent, e, title);
00198 }
00199
00200
00209 public static void showTextDialog(Component parent, String text,
00210 String title, int type) {
00211 JTextArea ta = new JTextArea(text);
00212 ta.setEditable(false);
00213 JOptionPane.showMessageDialog(parent, new JScrollPane(ta), title,
00214 type);
00215 }
00216
00223 public static void showThrowable(Component parent, Throwable t, String title) {
00224 StringWriter str = new StringWriter(4096);
00225 t.printStackTrace(new PrintWriter(str));
00226 showTextDialog(parent, str.toString(), title,
00227 JOptionPane.ERROR_MESSAGE);
00228 }
00229
00230 }