Example usage for javax.swing.table TableColumn getHeaderValue

List of usage examples for javax.swing.table TableColumn getHeaderValue

Introduction

In this page you can find the example usage for javax.swing.table TableColumn getHeaderValue.

Prototype

public Object getHeaderValue() 

Source Link

Document

Returns the Object used as the value for the header renderer.

Usage

From source file:op.tools.SYSTools.java

public static void packColumn(JTable table, int vColIndex, int margin) {
    TableModel model = table.getModel();
    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width = 0;

    // Get width of column header
    TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }//from www .j a  va 2s  . c  om
    Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
    width = comp.getPreferredSize().width;

    // Get maximum width of column data
    for (int r = 0; r < table.getRowCount(); r++) {
        renderer = table.getCellRenderer(r, vColIndex);
        comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r,
                vColIndex);
        width = Math.max(width, comp.getPreferredSize().width);
    }

    // Add margin
    width += 2 * margin;
    // Set the width
    col.setPreferredWidth(width);

    OPDE.debug("packColumn/3: col=" + vColIndex + "  width=" + width);
}

From source file:org.apache.cayenne.swing.TableBinding.java

protected void resizeColumns(Object[] sampleLongValues) {

    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
    TableColumnModel columnModel = table.getColumnModel();
    TableModel tableModel = table.getModel();

    for (int i = 0; i < columnModel.getColumnCount(); i++) {

        TableColumn column = columnModel.getColumn(i);

        Component header = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false,
                false, 0, 0);//from  w w w .ja  v  a  2 s. c o m
        int headerWidth = header.getPreferredSize().width;

        if (sampleLongValues[i] != null) {
            Component bigCell = table.getDefaultRenderer(tableModel.getColumnClass(i))
                    .getTableCellRendererComponent(table, sampleLongValues[i], false, false, 0, i);
            int cellWidth = bigCell.getPreferredSize().width;
            column.setPreferredWidth(Math.max(headerWidth, cellWidth));
        } else {
            column.setPreferredWidth(headerWidth);
        }
    }
}

From source file:org.isatools.isacreator.api.utils.SpreadsheetUtils.java

/**
 * Gets the freetext terms (which ideally should be ontology terms) in a Spreadsheet object
 *
 * @param spreadsheet @see Spreadsheet/* w  w w. j a  v  a  2 s.c  om*/
 * @return Map<Column Name, Set<Column Values>>
 */
public static Map<String, Set<String>> getFreetextInSpreadsheet(Spreadsheet spreadsheet) {
    Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns();

    Map<String, Set<String>> columnToFreeText = new HashMap<String, Set<String>>();

    while (columns.hasMoreElements()) {
        TableColumn tc = columns.nextElement();

        if (spreadsheet.getTableReferenceObject()
                .getClassType(tc.getHeaderValue().toString().trim()) == DataTypes.ONTOLOGY_TERM) {

            int colIndex = Utils.convertModelIndexToView(spreadsheet.getTable(), tc.getModelIndex());

            for (int row = 0; row < spreadsheet.getTable().getRowCount(); row++) {

                String columnValue = (spreadsheet.getTable().getValueAt(row, colIndex) == null) ? ""
                        : spreadsheet.getTable().getValueAt(row, colIndex).toString();

                if (columnValue != null && !columnValue.trim().equals("") && !columnValue.contains(":")) {
                    if (!columnToFreeText.containsKey(tc.getHeaderValue().toString())) {
                        columnToFreeText.put(tc.getHeaderValue().toString(), new HashSet<String>());
                    }
                    columnToFreeText.get(tc.getHeaderValue().toString()).add(columnValue);
                }
            }
        }
    }

    return columnToFreeText;
}

From source file:org.isatools.isacreator.api.utils.SpreadsheetUtils.java

public static void replaceFreeTextWithOntologyTerms(Spreadsheet spreadsheet,
        Map<String, OntologyTerm> annotations) {
    Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns();

    while (columns.hasMoreElements()) {
        TableColumn tc = columns.nextElement();

        if (spreadsheet.getTableReferenceObject()
                .getClassType(tc.getHeaderValue().toString().trim()) == DataTypes.ONTOLOGY_TERM) {
            int colIndex = Utils.convertModelIndexToView(spreadsheet.getTable(), tc.getModelIndex());

            for (int row = 0; row < spreadsheet.getTable().getRowCount(); row++) {

                String columnValue = (spreadsheet.getTable().getValueAt(row, colIndex) == null) ? ""
                        : spreadsheet.getTable().getValueAt(row, colIndex).toString();

                if (annotations.containsKey(columnValue)) {
                    spreadsheet.getTable().setValueAt(annotations.get(columnValue).getShortForm(), row,
                            colIndex);//from   w  w w.j av  a 2 s .  c o m
                }
            }
        }
    }
}

