All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.alpharogroup.swing.components.factories.JComponentFactory Maven / Gradle / Ivy

There is a newer version: 5.5
Show newest version
package de.alpharogroup.swing.components.factories;

import java.awt.Component;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Map;

import javax.swing.JInternalFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import de.alpharogroup.swing.menu.MenuItemBean;
import de.alpharogroup.swing.menu.popup.listeners.PopupListener;
import de.alpharogroup.swing.splashscreen.SplashScreen;

/**
 * A factory class for create swing Component objects.
 */
public final class JComponentFactory {

	/**
	 * Factory method for create a {@link SplashScreen}.
	 *
	 * @param image
	 *            the image
	 * @param text
	 *            the text
	 * @return the new {@link SplashScreen}.
	 */
	public static SplashScreen newSplashScreen(final String image, final String text) {
		final SplashScreen splashscreen = new SplashScreen(image, text);
		return splashscreen;
	}

	/**
	 * Factory method for create a JPopupMenu.
	 *
	 * @return the new {@link JPopupMenu}.
	 */
	public static JPopupMenu newJPopupMenu() {
		return newJPopupMenu("");
	}

	/**
	 * Factory method for create a JPopupMenu with the specified
	 * title.
	 *
	 * @param label
	 *            the string that a UI may use to display as a title for the
	 *            popup menu.
	 * @return the new {@link JPopupMenu}.
	 */
	public static JPopupMenu newJPopupMenu(final String label) {
		final JPopupMenu popup = new JPopupMenu(label);
		return popup;
	}

	/**
	 * Factory method for create a JPopupMenu that will be add a
	 * MouseListener to the given Component and an
	 * array of JMenuItem that will be added to the popup.
	 *
	 * @param component
	 *            the component
	 * @param items
	 *            the JMenuItems
	 * @return the new {@link JPopupMenu}.
	 */
	public static JPopupMenu newJPopupMenu(final Component component, final JMenuItem... items) {
		return newJPopupMenu("", component, items);
	}

	/**
	 * Factory method for create a JPopupMenu that will be add a
	 * MouseListener to the given Component and an
	 * array of JMenuItem that will be added to the popup.
	 *
	 * @param label
	 *            the label
	 * @param component
	 *            the component
	 * @param items
	 *            the JMenuItems
	 * @return the new {@link JPopupMenu}.
	 */
	public static JPopupMenu newJPopupMenu(final String label, final Component component, final JMenuItem... items) {
		// Create the popup menu.
		final JPopupMenu popup = newJPopupMenu(label);
		for (final JMenuItem jMenuItem : items) {
			popup.add(jMenuItem);
		}
		// Add listener to the component so the popup menu can come up.
		final MouseListener popupListener = new PopupListener(popup);
		component.addMouseListener(popupListener);
		return popup;
	}

	/**
	 * Factory method for create a {@link JInternalFrame} object.
	 *
	 * @param title
	 *            the title
	 * @param resizable
	 *            the resizable
	 * @param closable
	 *            the closable
	 * @param maximizable
	 *            the maximizable
	 * @param iconifiable
	 *            the iconifiable
	 * @return the new {@link JInternalFrame}.
	 */
	public static JInternalFrame newInternalFrame(final String title, final boolean resizable, final boolean closable,
			final boolean maximizable, final boolean iconifiable) {
		final JInternalFrame internalFrame = new JInternalFrame(title, resizable, closable, maximizable, iconifiable);
		return internalFrame;
	}

	/**
	 * Factory method for create a {@link TrayIcon} object.
	 *
	 * @param imgFilename
	 *            the img filename
	 * @param appName
	 *            the app name
	 * @param systemTrayPopupMenu
	 *            the system tray popup menu
	 * @param actionListeners
	 *            the action listeners
	 * @return the new {@link TrayIcon}.
	 */
	public static TrayIcon newTrayIcon(final String imgFilename, final String appName,
			final PopupMenu systemTrayPopupMenu, final Map actionListeners) {
		final Image image = Toolkit.getDefaultToolkit().getImage(imgFilename);
		final TrayIcon trayIcon = new TrayIcon(image, appName, systemTrayPopupMenu);
		for (final Map.Entry actionListener : actionListeners.entrySet()) {
			trayIcon.setActionCommand(actionListener.getKey());
			trayIcon.addActionListener(actionListener.getValue());
		}
		return trayIcon;
	}

	/**
	 * Factory method for create a {@link PopupMenu} object.
	 *
	 * @param menuItemBeans
	 *            the menu item beans
	 * @return the new {@link PopupMenu}.
	 */
	public static PopupMenu newPopupMenu(final List menuItemBeans) {
		final PopupMenu popupMenu = new PopupMenu();
		for (final MenuItemBean menuItemBean : menuItemBeans) {
			final MenuItem miBringToFront = new MenuItem(menuItemBean.getLabel());
			miBringToFront.setActionCommand(menuItemBean.getCommand());
			miBringToFront.addActionListener(menuItemBean.getActionListener());
			popupMenu.add(miBringToFront);
		}
		return popupMenu;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy