Java JSpinner generateSpinnerFor(Object... objs)

Here you can find the source of generateSpinnerFor(Object... objs)

Description

generate Spinner For

License

Open Source License

Declaration

public static JSpinner generateSpinnerFor(Object... objs) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.event.ComponentListener;
import java.awt.event.ContainerListener;
import java.awt.event.FocusListener;
import java.awt.event.HierarchyBoundsListener;
import java.awt.event.HierarchyListener;
import java.awt.event.InputMethodListener;

import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
import java.beans.PropertyChangeListener;
import java.beans.VetoableChangeListener;

import javax.swing.JComponent;

import javax.swing.JSpinner;

import javax.swing.SpinnerModel;
import javax.swing.border.Border;
import javax.swing.event.AncestorListener;

import javax.swing.event.ChangeListener;

import javax.swing.plaf.SpinnerUI;

import java.awt.Color;
import java.awt.Container;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;

public class Main {
    public static JSpinner generateSpinnerFor(Object... objs) {
        Container c = search(objs, Container.class);
        String[] name_value = search(objs, String[].class);
        ChangeListener listener = search(objs, ChangeListener.class);
        SpinnerModel model = search(objs, SpinnerModel.class);
        Color color = search(objs, Color.class);
        Dimension dimension = search(objs, Dimension.class);
        Font font = search(objs, Font.class);
        Rectangle bounds = search(objs, Rectangle.class);
        Border border = search(objs, Border.class);
        SpinnerUI ui = search(objs, SpinnerUI.class);
        JSpinner spinner = model == null ? createSpinner() : createSpinner(model);
        if (border != null)
            spinner.setBorder(border);/*from  w  w w .  j a  v  a  2  s  .  c o m*/
        if (color != null)
            spinner.setBackground(color);
        if (dimension != null)
            spinner.setPreferredSize(dimension);
        if (font != null)
            spinner.setFont(font);
        if (listener != null)
            spinner.addChangeListener(listener);
        if (bounds != null)
            spinner.setBounds(bounds);
        if (ui != null)
            spinner.setUI(ui);
        spinner.setName(name_value == null ? "" : name_value[0]);
        addJContainerListeners(spinner, objs);
        addToContainer(c, spinner);
        return spinner;
    }

    public static <T extends Component> T search(String name, Component comp) {
        if (name.equals(comp.getName())) {
            return (T) comp;
        } else if (comp instanceof Container) {
            for (Component c : ((Container) comp).getComponents())
                if ((c = search(name, c)) != null)
                    return (T) c;
        }
        return null;
    }

    private static <T> T search(Object[] objs, Class c) {
        for (Object o : objs) {
            if (c.isInstance(o))
                return (T) o;
        }
        return null;
    }

    private static JSpinner createSpinner() {
        return new JSpinner();
    }

    private static JSpinner createSpinner(SpinnerModel model) {
        return new JSpinner(model);
    }

    private static void addJContainerListeners(JComponent c, Object... objs) {
        if (c == null)
            return;
        addContainerListeners(c, objs);
        AncestorListener ancestorListener = search(objs, AncestorListener.class);
        VetoableChangeListener vetoableChangeListener = search(objs, VetoableChangeListener.class);
        if (ancestorListener != null)
            c.addAncestorListener(ancestorListener);
        if (vetoableChangeListener != null)
            c.addVetoableChangeListener(vetoableChangeListener);
    }

    private static void addToContainer(Container c, Component element) {
        if (c == null)
            return;
        c.add(element);
    }

    private static void addContainerListeners(Container c, Object... objs) {
        if (c == null)
            return;
        addComponentListeners(c, objs);
        ContainerListener containerListener = search(objs, ContainerListener.class);
        PropertyChangeListener propertyChangeListener = search(objs, PropertyChangeListener.class);
        if (containerListener != null)
            c.addContainerListener(containerListener);
        if (propertyChangeListener != null)
            c.addPropertyChangeListener(propertyChangeListener);
    }

    private static void addComponentListeners(Component c, Object... objs) {
        if (c == null)
            return;
        ComponentListener componentListener = search(objs, ComponentListener.class);
        FocusListener focusListener = search(objs, FocusListener.class);
        HierarchyBoundsListener hierarchyBoundsListener = search(objs, HierarchyBoundsListener.class);
        HierarchyListener hierarchyListener = search(objs, HierarchyListener.class);
        InputMethodListener inputMethodListener = search(objs, InputMethodListener.class);
        KeyListener keyListener = search(objs, KeyListener.class);
        MouseListener mouseListener = search(objs, MouseListener.class);
        MouseMotionListener mouseMotionListener = search(objs, MouseMotionListener.class);
        MouseWheelListener mouseWheelListener = search(objs, MouseWheelListener.class);
        if (componentListener != null)
            c.addComponentListener(componentListener);
        if (focusListener != null)
            c.addFocusListener(focusListener);
        if (hierarchyBoundsListener != null)
            c.addHierarchyBoundsListener(hierarchyBoundsListener);
        if (hierarchyListener != null)
            c.addHierarchyListener(hierarchyListener);
        if (inputMethodListener != null)
            c.addInputMethodListener(inputMethodListener);
        if (keyListener != null)
            c.addKeyListener(keyListener);
        if (mouseListener != null)
            c.addMouseListener(mouseListener);
        if (mouseMotionListener != null)
            c.addMouseMotionListener(mouseMotionListener);
        if (mouseWheelListener != null)
            c.addMouseWheelListener(mouseWheelListener);
    }
}

Related

  1. createSpinnerForm(String label, JSpinner spinner)
  2. dereferenceSpinnerNumberModel(SpinnerNumberModel model)
  3. detachChangeListeners(JSpinner spinner)
  4. fixIntegerList(final JSpinner spinner)
  5. formatSpinner(JSpinner spinner, String format)
  6. getFloat(JSpinner spinner)
  7. getInt(JSpinner spinner)
  8. getInt(SpinnerNumberModel model)
  9. getOnlySpinner(Container owner)