From source file:org.isatools.isacreator.api.utils.SpreadsheetUtils.java

/**
 * Method returns a Set of all the files defined in a spreadsheet. These locations are used to zip up the data files
 * in the ISArchive for submission to the index.
 *
 * @return Set of files defined in the spreadsheet
 *//*from   w  ww . j av  a2s  .  c o  m*/
public static Set<String> getFilesDefinedInTable(Spreadsheet spreadsheet) {
    Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns();
    Set<String> files = new HashSet<String>();

    while (columns.hasMoreElements()) {
        TableColumn tc = columns.nextElement();

        if (spreadsheet.getTableReferenceObject().acceptsFileLocations(tc.getHeaderValue().toString())) {
            int colIndex = Utils.convertModelIndexToView(spreadsheet.getTable(), tc.getModelIndex());

            for (int row = 0; row < spreadsheet.getTable().getRowCount(); row++) {
                String s = (spreadsheet.getTable().getValueAt(row, colIndex) == null) ? ""
                        : spreadsheet.getTable().getValueAt(row, colIndex).toString();

                if (s != null && !s.trim().equals("")) {
                    files.add(s);
                }
            }
        }
    }

    return files;
}

From source file:org.isatools.isacreator.api.utils.SpreadsheetUtils.java

/**
 * Given a spreadsheet and a set of values to find, will return a set of those
 * values found within the spreadsheet object
 *
 * @param spreadsheet - Spreadsheet to interrogate
 * @param values      - Set of values to be searched for.
 * @param fieldFocus  - target search on specific field types, e.g. Protocol REF, Characteristics and so forth.
 * @return Set<String> containing the values that were found.
 *///from   ww w .  j a v  a2s . c om
public static Set<String> findValueInSheet(Spreadsheet spreadsheet, Set<String> values,
        Set<String> fieldFocus) {
    Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns();

    Set<String> foundValues = new HashSet<String>();

    while (columns.hasMoreElements()) {
        TableColumn tc = columns.nextElement();

        boolean headerContained = fieldFocus.isEmpty() || fieldFocus.contains(tc.getHeaderValue().toString());

        if (headerContained) {
            int colIndex = Utils.convertModelIndexToView(spreadsheet.getTable(), tc.getModelIndex());

            for (int row = 0; row < spreadsheet.getTable().getRowCount(); row++) {
                String columnValue = (spreadsheet.getTable().getValueAt(row, colIndex) == null) ? ""
                        : spreadsheet.getTable().getValueAt(row, colIndex).toString();

                if (values.contains(columnValue)) {
                    foundValues.add(columnValue);
                }

                // end early if possible
                if (foundValues.size() == values.size()) {
                    return foundValues;
                }
            }
        }
    }
    return foundValues;
}

From source file:org.isatools.isacreator.api.utils.SpreadsheetUtils.java

public static Set<String> findValuesForColumnInSpreadsheet(Spreadsheet spreadsheet, String type) {

    Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns();
    Set<String> values = new HashSet<String>();
    while (columns.hasMoreElements()) {
        TableColumn tc = columns.nextElement();

        boolean isTargetFieldType = tc.getHeaderValue().toString().contains(type);

        if (isTargetFieldType) {
            int colIndex = Utils.convertModelIndexToView(spreadsheet.getTable(), tc.getModelIndex());

            for (int row = 0; row < spreadsheet.getTable().getRowCount(); row++) {
                String columnValue = (spreadsheet.getTable().getValueAt(row, colIndex) == null) ? ""
                        : spreadsheet.getTable().getValueAt(row, colIndex).toString();
                values.add(columnValue);
            }/*from   www.j a  v a2 s. com*/
        }
    }
    return values;
}

From source file:org.isatools.isacreator.spreadsheet.Spreadsheet.java

public String getColValAtRow(String colName, int rowNumber) {
    for (int col = 1; col < table.getColumnCount(); col++) {
        TableColumn column = table.getColumnModel().getColumn(col);

        if (column.getHeaderValue().toString().equalsIgnoreCase(colName)) {
            // safety precaution to finalise any cells. otherwise their value would be missed!
            if (table.getCellEditor(rowNumber, col) != null) {
                table.getCellEditor(rowNumber, col).stopCellEditing();
            }// w  ww  . j av  a  2  s.  c  o m
            return table.getValueAt(rowNumber, col).toString();
        }
    }
    return "";
}

