📄 swingutils.java
字号:
/* * JRemCntl - Copyright (C) 2007 Filippo Di Vattimo <fildiv@gmail.com> * See COPYING */package fildiv.jremcntl.server.gui.util.swing;import java.awt.Component;import java.awt.Cursor;import java.io.File;import java.util.Enumeration;import javax.swing.JFileChooser;import javax.swing.JOptionPane;import javax.swing.JTree;import javax.swing.filechooser.FileFilter;import javax.swing.tree.TreeNode;import javax.swing.tree.TreePath;import fildiv.jremcntl.server.core.JRemEnv;public class SwingUtils { private static final Cursor waitCursor = new Cursor(Cursor.WAIT_CURSOR); private static final Cursor defaultCursor = new Cursor( Cursor.DEFAULT_CURSOR); private static final String[] dlgYNCOptions = { "Yes", "No", "Cancel" }; private static final String[] dlgYNOptions = { "Yes", "No" }; private static final String[] dlgYOptions = { "Yes" }; private static final String[] dlgYCOptions = { "Yes", "Cancel" }; private static final String[] dlgOKOption = { "Ok" }; private static final String extXML = "xml"; public static abstract class AbstractFileFilter extends FileFilter { public boolean accept(File f) { if (f.isDirectory()) { return true; } String extension = getExtension(f); if (extension != null) { if (acceptExtension(extension)) return true; else return false; } return false; } abstract boolean acceptExtension(String extension); public String getDescription() { return "Configuration file (xml)"; } }; public static class DefaultFileFilter extends AbstractFileFilter { private String[] exts = {}; public DefaultFileFilter(String ext) { exts = new String[] { ext }; } public DefaultFileFilter(String[] exts) { this.exts = exts; } boolean acceptExtension(String extension) { for (int index = 0; index < exts.length; ++index) { String ext = exts[index]; if (ext.equals(extension)) return true; } return false; } } public static final DefaultFileFilter XML_FILE_FILTER = new DefaultFileFilter(extXML); public static void showWaitCursor(java.awt.Component component, boolean show) { if (show) component.setCursor(waitCursor); else component.setCursor(defaultCursor); } public static File showOpenDialog(JRemEnv env, Component parent) { return showOpenDialog(env, parent, null); } public static File showOpenDirDialog(JRemEnv env, Component parent) { return showOpenDialog(env, parent, null, JFileChooser.DIRECTORIES_ONLY); } public static File showOpenDialog(JRemEnv env, Component parent, FileFilter ff) { return showOpenDialog(env, parent, ff, JFileChooser.FILES_ONLY); } public static File showOpenDialog(JRemEnv env, Component parent, FileFilter ff, int selectionMode) { final JFileChooser fc = new JFileChooser(); fc.setFileFilter(ff); fc.setCurrentDirectory(new File(env.getConfigDirPath())); fc.setFileSelectionMode(selectionMode); int retVal = fc.showOpenDialog(parent); if (retVal != JFileChooser.APPROVE_OPTION) return null; return fc.getSelectedFile(); } public static File showSaveDialog(JRemEnv env, Component parent) { return showSaveDialog(env, parent, XML_FILE_FILTER); } public static File showSaveDialog(JRemEnv env, Component parent, FileFilter ff) { final JFileChooser fc = new JFileChooser(); fc.setFileFilter(ff); fc.setCurrentDirectory(new File(env.getConfigDirPath())); int retVal = fc.showSaveDialog(parent); if (retVal != JFileChooser.APPROVE_OPTION) return null; File f = fc.getSelectedFile(); if (f.exists()) { int ret = askYC(parent, env, "Do you want to override the file ? \n\n" + f.getAbsolutePath() + "\n"); if (ret == JFileChooser.CANCEL_OPTION) return null; } return f; } public static String getExtension(File f) { String ext = null; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i + 1).toLowerCase(); } return ext; } public static void showErrorMessage(JRemEnv env, Component parent, String message) { JOptionPane.showMessageDialog(parent, message, env.getApplicationName(), JOptionPane.ERROR_MESSAGE); } public static void showErrorMessage(Component parent, String title, String message) { JOptionPane.showMessageDialog(parent, message, title, JOptionPane.ERROR_MESSAGE); } public static int showMessage(Component parent, JRemEnv env, String message) { int n = JOptionPane.showOptionDialog(parent, message, env.getApplicationName(), JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, dlgOKOption, dlgOKOption[0]); return n; } public static int askYNC(Component parent, JRemEnv env, String message) { int n = JOptionPane.showOptionDialog(parent, message, env.getApplicationName(), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, dlgYNCOptions, dlgYNCOptions[2]); return n; } public static int askYN(Component parent, JRemEnv env, String message) { int n = JOptionPane.showOptionDialog(parent, message, env.getApplicationName(), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, dlgYNOptions, dlgYNOptions[1]); return n; } public static int askYC(Component parent, JRemEnv env, String message) { int n = JOptionPane.showOptionDialog(parent, message, env.getApplicationName(), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, dlgYCOptions, dlgYCOptions[1]); return n; } // From http://www.exampledepot.com/egs/javax.swing.tree/ExpandAll.html // If expand is true, expands all nodes in the tree. // Otherwise, collapses all nodes in the tree. public static void expandAll(JTree tree, boolean expand) { TreeNode root = (TreeNode)tree.getModel().getRoot(); // Traverse tree from root expandAll(tree, new TreePath(root), expand); } private static void expandAll(JTree tree, TreePath parent, boolean expand) { // Traverse children TreeNode node = (TreeNode)parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration e=node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode)e.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); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -