|
|
@@ -1,189 +0,0 @@
|
|
|
-/*
|
|
|
- * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
- * @copyright Ranides Atterwim
|
|
|
- * @license WTFPL
|
|
|
- * @url http://ranides.net/projects/assira
|
|
|
- */
|
|
|
-
|
|
|
-package net.ranides.assira.reflection;
|
|
|
-
|
|
|
-import java.awt.Component;
|
|
|
-import java.awt.Container;
|
|
|
-import javax.accessibility.Accessible;
|
|
|
-import javax.swing.*;
|
|
|
-import javax.swing.plaf.ComponentUI;
|
|
|
-import net.ranides.assira.text.StringCompiler;
|
|
|
-import net.ranides.assira.text.Strings;
|
|
|
-
|
|
|
-/**
|
|
|
- *
|
|
|
- * @author ranides
|
|
|
- */
|
|
|
-
|
|
|
-@SuppressWarnings("PMD.ShortVar")
|
|
|
-public final class ComponentInspector {
|
|
|
-
|
|
|
- private ComponentInspector() { }
|
|
|
-
|
|
|
- static String dumpControl(JButton control) {
|
|
|
- return Strings.sprintf("%s%s ", dumpControl( (Component)control ), control.getText() );
|
|
|
- }
|
|
|
-
|
|
|
- static String dumpControl(JLabel control) {
|
|
|
- return Strings.sprintf("%s%s ", dumpControl( (Component)control ), control.getText() );
|
|
|
- }
|
|
|
-
|
|
|
- static String dumpControl(JPanel control) {
|
|
|
- return Strings.sprintf("%s[%d:%d:%d:%d] ",
|
|
|
- dumpControl( (Component)control ),
|
|
|
- control.getLocation().x,
|
|
|
- control.getLocation().y,
|
|
|
- control.getWidth(),
|
|
|
- control.getHeight()
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- static String dumpControl(Component control) {
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
- result.append( control.getClass().getName() ).append(" - ");
|
|
|
- if(null != control.getName()) {
|
|
|
- result.append(StringCompiler.quote( control.getName() ) );
|
|
|
- }
|
|
|
- return result.toString();
|
|
|
- }
|
|
|
-
|
|
|
- /* ********************************************************************** */
|
|
|
-
|
|
|
- /**
|
|
|
- * Wyrzuca na konsolę listę wszystkich komponentów zawartych w kontenerze.
|
|
|
- * Wynik jest prezentowany w formie zagnieżdżonej listy numerowanej.
|
|
|
- * @param container kontener
|
|
|
- * @param indent początkowe wcięcie (lewy margines) listy
|
|
|
- */
|
|
|
- public static String iterate(Container container, String indent) {
|
|
|
- StringBuilder output = new StringBuilder();
|
|
|
- int max = container.getComponentCount();
|
|
|
-
|
|
|
- for(int i = 0; i<max; i++) {
|
|
|
- Component child = container.getComponent(i);
|
|
|
- output.append(indent).append(i).append(". ");
|
|
|
- if(child instanceof JButton) {
|
|
|
- output.append( dumpControl( (JButton)child ) );
|
|
|
- }
|
|
|
- else if(child instanceof JPanel) {
|
|
|
- output.append( dumpControl( (JPanel)child ) );
|
|
|
- }
|
|
|
- else if(child instanceof JLabel) {
|
|
|
- output.append( dumpControl( (JLabel)child ) );
|
|
|
- }
|
|
|
- else {
|
|
|
- output.append( dumpControl( child ) );
|
|
|
- }
|
|
|
-
|
|
|
- output.append("\n");
|
|
|
-
|
|
|
- if(child instanceof Container) {
|
|
|
- iterate((Container)child, " "+indent+i+".");
|
|
|
- }
|
|
|
- }
|
|
|
- return output.toString();
|
|
|
- }
|
|
|
-
|
|
|
- @edu.umd.cs.findbugs.annotations.SuppressWarnings({
|
|
|
- "DE_MIGHT_IGNORE",
|
|
|
- "DLS_DEAD_LOCAL_STORE",
|
|
|
- "REC_CATCH_EXCEPTION"
|
|
|
- })
|
|
|
- public static String iterate(ComponentUI ui, JComponent container, String indent) {
|
|
|
- StringBuilder output = new StringBuilder();
|
|
|
- int max = ui.getAccessibleChildrenCount(container);
|
|
|
-
|
|
|
- for(int i = 0; i<max; i++) {
|
|
|
- Accessible child = ui.getAccessibleChild(container, i);
|
|
|
- output.append(indent).append(i).append(". ");
|
|
|
-
|
|
|
- if(child instanceof JButton) {
|
|
|
- output.append( dumpControl( (JButton)child ) );
|
|
|
- }
|
|
|
- else if(child instanceof JPanel) {
|
|
|
- output.append( dumpControl( (JPanel)child ) );
|
|
|
- }
|
|
|
- else if(child instanceof JLabel) {
|
|
|
- output.append( dumpControl( (JLabel)child ) );
|
|
|
- }
|
|
|
- else {
|
|
|
- try { output.append( dumpControl( (Component)child ) ); }
|
|
|
- catch(Exception _) { /* ignore and continue enumeration */ } // NOPMD
|
|
|
- }
|
|
|
-
|
|
|
- output.append("\n");
|
|
|
-
|
|
|
- if(child instanceof Container) {
|
|
|
- output.append( iterate((Container)child, " "+indent+i+".") );
|
|
|
- }
|
|
|
- }
|
|
|
- return output.toString();
|
|
|
- }
|
|
|
-
|
|
|
- /* ********************************************************************** */
|
|
|
-
|
|
|
- /**
|
|
|
- * Zwraca komponent leżący w kontenerze. Struktura jest traktowana jako drzewo,
|
|
|
- * a indeks określa numer węzła na każdym kolejnym poziomie. Przykładowo
|
|
|
- * tablica {5,7} wybiera 5. element w pojemniku, następnie niego wybiera 7.
|
|
|
- * i zwraca jako wynik. Można podać ścieżkę o dowolnej długości.
|
|
|
- * @param container kontener
|
|
|
- * @param index numery węzłów, które mają zostać pobrane na kolejnych poziomach zagnieżdżenia
|
|
|
- * @return element albo null, jeśli nie istnieje
|
|
|
- */
|
|
|
- @edu.umd.cs.findbugs.annotations.SuppressWarnings({
|
|
|
- "DLS_DEAD_LOCAL_STORE"
|
|
|
- })
|
|
|
- public static Component find(Container container, int... index) {
|
|
|
- try {
|
|
|
- Component current = container;
|
|
|
- for(int i=0; i<index.length; i++) { current = ((Container)current).getComponent( index[i] ); }
|
|
|
- return current;
|
|
|
- } catch(RuntimeException _) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static <T extends Component> T find(Class<T> clazz, Container container, int... index) {
|
|
|
- Component result = find(container, index);
|
|
|
- return clazz.isInstance(result) ? clazz.cast(result) : null;
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings({"unchecked", "PMD"})
|
|
|
- public static <T extends Component> T $find(Container container, int[] index) {
|
|
|
- return (T)find(container, index);
|
|
|
- }
|
|
|
-
|
|
|
- /* ********************************************************************** */
|
|
|
-
|
|
|
- @edu.umd.cs.findbugs.annotations.SuppressWarnings({"BC_UNCONFIRMED_CAST", "DLS_DEAD_LOCAL_STORE"})
|
|
|
- public static JComponent find(ComponentUI ui, Accessible container, int... index) {
|
|
|
- try {
|
|
|
- JComponent icurrent = (JComponent)container;
|
|
|
- ComponentUI iui = ui;
|
|
|
- for(int i=0; i<index.length; i++) {
|
|
|
- icurrent = (JComponent)iui.getAccessibleChild( icurrent, index[i]);
|
|
|
- iui = UIManager.getUI(icurrent);
|
|
|
- }
|
|
|
- return icurrent;
|
|
|
- } catch(Exception _) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static <T extends JComponent> T find(Class<T> clazz, ComponentUI ui, Accessible container, int... index) {
|
|
|
- JComponent result = find(ui, container, index);
|
|
|
- return clazz.isInstance(result) ? clazz.cast(result) : null;
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings({"unchecked","PMD"})
|
|
|
- public static <T extends JComponent> T $find(ComponentUI ui, Accessible container, int[] index) {
|
|
|
- return (T)find(ui, container, index);
|
|
|
- }
|
|
|
-
|
|
|
-}
|