From source file:org.isatools.isacreator.spreadsheet.Spreadsheet.java

/**
 * Groups up types of columns such as factors and returns the concatenation of those values on a per row basis.
 *
 * @param group             - e.g. Factor, Characterisitc
 * @param exactMatch        - if the Group should be an exact match to a table value (e.g. Factor could be any factor but Factor Value[Run Time] would be a perfect match.)
 * @param returnSampleNames - return the sample names or return a list of row indexes corresponding to a group. True to return the sample names, false otherwise!
 * @return Map<String, List<Object>> where list will be Strings if return Sample Names or Row indexes
 *///www . ja  va 2 s  . co  m
public Map<String, List<Object>> getDataGroupsByColumn(String group, boolean exactMatch,
        boolean returnSampleNames) {
    Map<String, List<Object>> groups = new ListOrderedMap<String, List<Object>>();

    boolean allowedUnit = false;
    for (int row = 0; row < spreadsheetModel.getRowCount(); row++) {
        String groupVal = "";
        for (int col = 1; col < table.getColumnCount(); col++) {
            TableColumn column = table.getColumnModel().getColumn(col);

            boolean match = false;

            if (exactMatch) {
                if (column.getHeaderValue().toString().equalsIgnoreCase(group)) {
                    match = true;
                } else if (allowedUnit && column.getHeaderValue().toString().equalsIgnoreCase("unit")) {
                    match = true;
                }
            } else {
                if (column.getHeaderValue().toString().contains(group)) {
                    match = true;
                } else if (allowedUnit && column.getHeaderValue().toString().equalsIgnoreCase("unit")) {
                    match = true;
                }
            }

            if (match) {
                // safety precaution to finalise any cells. otherwise their value would be missed!
                if (table.getCellEditor(row, col) != null) {
                    try {
                        table.getCellEditor(row, col).stopCellEditing();
                    } catch (Exception e) {
                        // ignore error...
                    }
                }
                groupVal += " " + table.getValueAt(row, col);
                allowedUnit = true;
            } else {
                allowedUnit = false;
            }
        }
        if (!groupVal.equals("")) {
            groupVal = groupVal.trim();
            if (!groups.containsKey(groupVal)) {
                groups.put(groupVal, new ArrayList<Object>());
            }

            if (returnSampleNames) {
                groups.get(groupVal).add(getColValAtRow("Sample Name", row));
            } else {
                groups.get(groupVal).add(row);
            }
        }
    }

    return groups;
}

From source file:org.isatools.isacreator.spreadsheet.Spreadsheet.java

/**
 * This method checks through the spreadsheet to determine whether or not all the required fields defined in the configuration
 * have been filled in. If they have not been filled in, an ErrorLocator is logged and returned in a List of ErrorLocator objects!
 *
 * @return returns a List (@see List) of ErrorLocator (@see ErrorLocator) objects
 * @see org.isatools.isacreator.spreadsheet.model.TableReferenceObject
 * @see org.isatools.isacreator.archiveoutput.ArchiveOutputError
 *///from w w  w.j  a v a  2s.  c o  m
public List<ArchiveOutputError> checkForCompleteness() {
    Enumeration<TableColumn> columns = table.getColumnModel().getColumns();

    List<ArchiveOutputError> archiveOutputErrors = new ArrayList<ArchiveOutputError>();

    boolean lastTermRequired = false;
    while (columns.hasMoreElements()) {
        TableColumn tc = columns.nextElement();
        int columnViewIndex = Utils.convertModelIndexToView(table, tc.getModelIndex());
        if (tableReferenceObject.isRequired(tc.getHeaderValue().toString())
                || (tc.getHeaderValue().toString().equals("Unit") && lastTermRequired)) {
            lastTermRequired = SpreadsheetUtils
                    .isFactorParameterOrCharacteristic(tc.getHeaderValue().toString());
            for (int row = 0; row < spreadsheetModel.getRowCount(); row++) {
                Object cellObj = table.getValueAt(row, columnViewIndex);
                String value = (cellObj == null) ? "" : cellObj.toString().trim();
                if (value.equals("")) {
                    // a required value has not been filled! therefore report the index of the row and column as well as the calling]
                    // location and message!
                    archiveOutputErrors.add(new ArchiveOutputError(
                            "Data missing for " + tc.getHeaderValue().toString() + " at record " + row,
                            assayDataEntryEnvironment, tc.getHeaderValue().toString(), row, columnViewIndex));
                }
            }
        }
    }
    return archiveOutputErrors;
}