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

com.github.becausetesting.commonswindow.utils.ui.JPaneUtils Maven / Gradle / Ivy

package com.github.becausetesting.commonswindow.utils.ui;

/*-
 * false
 * commons-window
 * sectionDelimiter
 * Copyright (C) 2016 Alter Hu
 * sectionDelimiter
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

/*-
 * #%L
 * commons-window
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2016 Alter Hu
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import java.awt.Component;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import com.github.becausetesting.commonswindow.utils.LookAndFeelUtils;
import com.sun.jna.Platform;

import net.miginfocom.swing.MigLayout;

public class JPaneUtils {

	
	/**
	 * Create a dialog button panel with the order and alignment dependant on
	 * the platform.
	 *
	 * @param jbPositive
	 *            Positive button
	 * @param resizable
	 *            Is the dialog resizable?
	 * @return Dialog button panel
	 */
	public static JPanel createDialogButtonPanel(JButton jbPositive, boolean resizable) {
		return createDialogButtonPanel((jbPositive == null ? null : new JButton[] { jbPositive }), resizable);
	}

	/**
	 * Create a dialog button panel with the order and alignment dependant on
	 * the platform.
	 *
	 * @param jbPositives
	 *            Positive buttons
	 * @param resizable
	 *            Is the dialog resizable?
	 * @return Dialog button panel
	 */
	public static JPanel createDialogButtonPanel(JButton[] jbPositives, boolean resizable) {
		return createDialogButtonPanel(jbPositives, null, null, resizable, null);
	}

	/**
	 * Create a dialog button panel with the order and alignment dependant on
	 * the platform.
	 *
	 * @param jbPositive
	 *            Positive button
	 * @param jbNegative
	 *            Negative button
	 * @param resizable
	 *            Is the dialog resizable?
	 * @return Dialog button panel
	 */
	public static JPanel createDialogButtonPanel(JButton jbPositive, JButton jbNegative, boolean resizable) {
		return createDialogButtonPanel((jbPositive == null ? null : new JButton[] { jbPositive }), jbNegative,
				resizable);
	}

	/**
	 * Create a dialog button panel with the order and alignment dependant on
	 * the platform.
	 *
	 * @param jbPositives
	 *            Positive buttons
	 * @param jbNegative
	 *            Negative button
	 * @param resizable
	 *            Is the dialog resizable?
	 * @return Dialog button panel
	 */
	public static JPanel createDialogButtonPanel(JButton[] jbPositives, JButton jbNegative, boolean resizable) {
		return createDialogButtonPanel(jbPositives, jbNegative, null, resizable, null);
	}

	/**
	 * Create a dialog button panel with the order and alignment dependant on
	 * the platform.
	 *
	 * @param jbPositive
	 *            Positive button
	 * @param jbNegative
	 *            Negative button
	 * @param jbOther
	 *            Other button
	 * @param resizable
	 *            Is the dialog resizable?
	 * @return Dialog button panel
	 */
	public static JPanel createDialogButtonPanel(JButton jbPositive, JButton jbNegative, JButton jbOther,
			boolean resizable) {
		return createDialogButtonPanel(jbPositive, jbNegative, (jbOther == null ? null : new JButton[] { jbOther }),
				resizable);
	}

	/**
	 * Create a dialog button panel with the order and alignment dependant on
	 * the platform.
	 *
	 * @param jbPositive
	 *            Positive button
	 * @param jbNegative
	 *            Negative button
	 * @param jbOther
	 *            Other button
	 * @param resizable
	 *            Is the dialog resizable?
	 * @return Dialog button panel
	 */
	public static JPanel createDialogButtonPanel(JButton jbPositive, JButton jbNegative, JButton[] jbOther,
			boolean resizable) {
		return createDialogButtonPanel((jbPositive == null ? null : new JButton[] { jbPositive }), jbNegative, jbOther,
				resizable, null);
	}

	/**
	 * Create a dialog button panel with the order and alignment dependent on
	 * the platform.
	 *
	 * @param jbPositives
	 *            Positive buttons
	 * @param jbNegative
	 *            Negative button
	 * @param jbOthers
	 *            Other buttons
	 * @param resizable
	 *            Is the dialog resizable?
	 * @param insets
	 *            Insets for panel (MiGLayout constraint)
	 * @return Dialog button panel
	 */
	public static JPanel createDialogButtonPanel(JButton[] jbPositives, JButton jbNegative, JButton[] jbOthers,
			boolean resizable, String insets) {

		if (insets == null) {
			insets = "";
		} else {
			insets += ",";
		}

		JPanel panel = new JPanel(new MigLayout(insets + "nogrid, fillx, aligny 100%"));

		if (jbPositives != null) {
			for (JButton jButton : jbPositives) {
				panel.add(jButton, "tag ok");
			}
		}

		if (jbOthers != null) {
			for (JButton jButton : jbOthers) {
				panel.add(jButton, "sgx");
			}
		}

		if (jbNegative != null) {
			panel.add(jbNegative, "tag cancel");
		}

		return panel;
	}

	/**
	 * Create a dialog button panel with the order and alignment dependent on
	 * the platform.
	 * 
* This method creates zero spacing around the buttons. * * @param jbPositive * Positive button * @param jbNegative * Negative button * @return Dialog button panel */ public static JPanel createDialogButtonPanel(JButton jbPositive, JButton jbNegative) { JPanel panel = new JPanel(new MigLayout("insets 0, nogrid, fillx, aligny 100%")); if (jbPositive != null) { panel.add(jbPositive, "tag ok"); } if (jbNegative != null) { panel.add(jbNegative, "tag cancel"); } return panel; } /** * Create a scroll pane whose scroll bar policy conforms with the current * platform. i.e. on Mac OS if a scroll bar's policy states that it may * be shown as needed it should instead always be shown. For all other * platforms obey policy as provided. * * @param vsbPolicy * Vertical scroll bar policy * @param hsbPolicy * Horizontal scroll bar policy * @return Scroll pane */ public static JScrollPane createScrollPane(int vsbPolicy, int hsbPolicy) { return createScrollPane(null, vsbPolicy, hsbPolicy); } /** * Create a scroll pane whose scroll bar policy conforms with the current * platform. i.e. on Mac OS if a scroll bar's policy states that it may * be shown as needed it should instead always be shown. For all other * platforms obey policy as provided. * * @param view * Component to view * @param vsbPolicy * Vertical scroll bar policy * @param hsbPolicy * Horizontal scroll bar policy * @return Scroll pane */ public static JScrollPane createScrollPane(Component view, int vsbPolicy, int hsbPolicy) { // If Mac l&f convert supplied "scroll bars as needed" to "scroll bars always" if (LookAndFeelUtils.usingMacLnf()) { if (vsbPolicy == ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED) { vsbPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; } if (hsbPolicy == ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED) { hsbPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; } } JScrollPane scrollPane; if (view != null) { scrollPane = new JScrollPane(view, vsbPolicy, hsbPolicy); } else { scrollPane = new JScrollPane(vsbPolicy, hsbPolicy); } return scrollPane; } /** * Set mnemonic on button in a platform dependant manner. * * @param button * Button * @param mnemonic * Mnemonic */ public static void setMnemonic(AbstractButton button, int mnemonic) { setMnemonic(button, (char) mnemonic, -1); } /** * Set mnemonic on button in a platform dependant manner. * * @param button * Button * @param mnemonic * Mnemonic */ public static void setMnemonic(AbstractButton button, char mnemonic) { setMnemonic(button, mnemonic, -1); } /** * Set mnemonic on button in a platform dependant manner. * * @param button * Button * @param mnemonic * Mnemonic * @param index * Index of string to underline */ public static void setMnemonic(AbstractButton button, int mnemonic, int index) { setMnemonic(button, (char) mnemonic, index); } /** * Set mnemonic on button in a platform dependant manner. * * @param button * Button * @param mnemonic * Mnemonic * @param index * Index of string to underline */ public static void setMnemonic(AbstractButton button, char mnemonic, int index) { /* * Only set mnemonic if not using Mac OS - they are not recommended by * the style guidelines there and clash with established commands */ if (!Platform.isMac()) { button.setMnemonic(mnemonic); if (index >= 0) { button.setDisplayedMnemonicIndex(index); } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy