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:org.isatools.isacreator.spreadsheet.SpreadsheetFunctions.java

public Map<TableColumn, TableColumn> getFactors() {
    Map<TableColumn, TableColumn> factors = new HashMap<TableColumn, TableColumn>();

    Enumeration<TableColumn> tableColumns = spreadsheet.getTable().getColumnModel().getColumns();

    while (tableColumns.hasMoreElements()) {
        TableColumn column = tableColumns.nextElement();
        if (column.getHeaderValue().toString().contains("Factor Value")) {

            if (spreadsheet.columnDependencies.containsKey(column)) {
                for (TableColumn unitColumn : spreadsheet.columnDependencies.get(column)) {
                    factors.put(column, unitColumn);
                    break;
                }/*from  w ww  .j  a v  a2s  .c  o m*/

            } else {
                factors.put(column, null);
            }
        }
    }

    return factors;
}

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

/**
 * Remove a column from a spreadsheet.getTable() given a column name
 *
 * @param colName - Name of column to be removed.
 *//*  w  ww . j av  a 2s.c  o  m*/
public void removeColumnByName(String colName) {
    Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns();

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

        if (col.getHeaderValue().toString().equals(colName)) {
            spreadsheet.curColDelete = Utils.convertModelIndexToView(spreadsheet.getTable(),
                    col.getModelIndex());
            removeColumn();

            removeDependentColumns(col);
        }
    }

    ((SpreadsheetModel) spreadsheet.getTable().getModel()).fireTableStructureChanged();
}

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

/**
 * Check to see if a column with a given name exists.
 * Result is always false if the column allows multiple values.
 *
 * @param colName name of column to check for.
 * @return true if it exists, false otherwise.
 *//*from  w ww  .j a  v a 2s.co m*/
public boolean checkColumnExists(String colName) {
    Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns();
    // if the column can be referenced multiple times, then we should return false in this check.
    System.out.println(colName);
    if (colName != null) {
        if (!spreadsheet.getTableReferenceObject().acceptsMultipleValues(colName)) {
            while (columns.hasMoreElements()) {
                TableColumn col = columns.nextElement();

                if (col.getHeaderValue().toString().equals(colName)) {
                    return true;
                }
            }
        }
    }

    return false;
}

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

/**
 * Check to see if a column with a given name exists.
 * Result is always false if the column allows multiple values.
 *
 * @param colName name of column to check for.
 * @return true if it exists, false otherwise.
 *//*from www.ja  v a2 s  .  com*/
public int getModelIndexForColumn(String colName) {
    Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns();
    // if the column can be referenced multiple times, then we should return false in this check.

    if (colName != null) {
        if (!spreadsheet.getTableReferenceObject().acceptsMultipleValues(colName)) {
            while (columns.hasMoreElements()) {
                TableColumn col = columns.nextElement();

                if (col.getHeaderValue().toString().equalsIgnoreCase(colName)) {
                    return col.getModelIndex();
                }
            }
        }
    }

    return -1;
}

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

/**
 * Recovers cell editor for a field for attachment to a column
 *
 * @param col - Column to attach a custom cell editor to
 *///www  .ja v  a 2 s  .c  o m
