Package versusSNP.util.swing

Source Code of versusSNP.util.swing.SwingUtils

package versusSNP.util.swing;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public final class SwingUtils {

  /**
   * Set the frame center into the screen, the size of the frame must be set
   * before calling this method
   *
   * @param frame
   *            - the frame to be set location
   */
  public static void centerScreen(Frame frame) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height)
      frameSize.height = screenSize.height;
    if (frameSize.width > screenSize.width)
      frameSize.width = screenSize.width;
    frame.setLocation((screenSize.width - frameSize.width) / 2,
        (screenSize.height - frameSize.height) / 2);
  }

  /**
   * UI��ʹ�õ�Ĭ������
   */
  private static final javax.swing.plaf.FontUIResource font = new javax.swing.plaf.FontUIResource(
      "", Font.PLAIN, 12);

  /**
   * ����SwingUI,ʹ����΢�ÿ�����������
   */
  public static void setTheme() {
    try {
      javax.swing.plaf.metal.MetalLookAndFeel
          .setCurrentTheme(new javax.swing.plaf.metal.DefaultMetalTheme() {
            public javax.swing.plaf.FontUIResource getControlTextFont() {
              return font;
            }

            public javax.swing.plaf.FontUIResource getMenuTextFont() {
              return font;
            }
          });
      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (Exception e) {
    }
  }

  /**
   * ��ʼ��һ�����ڵĿ�Ⱥ͸߶�,��ʹ�������ʾ
   *
   * @param window
   *            ����,����ΪJWindow JFrame����JDialog
   * @param w
   *            ���
   * @param h
   *            �߶�
   */
  public static void initSizeAndLocation(Window window, int w, int h) {
    window.setSize(w, h);
    Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
    window.setLocation(size.width / 2 - w / 2, size.height / 2 - h / 2);
  }

  /**
   * ����һ������Ϊ�����ʾ
   *
   * @param window
   *            ����,����ΪJWindow JFrame����JDialog
   * @deprecated setFullScreeen
   */
  public static void setMaxSize(Window window) {
    Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
    window.setSize(size.width, size.height - 50);
    window.setLocation(0, 0);
  }

  /**
   * �ȴ�һ��������ʧ,������ǰ�߳�
   *
   * @param w
   *            ����,����ΪJWindow JFrame����JDialog
   */
  public static void waitForWindow(Window w) {
    try {
      while (true) {
        if (w.isShowing() == false) {
          return;
        } else {
          Thread.sleep(500L);
        }
      }
    } catch (InterruptedException ex) {
    }
  }

  /**
   * ����ij��Frame���ܹر�
   *
   * @param frame
   *            Frame
   */
  public static void setFrameCannotClose(JFrame frame) {
    // frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
  }

  /**
   * ����ij���Ի����ܹر�
   *
   * @param dialog
   *            Dialog
   */
  public static void setDialogCannotClose(JDialog dialog) {
    // dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dialog.getRootPane().setWindowDecorationStyle(
        JRootPane.INFORMATION_DIALOG);
  }

  /**
   * ����ij��Frameʼ������ǰ
   *
   * @param frame
   *            Frame
   */
  public static void setFrameAlwaysFocus(final JFrame frame) {
    new javax.swing.Timer(100, new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        // frame.toFront();
      }
    }).start();
  }

  /**
   * ��ȫչ��һ��JTree
   *
   * @param tree
   *            JTree
   */
  public static void expandTree(JTree tree) {
    TreeNode root = (TreeNode) tree.getModel().getRoot();
    expandAll(tree, new TreePath(root), true);
  }

  /**
   * ��ȫչ����ر�һ����,���ڵݹ�ִ��
   *
   * @param tree
   *            JTree
   * @param parent
   *            ���ڵ�
   * @param expand
   *            Ϊtrue���ʾչ����,����Ϊ�ر�������
   */
  private static void expandAll(JTree tree, TreePath parent, boolean expand) {
    // Traverse children
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
      for (Enumeration<TreeNode> en = node.children(); en
          .hasMoreElements();) {
        TreeNode n = en.nextElement();
        TreePath path = parent.pathByAddingChild(n);
        expandAll(tree, path, expand);
      }
    }

    // Expansion or collapse must be done bottom-up
    if (expand) {
      tree.expandPath(parent);
    } else {
      tree.collapsePath(parent);
    }
  }

  public static JPanel createLabelPanel(JLabel label) {
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    p.add(Box.createGlue());
    Box box = Box.createHorizontalBox();
    box.add(label);
    p.add(box);
    p.add(Box.createGlue());
    return p;
  }

  public static JPanel createLabelPanel(String label) {
    return createLabelPanel(new JLabel(label));
  }

  public static void setFullScreen(Frame frame) {
    GraphicsDevice myDevice = GraphicsEnvironment
        .getLocalGraphicsEnvironment().getDefaultScreenDevice();
    myDevice.setFullScreenWindow(frame);
  }
}
TOP

Related Classes of versusSNP.util.swing.SwingUtils

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.