Java JTable Header removeBadChars(JTable table, boolean skipHeaderBoolean)

Here you can find the source of removeBadChars(JTable table, boolean skipHeaderBoolean)

Description

remove Bad Chars

License

Open Source License

Declaration

public static void removeBadChars(JTable table, boolean skipHeaderBoolean) throws Exception 

Method Source Code

//package com.java2s;
/*/*from www . j a  va 2  s. c  o m*/
 * Id: 
 *
 * Copyright (C) 2004, Cladonia Ltd. All rights reserved.
 *
 * This software is the proprietary information of Cladonia Ltd.  
 * Use is subject to license terms.
 */

import java.util.Vector;
import javax.swing.JTable;

public class Main {
    public static String[] badChars = { "<", ">", "=", ",", "\"", "&", "[", "]" };
    public static String[] goodChars = { "", "", "", "", "", "", "", "" };

    public static void removeBadChars(JTable table, boolean skipHeaderBoolean) throws Exception {
        //   for each cell in row, remove bad chars
        int columns = table.getColumnCount();
        int rows = table.getRowCount();
        int startRow = 0;

        cleanHeaderCells(table);
        startRow = 1;

        for (int rCnt = startRow; rCnt < rows; ++rCnt) {
            //      for each row
            for (int cCnt = 0; cCnt < columns; ++cCnt) {
                //         for each column in a row
                if (table.getValueAt(rCnt, cCnt) == null) {
                    //                  set this cell to ""
                    table.setValueAt("", rCnt, cCnt);
                } else {
                    String cellValue = (String) table.getValueAt(rCnt, cCnt);

                    if (cellValue.length() > 0) {

                        String newCellValue = removeBadCharsFromString(cellValue, skipHeaderBoolean);
                        if (!cellValue.equals(newCellValue)) {

                            table.setValueAt(newCellValue, rCnt, cCnt);
                        }
                    } else {
                        //set this cell to ""
                        table.setValueAt("", rCnt, cCnt);
                    }
                }

            }
        }
    }

    public static Vector removeBadChars(Vector data, boolean skipHeaderBoolean) throws Exception {

        //   for each cell in row, remove bad chars
        int rows = data.size();
        int columns = ((String[]) data.get(0)).length;

        for (int rCnt = 0; rCnt < rows; ++rCnt) {
            //      for each row

            boolean rowWasChanged = false;
            String[] rowData = (String[]) data.get(rCnt);
            for (int cCnt = 0; cCnt < columns; ++cCnt) {
                //         for each column in a row
                String cellValue = rowData[cCnt];
                if (cellValue.length() > 0) {
                    String newCellValue = removeBadCharsFromString(cellValue, skipHeaderBoolean);

                    if (!cellValue.equals(newCellValue)) {
                        rowWasChanged = true;
                        rowData[cCnt] = newCellValue;
                    }
                } else {
                    //set this cell to ""
                    rowWasChanged = true;
                    rowData[cCnt] = "";
                }

            }
            if (rowWasChanged) {
                data.set(rCnt, rowData);
            }
        }

        return (data);
    }

    private static void cleanHeaderCells(JTable table) {

        for (int cCnt = 0; cCnt < table.getColumnCount(); ++cCnt) {
            //         for each column in a row
            if (table.getValueAt(0, cCnt) == null) {
                //              set this cell to ""
                table.setValueAt("", 0, cCnt);
            } else {
                String cellValue = (String) table.getValueAt(0, cCnt);

                if (cellValue.length() > 0) {
                    //System.out.println(cellValue);

                    for (int cnt = 0; cnt < badChars.length; ++cnt) {
                        //check if cellValue contains any of the bad chars
                        int indexValue = -1;
                        try {
                            indexValue = cellValue.indexOf(badChars[cnt]);
                            if (cellValue.indexOf(badChars[cnt]) != -1) {
                                //change char
                                //System.out.println(badChars[cnt]+":"+goodChars[cnt]);
                                cellValue = cellValue.replaceAll("\\" + badChars[cnt], goodChars[cnt]);
                                table.setValueAt(cellValue, 0, cCnt);
                            }
                        } catch (NullPointerException e) {
                            System.out.println("null");
                        }
                    }
                }
            }
        }

    }

    private static String removeBadCharsFromString(String cellValue, boolean skipBoolean) throws Exception {

        /*for (int cnt = 0; cnt < badChars.length; ++cnt) {
         //check if cellValue contains any of the bad chars
          int indexValue = -1;
          try {
          indexValue = cellValue.indexOf(badChars[cnt]);
          if (cellValue.indexOf(badChars[cnt]) != -1) {
          //change char
           //System.out.println(badChars[cnt]+":"+goodChars[cnt]);
        cellValue = cellValue.replaceAll("\\"+badChars[cnt],goodChars[cnt]);
        }
        } catch (NullPointerException e) {
        System.out.println("null");
        }
        }*/

        return (substituteSelectedCharacters(cellValue, skipBoolean));
    }

    /**
     * Substitutes the selected characters with entities.
     */
    public static String substituteSelectedCharacters(String text, boolean skip) {

        if (text != null) {

            StringBuffer newText = new StringBuffer();

            for (int i = 0; i < text.length(); i++) {
                char character = text.charAt(i);

                if (character == '<') {
                    if (skip)
                        newText.append("&lt;");
                    else
                        newText.append((char) character);

                } else if (character == '>') {
                    if (skip)
                        newText.append("&gt;");
                    else
                        newText.append((char) character);
                } else if (character == '&') {
                    if (skip) {
                        newText.append("&amp;");
                    } else {
                        newText.append((char) character);
                    }

                } else if (character == '\'') {
                    if (skip)
                        newText.append("&apos;");
                    else
                        newText.append((char) character);

                } else if (character == '\"') {
                    if (skip)
                        newText.append("&quot;");
                    else
                        newText.append((char) character);

                    /*} else if (  character > 127) {
                    if(!skip) {
                    String name = CommonEntities.getEntityName( character);
                        
                    try {
                    if ( !name.equals( "lt") && !name.equals( "gt") && !name.equals( "amp") && !name.equals( "apos") && !name.equals( "quot")) {
                        newText.append( "&");
                        newText.append( name);
                        newText.append( ";");
                    }
                    }
                    catch (NullPointerException e) {
                    //problem with converting char to entity
                    //just use that char
                    newText.append((char)character);
                    }
                    }
                    else {
                    newText.append( (char)character);
                    }*/
                } else {
                    newText.append((char) character);
                }
            }

            return (newText.toString());
        } else {
            return (text);
        }

    }
}

Related

  1. layoutHeaders(JTable p_Table)
  2. makeHeaderFillEmptySpace(JTable table)
  3. makeTableHeadersCentered(final JTable table, final boolean dataColsAlso)
  4. modifyTableHeader(JTableHeader head, Vector columnNames)
  5. paintHeader(Graphics graphics, JTable table, int x, int width)
  6. setHeaderIcon(JTable table, final int column, final ImageIcon icon)
  7. setOptimalHeaderWidth(int col)
  8. setPressedColumn(JTableHeader tableHeader, int columnModelIndex)
  9. setTableHeaderCellRenderer(TableColumn tableCol)