@SuppressWarnings({ "ConstantConditions" })
protected void addCellEditor(TableColumn col, String previousColumnName) {
    ValidationObject vo = spreadsheet.getTableReferenceObject()
            .getValidationConstraints(col.getHeaderValue().toString());
    DataTypes classType = spreadsheet.getTableReferenceObject().getColumnType(col.getHeaderValue().toString());

    String columnName = col.getHeaderValue().toString();

    PluginSpreadsheetWidget widget;

    if ((widget = SpreadsheetPluginRegistry.findPluginForColumn(columnName)) != null) {
        TableCellEditor editor = (TableCellEditor) widget;
        col.setCellEditor(editor);
        return;
    }

    if (vo != null && classType == DataTypes.STRING) {
        StringValidation sv = ((StringValidation) vo);
        col.setCellEditor(new StringEditor(sv));
        return;
    }

    if (columnName.equals("Sample Name") && !spreadsheet.getSpreadsheetTitle().contains("Sample Definitions")
            && spreadsheet.getStudyDataEntryEnvironment() != null) {

        if (sampleSelectorCellEditor == null) {
            sampleSelectorCellEditor = new SampleSelectorCellEditor(spreadsheet);
        }

        col.setCellEditor(sampleSelectorCellEditor);

        return;
    }

    if (columnName.equals("Protocol REF") && spreadsheet.getStudyDataEntryEnvironment() != null) {
        if (protocolSelectorCellEditor == null) {
            protocolSelectorCellEditor = new ProtocolSelectorCellEditor(spreadsheet);
        }
        col.setCellEditor(protocolSelectorCellEditor);
        return;
    }

    if (spreadsheet.getTableReferenceObject().getClassType(columnName) == DataTypes.ONTOLOGY_TERM) {

        Map<String, RecommendedOntology> recommendedOntologyMap = null;
        if (columnName.equalsIgnoreCase("unit")) {
            // we should be keeping note of the previous value, and automatically link this up with the unit.
            // If no link, is found, the fall back is to use no recommended ontology for that field.
            if (previousColumnName != null) {
                FieldObject unitField = spreadsheet.getTableReferenceObject()
                        .getNextUnitField(previousColumnName);
                if (unitField != null) {
                    recommendedOntologyMap = unitField.getRecommmendedOntologySource();
                }
            }
        } else {
            recommendedOntologyMap = spreadsheet.getTableReferenceObject().getRecommendedSource(columnName);
        }

        col.setCellEditor(new OntologyCellEditor(
                spreadsheet.getTableReferenceObject().acceptsMultipleValues(columnName),
                spreadsheet.getTableReferenceObject().forceOntology(columnName), recommendedOntologyMap));
        return;
    }

    if (spreadsheet.getTableReferenceObject().getClassType(columnName) == DataTypes.LIST) {
        if (columnName.equalsIgnoreCase("unit")) {
            FieldObject unitField = spreadsheet.getTableReferenceObject().getNextUnitField(previousColumnName);
            if (unitField != null) {
                System.out.println(Arrays.toString(unitField.getFieldList()));

                col.setCellEditor(
                        new AutoFilterComboCellEditor(new AutoFilterCombo(unitField.getFieldList(), false)));
            }
        } else {
            col.setCellEditor(new AutoFilterComboCellEditor(new AutoFilterCombo(
                    spreadsheet.getTableReferenceObject().getListItems(columnName), false)));

        }
        return;
    }

    if (spreadsheet.getTableReferenceObject().getClassType(columnName) == DataTypes.DATE) {
        col.setCellEditor(Spreadsheet.dateEditor);

        return;
    }

    if (spreadsheet.getTableReferenceObject().getClassType(columnName) == DataTypes.BOOLEAN) {
        col.setCellEditor(new StringEditor(
                new StringValidation("true|yes|TRUE|YES|NO|FALSE|no|false", "not a valid boolean!"), true));
        return;
    }

    if ((classType == DataTypes.STRING)
            && spreadsheet.getTableReferenceObject().acceptsFileLocations(columnName)) {
        col.setCellEditor(Spreadsheet.fileSelectEditor);
        return;
    }

    if (classType == DataTypes.INTEGER) {
        col.setCellEditor(
                new StringEditor(new StringValidation("[0-9]+", "Please enter an integer value!"), true));
        return;
    }

    if (classType == DataTypes.DOUBLE) {
        col.setCellEditor(new StringEditor(
                new StringValidation("[0-9]+[.]{0,1}[0-9]*", "Please enter a double value!"), true));
        return;
    }

    col.setCellEditor(new StringEditor(new StringValidation(".*", "")));
}

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

/**
 * Delete column given an index.//from w w w.j a  v  a2s .c  o  m
 *
 * @param vColIndex - column to remove.
 */
protected void deleteColumn(int vColIndex) {
    spreadsheet.curColDelete = vColIndex;
    spreadsheet.currentState = Spreadsheet.DELETING_COLUMN;

    TableColumn col = spreadsheet.getTable().getColumnModel().getColumn(spreadsheet.curColDelete);

    if (!col.getHeaderValue().toString().equals(TableReferenceObject.ROW_NO_TEXT)) {
        if (spreadsheet.getTableReferenceObject().isRequired(col.getHeaderValue().toString())
                && !areMultipleOccurences(col.getHeaderValue().toString())) {
            spreadsheet.optionPane = new JOptionPane(
                    "<html>This column can not be deleted due to it being a required field in this assay!</html>",
                    JOptionPane.OK_OPTION);
            spreadsheet.optionPane.setIcon(spreadsheet.requiredColumnWarningIcon);
            UIHelper.applyOptionPaneBackground(spreadsheet.optionPane, UIHelper.BG_COLOR);

            spreadsheet.optionPane.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent event) {
                    if (event.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) {
                        spreadsheet.getParentFrame().hideSheet();
                    }
                }
            });

            spreadsheet.getParentFrame()
                    .showJDialogAsSheet(spreadsheet.optionPane.createDialog(spreadsheet, "Can not delete"));
        } else {
            spreadsheet.optionPane = new JOptionPane(
                    "<html>Are you sure you want to delete this column? <p>This Action can not be undone!</p></html>",
                    JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_OPTION,
                    spreadsheet.confirmRemoveColumnIcon);
            UIHelper.applyOptionPaneBackground(spreadsheet.optionPane, UIHelper.BG_COLOR);
            spreadsheet.optionPane.addPropertyChangeListener(spreadsheet);
            spreadsheet.getParentFrame().showJDialogAsSheet(
                    spreadsheet.optionPane.createDialog(spreadsheet, "Confirm Delete Column"));
        }
    }
}

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

