Example usage for java.awt Container getClass

List of usage examples for java.awt Container getClass

Introduction

In this page you can find the example usage for java.awt Container getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

public static Container getTopContainer(Component component, Class<Component> lookupClass) {
    Container parent = component.getParent();
    if (parent.getClass().equals(lookupClass)) {
        return parent;
    } else {// w ww.j  av  a2s .  co m
        return getTopContainer(parent, lookupClass);
    }
}

From source file:Main.java

/**
 * Obtains selected text//from   w w w  .j av a2 s. co  m
 *
 * @param component the component that is an EditorPane or some of its
 *      subcomponents is an EditorPane
 * @return current caret position
 */
public static String getSelectedText(Container component) {
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method caretGetter = component.getClass().getMethod("getSelectedText",
                    new Class[] {});
            Object result = caretGetter.invoke(component, new Object[] {});
            if (result == null) {
                return null;
            }
            return (String) result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        Component childComponent = component.getComponent(i);
        if (childComponent instanceof JComponent) {
            return getSelectedText((JComponent) childComponent);
        }
    }

    return null;
}

From source file:Main.java

/**
 * Gets the current cursor position in the component or one of it's
 * subcomponents.// w  w  w . ja  va  2s .  c  o  m
 *
 * @param component the component that is an EditorPane or some of its
 *      subcomponents is an EditorPane
 * @return current caret position
 */
public static int getCaretPosition(Container component) {
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method caretGetter = component.getClass().getMethod("getCaretPosition",
                    new Class[] {});
            Object result = caretGetter.invoke(component, new Object[] {});
            return ((Integer) result).intValue();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        java.awt.Component childComponent = component.getComponent(i);
        if (childComponent instanceof javax.swing.JComponent) {
            int result = getCaretPosition((javax.swing.JComponent) childComponent);

            if (result >= 0) {
                return result;
            }
        }
    }

    return -1;
}

From source file:Main.java

@SuppressWarnings("unchecked")
private static <T> void addAllOf(Container container, Class<T> clazz, List<T> all) {
    int count = container.getComponentCount();

    if (container.getClass().equals(clazz)) {
        all.add((T) container);/*from  w  w  w. jav  a2s.  co m*/
    }

    for (int i = 0; i < count; i++) {
        Component component = container.getComponent(i);

        if (component instanceof Container) {
            addAllOf((Container) component, clazz, all); // Recursive
        } else if (component.getClass().equals(clazz)) {
            all.add((T) component);
        }
    }
}

From source file:Main.java

public static Container findContainer(Component thisComp, Class<? extends Container> containerClass) {
    if (thisComp == null) {
        return null;
    }// w  w  w .j  av  a 2 s .co  m
    Container parent = thisComp.getParent();
    do {
        parent = parent.getParent();
        if (parent != null && containerClass.isAssignableFrom(parent.getClass())) {
            return parent;
        }
    } while (parent != null);
    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
private static <T> void addAllOfExclude(Container container, Class<T> clazz, List<T> all,
        Collection<? extends Component> exclude) {
    if (exclude.contains(container)) {
        return;/*from  w  w w  .j  a va 2s  . co m*/
    }
    if (container.getClass().equals(clazz)) {
        all.add((T) container);
    }
    int count = container.getComponentCount();
    for (int i = 0; i < count; i++) {
        Component component = container.getComponent(i);
        if (exclude.contains(component)) {
            continue;
        }
        if (component instanceof Container) {
            addAllOfExclude((Container) component, clazz, all, exclude); // Recursive
        } else if (component.getClass().equals(clazz)) {
            all.add((T) component);
        }
    }
}

From source file:Main.java

public static Point positionToClickPoint(Container component, int caretPosition, Container invokedIn) {
    if (component == null) {
        return null;
    }//w w w.j  a va2s.c  o m

    //System.err.println("Checking: " + component.getClass().getName());
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method pointGetter = component.getClass().getMethod("modelToView",
                    new Class[] { Integer.TYPE });
            Rectangle rec = (Rectangle) pointGetter.invoke(component,
                    new Object[] { new Integer(caretPosition) });
            //System.err.println("Before: " + (int)rec.getY());
            // FIXME: somehow it fails here to convert point from scrollable component
            Point point = SwingUtilities.convertPoint(component, (int) rec.getX(), (int) rec.getY() + 10,
                    invokedIn);
            // FIXME: ugly hack :(
            if (point.getY() > 1024) {
                point = new Point((int) point.getX(), 250);
            }
            //System.err.println("After: " + (int)point.getY());
            return point;
        } catch (Exception e) {
            System.err.println("Method invocation exception caught");
            e.printStackTrace();

            //FIXME: BUG
            return null;
            //throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        java.awt.Component childComponent = component.getComponent(i);
        if (childComponent instanceof javax.swing.JComponent) {
            Point point = positionToClickPoint((javax.swing.JComponent) childComponent, caretPosition,
                    invokedIn);
            if (point != null) {
                return point;
            }
        }
    }

    return null;
}

From source file:net.sourceforge.squirrel_sql.fw.gui.debug.DebugEventListener.java

private void setToolTipText(JComponent source, AWTEvent event) {

    Container parent = source.getParent();
    String sourceName = source.getName();
    String sourceClassName = source.getClass().toString();
    String parentClassName = parent == null ? null : parent.getClass().toString();

    StringBuilder toolTipText = new StringBuilder(getEventMessagePrefix(event));

    if (source instanceof AbstractButton) {
        toolTipText.append("Button with parentClass=");
        toolTipText.append(parentClassName);
    } else {//  w  w  w  . ja  v  a2 s.c om
        if (!StringUtils.isEmpty(sourceName)) {
            toolTipText.append(sourceName);
        } else if (!StringUtils.isEmpty(sourceClassName)) {
            toolTipText.append(sourceClassName);
        }
    }
    source.setToolTipText(toolTipText.toString());
}

From source file:net.sourceforge.squirrel_sql.fw.gui.debug.DebugEventListener.java

private void printDebugInfo(JComponent source, AWTEvent event) {
    Container parent = source.getParent();
    String sourceName = source.getName();
    String sourceClassName = source.getClass().toString();
    String parentName = parent == null ? null : parent.getName();
    String parentClassName = parent == null ? null : parent.getClass().toString();

    StringBuilder msg = new StringBuilder(getEventMessagePrefix(event));
    msg.append("\n");
    msg.append("\t sourceName:").append(sourceName).append("\n");
    msg.append("\t sourceClassName:").append(sourceClassName).append("\n");
    msg.append("\t parentName:").append(parentName).append("\n");
    msg.append("\t parentClassName:").append(parentClassName);
    System.out.println(msg.toString());
}