misc.java
来自「优秀的打印控件全源代码,类似水晶表的设计器!」· Java 代码 · 共 514 行 · 第 1/2 页
JAVA
514 行
// remove this if you want to treat the tab characters in a special way result = replaceTabWithBlank(result); return result; } /** * Thanks to Teodor Danciu for this method (c) 2003 Teodor Danciu */ public static String replaceTabWithBlank(String source) { String result = source; if (source != null && source.length() > 0) { StringBuffer sbuffer = new StringBuffer(source); int offset = 0; int pos = source.indexOf("\t", offset); while (pos >= 0) { sbuffer.setCharAt(pos, ' '); offset = pos + 1; pos = source.indexOf("\t", offset); } result = sbuffer.toString(); } return result; } public static String toHTML(String s) { s = Misc.string_replace(">",">",s); s = Misc.string_replace("<","<",s); s = Misc.string_replace(" "," ",s); s = Misc.string_replace(" ","\t",s); s = Misc.string_replace("<br>", "\n", s); return s; } static public String getShortFileName(String filename) { if (filename.length() > 50) { java.io.File f = new java.io.File(filename); if (nvl(f.getParentFile(),"").length()>10) { String dir = f.getParentFile().getPath()+java.io.File.separatorChar; String shortDir = dir.substring(0,dir.indexOf( java.io.File.separatorChar)+1); dir = dir.substring(dir.indexOf( java.io.File.separatorChar)+1); if (dir.indexOf( java.io.File.separatorChar) > 0) { shortDir += dir.substring(0, dir.indexOf( java.io.File.separatorChar)+1); } return shortDir + "..."+ java.io.File.separatorChar + f.getName(); } } return filename; } /** * Thanx to Jackie Manning j.m@programmer.net for this method!! */ public static String getJdbcTypeClass( int t ) { String cls = "java.lang.String"; switch( t ) { case java.sql.Types.TINYINT: case java.sql.Types.BIT: cls = "java.lang.Byte"; break; case java.sql.Types.SMALLINT: cls = "java.lang.Short"; break; case java.sql.Types.INTEGER: cls = "java.lang.Integer"; break; case java.sql.Types.REAL: case java.sql.Types.DOUBLE: case java.sql.Types.NUMERIC: case java.sql.Types.DECIMAL: cls = "java.lang.Double"; break; case java.sql.Types.CHAR: case java.sql.Types.VARCHAR: cls = "java.lang.String"; break; case java.sql.Types.BIGINT: cls = "java.lang.Long"; break; case java.sql.Types.DATE: case java.sql.Types.TIME: cls = "java.util.Date"; break; case java.sql.Types.TIMESTAMP: cls = "java.sql.Timestamp"; break; } return cls; } public static long getLastWriteTime(String filename) { try { java.io.File f = new java.io.File(filename); return f.lastModified(); } catch (Exception ex) { return -1; } } /** *Method used to grab the Frame which is above this component in the hierarchy. *This allows programmers to make any component the parent of any window or *dialog easier. *@param comp the component to get the Frame for *@return the Frame above this component in the hierarchy */ public static java.awt.Frame frameFromComponent(java.awt.Component parent) { java.awt.Frame f = (java.awt.Frame)javax.swing.SwingUtilities.getAncestorOfClass(java.awt.Frame.class, parent); return f; }//end frameFromComponent //ErtanO 12.03.2004 public static java.util.List getAvailablePLAF(){ java.util.List l = new java.util.ArrayList(); l.add("System"); l.add("TinyLAF"); l.add("JGoodiesLAF-PlasticXP"); l.add("JGoodiesLAF-Plastic"); l.add("JGoodiesLAF-Plastic3D"); l.add("JGoodiesLAF-ExtWindows"); javax.swing.UIManager.LookAndFeelInfo[] lfinfo = javax.swing.UIManager.getInstalledLookAndFeels(); for (int i=0; i<lfinfo.length; ++i) { l.add( lfinfo[i].getName() ); } return l; } public static void setPLAF(String s){ try { if(s.equals("TinyLAF")) { javax.swing.UIManager.setLookAndFeel("de.muntjak.tinylookandfeel.TinyLookAndFeel"); } else if(s.equals("JGoodiesLAF-PlasticXP")) { javax.swing.UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); } else if(s.equals("JGoodiesLAF-Plastic")) { javax.swing.UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticLookAndFeel"); } else if(s.equals("JGoodiesLAF-Plastic3D")) { javax.swing.UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.Plastic3DLookAndFeel"); } else if(s.equals("JGoodiesLAF-ExtWindows")) { javax.swing.UIManager.setLookAndFeel("com.jgoodies.plaf.windows.ExtWindowsLookAndFeel"); } else if(s.equals("System")) { javax.swing.UIManager.setLookAndFeel( javax.swing.UIManager.getSystemLookAndFeelClassName() ); } else { javax.swing.UIManager.LookAndFeelInfo[] lfinfo = javax.swing.UIManager.getInstalledLookAndFeels(); for (int i=0; i<lfinfo.length; ++i) { if (lfinfo[i].getName().equalsIgnoreCase( s )) { javax.swing.UIManager.setLookAndFeel( lfinfo[i].getClassName() ); return; } } } } catch (Exception ex) { ex.printStackTrace(); } } public static String getClassPath() { return (String)System.getProperty("java.class.path"); } /** * Enumerates the resouces in a give package name. * This works even if the resources are loaded from a jar file! * * Adapted from code by mikewse * on the java.sun.com message boards. * http://forum.java.sun.com/thread.jsp?forum=22&thread=30984 * * @param packageName The package to enumerate * @return A Set of Strings for each resouce in the package. */ public static Set getResoucesInPackage(String packageName) throws IOException { String localPackageName; if( packageName.endsWith("/") ) { localPackageName = packageName; } else { localPackageName = packageName + '/'; } Enumeration dirEnum = ClassLoader.getSystemResources( localPackageName ); Set names = new HashSet(); // Loop CLASSPATH directories while( dirEnum.hasMoreElements() ) { URL resUrl = (URL) dirEnum.nextElement(); // Pointing to filesystem directory if ( resUrl.getProtocol().equals("file") ) { File dir = new File( resUrl.getFile() ); File[] files = dir.listFiles(); if ( files != null ) { for( int i=0; i<files.length; i++ ) { File file = files[i]; if ( file.isDirectory() ) continue; names.add( localPackageName + file.getName() ); } } // Pointing to Jar file } else if ( resUrl.getProtocol().equals("jar") ) { JarURLConnection jconn = (JarURLConnection) resUrl.openConnection(); JarFile jfile = jconn.getJarFile(); Enumeration entryEnum = jfile.entries(); while( entryEnum.hasMoreElements() ) { JarEntry entry = (JarEntry) entryEnum.nextElement(); String entryName = entry.getName(); // Exclude our own directory if ( entryName.equals(localPackageName) ) continue; String parentDirName = entryName.substring( 0, entryName.lastIndexOf('/')+1 ); if ( ! parentDirName.equals(localPackageName) ) continue; names.add( entryName ); } } else { // Invalid classpath entry } } return names; } /** * Take a filename, strip out the extension and append the new extension * newExtension = ".xyz" or "xyz" * If filename is null, ".xyz" is returned */ public static String changeFileExtension(String filename, String newExtension ) { if (!newExtension.startsWith(".")) newExtension = "."+newExtension; if (filename == null || filename.length()==0 ) { return newExtension; } int index = filename.lastIndexOf("."); if (index >= 0) { filename = filename.substring(0,index); } return filename += newExtension; } }//end class Misc
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?