Package net.sf.jpluck.swing

Source Code of net.sf.jpluck.swing.GUIUtil

package net.sf.jpluck.swing;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.KeyEvent;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.plaf.InputMapUIResource;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;

public class GUIUtil {
    private GUIUtil() {
    }
   
    public static void enablePanel(JPanel p, boolean enabled) {
      for (int i = 0, n= p.getComponentCount(); i < n ; i++) {
        p.getComponent(i).setEnabled(enabled);
      }
    }

    public static Icon createIcon(String filename) {
        return new ImageIcon(GUIUtil.class.getClassLoader().getResource(filename));
    }

    public static JButton createToolButton(Action action, String iconFilename) {
        JButton button = new JButton(action);
        button.setFocusable(false);
        button.setIcon(createIcon(iconFilename));
        button.setText(null);
        button.setMnemonic(0);
        button.setMargin(new Insets(0, 0, 0, 0));
        return button;
    }

  public static JButton createToolButton(Action action) {
    JButton button = new JButton(action);
    button.setFocusable(false);
    button.setIcon((Icon)action.getValue(Action.SMALL_ICON));
    button.setText(null);
    button.setMnemonic(0);
    button.setMargin(new Insets(0, 0, 0, 0));
    return button;
  }
   
    public static void makeEqualHeight(JComponent comp1, JComponent comp2) {
      int height = comp1.getPreferredSize().height;
      Dimension d =comp2.getPreferredSize();
      comp2.setPreferredSize(new Dimension(d.width, height));
    }

    public static void setPreferredHeight(JComponent component, int height) {
        component.setPreferredSize(new Dimension(component.getPreferredSize().width, height));
    }

    public static void setPreferredWidth(JComponent component, int width) {
        component.setPreferredSize(new Dimension(width, component.getPreferredSize().height));
    }

    public static void clearToolTips(Component component) {
        if (component instanceof JComponent) {
            ((JComponent) component).setToolTipText(null);
        }
        if (component instanceof Container) {
            Component[] children = ((Container) component).getComponents();
            if (component instanceof JMenu) {
                children = ((JMenu) component).getMenuComponents();
            }
            for (int i = 0; i < children.length; i++) {
                clearToolTips(children[i]);
            }
        }
    }

    public static void configureEscapeKey(JRootPane rootPane, Action action) {
        String CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY"; // Can be anything
        KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, CANCEL_ACTION_KEY);
        rootPane.getActionMap().put(CANCEL_ACTION_KEY, action);
    }

    public static String getText(JTextComponent textComponent) {
        String text = textComponent.getText().trim();
        if (text.length() == 0) {
            return null;
        } else {
            return text;
        }
    }

    public static void configureTextFieldKeyBindings() {
        InputMap im = new InputMapUIResource();
        im.setParent((javax.swing.InputMap) UIManager.get("TextField.focusInputMap"));
        im.put(KeyStroke.getKeyStroke("shift DELETE"), DefaultEditorKit.cutAction);
        im.put(KeyStroke.getKeyStroke("control INSERT"), DefaultEditorKit.copyAction);
        im.put(KeyStroke.getKeyStroke("shift INSERT"), DefaultEditorKit.pasteAction);
        UIManager.put("TextField.focusInputMap", im);
    }

    public static void makeNarrow(JButton button, int margin) {
        int width = button.getFontMetrics(button.getFont()).stringWidth(button.getText());
        setPreferredWidth(button, width + margin * 2);
    }
}
TOP

Related Classes of net.sf.jpluck.swing.GUIUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.