protected boolean areMultipleOccurences(String colName) {
    Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns();

    int count = 0;
    while (columns.hasMoreElements()) {
        TableColumn col = columns.nextElement();

        if (col.getHeaderValue().equals(colName)) {
            count++;/*w  w  w.ja  v  a  2  s  .  c o m*/
            if (count > 1) {
                return true;
            }
        }
    }

    return false;
}

From source file:org.lockss.devtools.plugindef.EDPInspectorTableModel.java

public void setColumnSize(JTable table, int col) {
    TableColumn column = null;
    Component comp = null;/*w  ww  .j  av  a  2  s. com*/
    int headerWidth = 0;
    int cellWidth = 0;
    String longestStr = "";
    String curString = "";

    for (int row = 0; row < inspectorEntries.length; row++) {
        curString = data[row][col].toString();
        if (curString.length() > longestStr.length()) {
            longestStr = curString;
        }
    }
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();

    column = table.getColumnModel().getColumn(col);

    comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
    headerWidth = comp.getPreferredSize().width;

    comp = table.getDefaultRenderer(getColumnClass(col)).getTableCellRendererComponent(table, longestStr, false,
            false, 0, col);
    cellWidth = comp.getPreferredSize().width;

    column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}

From source file:org.pentaho.reporting.ui.datasources.table.TableEditor.java

public void addColumn(final TableColumn column) {
    stopEditing();/*from w w  w.jav a 2  s. co  m*/
    if (column.getHeaderValue() == null) {
        final int modelColumn = column.getModelIndex();
        final String columnName = getModel().getColumnName(modelColumn);
        if (modelColumn == 0) {
            column.setResizable(false);
            column.setHeaderValue(columnName);
            column.setPreferredWidth(30);
            column.setMaxWidth(30);
            column.setMinWidth(30);
        } else {
            final Class columnType = getModel().getColumnClass(modelColumn);
            column.setHeaderValue(new TypedHeaderInformation(columnType, columnName));
        }
    }
    getColumnModel().addColumn(column);

}

From source file:org.yccheok.jstock.gui.JTableUtilities.java

public static void makeTableColumnWidthFit(JTable jTable, int col, int margin, boolean locking) {
    // strategy - get max width for cells in column and
    // make that the preferred width
    TableColumnModel columnModel = jTable.getColumnModel();
    int maxwidth = 0;

    for (int row = 0; row < jTable.getRowCount(); row++) {
        TableCellRenderer rend = jTable.getCellRenderer(row, col);
        Object value = jTable.getValueAt(row, col);
        Component comp = rend.getTableCellRendererComponent(jTable, value, false, false, row, col);

        maxwidth = Math.max(comp.getPreferredSize().width + margin, maxwidth);
    } // for row//from ww  w.  j  a  v  a  2  s .c om

    TableColumn column = columnModel.getColumn(col);
    TableCellRenderer headerRenderer = column.getHeaderRenderer();
    if (headerRenderer == null) {
        headerRenderer = jTable.getTableHeader().getDefaultRenderer();
    }
    Object headerValue = column.getHeaderValue();
    Component headerComp = headerRenderer.getTableCellRendererComponent(jTable, headerValue, false, false, 0,
            col);

    maxwidth = Math.max(maxwidth, headerComp.getPreferredSize().width + margin);
    column.setPreferredWidth(maxwidth);
    if (locking) {
        // User will not able to adjust the width manually.
        column.setMinWidth(maxwidth);
        column.setMaxWidth(maxwidth);
    }
}