Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.awt.Component;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.MenuElement;
import javax.swing.tree.TreeModel;

public class Main {
    static final String NEWLINE = System.lineSeparator();

    private static void appendTreeNodes(TreeModel model, Object node, StringBuilder builder, String indent) {
        builder.append(NEWLINE);
        builder.append(indent);
        if (node instanceof Component) {
            builder.append(node.toString());
            appendComponentStructure((Component) node, builder, "|   " + indent);
        } else {
            builder.append(node.toString());
            builder.append("  (");
            appendClassDerivation(node, builder);
            builder.append(")");
        }
        for (int i = 0; i < model.getChildCount(node); i++)
            appendTreeNodes(model, model.getChild(node, i), builder, "|   " + indent);
    }

    private static void appendComponentStructure(Component component, StringBuilder builder) {
        appendComponentStructure(component, builder, "");
    }

    private static void appendComponentStructure(Component component, StringBuilder builder, String indent) {
        builder.append(NEWLINE);
        builder.append(indent);
        builder.append(component.getName());
        builder.append("(");
        builder.append(component.getClass().getName());
        builder.append(")");
        appendComponentDetails(component, builder);
        if (component instanceof JTree)
            appendTreeNodes(((JTree) component).getModel(), ((JTree) component).getModel().getRoot(), builder,
                    "|   " + indent);
        if (component instanceof JMenuBar) {
            appendMenuItem(component, builder, "|   " + indent);
        } else if (component instanceof Container) {
            for (Component subComponent : ((Container) component).getComponents())
                appendComponentStructure(subComponent, builder, "|   " + indent);
        }
    }

    private static void appendClassDerivation(Object object, StringBuilder builder) {
        builder.append(object.getClass().getSimpleName());
        Class<?> c = object.getClass().getSuperclass();
        String s = null;
        while (c != null) {
            s = c.getSimpleName() + "." + s;
            c = c.getSuperclass();
        }
        builder.append(s);
    }

    private static void appendComponentDetails(Component component, StringBuilder builder) {
        builder.append(component.isEnabled() ? "" : "[Disabled]");
        if (component instanceof JButton) {
            builder.append("{");
            builder.append("JButton: ");
            builder.append(((JButton) component).getText());
            builder.append("}");
        } else if (component instanceof JCheckBox) {
            builder.append("{");
            builder.append("JCheckBox: ");
            builder.append(((JCheckBox) component).getText());
            builder.append("}");
        } else if (component instanceof JLabel) {
            builder.append("{");
            builder.append("JLabel: ");
            builder.append(((JLabel) component).getText());
            builder.append("}");
        } else if (component instanceof JOptionPane) {
            builder.append("{");
            builder.append("JOptionPane: ");
            builder.append(((JOptionPane) component).getMessage().toString());
            builder.append("}");
        } else if (component instanceof JRadioButton) {
            builder.append("{");
            builder.append("JRadioButton: ");
            builder.append(((JRadioButton) component).getText());
            builder.append("}");
        } else if (component instanceof JTextField) {
            builder.append("{");
            builder.append("JTextField: ");
            builder.append(((JTextField) component).getText());
            builder.append("}");
        } else if (component instanceof JMenuBar) {
            builder.append("{");
            builder.append("JMenuBar: ");
            builder.append(((JMenuBar) component).getName());
            builder.append("}");
        } else if (component instanceof JMenuItem) {
            builder.append("{");
            builder.append("JMenuItem: ");
            builder.append(((JMenuItem) component).getText());
            builder.append("}");
        } else if (component instanceof JTree) {
            builder.append("{");
            builder.append("JTree: ");
            builder.append("}");
        } else if (component instanceof JComboBox) {
            builder.append("{");
            builder.append("JComboBox: ");
            builder.append("}");
        } else if (component instanceof JList) {
            builder.append("{");
            builder.append("JList: ");
            builder.append("}");
        }
    }

    private static void appendMenuItem(Component menuItem, StringBuilder builder, String indent) {
        if (menuItem instanceof JMenuBar) {
            appendMenuSubElements((MenuElement) menuItem, builder, indent);
        } else if (menuItem instanceof JPopupMenu) {
            appendMenuSubElements((MenuElement) menuItem, builder, indent);
        } else if (menuItem instanceof JMenuItem) {
            builder.append(NEWLINE);
            builder.append(indent);
            builder.append(((JMenuItem) menuItem).getText());
            builder.append(((JMenuItem) menuItem).isEnabled() ? "" : "[Disabled]");
            appendMenuSubElements((JMenuItem) menuItem, builder, "|   " + indent);
        } else if (menuItem instanceof JSeparator) {
            builder.append(NEWLINE);
            builder.append(indent);
            builder.append("--------");
        }
    }

    private static void appendMenuSubElements(MenuElement element, StringBuilder builder, String indent) {
        for (MenuElement subItem : element.getSubElements()) {
            appendMenuItem((Component) subItem, builder, indent);
        }
    }
}