当前位置 : 主页 > 编程语言 > java >

CS-CommonUtil

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt package cn.huawei.com.CompressedSeacher.util;import cn.huawei.com.CompressedSeacher.view.StartUp;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Point;import java.io.File;import java.i
gistfile1.txt
package cn.huawei.com.CompressedSeacher.util;
import cn.huawei.com.CompressedSeacher.view.StartUp;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;

/**
 * **
 *
 * @author l00358914
 */
public class CommonUtil {
    private static CommonUtil instance = new CommonUtil();
    public static final int filesPerThread = 30;
    public static final String[] NONE_CLASS_extensions = {".java", ".xml", ".txt", ".TXT", ".properties", ".MF", ".wsdl", ".WSDL", ".mf", ".html", "HTML", ".dtd", ".DTD", ".xsd", ".XSD", ".jsp", ".JSP", ".sql", ".SQL", ".c", ".h", ".C", ".H"};
    public static CommonUtil getInstance() {
        return instance;
    }
    public static boolean isOneOfTheFileType(String value) {
        for (int a = 0; a < NONE_CLASS_extensions.length; a++) {
            if (value.trim().endsWith(NONE_CLASS_extensions[a])) {
                return true;
            }
            continue;
        }
        return false;
    }
    /**
     * **
     *
     * @param width
     * @param height
     * @return
     */
    public Point returnCenterPoint(int width, int height) {
//        Dimension size = new Dimension(width, height);
        Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        int x = 0;
        int y = 0;
        x = (int) ((d.getWidth() - width) / 2);
        y = (int) (d.getHeight() - height) / 2;
        return new Point(x, y);
    }
    /**
     * **
     *
     * @param com
     * @return
     */
    public Point returnCenterPoint_JComponent(JComponent com) {
        Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        int x = 0;
        int y = 0;
        x = (int) ((d.getWidth() - com.getWidth()) / 2);
        y = (int) (d.getHeight() - com.getHeight()) / 2;
        return new Point(x, y);
    }
    /**
     * **
     * 根据表格列的内容自动调整列宽度
     *
     * @param myTable
     */
    public static void fitTableColumns(JTable myTable) {
        myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JTableHeader header = myTable.getTableHeader();
        int rowCount = myTable.getRowCount();
        Enumeration columns = myTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
            int width = (int) header.getDefaultRenderer().getTableCellRendererComponent(myTable, column.getIdentifier(), false, false, -1, col).getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) myTable.getCellRenderer(row, col).getTableCellRendererComponent(myTable, myTable.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }
            header.setResizingColumn(column); // 此行很重要
            column.setWidth(width + myTable.getIntercellSpacing().width);
        }
    }
    public static String formatWindowsPath(String path) {
        return path.replaceAll("/", File.separator);
    }
    public static void decompileClassFolder(String scr, String clas) {
        String command = "cmd /k start script/jad.exe -f -ff -o -r -safe -stat -sjava -d" + formatWindowsPath(scr) + " " + formatWindowsPath(clas) + "**/*.class";
//        System.out.println("Windows :Invoking " + command);
        try {
            Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * **
     *
     * @param path
     */
    public static void decompileSingleClassFile(String path) {
        String command = "cmd /k start script/jad.exe -f -ff -o -r -safe -sjava -dD:\\decompileTest\\lukeall-0.9.9Lucene-Index-Toolbox-Demo D:\\decompileTest\\lukeall-0.9.9Lucene-Index-Toolbox.jar\\org\\jets3t\\service\\Jets3tProperties.class";
//        System.out.println("Windows :Invoking " + command);
        try {
            Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * **
     *
     * @param Table
     */
    public static void setTableRowColor(JTable Table, final int index) {
        try {
            DefaultTableCellRenderer defaultcellrender = new DefaultTableCellRenderer() {
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelectd, boolean hasFocus, int row, int column) {
                    Component cell = super.getTableCellRendererComponent(table, value, isSelectd, hasFocus, row, column);
                    Object obj = table.getValueAt(row, index);
                    String temp = obj.toString();
                    if (temp.endsWith(".java")) {
                        cell.setBackground(Color.green);
                        return cell;
                    }
                    if (temp.endsWith(".class")) {
                        cell.setBackground(Color.PINK);
                        return cell;
                    }
                    if (isOneOfTheFileType(temp)) {
//                    if (temp.endsWith(".txt") || temp.endsWith(".xml") || temp.endsWith(".properties") || temp.endsWith(".wsdl")||temp.endsWith(".html")||temp.endsWith(".dtd")||temp.endsWith(".xsd")||temp.endsWith(".jsp")) {
                        cell.setBackground(Color.yellow);
                        return cell;
                    } else {
                        cell.setBackground(Color.ORANGE);
                        return cell;
                    }
                }
            };
            for (int i = 0; i < Table.getColumnCount(); i++) {
                Table.getColumnModel().getColumn(i).setCellRenderer(defaultcellrender);
            }
        } catch (Exception E) {
            E.printStackTrace();
        }
    }
    /**
     * **
     *
     * @param Table
     */
    public static void setTableRowColor_basedOnIndex(JTable Table) {
        try {
            DefaultTableCellRenderer defaultcellrender = new DefaultTableCellRenderer() {
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelectd, boolean hasFocus, int row, int column) {
                    Component cell = super.getTableCellRendererComponent(table, value, isSelectd, hasFocus, row, column);
                    if (row % 2 == 0) {
                        cell.setBackground(Color.pink);
                        return cell;
                    } else {
                        cell.setBackground(Color.ORANGE);
                        return cell;
                    }
                }
            };
            for (int i = 0; i < Table.getColumnCount(); i++) {
                Table.getColumnModel().getColumn(i).setCellRenderer(defaultcellrender);
            }
        } catch (Exception E) {
            E.printStackTrace();
        }
    }
    /**
     * **
     *
     * @param textarea
     */
    public static void goToFirstLine(JTextArea textarea) {
        textarea.setCaretPosition(0);
        textarea.requestFocus();
    }
    /**
     * **
     *
     * @param textpane
     */
    public static void goToFirstLine(JTextPane textpane) {
        textpane.setCaretPosition(0);
        textpane.requestFocus();
    }
    /**
     * **
     *
     * @param textarea
     */
    public static void goToLastLine(JTextArea textarea) {
        textarea.selectAll();
        textarea.setCaretPosition(textarea.getText()
                .length());
        textarea.requestFocus();
    }
    /**
     * **
     *
     * @param inputJarFileName
     * @return
     */
    public static String removeBlankCharForJarFileName(String inputJarFileName) {
        return inputJarFileName.replaceAll("\\s", "-");
    }
    /**
     * **
     *
     * @param path
     * @return
     */
    public static boolean isFolderEmptyOrNotExist(String path) {
        File temp = new File(path);
//        if (temp.exists() && temp.getTotalSpace() > 0) {
        if (temp.exists()) {
            return false;
        } else {
            return true;
        }
    }
    /**
     * **
     *
     * @param textpane
     */
    public static void goToLastLine(JTextPane textpane) {
        textpane.selectAll();
        textpane.setCaretPosition(textpane.getSelectedText()
                .length());
        textpane.requestFocus();
    }
    /**
     * **
     *
     * @param area
     * @param keyword
     */
    public void hightlineKeyword(JTextArea area, String keyword) {
        String content = area.getText();
        int index = content.indexOf(keyword);
        if (index != -1) {
            area.getHighlighter().removeAllHighlights();
            try {
                while (index != -1 & !keyword.equals("")) {
                    area.getHighlighter().addHighlight(index, index + keyword.length(), DefaultHighlighter.DefaultPainter);
                    index = content.indexOf(keyword, index + keyword.length());
                }
            } catch (BadLocationException ex) {
                ex.printStackTrace();
            }
        } else {
//            area.getHighlighter().removeAllHighlights();
            JOptionPane.showMessageDialog(StartUp.getInstance(), "没有找到关键字:" + keyword);
        }
    }
    /**
     * **
     *
     * @param total
     * @param filesPerThread
     * @return
     */
    public static int[] getSplittedTableSets(int total) {
        //每一个线程处理filesPerThread个文件
        int a = total % filesPerThread;//取余数
        int b = total / filesPerThread;//去除数
        System.out.println("除数:" + b + " 余数:" + a);
        int[] intArray = {b, a};
        return intArray;
    }
// 45    0 45  -->0 45 
// 59    0 49  50 59   -->1 9
// 112   0 49  50 99   100 112   -->2  12   
// 167   0 49  50 99   100 149   150 167   -->3 17
// 223   0 49  50 99   100 149   150 199  200 223   -->4  23 
    public static void showSegments(int toal) {
        int[] sets = CommonUtil.getInstance().getSplittedTableSets(toal);
        int startindex = 0;
        int endindex = 0;
        for (int a = 0; a < (sets[0] + 1); a++) {
            startindex = a * CommonUtil.filesPerThread;//这个是固定的index*50     
            if (sets[0] == 0) {//如果只有一个结果集,而且是头一个
                endindex = sets[1];
                System.out.println("startindex=" + startindex + " endindex=" + endindex);
            } else if (sets[0] > 0) {//more than one sets
                if (a == 0) {//the first one
                    endindex = (a + 1) * CommonUtil.filesPerThread - 1;
                    System.out.println("startindex=" + startindex + " endindex=" + endindex);
                } else if (a == sets[0]) {//the last one
                    endindex = sets[0] * CommonUtil.filesPerThread + sets[1];
                    System.out.println("startindex=" + startindex + " endindex=" + endindex);
                } else {
                    endindex = (a + 1) * CommonUtil.filesPerThread - 1;
                    System.out.println("startindex=" + startindex + " endindex=" + endindex);
                }
            }
        }
    }
    public static void main(String[] args) {
//        CommonUtil.getInstance().getSplittedTableSets(48);
        CommonUtil.getInstance().showSegments(435);
    }
}
上一篇:CS-DecompressJar
下一篇:空心菱形2
网友评论