misc.java
来自「优秀的打印控件全源代码,类似水晶表的设计器!」· Java 代码 · 共 514 行 · 第 1/2 页
JAVA
514 行
/* * Misc.java * * iReport -- Visual designer for generating JasperReports Documents * Copyright (C) 2002-2003 Giulio Toffoli gt@businesslogic.it * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Giulio Toffoli * Via T.Aspetti, 233 * 35100 Padova ITALY * gt@businesslogic.it * * Created on 14 febbraio 2003, 16.35 */package it.businesslogic.ireport.util;import java.awt.*;import java.awt.image.*;import java.util.*;import java.util.jar.*;import java.net.*;import java.io.*;/** * * @author Administrator */public class Misc { /** Creates a new instance of Misc */ public Misc() { } public static java.awt.Image loadImageFromResources(String filename) { try { ClassLoader cl = ClassLoader.getSystemClassLoader(); //java.io.InputStream in = new java.io.FileInputStream( cl.getResource(filename).getPath() ); java.io.InputStream in = cl.getResourceAsStream(filename); byte[] data = getBytesFromInputStream(in, in.available()); return java.awt.Toolkit.getDefaultToolkit().createImage(data); } catch (Exception ex) { System.out.println("Exception loading resource: "+filename); //ex.getMessage(); //ex.printStackTrace(); } return null; } /** * Returns an array of bytes containing the bytecodes for * the class represented by the InputStream * @param in the inputstream of the class file * @return the bytecodes for the class * @exception java.io.IOException if the class cannot be read */ private static byte[] getBytesFromInputStream(java.io.InputStream in, int length) throws java.io.IOException { java.io.DataInputStream din = new java.io.DataInputStream(in); byte[] bytecodes = new byte[length]; try { din.readFully(bytecodes); } finally { if (din != null) din.close(); } return bytecodes; } public static java.awt.image.BufferedImage loadBufferedImageFromResources(Component c,String filename) { try { Misc m = new Misc(); java.awt.Image img = loadImageFromResources(filename); MediaTracker mt = new MediaTracker(c); mt.addImage(img,0); mt.waitForID(0); int width= img.getWidth(null); int height= img.getHeight(null); BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics gg = bi.getGraphics(); gg.drawImage(img, 0,0, null); gg.dispose(); return bi; } catch (Exception ex){ System.out.println(ex.toString()); } return null; } public static void updateComboBox(javax.swing.JComboBox comboBox, Vector newItems) { updateComboBox(comboBox,newItems, false); } public static void updateComboBox(javax.swing.JComboBox comboBox, Vector newItems, boolean addNullEntry) { Object itemSelected = null; if (comboBox.getSelectedIndex() >=0 ) { itemSelected = comboBox.getSelectedItem(); } comboBox.removeAllItems(); boolean selected = false; boolean foundNullItem = false; Enumeration e = newItems.elements(); while (e.hasMoreElements()) { Object item = e.nextElement(); comboBox.addItem(item); if (item == itemSelected) { selected = true; comboBox.setSelectedItem(itemSelected); } if (item.equals("")) { foundNullItem = true; } } if (addNullEntry) { if (!foundNullItem) comboBox.insertItemAt("", 0); if (!selected) comboBox.setSelectedItem(""); } } /** Mthis method perform equals based on string rapresentation of objects * */ public static void updateStringComboBox(javax.swing.JComboBox comboBox, Vector newItems, boolean addNullEntry) { Object itemSelected = null; if (comboBox.getSelectedIndex() >=0 ) { itemSelected = comboBox.getSelectedItem()+""; } comboBox.removeAllItems(); boolean selected = false; boolean foundNullItem = false; Enumeration e = newItems.elements(); while (e.hasMoreElements()) { String item = ""+e.nextElement(); comboBox.addItem(item); if (item.equals(itemSelected)) { selected = true; comboBox.setSelectedItem(itemSelected); } if (item.equals("")) { foundNullItem = true; } } if (addNullEntry) { if (!foundNullItem) comboBox.insertItemAt("", 0); if (!selected) comboBox.setSelectedItem(""); } } public static String nvl(Object obj, String def) { return (obj == null) ? def : obj.toString(); } public static void centerFrame(java.awt.Component c) { java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit(); c.setLocation((int)((tk.getScreenSize().getWidth()-c.getWidth())/2), (int)((tk.getScreenSize().getHeight()- c.getHeight())/2) ); } /** * Replace s2 with s1 in s3 **/ public static String string_replace(String s1, String s2, String s3) { String string=""; string = ""; if (s2 == null || s3 == null || s2.length() == 0) return s3; int pos_i = 0; // posizione corrente. int pos_f = 0; // posizione corrente finale int len = s2.length(); while ( (pos_f = s3.indexOf(s2, pos_i)) >= 0) { string += s3.substring(pos_i,pos_f)+s1; //+string.substring(pos+ s2.length()); pos_f = pos_i = pos_f + len; } string += s3.substring(pos_i); return string; } public static java.awt.Image loadImageFromFile(String path) { java.io.File file = new java.io.File(path); if (file.exists()) { java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit(); java.awt.Image img = tk.createImage(path); try { java.awt.MediaTracker mt = new java.awt.MediaTracker( new javax.swing.JPanel() ); mt.addImage(img,0); mt.waitForID(0); } catch (Exception ex){ return null; } return img; } return null; } /** * This method inserts a blank character between to consecutive * newline characters if encoutered. Also appends a blank character at * the beginning of the text, if the first character is a newline character * and at the end of the text, if the last character is also a newline. * This is useful when trying to layout the paragraphs. * Thanks to Teodor Danciu for this this method (c) 2003 Teodor Danciu */ public static String treatNewLineChars(String source) { String result = source; if (source != null && source.length() > 0) { StringBuffer sbuffer = new StringBuffer(source); // insert a blank character between every two consecutives // newline characters int offset = source.length() - 1; int pos = source.lastIndexOf("\n\n", offset); while (pos >= 0 && offset > 0) { sbuffer = sbuffer.insert(pos + 1, " "); offset = pos - 1; pos = source.lastIndexOf("\n\n", offset); } // append a blank character at the and of the text // if the last character is a newline character if (sbuffer.charAt(sbuffer.length() - 1) == '\n') { sbuffer.append(' '); } // append a blank character at the begining of the text // if the first character is a newline character if (sbuffer.charAt(0) == '\n') { sbuffer.insert(0, ' '); } result = sbuffer.toString(); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?